blob: 34811a76f66b6d34a0baeaa4b076542b00a77ca8 [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 Kastenbe5f05e2012-07-18 15:24:02 -0700267void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
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 Kastenbe5f05e2012-07-18 15:24:02 -0700299void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
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 Kastenbe5f05e2012-07-18 15:24:02 -0700314void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
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 Kastene33054e2012-11-14 12:54:39 -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 Kasten2fc14732013-08-05 14:58:14 -0700562 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700563
564 // move effect chain to this output thread if an effect on same session was waiting
565 // for a track to be created
566 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700567 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700568 Mutex::Autolock _dl(thread->mLock);
569 Mutex::Autolock _sl(effectThread->mLock);
570 moveEffectChain_l(lSessionId, effectThread, thread, true);
571 }
Eric Laurenta011e352012-03-29 15:51:43 -0700572
573 // Look for sync events awaiting for a session to be used.
574 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
575 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
576 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700577 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700578 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700579 } else {
580 mPendingSyncEvents[i]->cancel();
581 }
Eric Laurenta011e352012-03-29 15:51:43 -0700582 mPendingSyncEvents.removeAt(i);
583 i--;
584 }
585 }
586 }
Glenn Kastene198c362013-08-13 09:13:36 -0700587
Mathias Agopian65ab4712010-07-14 17:59:35 -0700588 }
Glenn Kastene198c362013-08-13 09:13:36 -0700589
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700591 // s for server's pid, n for normal mixer name, f for fast index
592 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
593 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594 trackHandle = new TrackHandle(track);
595 } else {
596 // remove local strong reference to Client before deleting the Track so that the Client
597 // destructor is called by the TrackBase destructor with mLock held
598 client.clear();
599 track.clear();
600 }
601
602Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700603 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 return trackHandle;
605}
606
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800607uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700608{
609 Mutex::Autolock _l(mLock);
610 PlaybackThread *thread = checkPlaybackThread_l(output);
611 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000612 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700613 return 0;
614 }
615 return thread->sampleRate();
616}
617
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800618int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619{
620 Mutex::Autolock _l(mLock);
621 PlaybackThread *thread = checkPlaybackThread_l(output);
622 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000623 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700624 return 0;
625 }
626 return thread->channelCount();
627}
628
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800629audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630{
631 Mutex::Autolock _l(mLock);
632 PlaybackThread *thread = checkPlaybackThread_l(output);
633 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000634 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800635 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636 }
637 return thread->format();
638}
639
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800640size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641{
642 Mutex::Autolock _l(mLock);
643 PlaybackThread *thread = checkPlaybackThread_l(output);
644 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000645 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 return 0;
647 }
Glenn Kasten58912562012-04-03 10:45:00 -0700648 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
649 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 return thread->frameCount();
651}
652
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800653uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654{
655 Mutex::Autolock _l(mLock);
656 PlaybackThread *thread = checkPlaybackThread_l(output);
657 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800658 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700659 return 0;
660 }
661 return thread->latency();
662}
663
664status_t AudioFlinger::setMasterVolume(float value)
665{
Eric Laurenta1884f92011-08-23 08:25:03 -0700666 status_t ret = initCheck();
667 if (ret != NO_ERROR) {
668 return ret;
669 }
670
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671 // check calling permissions
672 if (!settingsAllowed()) {
673 return PERMISSION_DENIED;
674 }
675
Eric Laurenta4c5a552012-03-29 10:12:40 -0700676 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700677 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700678
John Grossmanee578c02012-07-23 17:05:46 -0700679 // Set master volume in the HALs which support it.
680 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
681 AutoMutex lock(mHardwareLock);
682 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800683
John Grossmanee578c02012-07-23 17:05:46 -0700684 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
685 if (dev->canSetMasterVolume()) {
686 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800687 }
John Grossmanee578c02012-07-23 17:05:46 -0700688 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690
John Grossmanee578c02012-07-23 17:05:46 -0700691 // Now set the master volume in each playback thread. Playback threads
692 // assigned to HALs which do not have master volume support will apply
693 // master volume during the mix operation. Threads with HALs which do
694 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800695 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700696 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697
698 return NO_ERROR;
699}
700
Glenn Kastenf78aee72012-01-04 11:00:47 -0800701status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702{
Eric Laurenta1884f92011-08-23 08:25:03 -0700703 status_t ret = initCheck();
704 if (ret != NO_ERROR) {
705 return ret;
706 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707
708 // check calling permissions
709 if (!settingsAllowed()) {
710 return PERMISSION_DENIED;
711 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800712 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000713 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 return BAD_VALUE;
715 }
716
717 { // scope for the lock
718 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700719 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700721 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 mHardwareStatus = AUDIO_HW_IDLE;
723 }
724
725 if (NO_ERROR == ret) {
726 Mutex::Autolock _l(mLock);
727 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800728 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700729 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700730 }
731
732 return ret;
733}
734
735status_t AudioFlinger::setMicMute(bool state)
736{
Eric Laurenta1884f92011-08-23 08:25:03 -0700737 status_t ret = initCheck();
738 if (ret != NO_ERROR) {
739 return ret;
740 }
741
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742 // check calling permissions
743 if (!settingsAllowed()) {
744 return PERMISSION_DENIED;
745 }
746
747 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700748 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700750 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751 mHardwareStatus = AUDIO_HW_IDLE;
752 return ret;
753}
754
755bool AudioFlinger::getMicMute() const
756{
Eric Laurenta1884f92011-08-23 08:25:03 -0700757 status_t ret = initCheck();
758 if (ret != NO_ERROR) {
759 return false;
760 }
761
Dima Zavinfce7a472011-04-19 22:30:36 -0700762 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800763 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700764 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700766 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767 mHardwareStatus = AUDIO_HW_IDLE;
768 return state;
769}
770
771status_t AudioFlinger::setMasterMute(bool muted)
772{
John Grossmand8f178d2012-07-20 14:51:35 -0700773 status_t ret = initCheck();
774 if (ret != NO_ERROR) {
775 return ret;
776 }
777
Mathias Agopian65ab4712010-07-14 17:59:35 -0700778 // check calling permissions
779 if (!settingsAllowed()) {
780 return PERMISSION_DENIED;
781 }
782
John Grossmanee578c02012-07-23 17:05:46 -0700783 Mutex::Autolock _l(mLock);
784 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700785
John Grossmanee578c02012-07-23 17:05:46 -0700786 // Set master mute in the HALs which support it.
787 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
788 AutoMutex lock(mHardwareLock);
789 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700790
John Grossmanee578c02012-07-23 17:05:46 -0700791 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
792 if (dev->canSetMasterMute()) {
793 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700794 }
John Grossmanee578c02012-07-23 17:05:46 -0700795 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700796 }
797
John Grossmanee578c02012-07-23 17:05:46 -0700798 // Now set the master mute in each playback thread. Playback threads
799 // assigned to HALs which do not have master mute support will apply master
800 // mute during the mix operation. Threads with HALs which do support master
801 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800802 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700803 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804
805 return NO_ERROR;
806}
807
808float AudioFlinger::masterVolume() const
809{
Glenn Kasten98067102011-12-13 11:47:54 -0800810 Mutex::Autolock _l(mLock);
811 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812}
813
814bool AudioFlinger::masterMute() const
815{
Glenn Kasten98067102011-12-13 11:47:54 -0800816 Mutex::Autolock _l(mLock);
817 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818}
819
John Grossman4ff14ba2012-02-08 16:37:41 -0800820float AudioFlinger::masterVolume_l() const
821{
John Grossman4ff14ba2012-02-08 16:37:41 -0800822 return mMasterVolume;
823}
824
John Grossmand8f178d2012-07-20 14:51:35 -0700825bool AudioFlinger::masterMute_l() const
826{
John Grossmanee578c02012-07-23 17:05:46 -0700827 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700828}
829
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800830status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
831 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832{
833 // check calling permissions
834 if (!settingsAllowed()) {
835 return PERMISSION_DENIED;
836 }
837
Glenn Kasten263709e2012-01-06 08:40:01 -0800838 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000839 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840 return BAD_VALUE;
841 }
842
843 AutoMutex lock(mLock);
844 PlaybackThread *thread = NULL;
845 if (output) {
846 thread = checkPlaybackThread_l(output);
847 if (thread == NULL) {
848 return BAD_VALUE;
849 }
850 }
851
852 mStreamTypes[stream].volume = value;
853
854 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800855 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700856 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700857 }
858 } else {
859 thread->setStreamVolume(stream, value);
860 }
861
862 return NO_ERROR;
863}
864
Glenn Kastenfff6d712012-01-12 16:38:12 -0800865status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866{
867 // check calling permissions
868 if (!settingsAllowed()) {
869 return PERMISSION_DENIED;
870 }
871
Glenn Kasten263709e2012-01-06 08:40:01 -0800872 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700873 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000874 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700875 return BAD_VALUE;
876 }
877
Eric Laurent93575202011-01-18 18:39:02 -0800878 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879 mStreamTypes[stream].mute = muted;
880 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700881 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882
883 return NO_ERROR;
884}
885
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800886float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700887{
Glenn Kasten263709e2012-01-06 08:40:01 -0800888 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889 return 0.0f;
890 }
891
892 AutoMutex lock(mLock);
893 float volume;
894 if (output) {
895 PlaybackThread *thread = checkPlaybackThread_l(output);
896 if (thread == NULL) {
897 return 0.0f;
898 }
899 volume = thread->streamVolume(stream);
900 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800901 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902 }
903
904 return volume;
905}
906
Glenn Kastenfff6d712012-01-12 16:38:12 -0800907bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700908{
Glenn Kasten263709e2012-01-06 08:40:01 -0800909 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910 return true;
911 }
912
Glenn Kasten6637baa2012-01-09 09:40:36 -0800913 AutoMutex lock(mLock);
914 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700915}
916
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800917status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700919 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
920 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800921
Mathias Agopian65ab4712010-07-14 17:59:35 -0700922 // check calling permissions
923 if (!settingsAllowed()) {
924 return PERMISSION_DENIED;
925 }
926
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927 // ioHandle == 0 means the parameters are global to the audio hardware interface
928 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700929 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700930 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800931 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700932 AutoMutex lock(mHardwareLock);
933 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
934 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
935 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
936 status_t result = dev->set_parameters(dev, keyValuePairs.string());
937 final_result = result ?: final_result;
938 }
939 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800940 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700941 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
942 AudioParameter param = AudioParameter(keyValuePairs);
943 String8 value;
944 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700945 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
946 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700947 for (size_t i = 0; i < mRecordThreads.size(); i++) {
948 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700949 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700950 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
951 // collect all of the thread's session IDs
952 KeyedVector<int, bool> ids = thread->sessionIds();
953 // suspend effects associated with those session IDs
954 for (size_t j = 0; j < ids.size(); ++j) {
955 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700956 thread->setEffectSuspended(FX_IID_AEC,
957 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700958 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700959 thread->setEffectSuspended(FX_IID_NS,
960 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700961 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700962 }
963 }
Eric Laurentbee53372011-08-29 12:42:48 -0700964 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700965 }
966 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700967 String8 screenState;
968 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
969 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800970 if (isOff != (AudioFlinger::mScreenState & 1)) {
971 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700972 }
973 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700974 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975 }
976
977 // hold a strong ref on thread in case closeOutput() or closeInput() is called
978 // and the thread is exited once the lock is released
979 sp<ThreadBase> thread;
980 {
981 Mutex::Autolock _l(mLock);
982 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700983 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800985 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700986 // indicate output device change to all input threads for pre processing
987 AudioParameter param = AudioParameter(keyValuePairs);
988 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700989 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
990 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700991 for (size_t i = 0; i < mRecordThreads.size(); i++) {
992 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
993 }
994 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995 }
996 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800997 if (thread != 0) {
998 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999 }
1000 return BAD_VALUE;
1001}
1002
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001003String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001005 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1006 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001007
Eric Laurenta4c5a552012-03-29 10:12:40 -07001008 Mutex::Autolock _l(mLock);
1009
Mathias Agopian65ab4712010-07-14 17:59:35 -07001010 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001011 String8 out_s8;
1012
Dima Zavin799a70e2011-04-18 16:57:27 -07001013 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001014 char *s;
1015 {
1016 AutoMutex lock(mHardwareLock);
1017 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001018 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001019 s = dev->get_parameters(dev, keys.string());
1020 mHardwareStatus = AUDIO_HW_IDLE;
1021 }
John Grossmanef7740b2012-02-09 11:28:36 -08001022 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001023 free(s);
1024 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001025 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 }
1027
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1029 if (playbackThread != NULL) {
1030 return playbackThread->getParameters(keys);
1031 }
1032 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1033 if (recordThread != NULL) {
1034 return recordThread->getParameters(keys);
1035 }
1036 return String8("");
1037}
1038
Glenn Kastendd8104c2012-07-02 12:42:44 -07001039size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1040 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001041{
Eric Laurenta1884f92011-08-23 08:25:03 -07001042 status_t ret = initCheck();
1043 if (ret != NO_ERROR) {
1044 return 0;
1045 }
1046
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001047 AutoMutex lock(mHardwareLock);
1048 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001049 struct audio_config config;
1050 memset(&config, 0, sizeof(config));
1051 config.sample_rate = sampleRate;
1052 config.channel_mask = channelMask;
1053 config.format = format;
1054
John Grossmanee578c02012-07-23 17:05:46 -07001055 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1056 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001057 mHardwareStatus = AUDIO_HW_IDLE;
1058 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001059}
1060
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001061unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063 Mutex::Autolock _l(mLock);
1064
1065 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1066 if (recordThread != NULL) {
1067 return recordThread->getInputFramesLost();
1068 }
1069 return 0;
1070}
1071
1072status_t AudioFlinger::setVoiceVolume(float value)
1073{
Eric Laurenta1884f92011-08-23 08:25:03 -07001074 status_t ret = initCheck();
1075 if (ret != NO_ERROR) {
1076 return ret;
1077 }
1078
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079 // check calling permissions
1080 if (!settingsAllowed()) {
1081 return PERMISSION_DENIED;
1082 }
1083
1084 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001085 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001086 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001087 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001088 mHardwareStatus = AUDIO_HW_IDLE;
1089
1090 return ret;
1091}
1092
Glenn Kasten26c77552012-11-16 12:01:44 -08001093status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001094 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095{
1096 status_t status;
1097
1098 Mutex::Autolock _l(mLock);
1099
1100 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1101 if (playbackThread != NULL) {
1102 return playbackThread->getRenderPosition(halFrames, dspFrames);
1103 }
1104
1105 return BAD_VALUE;
1106}
1107
1108void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1109{
1110
1111 Mutex::Autolock _l(mLock);
1112
Glenn Kastenbb001922012-02-03 11:10:26 -08001113 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001114 if (mNotificationClients.indexOfKey(pid) < 0) {
1115 sp<NotificationClient> notificationClient = new NotificationClient(this,
1116 client,
1117 pid);
Steve Block3856b092011-10-20 11:56:00 +01001118 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001119
1120 mNotificationClients.add(pid, notificationClient);
1121
1122 sp<IBinder> binder = client->asBinder();
1123 binder->linkToDeath(notificationClient);
1124
1125 // the config change is always sent from playback or record threads to avoid deadlock
1126 // with AudioSystem::gLock
1127 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001128 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129 }
1130
1131 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001132 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133 }
1134 }
1135}
1136
1137void AudioFlinger::removeNotificationClient(pid_t pid)
1138{
1139 Mutex::Autolock _l(mLock);
1140
Glenn Kastena3b09252012-01-20 09:19:01 -08001141 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001142
Steve Block3856b092011-10-20 11:56:00 +01001143 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001144 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001145 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001146 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001147 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001148 ALOGV(" pid %d @ %d", ref->mPid, i);
1149 if (ref->mPid == pid) {
1150 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001151 mAudioSessionRefs.removeAt(i);
1152 delete ref;
1153 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001154 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001155 } else {
1156 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001157 }
1158 }
1159 if (removed) {
1160 purgeStaleEffects_l();
1161 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162}
1163
1164// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001165void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001166{
1167 size_t size = mNotificationClients.size();
1168 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001169 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1170 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001171 }
1172}
1173
1174// removeClient_l() must be called with AudioFlinger::mLock held
1175void AudioFlinger::removeClient_l(pid_t pid)
1176{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001177 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001178 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001179 mClients.removeItem(pid);
1180}
1181
Eric Laurent717e1282012-06-29 16:36:52 -07001182// getEffectThread_l() must be called with AudioFlinger::mLock held
1183sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1184{
1185 sp<PlaybackThread> thread;
1186
1187 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1188 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1189 ALOG_ASSERT(thread == 0);
1190 thread = mPlaybackThreads.valueAt(i);
1191 }
1192 }
1193
1194 return thread;
1195}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001196
Mathias Agopian65ab4712010-07-14 17:59:35 -07001197
Mathias Agopian65ab4712010-07-14 17:59:35 -07001198
1199// ----------------------------------------------------------------------------
1200
1201AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1202 : RefBase(),
1203 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001204 // 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 -07001205 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001206 mPid(pid),
1207 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001208{
1209 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1210}
1211
1212// Client destructor must be called with AudioFlinger::mLock held
1213AudioFlinger::Client::~Client()
1214{
1215 mAudioFlinger->removeClient_l(mPid);
1216}
1217
Glenn Kasten435dbe62012-01-30 10:15:48 -08001218sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219{
1220 return mMemoryDealer;
1221}
1222
John Grossman4ff14ba2012-02-08 16:37:41 -08001223// Reserve one of the limited slots for a timed audio track associated
1224// with this client
1225bool AudioFlinger::Client::reserveTimedTrack()
1226{
1227 const int kMaxTimedTracksPerClient = 4;
1228
1229 Mutex::Autolock _l(mTimedTrackLock);
1230
1231 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1232 ALOGW("can not create timed track - pid %d has exceeded the limit",
1233 mPid);
1234 return false;
1235 }
1236
1237 mTimedTrackCount++;
1238 return true;
1239}
1240
1241// Release a slot for a timed audio track
1242void AudioFlinger::Client::releaseTimedTrack()
1243{
1244 Mutex::Autolock _l(mTimedTrackLock);
1245 mTimedTrackCount--;
1246}
1247
Mathias Agopian65ab4712010-07-14 17:59:35 -07001248// ----------------------------------------------------------------------------
1249
1250AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1251 const sp<IAudioFlingerClient>& client,
1252 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001253 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001254{
1255}
1256
1257AudioFlinger::NotificationClient::~NotificationClient()
1258{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259}
1260
1261void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1262{
1263 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001264 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265}
1266
Mathias Agopian65ab4712010-07-14 17:59:35 -07001267
1268// ----------------------------------------------------------------------------
1269
Jeff Brown893a5642013-08-16 20:19:26 -07001270static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1271 return audio_is_remote_submix_device(inDevice);
1272}
1273
Mathias Agopian65ab4712010-07-14 17:59:35 -07001274sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001275 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001277 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001278 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001279 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001280 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001281 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001282 int *sessionId,
1283 status_t *status)
1284{
1285 sp<RecordThread::RecordTrack> recordTrack;
1286 sp<RecordHandle> recordHandle;
1287 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 status_t lStatus;
1289 RecordThread *thread;
1290 size_t inFrameCount;
1291 int lSessionId;
1292
1293 // check calling permissions
1294 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001295 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001296 lStatus = PERMISSION_DENIED;
1297 goto Exit;
1298 }
1299
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001300 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1301 ALOGE("openRecord() invalid format %d", format);
1302 lStatus = BAD_VALUE;
1303 goto Exit;
1304 }
1305
Mathias Agopian65ab4712010-07-14 17:59:35 -07001306 // add client to list
1307 { // scope for mLock
1308 Mutex::Autolock _l(mLock);
1309 thread = checkRecordThread_l(input);
1310 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001311 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312 lStatus = BAD_VALUE;
1313 goto Exit;
1314 }
1315
Jeff Brown893a5642013-08-16 20:19:26 -07001316 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1317 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001318 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001319 lStatus = PERMISSION_DENIED;
1320 goto Exit;
1321 }
1322
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001323 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001324 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001325
1326 // If no audio session id is provided, create one here
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001327 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001328 lSessionId = *sessionId;
1329 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001330 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331 if (sessionId != NULL) {
1332 *sessionId = lSessionId;
1333 }
1334 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001335 // create new record track.
1336 // The record track uses one track in mHardwareMixerThread by convention.
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001337 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001338 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001339 frameCount, lSessionId,
1340 IPCThreadState::self()->getCallingUid(),
1341 flags, tid, &lStatus);
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001342 LOG_ALWAYS_FATAL_IF((recordTrack != 0) != (lStatus == NO_ERROR));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001343 }
Glenn Kastene198c362013-08-13 09:13:36 -07001344
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001345 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001346 // remove local strong reference to Client before deleting the RecordTrack so that the
1347 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348 client.clear();
1349 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350 goto Exit;
1351 }
1352
Glenn Kasten2fc14732013-08-05 14:58:14 -07001353 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001354 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355
1356Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001357 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001358 return recordHandle;
1359}
1360
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001361
1362
Mathias Agopian65ab4712010-07-14 17:59:35 -07001363// ----------------------------------------------------------------------------
1364
Eric Laurenta4c5a552012-03-29 10:12:40 -07001365audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1366{
1367 if (!settingsAllowed()) {
1368 return 0;
1369 }
1370 Mutex::Autolock _l(mLock);
1371 return loadHwModule_l(name);
1372}
1373
1374// loadHwModule_l() must be called with AudioFlinger::mLock held
1375audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1376{
1377 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1378 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1379 ALOGW("loadHwModule() module %s already loaded", name);
1380 return mAudioHwDevs.keyAt(i);
1381 }
1382 }
1383
Eric Laurenta4c5a552012-03-29 10:12:40 -07001384 audio_hw_device_t *dev;
1385
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001386 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001387 if (rc) {
1388 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1389 return 0;
1390 }
1391
1392 mHardwareStatus = AUDIO_HW_INIT;
1393 rc = dev->init_check(dev);
1394 mHardwareStatus = AUDIO_HW_IDLE;
1395 if (rc) {
1396 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1397 return 0;
1398 }
1399
John Grossmanee578c02012-07-23 17:05:46 -07001400 // Check and cache this HAL's level of support for master mute and master
1401 // volume. If this is the first HAL opened, and it supports the get
1402 // methods, use the initial values provided by the HAL as the current
1403 // master mute and volume settings.
1404
1405 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1406 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001407 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001408
1409 if (0 == mAudioHwDevs.size()) {
1410 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1411 if (NULL != dev->get_master_volume) {
1412 float mv;
1413 if (OK == dev->get_master_volume(dev, &mv)) {
1414 mMasterVolume = mv;
1415 }
1416 }
1417
1418 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1419 if (NULL != dev->get_master_mute) {
1420 bool mm;
1421 if (OK == dev->get_master_mute(dev, &mm)) {
1422 mMasterMute = mm;
1423 }
1424 }
1425 }
1426
Eric Laurenta4c5a552012-03-29 10:12:40 -07001427 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001428 if ((NULL != dev->set_master_volume) &&
1429 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1430 flags = static_cast<AudioHwDevice::Flags>(flags |
1431 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1432 }
1433
1434 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1435 if ((NULL != dev->set_master_mute) &&
1436 (OK == dev->set_master_mute(dev, mMasterMute))) {
1437 flags = static_cast<AudioHwDevice::Flags>(flags |
1438 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1439 }
1440
Eric Laurenta4c5a552012-03-29 10:12:40 -07001441 mHardwareStatus = AUDIO_HW_IDLE;
1442 }
1443
1444 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001445 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001446
1447 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001448 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001449
1450 return handle;
1451
1452}
1453
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001454// ----------------------------------------------------------------------------
1455
Glenn Kasten3b16c762012-11-14 08:44:39 -08001456uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001457{
1458 Mutex::Autolock _l(mLock);
1459 PlaybackThread *thread = primaryPlaybackThread_l();
1460 return thread != NULL ? thread->sampleRate() : 0;
1461}
1462
Glenn Kastene33054e2012-11-14 12:54:39 -08001463size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001464{
1465 Mutex::Autolock _l(mLock);
1466 PlaybackThread *thread = primaryPlaybackThread_l();
1467 return thread != NULL ? thread->frameCountHAL() : 0;
1468}
1469
1470// ----------------------------------------------------------------------------
1471
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001472status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1473{
1474 uid_t uid = IPCThreadState::self()->getCallingUid();
1475 if (uid != AID_SYSTEM) {
1476 return PERMISSION_DENIED;
1477 }
1478 Mutex::Autolock _l(mLock);
1479 if (mIsDeviceTypeKnown) {
1480 return INVALID_OPERATION;
1481 }
1482 mIsLowRamDevice = isLowRamDevice;
1483 mIsDeviceTypeKnown = true;
1484 return NO_ERROR;
1485}
1486
1487// ----------------------------------------------------------------------------
1488
Eric Laurenta4c5a552012-03-29 10:12:40 -07001489audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1490 audio_devices_t *pDevices,
1491 uint32_t *pSamplingRate,
1492 audio_format_t *pFormat,
1493 audio_channel_mask_t *pChannelMask,
1494 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001495 audio_output_flags_t flags,
1496 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001497{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001498 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001499 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001500 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1501 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1502 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001503 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001504 config.offload_info = *offloadInfo;
1505 }
1506
Eric Laurentbfb1b832013-01-07 09:53:42 -08001507 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001508 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001509 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001510 config.sample_rate,
1511 config.format,
1512 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001513 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001514 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001515 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516
1517 if (pDevices == NULL || *pDevices == 0) {
1518 return 0;
1519 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001520
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521 Mutex::Autolock _l(mLock);
1522
Glenn Kasten32550952013-08-06 10:45:10 -07001523 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001524 if (outHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001525 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001526 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001527
John Grossmanee578c02012-07-23 17:05:46 -07001528 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001529 audio_io_handle_t id = nextUniqueId();
1530
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001531 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001532
Glenn Kasten32550952013-08-06 10:45:10 -07001533 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001534 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001535 id,
1536 *pDevices,
1537 (audio_output_flags_t)flags,
1538 &config,
1539 &outStream);
1540
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001541 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001542 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001543 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001544 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001545 config.sample_rate,
1546 config.format,
1547 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001548 status);
1549
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001550 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001551 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001552
Glenn Kasten32550952013-08-06 10:45:10 -07001553 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001554 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1555 thread = new OffloadThread(this, output, id, *pDevices);
1556 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1557 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001558 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1559 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001560 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001561 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562 } else {
1563 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001564 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001565 }
1566 mPlaybackThreads.add(id, thread);
1567
Glenn Kasten7c027242012-12-26 14:43:16 -08001568 if (pSamplingRate != NULL) {
1569 *pSamplingRate = config.sample_rate;
1570 }
1571 if (pFormat != NULL) {
1572 *pFormat = config.format;
1573 }
1574 if (pChannelMask != NULL) {
1575 *pChannelMask = config.channel_mask;
1576 }
1577 if (pLatencyMs != NULL) {
1578 *pLatencyMs = thread->latency();
1579 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001580
1581 // notify client processes of the new output creation
1582 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001583
1584 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001585 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001586 ALOGI("Using module %d has the primary audio interface", module);
1587 mPrimaryHardwareDev = outHwDev;
1588
1589 AutoMutex lock(mHardwareLock);
1590 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001591 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001592 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001593 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594 return id;
1595 }
1596
1597 return 0;
1598}
1599
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001600audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1601 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602{
1603 Mutex::Autolock _l(mLock);
1604 MixerThread *thread1 = checkMixerThread_l(output1);
1605 MixerThread *thread2 = checkMixerThread_l(output2);
1606
1607 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001608 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1609 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001610 return 0;
1611 }
1612
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001613 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1615 thread->addOutputTrack(thread2);
1616 mPlaybackThreads.add(id, thread);
1617 // notify client processes of the new output creation
1618 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1619 return id;
1620}
1621
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001622status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001623{
Glenn Kastend96c5722012-04-25 13:44:49 -07001624 return closeOutput_nonvirtual(output);
1625}
1626
1627status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1628{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001629 // keep strong reference on the playback thread so that
1630 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001631 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001632 {
1633 Mutex::Autolock _l(mLock);
1634 thread = checkPlaybackThread_l(output);
1635 if (thread == NULL) {
1636 return BAD_VALUE;
1637 }
1638
Steve Block3856b092011-10-20 11:56:00 +01001639 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001640
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001641 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001642 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001643 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001644 DuplicatingThread *dupThread =
1645 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001646 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001647
1648 }
1649 }
1650 }
1651
1652
1653 mPlaybackThreads.removeItem(output);
1654 // save all effects to the default thread
1655 if (mPlaybackThreads.size()) {
1656 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1657 if (dstThread != NULL) {
1658 // audioflinger lock is held here so the acquisition order of thread locks does not
1659 // matter
1660 Mutex::Autolock _dl(dstThread->mLock);
1661 Mutex::Autolock _sl(thread->mLock);
1662 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1663 for (size_t i = 0; i < effectChains.size(); i ++) {
1664 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001665 }
1666 }
1667 }
Glenn Kastena1117922012-01-26 10:53:32 -08001668 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001669 }
1670 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001671 // The thread entity (active unit of execution) is no longer running here,
1672 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001673
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001674 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001675 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001676 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001677 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001678 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001679 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001680 }
1681 return NO_ERROR;
1682}
1683
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001684status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001685{
1686 Mutex::Autolock _l(mLock);
1687 PlaybackThread *thread = checkPlaybackThread_l(output);
1688
1689 if (thread == NULL) {
1690 return BAD_VALUE;
1691 }
1692
Steve Block3856b092011-10-20 11:56:00 +01001693 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001694 thread->suspend();
1695
1696 return NO_ERROR;
1697}
1698
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001699status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001700{
1701 Mutex::Autolock _l(mLock);
1702 PlaybackThread *thread = checkPlaybackThread_l(output);
1703
1704 if (thread == NULL) {
1705 return BAD_VALUE;
1706 }
1707
Steve Block3856b092011-10-20 11:56:00 +01001708 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001709
1710 thread->restore();
1711
1712 return NO_ERROR;
1713}
1714
Eric Laurenta4c5a552012-03-29 10:12:40 -07001715audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1716 audio_devices_t *pDevices,
1717 uint32_t *pSamplingRate,
1718 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001719 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001720{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001721 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001722 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001723 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1724 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1725 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1726
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001727 uint32_t reqSamplingRate = config.sample_rate;
1728 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001729 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001730
1731 if (pDevices == NULL || *pDevices == 0) {
1732 return 0;
1733 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001734
Mathias Agopian65ab4712010-07-14 17:59:35 -07001735 Mutex::Autolock _l(mLock);
1736
Glenn Kasten32550952013-08-06 10:45:10 -07001737 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001738 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001739 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001740 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001741
John Grossmanee578c02012-07-23 17:05:46 -07001742 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001743 audio_io_handle_t id = nextUniqueId();
1744
Glenn Kasten32550952013-08-06 10:45:10 -07001745 audio_stream_in_t *inStream = NULL;
1746 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001747 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001748 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1749 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001750 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001751 config.sample_rate,
1752 config.format,
1753 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001754 status);
1755
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001756 // If the input could not be opened with the requested parameters and we can handle the
1757 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1758 // 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 -07001759 if (status == BAD_VALUE &&
1760 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1761 (config.sample_rate <= 2 * reqSamplingRate) &&
Glenn Kastenf506e942013-08-06 10:49:43 -07001762 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001763 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Glenn Kasten254af182012-07-03 14:59:05 -07001764 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001765 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001766 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07001767 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001768 }
1769
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001770 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001771
Glenn Kasten46909e72013-02-26 09:20:22 -08001772#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001773 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1774 // or (re-)create if current Pipe is idle and does not match the new format
1775 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001776 enum {
1777 TEE_SINK_NO, // don't copy input
1778 TEE_SINK_NEW, // copy input using a new pipe
1779 TEE_SINK_OLD, // copy input using an existing pipe
1780 } kind;
1781 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1782 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001783 if (!mTeeSinkInputEnabled) {
1784 kind = TEE_SINK_NO;
1785 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001786 kind = TEE_SINK_NO;
1787 } else if (mRecordTeeSink == 0) {
1788 kind = TEE_SINK_NEW;
1789 } else if (mRecordTeeSink->getStrongCount() != 1) {
1790 kind = TEE_SINK_NO;
1791 } else if (format == mRecordTeeSink->format()) {
1792 kind = TEE_SINK_OLD;
1793 } else {
1794 kind = TEE_SINK_NEW;
1795 }
1796 switch (kind) {
1797 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001798 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001799 size_t numCounterOffers = 0;
1800 const NBAIO_Format offers[1] = {format};
1801 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1802 ALOG_ASSERT(index == 0);
1803 PipeReader *pipeReader = new PipeReader(*pipe);
1804 numCounterOffers = 0;
1805 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1806 ALOG_ASSERT(index == 0);
1807 mRecordTeeSink = pipe;
1808 mRecordTeeSource = pipeReader;
1809 teeSink = pipe;
1810 }
1811 break;
1812 case TEE_SINK_OLD:
1813 teeSink = mRecordTeeSink;
1814 break;
1815 case TEE_SINK_NO:
1816 default:
1817 break;
1818 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001819#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001820
Dima Zavin799a70e2011-04-18 16:57:27 -07001821 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1822
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001823 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001824 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001825 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001826 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001827 input,
1828 reqSamplingRate,
Glenn Kastenf506e942013-08-06 10:49:43 -07001829 reqChannelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001830 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001831 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001832 *pDevices
1833#ifdef TEE_SINK
1834 , teeSink
1835#endif
1836 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001837 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001838 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001839 if (pSamplingRate != NULL) {
1840 *pSamplingRate = reqSamplingRate;
1841 }
1842 if (pFormat != NULL) {
1843 *pFormat = config.format;
1844 }
1845 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001846 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001847 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001848
Mathias Agopian65ab4712010-07-14 17:59:35 -07001849 // notify client processes of the new input creation
1850 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1851 return id;
1852 }
1853
1854 return 0;
1855}
1856
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001857status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001858{
Glenn Kastend96c5722012-04-25 13:44:49 -07001859 return closeInput_nonvirtual(input);
1860}
1861
1862status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1863{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001864 // keep strong reference on the record thread so that
1865 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001866 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001867 {
1868 Mutex::Autolock _l(mLock);
1869 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001870 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001871 return BAD_VALUE;
1872 }
1873
Steve Block3856b092011-10-20 11:56:00 +01001874 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001875 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001876 mRecordThreads.removeItem(input);
1877 }
1878 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001879 // The thread entity (active unit of execution) is no longer running here,
1880 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001881
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001882 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001883 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001884 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001885 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001886 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001887
1888 return NO_ERROR;
1889}
1890
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001891status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001892{
1893 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001894 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001895
1896 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1897 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001898 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001899 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900
1901 return NO_ERROR;
1902}
1903
1904
1905int AudioFlinger::newAudioSessionId()
1906{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001907 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001908}
1909
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001910void AudioFlinger::acquireAudioSessionId(int audioSession)
1911{
1912 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001913 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001914 ALOGV("acquiring %d from %d", audioSession, caller);
Eric Laurentd1b28d42013-09-18 18:47:13 -07001915
1916 // Ignore requests received from processes not known as notification client. The request
1917 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1918 // called from a different pid leaving a stale session reference. Also we don't know how
1919 // to clear this reference if the client process dies.
1920 if (mNotificationClients.indexOfKey(caller) < 0) {
1921 ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
1922 return;
1923 }
1924
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001925 size_t num = mAudioSessionRefs.size();
1926 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001927 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001928 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1929 ref->mCnt++;
1930 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001931 return;
1932 }
1933 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001934 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1935 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001936}
1937
1938void AudioFlinger::releaseAudioSessionId(int audioSession)
1939{
1940 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001941 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001942 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001943 size_t num = mAudioSessionRefs.size();
1944 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001945 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001946 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1947 ref->mCnt--;
1948 ALOGV(" decremented refcount to %d", ref->mCnt);
1949 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001950 mAudioSessionRefs.removeAt(i);
1951 delete ref;
1952 purgeStaleEffects_l();
1953 }
1954 return;
1955 }
1956 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001957 // If the caller is mediaserver it is likely that the session being released was acquired
1958 // on behalf of a process not in notification clients and we ignore the warning.
1959 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001960}
1961
1962void AudioFlinger::purgeStaleEffects_l() {
1963
Steve Block3856b092011-10-20 11:56:00 +01001964 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001965
1966 Vector< sp<EffectChain> > chains;
1967
1968 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1969 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1970 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1971 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001972 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1973 chains.push(ec);
1974 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001975 }
1976 }
1977 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1978 sp<RecordThread> t = mRecordThreads.valueAt(i);
1979 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1980 sp<EffectChain> ec = t->mEffectChains[j];
1981 chains.push(ec);
1982 }
1983 }
1984
1985 for (size_t i = 0; i < chains.size(); i++) {
1986 sp<EffectChain> ec = chains[i];
1987 int sessionid = ec->sessionId();
1988 sp<ThreadBase> t = ec->mThread.promote();
1989 if (t == 0) {
1990 continue;
1991 }
1992 size_t numsessionrefs = mAudioSessionRefs.size();
1993 bool found = false;
1994 for (size_t k = 0; k < numsessionrefs; k++) {
1995 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001996 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001997 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001998 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001999 found = true;
2000 break;
2001 }
2002 }
2003 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002004 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002005 // remove all effects from the chain
2006 while (ec->mEffects.size()) {
2007 sp<EffectModule> effect = ec->mEffects[0];
2008 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002009 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002010 if (effect->purgeHandles()) {
2011 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002012 }
2013 AudioSystem::unregisterEffect(effect->id());
2014 }
2015 }
2016 }
2017 return;
2018}
2019
Mathias Agopian65ab4712010-07-14 17:59:35 -07002020// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002021AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002022{
Glenn Kastena1117922012-01-26 10:53:32 -08002023 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024}
2025
2026// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002027AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002028{
2029 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002030 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002031}
2032
2033// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002034AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002035{
Glenn Kastena1117922012-01-26 10:53:32 -08002036 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002037}
2038
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002039uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002040{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002041 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002042}
2043
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002044AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002045{
2046 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2047 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002048 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002049 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002050 return thread;
2051 }
2052 }
2053 return NULL;
2054}
2055
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002056audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002057{
2058 PlaybackThread *thread = primaryPlaybackThread_l();
2059
2060 if (thread == NULL) {
2061 return 0;
2062 }
2063
Eric Laurentf1c04f92012-08-28 14:26:53 -07002064 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002065}
2066
Eric Laurenta011e352012-03-29 15:51:43 -07002067sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2068 int triggerSession,
2069 int listenerSession,
2070 sync_event_callback_t callBack,
2071 void *cookie)
2072{
2073 Mutex::Autolock _l(mLock);
2074
2075 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2076 status_t playStatus = NAME_NOT_FOUND;
2077 status_t recStatus = NAME_NOT_FOUND;
2078 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2079 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2080 if (playStatus == NO_ERROR) {
2081 return event;
2082 }
2083 }
2084 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2085 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2086 if (recStatus == NO_ERROR) {
2087 return event;
2088 }
2089 }
2090 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2091 mPendingSyncEvents.add(event);
2092 } else {
2093 ALOGV("createSyncEvent() invalid event %d", event->type());
2094 event.clear();
2095 }
2096 return event;
2097}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002098
Mathias Agopian65ab4712010-07-14 17:59:35 -07002099// ----------------------------------------------------------------------------
2100// Effect management
2101// ----------------------------------------------------------------------------
2102
2103
Glenn Kastenf587ba52012-01-26 16:25:10 -08002104status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002105{
2106 Mutex::Autolock _l(mLock);
2107 return EffectQueryNumberEffects(numEffects);
2108}
2109
Glenn Kastenf587ba52012-01-26 16:25:10 -08002110status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002111{
2112 Mutex::Autolock _l(mLock);
2113 return EffectQueryEffect(index, descriptor);
2114}
2115
Glenn Kasten5e92a782012-01-30 07:40:52 -08002116status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002117 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002118{
2119 Mutex::Autolock _l(mLock);
2120 return EffectGetDescriptor(pUuid, descriptor);
2121}
2122
2123
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002124sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002125 effect_descriptor_t *pDesc,
2126 const sp<IEffectClient>& effectClient,
2127 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002128 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002129 int sessionId,
2130 status_t *status,
2131 int *id,
2132 int *enabled)
2133{
2134 status_t lStatus = NO_ERROR;
2135 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002136 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002137
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002138 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002139 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002140 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002141
2142 if (pDesc == NULL) {
2143 lStatus = BAD_VALUE;
2144 goto Exit;
2145 }
2146
Eric Laurent84e9a102010-09-23 16:10:16 -07002147 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002148 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002149 lStatus = PERMISSION_DENIED;
2150 goto Exit;
2151 }
2152
Dima Zavinfce7a472011-04-19 22:30:36 -07002153 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002154 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002155 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002156 lStatus = PERMISSION_DENIED;
2157 goto Exit;
2158 }
2159
Mathias Agopian65ab4712010-07-14 17:59:35 -07002160 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002161 if (!EffectIsNullUuid(&pDesc->uuid)) {
2162 // if uuid is specified, request effect descriptor
2163 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2164 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002165 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002166 goto Exit;
2167 }
2168 } else {
2169 // if uuid is not specified, look for an available implementation
2170 // of the required type in effect factory
2171 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002172 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002173 lStatus = BAD_VALUE;
2174 goto Exit;
2175 }
2176 uint32_t numEffects = 0;
2177 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002178 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002179 bool found = false;
2180
2181 lStatus = EffectQueryNumberEffects(&numEffects);
2182 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002183 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184 goto Exit;
2185 }
2186 for (uint32_t i = 0; i < numEffects; i++) {
2187 lStatus = EffectQueryEffect(i, &desc);
2188 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002189 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002190 continue;
2191 }
2192 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2193 // If matching type found save effect descriptor. If the session is
2194 // 0 and the effect is not auxiliary, continue enumeration in case
2195 // an auxiliary version of this effect type is available
2196 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002197 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002198 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002199 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2200 break;
2201 }
2202 }
2203 }
2204 if (!found) {
2205 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002206 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002207 goto Exit;
2208 }
2209 // For same effect type, chose auxiliary version over insert version if
2210 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002211 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002212 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002213 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002214 }
2215 }
2216
2217 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002218 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002219 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2220 lStatus = INVALID_OPERATION;
2221 goto Exit;
2222 }
2223
Eric Laurent59255e42011-07-27 19:49:51 -07002224 // check recording permission for visualizer
2225 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2226 !recordingAllowed()) {
2227 lStatus = PERMISSION_DENIED;
2228 goto Exit;
2229 }
2230
Mathias Agopian65ab4712010-07-14 17:59:35 -07002231 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002232 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002233 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2234 // if the output returned by getOutputForEffect() is removed before we lock the
2235 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2236 // and we will exit safely
2237 io = AudioSystem::getOutputForEffect(&desc);
2238 ALOGV("createEffect got output %d", io);
2239 }
2240
2241 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002242
2243 // If output is not specified try to find a matching audio session ID in one of the
2244 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002245 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2246 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002247 // Note: io is never 0 when creating an effect on an input
2248 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002249 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2250 // output must be specified by AudioPolicyManager when using session
2251 // AUDIO_SESSION_OUTPUT_STAGE
2252 lStatus = BAD_VALUE;
2253 goto Exit;
2254 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002255 // look for the thread where the specified audio session is present
2256 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2257 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2258 io = mPlaybackThreads.keyAt(i);
2259 break;
2260 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002261 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002262 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002263 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2264 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2265 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002266 break;
2267 }
2268 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002269 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002270 // If no output thread contains the requested session ID, default to
2271 // first output. The effect chain will be moved to the correct output
2272 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002273 if (io == 0 && mPlaybackThreads.size()) {
2274 io = mPlaybackThreads.keyAt(0);
2275 }
Steve Block3856b092011-10-20 11:56:00 +01002276 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002277 }
2278 ThreadBase *thread = checkRecordThread_l(io);
2279 if (thread == NULL) {
2280 thread = checkPlaybackThread_l(io);
2281 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002282 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002283 lStatus = BAD_VALUE;
2284 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002285 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002286 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002287
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002288 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002289
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002290 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002291 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2292 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002293 if (handle != 0 && id != NULL) {
2294 *id = handle->id();
2295 }
2296 }
2297
2298Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002299 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002300 return handle;
2301}
2302
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002303status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2304 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002305{
Steve Block3856b092011-10-20 11:56:00 +01002306 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002307 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002308 Mutex::Autolock _l(mLock);
2309 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002310 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002311 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002312 }
Eric Laurentde070132010-07-13 04:45:46 -07002313 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2314 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002315 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002316 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002317 }
Eric Laurentde070132010-07-13 04:45:46 -07002318 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2319 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002320 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002321 return BAD_VALUE;
2322 }
2323
2324 Mutex::Autolock _dl(dstThread->mLock);
2325 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002326 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002327}
2328
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002329// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002330status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002331 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002332 AudioFlinger::PlaybackThread *dstThread,
2333 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002334{
Steve Block3856b092011-10-20 11:56:00 +01002335 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002336 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002337
Eric Laurent59255e42011-07-27 19:49:51 -07002338 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002339 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002340 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002341 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002342 return INVALID_OPERATION;
2343 }
2344
Eric Laurent39e94f82010-07-28 01:32:47 -07002345 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002346 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002347 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002348 // removed.
2349 srcThread->removeEffectChain_l(chain);
2350
2351 // transfer all effects one by one so that new effect chain is created on new thread with
2352 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002353 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002354 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002355 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002356 Vector< sp<EffectModule> > removed;
2357 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002358 while (effect != 0) {
2359 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002360 removed.add(effect);
2361 status = dstThread->addEffect_l(effect);
2362 if (status != NO_ERROR) {
2363 break;
2364 }
Eric Laurentec35a142011-10-05 17:42:25 -07002365 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2366 if (effect->state() == EffectModule::ACTIVE ||
2367 effect->state() == EffectModule::STOPPING) {
2368 effect->start();
2369 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002370 // if the move request is not received from audio policy manager, the effect must be
2371 // re-registered with the new strategy and output
2372 if (dstChain == 0) {
2373 dstChain = effect->chain().promote();
2374 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002375 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002376 status = NO_INIT;
2377 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002378 }
2379 strategy = dstChain->strategy();
2380 }
2381 if (reRegister) {
2382 AudioSystem::unregisterEffect(effect->id());
2383 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002384 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002385 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002386 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002387 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002388 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002389 }
Eric Laurentde070132010-07-13 04:45:46 -07002390 effect = chain->getEffectFromId_l(0);
2391 }
2392
Eric Laurent5baf2af2013-09-12 17:37:00 -07002393 if (status != NO_ERROR) {
2394 for (size_t i = 0; i < removed.size(); i++) {
2395 srcThread->addEffect_l(removed[i]);
2396 if (dstChain != 0 && reRegister) {
2397 AudioSystem::unregisterEffect(removed[i]->id());
2398 AudioSystem::registerEffect(&removed[i]->desc(),
2399 srcThread->id(),
2400 strategy,
2401 sessionId,
2402 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002403 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002404 }
2405 }
2406 }
2407
2408 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002409}
2410
Eric Laurent5baf2af2013-09-12 17:37:00 -07002411bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002412{
2413 if (mGlobalEffectEnableTime != 0 &&
2414 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2415 return true;
2416 }
2417
2418 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2419 sp<EffectChain> ec =
2420 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002421 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002422 return true;
2423 }
2424 }
2425 return false;
2426}
2427
Eric Laurent5baf2af2013-09-12 17:37:00 -07002428void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002429{
2430 Mutex::Autolock _l(mLock);
2431
2432 mGlobalEffectEnableTime = systemTime();
2433
2434 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2435 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2436 if (t->mType == ThreadBase::OFFLOAD) {
2437 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2438 }
2439 }
2440
2441}
2442
Glenn Kastenda6ef132013-01-10 12:31:01 -08002443struct Entry {
2444#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2445 char mName[MAX_NAME];
2446};
2447
2448int comparEntry(const void *p1, const void *p2)
2449{
2450 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2451}
2452
Glenn Kasten46909e72013-02-26 09:20:22 -08002453#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002454void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002455{
Eric Laurent81784c32012-11-19 14:55:58 -08002456 NBAIO_Source *teeSource = source.get();
2457 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002458 // .wav rotation
2459 // There is a benign race condition if 2 threads call this simultaneously.
2460 // They would both traverse the directory, but the result would simply be
2461 // failures at unlink() which are ignored. It's also unlikely since
2462 // normally dumpsys is only done by bugreport or from the command line.
2463 char teePath[32+256];
2464 strcpy(teePath, "/data/misc/media");
2465 size_t teePathLen = strlen(teePath);
2466 DIR *dir = opendir(teePath);
2467 teePath[teePathLen++] = '/';
2468 if (dir != NULL) {
2469#define MAX_SORT 20 // number of entries to sort
2470#define MAX_KEEP 10 // number of entries to keep
2471 struct Entry entries[MAX_SORT];
2472 size_t entryCount = 0;
2473 while (entryCount < MAX_SORT) {
2474 struct dirent de;
2475 struct dirent *result = NULL;
2476 int rc = readdir_r(dir, &de, &result);
2477 if (rc != 0) {
2478 ALOGW("readdir_r failed %d", rc);
2479 break;
2480 }
2481 if (result == NULL) {
2482 break;
2483 }
2484 if (result != &de) {
2485 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2486 break;
2487 }
2488 // ignore non .wav file entries
2489 size_t nameLen = strlen(de.d_name);
2490 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2491 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2492 continue;
2493 }
2494 strcpy(entries[entryCount++].mName, de.d_name);
2495 }
2496 (void) closedir(dir);
2497 if (entryCount > MAX_KEEP) {
2498 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2499 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2500 strcpy(&teePath[teePathLen], entries[i].mName);
2501 (void) unlink(teePath);
2502 }
2503 }
2504 } else {
2505 if (fd >= 0) {
2506 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2507 }
2508 }
Eric Laurent81784c32012-11-19 14:55:58 -08002509 char teeTime[16];
2510 struct timeval tv;
2511 gettimeofday(&tv, NULL);
2512 struct tm tm;
2513 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002514 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2515 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2516 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2517 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002518 if (teeFd >= 0) {
2519 char wavHeader[44];
2520 memcpy(wavHeader,
2521 "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",
2522 sizeof(wavHeader));
2523 NBAIO_Format format = teeSource->format();
2524 unsigned channelCount = Format_channelCount(format);
2525 ALOG_ASSERT(channelCount <= FCC_2);
2526 uint32_t sampleRate = Format_sampleRate(format);
2527 wavHeader[22] = channelCount; // number of channels
2528 wavHeader[24] = sampleRate; // sample rate
2529 wavHeader[25] = sampleRate >> 8;
2530 wavHeader[32] = channelCount * 2; // block alignment
2531 write(teeFd, wavHeader, sizeof(wavHeader));
2532 size_t total = 0;
2533 bool firstRead = true;
2534 for (;;) {
2535#define TEE_SINK_READ 1024
2536 short buffer[TEE_SINK_READ * FCC_2];
2537 size_t count = TEE_SINK_READ;
2538 ssize_t actual = teeSource->read(buffer, count,
2539 AudioBufferProvider::kInvalidPTS);
2540 bool wasFirstRead = firstRead;
2541 firstRead = false;
2542 if (actual <= 0) {
2543 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2544 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002545 }
Eric Laurent81784c32012-11-19 14:55:58 -08002546 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002547 }
Eric Laurent81784c32012-11-19 14:55:58 -08002548 ALOG_ASSERT(actual <= (ssize_t)count);
2549 write(teeFd, buffer, actual * channelCount * sizeof(short));
2550 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002551 }
Eric Laurent81784c32012-11-19 14:55:58 -08002552 lseek(teeFd, (off_t) 4, SEEK_SET);
2553 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2554 write(teeFd, &temp, sizeof(temp));
2555 lseek(teeFd, (off_t) 40, SEEK_SET);
2556 temp = total * channelCount * sizeof(short);
2557 write(teeFd, &temp, sizeof(temp));
2558 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002559 if (fd >= 0) {
2560 fdprintf(fd, "tee copied to %s\n", teePath);
2561 }
Eric Laurent59255e42011-07-27 19:49:51 -07002562 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002563 if (fd >= 0) {
2564 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2565 }
Eric Laurent59255e42011-07-27 19:49:51 -07002566 }
2567 }
2568}
Glenn Kasten46909e72013-02-26 09:20:22 -08002569#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002570
Mathias Agopian65ab4712010-07-14 17:59:35 -07002571// ----------------------------------------------------------------------------
2572
2573status_t AudioFlinger::onTransact(
2574 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2575{
2576 return BnAudioFlinger::onTransact(code, data, reply, flags);
2577}
2578
Mathias Agopian65ab4712010-07-14 17:59:35 -07002579}; // namespace android