blob: 5cf6ef3cdab3ac335d65a4bf67f173742adbc3dc [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
Glenn Kasten663c2242013-09-24 11:52:37 -0700479 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
480 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
481 lStatus = BAD_VALUE;
482 goto Exit;
483 }
484
Mathias Agopian65ab4712010-07-14 17:59:35 -0700485 {
486 Mutex::Autolock _l(mLock);
487 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700488 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700489 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800490 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491 lStatus = BAD_VALUE;
492 goto Exit;
493 }
494
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800495 pid_t pid = IPCThreadState::self()->getCallingPid();
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800496
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800497 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700498
Steve Block3856b092011-10-20 11:56:00 +0100499 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700500 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700501 // check if an effect chain with the same session ID is present on another
502 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700503 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700504 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
505 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700506 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700507 if (sessions & PlaybackThread::EFFECT_SESSION) {
508 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700509 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700510 }
Eric Laurentde070132010-07-13 04:45:46 -0700511 }
512 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700513 lSessionId = *sessionId;
514 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700515 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700516 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700517 if (sessionId != NULL) {
518 *sessionId = lSessionId;
519 }
520 }
Steve Block3856b092011-10-20 11:56:00 +0100521 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700522
523 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800524 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Glenn Kasten2fc14732013-08-05 14:58:14 -0700525 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700526
527 // move effect chain to this output thread if an effect on same session was waiting
528 // for a track to be created
529 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700530 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700531 Mutex::Autolock _dl(thread->mLock);
532 Mutex::Autolock _sl(effectThread->mLock);
533 moveEffectChain_l(lSessionId, effectThread, thread, true);
534 }
Eric Laurenta011e352012-03-29 15:51:43 -0700535
536 // Look for sync events awaiting for a session to be used.
537 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
538 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
539 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700540 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700541 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700542 } else {
543 mPendingSyncEvents[i]->cancel();
544 }
Eric Laurenta011e352012-03-29 15:51:43 -0700545 mPendingSyncEvents.removeAt(i);
546 i--;
547 }
548 }
549 }
Glenn Kastene198c362013-08-13 09:13:36 -0700550
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551 }
Glenn Kastene198c362013-08-13 09:13:36 -0700552
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700554 // s for server's pid, n for normal mixer name, f for fast index
555 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
556 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557 trackHandle = new TrackHandle(track);
558 } else {
559 // remove local strong reference to Client before deleting the Track so that the Client
560 // destructor is called by the TrackBase destructor with mLock held
561 client.clear();
562 track.clear();
563 }
564
565Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700566 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567 return trackHandle;
568}
569
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800570uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571{
572 Mutex::Autolock _l(mLock);
573 PlaybackThread *thread = checkPlaybackThread_l(output);
574 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000575 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700576 return 0;
577 }
578 return thread->sampleRate();
579}
580
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800581int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582{
583 Mutex::Autolock _l(mLock);
584 PlaybackThread *thread = checkPlaybackThread_l(output);
585 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000586 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 return 0;
588 }
589 return thread->channelCount();
590}
591
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800592audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593{
594 Mutex::Autolock _l(mLock);
595 PlaybackThread *thread = checkPlaybackThread_l(output);
596 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000597 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800598 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599 }
600 return thread->format();
601}
602
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800603size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604{
605 Mutex::Autolock _l(mLock);
606 PlaybackThread *thread = checkPlaybackThread_l(output);
607 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000608 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609 return 0;
610 }
Glenn Kasten58912562012-04-03 10:45:00 -0700611 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
612 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700613 return thread->frameCount();
614}
615
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800616uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700617{
618 Mutex::Autolock _l(mLock);
619 PlaybackThread *thread = checkPlaybackThread_l(output);
620 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800621 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622 return 0;
623 }
624 return thread->latency();
625}
626
627status_t AudioFlinger::setMasterVolume(float value)
628{
Eric Laurenta1884f92011-08-23 08:25:03 -0700629 status_t ret = initCheck();
630 if (ret != NO_ERROR) {
631 return ret;
632 }
633
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634 // check calling permissions
635 if (!settingsAllowed()) {
636 return PERMISSION_DENIED;
637 }
638
Eric Laurenta4c5a552012-03-29 10:12:40 -0700639 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700640 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700641
John Grossmanee578c02012-07-23 17:05:46 -0700642 // Set master volume in the HALs which support it.
643 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
644 AutoMutex lock(mHardwareLock);
645 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800646
John Grossmanee578c02012-07-23 17:05:46 -0700647 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
648 if (dev->canSetMasterVolume()) {
649 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800650 }
John Grossmanee578c02012-07-23 17:05:46 -0700651 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653
John Grossmanee578c02012-07-23 17:05:46 -0700654 // Now set the master volume in each playback thread. Playback threads
655 // assigned to HALs which do not have master volume support will apply
656 // master volume during the mix operation. Threads with HALs which do
657 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800658 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700659 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660
661 return NO_ERROR;
662}
663
Glenn Kastenf78aee72012-01-04 11:00:47 -0800664status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665{
Eric Laurenta1884f92011-08-23 08:25:03 -0700666 status_t ret = initCheck();
667 if (ret != NO_ERROR) {
668 return ret;
669 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670
671 // check calling permissions
672 if (!settingsAllowed()) {
673 return PERMISSION_DENIED;
674 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800675 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000676 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 return BAD_VALUE;
678 }
679
680 { // scope for the lock
681 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700682 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700684 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 mHardwareStatus = AUDIO_HW_IDLE;
686 }
687
688 if (NO_ERROR == ret) {
689 Mutex::Autolock _l(mLock);
690 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800691 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700692 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700693 }
694
695 return ret;
696}
697
698status_t AudioFlinger::setMicMute(bool state)
699{
Eric Laurenta1884f92011-08-23 08:25:03 -0700700 status_t ret = initCheck();
701 if (ret != NO_ERROR) {
702 return ret;
703 }
704
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705 // check calling permissions
706 if (!settingsAllowed()) {
707 return PERMISSION_DENIED;
708 }
709
710 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700711 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700713 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 mHardwareStatus = AUDIO_HW_IDLE;
715 return ret;
716}
717
718bool AudioFlinger::getMicMute() const
719{
Eric Laurenta1884f92011-08-23 08:25:03 -0700720 status_t ret = initCheck();
721 if (ret != NO_ERROR) {
722 return false;
723 }
724
Dima Zavinfce7a472011-04-19 22:30:36 -0700725 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800726 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700727 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700729 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700730 mHardwareStatus = AUDIO_HW_IDLE;
731 return state;
732}
733
734status_t AudioFlinger::setMasterMute(bool muted)
735{
John Grossmand8f178d2012-07-20 14:51:35 -0700736 status_t ret = initCheck();
737 if (ret != NO_ERROR) {
738 return ret;
739 }
740
Mathias Agopian65ab4712010-07-14 17:59:35 -0700741 // check calling permissions
742 if (!settingsAllowed()) {
743 return PERMISSION_DENIED;
744 }
745
John Grossmanee578c02012-07-23 17:05:46 -0700746 Mutex::Autolock _l(mLock);
747 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700748
John Grossmanee578c02012-07-23 17:05:46 -0700749 // Set master mute in the HALs which support it.
750 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
751 AutoMutex lock(mHardwareLock);
752 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700753
John Grossmanee578c02012-07-23 17:05:46 -0700754 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
755 if (dev->canSetMasterMute()) {
756 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700757 }
John Grossmanee578c02012-07-23 17:05:46 -0700758 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700759 }
760
John Grossmanee578c02012-07-23 17:05:46 -0700761 // Now set the master mute in each playback thread. Playback threads
762 // assigned to HALs which do not have master mute support will apply master
763 // mute during the mix operation. Threads with HALs which do support master
764 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800765 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700766 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767
768 return NO_ERROR;
769}
770
771float AudioFlinger::masterVolume() const
772{
Glenn Kasten98067102011-12-13 11:47:54 -0800773 Mutex::Autolock _l(mLock);
774 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775}
776
777bool AudioFlinger::masterMute() const
778{
Glenn Kasten98067102011-12-13 11:47:54 -0800779 Mutex::Autolock _l(mLock);
780 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700781}
782
John Grossman4ff14ba2012-02-08 16:37:41 -0800783float AudioFlinger::masterVolume_l() const
784{
John Grossman4ff14ba2012-02-08 16:37:41 -0800785 return mMasterVolume;
786}
787
John Grossmand8f178d2012-07-20 14:51:35 -0700788bool AudioFlinger::masterMute_l() const
789{
John Grossmanee578c02012-07-23 17:05:46 -0700790 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700791}
792
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800793status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
794 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795{
796 // check calling permissions
797 if (!settingsAllowed()) {
798 return PERMISSION_DENIED;
799 }
800
Glenn Kasten263709e2012-01-06 08:40:01 -0800801 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000802 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700803 return BAD_VALUE;
804 }
805
806 AutoMutex lock(mLock);
807 PlaybackThread *thread = NULL;
808 if (output) {
809 thread = checkPlaybackThread_l(output);
810 if (thread == NULL) {
811 return BAD_VALUE;
812 }
813 }
814
815 mStreamTypes[stream].volume = value;
816
817 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800818 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700819 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 }
821 } else {
822 thread->setStreamVolume(stream, value);
823 }
824
825 return NO_ERROR;
826}
827
Glenn Kastenfff6d712012-01-12 16:38:12 -0800828status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700829{
830 // check calling permissions
831 if (!settingsAllowed()) {
832 return PERMISSION_DENIED;
833 }
834
Glenn Kasten263709e2012-01-06 08:40:01 -0800835 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700836 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000837 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838 return BAD_VALUE;
839 }
840
Eric Laurent93575202011-01-18 18:39:02 -0800841 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700842 mStreamTypes[stream].mute = muted;
843 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700844 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845
846 return NO_ERROR;
847}
848
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800849float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850{
Glenn Kasten263709e2012-01-06 08:40:01 -0800851 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852 return 0.0f;
853 }
854
855 AutoMutex lock(mLock);
856 float volume;
857 if (output) {
858 PlaybackThread *thread = checkPlaybackThread_l(output);
859 if (thread == NULL) {
860 return 0.0f;
861 }
862 volume = thread->streamVolume(stream);
863 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800864 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865 }
866
867 return volume;
868}
869
Glenn Kastenfff6d712012-01-12 16:38:12 -0800870bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871{
Glenn Kasten263709e2012-01-06 08:40:01 -0800872 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700873 return true;
874 }
875
Glenn Kasten6637baa2012-01-09 09:40:36 -0800876 AutoMutex lock(mLock);
877 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878}
879
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800880status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700881{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700882 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
883 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800884
Mathias Agopian65ab4712010-07-14 17:59:35 -0700885 // check calling permissions
886 if (!settingsAllowed()) {
887 return PERMISSION_DENIED;
888 }
889
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890 // ioHandle == 0 means the parameters are global to the audio hardware interface
891 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700892 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700893 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800894 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700895 AutoMutex lock(mHardwareLock);
896 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
897 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
898 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
899 status_t result = dev->set_parameters(dev, keyValuePairs.string());
900 final_result = result ?: final_result;
901 }
902 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800903 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700904 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
905 AudioParameter param = AudioParameter(keyValuePairs);
906 String8 value;
907 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700908 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
909 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700910 for (size_t i = 0; i < mRecordThreads.size(); i++) {
911 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700912 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700913 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
914 // collect all of the thread's session IDs
915 KeyedVector<int, bool> ids = thread->sessionIds();
916 // suspend effects associated with those session IDs
917 for (size_t j = 0; j < ids.size(); ++j) {
918 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700919 thread->setEffectSuspended(FX_IID_AEC,
920 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700921 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700922 thread->setEffectSuspended(FX_IID_NS,
923 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700924 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700925 }
926 }
Eric Laurentbee53372011-08-29 12:42:48 -0700927 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700928 }
929 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700930 String8 screenState;
931 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
932 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800933 if (isOff != (AudioFlinger::mScreenState & 1)) {
934 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700935 }
936 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700937 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938 }
939
940 // hold a strong ref on thread in case closeOutput() or closeInput() is called
941 // and the thread is exited once the lock is released
942 sp<ThreadBase> thread;
943 {
944 Mutex::Autolock _l(mLock);
945 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700946 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800948 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700949 // indicate output device change to all input threads for pre processing
950 AudioParameter param = AudioParameter(keyValuePairs);
951 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700952 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
953 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700954 for (size_t i = 0; i < mRecordThreads.size(); i++) {
955 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
956 }
957 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 }
959 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800960 if (thread != 0) {
961 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700962 }
963 return BAD_VALUE;
964}
965
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800966String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700968 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
969 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970
Eric Laurenta4c5a552012-03-29 10:12:40 -0700971 Mutex::Autolock _l(mLock);
972
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700974 String8 out_s8;
975
Dima Zavin799a70e2011-04-18 16:57:27 -0700976 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800977 char *s;
978 {
979 AutoMutex lock(mHardwareLock);
980 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700981 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800982 s = dev->get_parameters(dev, keys.string());
983 mHardwareStatus = AUDIO_HW_IDLE;
984 }
John Grossmanef7740b2012-02-09 11:28:36 -0800985 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700986 free(s);
987 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700988 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700989 }
990
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
992 if (playbackThread != NULL) {
993 return playbackThread->getParameters(keys);
994 }
995 RecordThread *recordThread = checkRecordThread_l(ioHandle);
996 if (recordThread != NULL) {
997 return recordThread->getParameters(keys);
998 }
999 return String8("");
1000}
1001
Glenn Kastendd8104c2012-07-02 12:42:44 -07001002size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1003 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004{
Eric Laurenta1884f92011-08-23 08:25:03 -07001005 status_t ret = initCheck();
1006 if (ret != NO_ERROR) {
1007 return 0;
1008 }
1009
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001010 AutoMutex lock(mHardwareLock);
1011 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001012 struct audio_config config;
1013 memset(&config, 0, sizeof(config));
1014 config.sample_rate = sampleRate;
1015 config.channel_mask = channelMask;
1016 config.format = format;
1017
John Grossmanee578c02012-07-23 17:05:46 -07001018 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1019 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001020 mHardwareStatus = AUDIO_HW_IDLE;
1021 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022}
1023
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001024unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001025{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 Mutex::Autolock _l(mLock);
1027
1028 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1029 if (recordThread != NULL) {
1030 return recordThread->getInputFramesLost();
1031 }
1032 return 0;
1033}
1034
1035status_t AudioFlinger::setVoiceVolume(float value)
1036{
Eric Laurenta1884f92011-08-23 08:25:03 -07001037 status_t ret = initCheck();
1038 if (ret != NO_ERROR) {
1039 return ret;
1040 }
1041
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042 // check calling permissions
1043 if (!settingsAllowed()) {
1044 return PERMISSION_DENIED;
1045 }
1046
1047 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001048 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001049 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001050 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051 mHardwareStatus = AUDIO_HW_IDLE;
1052
1053 return ret;
1054}
1055
Glenn Kasten26c77552012-11-16 12:01:44 -08001056status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001057 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058{
1059 status_t status;
1060
1061 Mutex::Autolock _l(mLock);
1062
1063 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1064 if (playbackThread != NULL) {
1065 return playbackThread->getRenderPosition(halFrames, dspFrames);
1066 }
1067
1068 return BAD_VALUE;
1069}
1070
1071void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1072{
1073
1074 Mutex::Autolock _l(mLock);
1075
Glenn Kastenbb001922012-02-03 11:10:26 -08001076 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077 if (mNotificationClients.indexOfKey(pid) < 0) {
1078 sp<NotificationClient> notificationClient = new NotificationClient(this,
1079 client,
1080 pid);
Steve Block3856b092011-10-20 11:56:00 +01001081 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001082
1083 mNotificationClients.add(pid, notificationClient);
1084
1085 sp<IBinder> binder = client->asBinder();
1086 binder->linkToDeath(notificationClient);
1087
1088 // the config change is always sent from playback or record threads to avoid deadlock
1089 // with AudioSystem::gLock
1090 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001091 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001092 }
1093
1094 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001095 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001096 }
1097 }
1098}
1099
1100void AudioFlinger::removeNotificationClient(pid_t pid)
1101{
1102 Mutex::Autolock _l(mLock);
1103
Glenn Kastena3b09252012-01-20 09:19:01 -08001104 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001105
Steve Block3856b092011-10-20 11:56:00 +01001106 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001107 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001108 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001109 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001110 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001111 ALOGV(" pid %d @ %d", ref->mPid, i);
1112 if (ref->mPid == pid) {
1113 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001114 mAudioSessionRefs.removeAt(i);
1115 delete ref;
1116 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001117 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001118 } else {
1119 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001120 }
1121 }
1122 if (removed) {
1123 purgeStaleEffects_l();
1124 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125}
1126
1127// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001128void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129{
1130 size_t size = mNotificationClients.size();
1131 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001132 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1133 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134 }
1135}
1136
1137// removeClient_l() must be called with AudioFlinger::mLock held
1138void AudioFlinger::removeClient_l(pid_t pid)
1139{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001140 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001141 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142 mClients.removeItem(pid);
1143}
1144
Eric Laurent717e1282012-06-29 16:36:52 -07001145// getEffectThread_l() must be called with AudioFlinger::mLock held
1146sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1147{
1148 sp<PlaybackThread> thread;
1149
1150 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1151 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1152 ALOG_ASSERT(thread == 0);
1153 thread = mPlaybackThreads.valueAt(i);
1154 }
1155 }
1156
1157 return thread;
1158}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001159
Mathias Agopian65ab4712010-07-14 17:59:35 -07001160
Mathias Agopian65ab4712010-07-14 17:59:35 -07001161
1162// ----------------------------------------------------------------------------
1163
1164AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1165 : RefBase(),
1166 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001167 // 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 -07001168 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001169 mPid(pid),
1170 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001171{
1172 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1173}
1174
1175// Client destructor must be called with AudioFlinger::mLock held
1176AudioFlinger::Client::~Client()
1177{
1178 mAudioFlinger->removeClient_l(mPid);
1179}
1180
Glenn Kasten435dbe62012-01-30 10:15:48 -08001181sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001182{
1183 return mMemoryDealer;
1184}
1185
John Grossman4ff14ba2012-02-08 16:37:41 -08001186// Reserve one of the limited slots for a timed audio track associated
1187// with this client
1188bool AudioFlinger::Client::reserveTimedTrack()
1189{
1190 const int kMaxTimedTracksPerClient = 4;
1191
1192 Mutex::Autolock _l(mTimedTrackLock);
1193
1194 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1195 ALOGW("can not create timed track - pid %d has exceeded the limit",
1196 mPid);
1197 return false;
1198 }
1199
1200 mTimedTrackCount++;
1201 return true;
1202}
1203
1204// Release a slot for a timed audio track
1205void AudioFlinger::Client::releaseTimedTrack()
1206{
1207 Mutex::Autolock _l(mTimedTrackLock);
1208 mTimedTrackCount--;
1209}
1210
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211// ----------------------------------------------------------------------------
1212
1213AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1214 const sp<IAudioFlingerClient>& client,
1215 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001216 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217{
1218}
1219
1220AudioFlinger::NotificationClient::~NotificationClient()
1221{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222}
1223
1224void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1225{
1226 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001227 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001228}
1229
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230
1231// ----------------------------------------------------------------------------
1232
Jeff Brown893a5642013-08-16 20:19:26 -07001233static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1234 return audio_is_remote_submix_device(inDevice);
1235}
1236
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001238 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001239 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001240 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001241 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001242 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001243 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001244 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001245 int *sessionId,
1246 status_t *status)
1247{
1248 sp<RecordThread::RecordTrack> recordTrack;
1249 sp<RecordHandle> recordHandle;
1250 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251 status_t lStatus;
1252 RecordThread *thread;
1253 size_t inFrameCount;
1254 int lSessionId;
1255
1256 // check calling permissions
1257 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001258 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 lStatus = PERMISSION_DENIED;
1260 goto Exit;
1261 }
1262
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001263 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1264 ALOGE("openRecord() invalid format %d", format);
1265 lStatus = BAD_VALUE;
1266 goto Exit;
1267 }
1268
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269 // add client to list
1270 { // scope for mLock
1271 Mutex::Autolock _l(mLock);
1272 thread = checkRecordThread_l(input);
1273 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001274 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275 lStatus = BAD_VALUE;
1276 goto Exit;
1277 }
1278
Jeff Brown893a5642013-08-16 20:19:26 -07001279 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1280 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001281 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001282 lStatus = PERMISSION_DENIED;
1283 goto Exit;
1284 }
1285
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001286 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001287 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288
1289 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001290 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001291 lSessionId = *sessionId;
1292 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001293 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001294 if (sessionId != NULL) {
1295 *sessionId = lSessionId;
1296 }
1297 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001298 // create new record track.
1299 // The record track uses one track in mHardwareMixerThread by convention.
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001300 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001301 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001302 frameCount, lSessionId,
1303 IPCThreadState::self()->getCallingUid(),
1304 flags, tid, &lStatus);
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001305 LOG_ALWAYS_FATAL_IF((recordTrack != 0) != (lStatus == NO_ERROR));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001306 }
Glenn Kastene198c362013-08-13 09:13:36 -07001307
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001308 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001309 // remove local strong reference to Client before deleting the RecordTrack so that the
1310 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001311 client.clear();
1312 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001313 goto Exit;
1314 }
1315
Glenn Kasten2fc14732013-08-05 14:58:14 -07001316 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001317 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001318
1319Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001320 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001321 return recordHandle;
1322}
1323
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001324
1325
Mathias Agopian65ab4712010-07-14 17:59:35 -07001326// ----------------------------------------------------------------------------
1327
Eric Laurenta4c5a552012-03-29 10:12:40 -07001328audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1329{
1330 if (!settingsAllowed()) {
1331 return 0;
1332 }
1333 Mutex::Autolock _l(mLock);
1334 return loadHwModule_l(name);
1335}
1336
1337// loadHwModule_l() must be called with AudioFlinger::mLock held
1338audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1339{
1340 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1341 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1342 ALOGW("loadHwModule() module %s already loaded", name);
1343 return mAudioHwDevs.keyAt(i);
1344 }
1345 }
1346
Eric Laurenta4c5a552012-03-29 10:12:40 -07001347 audio_hw_device_t *dev;
1348
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001349 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001350 if (rc) {
1351 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1352 return 0;
1353 }
1354
1355 mHardwareStatus = AUDIO_HW_INIT;
1356 rc = dev->init_check(dev);
1357 mHardwareStatus = AUDIO_HW_IDLE;
1358 if (rc) {
1359 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1360 return 0;
1361 }
1362
John Grossmanee578c02012-07-23 17:05:46 -07001363 // Check and cache this HAL's level of support for master mute and master
1364 // volume. If this is the first HAL opened, and it supports the get
1365 // methods, use the initial values provided by the HAL as the current
1366 // master mute and volume settings.
1367
1368 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1369 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001370 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001371
1372 if (0 == mAudioHwDevs.size()) {
1373 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1374 if (NULL != dev->get_master_volume) {
1375 float mv;
1376 if (OK == dev->get_master_volume(dev, &mv)) {
1377 mMasterVolume = mv;
1378 }
1379 }
1380
1381 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1382 if (NULL != dev->get_master_mute) {
1383 bool mm;
1384 if (OK == dev->get_master_mute(dev, &mm)) {
1385 mMasterMute = mm;
1386 }
1387 }
1388 }
1389
Eric Laurenta4c5a552012-03-29 10:12:40 -07001390 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001391 if ((NULL != dev->set_master_volume) &&
1392 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1393 flags = static_cast<AudioHwDevice::Flags>(flags |
1394 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1395 }
1396
1397 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1398 if ((NULL != dev->set_master_mute) &&
1399 (OK == dev->set_master_mute(dev, mMasterMute))) {
1400 flags = static_cast<AudioHwDevice::Flags>(flags |
1401 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1402 }
1403
Eric Laurenta4c5a552012-03-29 10:12:40 -07001404 mHardwareStatus = AUDIO_HW_IDLE;
1405 }
1406
1407 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001408 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001409
1410 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001411 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001412
1413 return handle;
1414
1415}
1416
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001417// ----------------------------------------------------------------------------
1418
Glenn Kasten3b16c762012-11-14 08:44:39 -08001419uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001420{
1421 Mutex::Autolock _l(mLock);
1422 PlaybackThread *thread = primaryPlaybackThread_l();
1423 return thread != NULL ? thread->sampleRate() : 0;
1424}
1425
Glenn Kastene33054e2012-11-14 12:54:39 -08001426size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001427{
1428 Mutex::Autolock _l(mLock);
1429 PlaybackThread *thread = primaryPlaybackThread_l();
1430 return thread != NULL ? thread->frameCountHAL() : 0;
1431}
1432
1433// ----------------------------------------------------------------------------
1434
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001435status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1436{
1437 uid_t uid = IPCThreadState::self()->getCallingUid();
1438 if (uid != AID_SYSTEM) {
1439 return PERMISSION_DENIED;
1440 }
1441 Mutex::Autolock _l(mLock);
1442 if (mIsDeviceTypeKnown) {
1443 return INVALID_OPERATION;
1444 }
1445 mIsLowRamDevice = isLowRamDevice;
1446 mIsDeviceTypeKnown = true;
1447 return NO_ERROR;
1448}
1449
1450// ----------------------------------------------------------------------------
1451
Eric Laurenta4c5a552012-03-29 10:12:40 -07001452audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1453 audio_devices_t *pDevices,
1454 uint32_t *pSamplingRate,
1455 audio_format_t *pFormat,
1456 audio_channel_mask_t *pChannelMask,
1457 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001458 audio_output_flags_t flags,
1459 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001460{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001461 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001462 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001463 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1464 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1465 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001466 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001467 config.offload_info = *offloadInfo;
1468 }
1469
Eric Laurentbfb1b832013-01-07 09:53:42 -08001470 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001471 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001472 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001473 config.sample_rate,
1474 config.format,
1475 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001476 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001477 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001478 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001479
1480 if (pDevices == NULL || *pDevices == 0) {
1481 return 0;
1482 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001483
Mathias Agopian65ab4712010-07-14 17:59:35 -07001484 Mutex::Autolock _l(mLock);
1485
Glenn Kasten32550952013-08-06 10:45:10 -07001486 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001487 if (outHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001488 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001489 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001490
John Grossmanee578c02012-07-23 17:05:46 -07001491 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001492 audio_io_handle_t id = nextUniqueId();
1493
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001494 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001495
Glenn Kasten32550952013-08-06 10:45:10 -07001496 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001497 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001498 id,
1499 *pDevices,
1500 (audio_output_flags_t)flags,
1501 &config,
1502 &outStream);
1503
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001504 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001505 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001506 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001507 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001508 config.sample_rate,
1509 config.format,
1510 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001511 status);
1512
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001513 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001514 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001515
Glenn Kasten32550952013-08-06 10:45:10 -07001516 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001517 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1518 thread = new OffloadThread(this, output, id, *pDevices);
1519 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1520 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001521 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1522 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001523 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001524 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525 } else {
1526 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001527 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001528 }
1529 mPlaybackThreads.add(id, thread);
1530
Glenn Kasten7c027242012-12-26 14:43:16 -08001531 if (pSamplingRate != NULL) {
1532 *pSamplingRate = config.sample_rate;
1533 }
1534 if (pFormat != NULL) {
1535 *pFormat = config.format;
1536 }
1537 if (pChannelMask != NULL) {
1538 *pChannelMask = config.channel_mask;
1539 }
1540 if (pLatencyMs != NULL) {
1541 *pLatencyMs = thread->latency();
1542 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543
1544 // notify client processes of the new output creation
1545 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001546
1547 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001548 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001549 ALOGI("Using module %d has the primary audio interface", module);
1550 mPrimaryHardwareDev = outHwDev;
1551
1552 AutoMutex lock(mHardwareLock);
1553 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001554 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001555 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001556 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001557 return id;
1558 }
1559
1560 return 0;
1561}
1562
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001563audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1564 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001565{
1566 Mutex::Autolock _l(mLock);
1567 MixerThread *thread1 = checkMixerThread_l(output1);
1568 MixerThread *thread2 = checkMixerThread_l(output2);
1569
1570 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001571 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1572 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001573 return 0;
1574 }
1575
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001576 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001577 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1578 thread->addOutputTrack(thread2);
1579 mPlaybackThreads.add(id, thread);
1580 // notify client processes of the new output creation
1581 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1582 return id;
1583}
1584
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001585status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001586{
Glenn Kastend96c5722012-04-25 13:44:49 -07001587 return closeOutput_nonvirtual(output);
1588}
1589
1590status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1591{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001592 // keep strong reference on the playback thread so that
1593 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001594 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001595 {
1596 Mutex::Autolock _l(mLock);
1597 thread = checkPlaybackThread_l(output);
1598 if (thread == NULL) {
1599 return BAD_VALUE;
1600 }
1601
Steve Block3856b092011-10-20 11:56:00 +01001602 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001603
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001604 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001605 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001606 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001607 DuplicatingThread *dupThread =
1608 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001610
1611 }
1612 }
1613 }
1614
1615
1616 mPlaybackThreads.removeItem(output);
1617 // save all effects to the default thread
1618 if (mPlaybackThreads.size()) {
1619 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1620 if (dstThread != NULL) {
1621 // audioflinger lock is held here so the acquisition order of thread locks does not
1622 // matter
1623 Mutex::Autolock _dl(dstThread->mLock);
1624 Mutex::Autolock _sl(thread->mLock);
1625 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1626 for (size_t i = 0; i < effectChains.size(); i ++) {
1627 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001628 }
1629 }
1630 }
Glenn Kastena1117922012-01-26 10:53:32 -08001631 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001632 }
1633 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001634 // The thread entity (active unit of execution) is no longer running here,
1635 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001636
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001637 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001638 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001639 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001640 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001641 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001642 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001643 }
1644 return NO_ERROR;
1645}
1646
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001647status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648{
1649 Mutex::Autolock _l(mLock);
1650 PlaybackThread *thread = checkPlaybackThread_l(output);
1651
1652 if (thread == NULL) {
1653 return BAD_VALUE;
1654 }
1655
Steve Block3856b092011-10-20 11:56:00 +01001656 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001657 thread->suspend();
1658
1659 return NO_ERROR;
1660}
1661
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001662status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001663{
1664 Mutex::Autolock _l(mLock);
1665 PlaybackThread *thread = checkPlaybackThread_l(output);
1666
1667 if (thread == NULL) {
1668 return BAD_VALUE;
1669 }
1670
Steve Block3856b092011-10-20 11:56:00 +01001671 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001672
1673 thread->restore();
1674
1675 return NO_ERROR;
1676}
1677
Eric Laurenta4c5a552012-03-29 10:12:40 -07001678audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1679 audio_devices_t *pDevices,
1680 uint32_t *pSamplingRate,
1681 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001682 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001683{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001684 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001685 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001686 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1687 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1688 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1689
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001690 uint32_t reqSamplingRate = config.sample_rate;
1691 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001692 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001693
1694 if (pDevices == NULL || *pDevices == 0) {
1695 return 0;
1696 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001697
Mathias Agopian65ab4712010-07-14 17:59:35 -07001698 Mutex::Autolock _l(mLock);
1699
Glenn Kasten32550952013-08-06 10:45:10 -07001700 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001701 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001702 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001703 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001704
John Grossmanee578c02012-07-23 17:05:46 -07001705 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001706 audio_io_handle_t id = nextUniqueId();
1707
Glenn Kasten32550952013-08-06 10:45:10 -07001708 audio_stream_in_t *inStream = NULL;
1709 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001710 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001711 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1712 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001713 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001714 config.sample_rate,
1715 config.format,
1716 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001717 status);
1718
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001719 // If the input could not be opened with the requested parameters and we can handle the
1720 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1721 // 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 -07001722 if (status == BAD_VALUE &&
1723 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1724 (config.sample_rate <= 2 * reqSamplingRate) &&
Glenn Kastenf506e942013-08-06 10:49:43 -07001725 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001726 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Glenn Kasten254af182012-07-03 14:59:05 -07001727 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001728 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001729 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07001730 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001731 }
1732
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001733 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001734
Glenn Kasten46909e72013-02-26 09:20:22 -08001735#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001736 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1737 // or (re-)create if current Pipe is idle and does not match the new format
1738 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001739 enum {
1740 TEE_SINK_NO, // don't copy input
1741 TEE_SINK_NEW, // copy input using a new pipe
1742 TEE_SINK_OLD, // copy input using an existing pipe
1743 } kind;
1744 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1745 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001746 if (!mTeeSinkInputEnabled) {
1747 kind = TEE_SINK_NO;
1748 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001749 kind = TEE_SINK_NO;
1750 } else if (mRecordTeeSink == 0) {
1751 kind = TEE_SINK_NEW;
1752 } else if (mRecordTeeSink->getStrongCount() != 1) {
1753 kind = TEE_SINK_NO;
1754 } else if (format == mRecordTeeSink->format()) {
1755 kind = TEE_SINK_OLD;
1756 } else {
1757 kind = TEE_SINK_NEW;
1758 }
1759 switch (kind) {
1760 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001761 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001762 size_t numCounterOffers = 0;
1763 const NBAIO_Format offers[1] = {format};
1764 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1765 ALOG_ASSERT(index == 0);
1766 PipeReader *pipeReader = new PipeReader(*pipe);
1767 numCounterOffers = 0;
1768 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1769 ALOG_ASSERT(index == 0);
1770 mRecordTeeSink = pipe;
1771 mRecordTeeSource = pipeReader;
1772 teeSink = pipe;
1773 }
1774 break;
1775 case TEE_SINK_OLD:
1776 teeSink = mRecordTeeSink;
1777 break;
1778 case TEE_SINK_NO:
1779 default:
1780 break;
1781 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001782#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001783
Dima Zavin799a70e2011-04-18 16:57:27 -07001784 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1785
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001786 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001787 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001788 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001789 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001790 input,
1791 reqSamplingRate,
Glenn Kastenf506e942013-08-06 10:49:43 -07001792 reqChannelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001793 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001794 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001795 *pDevices
1796#ifdef TEE_SINK
1797 , teeSink
1798#endif
1799 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001800 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001801 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001802 if (pSamplingRate != NULL) {
1803 *pSamplingRate = reqSamplingRate;
1804 }
1805 if (pFormat != NULL) {
1806 *pFormat = config.format;
1807 }
1808 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001809 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001810 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001811
Mathias Agopian65ab4712010-07-14 17:59:35 -07001812 // notify client processes of the new input creation
1813 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1814 return id;
1815 }
1816
1817 return 0;
1818}
1819
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001820status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001821{
Glenn Kastend96c5722012-04-25 13:44:49 -07001822 return closeInput_nonvirtual(input);
1823}
1824
1825status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1826{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001827 // keep strong reference on the record thread so that
1828 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001829 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001830 {
1831 Mutex::Autolock _l(mLock);
1832 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001833 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001834 return BAD_VALUE;
1835 }
1836
Steve Block3856b092011-10-20 11:56:00 +01001837 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001838 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001839 mRecordThreads.removeItem(input);
1840 }
1841 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001842 // The thread entity (active unit of execution) is no longer running here,
1843 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001844
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001845 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001846 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001847 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001848 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001849 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001850
1851 return NO_ERROR;
1852}
1853
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001854status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001855{
1856 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001857 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001858
1859 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1860 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001861 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001862 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001863
1864 return NO_ERROR;
1865}
1866
1867
1868int AudioFlinger::newAudioSessionId()
1869{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001870 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001871}
1872
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001873void AudioFlinger::acquireAudioSessionId(int audioSession)
1874{
1875 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001876 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001877 ALOGV("acquiring %d from %d", audioSession, caller);
Eric Laurentd1b28d42013-09-18 18:47:13 -07001878
1879 // Ignore requests received from processes not known as notification client. The request
1880 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1881 // called from a different pid leaving a stale session reference. Also we don't know how
1882 // to clear this reference if the client process dies.
1883 if (mNotificationClients.indexOfKey(caller) < 0) {
1884 ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
1885 return;
1886 }
1887
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001888 size_t num = mAudioSessionRefs.size();
1889 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001890 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001891 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1892 ref->mCnt++;
1893 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001894 return;
1895 }
1896 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001897 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1898 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001899}
1900
1901void AudioFlinger::releaseAudioSessionId(int audioSession)
1902{
1903 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001904 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001905 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001906 size_t num = mAudioSessionRefs.size();
1907 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001908 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001909 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1910 ref->mCnt--;
1911 ALOGV(" decremented refcount to %d", ref->mCnt);
1912 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001913 mAudioSessionRefs.removeAt(i);
1914 delete ref;
1915 purgeStaleEffects_l();
1916 }
1917 return;
1918 }
1919 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001920 // If the caller is mediaserver it is likely that the session being released was acquired
1921 // on behalf of a process not in notification clients and we ignore the warning.
1922 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001923}
1924
1925void AudioFlinger::purgeStaleEffects_l() {
1926
Steve Block3856b092011-10-20 11:56:00 +01001927 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001928
1929 Vector< sp<EffectChain> > chains;
1930
1931 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1932 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1933 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1934 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001935 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1936 chains.push(ec);
1937 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001938 }
1939 }
1940 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1941 sp<RecordThread> t = mRecordThreads.valueAt(i);
1942 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1943 sp<EffectChain> ec = t->mEffectChains[j];
1944 chains.push(ec);
1945 }
1946 }
1947
1948 for (size_t i = 0; i < chains.size(); i++) {
1949 sp<EffectChain> ec = chains[i];
1950 int sessionid = ec->sessionId();
1951 sp<ThreadBase> t = ec->mThread.promote();
1952 if (t == 0) {
1953 continue;
1954 }
1955 size_t numsessionrefs = mAudioSessionRefs.size();
1956 bool found = false;
1957 for (size_t k = 0; k < numsessionrefs; k++) {
1958 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001959 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001960 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001961 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001962 found = true;
1963 break;
1964 }
1965 }
1966 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07001967 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001968 // remove all effects from the chain
1969 while (ec->mEffects.size()) {
1970 sp<EffectModule> effect = ec->mEffects[0];
1971 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001972 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001973 if (effect->purgeHandles()) {
1974 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001975 }
1976 AudioSystem::unregisterEffect(effect->id());
1977 }
1978 }
1979 }
1980 return;
1981}
1982
Mathias Agopian65ab4712010-07-14 17:59:35 -07001983// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001984AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985{
Glenn Kastena1117922012-01-26 10:53:32 -08001986 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001987}
1988
1989// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001990AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001991{
1992 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001993 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001994}
1995
1996// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001997AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001998{
Glenn Kastena1117922012-01-26 10:53:32 -08001999 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002000}
2001
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002002uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002003{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002004 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002005}
2006
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002007AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002008{
2009 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2010 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002011 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002012 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002013 return thread;
2014 }
2015 }
2016 return NULL;
2017}
2018
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002019audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002020{
2021 PlaybackThread *thread = primaryPlaybackThread_l();
2022
2023 if (thread == NULL) {
2024 return 0;
2025 }
2026
Eric Laurentf1c04f92012-08-28 14:26:53 -07002027 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002028}
2029
Eric Laurenta011e352012-03-29 15:51:43 -07002030sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2031 int triggerSession,
2032 int listenerSession,
2033 sync_event_callback_t callBack,
2034 void *cookie)
2035{
2036 Mutex::Autolock _l(mLock);
2037
2038 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2039 status_t playStatus = NAME_NOT_FOUND;
2040 status_t recStatus = NAME_NOT_FOUND;
2041 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2042 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2043 if (playStatus == NO_ERROR) {
2044 return event;
2045 }
2046 }
2047 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2048 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2049 if (recStatus == NO_ERROR) {
2050 return event;
2051 }
2052 }
2053 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2054 mPendingSyncEvents.add(event);
2055 } else {
2056 ALOGV("createSyncEvent() invalid event %d", event->type());
2057 event.clear();
2058 }
2059 return event;
2060}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002061
Mathias Agopian65ab4712010-07-14 17:59:35 -07002062// ----------------------------------------------------------------------------
2063// Effect management
2064// ----------------------------------------------------------------------------
2065
2066
Glenn Kastenf587ba52012-01-26 16:25:10 -08002067status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002068{
2069 Mutex::Autolock _l(mLock);
2070 return EffectQueryNumberEffects(numEffects);
2071}
2072
Glenn Kastenf587ba52012-01-26 16:25:10 -08002073status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002074{
2075 Mutex::Autolock _l(mLock);
2076 return EffectQueryEffect(index, descriptor);
2077}
2078
Glenn Kasten5e92a782012-01-30 07:40:52 -08002079status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002080 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002081{
2082 Mutex::Autolock _l(mLock);
2083 return EffectGetDescriptor(pUuid, descriptor);
2084}
2085
2086
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002087sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002088 effect_descriptor_t *pDesc,
2089 const sp<IEffectClient>& effectClient,
2090 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002091 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002092 int sessionId,
2093 status_t *status,
2094 int *id,
2095 int *enabled)
2096{
2097 status_t lStatus = NO_ERROR;
2098 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002099 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002100
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002101 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002102 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002103 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002104
2105 if (pDesc == NULL) {
2106 lStatus = BAD_VALUE;
2107 goto Exit;
2108 }
2109
Eric Laurent84e9a102010-09-23 16:10:16 -07002110 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002111 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002112 lStatus = PERMISSION_DENIED;
2113 goto Exit;
2114 }
2115
Dima Zavinfce7a472011-04-19 22:30:36 -07002116 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002117 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002118 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002119 lStatus = PERMISSION_DENIED;
2120 goto Exit;
2121 }
2122
Mathias Agopian65ab4712010-07-14 17:59:35 -07002123 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002124 if (!EffectIsNullUuid(&pDesc->uuid)) {
2125 // if uuid is specified, request effect descriptor
2126 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2127 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002128 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002129 goto Exit;
2130 }
2131 } else {
2132 // if uuid is not specified, look for an available implementation
2133 // of the required type in effect factory
2134 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002135 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002136 lStatus = BAD_VALUE;
2137 goto Exit;
2138 }
2139 uint32_t numEffects = 0;
2140 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002141 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002142 bool found = false;
2143
2144 lStatus = EffectQueryNumberEffects(&numEffects);
2145 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002146 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002147 goto Exit;
2148 }
2149 for (uint32_t i = 0; i < numEffects; i++) {
2150 lStatus = EffectQueryEffect(i, &desc);
2151 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002152 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002153 continue;
2154 }
2155 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2156 // If matching type found save effect descriptor. If the session is
2157 // 0 and the effect is not auxiliary, continue enumeration in case
2158 // an auxiliary version of this effect type is available
2159 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002160 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002161 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002162 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2163 break;
2164 }
2165 }
2166 }
2167 if (!found) {
2168 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002169 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002170 goto Exit;
2171 }
2172 // For same effect type, chose auxiliary version over insert version if
2173 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002174 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002175 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002176 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002177 }
2178 }
2179
2180 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002181 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002182 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2183 lStatus = INVALID_OPERATION;
2184 goto Exit;
2185 }
2186
Eric Laurent59255e42011-07-27 19:49:51 -07002187 // check recording permission for visualizer
2188 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2189 !recordingAllowed()) {
2190 lStatus = PERMISSION_DENIED;
2191 goto Exit;
2192 }
2193
Mathias Agopian65ab4712010-07-14 17:59:35 -07002194 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002195 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002196 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2197 // if the output returned by getOutputForEffect() is removed before we lock the
2198 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2199 // and we will exit safely
2200 io = AudioSystem::getOutputForEffect(&desc);
2201 ALOGV("createEffect got output %d", io);
2202 }
2203
2204 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002205
2206 // If output is not specified try to find a matching audio session ID in one of the
2207 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002208 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2209 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002210 // Note: io is never 0 when creating an effect on an input
2211 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002212 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2213 // output must be specified by AudioPolicyManager when using session
2214 // AUDIO_SESSION_OUTPUT_STAGE
2215 lStatus = BAD_VALUE;
2216 goto Exit;
2217 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002218 // look for the thread where the specified audio session is present
2219 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2220 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2221 io = mPlaybackThreads.keyAt(i);
2222 break;
2223 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002224 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002225 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002226 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2227 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2228 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002229 break;
2230 }
2231 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002232 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002233 // If no output thread contains the requested session ID, default to
2234 // first output. The effect chain will be moved to the correct output
2235 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002236 if (io == 0 && mPlaybackThreads.size()) {
2237 io = mPlaybackThreads.keyAt(0);
2238 }
Steve Block3856b092011-10-20 11:56:00 +01002239 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002240 }
2241 ThreadBase *thread = checkRecordThread_l(io);
2242 if (thread == NULL) {
2243 thread = checkPlaybackThread_l(io);
2244 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002245 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002246 lStatus = BAD_VALUE;
2247 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002248 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002249 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002250
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002251 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002252
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002253 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002254 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2255 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002256 if (handle != 0 && id != NULL) {
2257 *id = handle->id();
2258 }
2259 }
2260
2261Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002262 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002263 return handle;
2264}
2265
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002266status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2267 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002268{
Steve Block3856b092011-10-20 11:56:00 +01002269 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002270 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002271 Mutex::Autolock _l(mLock);
2272 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002273 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002274 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002275 }
Eric Laurentde070132010-07-13 04:45:46 -07002276 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2277 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002278 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002279 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002280 }
Eric Laurentde070132010-07-13 04:45:46 -07002281 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2282 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002283 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002284 return BAD_VALUE;
2285 }
2286
2287 Mutex::Autolock _dl(dstThread->mLock);
2288 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002289 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002290}
2291
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002292// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002293status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002294 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002295 AudioFlinger::PlaybackThread *dstThread,
2296 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002297{
Steve Block3856b092011-10-20 11:56:00 +01002298 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002299 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002300
Eric Laurent59255e42011-07-27 19:49:51 -07002301 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002302 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002303 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002304 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002305 return INVALID_OPERATION;
2306 }
2307
Eric Laurent39e94f82010-07-28 01:32:47 -07002308 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002309 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002310 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002311 // removed.
2312 srcThread->removeEffectChain_l(chain);
2313
2314 // transfer all effects one by one so that new effect chain is created on new thread with
2315 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002316 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002317 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002318 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002319 Vector< sp<EffectModule> > removed;
2320 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002321 while (effect != 0) {
2322 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002323 removed.add(effect);
2324 status = dstThread->addEffect_l(effect);
2325 if (status != NO_ERROR) {
2326 break;
2327 }
Eric Laurentec35a142011-10-05 17:42:25 -07002328 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2329 if (effect->state() == EffectModule::ACTIVE ||
2330 effect->state() == EffectModule::STOPPING) {
2331 effect->start();
2332 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002333 // if the move request is not received from audio policy manager, the effect must be
2334 // re-registered with the new strategy and output
2335 if (dstChain == 0) {
2336 dstChain = effect->chain().promote();
2337 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002338 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002339 status = NO_INIT;
2340 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002341 }
2342 strategy = dstChain->strategy();
2343 }
2344 if (reRegister) {
2345 AudioSystem::unregisterEffect(effect->id());
2346 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002347 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002348 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002349 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002350 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002351 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002352 }
Eric Laurentde070132010-07-13 04:45:46 -07002353 effect = chain->getEffectFromId_l(0);
2354 }
2355
Eric Laurent5baf2af2013-09-12 17:37:00 -07002356 if (status != NO_ERROR) {
2357 for (size_t i = 0; i < removed.size(); i++) {
2358 srcThread->addEffect_l(removed[i]);
2359 if (dstChain != 0 && reRegister) {
2360 AudioSystem::unregisterEffect(removed[i]->id());
2361 AudioSystem::registerEffect(&removed[i]->desc(),
2362 srcThread->id(),
2363 strategy,
2364 sessionId,
2365 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002366 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002367 }
2368 }
2369 }
2370
2371 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002372}
2373
Eric Laurent5baf2af2013-09-12 17:37:00 -07002374bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002375{
2376 if (mGlobalEffectEnableTime != 0 &&
2377 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2378 return true;
2379 }
2380
2381 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2382 sp<EffectChain> ec =
2383 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002384 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002385 return true;
2386 }
2387 }
2388 return false;
2389}
2390
Eric Laurent5baf2af2013-09-12 17:37:00 -07002391void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002392{
2393 Mutex::Autolock _l(mLock);
2394
2395 mGlobalEffectEnableTime = systemTime();
2396
2397 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2398 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2399 if (t->mType == ThreadBase::OFFLOAD) {
2400 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2401 }
2402 }
2403
2404}
2405
Glenn Kastenda6ef132013-01-10 12:31:01 -08002406struct Entry {
2407#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2408 char mName[MAX_NAME];
2409};
2410
2411int comparEntry(const void *p1, const void *p2)
2412{
2413 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2414}
2415
Glenn Kasten46909e72013-02-26 09:20:22 -08002416#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002417void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002418{
Eric Laurent81784c32012-11-19 14:55:58 -08002419 NBAIO_Source *teeSource = source.get();
2420 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002421 // .wav rotation
2422 // There is a benign race condition if 2 threads call this simultaneously.
2423 // They would both traverse the directory, but the result would simply be
2424 // failures at unlink() which are ignored. It's also unlikely since
2425 // normally dumpsys is only done by bugreport or from the command line.
2426 char teePath[32+256];
2427 strcpy(teePath, "/data/misc/media");
2428 size_t teePathLen = strlen(teePath);
2429 DIR *dir = opendir(teePath);
2430 teePath[teePathLen++] = '/';
2431 if (dir != NULL) {
2432#define MAX_SORT 20 // number of entries to sort
2433#define MAX_KEEP 10 // number of entries to keep
2434 struct Entry entries[MAX_SORT];
2435 size_t entryCount = 0;
2436 while (entryCount < MAX_SORT) {
2437 struct dirent de;
2438 struct dirent *result = NULL;
2439 int rc = readdir_r(dir, &de, &result);
2440 if (rc != 0) {
2441 ALOGW("readdir_r failed %d", rc);
2442 break;
2443 }
2444 if (result == NULL) {
2445 break;
2446 }
2447 if (result != &de) {
2448 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2449 break;
2450 }
2451 // ignore non .wav file entries
2452 size_t nameLen = strlen(de.d_name);
2453 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2454 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2455 continue;
2456 }
2457 strcpy(entries[entryCount++].mName, de.d_name);
2458 }
2459 (void) closedir(dir);
2460 if (entryCount > MAX_KEEP) {
2461 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2462 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2463 strcpy(&teePath[teePathLen], entries[i].mName);
2464 (void) unlink(teePath);
2465 }
2466 }
2467 } else {
2468 if (fd >= 0) {
2469 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2470 }
2471 }
Eric Laurent81784c32012-11-19 14:55:58 -08002472 char teeTime[16];
2473 struct timeval tv;
2474 gettimeofday(&tv, NULL);
2475 struct tm tm;
2476 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002477 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2478 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2479 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2480 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002481 if (teeFd >= 0) {
2482 char wavHeader[44];
2483 memcpy(wavHeader,
2484 "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",
2485 sizeof(wavHeader));
2486 NBAIO_Format format = teeSource->format();
2487 unsigned channelCount = Format_channelCount(format);
2488 ALOG_ASSERT(channelCount <= FCC_2);
2489 uint32_t sampleRate = Format_sampleRate(format);
2490 wavHeader[22] = channelCount; // number of channels
2491 wavHeader[24] = sampleRate; // sample rate
2492 wavHeader[25] = sampleRate >> 8;
2493 wavHeader[32] = channelCount * 2; // block alignment
2494 write(teeFd, wavHeader, sizeof(wavHeader));
2495 size_t total = 0;
2496 bool firstRead = true;
2497 for (;;) {
2498#define TEE_SINK_READ 1024
2499 short buffer[TEE_SINK_READ * FCC_2];
2500 size_t count = TEE_SINK_READ;
2501 ssize_t actual = teeSource->read(buffer, count,
2502 AudioBufferProvider::kInvalidPTS);
2503 bool wasFirstRead = firstRead;
2504 firstRead = false;
2505 if (actual <= 0) {
2506 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2507 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002508 }
Eric Laurent81784c32012-11-19 14:55:58 -08002509 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002510 }
Eric Laurent81784c32012-11-19 14:55:58 -08002511 ALOG_ASSERT(actual <= (ssize_t)count);
2512 write(teeFd, buffer, actual * channelCount * sizeof(short));
2513 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002514 }
Eric Laurent81784c32012-11-19 14:55:58 -08002515 lseek(teeFd, (off_t) 4, SEEK_SET);
2516 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2517 write(teeFd, &temp, sizeof(temp));
2518 lseek(teeFd, (off_t) 40, SEEK_SET);
2519 temp = total * channelCount * sizeof(short);
2520 write(teeFd, &temp, sizeof(temp));
2521 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002522 if (fd >= 0) {
2523 fdprintf(fd, "tee copied to %s\n", teePath);
2524 }
Eric Laurent59255e42011-07-27 19:49:51 -07002525 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002526 if (fd >= 0) {
2527 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2528 }
Eric Laurent59255e42011-07-27 19:49:51 -07002529 }
2530 }
2531}
Glenn Kasten46909e72013-02-26 09:20:22 -08002532#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002533
Mathias Agopian65ab4712010-07-14 17:59:35 -07002534// ----------------------------------------------------------------------------
2535
2536status_t AudioFlinger::onTransact(
2537 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2538{
2539 return BnAudioFlinger::onTransact(code, data, reply, flags);
2540}
2541
Mathias Agopian65ab4712010-07-14 17:59:35 -07002542}; // namespace android