blob: 360db4ff16dce8297e815f718d7c2ad457bf64ae [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 }
216}
217
Eric Laurenta4c5a552012-03-29 10:12:40 -0700218static const char * const audio_interfaces[] = {
219 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
220 AUDIO_HARDWARE_MODULE_ID_A2DP,
221 AUDIO_HARDWARE_MODULE_ID_USB,
222};
223#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
224
John Grossmanee578c02012-07-23 17:05:46 -0700225AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
226 audio_module_handle_t module,
227 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700228{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700229 // if module is 0, the request comes from an old policy manager and we should load
230 // well known modules
231 if (module == 0) {
232 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
233 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
234 loadHwModule_l(audio_interfaces[i]);
235 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700236 // then try to find a module supporting the requested device.
237 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
238 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
239 audio_hw_device_t *dev = audioHwDevice->hwDevice();
240 if ((dev->get_supported_devices != NULL) &&
241 (dev->get_supported_devices(dev) & devices) == devices)
242 return audioHwDevice;
243 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700244 } else {
245 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700246 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
247 if (audioHwDevice != NULL) {
248 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700249 }
250 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700251
Dima Zavin799a70e2011-04-18 16:57:27 -0700252 return NULL;
253}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700255void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256{
257 const size_t SIZE = 256;
258 char buffer[SIZE];
259 String8 result;
260
261 result.append("Clients:\n");
262 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800263 sp<Client> client = mClients.valueAt(i).promote();
264 if (client != 0) {
265 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
266 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267 }
268 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700269
Eric Laurentd1b28d42013-09-18 18:47:13 -0700270 result.append("Notification Clients:\n");
271 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
272 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
273 result.append(buffer);
274 }
275
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700276 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800277 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700278 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
279 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800280 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700281 result.append(buffer);
282 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700283 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284}
285
286
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700287void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700288{
289 const size_t SIZE = 256;
290 char buffer[SIZE];
291 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800292 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700293
John Grossman4ff14ba2012-02-08 16:37:41 -0800294 snprintf(buffer, SIZE, "Hardware status: %d\n"
295 "Standby Time mSec: %u\n",
296 hardwareStatus,
297 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700298 result.append(buffer);
299 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300}
301
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700302void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303{
304 const size_t SIZE = 256;
305 char buffer[SIZE];
306 String8 result;
307 snprintf(buffer, SIZE, "Permission Denial: "
308 "can't dump AudioFlinger from pid=%d, uid=%d\n",
309 IPCThreadState::self()->getCallingPid(),
310 IPCThreadState::self()->getCallingUid());
311 result.append(buffer);
312 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313}
314
Eric Laurent81784c32012-11-19 14:55:58 -0800315bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316{
317 bool locked = false;
318 for (int i = 0; i < kDumpLockRetries; ++i) {
319 if (mutex.tryLock() == NO_ERROR) {
320 locked = true;
321 break;
322 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800323 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 }
325 return locked;
326}
327
328status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
329{
Glenn Kasten44deb052012-02-05 18:09:08 -0800330 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331 dumpPermissionDenial(fd, args);
332 } else {
333 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800334 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 if (!hardwareLocked) {
336 String8 result(kHardwareLockedString);
337 write(fd, result.string(), result.size());
338 } else {
339 mHardwareLock.unlock();
340 }
341
Eric Laurent81784c32012-11-19 14:55:58 -0800342 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343
344 // failed to lock - AudioFlinger is probably deadlocked
345 if (!locked) {
346 String8 result(kDeadlockedString);
347 write(fd, result.string(), result.size());
348 }
349
350 dumpClients(fd, args);
351 dumpInternals(fd, args);
352
353 // dump playback threads
354 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
355 mPlaybackThreads.valueAt(i)->dump(fd, args);
356 }
357
358 // dump record threads
359 for (size_t i = 0; i < mRecordThreads.size(); i++) {
360 mRecordThreads.valueAt(i)->dump(fd, args);
361 }
362
Dima Zavin799a70e2011-04-18 16:57:27 -0700363 // dump all hardware devs
364 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700365 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700366 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700367 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700368
Glenn Kasten46909e72013-02-26 09:20:22 -0800369#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700370 // dump the serially shared record tee sink
371 if (mRecordTeeSource != 0) {
372 dumpTee(fd, mRecordTeeSource);
373 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800374#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700375
Glenn Kastend65d73c2012-06-22 17:21:07 -0700376 if (locked) {
377 mLock.unlock();
378 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800379
380 // append a copy of media.log here by forwarding fd to it, but don't attempt
381 // to lookup the service if it's not running, as it will block for a second
382 if (mLogMemoryDealer != 0) {
383 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
384 if (binder != 0) {
385 fdprintf(fd, "\nmedia.log:\n");
386 Vector<String16> args;
387 binder->dump(fd, args);
388 }
389 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390 }
391 return NO_ERROR;
392}
393
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800394sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
395{
396 // If pid is already in the mClients wp<> map, then use that entry
397 // (for which promote() is always != 0), otherwise create a new entry and Client.
398 sp<Client> client = mClients.valueFor(pid).promote();
399 if (client == 0) {
400 client = new Client(this, pid);
401 mClients.add(pid, client);
402 }
403
404 return client;
405}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700406
Glenn Kasten9e58b552013-01-18 15:09:48 -0800407sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
408{
409 if (mLogMemoryDealer == 0) {
410 return new NBLog::Writer();
411 }
412 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
413 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
414 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
415 if (binder != 0) {
416 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
417 }
418 return writer;
419}
420
421void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
422{
Glenn Kasten685ef092013-02-04 08:15:34 -0800423 if (writer == 0) {
424 return;
425 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800426 sp<IMemory> iMemory(writer->getIMemory());
427 if (iMemory == 0) {
428 return;
429 }
430 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
431 if (binder != 0) {
432 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
433 // Now the media.log remote reference to IMemory is gone.
434 // When our last local reference to IMemory also drops to zero,
435 // the IMemory destructor will deallocate the region from mMemoryDealer.
436 }
437}
438
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439// IAudioFlinger interface
440
441
442sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800443 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700444 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800445 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700446 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800447 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800448 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700449 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800450 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800451 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700453 String8& name,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800454 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700455 status_t *status)
456{
457 sp<PlaybackThread::Track> track;
458 sp<TrackHandle> trackHandle;
459 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700460 status_t lStatus;
461 int lSessionId;
462
Glenn Kasten263709e2012-01-06 08:40:01 -0800463 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
464 // but if someone uses binder directly they could bypass that and cause us to crash
465 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000466 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700467 lStatus = BAD_VALUE;
468 goto Exit;
469 }
470
Glenn Kasten60a83922012-06-21 12:56:37 -0700471 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
472 // and we don't yet support 8.24 or 32-bit PCM
473 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
474 ALOGE("createTrack() invalid format %d", format);
475 lStatus = BAD_VALUE;
476 goto Exit;
477 }
478
Mathias Agopian65ab4712010-07-14 17:59:35 -0700479 {
480 Mutex::Autolock _l(mLock);
481 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700482 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800484 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700485 lStatus = BAD_VALUE;
486 goto Exit;
487 }
488
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800489 pid_t pid = IPCThreadState::self()->getCallingPid();
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800490
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800491 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700492
Steve Block3856b092011-10-20 11:56:00 +0100493 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700494 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700495 // check if an effect chain with the same session ID is present on another
496 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700497 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700498 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
499 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700500 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700501 if (sessions & PlaybackThread::EFFECT_SESSION) {
502 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700503 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700504 }
Eric Laurentde070132010-07-13 04:45:46 -0700505 }
506 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700507 lSessionId = *sessionId;
508 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700509 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700510 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511 if (sessionId != NULL) {
512 *sessionId = lSessionId;
513 }
514 }
Steve Block3856b092011-10-20 11:56:00 +0100515 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700516
517 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800518 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Glenn Kasten2fc14732013-08-05 14:58:14 -0700519 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700520
521 // move effect chain to this output thread if an effect on same session was waiting
522 // for a track to be created
523 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700524 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700525 Mutex::Autolock _dl(thread->mLock);
526 Mutex::Autolock _sl(effectThread->mLock);
527 moveEffectChain_l(lSessionId, effectThread, thread, true);
528 }
Eric Laurenta011e352012-03-29 15:51:43 -0700529
530 // Look for sync events awaiting for a session to be used.
531 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
532 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
533 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700534 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700535 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700536 } else {
537 mPendingSyncEvents[i]->cancel();
538 }
Eric Laurenta011e352012-03-29 15:51:43 -0700539 mPendingSyncEvents.removeAt(i);
540 i--;
541 }
542 }
543 }
Glenn Kastene198c362013-08-13 09:13:36 -0700544
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545 }
Glenn Kastene198c362013-08-13 09:13:36 -0700546
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700548 // s for server's pid, n for normal mixer name, f for fast index
549 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
550 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551 trackHandle = new TrackHandle(track);
552 } else {
553 // remove local strong reference to Client before deleting the Track so that the Client
554 // destructor is called by the TrackBase destructor with mLock held
555 client.clear();
556 track.clear();
557 }
558
559Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700560 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561 return trackHandle;
562}
563
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800564uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700565{
566 Mutex::Autolock _l(mLock);
567 PlaybackThread *thread = checkPlaybackThread_l(output);
568 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000569 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700570 return 0;
571 }
572 return thread->sampleRate();
573}
574
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800575int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700576{
577 Mutex::Autolock _l(mLock);
578 PlaybackThread *thread = checkPlaybackThread_l(output);
579 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000580 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581 return 0;
582 }
583 return thread->channelCount();
584}
585
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800586audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587{
588 Mutex::Autolock _l(mLock);
589 PlaybackThread *thread = checkPlaybackThread_l(output);
590 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000591 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800592 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593 }
594 return thread->format();
595}
596
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800597size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700598{
599 Mutex::Autolock _l(mLock);
600 PlaybackThread *thread = checkPlaybackThread_l(output);
601 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000602 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603 return 0;
604 }
Glenn Kasten58912562012-04-03 10:45:00 -0700605 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
606 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607 return thread->frameCount();
608}
609
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800610uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700611{
612 Mutex::Autolock _l(mLock);
613 PlaybackThread *thread = checkPlaybackThread_l(output);
614 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800615 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700616 return 0;
617 }
618 return thread->latency();
619}
620
621status_t AudioFlinger::setMasterVolume(float value)
622{
Eric Laurenta1884f92011-08-23 08:25:03 -0700623 status_t ret = initCheck();
624 if (ret != NO_ERROR) {
625 return ret;
626 }
627
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 // check calling permissions
629 if (!settingsAllowed()) {
630 return PERMISSION_DENIED;
631 }
632
Eric Laurenta4c5a552012-03-29 10:12:40 -0700633 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700634 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700635
John Grossmanee578c02012-07-23 17:05:46 -0700636 // Set master volume in the HALs which support it.
637 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
638 AutoMutex lock(mHardwareLock);
639 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800640
John Grossmanee578c02012-07-23 17:05:46 -0700641 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
642 if (dev->canSetMasterVolume()) {
643 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800644 }
John Grossmanee578c02012-07-23 17:05:46 -0700645 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647
John Grossmanee578c02012-07-23 17:05:46 -0700648 // Now set the master volume in each playback thread. Playback threads
649 // assigned to HALs which do not have master volume support will apply
650 // master volume during the mix operation. Threads with HALs which do
651 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800652 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700653 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654
655 return NO_ERROR;
656}
657
Glenn Kastenf78aee72012-01-04 11:00:47 -0800658status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700659{
Eric Laurenta1884f92011-08-23 08:25:03 -0700660 status_t ret = initCheck();
661 if (ret != NO_ERROR) {
662 return ret;
663 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664
665 // check calling permissions
666 if (!settingsAllowed()) {
667 return PERMISSION_DENIED;
668 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800669 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000670 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671 return BAD_VALUE;
672 }
673
674 { // scope for the lock
675 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700676 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700678 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679 mHardwareStatus = AUDIO_HW_IDLE;
680 }
681
682 if (NO_ERROR == ret) {
683 Mutex::Autolock _l(mLock);
684 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800685 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700686 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687 }
688
689 return ret;
690}
691
692status_t AudioFlinger::setMicMute(bool state)
693{
Eric Laurenta1884f92011-08-23 08:25:03 -0700694 status_t ret = initCheck();
695 if (ret != NO_ERROR) {
696 return ret;
697 }
698
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 // check calling permissions
700 if (!settingsAllowed()) {
701 return PERMISSION_DENIED;
702 }
703
704 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700705 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700707 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 mHardwareStatus = AUDIO_HW_IDLE;
709 return ret;
710}
711
712bool AudioFlinger::getMicMute() const
713{
Eric Laurenta1884f92011-08-23 08:25:03 -0700714 status_t ret = initCheck();
715 if (ret != NO_ERROR) {
716 return false;
717 }
718
Dima Zavinfce7a472011-04-19 22:30:36 -0700719 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800720 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700721 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700723 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724 mHardwareStatus = AUDIO_HW_IDLE;
725 return state;
726}
727
728status_t AudioFlinger::setMasterMute(bool muted)
729{
John Grossmand8f178d2012-07-20 14:51:35 -0700730 status_t ret = initCheck();
731 if (ret != NO_ERROR) {
732 return ret;
733 }
734
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 // check calling permissions
736 if (!settingsAllowed()) {
737 return PERMISSION_DENIED;
738 }
739
John Grossmanee578c02012-07-23 17:05:46 -0700740 Mutex::Autolock _l(mLock);
741 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700742
John Grossmanee578c02012-07-23 17:05:46 -0700743 // Set master mute in the HALs which support it.
744 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
745 AutoMutex lock(mHardwareLock);
746 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700747
John Grossmanee578c02012-07-23 17:05:46 -0700748 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
749 if (dev->canSetMasterMute()) {
750 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700751 }
John Grossmanee578c02012-07-23 17:05:46 -0700752 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700753 }
754
John Grossmanee578c02012-07-23 17:05:46 -0700755 // Now set the master mute in each playback thread. Playback threads
756 // assigned to HALs which do not have master mute support will apply master
757 // mute during the mix operation. Threads with HALs which do support master
758 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800759 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700760 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761
762 return NO_ERROR;
763}
764
765float AudioFlinger::masterVolume() const
766{
Glenn Kasten98067102011-12-13 11:47:54 -0800767 Mutex::Autolock _l(mLock);
768 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769}
770
771bool AudioFlinger::masterMute() const
772{
Glenn Kasten98067102011-12-13 11:47:54 -0800773 Mutex::Autolock _l(mLock);
774 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775}
776
John Grossman4ff14ba2012-02-08 16:37:41 -0800777float AudioFlinger::masterVolume_l() const
778{
John Grossman4ff14ba2012-02-08 16:37:41 -0800779 return mMasterVolume;
780}
781
John Grossmand8f178d2012-07-20 14:51:35 -0700782bool AudioFlinger::masterMute_l() const
783{
John Grossmanee578c02012-07-23 17:05:46 -0700784 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700785}
786
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800787status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
788 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789{
790 // check calling permissions
791 if (!settingsAllowed()) {
792 return PERMISSION_DENIED;
793 }
794
Glenn Kasten263709e2012-01-06 08:40:01 -0800795 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000796 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797 return BAD_VALUE;
798 }
799
800 AutoMutex lock(mLock);
801 PlaybackThread *thread = NULL;
802 if (output) {
803 thread = checkPlaybackThread_l(output);
804 if (thread == NULL) {
805 return BAD_VALUE;
806 }
807 }
808
809 mStreamTypes[stream].volume = value;
810
811 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800812 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700813 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 }
815 } else {
816 thread->setStreamVolume(stream, value);
817 }
818
819 return NO_ERROR;
820}
821
Glenn Kastenfff6d712012-01-12 16:38:12 -0800822status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823{
824 // check calling permissions
825 if (!settingsAllowed()) {
826 return PERMISSION_DENIED;
827 }
828
Glenn Kasten263709e2012-01-06 08:40:01 -0800829 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700830 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000831 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832 return BAD_VALUE;
833 }
834
Eric Laurent93575202011-01-18 18:39:02 -0800835 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700836 mStreamTypes[stream].mute = muted;
837 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700838 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839
840 return NO_ERROR;
841}
842
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800843float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844{
Glenn Kasten263709e2012-01-06 08:40:01 -0800845 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 return 0.0f;
847 }
848
849 AutoMutex lock(mLock);
850 float volume;
851 if (output) {
852 PlaybackThread *thread = checkPlaybackThread_l(output);
853 if (thread == NULL) {
854 return 0.0f;
855 }
856 volume = thread->streamVolume(stream);
857 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800858 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 }
860
861 return volume;
862}
863
Glenn Kastenfff6d712012-01-12 16:38:12 -0800864bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865{
Glenn Kasten263709e2012-01-06 08:40:01 -0800866 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867 return true;
868 }
869
Glenn Kasten6637baa2012-01-09 09:40:36 -0800870 AutoMutex lock(mLock);
871 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872}
873
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800874status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700875{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700876 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
877 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800878
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879 // check calling permissions
880 if (!settingsAllowed()) {
881 return PERMISSION_DENIED;
882 }
883
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884 // ioHandle == 0 means the parameters are global to the audio hardware interface
885 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700886 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700887 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800888 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700889 AutoMutex lock(mHardwareLock);
890 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
891 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
892 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
893 status_t result = dev->set_parameters(dev, keyValuePairs.string());
894 final_result = result ?: final_result;
895 }
896 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800897 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700898 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
899 AudioParameter param = AudioParameter(keyValuePairs);
900 String8 value;
901 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700902 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
903 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700904 for (size_t i = 0; i < mRecordThreads.size(); i++) {
905 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700906 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700907 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
908 // collect all of the thread's session IDs
909 KeyedVector<int, bool> ids = thread->sessionIds();
910 // suspend effects associated with those session IDs
911 for (size_t j = 0; j < ids.size(); ++j) {
912 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700913 thread->setEffectSuspended(FX_IID_AEC,
914 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700915 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700916 thread->setEffectSuspended(FX_IID_NS,
917 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700918 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700919 }
920 }
Eric Laurentbee53372011-08-29 12:42:48 -0700921 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700922 }
923 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700924 String8 screenState;
925 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
926 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800927 if (isOff != (AudioFlinger::mScreenState & 1)) {
928 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700929 }
930 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700931 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932 }
933
934 // hold a strong ref on thread in case closeOutput() or closeInput() is called
935 // and the thread is exited once the lock is released
936 sp<ThreadBase> thread;
937 {
938 Mutex::Autolock _l(mLock);
939 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700940 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800942 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700943 // indicate output device change to all input threads for pre processing
944 AudioParameter param = AudioParameter(keyValuePairs);
945 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700946 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
947 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700948 for (size_t i = 0; i < mRecordThreads.size(); i++) {
949 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
950 }
951 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952 }
953 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800954 if (thread != 0) {
955 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956 }
957 return BAD_VALUE;
958}
959
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800960String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700962 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
963 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964
Eric Laurenta4c5a552012-03-29 10:12:40 -0700965 Mutex::Autolock _l(mLock);
966
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700968 String8 out_s8;
969
Dima Zavin799a70e2011-04-18 16:57:27 -0700970 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800971 char *s;
972 {
973 AutoMutex lock(mHardwareLock);
974 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700975 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800976 s = dev->get_parameters(dev, keys.string());
977 mHardwareStatus = AUDIO_HW_IDLE;
978 }
John Grossmanef7740b2012-02-09 11:28:36 -0800979 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700980 free(s);
981 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700982 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700983 }
984
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
986 if (playbackThread != NULL) {
987 return playbackThread->getParameters(keys);
988 }
989 RecordThread *recordThread = checkRecordThread_l(ioHandle);
990 if (recordThread != NULL) {
991 return recordThread->getParameters(keys);
992 }
993 return String8("");
994}
995
Glenn Kastendd8104c2012-07-02 12:42:44 -0700996size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
997 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998{
Eric Laurenta1884f92011-08-23 08:25:03 -0700999 status_t ret = initCheck();
1000 if (ret != NO_ERROR) {
1001 return 0;
1002 }
1003
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001004 AutoMutex lock(mHardwareLock);
1005 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001006 struct audio_config config;
1007 memset(&config, 0, sizeof(config));
1008 config.sample_rate = sampleRate;
1009 config.channel_mask = channelMask;
1010 config.format = format;
1011
John Grossmanee578c02012-07-23 17:05:46 -07001012 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1013 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001014 mHardwareStatus = AUDIO_HW_IDLE;
1015 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016}
1017
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001018unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001019{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001020 Mutex::Autolock _l(mLock);
1021
1022 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1023 if (recordThread != NULL) {
1024 return recordThread->getInputFramesLost();
1025 }
1026 return 0;
1027}
1028
1029status_t AudioFlinger::setVoiceVolume(float value)
1030{
Eric Laurenta1884f92011-08-23 08:25:03 -07001031 status_t ret = initCheck();
1032 if (ret != NO_ERROR) {
1033 return ret;
1034 }
1035
Mathias Agopian65ab4712010-07-14 17:59:35 -07001036 // check calling permissions
1037 if (!settingsAllowed()) {
1038 return PERMISSION_DENIED;
1039 }
1040
1041 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001042 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001043 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001044 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045 mHardwareStatus = AUDIO_HW_IDLE;
1046
1047 return ret;
1048}
1049
Glenn Kasten26c77552012-11-16 12:01:44 -08001050status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001051 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052{
1053 status_t status;
1054
1055 Mutex::Autolock _l(mLock);
1056
1057 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1058 if (playbackThread != NULL) {
1059 return playbackThread->getRenderPosition(halFrames, dspFrames);
1060 }
1061
1062 return BAD_VALUE;
1063}
1064
1065void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1066{
1067
1068 Mutex::Autolock _l(mLock);
1069
Glenn Kastenbb001922012-02-03 11:10:26 -08001070 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071 if (mNotificationClients.indexOfKey(pid) < 0) {
1072 sp<NotificationClient> notificationClient = new NotificationClient(this,
1073 client,
1074 pid);
Steve Block3856b092011-10-20 11:56:00 +01001075 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001076
1077 mNotificationClients.add(pid, notificationClient);
1078
1079 sp<IBinder> binder = client->asBinder();
1080 binder->linkToDeath(notificationClient);
1081
1082 // the config change is always sent from playback or record threads to avoid deadlock
1083 // with AudioSystem::gLock
1084 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001085 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001086 }
1087
1088 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001089 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090 }
1091 }
1092}
1093
1094void AudioFlinger::removeNotificationClient(pid_t pid)
1095{
1096 Mutex::Autolock _l(mLock);
1097
Glenn Kastena3b09252012-01-20 09:19:01 -08001098 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001099
Steve Block3856b092011-10-20 11:56:00 +01001100 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001101 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001102 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001103 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001104 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001105 ALOGV(" pid %d @ %d", ref->mPid, i);
1106 if (ref->mPid == pid) {
1107 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001108 mAudioSessionRefs.removeAt(i);
1109 delete ref;
1110 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001111 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001112 } else {
1113 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001114 }
1115 }
1116 if (removed) {
1117 purgeStaleEffects_l();
1118 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001119}
1120
1121// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001122void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123{
1124 size_t size = mNotificationClients.size();
1125 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001126 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1127 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128 }
1129}
1130
1131// removeClient_l() must be called with AudioFlinger::mLock held
1132void AudioFlinger::removeClient_l(pid_t pid)
1133{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001134 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001135 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001136 mClients.removeItem(pid);
1137}
1138
Eric Laurent717e1282012-06-29 16:36:52 -07001139// getEffectThread_l() must be called with AudioFlinger::mLock held
1140sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1141{
1142 sp<PlaybackThread> thread;
1143
1144 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1145 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1146 ALOG_ASSERT(thread == 0);
1147 thread = mPlaybackThreads.valueAt(i);
1148 }
1149 }
1150
1151 return thread;
1152}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001153
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155
1156// ----------------------------------------------------------------------------
1157
1158AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1159 : RefBase(),
1160 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001161 // 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 -07001162 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001163 mPid(pid),
1164 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001165{
1166 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1167}
1168
1169// Client destructor must be called with AudioFlinger::mLock held
1170AudioFlinger::Client::~Client()
1171{
1172 mAudioFlinger->removeClient_l(mPid);
1173}
1174
Glenn Kasten435dbe62012-01-30 10:15:48 -08001175sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001176{
1177 return mMemoryDealer;
1178}
1179
John Grossman4ff14ba2012-02-08 16:37:41 -08001180// Reserve one of the limited slots for a timed audio track associated
1181// with this client
1182bool AudioFlinger::Client::reserveTimedTrack()
1183{
1184 const int kMaxTimedTracksPerClient = 4;
1185
1186 Mutex::Autolock _l(mTimedTrackLock);
1187
1188 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1189 ALOGW("can not create timed track - pid %d has exceeded the limit",
1190 mPid);
1191 return false;
1192 }
1193
1194 mTimedTrackCount++;
1195 return true;
1196}
1197
1198// Release a slot for a timed audio track
1199void AudioFlinger::Client::releaseTimedTrack()
1200{
1201 Mutex::Autolock _l(mTimedTrackLock);
1202 mTimedTrackCount--;
1203}
1204
Mathias Agopian65ab4712010-07-14 17:59:35 -07001205// ----------------------------------------------------------------------------
1206
1207AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1208 const sp<IAudioFlingerClient>& client,
1209 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001210 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211{
1212}
1213
1214AudioFlinger::NotificationClient::~NotificationClient()
1215{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001216}
1217
1218void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1219{
1220 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001221 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222}
1223
Mathias Agopian65ab4712010-07-14 17:59:35 -07001224
1225// ----------------------------------------------------------------------------
1226
Jeff Brown893a5642013-08-16 20:19:26 -07001227static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1228 return audio_is_remote_submix_device(inDevice);
1229}
1230
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001232 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001233 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001234 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001235 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001236 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001237 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001238 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001239 int *sessionId,
1240 status_t *status)
1241{
1242 sp<RecordThread::RecordTrack> recordTrack;
1243 sp<RecordHandle> recordHandle;
1244 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001245 status_t lStatus;
1246 RecordThread *thread;
1247 size_t inFrameCount;
1248 int lSessionId;
1249
1250 // check calling permissions
1251 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001252 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001253 lStatus = PERMISSION_DENIED;
1254 goto Exit;
1255 }
1256
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001257 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1258 ALOGE("openRecord() invalid format %d", format);
1259 lStatus = BAD_VALUE;
1260 goto Exit;
1261 }
1262
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263 // add client to list
1264 { // scope for mLock
1265 Mutex::Autolock _l(mLock);
1266 thread = checkRecordThread_l(input);
1267 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001268 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269 lStatus = BAD_VALUE;
1270 goto Exit;
1271 }
1272
Jeff Brown893a5642013-08-16 20:19:26 -07001273 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1274 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001275 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001276 lStatus = PERMISSION_DENIED;
1277 goto Exit;
1278 }
1279
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001280 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001281 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001282
1283 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001284 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001285 lSessionId = *sessionId;
1286 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001287 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 if (sessionId != NULL) {
1289 *sessionId = lSessionId;
1290 }
1291 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001292 // create new record track.
1293 // The record track uses one track in mHardwareMixerThread by convention.
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001294 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001295 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001296 frameCount, lSessionId,
1297 IPCThreadState::self()->getCallingUid(),
1298 flags, tid, &lStatus);
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001299 LOG_ALWAYS_FATAL_IF((recordTrack != 0) != (lStatus == NO_ERROR));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300 }
Glenn Kastene198c362013-08-13 09:13:36 -07001301
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001302 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001303 // remove local strong reference to Client before deleting the RecordTrack so that the
1304 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305 client.clear();
1306 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001307 goto Exit;
1308 }
1309
Glenn Kasten2fc14732013-08-05 14:58:14 -07001310 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001311 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312
1313Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001314 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001315 return recordHandle;
1316}
1317
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001318
1319
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320// ----------------------------------------------------------------------------
1321
Eric Laurenta4c5a552012-03-29 10:12:40 -07001322audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1323{
1324 if (!settingsAllowed()) {
1325 return 0;
1326 }
1327 Mutex::Autolock _l(mLock);
1328 return loadHwModule_l(name);
1329}
1330
1331// loadHwModule_l() must be called with AudioFlinger::mLock held
1332audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1333{
1334 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1335 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1336 ALOGW("loadHwModule() module %s already loaded", name);
1337 return mAudioHwDevs.keyAt(i);
1338 }
1339 }
1340
Eric Laurenta4c5a552012-03-29 10:12:40 -07001341 audio_hw_device_t *dev;
1342
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001343 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001344 if (rc) {
1345 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1346 return 0;
1347 }
1348
1349 mHardwareStatus = AUDIO_HW_INIT;
1350 rc = dev->init_check(dev);
1351 mHardwareStatus = AUDIO_HW_IDLE;
1352 if (rc) {
1353 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1354 return 0;
1355 }
1356
John Grossmanee578c02012-07-23 17:05:46 -07001357 // Check and cache this HAL's level of support for master mute and master
1358 // volume. If this is the first HAL opened, and it supports the get
1359 // methods, use the initial values provided by the HAL as the current
1360 // master mute and volume settings.
1361
1362 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1363 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001364 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001365
1366 if (0 == mAudioHwDevs.size()) {
1367 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1368 if (NULL != dev->get_master_volume) {
1369 float mv;
1370 if (OK == dev->get_master_volume(dev, &mv)) {
1371 mMasterVolume = mv;
1372 }
1373 }
1374
1375 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1376 if (NULL != dev->get_master_mute) {
1377 bool mm;
1378 if (OK == dev->get_master_mute(dev, &mm)) {
1379 mMasterMute = mm;
1380 }
1381 }
1382 }
1383
Eric Laurenta4c5a552012-03-29 10:12:40 -07001384 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001385 if ((NULL != dev->set_master_volume) &&
1386 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1387 flags = static_cast<AudioHwDevice::Flags>(flags |
1388 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1389 }
1390
1391 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1392 if ((NULL != dev->set_master_mute) &&
1393 (OK == dev->set_master_mute(dev, mMasterMute))) {
1394 flags = static_cast<AudioHwDevice::Flags>(flags |
1395 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1396 }
1397
Eric Laurenta4c5a552012-03-29 10:12:40 -07001398 mHardwareStatus = AUDIO_HW_IDLE;
1399 }
1400
1401 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001402 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001403
1404 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001405 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001406
1407 return handle;
1408
1409}
1410
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001411// ----------------------------------------------------------------------------
1412
Glenn Kasten3b16c762012-11-14 08:44:39 -08001413uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001414{
1415 Mutex::Autolock _l(mLock);
1416 PlaybackThread *thread = primaryPlaybackThread_l();
1417 return thread != NULL ? thread->sampleRate() : 0;
1418}
1419
Glenn Kastene33054e2012-11-14 12:54:39 -08001420size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001421{
1422 Mutex::Autolock _l(mLock);
1423 PlaybackThread *thread = primaryPlaybackThread_l();
1424 return thread != NULL ? thread->frameCountHAL() : 0;
1425}
1426
1427// ----------------------------------------------------------------------------
1428
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001429status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1430{
1431 uid_t uid = IPCThreadState::self()->getCallingUid();
1432 if (uid != AID_SYSTEM) {
1433 return PERMISSION_DENIED;
1434 }
1435 Mutex::Autolock _l(mLock);
1436 if (mIsDeviceTypeKnown) {
1437 return INVALID_OPERATION;
1438 }
1439 mIsLowRamDevice = isLowRamDevice;
1440 mIsDeviceTypeKnown = true;
1441 return NO_ERROR;
1442}
1443
1444// ----------------------------------------------------------------------------
1445
Eric Laurenta4c5a552012-03-29 10:12:40 -07001446audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1447 audio_devices_t *pDevices,
1448 uint32_t *pSamplingRate,
1449 audio_format_t *pFormat,
1450 audio_channel_mask_t *pChannelMask,
1451 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001452 audio_output_flags_t flags,
1453 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001454{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001455 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001456 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001457 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1458 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1459 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001460 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001461 config.offload_info = *offloadInfo;
1462 }
1463
Eric Laurentbfb1b832013-01-07 09:53:42 -08001464 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001465 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001466 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001467 config.sample_rate,
1468 config.format,
1469 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001470 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001471 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001472 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473
1474 if (pDevices == NULL || *pDevices == 0) {
1475 return 0;
1476 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001477
Mathias Agopian65ab4712010-07-14 17:59:35 -07001478 Mutex::Autolock _l(mLock);
1479
Glenn Kasten32550952013-08-06 10:45:10 -07001480 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001481 if (outHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001482 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001483 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001484
John Grossmanee578c02012-07-23 17:05:46 -07001485 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001486 audio_io_handle_t id = nextUniqueId();
1487
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001488 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001489
Glenn Kasten32550952013-08-06 10:45:10 -07001490 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001491 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001492 id,
1493 *pDevices,
1494 (audio_output_flags_t)flags,
1495 &config,
1496 &outStream);
1497
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001498 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001499 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001500 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001501 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001502 config.sample_rate,
1503 config.format,
1504 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001505 status);
1506
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001507 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001508 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001509
Glenn Kasten32550952013-08-06 10:45:10 -07001510 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001511 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1512 thread = new OffloadThread(this, output, id, *pDevices);
1513 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1514 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001515 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1516 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001517 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001518 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519 } else {
1520 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001521 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522 }
1523 mPlaybackThreads.add(id, thread);
1524
Glenn Kasten7c027242012-12-26 14:43:16 -08001525 if (pSamplingRate != NULL) {
1526 *pSamplingRate = config.sample_rate;
1527 }
1528 if (pFormat != NULL) {
1529 *pFormat = config.format;
1530 }
1531 if (pChannelMask != NULL) {
1532 *pChannelMask = config.channel_mask;
1533 }
1534 if (pLatencyMs != NULL) {
1535 *pLatencyMs = thread->latency();
1536 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001537
1538 // notify client processes of the new output creation
1539 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001540
1541 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001542 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001543 ALOGI("Using module %d has the primary audio interface", module);
1544 mPrimaryHardwareDev = outHwDev;
1545
1546 AutoMutex lock(mHardwareLock);
1547 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001548 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001549 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001550 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551 return id;
1552 }
1553
1554 return 0;
1555}
1556
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001557audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1558 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001559{
1560 Mutex::Autolock _l(mLock);
1561 MixerThread *thread1 = checkMixerThread_l(output1);
1562 MixerThread *thread2 = checkMixerThread_l(output2);
1563
1564 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001565 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1566 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001567 return 0;
1568 }
1569
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001570 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001571 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1572 thread->addOutputTrack(thread2);
1573 mPlaybackThreads.add(id, thread);
1574 // notify client processes of the new output creation
1575 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1576 return id;
1577}
1578
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001579status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001580{
Glenn Kastend96c5722012-04-25 13:44:49 -07001581 return closeOutput_nonvirtual(output);
1582}
1583
1584status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1585{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001586 // keep strong reference on the playback thread so that
1587 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001588 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001589 {
1590 Mutex::Autolock _l(mLock);
1591 thread = checkPlaybackThread_l(output);
1592 if (thread == NULL) {
1593 return BAD_VALUE;
1594 }
1595
Steve Block3856b092011-10-20 11:56:00 +01001596 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001597
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001598 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001599 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001600 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001601 DuplicatingThread *dupThread =
1602 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001603 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001604
1605 }
1606 }
1607 }
1608
1609
1610 mPlaybackThreads.removeItem(output);
1611 // save all effects to the default thread
1612 if (mPlaybackThreads.size()) {
1613 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1614 if (dstThread != NULL) {
1615 // audioflinger lock is held here so the acquisition order of thread locks does not
1616 // matter
1617 Mutex::Autolock _dl(dstThread->mLock);
1618 Mutex::Autolock _sl(thread->mLock);
1619 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1620 for (size_t i = 0; i < effectChains.size(); i ++) {
1621 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001622 }
1623 }
1624 }
Glenn Kastena1117922012-01-26 10:53:32 -08001625 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001626 }
1627 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001628 // The thread entity (active unit of execution) is no longer running here,
1629 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001630
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001631 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001632 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001633 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001634 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001635 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001636 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001637 }
1638 return NO_ERROR;
1639}
1640
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001641status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001642{
1643 Mutex::Autolock _l(mLock);
1644 PlaybackThread *thread = checkPlaybackThread_l(output);
1645
1646 if (thread == NULL) {
1647 return BAD_VALUE;
1648 }
1649
Steve Block3856b092011-10-20 11:56:00 +01001650 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001651 thread->suspend();
1652
1653 return NO_ERROR;
1654}
1655
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001656status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001657{
1658 Mutex::Autolock _l(mLock);
1659 PlaybackThread *thread = checkPlaybackThread_l(output);
1660
1661 if (thread == NULL) {
1662 return BAD_VALUE;
1663 }
1664
Steve Block3856b092011-10-20 11:56:00 +01001665 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001666
1667 thread->restore();
1668
1669 return NO_ERROR;
1670}
1671
Eric Laurenta4c5a552012-03-29 10:12:40 -07001672audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1673 audio_devices_t *pDevices,
1674 uint32_t *pSamplingRate,
1675 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001676 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001677{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001678 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001679 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001680 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1681 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1682 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1683
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001684 uint32_t reqSamplingRate = config.sample_rate;
1685 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001686 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001687
1688 if (pDevices == NULL || *pDevices == 0) {
1689 return 0;
1690 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001691
Mathias Agopian65ab4712010-07-14 17:59:35 -07001692 Mutex::Autolock _l(mLock);
1693
Glenn Kasten32550952013-08-06 10:45:10 -07001694 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001695 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001696 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001697 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001698
John Grossmanee578c02012-07-23 17:05:46 -07001699 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001700 audio_io_handle_t id = nextUniqueId();
1701
Glenn Kasten32550952013-08-06 10:45:10 -07001702 audio_stream_in_t *inStream = NULL;
1703 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001704 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001705 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1706 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001707 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001708 config.sample_rate,
1709 config.format,
1710 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001711 status);
1712
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001713 // If the input could not be opened with the requested parameters and we can handle the
1714 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1715 // 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 -07001716 if (status == BAD_VALUE &&
1717 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1718 (config.sample_rate <= 2 * reqSamplingRate) &&
Glenn Kastenf506e942013-08-06 10:49:43 -07001719 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001720 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001721 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001722 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001723 }
1724
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001725 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001726
Glenn Kasten46909e72013-02-26 09:20:22 -08001727#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001728 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1729 // or (re-)create if current Pipe is idle and does not match the new format
1730 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001731 enum {
1732 TEE_SINK_NO, // don't copy input
1733 TEE_SINK_NEW, // copy input using a new pipe
1734 TEE_SINK_OLD, // copy input using an existing pipe
1735 } kind;
1736 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1737 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001738 if (!mTeeSinkInputEnabled) {
1739 kind = TEE_SINK_NO;
1740 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001741 kind = TEE_SINK_NO;
1742 } else if (mRecordTeeSink == 0) {
1743 kind = TEE_SINK_NEW;
1744 } else if (mRecordTeeSink->getStrongCount() != 1) {
1745 kind = TEE_SINK_NO;
1746 } else if (format == mRecordTeeSink->format()) {
1747 kind = TEE_SINK_OLD;
1748 } else {
1749 kind = TEE_SINK_NEW;
1750 }
1751 switch (kind) {
1752 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001753 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001754 size_t numCounterOffers = 0;
1755 const NBAIO_Format offers[1] = {format};
1756 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1757 ALOG_ASSERT(index == 0);
1758 PipeReader *pipeReader = new PipeReader(*pipe);
1759 numCounterOffers = 0;
1760 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1761 ALOG_ASSERT(index == 0);
1762 mRecordTeeSink = pipe;
1763 mRecordTeeSource = pipeReader;
1764 teeSink = pipe;
1765 }
1766 break;
1767 case TEE_SINK_OLD:
1768 teeSink = mRecordTeeSink;
1769 break;
1770 case TEE_SINK_NO:
1771 default:
1772 break;
1773 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001774#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001775
Dima Zavin799a70e2011-04-18 16:57:27 -07001776 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1777
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001778 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001779 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001780 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001781 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001782 input,
1783 reqSamplingRate,
Glenn Kastenf506e942013-08-06 10:49:43 -07001784 reqChannelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001785 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001786 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001787 *pDevices
1788#ifdef TEE_SINK
1789 , teeSink
1790#endif
1791 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001792 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001793 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001794 if (pSamplingRate != NULL) {
1795 *pSamplingRate = reqSamplingRate;
1796 }
1797 if (pFormat != NULL) {
1798 *pFormat = config.format;
1799 }
1800 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001801 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001802 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001803
Mathias Agopian65ab4712010-07-14 17:59:35 -07001804 // notify client processes of the new input creation
1805 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1806 return id;
1807 }
1808
1809 return 0;
1810}
1811
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001812status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001813{
Glenn Kastend96c5722012-04-25 13:44:49 -07001814 return closeInput_nonvirtual(input);
1815}
1816
1817status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1818{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001819 // keep strong reference on the record thread so that
1820 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001821 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001822 {
1823 Mutex::Autolock _l(mLock);
1824 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001825 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001826 return BAD_VALUE;
1827 }
1828
Steve Block3856b092011-10-20 11:56:00 +01001829 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001830 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001831 mRecordThreads.removeItem(input);
1832 }
1833 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001834 // The thread entity (active unit of execution) is no longer running here,
1835 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001836
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001837 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001838 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001839 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001840 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001841 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001842
1843 return NO_ERROR;
1844}
1845
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001846status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001847{
1848 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001849 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001850
1851 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1852 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001853 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001854 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001855
1856 return NO_ERROR;
1857}
1858
1859
1860int AudioFlinger::newAudioSessionId()
1861{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001862 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001863}
1864
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001865void AudioFlinger::acquireAudioSessionId(int audioSession)
1866{
1867 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001868 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001869 ALOGV("acquiring %d from %d", audioSession, caller);
Eric Laurentd1b28d42013-09-18 18:47:13 -07001870
1871 // Ignore requests received from processes not known as notification client. The request
1872 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1873 // called from a different pid leaving a stale session reference. Also we don't know how
1874 // to clear this reference if the client process dies.
1875 if (mNotificationClients.indexOfKey(caller) < 0) {
1876 ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
1877 return;
1878 }
1879
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001880 size_t num = mAudioSessionRefs.size();
1881 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001882 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001883 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1884 ref->mCnt++;
1885 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001886 return;
1887 }
1888 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001889 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1890 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001891}
1892
1893void AudioFlinger::releaseAudioSessionId(int audioSession)
1894{
1895 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001896 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001897 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001898 size_t num = mAudioSessionRefs.size();
1899 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001900 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001901 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1902 ref->mCnt--;
1903 ALOGV(" decremented refcount to %d", ref->mCnt);
1904 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001905 mAudioSessionRefs.removeAt(i);
1906 delete ref;
1907 purgeStaleEffects_l();
1908 }
1909 return;
1910 }
1911 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001912 // If the caller is mediaserver it is likely that the session being released was acquired
1913 // on behalf of a process not in notification clients and we ignore the warning.
1914 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001915}
1916
1917void AudioFlinger::purgeStaleEffects_l() {
1918
Steve Block3856b092011-10-20 11:56:00 +01001919 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001920
1921 Vector< sp<EffectChain> > chains;
1922
1923 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1924 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1925 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1926 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001927 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1928 chains.push(ec);
1929 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001930 }
1931 }
1932 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1933 sp<RecordThread> t = mRecordThreads.valueAt(i);
1934 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1935 sp<EffectChain> ec = t->mEffectChains[j];
1936 chains.push(ec);
1937 }
1938 }
1939
1940 for (size_t i = 0; i < chains.size(); i++) {
1941 sp<EffectChain> ec = chains[i];
1942 int sessionid = ec->sessionId();
1943 sp<ThreadBase> t = ec->mThread.promote();
1944 if (t == 0) {
1945 continue;
1946 }
1947 size_t numsessionrefs = mAudioSessionRefs.size();
1948 bool found = false;
1949 for (size_t k = 0; k < numsessionrefs; k++) {
1950 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001951 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001952 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001953 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001954 found = true;
1955 break;
1956 }
1957 }
1958 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07001959 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001960 // remove all effects from the chain
1961 while (ec->mEffects.size()) {
1962 sp<EffectModule> effect = ec->mEffects[0];
1963 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001964 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001965 if (effect->purgeHandles()) {
1966 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001967 }
1968 AudioSystem::unregisterEffect(effect->id());
1969 }
1970 }
1971 }
1972 return;
1973}
1974
Mathias Agopian65ab4712010-07-14 17:59:35 -07001975// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001976AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001977{
Glenn Kastena1117922012-01-26 10:53:32 -08001978 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979}
1980
1981// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001982AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001983{
1984 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001985 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001986}
1987
1988// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001989AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001990{
Glenn Kastena1117922012-01-26 10:53:32 -08001991 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001992}
1993
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001994uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001995{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001996 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001997}
1998
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001999AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002000{
2001 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2002 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002003 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002004 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002005 return thread;
2006 }
2007 }
2008 return NULL;
2009}
2010
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002011audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002012{
2013 PlaybackThread *thread = primaryPlaybackThread_l();
2014
2015 if (thread == NULL) {
2016 return 0;
2017 }
2018
Eric Laurentf1c04f92012-08-28 14:26:53 -07002019 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002020}
2021
Eric Laurenta011e352012-03-29 15:51:43 -07002022sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2023 int triggerSession,
2024 int listenerSession,
2025 sync_event_callback_t callBack,
2026 void *cookie)
2027{
2028 Mutex::Autolock _l(mLock);
2029
2030 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2031 status_t playStatus = NAME_NOT_FOUND;
2032 status_t recStatus = NAME_NOT_FOUND;
2033 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2034 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2035 if (playStatus == NO_ERROR) {
2036 return event;
2037 }
2038 }
2039 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2040 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2041 if (recStatus == NO_ERROR) {
2042 return event;
2043 }
2044 }
2045 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2046 mPendingSyncEvents.add(event);
2047 } else {
2048 ALOGV("createSyncEvent() invalid event %d", event->type());
2049 event.clear();
2050 }
2051 return event;
2052}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002053
Mathias Agopian65ab4712010-07-14 17:59:35 -07002054// ----------------------------------------------------------------------------
2055// Effect management
2056// ----------------------------------------------------------------------------
2057
2058
Glenn Kastenf587ba52012-01-26 16:25:10 -08002059status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002060{
2061 Mutex::Autolock _l(mLock);
2062 return EffectQueryNumberEffects(numEffects);
2063}
2064
Glenn Kastenf587ba52012-01-26 16:25:10 -08002065status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002066{
2067 Mutex::Autolock _l(mLock);
2068 return EffectQueryEffect(index, descriptor);
2069}
2070
Glenn Kasten5e92a782012-01-30 07:40:52 -08002071status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002072 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002073{
2074 Mutex::Autolock _l(mLock);
2075 return EffectGetDescriptor(pUuid, descriptor);
2076}
2077
2078
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002079sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002080 effect_descriptor_t *pDesc,
2081 const sp<IEffectClient>& effectClient,
2082 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002083 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002084 int sessionId,
2085 status_t *status,
2086 int *id,
2087 int *enabled)
2088{
2089 status_t lStatus = NO_ERROR;
2090 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002091 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002092
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002093 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002094 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002095 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002096
2097 if (pDesc == NULL) {
2098 lStatus = BAD_VALUE;
2099 goto Exit;
2100 }
2101
Eric Laurent84e9a102010-09-23 16:10:16 -07002102 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002103 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002104 lStatus = PERMISSION_DENIED;
2105 goto Exit;
2106 }
2107
Dima Zavinfce7a472011-04-19 22:30:36 -07002108 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002109 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002110 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002111 lStatus = PERMISSION_DENIED;
2112 goto Exit;
2113 }
2114
Mathias Agopian65ab4712010-07-14 17:59:35 -07002115 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002116 if (!EffectIsNullUuid(&pDesc->uuid)) {
2117 // if uuid is specified, request effect descriptor
2118 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2119 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002120 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002121 goto Exit;
2122 }
2123 } else {
2124 // if uuid is not specified, look for an available implementation
2125 // of the required type in effect factory
2126 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002127 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002128 lStatus = BAD_VALUE;
2129 goto Exit;
2130 }
2131 uint32_t numEffects = 0;
2132 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002133 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002134 bool found = false;
2135
2136 lStatus = EffectQueryNumberEffects(&numEffects);
2137 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002138 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002139 goto Exit;
2140 }
2141 for (uint32_t i = 0; i < numEffects; i++) {
2142 lStatus = EffectQueryEffect(i, &desc);
2143 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002144 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002145 continue;
2146 }
2147 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2148 // If matching type found save effect descriptor. If the session is
2149 // 0 and the effect is not auxiliary, continue enumeration in case
2150 // an auxiliary version of this effect type is available
2151 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002152 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002153 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002154 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2155 break;
2156 }
2157 }
2158 }
2159 if (!found) {
2160 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002161 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002162 goto Exit;
2163 }
2164 // For same effect type, chose auxiliary version over insert version if
2165 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002166 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002167 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002168 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002169 }
2170 }
2171
2172 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002173 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002174 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2175 lStatus = INVALID_OPERATION;
2176 goto Exit;
2177 }
2178
Eric Laurent59255e42011-07-27 19:49:51 -07002179 // check recording permission for visualizer
2180 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2181 !recordingAllowed()) {
2182 lStatus = PERMISSION_DENIED;
2183 goto Exit;
2184 }
2185
Mathias Agopian65ab4712010-07-14 17:59:35 -07002186 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002187 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002188 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2189 // if the output returned by getOutputForEffect() is removed before we lock the
2190 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2191 // and we will exit safely
2192 io = AudioSystem::getOutputForEffect(&desc);
2193 ALOGV("createEffect got output %d", io);
2194 }
2195
2196 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002197
2198 // If output is not specified try to find a matching audio session ID in one of the
2199 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002200 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2201 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002202 // Note: io is never 0 when creating an effect on an input
2203 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002204 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2205 // output must be specified by AudioPolicyManager when using session
2206 // AUDIO_SESSION_OUTPUT_STAGE
2207 lStatus = BAD_VALUE;
2208 goto Exit;
2209 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002210 // look for the thread where the specified audio session is present
2211 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2212 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2213 io = mPlaybackThreads.keyAt(i);
2214 break;
2215 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002216 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002217 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002218 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2219 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2220 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002221 break;
2222 }
2223 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002224 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002225 // If no output thread contains the requested session ID, default to
2226 // first output. The effect chain will be moved to the correct output
2227 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002228 if (io == 0 && mPlaybackThreads.size()) {
2229 io = mPlaybackThreads.keyAt(0);
2230 }
Steve Block3856b092011-10-20 11:56:00 +01002231 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002232 }
2233 ThreadBase *thread = checkRecordThread_l(io);
2234 if (thread == NULL) {
2235 thread = checkPlaybackThread_l(io);
2236 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002237 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002238 lStatus = BAD_VALUE;
2239 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002240 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002241 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002242
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002243 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002244
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002245 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002246 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2247 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002248 if (handle != 0 && id != NULL) {
2249 *id = handle->id();
2250 }
2251 }
2252
2253Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002254 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002255 return handle;
2256}
2257
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002258status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2259 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002260{
Steve Block3856b092011-10-20 11:56:00 +01002261 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002262 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002263 Mutex::Autolock _l(mLock);
2264 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002265 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002266 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002267 }
Eric Laurentde070132010-07-13 04:45:46 -07002268 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2269 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002270 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002271 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002272 }
Eric Laurentde070132010-07-13 04:45:46 -07002273 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2274 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002275 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002276 return BAD_VALUE;
2277 }
2278
2279 Mutex::Autolock _dl(dstThread->mLock);
2280 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002281 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002282}
2283
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002284// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002285status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002286 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002287 AudioFlinger::PlaybackThread *dstThread,
2288 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002289{
Steve Block3856b092011-10-20 11:56:00 +01002290 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002291 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002292
Eric Laurent59255e42011-07-27 19:49:51 -07002293 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002294 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002295 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002296 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002297 return INVALID_OPERATION;
2298 }
2299
Eric Laurent39e94f82010-07-28 01:32:47 -07002300 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002301 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002302 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002303 // removed.
2304 srcThread->removeEffectChain_l(chain);
2305
2306 // transfer all effects one by one so that new effect chain is created on new thread with
2307 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002308 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002309 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002310 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002311 Vector< sp<EffectModule> > removed;
2312 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002313 while (effect != 0) {
2314 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002315 removed.add(effect);
2316 status = dstThread->addEffect_l(effect);
2317 if (status != NO_ERROR) {
2318 break;
2319 }
Eric Laurentec35a142011-10-05 17:42:25 -07002320 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2321 if (effect->state() == EffectModule::ACTIVE ||
2322 effect->state() == EffectModule::STOPPING) {
2323 effect->start();
2324 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002325 // if the move request is not received from audio policy manager, the effect must be
2326 // re-registered with the new strategy and output
2327 if (dstChain == 0) {
2328 dstChain = effect->chain().promote();
2329 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002330 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002331 status = NO_INIT;
2332 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002333 }
2334 strategy = dstChain->strategy();
2335 }
2336 if (reRegister) {
2337 AudioSystem::unregisterEffect(effect->id());
2338 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002339 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002340 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002341 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002342 effect->id());
2343 }
Eric Laurentde070132010-07-13 04:45:46 -07002344 effect = chain->getEffectFromId_l(0);
2345 }
2346
Eric Laurent5baf2af2013-09-12 17:37:00 -07002347 if (status != NO_ERROR) {
2348 for (size_t i = 0; i < removed.size(); i++) {
2349 srcThread->addEffect_l(removed[i]);
2350 if (dstChain != 0 && reRegister) {
2351 AudioSystem::unregisterEffect(removed[i]->id());
2352 AudioSystem::registerEffect(&removed[i]->desc(),
2353 srcThread->id(),
2354 strategy,
2355 sessionId,
2356 removed[i]->id());
2357 }
2358 }
2359 }
2360
2361 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002362}
2363
Eric Laurent5baf2af2013-09-12 17:37:00 -07002364bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002365{
2366 if (mGlobalEffectEnableTime != 0 &&
2367 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2368 return true;
2369 }
2370
2371 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2372 sp<EffectChain> ec =
2373 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002374 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002375 return true;
2376 }
2377 }
2378 return false;
2379}
2380
Eric Laurent5baf2af2013-09-12 17:37:00 -07002381void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002382{
2383 Mutex::Autolock _l(mLock);
2384
2385 mGlobalEffectEnableTime = systemTime();
2386
2387 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2388 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2389 if (t->mType == ThreadBase::OFFLOAD) {
2390 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2391 }
2392 }
2393
2394}
2395
Glenn Kastenda6ef132013-01-10 12:31:01 -08002396struct Entry {
2397#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2398 char mName[MAX_NAME];
2399};
2400
2401int comparEntry(const void *p1, const void *p2)
2402{
2403 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2404}
2405
Glenn Kasten46909e72013-02-26 09:20:22 -08002406#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002407void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002408{
Eric Laurent81784c32012-11-19 14:55:58 -08002409 NBAIO_Source *teeSource = source.get();
2410 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002411 // .wav rotation
2412 // There is a benign race condition if 2 threads call this simultaneously.
2413 // They would both traverse the directory, but the result would simply be
2414 // failures at unlink() which are ignored. It's also unlikely since
2415 // normally dumpsys is only done by bugreport or from the command line.
2416 char teePath[32+256];
2417 strcpy(teePath, "/data/misc/media");
2418 size_t teePathLen = strlen(teePath);
2419 DIR *dir = opendir(teePath);
2420 teePath[teePathLen++] = '/';
2421 if (dir != NULL) {
2422#define MAX_SORT 20 // number of entries to sort
2423#define MAX_KEEP 10 // number of entries to keep
2424 struct Entry entries[MAX_SORT];
2425 size_t entryCount = 0;
2426 while (entryCount < MAX_SORT) {
2427 struct dirent de;
2428 struct dirent *result = NULL;
2429 int rc = readdir_r(dir, &de, &result);
2430 if (rc != 0) {
2431 ALOGW("readdir_r failed %d", rc);
2432 break;
2433 }
2434 if (result == NULL) {
2435 break;
2436 }
2437 if (result != &de) {
2438 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2439 break;
2440 }
2441 // ignore non .wav file entries
2442 size_t nameLen = strlen(de.d_name);
2443 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2444 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2445 continue;
2446 }
2447 strcpy(entries[entryCount++].mName, de.d_name);
2448 }
2449 (void) closedir(dir);
2450 if (entryCount > MAX_KEEP) {
2451 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2452 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2453 strcpy(&teePath[teePathLen], entries[i].mName);
2454 (void) unlink(teePath);
2455 }
2456 }
2457 } else {
2458 if (fd >= 0) {
2459 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2460 }
2461 }
Eric Laurent81784c32012-11-19 14:55:58 -08002462 char teeTime[16];
2463 struct timeval tv;
2464 gettimeofday(&tv, NULL);
2465 struct tm tm;
2466 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002467 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2468 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2469 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2470 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002471 if (teeFd >= 0) {
2472 char wavHeader[44];
2473 memcpy(wavHeader,
2474 "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",
2475 sizeof(wavHeader));
2476 NBAIO_Format format = teeSource->format();
2477 unsigned channelCount = Format_channelCount(format);
2478 ALOG_ASSERT(channelCount <= FCC_2);
2479 uint32_t sampleRate = Format_sampleRate(format);
2480 wavHeader[22] = channelCount; // number of channels
2481 wavHeader[24] = sampleRate; // sample rate
2482 wavHeader[25] = sampleRate >> 8;
2483 wavHeader[32] = channelCount * 2; // block alignment
2484 write(teeFd, wavHeader, sizeof(wavHeader));
2485 size_t total = 0;
2486 bool firstRead = true;
2487 for (;;) {
2488#define TEE_SINK_READ 1024
2489 short buffer[TEE_SINK_READ * FCC_2];
2490 size_t count = TEE_SINK_READ;
2491 ssize_t actual = teeSource->read(buffer, count,
2492 AudioBufferProvider::kInvalidPTS);
2493 bool wasFirstRead = firstRead;
2494 firstRead = false;
2495 if (actual <= 0) {
2496 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2497 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002498 }
Eric Laurent81784c32012-11-19 14:55:58 -08002499 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002500 }
Eric Laurent81784c32012-11-19 14:55:58 -08002501 ALOG_ASSERT(actual <= (ssize_t)count);
2502 write(teeFd, buffer, actual * channelCount * sizeof(short));
2503 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002504 }
Eric Laurent81784c32012-11-19 14:55:58 -08002505 lseek(teeFd, (off_t) 4, SEEK_SET);
2506 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2507 write(teeFd, &temp, sizeof(temp));
2508 lseek(teeFd, (off_t) 40, SEEK_SET);
2509 temp = total * channelCount * sizeof(short);
2510 write(teeFd, &temp, sizeof(temp));
2511 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002512 if (fd >= 0) {
2513 fdprintf(fd, "tee copied to %s\n", teePath);
2514 }
Eric Laurent59255e42011-07-27 19:49:51 -07002515 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002516 if (fd >= 0) {
2517 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2518 }
Eric Laurent59255e42011-07-27 19:49:51 -07002519 }
2520 }
2521}
Glenn Kasten46909e72013-02-26 09:20:22 -08002522#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002523
Mathias Agopian65ab4712010-07-14 17:59:35 -07002524// ----------------------------------------------------------------------------
2525
2526status_t AudioFlinger::onTransact(
2527 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2528{
2529 return BnAudioFlinger::onTransact(code, data, reply, flags);
2530}
2531
Mathias Agopian65ab4712010-07-14 17:59:35 -07002532}; // namespace android