blob: 1e134d0409d0122d783aff7df32e05842e33078b [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";
Mathias Agopian65ab4712010-07-14 17:59:35 -070085
Glenn Kasten58912562012-04-03 10:45:00 -070086
John Grossman4ff14ba2012-02-08 16:37:41 -080087nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080088
Eric Laurent81784c32012-11-19 14:55:58 -080089uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070090
Glenn Kasten46909e72013-02-26 09:20:22 -080091#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080092bool AudioFlinger::mTeeSinkInputEnabled = false;
93bool AudioFlinger::mTeeSinkOutputEnabled = false;
94bool AudioFlinger::mTeeSinkTrackEnabled = false;
95
96size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
97size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
98size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -080099#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800100
Eric Laurent813e2a72013-08-31 12:59:48 -0700101// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
102// we define a minimum time during which a global effect is considered enabled.
103static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
104
Mathias Agopian65ab4712010-07-14 17:59:35 -0700105// ----------------------------------------------------------------------------
106
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700107static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700108{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700109 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700110 int rc;
111
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700112 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
113 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
114 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
115 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700116 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700117 }
118 rc = audio_hw_device_open(mod, dev);
119 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
120 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
121 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700122 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700123 }
124 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
125 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
126 rc = BAD_VALUE;
127 goto out;
128 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700129 return 0;
130
131out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700132 *dev = NULL;
133 return rc;
134}
135
Mathias Agopian65ab4712010-07-14 17:59:35 -0700136// ----------------------------------------------------------------------------
137
138AudioFlinger::AudioFlinger()
139 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800140 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700141 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800142 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800143 mMasterMute(false),
144 mNextUniqueId(1),
145 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700146 mBtNrecIsOff(false),
147 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700148 mIsDeviceTypeKnown(false),
149 mGlobalEffectEnableTime(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700150{
Glenn Kasten949a9262013-04-16 12:35:20 -0700151 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800152 char value[PROPERTY_VALUE_MAX];
153 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
154 if (doLog) {
155 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
156 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800157#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800158 (void) property_get("ro.debuggable", value, "0");
159 int debuggable = atoi(value);
160 int teeEnabled = 0;
161 if (debuggable) {
162 (void) property_get("af.tee", value, "0");
163 teeEnabled = atoi(value);
164 }
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700165 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800166 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700167 }
168 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800169 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700170 }
171 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800172 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700173 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800174#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700175}
176
177void AudioFlinger::onFirstRef()
178{
Dima Zavin799a70e2011-04-18 16:57:27 -0700179 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700180
Eric Laurent93575202011-01-18 18:39:02 -0800181 Mutex::Autolock _l(mLock);
182
Dima Zavin799a70e2011-04-18 16:57:27 -0700183 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800184 char val_str[PROPERTY_VALUE_MAX] = { 0 };
185 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
186 uint32_t int_val;
187 if (1 == sscanf(val_str, "%u", &int_val)) {
188 mStandbyTimeInNsecs = milliseconds(int_val);
189 ALOGI("Using %u mSec as standby time.", int_val);
190 } else {
191 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
192 ALOGI("Using default %u mSec as standby time.",
193 (uint32_t)(mStandbyTimeInNsecs / 1000000));
194 }
195 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196
Eric Laurenta4c5a552012-03-29 10:12:40 -0700197 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700198}
199
200AudioFlinger::~AudioFlinger()
201{
202 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700203 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700204 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 }
206 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700207 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700208 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700209 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700210
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800211 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
212 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700213 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
214 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700215 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700216
217 // Tell media.log service about any old writers that still need to be unregistered
218 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
219 if (binder != 0) {
220 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
221 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
222 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
223 mUnregisteredWriters.pop();
224 mediaLogService->unregisterWriter(iMemory);
225 }
226 }
227
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228}
229
Eric Laurenta4c5a552012-03-29 10:12:40 -0700230static const char * const audio_interfaces[] = {
231 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
232 AUDIO_HARDWARE_MODULE_ID_A2DP,
233 AUDIO_HARDWARE_MODULE_ID_USB,
234};
235#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
236
John Grossmanee578c02012-07-23 17:05:46 -0700237AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
238 audio_module_handle_t module,
239 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700240{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700241 // if module is 0, the request comes from an old policy manager and we should load
242 // well known modules
243 if (module == 0) {
244 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
245 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
246 loadHwModule_l(audio_interfaces[i]);
247 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700248 // then try to find a module supporting the requested device.
249 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
250 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
251 audio_hw_device_t *dev = audioHwDevice->hwDevice();
252 if ((dev->get_supported_devices != NULL) &&
253 (dev->get_supported_devices(dev) & devices) == devices)
254 return audioHwDevice;
255 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700256 } else {
257 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700258 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
259 if (audioHwDevice != NULL) {
260 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700261 }
262 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700263
Dima Zavin799a70e2011-04-18 16:57:27 -0700264 return NULL;
265}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266
Glenn Kasten0f11b512014-01-31 16:18:54 -0800267void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268{
269 const size_t SIZE = 256;
270 char buffer[SIZE];
271 String8 result;
272
273 result.append("Clients:\n");
274 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800275 sp<Client> client = mClients.valueAt(i).promote();
276 if (client != 0) {
277 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
278 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279 }
280 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700281
Eric Laurentd1b28d42013-09-18 18:47:13 -0700282 result.append("Notification Clients:\n");
283 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
284 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
285 result.append(buffer);
286 }
287
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700288 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800289 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700290 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
291 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800292 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700293 result.append(buffer);
294 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700296}
297
298
Glenn Kasten0f11b512014-01-31 16:18:54 -0800299void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300{
301 const size_t SIZE = 256;
302 char buffer[SIZE];
303 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800304 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305
John Grossman4ff14ba2012-02-08 16:37:41 -0800306 snprintf(buffer, SIZE, "Hardware status: %d\n"
307 "Standby Time mSec: %u\n",
308 hardwareStatus,
309 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310 result.append(buffer);
311 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312}
313
Glenn Kasten0f11b512014-01-31 16:18:54 -0800314void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315{
316 const size_t SIZE = 256;
317 char buffer[SIZE];
318 String8 result;
319 snprintf(buffer, SIZE, "Permission Denial: "
320 "can't dump AudioFlinger from pid=%d, uid=%d\n",
321 IPCThreadState::self()->getCallingPid(),
322 IPCThreadState::self()->getCallingUid());
323 result.append(buffer);
324 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325}
326
Eric Laurent81784c32012-11-19 14:55:58 -0800327bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328{
329 bool locked = false;
330 for (int i = 0; i < kDumpLockRetries; ++i) {
331 if (mutex.tryLock() == NO_ERROR) {
332 locked = true;
333 break;
334 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800335 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 }
337 return locked;
338}
339
340status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
341{
Glenn Kasten44deb052012-02-05 18:09:08 -0800342 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343 dumpPermissionDenial(fd, args);
344 } else {
345 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800346 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 if (!hardwareLocked) {
348 String8 result(kHardwareLockedString);
349 write(fd, result.string(), result.size());
350 } else {
351 mHardwareLock.unlock();
352 }
353
Eric Laurent81784c32012-11-19 14:55:58 -0800354 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355
356 // failed to lock - AudioFlinger is probably deadlocked
357 if (!locked) {
358 String8 result(kDeadlockedString);
359 write(fd, result.string(), result.size());
360 }
361
362 dumpClients(fd, args);
363 dumpInternals(fd, args);
364
365 // dump playback threads
366 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
367 mPlaybackThreads.valueAt(i)->dump(fd, args);
368 }
369
370 // dump record threads
371 for (size_t i = 0; i < mRecordThreads.size(); i++) {
372 mRecordThreads.valueAt(i)->dump(fd, args);
373 }
374
Dima Zavin799a70e2011-04-18 16:57:27 -0700375 // dump all hardware devs
376 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700377 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700378 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700380
Glenn Kasten46909e72013-02-26 09:20:22 -0800381#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700382 // dump the serially shared record tee sink
383 if (mRecordTeeSource != 0) {
384 dumpTee(fd, mRecordTeeSource);
385 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800386#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700387
Glenn Kastend65d73c2012-06-22 17:21:07 -0700388 if (locked) {
389 mLock.unlock();
390 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800391
392 // append a copy of media.log here by forwarding fd to it, but don't attempt
393 // to lookup the service if it's not running, as it will block for a second
394 if (mLogMemoryDealer != 0) {
395 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
396 if (binder != 0) {
397 fdprintf(fd, "\nmedia.log:\n");
398 Vector<String16> args;
399 binder->dump(fd, args);
400 }
401 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402 }
403 return NO_ERROR;
404}
405
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800406sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
407{
408 // If pid is already in the mClients wp<> map, then use that entry
409 // (for which promote() is always != 0), otherwise create a new entry and Client.
410 sp<Client> client = mClients.valueFor(pid).promote();
411 if (client == 0) {
412 client = new Client(this, pid);
413 mClients.add(pid, client);
414 }
415
416 return client;
417}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418
Glenn Kasten9e58b552013-01-18 15:09:48 -0800419sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
420{
Glenn Kasten481fb672013-09-30 14:39:28 -0700421 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800422 if (mLogMemoryDealer == 0) {
423 return new NBLog::Writer();
424 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800425 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700426 // Similarly if we can't contact the media.log service, also return a dummy writer
427 if (binder == 0) {
428 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800429 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700430 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
431 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
432 // If allocation fails, consult the vector of previously unregistered writers
433 // and garbage-collect one or more them until an allocation succeeds
434 if (shared == 0) {
435 Mutex::Autolock _l(mUnregisteredWritersLock);
436 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
437 {
438 // Pick the oldest stale writer to garbage-collect
439 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
440 mUnregisteredWriters.removeAt(0);
441 mediaLogService->unregisterWriter(iMemory);
442 // Now the media.log remote reference to IMemory is gone. When our last local
443 // reference to IMemory also drops to zero at end of this block,
444 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
445 }
446 // Re-attempt the allocation
447 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
448 if (shared != 0) {
449 goto success;
450 }
451 }
452 // Even after garbage-collecting all old writers, there is still not enough memory,
453 // so return a dummy writer
454 return new NBLog::Writer();
455 }
456success:
457 mediaLogService->registerWriter(shared, size, name);
458 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800459}
460
461void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
462{
Glenn Kasten685ef092013-02-04 08:15:34 -0800463 if (writer == 0) {
464 return;
465 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800466 sp<IMemory> iMemory(writer->getIMemory());
467 if (iMemory == 0) {
468 return;
469 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700470 // Rather than removing the writer immediately, append it to a queue of old writers to
471 // be garbage-collected later. This allows us to continue to view old logs for a while.
472 Mutex::Autolock _l(mUnregisteredWritersLock);
473 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800474}
475
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476// IAudioFlinger interface
477
478
479sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800480 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700481 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800482 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700483 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800484 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800485 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700486 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800487 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800488 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700489 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700490 String8& name,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800491 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700492 status_t *status)
493{
494 sp<PlaybackThread::Track> track;
495 sp<TrackHandle> trackHandle;
496 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700497 status_t lStatus;
498 int lSessionId;
499
Glenn Kasten263709e2012-01-06 08:40:01 -0800500 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
501 // but if someone uses binder directly they could bypass that and cause us to crash
502 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000503 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700504 lStatus = BAD_VALUE;
505 goto Exit;
506 }
507
Glenn Kasten60a83922012-06-21 12:56:37 -0700508 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
509 // and we don't yet support 8.24 or 32-bit PCM
510 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
511 ALOGE("createTrack() invalid format %d", format);
512 lStatus = BAD_VALUE;
513 goto Exit;
514 }
515
Glenn Kasten663c2242013-09-24 11:52:37 -0700516 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
517 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
518 lStatus = BAD_VALUE;
519 goto Exit;
520 }
521
Mathias Agopian65ab4712010-07-14 17:59:35 -0700522 {
523 Mutex::Autolock _l(mLock);
524 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700525 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800527 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700528 lStatus = BAD_VALUE;
529 goto Exit;
530 }
531
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800532 pid_t pid = IPCThreadState::self()->getCallingPid();
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800533
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800534 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700535
Steve Block3856b092011-10-20 11:56:00 +0100536 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700537 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700538 // check if an effect chain with the same session ID is present on another
539 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700540 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700541 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
542 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700543 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700544 if (sessions & PlaybackThread::EFFECT_SESSION) {
545 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700546 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700547 }
Eric Laurentde070132010-07-13 04:45:46 -0700548 }
549 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 lSessionId = *sessionId;
551 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700552 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700553 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700554 if (sessionId != NULL) {
555 *sessionId = lSessionId;
556 }
557 }
Steve Block3856b092011-10-20 11:56:00 +0100558 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700559
560 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800561 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Glenn Kasten0cde0762014-01-16 15:06:36 -0800562 LOG_ALWAYS_FATAL_IF((track != 0) != (lStatus == NO_ERROR));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700563 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700564
565 // move effect chain to this output thread if an effect on same session was waiting
566 // for a track to be created
567 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700568 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700569 Mutex::Autolock _dl(thread->mLock);
570 Mutex::Autolock _sl(effectThread->mLock);
571 moveEffectChain_l(lSessionId, effectThread, thread, true);
572 }
Eric Laurenta011e352012-03-29 15:51:43 -0700573
574 // Look for sync events awaiting for a session to be used.
575 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
576 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
577 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700578 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700579 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700580 } else {
581 mPendingSyncEvents[i]->cancel();
582 }
Eric Laurenta011e352012-03-29 15:51:43 -0700583 mPendingSyncEvents.removeAt(i);
584 i--;
585 }
586 }
587 }
Glenn Kastene198c362013-08-13 09:13:36 -0700588
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589 }
Glenn Kastene198c362013-08-13 09:13:36 -0700590
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700592 // s for server's pid, n for normal mixer name, f for fast index
593 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
594 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 trackHandle = new TrackHandle(track);
596 } else {
597 // remove local strong reference to Client before deleting the Track so that the Client
598 // destructor is called by the TrackBase destructor with mLock held
599 client.clear();
600 track.clear();
601 }
602
603Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700604 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605 return trackHandle;
606}
607
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800608uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609{
610 Mutex::Autolock _l(mLock);
611 PlaybackThread *thread = checkPlaybackThread_l(output);
612 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000613 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700614 return 0;
615 }
616 return thread->sampleRate();
617}
618
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800619int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700620{
621 Mutex::Autolock _l(mLock);
622 PlaybackThread *thread = checkPlaybackThread_l(output);
623 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000624 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625 return 0;
626 }
627 return thread->channelCount();
628}
629
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800630audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631{
632 Mutex::Autolock _l(mLock);
633 PlaybackThread *thread = checkPlaybackThread_l(output);
634 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000635 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800636 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 }
638 return thread->format();
639}
640
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800641size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642{
643 Mutex::Autolock _l(mLock);
644 PlaybackThread *thread = checkPlaybackThread_l(output);
645 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000646 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647 return 0;
648 }
Glenn Kasten58912562012-04-03 10:45:00 -0700649 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
650 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651 return thread->frameCount();
652}
653
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800654uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655{
656 Mutex::Autolock _l(mLock);
657 PlaybackThread *thread = checkPlaybackThread_l(output);
658 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800659 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660 return 0;
661 }
662 return thread->latency();
663}
664
665status_t AudioFlinger::setMasterVolume(float value)
666{
Eric Laurenta1884f92011-08-23 08:25:03 -0700667 status_t ret = initCheck();
668 if (ret != NO_ERROR) {
669 return ret;
670 }
671
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 // check calling permissions
673 if (!settingsAllowed()) {
674 return PERMISSION_DENIED;
675 }
676
Eric Laurenta4c5a552012-03-29 10:12:40 -0700677 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700678 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700679
John Grossmanee578c02012-07-23 17:05:46 -0700680 // Set master volume in the HALs which support it.
681 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
682 AutoMutex lock(mHardwareLock);
683 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800684
John Grossmanee578c02012-07-23 17:05:46 -0700685 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
686 if (dev->canSetMasterVolume()) {
687 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800688 }
John Grossmanee578c02012-07-23 17:05:46 -0700689 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691
John Grossmanee578c02012-07-23 17:05:46 -0700692 // Now set the master volume in each playback thread. Playback threads
693 // assigned to HALs which do not have master volume support will apply
694 // master volume during the mix operation. Threads with HALs which do
695 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800696 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700697 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698
699 return NO_ERROR;
700}
701
Glenn Kastenf78aee72012-01-04 11:00:47 -0800702status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703{
Eric Laurenta1884f92011-08-23 08:25:03 -0700704 status_t ret = initCheck();
705 if (ret != NO_ERROR) {
706 return ret;
707 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708
709 // check calling permissions
710 if (!settingsAllowed()) {
711 return PERMISSION_DENIED;
712 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800713 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000714 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715 return BAD_VALUE;
716 }
717
718 { // scope for the lock
719 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700720 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700721 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700722 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723 mHardwareStatus = AUDIO_HW_IDLE;
724 }
725
726 if (NO_ERROR == ret) {
727 Mutex::Autolock _l(mLock);
728 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800729 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700730 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700731 }
732
733 return ret;
734}
735
736status_t AudioFlinger::setMicMute(bool state)
737{
Eric Laurenta1884f92011-08-23 08:25:03 -0700738 status_t ret = initCheck();
739 if (ret != NO_ERROR) {
740 return ret;
741 }
742
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743 // check calling permissions
744 if (!settingsAllowed()) {
745 return PERMISSION_DENIED;
746 }
747
748 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700749 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700751 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752 mHardwareStatus = AUDIO_HW_IDLE;
753 return ret;
754}
755
756bool AudioFlinger::getMicMute() const
757{
Eric Laurenta1884f92011-08-23 08:25:03 -0700758 status_t ret = initCheck();
759 if (ret != NO_ERROR) {
760 return false;
761 }
762
Dima Zavinfce7a472011-04-19 22:30:36 -0700763 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800764 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700765 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700767 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768 mHardwareStatus = AUDIO_HW_IDLE;
769 return state;
770}
771
772status_t AudioFlinger::setMasterMute(bool muted)
773{
John Grossmand8f178d2012-07-20 14:51:35 -0700774 status_t ret = initCheck();
775 if (ret != NO_ERROR) {
776 return ret;
777 }
778
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779 // check calling permissions
780 if (!settingsAllowed()) {
781 return PERMISSION_DENIED;
782 }
783
John Grossmanee578c02012-07-23 17:05:46 -0700784 Mutex::Autolock _l(mLock);
785 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700786
John Grossmanee578c02012-07-23 17:05:46 -0700787 // Set master mute in the HALs which support it.
788 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
789 AutoMutex lock(mHardwareLock);
790 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700791
John Grossmanee578c02012-07-23 17:05:46 -0700792 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
793 if (dev->canSetMasterMute()) {
794 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700795 }
John Grossmanee578c02012-07-23 17:05:46 -0700796 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700797 }
798
John Grossmanee578c02012-07-23 17:05:46 -0700799 // Now set the master mute in each playback thread. Playback threads
800 // assigned to HALs which do not have master mute support will apply master
801 // mute during the mix operation. Threads with HALs which do support master
802 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800803 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700804 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805
806 return NO_ERROR;
807}
808
809float AudioFlinger::masterVolume() const
810{
Glenn Kasten98067102011-12-13 11:47:54 -0800811 Mutex::Autolock _l(mLock);
812 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700813}
814
815bool AudioFlinger::masterMute() const
816{
Glenn Kasten98067102011-12-13 11:47:54 -0800817 Mutex::Autolock _l(mLock);
818 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819}
820
John Grossman4ff14ba2012-02-08 16:37:41 -0800821float AudioFlinger::masterVolume_l() const
822{
John Grossman4ff14ba2012-02-08 16:37:41 -0800823 return mMasterVolume;
824}
825
John Grossmand8f178d2012-07-20 14:51:35 -0700826bool AudioFlinger::masterMute_l() const
827{
John Grossmanee578c02012-07-23 17:05:46 -0700828 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700829}
830
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800831status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
832 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833{
834 // check calling permissions
835 if (!settingsAllowed()) {
836 return PERMISSION_DENIED;
837 }
838
Glenn Kasten263709e2012-01-06 08:40:01 -0800839 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000840 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841 return BAD_VALUE;
842 }
843
844 AutoMutex lock(mLock);
845 PlaybackThread *thread = NULL;
846 if (output) {
847 thread = checkPlaybackThread_l(output);
848 if (thread == NULL) {
849 return BAD_VALUE;
850 }
851 }
852
853 mStreamTypes[stream].volume = value;
854
855 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800856 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700857 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 }
859 } else {
860 thread->setStreamVolume(stream, value);
861 }
862
863 return NO_ERROR;
864}
865
Glenn Kastenfff6d712012-01-12 16:38:12 -0800866status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867{
868 // check calling permissions
869 if (!settingsAllowed()) {
870 return PERMISSION_DENIED;
871 }
872
Glenn Kasten263709e2012-01-06 08:40:01 -0800873 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700874 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000875 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876 return BAD_VALUE;
877 }
878
Eric Laurent93575202011-01-18 18:39:02 -0800879 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880 mStreamTypes[stream].mute = muted;
881 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700882 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700883
884 return NO_ERROR;
885}
886
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800887float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888{
Glenn Kasten263709e2012-01-06 08:40:01 -0800889 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890 return 0.0f;
891 }
892
893 AutoMutex lock(mLock);
894 float volume;
895 if (output) {
896 PlaybackThread *thread = checkPlaybackThread_l(output);
897 if (thread == NULL) {
898 return 0.0f;
899 }
900 volume = thread->streamVolume(stream);
901 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800902 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700903 }
904
905 return volume;
906}
907
Glenn Kastenfff6d712012-01-12 16:38:12 -0800908bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909{
Glenn Kasten263709e2012-01-06 08:40:01 -0800910 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 return true;
912 }
913
Glenn Kasten6637baa2012-01-09 09:40:36 -0800914 AutoMutex lock(mLock);
915 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700916}
917
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800918status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700920 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
921 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800922
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923 // check calling permissions
924 if (!settingsAllowed()) {
925 return PERMISSION_DENIED;
926 }
927
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 // ioHandle == 0 means the parameters are global to the audio hardware interface
929 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700930 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700931 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800932 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700933 AutoMutex lock(mHardwareLock);
934 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
935 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
936 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
937 status_t result = dev->set_parameters(dev, keyValuePairs.string());
938 final_result = result ?: final_result;
939 }
940 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800941 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700942 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
943 AudioParameter param = AudioParameter(keyValuePairs);
944 String8 value;
945 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700946 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
947 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700948 for (size_t i = 0; i < mRecordThreads.size(); i++) {
949 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700950 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700951 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
952 // collect all of the thread's session IDs
953 KeyedVector<int, bool> ids = thread->sessionIds();
954 // suspend effects associated with those session IDs
955 for (size_t j = 0; j < ids.size(); ++j) {
956 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700957 thread->setEffectSuspended(FX_IID_AEC,
958 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700959 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700960 thread->setEffectSuspended(FX_IID_NS,
961 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700962 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700963 }
964 }
Eric Laurentbee53372011-08-29 12:42:48 -0700965 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700966 }
967 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700968 String8 screenState;
969 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
970 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800971 if (isOff != (AudioFlinger::mScreenState & 1)) {
972 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700973 }
974 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700975 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700976 }
977
978 // hold a strong ref on thread in case closeOutput() or closeInput() is called
979 // and the thread is exited once the lock is released
980 sp<ThreadBase> thread;
981 {
982 Mutex::Autolock _l(mLock);
983 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700984 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800986 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700987 // indicate output device change to all input threads for pre processing
988 AudioParameter param = AudioParameter(keyValuePairs);
989 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700990 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
991 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700992 for (size_t i = 0; i < mRecordThreads.size(); i++) {
993 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
994 }
995 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996 }
997 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800998 if (thread != 0) {
999 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001000 }
1001 return BAD_VALUE;
1002}
1003
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001004String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001005{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001006 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1007 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008
Eric Laurenta4c5a552012-03-29 10:12:40 -07001009 Mutex::Autolock _l(mLock);
1010
Mathias Agopian65ab4712010-07-14 17:59:35 -07001011 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001012 String8 out_s8;
1013
Dima Zavin799a70e2011-04-18 16:57:27 -07001014 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001015 char *s;
1016 {
1017 AutoMutex lock(mHardwareLock);
1018 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001019 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001020 s = dev->get_parameters(dev, keys.string());
1021 mHardwareStatus = AUDIO_HW_IDLE;
1022 }
John Grossmanef7740b2012-02-09 11:28:36 -08001023 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001024 free(s);
1025 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001026 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001027 }
1028
Mathias Agopian65ab4712010-07-14 17:59:35 -07001029 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1030 if (playbackThread != NULL) {
1031 return playbackThread->getParameters(keys);
1032 }
1033 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1034 if (recordThread != NULL) {
1035 return recordThread->getParameters(keys);
1036 }
1037 return String8("");
1038}
1039
Glenn Kastendd8104c2012-07-02 12:42:44 -07001040size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1041 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042{
Eric Laurenta1884f92011-08-23 08:25:03 -07001043 status_t ret = initCheck();
1044 if (ret != NO_ERROR) {
1045 return 0;
1046 }
1047
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001048 AutoMutex lock(mHardwareLock);
1049 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001050 struct audio_config config;
1051 memset(&config, 0, sizeof(config));
1052 config.sample_rate = sampleRate;
1053 config.channel_mask = channelMask;
1054 config.format = format;
1055
John Grossmanee578c02012-07-23 17:05:46 -07001056 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1057 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001058 mHardwareStatus = AUDIO_HW_IDLE;
1059 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060}
1061
Glenn Kasten5f972c02014-01-13 09:59:31 -08001062uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064 Mutex::Autolock _l(mLock);
1065
1066 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1067 if (recordThread != NULL) {
1068 return recordThread->getInputFramesLost();
1069 }
1070 return 0;
1071}
1072
1073status_t AudioFlinger::setVoiceVolume(float value)
1074{
Eric Laurenta1884f92011-08-23 08:25:03 -07001075 status_t ret = initCheck();
1076 if (ret != NO_ERROR) {
1077 return ret;
1078 }
1079
Mathias Agopian65ab4712010-07-14 17:59:35 -07001080 // check calling permissions
1081 if (!settingsAllowed()) {
1082 return PERMISSION_DENIED;
1083 }
1084
1085 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001086 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001087 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001088 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089 mHardwareStatus = AUDIO_HW_IDLE;
1090
1091 return ret;
1092}
1093
Glenn Kasten26c77552012-11-16 12:01:44 -08001094status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001095 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001096{
1097 status_t status;
1098
1099 Mutex::Autolock _l(mLock);
1100
1101 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1102 if (playbackThread != NULL) {
1103 return playbackThread->getRenderPosition(halFrames, dspFrames);
1104 }
1105
1106 return BAD_VALUE;
1107}
1108
1109void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1110{
1111
1112 Mutex::Autolock _l(mLock);
1113
Glenn Kastenbb001922012-02-03 11:10:26 -08001114 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115 if (mNotificationClients.indexOfKey(pid) < 0) {
1116 sp<NotificationClient> notificationClient = new NotificationClient(this,
1117 client,
1118 pid);
Steve Block3856b092011-10-20 11:56:00 +01001119 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001120
1121 mNotificationClients.add(pid, notificationClient);
1122
1123 sp<IBinder> binder = client->asBinder();
1124 binder->linkToDeath(notificationClient);
1125
1126 // the config change is always sent from playback or record threads to avoid deadlock
1127 // with AudioSystem::gLock
1128 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001129 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130 }
1131
1132 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001133 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134 }
1135 }
1136}
1137
1138void AudioFlinger::removeNotificationClient(pid_t pid)
1139{
1140 Mutex::Autolock _l(mLock);
1141
Glenn Kastena3b09252012-01-20 09:19:01 -08001142 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001143
Steve Block3856b092011-10-20 11:56:00 +01001144 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001145 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001146 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001147 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001148 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001149 ALOGV(" pid %d @ %d", ref->mPid, i);
1150 if (ref->mPid == pid) {
1151 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001152 mAudioSessionRefs.removeAt(i);
1153 delete ref;
1154 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001155 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001156 } else {
1157 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001158 }
1159 }
1160 if (removed) {
1161 purgeStaleEffects_l();
1162 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163}
1164
1165// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001166void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167{
1168 size_t size = mNotificationClients.size();
1169 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001170 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1171 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001172 }
1173}
1174
1175// removeClient_l() must be called with AudioFlinger::mLock held
1176void AudioFlinger::removeClient_l(pid_t pid)
1177{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001178 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001179 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001180 mClients.removeItem(pid);
1181}
1182
Eric Laurent717e1282012-06-29 16:36:52 -07001183// getEffectThread_l() must be called with AudioFlinger::mLock held
1184sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1185{
1186 sp<PlaybackThread> thread;
1187
1188 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1189 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1190 ALOG_ASSERT(thread == 0);
1191 thread = mPlaybackThreads.valueAt(i);
1192 }
1193 }
1194
1195 return thread;
1196}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001197
Mathias Agopian65ab4712010-07-14 17:59:35 -07001198
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199
1200// ----------------------------------------------------------------------------
1201
1202AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1203 : RefBase(),
1204 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001205 // 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 -07001206 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001207 mPid(pid),
1208 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001209{
1210 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1211}
1212
1213// Client destructor must be called with AudioFlinger::mLock held
1214AudioFlinger::Client::~Client()
1215{
1216 mAudioFlinger->removeClient_l(mPid);
1217}
1218
Glenn Kasten435dbe62012-01-30 10:15:48 -08001219sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220{
1221 return mMemoryDealer;
1222}
1223
John Grossman4ff14ba2012-02-08 16:37:41 -08001224// Reserve one of the limited slots for a timed audio track associated
1225// with this client
1226bool AudioFlinger::Client::reserveTimedTrack()
1227{
1228 const int kMaxTimedTracksPerClient = 4;
1229
1230 Mutex::Autolock _l(mTimedTrackLock);
1231
1232 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1233 ALOGW("can not create timed track - pid %d has exceeded the limit",
1234 mPid);
1235 return false;
1236 }
1237
1238 mTimedTrackCount++;
1239 return true;
1240}
1241
1242// Release a slot for a timed audio track
1243void AudioFlinger::Client::releaseTimedTrack()
1244{
1245 Mutex::Autolock _l(mTimedTrackLock);
1246 mTimedTrackCount--;
1247}
1248
Mathias Agopian65ab4712010-07-14 17:59:35 -07001249// ----------------------------------------------------------------------------
1250
1251AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1252 const sp<IAudioFlingerClient>& client,
1253 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001254 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255{
1256}
1257
1258AudioFlinger::NotificationClient::~NotificationClient()
1259{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001260}
1261
Glenn Kasten0f11b512014-01-31 16:18:54 -08001262void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263{
1264 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001265 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001266}
1267
Mathias Agopian65ab4712010-07-14 17:59:35 -07001268
1269// ----------------------------------------------------------------------------
1270
Jeff Brown893a5642013-08-16 20:19:26 -07001271static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1272 return audio_is_remote_submix_device(inDevice);
1273}
1274
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001276 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001278 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001279 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001280 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001281 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001282 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001283 int *sessionId,
1284 status_t *status)
1285{
1286 sp<RecordThread::RecordTrack> recordTrack;
1287 sp<RecordHandle> recordHandle;
1288 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289 status_t lStatus;
1290 RecordThread *thread;
1291 size_t inFrameCount;
1292 int lSessionId;
1293
1294 // check calling permissions
1295 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001296 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001297 lStatus = PERMISSION_DENIED;
1298 goto Exit;
1299 }
1300
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001301 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1302 ALOGE("openRecord() invalid format %d", format);
1303 lStatus = BAD_VALUE;
1304 goto Exit;
1305 }
1306
Mathias Agopian65ab4712010-07-14 17:59:35 -07001307 // add client to list
1308 { // scope for mLock
1309 Mutex::Autolock _l(mLock);
1310 thread = checkRecordThread_l(input);
1311 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001312 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001313 lStatus = BAD_VALUE;
1314 goto Exit;
1315 }
1316
Jeff Brown893a5642013-08-16 20:19:26 -07001317 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1318 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001319 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001320 lStatus = PERMISSION_DENIED;
1321 goto Exit;
1322 }
1323
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001324 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001325 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001326
1327 // If no audio session id is provided, create one here
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001328 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001329 lSessionId = *sessionId;
1330 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001331 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001332 if (sessionId != NULL) {
1333 *sessionId = lSessionId;
1334 }
1335 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001336 // create new record track.
1337 // The record track uses one track in mHardwareMixerThread by convention.
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001338 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001339 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001340 frameCount, lSessionId,
1341 IPCThreadState::self()->getCallingUid(),
1342 flags, tid, &lStatus);
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001343 LOG_ALWAYS_FATAL_IF((recordTrack != 0) != (lStatus == NO_ERROR));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001344 }
Glenn Kastene198c362013-08-13 09:13:36 -07001345
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001346 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001347 // remove local strong reference to Client before deleting the RecordTrack so that the
1348 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349 client.clear();
1350 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001351 goto Exit;
1352 }
1353
Glenn Kasten2fc14732013-08-05 14:58:14 -07001354 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001356
1357Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001358 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001359 return recordHandle;
1360}
1361
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001362
1363
Mathias Agopian65ab4712010-07-14 17:59:35 -07001364// ----------------------------------------------------------------------------
1365
Eric Laurenta4c5a552012-03-29 10:12:40 -07001366audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1367{
1368 if (!settingsAllowed()) {
1369 return 0;
1370 }
1371 Mutex::Autolock _l(mLock);
1372 return loadHwModule_l(name);
1373}
1374
1375// loadHwModule_l() must be called with AudioFlinger::mLock held
1376audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1377{
1378 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1379 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1380 ALOGW("loadHwModule() module %s already loaded", name);
1381 return mAudioHwDevs.keyAt(i);
1382 }
1383 }
1384
Eric Laurenta4c5a552012-03-29 10:12:40 -07001385 audio_hw_device_t *dev;
1386
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001387 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001388 if (rc) {
1389 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1390 return 0;
1391 }
1392
1393 mHardwareStatus = AUDIO_HW_INIT;
1394 rc = dev->init_check(dev);
1395 mHardwareStatus = AUDIO_HW_IDLE;
1396 if (rc) {
1397 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1398 return 0;
1399 }
1400
John Grossmanee578c02012-07-23 17:05:46 -07001401 // Check and cache this HAL's level of support for master mute and master
1402 // volume. If this is the first HAL opened, and it supports the get
1403 // methods, use the initial values provided by the HAL as the current
1404 // master mute and volume settings.
1405
1406 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1407 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001408 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001409
1410 if (0 == mAudioHwDevs.size()) {
1411 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1412 if (NULL != dev->get_master_volume) {
1413 float mv;
1414 if (OK == dev->get_master_volume(dev, &mv)) {
1415 mMasterVolume = mv;
1416 }
1417 }
1418
1419 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1420 if (NULL != dev->get_master_mute) {
1421 bool mm;
1422 if (OK == dev->get_master_mute(dev, &mm)) {
1423 mMasterMute = mm;
1424 }
1425 }
1426 }
1427
Eric Laurenta4c5a552012-03-29 10:12:40 -07001428 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001429 if ((NULL != dev->set_master_volume) &&
1430 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1431 flags = static_cast<AudioHwDevice::Flags>(flags |
1432 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1433 }
1434
1435 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1436 if ((NULL != dev->set_master_mute) &&
1437 (OK == dev->set_master_mute(dev, mMasterMute))) {
1438 flags = static_cast<AudioHwDevice::Flags>(flags |
1439 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1440 }
1441
Eric Laurenta4c5a552012-03-29 10:12:40 -07001442 mHardwareStatus = AUDIO_HW_IDLE;
1443 }
1444
1445 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001446 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001447
1448 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001449 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001450
1451 return handle;
1452
1453}
1454
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001455// ----------------------------------------------------------------------------
1456
Glenn Kasten3b16c762012-11-14 08:44:39 -08001457uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001458{
1459 Mutex::Autolock _l(mLock);
1460 PlaybackThread *thread = primaryPlaybackThread_l();
1461 return thread != NULL ? thread->sampleRate() : 0;
1462}
1463
Glenn Kastene33054e2012-11-14 12:54:39 -08001464size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001465{
1466 Mutex::Autolock _l(mLock);
1467 PlaybackThread *thread = primaryPlaybackThread_l();
1468 return thread != NULL ? thread->frameCountHAL() : 0;
1469}
1470
1471// ----------------------------------------------------------------------------
1472
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001473status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1474{
1475 uid_t uid = IPCThreadState::self()->getCallingUid();
1476 if (uid != AID_SYSTEM) {
1477 return PERMISSION_DENIED;
1478 }
1479 Mutex::Autolock _l(mLock);
1480 if (mIsDeviceTypeKnown) {
1481 return INVALID_OPERATION;
1482 }
1483 mIsLowRamDevice = isLowRamDevice;
1484 mIsDeviceTypeKnown = true;
1485 return NO_ERROR;
1486}
1487
1488// ----------------------------------------------------------------------------
1489
Eric Laurenta4c5a552012-03-29 10:12:40 -07001490audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1491 audio_devices_t *pDevices,
1492 uint32_t *pSamplingRate,
1493 audio_format_t *pFormat,
1494 audio_channel_mask_t *pChannelMask,
1495 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001496 audio_output_flags_t flags,
1497 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001499 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001500 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001501 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1502 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1503 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001504 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001505 config.offload_info = *offloadInfo;
1506 }
1507
Eric Laurentbfb1b832013-01-07 09:53:42 -08001508 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001509 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001510 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001511 config.sample_rate,
1512 config.format,
1513 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001514 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001515 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001516 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001517
1518 if (pDevices == NULL || *pDevices == 0) {
1519 return 0;
1520 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001521
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522 Mutex::Autolock _l(mLock);
1523
Glenn Kasten32550952013-08-06 10:45:10 -07001524 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001525 if (outHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001526 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001527 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001528
John Grossmanee578c02012-07-23 17:05:46 -07001529 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001530 audio_io_handle_t id = nextUniqueId();
1531
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001532 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001533
Glenn Kasten32550952013-08-06 10:45:10 -07001534 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001535 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001536 id,
1537 *pDevices,
1538 (audio_output_flags_t)flags,
1539 &config,
1540 &outStream);
1541
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001542 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001543 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001544 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001545 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001546 config.sample_rate,
1547 config.format,
1548 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001549 status);
1550
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001551 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001552 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001553
Glenn Kasten32550952013-08-06 10:45:10 -07001554 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001555 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1556 thread = new OffloadThread(this, output, id, *pDevices);
1557 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1558 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001559 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1560 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001561 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001562 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563 } else {
1564 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001565 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001566 }
1567 mPlaybackThreads.add(id, thread);
1568
Glenn Kasten7c027242012-12-26 14:43:16 -08001569 if (pSamplingRate != NULL) {
1570 *pSamplingRate = config.sample_rate;
1571 }
1572 if (pFormat != NULL) {
1573 *pFormat = config.format;
1574 }
1575 if (pChannelMask != NULL) {
1576 *pChannelMask = config.channel_mask;
1577 }
1578 if (pLatencyMs != NULL) {
1579 *pLatencyMs = thread->latency();
1580 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001581
1582 // notify client processes of the new output creation
1583 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001584
1585 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001586 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001587 ALOGI("Using module %d has the primary audio interface", module);
1588 mPrimaryHardwareDev = outHwDev;
1589
1590 AutoMutex lock(mHardwareLock);
1591 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001592 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001593 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001594 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001595 return id;
1596 }
1597
1598 return 0;
1599}
1600
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001601audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1602 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001603{
1604 Mutex::Autolock _l(mLock);
1605 MixerThread *thread1 = checkMixerThread_l(output1);
1606 MixerThread *thread2 = checkMixerThread_l(output2);
1607
1608 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001609 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1610 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001611 return 0;
1612 }
1613
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001614 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001615 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1616 thread->addOutputTrack(thread2);
1617 mPlaybackThreads.add(id, thread);
1618 // notify client processes of the new output creation
1619 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1620 return id;
1621}
1622
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001623status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001624{
Glenn Kastend96c5722012-04-25 13:44:49 -07001625 return closeOutput_nonvirtual(output);
1626}
1627
1628status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1629{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001630 // keep strong reference on the playback thread so that
1631 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001632 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001633 {
1634 Mutex::Autolock _l(mLock);
1635 thread = checkPlaybackThread_l(output);
1636 if (thread == NULL) {
1637 return BAD_VALUE;
1638 }
1639
Steve Block3856b092011-10-20 11:56:00 +01001640 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001641
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001642 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001643 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001644 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001645 DuplicatingThread *dupThread =
1646 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001647 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001648
1649 }
1650 }
1651 }
1652
1653
1654 mPlaybackThreads.removeItem(output);
1655 // save all effects to the default thread
1656 if (mPlaybackThreads.size()) {
1657 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1658 if (dstThread != NULL) {
1659 // audioflinger lock is held here so the acquisition order of thread locks does not
1660 // matter
1661 Mutex::Autolock _dl(dstThread->mLock);
1662 Mutex::Autolock _sl(thread->mLock);
1663 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1664 for (size_t i = 0; i < effectChains.size(); i ++) {
1665 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001666 }
1667 }
1668 }
Glenn Kastena1117922012-01-26 10:53:32 -08001669 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001670 }
1671 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001672 // The thread entity (active unit of execution) is no longer running here,
1673 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001674
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001675 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001676 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001677 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001678 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001679 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001680 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001681 }
1682 return NO_ERROR;
1683}
1684
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001685status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001686{
1687 Mutex::Autolock _l(mLock);
1688 PlaybackThread *thread = checkPlaybackThread_l(output);
1689
1690 if (thread == NULL) {
1691 return BAD_VALUE;
1692 }
1693
Steve Block3856b092011-10-20 11:56:00 +01001694 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001695 thread->suspend();
1696
1697 return NO_ERROR;
1698}
1699
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001700status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001701{
1702 Mutex::Autolock _l(mLock);
1703 PlaybackThread *thread = checkPlaybackThread_l(output);
1704
1705 if (thread == NULL) {
1706 return BAD_VALUE;
1707 }
1708
Steve Block3856b092011-10-20 11:56:00 +01001709 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001710
1711 thread->restore();
1712
1713 return NO_ERROR;
1714}
1715
Eric Laurenta4c5a552012-03-29 10:12:40 -07001716audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1717 audio_devices_t *pDevices,
1718 uint32_t *pSamplingRate,
1719 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001720 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001721{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001722 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001723 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001724 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1725 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1726 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1727
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001728 uint32_t reqSamplingRate = config.sample_rate;
1729 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001730 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001731
1732 if (pDevices == NULL || *pDevices == 0) {
1733 return 0;
1734 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001735
Mathias Agopian65ab4712010-07-14 17:59:35 -07001736 Mutex::Autolock _l(mLock);
1737
Glenn Kasten32550952013-08-06 10:45:10 -07001738 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001739 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001740 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001741 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001742
John Grossmanee578c02012-07-23 17:05:46 -07001743 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001744 audio_io_handle_t id = nextUniqueId();
1745
Glenn Kasten32550952013-08-06 10:45:10 -07001746 audio_stream_in_t *inStream = NULL;
1747 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001748 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001749 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1750 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001751 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001752 config.sample_rate,
1753 config.format,
1754 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001755 status);
1756
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001757 // If the input could not be opened with the requested parameters and we can handle the
1758 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1759 // 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 -07001760 if (status == BAD_VALUE &&
1761 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1762 (config.sample_rate <= 2 * reqSamplingRate) &&
Glenn Kastenf506e942013-08-06 10:49:43 -07001763 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001764 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Glenn Kasten254af182012-07-03 14:59:05 -07001765 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001766 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001767 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07001768 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001769 }
1770
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001771 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001772
Glenn Kasten46909e72013-02-26 09:20:22 -08001773#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001774 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1775 // or (re-)create if current Pipe is idle and does not match the new format
1776 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001777 enum {
1778 TEE_SINK_NO, // don't copy input
1779 TEE_SINK_NEW, // copy input using a new pipe
1780 TEE_SINK_OLD, // copy input using an existing pipe
1781 } kind;
1782 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1783 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001784 if (!mTeeSinkInputEnabled) {
1785 kind = TEE_SINK_NO;
1786 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001787 kind = TEE_SINK_NO;
1788 } else if (mRecordTeeSink == 0) {
1789 kind = TEE_SINK_NEW;
1790 } else if (mRecordTeeSink->getStrongCount() != 1) {
1791 kind = TEE_SINK_NO;
1792 } else if (format == mRecordTeeSink->format()) {
1793 kind = TEE_SINK_OLD;
1794 } else {
1795 kind = TEE_SINK_NEW;
1796 }
1797 switch (kind) {
1798 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001799 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001800 size_t numCounterOffers = 0;
1801 const NBAIO_Format offers[1] = {format};
1802 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1803 ALOG_ASSERT(index == 0);
1804 PipeReader *pipeReader = new PipeReader(*pipe);
1805 numCounterOffers = 0;
1806 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1807 ALOG_ASSERT(index == 0);
1808 mRecordTeeSink = pipe;
1809 mRecordTeeSource = pipeReader;
1810 teeSink = pipe;
1811 }
1812 break;
1813 case TEE_SINK_OLD:
1814 teeSink = mRecordTeeSink;
1815 break;
1816 case TEE_SINK_NO:
1817 default:
1818 break;
1819 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001820#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001821
Dima Zavin799a70e2011-04-18 16:57:27 -07001822 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1823
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001824 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001825 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001826 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001827 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001828 input,
1829 reqSamplingRate,
Glenn Kastenf506e942013-08-06 10:49:43 -07001830 reqChannelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001831 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001832 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001833 *pDevices
1834#ifdef TEE_SINK
1835 , teeSink
1836#endif
1837 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001838 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001839 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001840 if (pSamplingRate != NULL) {
1841 *pSamplingRate = reqSamplingRate;
1842 }
1843 if (pFormat != NULL) {
1844 *pFormat = config.format;
1845 }
1846 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001847 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001848 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001849
Mathias Agopian65ab4712010-07-14 17:59:35 -07001850 // notify client processes of the new input creation
1851 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1852 return id;
1853 }
1854
1855 return 0;
1856}
1857
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001858status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001859{
Glenn Kastend96c5722012-04-25 13:44:49 -07001860 return closeInput_nonvirtual(input);
1861}
1862
1863status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1864{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001865 // keep strong reference on the record thread so that
1866 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001867 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001868 {
1869 Mutex::Autolock _l(mLock);
1870 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001871 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001872 return BAD_VALUE;
1873 }
1874
Steve Block3856b092011-10-20 11:56:00 +01001875 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001876 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001877 mRecordThreads.removeItem(input);
1878 }
1879 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001880 // The thread entity (active unit of execution) is no longer running here,
1881 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001882
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001883 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001884 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001885 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001886 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001887 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001888
1889 return NO_ERROR;
1890}
1891
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001892status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001893{
1894 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001895 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001896
1897 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1898 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001899 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001900 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901
1902 return NO_ERROR;
1903}
1904
1905
1906int AudioFlinger::newAudioSessionId()
1907{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001908 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001909}
1910
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001911void AudioFlinger::acquireAudioSessionId(int audioSession)
1912{
1913 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001914 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001915 ALOGV("acquiring %d from %d", audioSession, caller);
Eric Laurentd1b28d42013-09-18 18:47:13 -07001916
1917 // Ignore requests received from processes not known as notification client. The request
1918 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1919 // called from a different pid leaving a stale session reference. Also we don't know how
1920 // to clear this reference if the client process dies.
1921 if (mNotificationClients.indexOfKey(caller) < 0) {
1922 ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
1923 return;
1924 }
1925
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001926 size_t num = mAudioSessionRefs.size();
1927 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001928 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001929 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1930 ref->mCnt++;
1931 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001932 return;
1933 }
1934 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001935 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1936 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001937}
1938
1939void AudioFlinger::releaseAudioSessionId(int audioSession)
1940{
1941 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001942 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001943 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001944 size_t num = mAudioSessionRefs.size();
1945 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001946 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001947 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1948 ref->mCnt--;
1949 ALOGV(" decremented refcount to %d", ref->mCnt);
1950 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001951 mAudioSessionRefs.removeAt(i);
1952 delete ref;
1953 purgeStaleEffects_l();
1954 }
1955 return;
1956 }
1957 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001958 // If the caller is mediaserver it is likely that the session being released was acquired
1959 // on behalf of a process not in notification clients and we ignore the warning.
1960 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001961}
1962
1963void AudioFlinger::purgeStaleEffects_l() {
1964
Steve Block3856b092011-10-20 11:56:00 +01001965 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001966
1967 Vector< sp<EffectChain> > chains;
1968
1969 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1970 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1971 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1972 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001973 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1974 chains.push(ec);
1975 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001976 }
1977 }
1978 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1979 sp<RecordThread> t = mRecordThreads.valueAt(i);
1980 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1981 sp<EffectChain> ec = t->mEffectChains[j];
1982 chains.push(ec);
1983 }
1984 }
1985
1986 for (size_t i = 0; i < chains.size(); i++) {
1987 sp<EffectChain> ec = chains[i];
1988 int sessionid = ec->sessionId();
1989 sp<ThreadBase> t = ec->mThread.promote();
1990 if (t == 0) {
1991 continue;
1992 }
1993 size_t numsessionrefs = mAudioSessionRefs.size();
1994 bool found = false;
1995 for (size_t k = 0; k < numsessionrefs; k++) {
1996 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001997 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001998 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001999 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002000 found = true;
2001 break;
2002 }
2003 }
2004 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002005 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002006 // remove all effects from the chain
2007 while (ec->mEffects.size()) {
2008 sp<EffectModule> effect = ec->mEffects[0];
2009 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002010 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002011 if (effect->purgeHandles()) {
2012 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002013 }
2014 AudioSystem::unregisterEffect(effect->id());
2015 }
2016 }
2017 }
2018 return;
2019}
2020
Mathias Agopian65ab4712010-07-14 17:59:35 -07002021// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002022AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002023{
Glenn Kastena1117922012-01-26 10:53:32 -08002024 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002025}
2026
2027// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002028AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002029{
2030 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002031 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002032}
2033
2034// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002035AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002036{
Glenn Kastena1117922012-01-26 10:53:32 -08002037 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002038}
2039
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002040uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002041{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002042 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002043}
2044
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002045AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002046{
2047 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2048 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002049 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002050 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002051 return thread;
2052 }
2053 }
2054 return NULL;
2055}
2056
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002057audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002058{
2059 PlaybackThread *thread = primaryPlaybackThread_l();
2060
2061 if (thread == NULL) {
2062 return 0;
2063 }
2064
Eric Laurentf1c04f92012-08-28 14:26:53 -07002065 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002066}
2067
Eric Laurenta011e352012-03-29 15:51:43 -07002068sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2069 int triggerSession,
2070 int listenerSession,
2071 sync_event_callback_t callBack,
2072 void *cookie)
2073{
2074 Mutex::Autolock _l(mLock);
2075
2076 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2077 status_t playStatus = NAME_NOT_FOUND;
2078 status_t recStatus = NAME_NOT_FOUND;
2079 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2080 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2081 if (playStatus == NO_ERROR) {
2082 return event;
2083 }
2084 }
2085 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2086 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2087 if (recStatus == NO_ERROR) {
2088 return event;
2089 }
2090 }
2091 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2092 mPendingSyncEvents.add(event);
2093 } else {
2094 ALOGV("createSyncEvent() invalid event %d", event->type());
2095 event.clear();
2096 }
2097 return event;
2098}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002099
Mathias Agopian65ab4712010-07-14 17:59:35 -07002100// ----------------------------------------------------------------------------
2101// Effect management
2102// ----------------------------------------------------------------------------
2103
2104
Glenn Kastenf587ba52012-01-26 16:25:10 -08002105status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002106{
2107 Mutex::Autolock _l(mLock);
2108 return EffectQueryNumberEffects(numEffects);
2109}
2110
Glenn Kastenf587ba52012-01-26 16:25:10 -08002111status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002112{
2113 Mutex::Autolock _l(mLock);
2114 return EffectQueryEffect(index, descriptor);
2115}
2116
Glenn Kasten5e92a782012-01-30 07:40:52 -08002117status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002118 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002119{
2120 Mutex::Autolock _l(mLock);
2121 return EffectGetDescriptor(pUuid, descriptor);
2122}
2123
2124
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002125sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002126 effect_descriptor_t *pDesc,
2127 const sp<IEffectClient>& effectClient,
2128 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002129 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002130 int sessionId,
2131 status_t *status,
2132 int *id,
2133 int *enabled)
2134{
2135 status_t lStatus = NO_ERROR;
2136 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002137 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002138
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002139 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002140 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002141 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002142
2143 if (pDesc == NULL) {
2144 lStatus = BAD_VALUE;
2145 goto Exit;
2146 }
2147
Eric Laurent84e9a102010-09-23 16:10:16 -07002148 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002149 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002150 lStatus = PERMISSION_DENIED;
2151 goto Exit;
2152 }
2153
Dima Zavinfce7a472011-04-19 22:30:36 -07002154 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002155 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002156 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002157 lStatus = PERMISSION_DENIED;
2158 goto Exit;
2159 }
2160
Mathias Agopian65ab4712010-07-14 17:59:35 -07002161 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002162 if (!EffectIsNullUuid(&pDesc->uuid)) {
2163 // if uuid is specified, request effect descriptor
2164 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2165 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002166 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002167 goto Exit;
2168 }
2169 } else {
2170 // if uuid is not specified, look for an available implementation
2171 // of the required type in effect factory
2172 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002173 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002174 lStatus = BAD_VALUE;
2175 goto Exit;
2176 }
2177 uint32_t numEffects = 0;
2178 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002179 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002180 bool found = false;
2181
2182 lStatus = EffectQueryNumberEffects(&numEffects);
2183 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002184 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002185 goto Exit;
2186 }
2187 for (uint32_t i = 0; i < numEffects; i++) {
2188 lStatus = EffectQueryEffect(i, &desc);
2189 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002190 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002191 continue;
2192 }
2193 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2194 // If matching type found save effect descriptor. If the session is
2195 // 0 and the effect is not auxiliary, continue enumeration in case
2196 // an auxiliary version of this effect type is available
2197 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002198 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002199 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002200 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2201 break;
2202 }
2203 }
2204 }
2205 if (!found) {
2206 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002207 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002208 goto Exit;
2209 }
2210 // For same effect type, chose auxiliary version over insert version if
2211 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002212 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002213 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002214 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002215 }
2216 }
2217
2218 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002219 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002220 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2221 lStatus = INVALID_OPERATION;
2222 goto Exit;
2223 }
2224
Eric Laurent59255e42011-07-27 19:49:51 -07002225 // check recording permission for visualizer
2226 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2227 !recordingAllowed()) {
2228 lStatus = PERMISSION_DENIED;
2229 goto Exit;
2230 }
2231
Mathias Agopian65ab4712010-07-14 17:59:35 -07002232 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002233 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002234 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2235 // if the output returned by getOutputForEffect() is removed before we lock the
2236 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2237 // and we will exit safely
2238 io = AudioSystem::getOutputForEffect(&desc);
2239 ALOGV("createEffect got output %d", io);
2240 }
2241
2242 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002243
2244 // If output is not specified try to find a matching audio session ID in one of the
2245 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002246 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2247 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002248 // Note: io is never 0 when creating an effect on an input
2249 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002250 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2251 // output must be specified by AudioPolicyManager when using session
2252 // AUDIO_SESSION_OUTPUT_STAGE
2253 lStatus = BAD_VALUE;
2254 goto Exit;
2255 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002256 // look for the thread where the specified audio session is present
2257 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2258 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2259 io = mPlaybackThreads.keyAt(i);
2260 break;
2261 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002262 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002263 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002264 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2265 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2266 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002267 break;
2268 }
2269 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002270 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002271 // If no output thread contains the requested session ID, default to
2272 // first output. The effect chain will be moved to the correct output
2273 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002274 if (io == 0 && mPlaybackThreads.size()) {
2275 io = mPlaybackThreads.keyAt(0);
2276 }
Steve Block3856b092011-10-20 11:56:00 +01002277 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002278 }
2279 ThreadBase *thread = checkRecordThread_l(io);
2280 if (thread == NULL) {
2281 thread = checkPlaybackThread_l(io);
2282 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002283 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002284 lStatus = BAD_VALUE;
2285 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002286 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002287 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002288
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002289 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002290
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002291 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002292 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2293 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002294 if (handle != 0 && id != NULL) {
2295 *id = handle->id();
2296 }
2297 }
2298
2299Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002300 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002301 return handle;
2302}
2303
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002304status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2305 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002306{
Steve Block3856b092011-10-20 11:56:00 +01002307 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002308 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002309 Mutex::Autolock _l(mLock);
2310 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002311 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002312 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002313 }
Eric Laurentde070132010-07-13 04:45:46 -07002314 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2315 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002316 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002317 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002318 }
Eric Laurentde070132010-07-13 04:45:46 -07002319 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2320 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002321 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002322 return BAD_VALUE;
2323 }
2324
2325 Mutex::Autolock _dl(dstThread->mLock);
2326 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002327 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002328}
2329
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002330// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002331status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002332 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002333 AudioFlinger::PlaybackThread *dstThread,
2334 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002335{
Steve Block3856b092011-10-20 11:56:00 +01002336 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002337 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002338
Eric Laurent59255e42011-07-27 19:49:51 -07002339 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002340 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002341 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002342 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002343 return INVALID_OPERATION;
2344 }
2345
Eric Laurent39e94f82010-07-28 01:32:47 -07002346 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002347 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002348 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002349 // removed.
2350 srcThread->removeEffectChain_l(chain);
2351
2352 // transfer all effects one by one so that new effect chain is created on new thread with
2353 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002354 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002355 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002356 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002357 Vector< sp<EffectModule> > removed;
2358 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002359 while (effect != 0) {
2360 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002361 removed.add(effect);
2362 status = dstThread->addEffect_l(effect);
2363 if (status != NO_ERROR) {
2364 break;
2365 }
Eric Laurentec35a142011-10-05 17:42:25 -07002366 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2367 if (effect->state() == EffectModule::ACTIVE ||
2368 effect->state() == EffectModule::STOPPING) {
2369 effect->start();
2370 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002371 // if the move request is not received from audio policy manager, the effect must be
2372 // re-registered with the new strategy and output
2373 if (dstChain == 0) {
2374 dstChain = effect->chain().promote();
2375 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002376 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002377 status = NO_INIT;
2378 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002379 }
2380 strategy = dstChain->strategy();
2381 }
2382 if (reRegister) {
2383 AudioSystem::unregisterEffect(effect->id());
2384 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002385 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002386 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002387 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002388 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002389 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002390 }
Eric Laurentde070132010-07-13 04:45:46 -07002391 effect = chain->getEffectFromId_l(0);
2392 }
2393
Eric Laurent5baf2af2013-09-12 17:37:00 -07002394 if (status != NO_ERROR) {
2395 for (size_t i = 0; i < removed.size(); i++) {
2396 srcThread->addEffect_l(removed[i]);
2397 if (dstChain != 0 && reRegister) {
2398 AudioSystem::unregisterEffect(removed[i]->id());
2399 AudioSystem::registerEffect(&removed[i]->desc(),
2400 srcThread->id(),
2401 strategy,
2402 sessionId,
2403 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002404 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002405 }
2406 }
2407 }
2408
2409 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002410}
2411
Eric Laurent5baf2af2013-09-12 17:37:00 -07002412bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002413{
2414 if (mGlobalEffectEnableTime != 0 &&
2415 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2416 return true;
2417 }
2418
2419 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2420 sp<EffectChain> ec =
2421 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002422 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002423 return true;
2424 }
2425 }
2426 return false;
2427}
2428
Eric Laurent5baf2af2013-09-12 17:37:00 -07002429void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002430{
2431 Mutex::Autolock _l(mLock);
2432
2433 mGlobalEffectEnableTime = systemTime();
2434
2435 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2436 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2437 if (t->mType == ThreadBase::OFFLOAD) {
2438 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2439 }
2440 }
2441
2442}
2443
Glenn Kastenda6ef132013-01-10 12:31:01 -08002444struct Entry {
2445#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2446 char mName[MAX_NAME];
2447};
2448
2449int comparEntry(const void *p1, const void *p2)
2450{
2451 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2452}
2453
Glenn Kasten46909e72013-02-26 09:20:22 -08002454#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002455void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002456{
Eric Laurent81784c32012-11-19 14:55:58 -08002457 NBAIO_Source *teeSource = source.get();
2458 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002459 // .wav rotation
2460 // There is a benign race condition if 2 threads call this simultaneously.
2461 // They would both traverse the directory, but the result would simply be
2462 // failures at unlink() which are ignored. It's also unlikely since
2463 // normally dumpsys is only done by bugreport or from the command line.
2464 char teePath[32+256];
2465 strcpy(teePath, "/data/misc/media");
2466 size_t teePathLen = strlen(teePath);
2467 DIR *dir = opendir(teePath);
2468 teePath[teePathLen++] = '/';
2469 if (dir != NULL) {
2470#define MAX_SORT 20 // number of entries to sort
2471#define MAX_KEEP 10 // number of entries to keep
2472 struct Entry entries[MAX_SORT];
2473 size_t entryCount = 0;
2474 while (entryCount < MAX_SORT) {
2475 struct dirent de;
2476 struct dirent *result = NULL;
2477 int rc = readdir_r(dir, &de, &result);
2478 if (rc != 0) {
2479 ALOGW("readdir_r failed %d", rc);
2480 break;
2481 }
2482 if (result == NULL) {
2483 break;
2484 }
2485 if (result != &de) {
2486 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2487 break;
2488 }
2489 // ignore non .wav file entries
2490 size_t nameLen = strlen(de.d_name);
2491 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2492 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2493 continue;
2494 }
2495 strcpy(entries[entryCount++].mName, de.d_name);
2496 }
2497 (void) closedir(dir);
2498 if (entryCount > MAX_KEEP) {
2499 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2500 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2501 strcpy(&teePath[teePathLen], entries[i].mName);
2502 (void) unlink(teePath);
2503 }
2504 }
2505 } else {
2506 if (fd >= 0) {
2507 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2508 }
2509 }
Eric Laurent81784c32012-11-19 14:55:58 -08002510 char teeTime[16];
2511 struct timeval tv;
2512 gettimeofday(&tv, NULL);
2513 struct tm tm;
2514 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002515 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2516 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2517 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2518 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002519 if (teeFd >= 0) {
2520 char wavHeader[44];
2521 memcpy(wavHeader,
2522 "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",
2523 sizeof(wavHeader));
2524 NBAIO_Format format = teeSource->format();
2525 unsigned channelCount = Format_channelCount(format);
2526 ALOG_ASSERT(channelCount <= FCC_2);
2527 uint32_t sampleRate = Format_sampleRate(format);
2528 wavHeader[22] = channelCount; // number of channels
2529 wavHeader[24] = sampleRate; // sample rate
2530 wavHeader[25] = sampleRate >> 8;
2531 wavHeader[32] = channelCount * 2; // block alignment
2532 write(teeFd, wavHeader, sizeof(wavHeader));
2533 size_t total = 0;
2534 bool firstRead = true;
2535 for (;;) {
2536#define TEE_SINK_READ 1024
2537 short buffer[TEE_SINK_READ * FCC_2];
2538 size_t count = TEE_SINK_READ;
2539 ssize_t actual = teeSource->read(buffer, count,
2540 AudioBufferProvider::kInvalidPTS);
2541 bool wasFirstRead = firstRead;
2542 firstRead = false;
2543 if (actual <= 0) {
2544 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2545 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002546 }
Eric Laurent81784c32012-11-19 14:55:58 -08002547 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002548 }
Eric Laurent81784c32012-11-19 14:55:58 -08002549 ALOG_ASSERT(actual <= (ssize_t)count);
2550 write(teeFd, buffer, actual * channelCount * sizeof(short));
2551 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002552 }
Eric Laurent81784c32012-11-19 14:55:58 -08002553 lseek(teeFd, (off_t) 4, SEEK_SET);
2554 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2555 write(teeFd, &temp, sizeof(temp));
2556 lseek(teeFd, (off_t) 40, SEEK_SET);
2557 temp = total * channelCount * sizeof(short);
2558 write(teeFd, &temp, sizeof(temp));
2559 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002560 if (fd >= 0) {
2561 fdprintf(fd, "tee copied to %s\n", teePath);
2562 }
Eric Laurent59255e42011-07-27 19:49:51 -07002563 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002564 if (fd >= 0) {
2565 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2566 }
Eric Laurent59255e42011-07-27 19:49:51 -07002567 }
2568 }
2569}
Glenn Kasten46909e72013-02-26 09:20:22 -08002570#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002571
Mathias Agopian65ab4712010-07-14 17:59:35 -07002572// ----------------------------------------------------------------------------
2573
2574status_t AudioFlinger::onTransact(
2575 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2576{
2577 return BnAudioFlinger::onTransact(code, data, reply, flags);
2578}
2579
Mathias Agopian65ab4712010-07-14 17:59:35 -07002580}; // namespace android