blob: 78aeb4b51829505993e87d4d6f5ee0ff0c0fea47 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080023#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <math.h>
25#include <signal.h>
26#include <sys/time.h>
27#include <sys/resource.h>
28
Gloria Wang9ee159b2011-02-24 14:51:45 -080029#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <binder/IServiceManager.h>
31#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070032#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070036#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
Dima Zavinfce7a472011-04-19 22:30:36 -070038#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039#include <cutils/properties.h>
40
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
44#include "AudioMixer.h"
45#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080046#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070049#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070050#include <audio_effects/effect_ns.h>
51#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten3b21c502011-12-15 09:52:39 -080053#include <audio_utils/primitives.h>
54
Eric Laurentfeb0db62011-07-22 09:04:31 -070055#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080056
John Grossman4ff14ba2012-02-08 16:37:41 -080057#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070058
Glenn Kasten9e58b552013-01-18 15:09:48 -080059#include <media/IMediaLogService.h>
60
Glenn Kastenda6ef132013-01-10 12:31:01 -080061#include <media/nbaio/Pipe.h>
62#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070063#include <media/AudioParameter.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070064#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080065
Mathias Agopian65ab4712010-07-14 17:59:35 -070066// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
John Grossman1c345192012-03-27 14:00:17 -070068// Note: the following macro is used for extremely verbose logging message. In
69// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
70// 0; but one side effect of this is to turn all LOGV's as well. Some messages
71// are so verbose that we want to suppress them even when we have ALOG_ASSERT
72// turned on. Do not uncomment the #def below unless you really know what you
73// are doing and want to see all of the extremely verbose messages.
74//#define VERY_VERY_VERBOSE_LOGGING
75#ifdef VERY_VERY_VERBOSE_LOGGING
76#define ALOGVV ALOGV
77#else
78#define ALOGVV(a...) do { } while(0)
79#endif
Eric Laurentde070132010-07-13 04:45:46 -070080
Mathias Agopian65ab4712010-07-14 17:59:35 -070081namespace android {
82
Glenn Kastenec1d6b52011-12-12 09:04:45 -080083static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
84static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070085static const char kClientLockedString[] = "Client lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070086
Glenn Kasten58912562012-04-03 10:45:00 -070087
John Grossman4ff14ba2012-02-08 16:37:41 -080088nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080089
Eric Laurent81784c32012-11-19 14:55:58 -080090uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070091
Glenn Kasten46909e72013-02-26 09:20:22 -080092#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080093bool AudioFlinger::mTeeSinkInputEnabled = false;
94bool AudioFlinger::mTeeSinkOutputEnabled = false;
95bool AudioFlinger::mTeeSinkTrackEnabled = false;
96
97size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
98size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
99size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800100#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800101
Eric Laurent813e2a72013-08-31 12:59:48 -0700102// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
103// we define a minimum time during which a global effect is considered enabled.
104static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
105
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106// ----------------------------------------------------------------------------
107
Marco Nelissenb2208842014-02-07 14:00:50 -0800108const char *formatToString(audio_format_t format) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530109 switch (format & AUDIO_FORMAT_MAIN_MASK) {
110 case AUDIO_FORMAT_PCM:
111 switch (format) {
112 case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
113 case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
114 case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
115 case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
116 case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
117 case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
118 default:
119 break;
120 }
121 break;
Marco Nelissenb2208842014-02-07 14:00:50 -0800122 case AUDIO_FORMAT_MP3: return "mp3";
123 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
124 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
125 case AUDIO_FORMAT_AAC: return "aac";
126 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
127 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
128 case AUDIO_FORMAT_VORBIS: return "vorbis";
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530129 case AUDIO_FORMAT_OPUS: return "opus";
130 case AUDIO_FORMAT_AC3: return "ac-3";
131 case AUDIO_FORMAT_E_AC3: return "e-ac-3";
Marco Nelissenb2208842014-02-07 14:00:50 -0800132 default:
133 break;
134 }
135 return "unknown";
136}
137
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700138static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700139{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700140 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700141 int rc;
142
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700143 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
144 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
145 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
146 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700147 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700148 }
149 rc = audio_hw_device_open(mod, dev);
150 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
151 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
152 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700153 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700154 }
Eric Laurent5806b352014-05-22 12:23:26 -0700155 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700156 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
157 rc = BAD_VALUE;
158 goto out;
159 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700160 return 0;
161
162out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700163 *dev = NULL;
164 return rc;
165}
166
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167// ----------------------------------------------------------------------------
168
169AudioFlinger::AudioFlinger()
170 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800171 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800172 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700173 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800174 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800175 mMasterMute(false),
176 mNextUniqueId(1),
177 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700178 mBtNrecIsOff(false),
179 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700180 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700181 mGlobalEffectEnableTime(0),
182 mPrimaryOutputSampleRate(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700183{
Glenn Kasten949a9262013-04-16 12:35:20 -0700184 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800185 char value[PROPERTY_VALUE_MAX];
186 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
187 if (doLog) {
Glenn Kastencf515ab2014-03-14 16:01:58 -0700188 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800189 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700190
Glenn Kasten46909e72013-02-26 09:20:22 -0800191#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800192 (void) property_get("ro.debuggable", value, "0");
193 int debuggable = atoi(value);
194 int teeEnabled = 0;
195 if (debuggable) {
196 (void) property_get("af.tee", value, "0");
197 teeEnabled = atoi(value);
198 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800199 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700200 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800201 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700202 }
203 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800204 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700205 }
206 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800207 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700208 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800209#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700210}
211
212void AudioFlinger::onFirstRef()
213{
Dima Zavin799a70e2011-04-18 16:57:27 -0700214 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700215
Eric Laurent93575202011-01-18 18:39:02 -0800216 Mutex::Autolock _l(mLock);
217
Dima Zavin799a70e2011-04-18 16:57:27 -0700218 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800219 char val_str[PROPERTY_VALUE_MAX] = { 0 };
220 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
221 uint32_t int_val;
222 if (1 == sscanf(val_str, "%u", &int_val)) {
223 mStandbyTimeInNsecs = milliseconds(int_val);
224 ALOGI("Using %u mSec as standby time.", int_val);
225 } else {
226 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
227 ALOGI("Using default %u mSec as standby time.",
228 (uint32_t)(mStandbyTimeInNsecs / 1000000));
229 }
230 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231
Eric Laurent1c333e22014-05-20 10:48:17 -0700232 mPatchPanel = new PatchPanel(this);
233
Eric Laurenta4c5a552012-03-29 10:12:40 -0700234 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235}
236
237AudioFlinger::~AudioFlinger()
238{
239 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700240 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700241 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 }
243 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700244 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700245 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700247
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800248 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
249 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700250 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
251 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700253
254 // Tell media.log service about any old writers that still need to be unregistered
255 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
256 if (binder != 0) {
257 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
258 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
259 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
260 mUnregisteredWriters.pop();
261 mediaLogService->unregisterWriter(iMemory);
262 }
263 }
264
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265}
266
Eric Laurenta4c5a552012-03-29 10:12:40 -0700267static const char * const audio_interfaces[] = {
268 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
269 AUDIO_HARDWARE_MODULE_ID_A2DP,
270 AUDIO_HARDWARE_MODULE_ID_USB,
271};
272#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
273
John Grossmanee578c02012-07-23 17:05:46 -0700274AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
275 audio_module_handle_t module,
276 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700277{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700278 // if module is 0, the request comes from an old policy manager and we should load
279 // well known modules
280 if (module == 0) {
281 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
282 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
283 loadHwModule_l(audio_interfaces[i]);
284 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700285 // then try to find a module supporting the requested device.
286 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
287 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
288 audio_hw_device_t *dev = audioHwDevice->hwDevice();
289 if ((dev->get_supported_devices != NULL) &&
290 (dev->get_supported_devices(dev) & devices) == devices)
291 return audioHwDevice;
292 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700293 } else {
294 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700295 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
296 if (audioHwDevice != NULL) {
297 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700298 }
299 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700300
Dima Zavin799a70e2011-04-18 16:57:27 -0700301 return NULL;
302}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303
Glenn Kasten0f11b512014-01-31 16:18:54 -0800304void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305{
306 const size_t SIZE = 256;
307 char buffer[SIZE];
308 String8 result;
309
310 result.append("Clients:\n");
311 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800312 sp<Client> client = mClients.valueAt(i).promote();
313 if (client != 0) {
314 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
315 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 }
317 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700318
Eric Laurentd1b28d42013-09-18 18:47:13 -0700319 result.append("Notification Clients:\n");
320 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
321 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
322 result.append(buffer);
323 }
324
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700325 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800326 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700327 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
328 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800329 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700330 result.append(buffer);
331 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333}
334
335
Glenn Kasten0f11b512014-01-31 16:18:54 -0800336void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337{
338 const size_t SIZE = 256;
339 char buffer[SIZE];
340 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800341 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342
John Grossman4ff14ba2012-02-08 16:37:41 -0800343 snprintf(buffer, SIZE, "Hardware status: %d\n"
344 "Standby Time mSec: %u\n",
345 hardwareStatus,
346 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 result.append(buffer);
348 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349}
350
Glenn Kasten0f11b512014-01-31 16:18:54 -0800351void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352{
353 const size_t SIZE = 256;
354 char buffer[SIZE];
355 String8 result;
356 snprintf(buffer, SIZE, "Permission Denial: "
357 "can't dump AudioFlinger from pid=%d, uid=%d\n",
358 IPCThreadState::self()->getCallingPid(),
359 IPCThreadState::self()->getCallingUid());
360 result.append(buffer);
361 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362}
363
Eric Laurent81784c32012-11-19 14:55:58 -0800364bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365{
366 bool locked = false;
367 for (int i = 0; i < kDumpLockRetries; ++i) {
368 if (mutex.tryLock() == NO_ERROR) {
369 locked = true;
370 break;
371 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800372 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 }
374 return locked;
375}
376
377status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
378{
Glenn Kasten44deb052012-02-05 18:09:08 -0800379 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 dumpPermissionDenial(fd, args);
381 } else {
382 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800383 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 if (!hardwareLocked) {
385 String8 result(kHardwareLockedString);
386 write(fd, result.string(), result.size());
387 } else {
388 mHardwareLock.unlock();
389 }
390
Eric Laurent81784c32012-11-19 14:55:58 -0800391 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392
393 // failed to lock - AudioFlinger is probably deadlocked
394 if (!locked) {
395 String8 result(kDeadlockedString);
396 write(fd, result.string(), result.size());
397 }
398
Eric Laurent021cf962014-05-13 10:18:14 -0700399 bool clientLocked = dumpTryLock(mClientLock);
400 if (!clientLocked) {
401 String8 result(kClientLockedString);
402 write(fd, result.string(), result.size());
403 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700404
405 EffectDumpEffects(fd);
406
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700408 if (clientLocked) {
409 mClientLock.unlock();
410 }
411
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 dumpInternals(fd, args);
413
414 // dump playback threads
415 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
416 mPlaybackThreads.valueAt(i)->dump(fd, args);
417 }
418
419 // dump record threads
420 for (size_t i = 0; i < mRecordThreads.size(); i++) {
421 mRecordThreads.valueAt(i)->dump(fd, args);
422 }
423
Eric Laurentaaa44472014-09-12 17:41:50 -0700424 // dump orphan effect chains
425 if (mOrphanEffectChains.size() != 0) {
426 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
427 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
428 mOrphanEffectChains.valueAt(i)->dump(fd, args);
429 }
430 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700431 // dump all hardware devs
432 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700433 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700434 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700436
Glenn Kasten46909e72013-02-26 09:20:22 -0800437#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700438 // dump the serially shared record tee sink
439 if (mRecordTeeSource != 0) {
440 dumpTee(fd, mRecordTeeSource);
441 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800442#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700443
Glenn Kastend65d73c2012-06-22 17:21:07 -0700444 if (locked) {
445 mLock.unlock();
446 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800447
448 // append a copy of media.log here by forwarding fd to it, but don't attempt
449 // to lookup the service if it's not running, as it will block for a second
450 if (mLogMemoryDealer != 0) {
451 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
452 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700453 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800454 Vector<String16> args;
455 binder->dump(fd, args);
456 }
457 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700458 }
459 return NO_ERROR;
460}
461
Eric Laurent021cf962014-05-13 10:18:14 -0700462sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800463{
Eric Laurent021cf962014-05-13 10:18:14 -0700464 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800465 // If pid is already in the mClients wp<> map, then use that entry
466 // (for which promote() is always != 0), otherwise create a new entry and Client.
467 sp<Client> client = mClients.valueFor(pid).promote();
468 if (client == 0) {
469 client = new Client(this, pid);
470 mClients.add(pid, client);
471 }
472
473 return client;
474}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475
Glenn Kasten9e58b552013-01-18 15:09:48 -0800476sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
477{
Glenn Kasten481fb672013-09-30 14:39:28 -0700478 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800479 if (mLogMemoryDealer == 0) {
480 return new NBLog::Writer();
481 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800482 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700483 // Similarly if we can't contact the media.log service, also return a dummy writer
484 if (binder == 0) {
485 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800486 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700487 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
488 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
489 // If allocation fails, consult the vector of previously unregistered writers
490 // and garbage-collect one or more them until an allocation succeeds
491 if (shared == 0) {
492 Mutex::Autolock _l(mUnregisteredWritersLock);
493 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
494 {
495 // Pick the oldest stale writer to garbage-collect
496 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
497 mUnregisteredWriters.removeAt(0);
498 mediaLogService->unregisterWriter(iMemory);
499 // Now the media.log remote reference to IMemory is gone. When our last local
500 // reference to IMemory also drops to zero at end of this block,
501 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
502 }
503 // Re-attempt the allocation
504 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
505 if (shared != 0) {
506 goto success;
507 }
508 }
509 // Even after garbage-collecting all old writers, there is still not enough memory,
510 // so return a dummy writer
511 return new NBLog::Writer();
512 }
513success:
514 mediaLogService->registerWriter(shared, size, name);
515 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800516}
517
518void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
519{
Glenn Kasten685ef092013-02-04 08:15:34 -0800520 if (writer == 0) {
521 return;
522 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800523 sp<IMemory> iMemory(writer->getIMemory());
524 if (iMemory == 0) {
525 return;
526 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700527 // Rather than removing the writer immediately, append it to a queue of old writers to
528 // be garbage-collected later. This allows us to continue to view old logs for a while.
529 Mutex::Autolock _l(mUnregisteredWritersLock);
530 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800531}
532
Mathias Agopian65ab4712010-07-14 17:59:35 -0700533// IAudioFlinger interface
534
535
536sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800537 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800539 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700540 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800541 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800542 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800544 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800545 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800547 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 status_t *status)
549{
550 sp<PlaybackThread::Track> track;
551 sp<TrackHandle> trackHandle;
552 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 status_t lStatus;
554 int lSessionId;
555
Glenn Kasten263709e2012-01-06 08:40:01 -0800556 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
557 // but if someone uses binder directly they could bypass that and cause us to crash
558 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000559 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560 lStatus = BAD_VALUE;
561 goto Exit;
562 }
563
Glenn Kasten53b5d092014-02-05 10:00:23 -0800564 // further sample rate checks are performed by createTrack_l() depending on the thread type
565 if (sampleRate == 0) {
566 ALOGE("createTrack() invalid sample rate %u", sampleRate);
567 lStatus = BAD_VALUE;
568 goto Exit;
569 }
570
571 // further channel mask checks are performed by createTrack_l() depending on the thread type
572 if (!audio_is_output_channel(channelMask)) {
573 ALOGE("createTrack() invalid channel mask %#x", channelMask);
574 lStatus = BAD_VALUE;
575 goto Exit;
576 }
577
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700578 // further format checks are performed by createTrack_l() depending on the thread type
579 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800580 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700581 lStatus = BAD_VALUE;
582 goto Exit;
583 }
584
Glenn Kasten663c2242013-09-24 11:52:37 -0700585 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
586 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
587 lStatus = BAD_VALUE;
588 goto Exit;
589 }
590
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 {
592 Mutex::Autolock _l(mLock);
593 PlaybackThread *thread = checkPlaybackThread_l(output);
594 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800595 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 lStatus = BAD_VALUE;
597 goto Exit;
598 }
599
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800600 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700601 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602
Glenn Kastene848bd92014-03-13 15:00:32 -0700603 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700604 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700605 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700606 // check if an effect chain with the same session ID is present on another
607 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700608 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700609 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
610 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700611 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700612 if (sessions & PlaybackThread::EFFECT_SESSION) {
613 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700614 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700615 }
Eric Laurentde070132010-07-13 04:45:46 -0700616 }
617 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700619 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700620 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 if (sessionId != NULL) {
622 *sessionId = lSessionId;
623 }
624 }
Steve Block3856b092011-10-20 11:56:00 +0100625 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626
627 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800628 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800629 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700630 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700631
632 // move effect chain to this output thread if an effect on same session was waiting
633 // for a track to be created
634 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700635 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700636 Mutex::Autolock _dl(thread->mLock);
637 Mutex::Autolock _sl(effectThread->mLock);
638 moveEffectChain_l(lSessionId, effectThread, thread, true);
639 }
Eric Laurenta011e352012-03-29 15:51:43 -0700640
641 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700642 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700643 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
644 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700645 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700646 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700647 } else {
648 mPendingSyncEvents[i]->cancel();
649 }
Eric Laurenta011e352012-03-29 15:51:43 -0700650 mPendingSyncEvents.removeAt(i);
651 i--;
652 }
653 }
654 }
Glenn Kastene198c362013-08-13 09:13:36 -0700655
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656 }
Glenn Kastene198c362013-08-13 09:13:36 -0700657
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700658 if (lStatus != NO_ERROR) {
659 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700660 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700661 // Don't hold mClientLock when releasing the reference on the track as the
662 // destructor will acquire it.
663 {
664 Mutex::Autolock _cl(mClientLock);
665 client.clear();
666 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700668 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 }
670
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700671 // return handle to client
672 trackHandle = new TrackHandle(track);
673
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700675 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676 return trackHandle;
677}
678
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800679uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680{
681 Mutex::Autolock _l(mLock);
682 PlaybackThread *thread = checkPlaybackThread_l(output);
683 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000684 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 return 0;
686 }
687 return thread->sampleRate();
688}
689
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800690audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691{
692 Mutex::Autolock _l(mLock);
693 PlaybackThread *thread = checkPlaybackThread_l(output);
694 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000695 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800696 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697 }
698 return thread->format();
699}
700
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800701size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702{
703 Mutex::Autolock _l(mLock);
704 PlaybackThread *thread = checkPlaybackThread_l(output);
705 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000706 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707 return 0;
708 }
Glenn Kasten58912562012-04-03 10:45:00 -0700709 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
710 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 return thread->frameCount();
712}
713
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800714uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715{
716 Mutex::Autolock _l(mLock);
717 PlaybackThread *thread = checkPlaybackThread_l(output);
718 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800719 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 return 0;
721 }
722 return thread->latency();
723}
724
725status_t AudioFlinger::setMasterVolume(float value)
726{
Eric Laurenta1884f92011-08-23 08:25:03 -0700727 status_t ret = initCheck();
728 if (ret != NO_ERROR) {
729 return ret;
730 }
731
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732 // check calling permissions
733 if (!settingsAllowed()) {
734 return PERMISSION_DENIED;
735 }
736
Eric Laurenta4c5a552012-03-29 10:12:40 -0700737 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700738 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700739
John Grossmanee578c02012-07-23 17:05:46 -0700740 // Set master volume in the HALs which support it.
741 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
742 AutoMutex lock(mHardwareLock);
743 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800744
John Grossmanee578c02012-07-23 17:05:46 -0700745 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
746 if (dev->canSetMasterVolume()) {
747 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800748 }
John Grossmanee578c02012-07-23 17:05:46 -0700749 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751
John Grossmanee578c02012-07-23 17:05:46 -0700752 // Now set the master volume in each playback thread. Playback threads
753 // assigned to HALs which do not have master volume support will apply
754 // master volume during the mix operation. Threads with HALs which do
755 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800756 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700757 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758
759 return NO_ERROR;
760}
761
Glenn Kastenf78aee72012-01-04 11:00:47 -0800762status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763{
Eric Laurenta1884f92011-08-23 08:25:03 -0700764 status_t ret = initCheck();
765 if (ret != NO_ERROR) {
766 return ret;
767 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768
769 // check calling permissions
770 if (!settingsAllowed()) {
771 return PERMISSION_DENIED;
772 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800773 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000774 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 return BAD_VALUE;
776 }
777
778 { // scope for the lock
779 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700780 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700781 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700782 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783 mHardwareStatus = AUDIO_HW_IDLE;
784 }
785
786 if (NO_ERROR == ret) {
787 Mutex::Autolock _l(mLock);
788 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800789 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700790 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791 }
792
793 return ret;
794}
795
796status_t AudioFlinger::setMicMute(bool state)
797{
Eric Laurenta1884f92011-08-23 08:25:03 -0700798 status_t ret = initCheck();
799 if (ret != NO_ERROR) {
800 return ret;
801 }
802
Mathias Agopian65ab4712010-07-14 17:59:35 -0700803 // check calling permissions
804 if (!settingsAllowed()) {
805 return PERMISSION_DENIED;
806 }
807
808 AutoMutex lock(mHardwareLock);
809 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700810 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
811 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
812 status_t result = dev->set_mic_mute(dev, state);
813 if (result != NO_ERROR) {
814 ret = result;
815 }
816 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817 mHardwareStatus = AUDIO_HW_IDLE;
818 return ret;
819}
820
821bool AudioFlinger::getMicMute() const
822{
Eric Laurenta1884f92011-08-23 08:25:03 -0700823 status_t ret = initCheck();
824 if (ret != NO_ERROR) {
825 return false;
826 }
827
Dima Zavinfce7a472011-04-19 22:30:36 -0700828 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800829 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700830 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700832 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 mHardwareStatus = AUDIO_HW_IDLE;
834 return state;
835}
836
837status_t AudioFlinger::setMasterMute(bool muted)
838{
John Grossmand8f178d2012-07-20 14:51:35 -0700839 status_t ret = initCheck();
840 if (ret != NO_ERROR) {
841 return ret;
842 }
843
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844 // check calling permissions
845 if (!settingsAllowed()) {
846 return PERMISSION_DENIED;
847 }
848
John Grossmanee578c02012-07-23 17:05:46 -0700849 Mutex::Autolock _l(mLock);
850 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700851
John Grossmanee578c02012-07-23 17:05:46 -0700852 // Set master mute in the HALs which support it.
853 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
854 AutoMutex lock(mHardwareLock);
855 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700856
John Grossmanee578c02012-07-23 17:05:46 -0700857 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
858 if (dev->canSetMasterMute()) {
859 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700860 }
John Grossmanee578c02012-07-23 17:05:46 -0700861 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700862 }
863
John Grossmanee578c02012-07-23 17:05:46 -0700864 // Now set the master mute in each playback thread. Playback threads
865 // assigned to HALs which do not have master mute support will apply master
866 // mute during the mix operation. Threads with HALs which do support master
867 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800868 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700869 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870
871 return NO_ERROR;
872}
873
874float AudioFlinger::masterVolume() const
875{
Glenn Kasten98067102011-12-13 11:47:54 -0800876 Mutex::Autolock _l(mLock);
877 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878}
879
880bool AudioFlinger::masterMute() const
881{
Glenn Kasten98067102011-12-13 11:47:54 -0800882 Mutex::Autolock _l(mLock);
883 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884}
885
John Grossman4ff14ba2012-02-08 16:37:41 -0800886float AudioFlinger::masterVolume_l() const
887{
John Grossman4ff14ba2012-02-08 16:37:41 -0800888 return mMasterVolume;
889}
890
John Grossmand8f178d2012-07-20 14:51:35 -0700891bool AudioFlinger::masterMute_l() const
892{
John Grossmanee578c02012-07-23 17:05:46 -0700893 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700894}
895
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800896status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
897 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898{
899 // check calling permissions
900 if (!settingsAllowed()) {
901 return PERMISSION_DENIED;
902 }
903
Glenn Kasten263709e2012-01-06 08:40:01 -0800904 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000905 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906 return BAD_VALUE;
907 }
908
909 AutoMutex lock(mLock);
910 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700911 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 thread = checkPlaybackThread_l(output);
913 if (thread == NULL) {
914 return BAD_VALUE;
915 }
916 }
917
918 mStreamTypes[stream].volume = value;
919
920 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800921 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700922 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923 }
924 } else {
925 thread->setStreamVolume(stream, value);
926 }
927
928 return NO_ERROR;
929}
930
Glenn Kastenfff6d712012-01-12 16:38:12 -0800931status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932{
933 // check calling permissions
934 if (!settingsAllowed()) {
935 return PERMISSION_DENIED;
936 }
937
Glenn Kasten263709e2012-01-06 08:40:01 -0800938 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700939 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000940 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941 return BAD_VALUE;
942 }
943
Eric Laurent93575202011-01-18 18:39:02 -0800944 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700946 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700947 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948
949 return NO_ERROR;
950}
951
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800952float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953{
Glenn Kasten263709e2012-01-06 08:40:01 -0800954 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 return 0.0f;
956 }
957
958 AutoMutex lock(mLock);
959 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -0700960 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961 PlaybackThread *thread = checkPlaybackThread_l(output);
962 if (thread == NULL) {
963 return 0.0f;
964 }
965 volume = thread->streamVolume(stream);
966 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800967 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968 }
969
970 return volume;
971}
972
Glenn Kastenfff6d712012-01-12 16:38:12 -0800973bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700974{
Glenn Kasten263709e2012-01-06 08:40:01 -0800975 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700976 return true;
977 }
978
Glenn Kasten6637baa2012-01-09 09:40:36 -0800979 AutoMutex lock(mLock);
980 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981}
982
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800983status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700985 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
986 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800987
Mathias Agopian65ab4712010-07-14 17:59:35 -0700988 // check calling permissions
989 if (!settingsAllowed()) {
990 return PERMISSION_DENIED;
991 }
992
Glenn Kasten142f5192014-03-25 17:44:59 -0700993 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
994 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700995 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700996 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800997 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700998 AutoMutex lock(mHardwareLock);
999 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1000 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1001 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1002 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1003 final_result = result ?: final_result;
1004 }
1005 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001006 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001007 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1008 AudioParameter param = AudioParameter(keyValuePairs);
1009 String8 value;
1010 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001011 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1012 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001013 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1014 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001015 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001016 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1017 // collect all of the thread's session IDs
1018 KeyedVector<int, bool> ids = thread->sessionIds();
1019 // suspend effects associated with those session IDs
1020 for (size_t j = 0; j < ids.size(); ++j) {
1021 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001022 thread->setEffectSuspended(FX_IID_AEC,
1023 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001024 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001025 thread->setEffectSuspended(FX_IID_NS,
1026 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001027 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001028 }
1029 }
Eric Laurentbee53372011-08-29 12:42:48 -07001030 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001031 }
1032 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001033 String8 screenState;
1034 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1035 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001036 if (isOff != (AudioFlinger::mScreenState & 1)) {
1037 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001038 }
1039 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001040 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001041 }
1042
1043 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1044 // and the thread is exited once the lock is released
1045 sp<ThreadBase> thread;
1046 {
1047 Mutex::Autolock _l(mLock);
1048 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001049 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001051 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001052 // indicate output device change to all input threads for pre processing
1053 AudioParameter param = AudioParameter(keyValuePairs);
1054 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001055 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1056 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001057 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1058 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1059 }
1060 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001061 }
1062 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001063 if (thread != 0) {
1064 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065 }
1066 return BAD_VALUE;
1067}
1068
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001069String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001070{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001071 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1072 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073
Eric Laurenta4c5a552012-03-29 10:12:40 -07001074 Mutex::Autolock _l(mLock);
1075
Glenn Kasten142f5192014-03-25 17:44:59 -07001076 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001077 String8 out_s8;
1078
Dima Zavin799a70e2011-04-18 16:57:27 -07001079 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001080 char *s;
1081 {
1082 AutoMutex lock(mHardwareLock);
1083 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001084 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001085 s = dev->get_parameters(dev, keys.string());
1086 mHardwareStatus = AUDIO_HW_IDLE;
1087 }
John Grossmanef7740b2012-02-09 11:28:36 -08001088 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001089 free(s);
1090 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001091 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001092 }
1093
Mathias Agopian65ab4712010-07-14 17:59:35 -07001094 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1095 if (playbackThread != NULL) {
1096 return playbackThread->getParameters(keys);
1097 }
1098 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1099 if (recordThread != NULL) {
1100 return recordThread->getParameters(keys);
1101 }
1102 return String8("");
1103}
1104
Glenn Kastendd8104c2012-07-02 12:42:44 -07001105size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1106 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001107{
Eric Laurenta1884f92011-08-23 08:25:03 -07001108 status_t ret = initCheck();
1109 if (ret != NO_ERROR) {
1110 return 0;
1111 }
1112
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001113 AutoMutex lock(mHardwareLock);
1114 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001115 audio_config_t config;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001116 memset(&config, 0, sizeof(config));
1117 config.sample_rate = sampleRate;
1118 config.channel_mask = channelMask;
1119 config.format = format;
1120
John Grossmanee578c02012-07-23 17:05:46 -07001121 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1122 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001123 mHardwareStatus = AUDIO_HW_IDLE;
1124 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125}
1126
Glenn Kasten5f972c02014-01-13 09:59:31 -08001127uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129 Mutex::Autolock _l(mLock);
1130
1131 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1132 if (recordThread != NULL) {
1133 return recordThread->getInputFramesLost();
1134 }
1135 return 0;
1136}
1137
1138status_t AudioFlinger::setVoiceVolume(float value)
1139{
Eric Laurenta1884f92011-08-23 08:25:03 -07001140 status_t ret = initCheck();
1141 if (ret != NO_ERROR) {
1142 return ret;
1143 }
1144
Mathias Agopian65ab4712010-07-14 17:59:35 -07001145 // check calling permissions
1146 if (!settingsAllowed()) {
1147 return PERMISSION_DENIED;
1148 }
1149
1150 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001151 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001152 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001153 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154 mHardwareStatus = AUDIO_HW_IDLE;
1155
1156 return ret;
1157}
1158
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001159status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001160 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001161{
1162 status_t status;
1163
1164 Mutex::Autolock _l(mLock);
1165
1166 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1167 if (playbackThread != NULL) {
1168 return playbackThread->getRenderPosition(halFrames, dspFrames);
1169 }
1170
1171 return BAD_VALUE;
1172}
1173
1174void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1175{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001176 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001177 if (client == 0) {
1178 return;
1179 }
Eric Laurent021cf962014-05-13 10:18:14 -07001180 bool clientAdded = false;
1181 {
1182 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001183
Eric Laurent021cf962014-05-13 10:18:14 -07001184 pid_t pid = IPCThreadState::self()->getCallingPid();
1185 if (mNotificationClients.indexOfKey(pid) < 0) {
1186 sp<NotificationClient> notificationClient = new NotificationClient(this,
1187 client,
1188 pid);
1189 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001190
Eric Laurent021cf962014-05-13 10:18:14 -07001191 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001192
Eric Laurent021cf962014-05-13 10:18:14 -07001193 sp<IBinder> binder = client->asBinder();
1194 binder->linkToDeath(notificationClient);
1195 clientAdded = true;
1196 }
1197 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001198
Eric Laurent021cf962014-05-13 10:18:14 -07001199 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001200 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent021cf962014-05-13 10:18:14 -07001201 if (clientAdded) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001202 // the config change is always sent from playback or record threads to avoid deadlock
1203 // with AudioSystem::gLock
1204 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001205 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001206 }
1207
1208 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001209 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001210 }
1211 }
1212}
1213
1214void AudioFlinger::removeNotificationClient(pid_t pid)
1215{
1216 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001217 {
1218 Mutex::Autolock _cl(mClientLock);
1219 mNotificationClients.removeItem(pid);
1220 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001221
Steve Block3856b092011-10-20 11:56:00 +01001222 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001223 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001224 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001225 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001226 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001227 ALOGV(" pid %d @ %d", ref->mPid, i);
1228 if (ref->mPid == pid) {
1229 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001230 mAudioSessionRefs.removeAt(i);
1231 delete ref;
1232 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001233 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001234 } else {
1235 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001236 }
1237 }
1238 if (removed) {
1239 purgeStaleEffects_l();
1240 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001241}
1242
Eric Laurent021cf962014-05-13 10:18:14 -07001243void AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244{
Eric Laurent021cf962014-05-13 10:18:14 -07001245 Mutex::Autolock _l(mClientLock);
1246 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001247 for (size_t i = 0; i < size; i++) {
Eric Laurent021cf962014-05-13 10:18:14 -07001248 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
Eric Laurent10351942014-05-08 18:49:52 -07001249 ioHandle,
1250 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251 }
1252}
1253
Eric Laurent021cf962014-05-13 10:18:14 -07001254// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255void AudioFlinger::removeClient_l(pid_t pid)
1256{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001257 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001258 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 mClients.removeItem(pid);
1260}
1261
Eric Laurent717e1282012-06-29 16:36:52 -07001262// getEffectThread_l() must be called with AudioFlinger::mLock held
1263sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1264{
1265 sp<PlaybackThread> thread;
1266
1267 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1268 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1269 ALOG_ASSERT(thread == 0);
1270 thread = mPlaybackThreads.valueAt(i);
1271 }
1272 }
1273
1274 return thread;
1275}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278
1279// ----------------------------------------------------------------------------
1280
1281AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1282 : RefBase(),
1283 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001284 // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below
Mathias Agopian65ab4712010-07-14 17:59:35 -07001285 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001286 mPid(pid),
1287 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288{
1289 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1290}
1291
Eric Laurent021cf962014-05-13 10:18:14 -07001292// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001293AudioFlinger::Client::~Client()
1294{
1295 mAudioFlinger->removeClient_l(mPid);
1296}
1297
Glenn Kasten435dbe62012-01-30 10:15:48 -08001298sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001299{
1300 return mMemoryDealer;
1301}
1302
John Grossman4ff14ba2012-02-08 16:37:41 -08001303// Reserve one of the limited slots for a timed audio track associated
1304// with this client
1305bool AudioFlinger::Client::reserveTimedTrack()
1306{
1307 const int kMaxTimedTracksPerClient = 4;
1308
1309 Mutex::Autolock _l(mTimedTrackLock);
1310
1311 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1312 ALOGW("can not create timed track - pid %d has exceeded the limit",
1313 mPid);
1314 return false;
1315 }
1316
1317 mTimedTrackCount++;
1318 return true;
1319}
1320
1321// Release a slot for a timed audio track
1322void AudioFlinger::Client::releaseTimedTrack()
1323{
1324 Mutex::Autolock _l(mTimedTrackLock);
1325 mTimedTrackCount--;
1326}
1327
Mathias Agopian65ab4712010-07-14 17:59:35 -07001328// ----------------------------------------------------------------------------
1329
1330AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1331 const sp<IAudioFlingerClient>& client,
1332 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001333 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001334{
1335}
1336
1337AudioFlinger::NotificationClient::~NotificationClient()
1338{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001339}
1340
Glenn Kasten0f11b512014-01-31 16:18:54 -08001341void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001342{
1343 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001344 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001345}
1346
Mathias Agopian65ab4712010-07-14 17:59:35 -07001347
1348// ----------------------------------------------------------------------------
1349
Jeff Brown893a5642013-08-16 20:19:26 -07001350static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1351 return audio_is_remote_submix_device(inDevice);
1352}
1353
Mathias Agopian65ab4712010-07-14 17:59:35 -07001354sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001355 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001356 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001357 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001358 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001359 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001360 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001361 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001362 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001363 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001364 sp<IMemory>& cblk,
1365 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001366 status_t *status)
1367{
1368 sp<RecordThread::RecordTrack> recordTrack;
1369 sp<RecordHandle> recordHandle;
1370 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001371 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372 int lSessionId;
1373
Glenn Kastend776ac62014-05-07 09:16:09 -07001374 cblk.clear();
1375 buffers.clear();
1376
Mathias Agopian65ab4712010-07-14 17:59:35 -07001377 // check calling permissions
1378 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001379 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380 lStatus = PERMISSION_DENIED;
1381 goto Exit;
1382 }
1383
Glenn Kasten53b5d092014-02-05 10:00:23 -08001384 // further sample rate checks are performed by createRecordTrack_l()
1385 if (sampleRate == 0) {
1386 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1387 lStatus = BAD_VALUE;
1388 goto Exit;
1389 }
1390
Glenn Kasten0e0e8462014-03-13 15:02:40 -07001391 // we don't yet support anything other than 16-bit PCM
1392 if (!(audio_is_valid_format(format) &&
1393 audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001394 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001395 lStatus = BAD_VALUE;
1396 goto Exit;
1397 }
1398
Glenn Kasten53b5d092014-02-05 10:00:23 -08001399 // further channel mask checks are performed by createRecordTrack_l()
1400 if (!audio_is_input_channel(channelMask)) {
1401 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1402 lStatus = BAD_VALUE;
1403 goto Exit;
1404 }
1405
Glenn Kasten05997e22014-03-13 15:08:33 -07001406 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001408 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001410 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001411 lStatus = BAD_VALUE;
1412 goto Exit;
1413 }
1414
Jeff Brown893a5642013-08-16 20:19:26 -07001415 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1416 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001417 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001418 lStatus = PERMISSION_DENIED;
1419 goto Exit;
1420 }
1421
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001422 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001423 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001425 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001426 lSessionId = *sessionId;
1427 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001428 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001429 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001430 if (sessionId != NULL) {
1431 *sessionId = lSessionId;
1432 }
1433 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001434 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001435
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001436 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001437 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001438 frameCount, lSessionId, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001439 IPCThreadState::self()->getCallingUid(),
1440 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001441 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001442
1443 if (lStatus == NO_ERROR) {
1444 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1445 // session and move it to this thread.
1446 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
1447 if (chain != 0) {
1448 Mutex::Autolock _l(thread->mLock);
1449 thread->addEffectChain_l(chain);
1450 }
1451 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001452 }
Glenn Kastene198c362013-08-13 09:13:36 -07001453
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001454 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001455 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001456 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001457 // Don't hold mClientLock when releasing the reference on the track as the
1458 // destructor will acquire it.
1459 {
1460 Mutex::Autolock _cl(mClientLock);
1461 client.clear();
1462 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001463 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001464 goto Exit;
1465 }
1466
Glenn Kastend776ac62014-05-07 09:16:09 -07001467 cblk = recordTrack->getCblk();
1468 buffers = recordTrack->getBuffers();
1469
Glenn Kasten2fc14732013-08-05 14:58:14 -07001470 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001472
1473Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001474 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001475 return recordHandle;
1476}
1477
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001478
1479
Mathias Agopian65ab4712010-07-14 17:59:35 -07001480// ----------------------------------------------------------------------------
1481
Eric Laurenta4c5a552012-03-29 10:12:40 -07001482audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1483{
Eric Laurent44622db2014-08-01 19:00:33 -07001484 if (name == NULL) {
1485 return 0;
1486 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001487 if (!settingsAllowed()) {
1488 return 0;
1489 }
1490 Mutex::Autolock _l(mLock);
1491 return loadHwModule_l(name);
1492}
1493
1494// loadHwModule_l() must be called with AudioFlinger::mLock held
1495audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1496{
1497 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1498 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1499 ALOGW("loadHwModule() module %s already loaded", name);
1500 return mAudioHwDevs.keyAt(i);
1501 }
1502 }
1503
Eric Laurenta4c5a552012-03-29 10:12:40 -07001504 audio_hw_device_t *dev;
1505
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001506 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001507 if (rc) {
1508 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1509 return 0;
1510 }
1511
1512 mHardwareStatus = AUDIO_HW_INIT;
1513 rc = dev->init_check(dev);
1514 mHardwareStatus = AUDIO_HW_IDLE;
1515 if (rc) {
1516 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1517 return 0;
1518 }
1519
John Grossmanee578c02012-07-23 17:05:46 -07001520 // Check and cache this HAL's level of support for master mute and master
1521 // volume. If this is the first HAL opened, and it supports the get
1522 // methods, use the initial values provided by the HAL as the current
1523 // master mute and volume settings.
1524
1525 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1526 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001527 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001528
1529 if (0 == mAudioHwDevs.size()) {
1530 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1531 if (NULL != dev->get_master_volume) {
1532 float mv;
1533 if (OK == dev->get_master_volume(dev, &mv)) {
1534 mMasterVolume = mv;
1535 }
1536 }
1537
1538 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1539 if (NULL != dev->get_master_mute) {
1540 bool mm;
1541 if (OK == dev->get_master_mute(dev, &mm)) {
1542 mMasterMute = mm;
1543 }
1544 }
1545 }
1546
Eric Laurenta4c5a552012-03-29 10:12:40 -07001547 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001548 if ((NULL != dev->set_master_volume) &&
1549 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1550 flags = static_cast<AudioHwDevice::Flags>(flags |
1551 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1552 }
1553
1554 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1555 if ((NULL != dev->set_master_mute) &&
1556 (OK == dev->set_master_mute(dev, mMasterMute))) {
1557 flags = static_cast<AudioHwDevice::Flags>(flags |
1558 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1559 }
1560
Eric Laurenta4c5a552012-03-29 10:12:40 -07001561 mHardwareStatus = AUDIO_HW_IDLE;
1562 }
1563
1564 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001565 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001566
1567 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001568 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001569
1570 return handle;
1571
1572}
1573
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001574// ----------------------------------------------------------------------------
1575
Glenn Kasten3b16c762012-11-14 08:44:39 -08001576uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001577{
1578 Mutex::Autolock _l(mLock);
1579 PlaybackThread *thread = primaryPlaybackThread_l();
1580 return thread != NULL ? thread->sampleRate() : 0;
1581}
1582
Glenn Kastene33054e2012-11-14 12:54:39 -08001583size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001584{
1585 Mutex::Autolock _l(mLock);
1586 PlaybackThread *thread = primaryPlaybackThread_l();
1587 return thread != NULL ? thread->frameCountHAL() : 0;
1588}
1589
1590// ----------------------------------------------------------------------------
1591
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001592status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1593{
1594 uid_t uid = IPCThreadState::self()->getCallingUid();
1595 if (uid != AID_SYSTEM) {
1596 return PERMISSION_DENIED;
1597 }
1598 Mutex::Autolock _l(mLock);
1599 if (mIsDeviceTypeKnown) {
1600 return INVALID_OPERATION;
1601 }
1602 mIsLowRamDevice = isLowRamDevice;
1603 mIsDeviceTypeKnown = true;
1604 return NO_ERROR;
1605}
1606
Eric Laurent93c3d412014-08-01 14:48:35 -07001607audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1608{
1609 Mutex::Autolock _l(mLock);
1610 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1611 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1612 if ((thread->hasAudioSession(sessionId) & ThreadBase::TRACK_SESSION) != 0) {
1613 // A session can only be on one thread, so exit after first match
1614 String8 reply = thread->getParameters(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC));
1615 AudioParameter param = AudioParameter(reply);
1616 int value;
1617 if (param.getInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value) == NO_ERROR) {
1618 return value;
1619 }
1620 break;
1621 }
1622 }
1623 return AUDIO_HW_SYNC_INVALID;
1624}
1625
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001626// ----------------------------------------------------------------------------
1627
Eric Laurent83b88082014-06-20 18:31:16 -07001628
1629sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001630 audio_io_handle_t *output,
1631 audio_config_t *config,
1632 audio_devices_t devices,
1633 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001634 audio_output_flags_t flags)
1635{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001636 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001637 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001638 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001639 }
1640
1641 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001642 if (*output == AUDIO_IO_HANDLE_NONE) {
1643 *output = nextUniqueId();
1644 }
Eric Laurent83b88082014-06-20 18:31:16 -07001645
1646 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1647
1648 audio_stream_out_t *outStream = NULL;
1649
1650 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001651 // This if statement allows overriding the audio policy settings
1652 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1653 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1654 // Check only for Normal Mixing mode
1655 if (kEnableExtendedPrecision) {
1656 // Specify format (uncomment one below to choose)
1657 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1658 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1659 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1660 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001661 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001662 }
1663 if (kEnableExtendedChannels) {
1664 // Specify channel mask (uncomment one below to choose)
1665 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1666 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1667 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1668 }
Eric Laurent83b88082014-06-20 18:31:16 -07001669 }
1670
1671 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001672 *output,
1673 devices,
1674 flags,
1675 config,
1676 &outStream,
1677 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001678
1679 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001680 ALOGV("openOutput_l() openOutputStream returned output %p, sampleRate %d, Format %#x, "
Andy Hung9a592762014-07-21 21:56:01 -07001681 "channelMask %#x, status %d",
Eric Laurent83b88082014-06-20 18:31:16 -07001682 outStream,
1683 config->sample_rate,
1684 config->format,
1685 config->channel_mask,
1686 status);
1687
1688 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001689 AudioStreamOut *outputStream = new AudioStreamOut(outHwDev, outStream, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001690
1691 PlaybackThread *thread;
1692 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001693 thread = new OffloadThread(this, outputStream, *output, devices);
1694 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001695 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1696 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001697 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001698 thread = new DirectOutputThread(this, outputStream, *output, devices);
1699 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001700 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001701 thread = new MixerThread(this, outputStream, *output, devices);
1702 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001703 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001704 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001705 return thread;
1706 }
1707
1708 return 0;
1709}
1710
Eric Laurentcf2c0212014-07-25 16:20:43 -07001711status_t AudioFlinger::openOutput(audio_module_handle_t module,
1712 audio_io_handle_t *output,
1713 audio_config_t *config,
1714 audio_devices_t *devices,
1715 const String8& address,
1716 uint32_t *latencyMs,
1717 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001718{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001719 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001720 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001721 (devices != NULL) ? *devices : 0,
1722 config->sample_rate,
1723 config->format,
1724 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001725 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001726
Eric Laurentcf2c0212014-07-25 16:20:43 -07001727 if (*devices == AUDIO_DEVICE_NONE) {
1728 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001729 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001730
Mathias Agopian65ab4712010-07-14 17:59:35 -07001731 Mutex::Autolock _l(mLock);
1732
Eric Laurentcf2c0212014-07-25 16:20:43 -07001733 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001734 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001735 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001736
1737 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001738 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001739
1740 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001741 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001742 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001743 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001744
1745 AutoMutex lock(mHardwareLock);
1746 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001747 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001748 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten4ea00a22014-06-02 08:29:22 -07001749
Eric Laurentcf2c0212014-07-25 16:20:43 -07001750 mPrimaryOutputSampleRate = config->sample_rate;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001751 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001752 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001753 }
1754
Eric Laurentcf2c0212014-07-25 16:20:43 -07001755 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001756}
1757
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001758audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1759 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001760{
1761 Mutex::Autolock _l(mLock);
1762 MixerThread *thread1 = checkMixerThread_l(output1);
1763 MixerThread *thread2 = checkMixerThread_l(output2);
1764
1765 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001766 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1767 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001768 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001769 }
1770
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001771 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001772 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1773 thread->addOutputTrack(thread2);
1774 mPlaybackThreads.add(id, thread);
1775 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001776 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001777 return id;
1778}
1779
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001780status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001781{
Glenn Kastend96c5722012-04-25 13:44:49 -07001782 return closeOutput_nonvirtual(output);
1783}
1784
1785status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1786{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001787 // keep strong reference on the playback thread so that
1788 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001789 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001790 {
1791 Mutex::Autolock _l(mLock);
1792 thread = checkPlaybackThread_l(output);
1793 if (thread == NULL) {
1794 return BAD_VALUE;
1795 }
1796
Steve Block3856b092011-10-20 11:56:00 +01001797 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001798
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001799 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001800 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001801 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001802 DuplicatingThread *dupThread =
1803 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001804 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001805
1806 }
1807 }
1808 }
1809
1810
1811 mPlaybackThreads.removeItem(output);
1812 // save all effects to the default thread
1813 if (mPlaybackThreads.size()) {
1814 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1815 if (dstThread != NULL) {
1816 // audioflinger lock is held here so the acquisition order of thread locks does not
1817 // matter
1818 Mutex::Autolock _dl(dstThread->mLock);
1819 Mutex::Autolock _sl(thread->mLock);
1820 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1821 for (size_t i = 0; i < effectChains.size(); i ++) {
1822 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001823 }
1824 }
1825 }
Eric Laurent021cf962014-05-13 10:18:14 -07001826 audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001827 }
1828 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001829 // The thread entity (active unit of execution) is no longer running here,
1830 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001831
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001832 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurent83b88082014-06-20 18:31:16 -07001833 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001834 }
Eric Laurent83b88082014-06-20 18:31:16 -07001835
Mathias Agopian65ab4712010-07-14 17:59:35 -07001836 return NO_ERROR;
1837}
1838
Eric Laurent83b88082014-06-20 18:31:16 -07001839void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1840{
1841 AudioStreamOut *out = thread->clearOutput();
1842 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1843 // from now on thread->mOutput is NULL
1844 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1845 delete out;
1846}
1847
1848void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1849{
1850 mPlaybackThreads.removeItem(thread->mId);
1851 thread->exit();
1852 closeOutputFinish(thread);
1853}
1854
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001855status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001856{
1857 Mutex::Autolock _l(mLock);
1858 PlaybackThread *thread = checkPlaybackThread_l(output);
1859
1860 if (thread == NULL) {
1861 return BAD_VALUE;
1862 }
1863
Steve Block3856b092011-10-20 11:56:00 +01001864 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001865 thread->suspend();
1866
1867 return NO_ERROR;
1868}
1869
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001870status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001871{
1872 Mutex::Autolock _l(mLock);
1873 PlaybackThread *thread = checkPlaybackThread_l(output);
1874
1875 if (thread == NULL) {
1876 return BAD_VALUE;
1877 }
1878
Steve Block3856b092011-10-20 11:56:00 +01001879 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001880
1881 thread->restore();
1882
1883 return NO_ERROR;
1884}
1885
Eric Laurentcf2c0212014-07-25 16:20:43 -07001886status_t AudioFlinger::openInput(audio_module_handle_t module,
1887 audio_io_handle_t *input,
1888 audio_config_t *config,
1889 audio_devices_t *device,
1890 const String8& address,
1891 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07001892 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001893{
Eric Laurent83b88082014-06-20 18:31:16 -07001894 Mutex::Autolock _l(mLock);
1895
Eric Laurentcf2c0212014-07-25 16:20:43 -07001896 if (*device == AUDIO_DEVICE_NONE) {
1897 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07001898 }
1899
Eric Laurentcf2c0212014-07-25 16:20:43 -07001900 sp<RecordThread> thread = openInput_l(module, input, config, *device, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001901
1902 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07001903 // notify client processes of the new input creation
1904 thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001905 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001906 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001907 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07001908}
Dima Zavin799a70e2011-04-18 16:57:27 -07001909
Eric Laurent83b88082014-06-20 18:31:16 -07001910sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001911 audio_io_handle_t *input,
1912 audio_config_t *config,
Eric Laurent83b88082014-06-20 18:31:16 -07001913 audio_devices_t device,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001914 const String8& address,
1915 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07001916 audio_input_flags_t flags)
1917{
Eric Laurent83b88082014-06-20 18:31:16 -07001918 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, device);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001919 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001920 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07001921 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001922 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001923
Eric Laurentcf2c0212014-07-25 16:20:43 -07001924 if (*input == AUDIO_IO_HANDLE_NONE) {
1925 *input = nextUniqueId();
1926 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001927
Eric Laurentcf2c0212014-07-25 16:20:43 -07001928 audio_config_t halconfig = *config;
1929 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07001930 audio_stream_in_t *inStream = NULL;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001931 status_t status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
1932 &inStream, flags, address.string(), source);
1933 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
1934 ", Format %#x, Channels %x, flags %#x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001935 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001936 halconfig.sample_rate,
1937 halconfig.format,
1938 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07001939 flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001940 status);
1941
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001942 // If the input could not be opened with the requested parameters and we can handle the
1943 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1944 // resample the input and do mono to stereo or stereo to mono conversions on 16 bit PCM inputs.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001945 if (status == BAD_VALUE &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07001946 config->format == halconfig.format && halconfig.format == AUDIO_FORMAT_PCM_16_BIT &&
1947 (halconfig.sample_rate <= 2 * config->sample_rate) &&
1948 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
1949 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001950 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07001951 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001952 inStream = NULL;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001953 status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
1954 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07001955 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001956 }
1957
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001958 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001959
Glenn Kasten46909e72013-02-26 09:20:22 -08001960#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001961 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1962 // or (re-)create if current Pipe is idle and does not match the new format
1963 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001964 enum {
1965 TEE_SINK_NO, // don't copy input
1966 TEE_SINK_NEW, // copy input using a new pipe
1967 TEE_SINK_OLD, // copy input using an existing pipe
1968 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07001969 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
1970 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08001971 if (!mTeeSinkInputEnabled) {
1972 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08001973 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001974 kind = TEE_SINK_NO;
1975 } else if (mRecordTeeSink == 0) {
1976 kind = TEE_SINK_NEW;
1977 } else if (mRecordTeeSink->getStrongCount() != 1) {
1978 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08001979 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001980 kind = TEE_SINK_OLD;
1981 } else {
1982 kind = TEE_SINK_NEW;
1983 }
1984 switch (kind) {
1985 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001986 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001987 size_t numCounterOffers = 0;
1988 const NBAIO_Format offers[1] = {format};
1989 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1990 ALOG_ASSERT(index == 0);
1991 PipeReader *pipeReader = new PipeReader(*pipe);
1992 numCounterOffers = 0;
1993 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1994 ALOG_ASSERT(index == 0);
1995 mRecordTeeSink = pipe;
1996 mRecordTeeSource = pipeReader;
1997 teeSink = pipe;
1998 }
1999 break;
2000 case TEE_SINK_OLD:
2001 teeSink = mRecordTeeSink;
2002 break;
2003 case TEE_SINK_NO:
2004 default:
2005 break;
2006 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002007#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002008
Eric Laurentcf2c0212014-07-25 16:20:43 -07002009 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002010
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002011 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002012 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002013 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002014 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002015 inputStream,
2016 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002017 primaryOutputDevice_l(),
Eric Laurent83b88082014-06-20 18:31:16 -07002018 device
Glenn Kasten46909e72013-02-26 09:20:22 -08002019#ifdef TEE_SINK
2020 , teeSink
2021#endif
2022 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002023 mRecordThreads.add(*input, thread);
2024 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002025 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002026 }
2027
Eric Laurentcf2c0212014-07-25 16:20:43 -07002028 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002029 return 0;
2030}
2031
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002032status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002033{
Glenn Kastend96c5722012-04-25 13:44:49 -07002034 return closeInput_nonvirtual(input);
2035}
2036
2037status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2038{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002039 // keep strong reference on the record thread so that
2040 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002041 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002042 {
2043 Mutex::Autolock _l(mLock);
2044 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002045 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002046 return BAD_VALUE;
2047 }
2048
Steve Block3856b092011-10-20 11:56:00 +01002049 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002050
2051 // If we still have effect chains, it means that a client still holds a handle
2052 // on at least one effect. We must either move the chain to an existing thread with the
2053 // same session ID or put it aside in case a new record thread is opened for a
2054 // new capture on the same session
2055 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002056 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002057 Mutex::Autolock _sl(thread->mLock);
2058 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002059 // Note: maximum one chain per record thread
2060 if (effectChains.size() != 0) {
2061 chain = effectChains[0];
2062 }
2063 }
2064 if (chain != 0) {
2065 // first check if a record thread is already opened with a client on the same session.
2066 // This should only happen in case of overlap between one thread tear down and the
2067 // creation of its replacement
2068 size_t i;
2069 for (i = 0; i < mRecordThreads.size(); i++) {
2070 sp<RecordThread> t = mRecordThreads.valueAt(i);
2071 if (t == thread) {
2072 continue;
2073 }
2074 if (t->hasAudioSession(chain->sessionId()) != 0) {
2075 Mutex::Autolock _l(t->mLock);
2076 ALOGV("closeInput() found thread %d for effect session %d",
2077 t->id(), chain->sessionId());
2078 t->addEffectChain_l(chain);
2079 break;
2080 }
2081 }
2082 // put the chain aside if we could not find a record thread with the same session id.
2083 if (i == mRecordThreads.size()) {
2084 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002085 }
2086 }
Eric Laurent021cf962014-05-13 10:18:14 -07002087 audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002088 mRecordThreads.removeItem(input);
2089 }
Eric Laurent83b88082014-06-20 18:31:16 -07002090 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2091 // we have a different lock for notification client
2092 closeInputFinish(thread);
2093 return NO_ERROR;
2094}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002095
Eric Laurent83b88082014-06-20 18:31:16 -07002096void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2097{
2098 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002099 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002100 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002101 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002102 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002103 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002104}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002105
Eric Laurent83b88082014-06-20 18:31:16 -07002106void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2107{
2108 mRecordThreads.removeItem(thread->mId);
2109 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002110}
2111
Glenn Kastend2304db2014-02-03 07:40:31 -08002112status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002113{
2114 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002115 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002116
2117 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2118 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002119 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002120 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002121
2122 return NO_ERROR;
2123}
2124
2125
Eric Laurentde3f8392014-07-27 18:38:22 -07002126audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002127{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002128 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002129}
2130
Marco Nelissend457c972014-02-11 08:47:07 -08002131void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002132{
2133 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002134 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002135 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2136 if (pid != -1 && (caller == getpid_cached)) {
2137 caller = pid;
2138 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002139
Eric Laurent021cf962014-05-13 10:18:14 -07002140 {
2141 Mutex::Autolock _cl(mClientLock);
2142 // Ignore requests received from processes not known as notification client. The request
2143 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2144 // called from a different pid leaving a stale session reference. Also we don't know how
2145 // to clear this reference if the client process dies.
2146 if (mNotificationClients.indexOfKey(caller) < 0) {
2147 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2148 return;
2149 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002150 }
2151
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002152 size_t num = mAudioSessionRefs.size();
2153 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002154 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002155 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2156 ref->mCnt++;
2157 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002158 return;
2159 }
2160 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002161 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2162 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002163}
2164
Marco Nelissend457c972014-02-11 08:47:07 -08002165void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002166{
2167 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002168 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002169 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2170 if (pid != -1 && (caller == getpid_cached)) {
2171 caller = pid;
2172 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002173 size_t num = mAudioSessionRefs.size();
2174 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002175 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002176 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2177 ref->mCnt--;
2178 ALOGV(" decremented refcount to %d", ref->mCnt);
2179 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002180 mAudioSessionRefs.removeAt(i);
2181 delete ref;
2182 purgeStaleEffects_l();
2183 }
2184 return;
2185 }
2186 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002187 // If the caller is mediaserver it is likely that the session being released was acquired
2188 // on behalf of a process not in notification clients and we ignore the warning.
2189 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002190}
2191
2192void AudioFlinger::purgeStaleEffects_l() {
2193
Steve Block3856b092011-10-20 11:56:00 +01002194 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002195
2196 Vector< sp<EffectChain> > chains;
2197
2198 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2199 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2200 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2201 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002202 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2203 chains.push(ec);
2204 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002205 }
2206 }
2207 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2208 sp<RecordThread> t = mRecordThreads.valueAt(i);
2209 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2210 sp<EffectChain> ec = t->mEffectChains[j];
2211 chains.push(ec);
2212 }
2213 }
2214
2215 for (size_t i = 0; i < chains.size(); i++) {
2216 sp<EffectChain> ec = chains[i];
2217 int sessionid = ec->sessionId();
2218 sp<ThreadBase> t = ec->mThread.promote();
2219 if (t == 0) {
2220 continue;
2221 }
2222 size_t numsessionrefs = mAudioSessionRefs.size();
2223 bool found = false;
2224 for (size_t k = 0; k < numsessionrefs; k++) {
2225 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002226 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002227 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002228 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002229 found = true;
2230 break;
2231 }
2232 }
2233 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002234 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002235 // remove all effects from the chain
2236 while (ec->mEffects.size()) {
2237 sp<EffectModule> effect = ec->mEffects[0];
2238 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002239 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002240 if (effect->purgeHandles()) {
2241 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002242 }
2243 AudioSystem::unregisterEffect(effect->id());
2244 }
2245 }
2246 }
2247 return;
2248}
2249
Mathias Agopian65ab4712010-07-14 17:59:35 -07002250// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002251AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002252{
Glenn Kastena1117922012-01-26 10:53:32 -08002253 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254}
2255
2256// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002257AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002258{
2259 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002260 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002261}
2262
2263// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002264AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002265{
Glenn Kastena1117922012-01-26 10:53:32 -08002266 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002267}
2268
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002269uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002270{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002271 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002272}
2273
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002274AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002275{
2276 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2277 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002278 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002279 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002280 return thread;
2281 }
2282 }
2283 return NULL;
2284}
2285
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002286audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002287{
2288 PlaybackThread *thread = primaryPlaybackThread_l();
2289
2290 if (thread == NULL) {
2291 return 0;
2292 }
2293
Eric Laurentf1c04f92012-08-28 14:26:53 -07002294 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002295}
2296
Eric Laurenta011e352012-03-29 15:51:43 -07002297sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2298 int triggerSession,
2299 int listenerSession,
2300 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002301 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002302{
2303 Mutex::Autolock _l(mLock);
2304
2305 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2306 status_t playStatus = NAME_NOT_FOUND;
2307 status_t recStatus = NAME_NOT_FOUND;
2308 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2309 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2310 if (playStatus == NO_ERROR) {
2311 return event;
2312 }
2313 }
2314 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2315 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2316 if (recStatus == NO_ERROR) {
2317 return event;
2318 }
2319 }
2320 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2321 mPendingSyncEvents.add(event);
2322 } else {
2323 ALOGV("createSyncEvent() invalid event %d", event->type());
2324 event.clear();
2325 }
2326 return event;
2327}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002328
Mathias Agopian65ab4712010-07-14 17:59:35 -07002329// ----------------------------------------------------------------------------
2330// Effect management
2331// ----------------------------------------------------------------------------
2332
2333
Glenn Kastenf587ba52012-01-26 16:25:10 -08002334status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002335{
2336 Mutex::Autolock _l(mLock);
2337 return EffectQueryNumberEffects(numEffects);
2338}
2339
Glenn Kastenf587ba52012-01-26 16:25:10 -08002340status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002341{
2342 Mutex::Autolock _l(mLock);
2343 return EffectQueryEffect(index, descriptor);
2344}
2345
Glenn Kasten5e92a782012-01-30 07:40:52 -08002346status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002347 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002348{
2349 Mutex::Autolock _l(mLock);
2350 return EffectGetDescriptor(pUuid, descriptor);
2351}
2352
2353
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002354sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002355 effect_descriptor_t *pDesc,
2356 const sp<IEffectClient>& effectClient,
2357 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002358 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002359 int sessionId,
2360 status_t *status,
2361 int *id,
2362 int *enabled)
2363{
2364 status_t lStatus = NO_ERROR;
2365 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002366 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002367
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002368 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002369 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002370 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002371
2372 if (pDesc == NULL) {
2373 lStatus = BAD_VALUE;
2374 goto Exit;
2375 }
2376
Eric Laurent84e9a102010-09-23 16:10:16 -07002377 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002378 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002379 lStatus = PERMISSION_DENIED;
2380 goto Exit;
2381 }
2382
Dima Zavinfce7a472011-04-19 22:30:36 -07002383 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002384 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002385 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002386 lStatus = PERMISSION_DENIED;
2387 goto Exit;
2388 }
2389
Mathias Agopian65ab4712010-07-14 17:59:35 -07002390 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002391 if (!EffectIsNullUuid(&pDesc->uuid)) {
2392 // if uuid is specified, request effect descriptor
2393 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2394 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002395 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002396 goto Exit;
2397 }
2398 } else {
2399 // if uuid is not specified, look for an available implementation
2400 // of the required type in effect factory
2401 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002402 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002403 lStatus = BAD_VALUE;
2404 goto Exit;
2405 }
2406 uint32_t numEffects = 0;
2407 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002408 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002409 bool found = false;
2410
2411 lStatus = EffectQueryNumberEffects(&numEffects);
2412 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002413 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002414 goto Exit;
2415 }
2416 for (uint32_t i = 0; i < numEffects; i++) {
2417 lStatus = EffectQueryEffect(i, &desc);
2418 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002419 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002420 continue;
2421 }
2422 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2423 // If matching type found save effect descriptor. If the session is
2424 // 0 and the effect is not auxiliary, continue enumeration in case
2425 // an auxiliary version of this effect type is available
2426 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002427 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002428 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002429 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2430 break;
2431 }
2432 }
2433 }
2434 if (!found) {
2435 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002436 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002437 goto Exit;
2438 }
2439 // For same effect type, chose auxiliary version over insert version if
2440 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002441 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002442 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002443 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002444 }
2445 }
2446
2447 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002448 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002449 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2450 lStatus = INVALID_OPERATION;
2451 goto Exit;
2452 }
2453
Eric Laurent59255e42011-07-27 19:49:51 -07002454 // check recording permission for visualizer
2455 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2456 !recordingAllowed()) {
2457 lStatus = PERMISSION_DENIED;
2458 goto Exit;
2459 }
2460
Mathias Agopian65ab4712010-07-14 17:59:35 -07002461 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002462 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002463 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002464 // if the output returned by getOutputForEffect() is removed before we lock the
2465 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2466 // and we will exit safely
2467 io = AudioSystem::getOutputForEffect(&desc);
2468 ALOGV("createEffect got output %d", io);
2469 }
2470
2471 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002472
2473 // If output is not specified try to find a matching audio session ID in one of the
2474 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002475 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2476 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002477 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002478 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002479 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2480 // output must be specified by AudioPolicyManager when using session
2481 // AUDIO_SESSION_OUTPUT_STAGE
2482 lStatus = BAD_VALUE;
2483 goto Exit;
2484 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002485 // look for the thread where the specified audio session is present
2486 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2487 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2488 io = mPlaybackThreads.keyAt(i);
2489 break;
2490 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002491 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002492 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002493 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2494 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2495 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002496 break;
2497 }
2498 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002499 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002500 // If no output thread contains the requested session ID, default to
2501 // first output. The effect chain will be moved to the correct output
2502 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002503 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002504 io = mPlaybackThreads.keyAt(0);
2505 }
Steve Block3856b092011-10-20 11:56:00 +01002506 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002507 }
2508 ThreadBase *thread = checkRecordThread_l(io);
2509 if (thread == NULL) {
2510 thread = checkPlaybackThread_l(io);
2511 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002512 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002513 lStatus = BAD_VALUE;
2514 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002515 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002516 } else {
2517 // Check if one effect chain was awaiting for an effect to be created on this
2518 // session and used it instead of creating a new one.
2519 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2520 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002521 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002522 thread->addEffectChain_l(chain);
2523 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002524 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002525
Eric Laurent021cf962014-05-13 10:18:14 -07002526 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002527
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002528 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002529 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2530 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002531 if (handle != 0 && id != NULL) {
2532 *id = handle->id();
2533 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002534 if (handle == 0) {
2535 // remove local strong reference to Client with mClientLock held
2536 Mutex::Autolock _cl(mClientLock);
2537 client.clear();
2538 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002539 }
2540
2541Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002542 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002543 return handle;
2544}
2545
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002546status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2547 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002548{
Steve Block3856b092011-10-20 11:56:00 +01002549 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002550 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002551 Mutex::Autolock _l(mLock);
2552 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002553 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002554 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002555 }
Eric Laurentde070132010-07-13 04:45:46 -07002556 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2557 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002558 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002559 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002560 }
Eric Laurentde070132010-07-13 04:45:46 -07002561 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2562 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002563 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002564 return BAD_VALUE;
2565 }
2566
2567 Mutex::Autolock _dl(dstThread->mLock);
2568 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002569 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002570}
2571
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002572// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002573status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002574 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002575 AudioFlinger::PlaybackThread *dstThread,
2576 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002577{
Steve Block3856b092011-10-20 11:56:00 +01002578 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002579 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002580
Eric Laurent59255e42011-07-27 19:49:51 -07002581 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002582 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002583 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002584 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002585 return INVALID_OPERATION;
2586 }
2587
Andy Hung9a592762014-07-21 21:56:01 -07002588 // Check whether the destination thread has a channel count of FCC_2, which is
2589 // currently required for (most) effects. Prevent moving the effect chain here rather
2590 // than disabling the addEffect_l() call in dstThread below.
2591 if (dstThread->mChannelCount != FCC_2) {
2592 ALOGW("moveEffectChain_l() effect chain failed because"
2593 " destination thread %p channel count(%u) != %u",
2594 dstThread, dstThread->mChannelCount, FCC_2);
2595 return INVALID_OPERATION;
2596 }
2597
Eric Laurent39e94f82010-07-28 01:32:47 -07002598 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002599 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002600 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002601 // removed.
2602 srcThread->removeEffectChain_l(chain);
2603
2604 // transfer all effects one by one so that new effect chain is created on new thread with
2605 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002606 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002607 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002608 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002609 Vector< sp<EffectModule> > removed;
2610 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002611 while (effect != 0) {
2612 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002613 removed.add(effect);
2614 status = dstThread->addEffect_l(effect);
2615 if (status != NO_ERROR) {
2616 break;
2617 }
Eric Laurentec35a142011-10-05 17:42:25 -07002618 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2619 if (effect->state() == EffectModule::ACTIVE ||
2620 effect->state() == EffectModule::STOPPING) {
2621 effect->start();
2622 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002623 // if the move request is not received from audio policy manager, the effect must be
2624 // re-registered with the new strategy and output
2625 if (dstChain == 0) {
2626 dstChain = effect->chain().promote();
2627 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002628 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002629 status = NO_INIT;
2630 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002631 }
2632 strategy = dstChain->strategy();
2633 }
2634 if (reRegister) {
2635 AudioSystem::unregisterEffect(effect->id());
2636 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002637 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002638 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002639 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002640 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002641 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002642 }
Eric Laurentde070132010-07-13 04:45:46 -07002643 effect = chain->getEffectFromId_l(0);
2644 }
2645
Eric Laurent5baf2af2013-09-12 17:37:00 -07002646 if (status != NO_ERROR) {
2647 for (size_t i = 0; i < removed.size(); i++) {
2648 srcThread->addEffect_l(removed[i]);
2649 if (dstChain != 0 && reRegister) {
2650 AudioSystem::unregisterEffect(removed[i]->id());
2651 AudioSystem::registerEffect(&removed[i]->desc(),
2652 srcThread->id(),
2653 strategy,
2654 sessionId,
2655 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002656 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002657 }
2658 }
2659 }
2660
2661 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002662}
2663
Eric Laurent5baf2af2013-09-12 17:37:00 -07002664bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002665{
2666 if (mGlobalEffectEnableTime != 0 &&
2667 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2668 return true;
2669 }
2670
2671 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2672 sp<EffectChain> ec =
2673 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002674 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002675 return true;
2676 }
2677 }
2678 return false;
2679}
2680
Eric Laurent5baf2af2013-09-12 17:37:00 -07002681void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002682{
2683 Mutex::Autolock _l(mLock);
2684
2685 mGlobalEffectEnableTime = systemTime();
2686
2687 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2688 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2689 if (t->mType == ThreadBase::OFFLOAD) {
2690 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2691 }
2692 }
2693
2694}
2695
Eric Laurentaaa44472014-09-12 17:41:50 -07002696status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2697{
2698 audio_session_t session = (audio_session_t)chain->sessionId();
2699 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2700 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2701 if (index >= 0) {
2702 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2703 return ALREADY_EXISTS;
2704 }
2705 mOrphanEffectChains.add(session, chain);
2706 return NO_ERROR;
2707}
2708
2709sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2710{
2711 sp<EffectChain> chain;
2712 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2713 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2714 if (index >= 0) {
2715 chain = mOrphanEffectChains.valueAt(index);
2716 mOrphanEffectChains.removeItemsAt(index);
2717 }
2718 return chain;
2719}
2720
2721bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2722{
2723 Mutex::Autolock _l(mLock);
2724 audio_session_t session = (audio_session_t)effect->sessionId();
2725 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2726 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2727 if (index >= 0) {
2728 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2729 if (chain->removeEffect_l(effect) == 0) {
2730 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2731 mOrphanEffectChains.removeItemsAt(index);
2732 }
2733 return true;
2734 }
2735 return false;
2736}
2737
2738
Glenn Kastenda6ef132013-01-10 12:31:01 -08002739struct Entry {
2740#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2741 char mName[MAX_NAME];
2742};
2743
2744int comparEntry(const void *p1, const void *p2)
2745{
2746 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2747}
2748
Glenn Kasten46909e72013-02-26 09:20:22 -08002749#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002750void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002751{
Eric Laurent81784c32012-11-19 14:55:58 -08002752 NBAIO_Source *teeSource = source.get();
2753 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002754 // .wav rotation
2755 // There is a benign race condition if 2 threads call this simultaneously.
2756 // They would both traverse the directory, but the result would simply be
2757 // failures at unlink() which are ignored. It's also unlikely since
2758 // normally dumpsys is only done by bugreport or from the command line.
2759 char teePath[32+256];
2760 strcpy(teePath, "/data/misc/media");
2761 size_t teePathLen = strlen(teePath);
2762 DIR *dir = opendir(teePath);
2763 teePath[teePathLen++] = '/';
2764 if (dir != NULL) {
2765#define MAX_SORT 20 // number of entries to sort
2766#define MAX_KEEP 10 // number of entries to keep
2767 struct Entry entries[MAX_SORT];
2768 size_t entryCount = 0;
2769 while (entryCount < MAX_SORT) {
2770 struct dirent de;
2771 struct dirent *result = NULL;
2772 int rc = readdir_r(dir, &de, &result);
2773 if (rc != 0) {
2774 ALOGW("readdir_r failed %d", rc);
2775 break;
2776 }
2777 if (result == NULL) {
2778 break;
2779 }
2780 if (result != &de) {
2781 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2782 break;
2783 }
2784 // ignore non .wav file entries
2785 size_t nameLen = strlen(de.d_name);
2786 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2787 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2788 continue;
2789 }
2790 strcpy(entries[entryCount++].mName, de.d_name);
2791 }
2792 (void) closedir(dir);
2793 if (entryCount > MAX_KEEP) {
2794 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2795 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2796 strcpy(&teePath[teePathLen], entries[i].mName);
2797 (void) unlink(teePath);
2798 }
2799 }
2800 } else {
2801 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002802 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002803 }
2804 }
Eric Laurent81784c32012-11-19 14:55:58 -08002805 char teeTime[16];
2806 struct timeval tv;
2807 gettimeofday(&tv, NULL);
2808 struct tm tm;
2809 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002810 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2811 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2812 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2813 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002814 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002815 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002816 char wavHeader[44];
2817 memcpy(wavHeader,
2818 "RIFF\0\0\0\0WAVEfmt \20\0\0\0\1\0\2\0\104\254\0\0\0\0\0\0\4\0\20\0data\0\0\0\0",
2819 sizeof(wavHeader));
2820 NBAIO_Format format = teeSource->format();
2821 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002822 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002823 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002824 wavHeader[22] = channelCount; // number of channels
2825 wavHeader[24] = sampleRate; // sample rate
2826 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002827 wavHeader[32] = frameSize; // block alignment
2828 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002829 write(teeFd, wavHeader, sizeof(wavHeader));
2830 size_t total = 0;
2831 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002832#define TEE_SINK_READ 1024 // frames per I/O operation
2833 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002834 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002835 size_t count = TEE_SINK_READ;
2836 ssize_t actual = teeSource->read(buffer, count,
2837 AudioBufferProvider::kInvalidPTS);
2838 bool wasFirstRead = firstRead;
2839 firstRead = false;
2840 if (actual <= 0) {
2841 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2842 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002843 }
Eric Laurent81784c32012-11-19 14:55:58 -08002844 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002845 }
Eric Laurent81784c32012-11-19 14:55:58 -08002846 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002847 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002848 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002849 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002850 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002851 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002852 uint32_t temp = 44 + total * frameSize - 8;
2853 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002854 write(teeFd, &temp, sizeof(temp));
2855 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002856 temp = total * frameSize;
2857 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002858 write(teeFd, &temp, sizeof(temp));
2859 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002860 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002861 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002862 }
Eric Laurent59255e42011-07-27 19:49:51 -07002863 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002864 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002865 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002866 }
Eric Laurent59255e42011-07-27 19:49:51 -07002867 }
2868 }
2869}
Glenn Kasten46909e72013-02-26 09:20:22 -08002870#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002871
Mathias Agopian65ab4712010-07-14 17:59:35 -07002872// ----------------------------------------------------------------------------
2873
2874status_t AudioFlinger::onTransact(
2875 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2876{
2877 return BnAudioFlinger::onTransact(code, data, reply, flags);
2878}
2879
Mathias Agopian65ab4712010-07-14 17:59:35 -07002880}; // namespace android