blob: 8fedc7fdc96ba17162dc04525a79e3fa4c8ce975 [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,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700454 status_t *status)
455{
456 sp<PlaybackThread::Track> track;
457 sp<TrackHandle> trackHandle;
458 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700459 status_t lStatus;
460 int lSessionId;
461
Glenn Kasten263709e2012-01-06 08:40:01 -0800462 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
463 // but if someone uses binder directly they could bypass that and cause us to crash
464 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000465 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466 lStatus = BAD_VALUE;
467 goto Exit;
468 }
469
Glenn Kasten60a83922012-06-21 12:56:37 -0700470 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
471 // and we don't yet support 8.24 or 32-bit PCM
472 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
473 ALOGE("createTrack() invalid format %d", format);
474 lStatus = BAD_VALUE;
475 goto Exit;
476 }
477
Mathias Agopian65ab4712010-07-14 17:59:35 -0700478 {
479 Mutex::Autolock _l(mLock);
480 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700481 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700482 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800483 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700484 lStatus = BAD_VALUE;
485 goto Exit;
486 }
487
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800488 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800489 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700490
Steve Block3856b092011-10-20 11:56:00 +0100491 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700492 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700493 // check if an effect chain with the same session ID is present on another
494 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700495 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700496 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
497 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700498 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700499 if (sessions & PlaybackThread::EFFECT_SESSION) {
500 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700501 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700502 }
Eric Laurentde070132010-07-13 04:45:46 -0700503 }
504 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505 lSessionId = *sessionId;
506 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700507 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700508 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700509 if (sessionId != NULL) {
510 *sessionId = lSessionId;
511 }
512 }
Steve Block3856b092011-10-20 11:56:00 +0100513 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700514
515 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800516 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Glenn Kasten2fc14732013-08-05 14:58:14 -0700517 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700518
519 // move effect chain to this output thread if an effect on same session was waiting
520 // for a track to be created
521 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700522 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700523 Mutex::Autolock _dl(thread->mLock);
524 Mutex::Autolock _sl(effectThread->mLock);
525 moveEffectChain_l(lSessionId, effectThread, thread, true);
526 }
Eric Laurenta011e352012-03-29 15:51:43 -0700527
528 // Look for sync events awaiting for a session to be used.
529 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
530 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
531 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700532 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700533 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700534 } else {
535 mPendingSyncEvents[i]->cancel();
536 }
Eric Laurenta011e352012-03-29 15:51:43 -0700537 mPendingSyncEvents.removeAt(i);
538 i--;
539 }
540 }
541 }
Glenn Kastene198c362013-08-13 09:13:36 -0700542
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 }
Glenn Kastene198c362013-08-13 09:13:36 -0700544
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700546 // s for server's pid, n for normal mixer name, f for fast index
547 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
548 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549 trackHandle = new TrackHandle(track);
550 } else {
551 // remove local strong reference to Client before deleting the Track so that the Client
552 // destructor is called by the TrackBase destructor with mLock held
553 client.clear();
554 track.clear();
555 }
556
557Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700558 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700559 return trackHandle;
560}
561
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800562uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563{
564 Mutex::Autolock _l(mLock);
565 PlaybackThread *thread = checkPlaybackThread_l(output);
566 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000567 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700568 return 0;
569 }
570 return thread->sampleRate();
571}
572
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800573int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574{
575 Mutex::Autolock _l(mLock);
576 PlaybackThread *thread = checkPlaybackThread_l(output);
577 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000578 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700579 return 0;
580 }
581 return thread->channelCount();
582}
583
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800584audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585{
586 Mutex::Autolock _l(mLock);
587 PlaybackThread *thread = checkPlaybackThread_l(output);
588 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000589 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800590 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 }
592 return thread->format();
593}
594
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800595size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596{
597 Mutex::Autolock _l(mLock);
598 PlaybackThread *thread = checkPlaybackThread_l(output);
599 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000600 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601 return 0;
602 }
Glenn Kasten58912562012-04-03 10:45:00 -0700603 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
604 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605 return thread->frameCount();
606}
607
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800608uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609{
610 Mutex::Autolock _l(mLock);
611 PlaybackThread *thread = checkPlaybackThread_l(output);
612 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800613 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700614 return 0;
615 }
616 return thread->latency();
617}
618
619status_t AudioFlinger::setMasterVolume(float value)
620{
Eric Laurenta1884f92011-08-23 08:25:03 -0700621 status_t ret = initCheck();
622 if (ret != NO_ERROR) {
623 return ret;
624 }
625
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626 // check calling permissions
627 if (!settingsAllowed()) {
628 return PERMISSION_DENIED;
629 }
630
Eric Laurenta4c5a552012-03-29 10:12:40 -0700631 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700632 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700633
John Grossmanee578c02012-07-23 17:05:46 -0700634 // Set master volume in the HALs which support it.
635 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
636 AutoMutex lock(mHardwareLock);
637 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800638
John Grossmanee578c02012-07-23 17:05:46 -0700639 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
640 if (dev->canSetMasterVolume()) {
641 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800642 }
John Grossmanee578c02012-07-23 17:05:46 -0700643 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700645
John Grossmanee578c02012-07-23 17:05:46 -0700646 // Now set the master volume in each playback thread. Playback threads
647 // assigned to HALs which do not have master volume support will apply
648 // master volume during the mix operation. Threads with HALs which do
649 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800650 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700651 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652
653 return NO_ERROR;
654}
655
Glenn Kastenf78aee72012-01-04 11:00:47 -0800656status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657{
Eric Laurenta1884f92011-08-23 08:25:03 -0700658 status_t ret = initCheck();
659 if (ret != NO_ERROR) {
660 return ret;
661 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700662
663 // check calling permissions
664 if (!settingsAllowed()) {
665 return PERMISSION_DENIED;
666 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800667 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000668 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 return BAD_VALUE;
670 }
671
672 { // scope for the lock
673 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700674 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700676 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 mHardwareStatus = AUDIO_HW_IDLE;
678 }
679
680 if (NO_ERROR == ret) {
681 Mutex::Autolock _l(mLock);
682 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800683 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700684 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 }
686
687 return ret;
688}
689
690status_t AudioFlinger::setMicMute(bool state)
691{
Eric Laurenta1884f92011-08-23 08:25:03 -0700692 status_t ret = initCheck();
693 if (ret != NO_ERROR) {
694 return ret;
695 }
696
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697 // check calling permissions
698 if (!settingsAllowed()) {
699 return PERMISSION_DENIED;
700 }
701
702 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700703 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700705 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706 mHardwareStatus = AUDIO_HW_IDLE;
707 return ret;
708}
709
710bool AudioFlinger::getMicMute() const
711{
Eric Laurenta1884f92011-08-23 08:25:03 -0700712 status_t ret = initCheck();
713 if (ret != NO_ERROR) {
714 return false;
715 }
716
Dima Zavinfce7a472011-04-19 22:30:36 -0700717 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800718 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700719 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700721 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 mHardwareStatus = AUDIO_HW_IDLE;
723 return state;
724}
725
726status_t AudioFlinger::setMasterMute(bool muted)
727{
John Grossmand8f178d2012-07-20 14:51:35 -0700728 status_t ret = initCheck();
729 if (ret != NO_ERROR) {
730 return ret;
731 }
732
Mathias Agopian65ab4712010-07-14 17:59:35 -0700733 // check calling permissions
734 if (!settingsAllowed()) {
735 return PERMISSION_DENIED;
736 }
737
John Grossmanee578c02012-07-23 17:05:46 -0700738 Mutex::Autolock _l(mLock);
739 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700740
John Grossmanee578c02012-07-23 17:05:46 -0700741 // Set master mute in the HALs which support it.
742 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
743 AutoMutex lock(mHardwareLock);
744 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700745
John Grossmanee578c02012-07-23 17:05:46 -0700746 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
747 if (dev->canSetMasterMute()) {
748 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700749 }
John Grossmanee578c02012-07-23 17:05:46 -0700750 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700751 }
752
John Grossmanee578c02012-07-23 17:05:46 -0700753 // Now set the master mute in each playback thread. Playback threads
754 // assigned to HALs which do not have master mute support will apply master
755 // mute during the mix operation. Threads with HALs which do support master
756 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800757 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700758 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700759
760 return NO_ERROR;
761}
762
763float AudioFlinger::masterVolume() const
764{
Glenn Kasten98067102011-12-13 11:47:54 -0800765 Mutex::Autolock _l(mLock);
766 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767}
768
769bool AudioFlinger::masterMute() const
770{
Glenn Kasten98067102011-12-13 11:47:54 -0800771 Mutex::Autolock _l(mLock);
772 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773}
774
John Grossman4ff14ba2012-02-08 16:37:41 -0800775float AudioFlinger::masterVolume_l() const
776{
John Grossman4ff14ba2012-02-08 16:37:41 -0800777 return mMasterVolume;
778}
779
John Grossmand8f178d2012-07-20 14:51:35 -0700780bool AudioFlinger::masterMute_l() const
781{
John Grossmanee578c02012-07-23 17:05:46 -0700782 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700783}
784
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800785status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
786 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700787{
788 // check calling permissions
789 if (!settingsAllowed()) {
790 return PERMISSION_DENIED;
791 }
792
Glenn Kasten263709e2012-01-06 08:40:01 -0800793 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000794 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795 return BAD_VALUE;
796 }
797
798 AutoMutex lock(mLock);
799 PlaybackThread *thread = NULL;
800 if (output) {
801 thread = checkPlaybackThread_l(output);
802 if (thread == NULL) {
803 return BAD_VALUE;
804 }
805 }
806
807 mStreamTypes[stream].volume = value;
808
809 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800810 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700811 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 }
813 } else {
814 thread->setStreamVolume(stream, value);
815 }
816
817 return NO_ERROR;
818}
819
Glenn Kastenfff6d712012-01-12 16:38:12 -0800820status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700821{
822 // check calling permissions
823 if (!settingsAllowed()) {
824 return PERMISSION_DENIED;
825 }
826
Glenn Kasten263709e2012-01-06 08:40:01 -0800827 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700828 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000829 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700830 return BAD_VALUE;
831 }
832
Eric Laurent93575202011-01-18 18:39:02 -0800833 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 mStreamTypes[stream].mute = muted;
835 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700836 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837
838 return NO_ERROR;
839}
840
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800841float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700842{
Glenn Kasten263709e2012-01-06 08:40:01 -0800843 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844 return 0.0f;
845 }
846
847 AutoMutex lock(mLock);
848 float volume;
849 if (output) {
850 PlaybackThread *thread = checkPlaybackThread_l(output);
851 if (thread == NULL) {
852 return 0.0f;
853 }
854 volume = thread->streamVolume(stream);
855 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800856 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700857 }
858
859 return volume;
860}
861
Glenn Kastenfff6d712012-01-12 16:38:12 -0800862bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863{
Glenn Kasten263709e2012-01-06 08:40:01 -0800864 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865 return true;
866 }
867
Glenn Kasten6637baa2012-01-09 09:40:36 -0800868 AutoMutex lock(mLock);
869 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870}
871
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800872status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700873{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700874 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
875 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800876
Mathias Agopian65ab4712010-07-14 17:59:35 -0700877 // check calling permissions
878 if (!settingsAllowed()) {
879 return PERMISSION_DENIED;
880 }
881
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882 // ioHandle == 0 means the parameters are global to the audio hardware interface
883 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700884 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700885 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800886 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700887 AutoMutex lock(mHardwareLock);
888 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
889 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
890 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
891 status_t result = dev->set_parameters(dev, keyValuePairs.string());
892 final_result = result ?: final_result;
893 }
894 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800895 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700896 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
897 AudioParameter param = AudioParameter(keyValuePairs);
898 String8 value;
899 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700900 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
901 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700902 for (size_t i = 0; i < mRecordThreads.size(); i++) {
903 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700904 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700905 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
906 // collect all of the thread's session IDs
907 KeyedVector<int, bool> ids = thread->sessionIds();
908 // suspend effects associated with those session IDs
909 for (size_t j = 0; j < ids.size(); ++j) {
910 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700911 thread->setEffectSuspended(FX_IID_AEC,
912 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700913 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700914 thread->setEffectSuspended(FX_IID_NS,
915 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700916 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700917 }
918 }
Eric Laurentbee53372011-08-29 12:42:48 -0700919 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700920 }
921 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700922 String8 screenState;
923 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
924 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800925 if (isOff != (AudioFlinger::mScreenState & 1)) {
926 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700927 }
928 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700929 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930 }
931
932 // hold a strong ref on thread in case closeOutput() or closeInput() is called
933 // and the thread is exited once the lock is released
934 sp<ThreadBase> thread;
935 {
936 Mutex::Autolock _l(mLock);
937 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700938 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800940 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700941 // indicate output device change to all input threads for pre processing
942 AudioParameter param = AudioParameter(keyValuePairs);
943 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700944 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
945 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700946 for (size_t i = 0; i < mRecordThreads.size(); i++) {
947 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
948 }
949 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700950 }
951 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800952 if (thread != 0) {
953 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954 }
955 return BAD_VALUE;
956}
957
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800958String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700959{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700960 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
961 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700962
Eric Laurenta4c5a552012-03-29 10:12:40 -0700963 Mutex::Autolock _l(mLock);
964
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700966 String8 out_s8;
967
Dima Zavin799a70e2011-04-18 16:57:27 -0700968 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800969 char *s;
970 {
971 AutoMutex lock(mHardwareLock);
972 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700973 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800974 s = dev->get_parameters(dev, keys.string());
975 mHardwareStatus = AUDIO_HW_IDLE;
976 }
John Grossmanef7740b2012-02-09 11:28:36 -0800977 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700978 free(s);
979 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700980 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 }
982
Mathias Agopian65ab4712010-07-14 17:59:35 -0700983 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
984 if (playbackThread != NULL) {
985 return playbackThread->getParameters(keys);
986 }
987 RecordThread *recordThread = checkRecordThread_l(ioHandle);
988 if (recordThread != NULL) {
989 return recordThread->getParameters(keys);
990 }
991 return String8("");
992}
993
Glenn Kastendd8104c2012-07-02 12:42:44 -0700994size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
995 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996{
Eric Laurenta1884f92011-08-23 08:25:03 -0700997 status_t ret = initCheck();
998 if (ret != NO_ERROR) {
999 return 0;
1000 }
1001
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001002 AutoMutex lock(mHardwareLock);
1003 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001004 struct audio_config config;
1005 memset(&config, 0, sizeof(config));
1006 config.sample_rate = sampleRate;
1007 config.channel_mask = channelMask;
1008 config.format = format;
1009
John Grossmanee578c02012-07-23 17:05:46 -07001010 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1011 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001012 mHardwareStatus = AUDIO_HW_IDLE;
1013 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001014}
1015
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001016unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001017{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001018 Mutex::Autolock _l(mLock);
1019
1020 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1021 if (recordThread != NULL) {
1022 return recordThread->getInputFramesLost();
1023 }
1024 return 0;
1025}
1026
1027status_t AudioFlinger::setVoiceVolume(float value)
1028{
Eric Laurenta1884f92011-08-23 08:25:03 -07001029 status_t ret = initCheck();
1030 if (ret != NO_ERROR) {
1031 return ret;
1032 }
1033
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034 // check calling permissions
1035 if (!settingsAllowed()) {
1036 return PERMISSION_DENIED;
1037 }
1038
1039 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001040 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001041 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001042 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001043 mHardwareStatus = AUDIO_HW_IDLE;
1044
1045 return ret;
1046}
1047
Glenn Kasten26c77552012-11-16 12:01:44 -08001048status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001049 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050{
1051 status_t status;
1052
1053 Mutex::Autolock _l(mLock);
1054
1055 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1056 if (playbackThread != NULL) {
1057 return playbackThread->getRenderPosition(halFrames, dspFrames);
1058 }
1059
1060 return BAD_VALUE;
1061}
1062
1063void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1064{
1065
1066 Mutex::Autolock _l(mLock);
1067
Glenn Kastenbb001922012-02-03 11:10:26 -08001068 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001069 if (mNotificationClients.indexOfKey(pid) < 0) {
1070 sp<NotificationClient> notificationClient = new NotificationClient(this,
1071 client,
1072 pid);
Steve Block3856b092011-10-20 11:56:00 +01001073 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074
1075 mNotificationClients.add(pid, notificationClient);
1076
1077 sp<IBinder> binder = client->asBinder();
1078 binder->linkToDeath(notificationClient);
1079
1080 // the config change is always sent from playback or record threads to avoid deadlock
1081 // with AudioSystem::gLock
1082 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001083 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001084 }
1085
1086 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001087 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001088 }
1089 }
1090}
1091
1092void AudioFlinger::removeNotificationClient(pid_t pid)
1093{
1094 Mutex::Autolock _l(mLock);
1095
Glenn Kastena3b09252012-01-20 09:19:01 -08001096 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001097
Steve Block3856b092011-10-20 11:56:00 +01001098 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001099 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001100 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001101 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001102 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001103 ALOGV(" pid %d @ %d", ref->mPid, i);
1104 if (ref->mPid == pid) {
1105 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001106 mAudioSessionRefs.removeAt(i);
1107 delete ref;
1108 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001109 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001110 } else {
1111 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001112 }
1113 }
1114 if (removed) {
1115 purgeStaleEffects_l();
1116 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117}
1118
1119// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001120void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121{
1122 size_t size = mNotificationClients.size();
1123 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001124 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1125 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001126 }
1127}
1128
1129// removeClient_l() must be called with AudioFlinger::mLock held
1130void AudioFlinger::removeClient_l(pid_t pid)
1131{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001132 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001133 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134 mClients.removeItem(pid);
1135}
1136
Eric Laurent717e1282012-06-29 16:36:52 -07001137// getEffectThread_l() must be called with AudioFlinger::mLock held
1138sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1139{
1140 sp<PlaybackThread> thread;
1141
1142 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1143 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1144 ALOG_ASSERT(thread == 0);
1145 thread = mPlaybackThreads.valueAt(i);
1146 }
1147 }
1148
1149 return thread;
1150}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152
Mathias Agopian65ab4712010-07-14 17:59:35 -07001153
1154// ----------------------------------------------------------------------------
1155
1156AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1157 : RefBase(),
1158 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001159 // 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 -07001160 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001161 mPid(pid),
1162 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163{
1164 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1165}
1166
1167// Client destructor must be called with AudioFlinger::mLock held
1168AudioFlinger::Client::~Client()
1169{
1170 mAudioFlinger->removeClient_l(mPid);
1171}
1172
Glenn Kasten435dbe62012-01-30 10:15:48 -08001173sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001174{
1175 return mMemoryDealer;
1176}
1177
John Grossman4ff14ba2012-02-08 16:37:41 -08001178// Reserve one of the limited slots for a timed audio track associated
1179// with this client
1180bool AudioFlinger::Client::reserveTimedTrack()
1181{
1182 const int kMaxTimedTracksPerClient = 4;
1183
1184 Mutex::Autolock _l(mTimedTrackLock);
1185
1186 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1187 ALOGW("can not create timed track - pid %d has exceeded the limit",
1188 mPid);
1189 return false;
1190 }
1191
1192 mTimedTrackCount++;
1193 return true;
1194}
1195
1196// Release a slot for a timed audio track
1197void AudioFlinger::Client::releaseTimedTrack()
1198{
1199 Mutex::Autolock _l(mTimedTrackLock);
1200 mTimedTrackCount--;
1201}
1202
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203// ----------------------------------------------------------------------------
1204
1205AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1206 const sp<IAudioFlingerClient>& client,
1207 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001208 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001209{
1210}
1211
1212AudioFlinger::NotificationClient::~NotificationClient()
1213{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001214}
1215
1216void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1217{
1218 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001219 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220}
1221
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222
1223// ----------------------------------------------------------------------------
1224
Jeff Brown893a5642013-08-16 20:19:26 -07001225static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1226 return audio_is_remote_submix_device(inDevice);
1227}
1228
Mathias Agopian65ab4712010-07-14 17:59:35 -07001229sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001230 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001232 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001233 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001234 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001235 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001236 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237 int *sessionId,
1238 status_t *status)
1239{
1240 sp<RecordThread::RecordTrack> recordTrack;
1241 sp<RecordHandle> recordHandle;
1242 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001243 status_t lStatus;
1244 RecordThread *thread;
1245 size_t inFrameCount;
1246 int lSessionId;
1247
1248 // check calling permissions
1249 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001250 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251 lStatus = PERMISSION_DENIED;
1252 goto Exit;
1253 }
1254
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001255 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1256 ALOGE("openRecord() invalid format %d", format);
1257 lStatus = BAD_VALUE;
1258 goto Exit;
1259 }
1260
Mathias Agopian65ab4712010-07-14 17:59:35 -07001261 // add client to list
1262 { // scope for mLock
1263 Mutex::Autolock _l(mLock);
1264 thread = checkRecordThread_l(input);
1265 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001266 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001267 lStatus = BAD_VALUE;
1268 goto Exit;
1269 }
1270
Jeff Brown893a5642013-08-16 20:19:26 -07001271 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1272 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001273 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001274 lStatus = PERMISSION_DENIED;
1275 goto Exit;
1276 }
1277
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001278 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001279 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001280
1281 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001282 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001283 lSessionId = *sessionId;
1284 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001285 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286 if (sessionId != NULL) {
1287 *sessionId = lSessionId;
1288 }
1289 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001290 // create new record track.
1291 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001292 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kastenddb0ccf2013-07-31 16:14:50 -07001293 frameCount, lSessionId, flags, tid, &lStatus);
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001294 LOG_ALWAYS_FATAL_IF((recordTrack != 0) != (lStatus == NO_ERROR));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001295 }
Glenn Kastene198c362013-08-13 09:13:36 -07001296
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001297 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001298 // remove local strong reference to Client before deleting the RecordTrack so that the
1299 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300 client.clear();
1301 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001302 goto Exit;
1303 }
1304
Glenn Kasten2fc14732013-08-05 14:58:14 -07001305 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001306 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001307
1308Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001309 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310 return recordHandle;
1311}
1312
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001313
1314
Mathias Agopian65ab4712010-07-14 17:59:35 -07001315// ----------------------------------------------------------------------------
1316
Eric Laurenta4c5a552012-03-29 10:12:40 -07001317audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1318{
1319 if (!settingsAllowed()) {
1320 return 0;
1321 }
1322 Mutex::Autolock _l(mLock);
1323 return loadHwModule_l(name);
1324}
1325
1326// loadHwModule_l() must be called with AudioFlinger::mLock held
1327audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1328{
1329 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1330 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1331 ALOGW("loadHwModule() module %s already loaded", name);
1332 return mAudioHwDevs.keyAt(i);
1333 }
1334 }
1335
Eric Laurenta4c5a552012-03-29 10:12:40 -07001336 audio_hw_device_t *dev;
1337
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001338 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001339 if (rc) {
1340 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1341 return 0;
1342 }
1343
1344 mHardwareStatus = AUDIO_HW_INIT;
1345 rc = dev->init_check(dev);
1346 mHardwareStatus = AUDIO_HW_IDLE;
1347 if (rc) {
1348 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1349 return 0;
1350 }
1351
John Grossmanee578c02012-07-23 17:05:46 -07001352 // Check and cache this HAL's level of support for master mute and master
1353 // volume. If this is the first HAL opened, and it supports the get
1354 // methods, use the initial values provided by the HAL as the current
1355 // master mute and volume settings.
1356
1357 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1358 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001359 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001360
1361 if (0 == mAudioHwDevs.size()) {
1362 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1363 if (NULL != dev->get_master_volume) {
1364 float mv;
1365 if (OK == dev->get_master_volume(dev, &mv)) {
1366 mMasterVolume = mv;
1367 }
1368 }
1369
1370 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1371 if (NULL != dev->get_master_mute) {
1372 bool mm;
1373 if (OK == dev->get_master_mute(dev, &mm)) {
1374 mMasterMute = mm;
1375 }
1376 }
1377 }
1378
Eric Laurenta4c5a552012-03-29 10:12:40 -07001379 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001380 if ((NULL != dev->set_master_volume) &&
1381 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1382 flags = static_cast<AudioHwDevice::Flags>(flags |
1383 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1384 }
1385
1386 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1387 if ((NULL != dev->set_master_mute) &&
1388 (OK == dev->set_master_mute(dev, mMasterMute))) {
1389 flags = static_cast<AudioHwDevice::Flags>(flags |
1390 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1391 }
1392
Eric Laurenta4c5a552012-03-29 10:12:40 -07001393 mHardwareStatus = AUDIO_HW_IDLE;
1394 }
1395
1396 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001397 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001398
1399 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001400 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001401
1402 return handle;
1403
1404}
1405
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001406// ----------------------------------------------------------------------------
1407
Glenn Kasten3b16c762012-11-14 08:44:39 -08001408uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001409{
1410 Mutex::Autolock _l(mLock);
1411 PlaybackThread *thread = primaryPlaybackThread_l();
1412 return thread != NULL ? thread->sampleRate() : 0;
1413}
1414
Glenn Kastene33054e2012-11-14 12:54:39 -08001415size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001416{
1417 Mutex::Autolock _l(mLock);
1418 PlaybackThread *thread = primaryPlaybackThread_l();
1419 return thread != NULL ? thread->frameCountHAL() : 0;
1420}
1421
1422// ----------------------------------------------------------------------------
1423
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001424status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1425{
1426 uid_t uid = IPCThreadState::self()->getCallingUid();
1427 if (uid != AID_SYSTEM) {
1428 return PERMISSION_DENIED;
1429 }
1430 Mutex::Autolock _l(mLock);
1431 if (mIsDeviceTypeKnown) {
1432 return INVALID_OPERATION;
1433 }
1434 mIsLowRamDevice = isLowRamDevice;
1435 mIsDeviceTypeKnown = true;
1436 return NO_ERROR;
1437}
1438
1439// ----------------------------------------------------------------------------
1440
Eric Laurenta4c5a552012-03-29 10:12:40 -07001441audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1442 audio_devices_t *pDevices,
1443 uint32_t *pSamplingRate,
1444 audio_format_t *pFormat,
1445 audio_channel_mask_t *pChannelMask,
1446 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001447 audio_output_flags_t flags,
1448 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001450 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001451 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001452 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1453 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1454 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001455 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001456 config.offload_info = *offloadInfo;
1457 }
1458
Eric Laurentbfb1b832013-01-07 09:53:42 -08001459 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001460 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001461 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001462 config.sample_rate,
1463 config.format,
1464 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001465 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001466 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001467 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001468
1469 if (pDevices == NULL || *pDevices == 0) {
1470 return 0;
1471 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001472
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473 Mutex::Autolock _l(mLock);
1474
Glenn Kasten32550952013-08-06 10:45:10 -07001475 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001476 if (outHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001477 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001478 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001479
John Grossmanee578c02012-07-23 17:05:46 -07001480 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001481 audio_io_handle_t id = nextUniqueId();
1482
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001483 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001484
Glenn Kasten32550952013-08-06 10:45:10 -07001485 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001486 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001487 id,
1488 *pDevices,
1489 (audio_output_flags_t)flags,
1490 &config,
1491 &outStream);
1492
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001493 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001494 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001495 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001496 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001497 config.sample_rate,
1498 config.format,
1499 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001500 status);
1501
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001502 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001503 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001504
Glenn Kasten32550952013-08-06 10:45:10 -07001505 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001506 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1507 thread = new OffloadThread(this, output, id, *pDevices);
1508 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1509 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001510 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1511 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001513 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001514 } else {
1515 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001516 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001517 }
1518 mPlaybackThreads.add(id, thread);
1519
Glenn Kasten7c027242012-12-26 14:43:16 -08001520 if (pSamplingRate != NULL) {
1521 *pSamplingRate = config.sample_rate;
1522 }
1523 if (pFormat != NULL) {
1524 *pFormat = config.format;
1525 }
1526 if (pChannelMask != NULL) {
1527 *pChannelMask = config.channel_mask;
1528 }
1529 if (pLatencyMs != NULL) {
1530 *pLatencyMs = thread->latency();
1531 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001532
1533 // notify client processes of the new output creation
1534 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001535
1536 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001537 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001538 ALOGI("Using module %d has the primary audio interface", module);
1539 mPrimaryHardwareDev = outHwDev;
1540
1541 AutoMutex lock(mHardwareLock);
1542 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001543 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001544 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001545 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001546 return id;
1547 }
1548
1549 return 0;
1550}
1551
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001552audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1553 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001554{
1555 Mutex::Autolock _l(mLock);
1556 MixerThread *thread1 = checkMixerThread_l(output1);
1557 MixerThread *thread2 = checkMixerThread_l(output2);
1558
1559 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001560 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1561 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562 return 0;
1563 }
1564
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001565 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001566 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1567 thread->addOutputTrack(thread2);
1568 mPlaybackThreads.add(id, thread);
1569 // notify client processes of the new output creation
1570 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1571 return id;
1572}
1573
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001574status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001575{
Glenn Kastend96c5722012-04-25 13:44:49 -07001576 return closeOutput_nonvirtual(output);
1577}
1578
1579status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1580{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001581 // keep strong reference on the playback thread so that
1582 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001583 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001584 {
1585 Mutex::Autolock _l(mLock);
1586 thread = checkPlaybackThread_l(output);
1587 if (thread == NULL) {
1588 return BAD_VALUE;
1589 }
1590
Steve Block3856b092011-10-20 11:56:00 +01001591 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001592
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001593 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001595 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001596 DuplicatingThread *dupThread =
1597 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001598 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001599
1600 }
1601 }
1602 }
1603
1604
1605 mPlaybackThreads.removeItem(output);
1606 // save all effects to the default thread
1607 if (mPlaybackThreads.size()) {
1608 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1609 if (dstThread != NULL) {
1610 // audioflinger lock is held here so the acquisition order of thread locks does not
1611 // matter
1612 Mutex::Autolock _dl(dstThread->mLock);
1613 Mutex::Autolock _sl(thread->mLock);
1614 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1615 for (size_t i = 0; i < effectChains.size(); i ++) {
1616 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001617 }
1618 }
1619 }
Glenn Kastena1117922012-01-26 10:53:32 -08001620 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001621 }
1622 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001623 // The thread entity (active unit of execution) is no longer running here,
1624 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001625
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001626 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001627 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001628 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001629 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001630 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001631 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001632 }
1633 return NO_ERROR;
1634}
1635
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001636status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001637{
1638 Mutex::Autolock _l(mLock);
1639 PlaybackThread *thread = checkPlaybackThread_l(output);
1640
1641 if (thread == NULL) {
1642 return BAD_VALUE;
1643 }
1644
Steve Block3856b092011-10-20 11:56:00 +01001645 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001646 thread->suspend();
1647
1648 return NO_ERROR;
1649}
1650
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001651status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001652{
1653 Mutex::Autolock _l(mLock);
1654 PlaybackThread *thread = checkPlaybackThread_l(output);
1655
1656 if (thread == NULL) {
1657 return BAD_VALUE;
1658 }
1659
Steve Block3856b092011-10-20 11:56:00 +01001660 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001661
1662 thread->restore();
1663
1664 return NO_ERROR;
1665}
1666
Eric Laurenta4c5a552012-03-29 10:12:40 -07001667audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1668 audio_devices_t *pDevices,
1669 uint32_t *pSamplingRate,
1670 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001671 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001672{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001673 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001674 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001675 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1676 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1677 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1678
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001679 uint32_t reqSamplingRate = config.sample_rate;
1680 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001681 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001682
1683 if (pDevices == NULL || *pDevices == 0) {
1684 return 0;
1685 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001686
Mathias Agopian65ab4712010-07-14 17:59:35 -07001687 Mutex::Autolock _l(mLock);
1688
Glenn Kasten32550952013-08-06 10:45:10 -07001689 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001690 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001691 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001692 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001693
John Grossmanee578c02012-07-23 17:05:46 -07001694 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001695 audio_io_handle_t id = nextUniqueId();
1696
Glenn Kasten32550952013-08-06 10:45:10 -07001697 audio_stream_in_t *inStream = NULL;
1698 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001699 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001700 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1701 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001702 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001703 config.sample_rate,
1704 config.format,
1705 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001706 status);
1707
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001708 // If the input could not be opened with the requested parameters and we can handle the
1709 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1710 // 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 -07001711 if (status == BAD_VALUE &&
1712 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1713 (config.sample_rate <= 2 * reqSamplingRate) &&
Glenn Kastenf506e942013-08-06 10:49:43 -07001714 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001715 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001716 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001717 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001718 }
1719
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001720 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001721
Glenn Kasten46909e72013-02-26 09:20:22 -08001722#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001723 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1724 // or (re-)create if current Pipe is idle and does not match the new format
1725 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001726 enum {
1727 TEE_SINK_NO, // don't copy input
1728 TEE_SINK_NEW, // copy input using a new pipe
1729 TEE_SINK_OLD, // copy input using an existing pipe
1730 } kind;
1731 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1732 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001733 if (!mTeeSinkInputEnabled) {
1734 kind = TEE_SINK_NO;
1735 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001736 kind = TEE_SINK_NO;
1737 } else if (mRecordTeeSink == 0) {
1738 kind = TEE_SINK_NEW;
1739 } else if (mRecordTeeSink->getStrongCount() != 1) {
1740 kind = TEE_SINK_NO;
1741 } else if (format == mRecordTeeSink->format()) {
1742 kind = TEE_SINK_OLD;
1743 } else {
1744 kind = TEE_SINK_NEW;
1745 }
1746 switch (kind) {
1747 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001748 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001749 size_t numCounterOffers = 0;
1750 const NBAIO_Format offers[1] = {format};
1751 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1752 ALOG_ASSERT(index == 0);
1753 PipeReader *pipeReader = new PipeReader(*pipe);
1754 numCounterOffers = 0;
1755 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1756 ALOG_ASSERT(index == 0);
1757 mRecordTeeSink = pipe;
1758 mRecordTeeSource = pipeReader;
1759 teeSink = pipe;
1760 }
1761 break;
1762 case TEE_SINK_OLD:
1763 teeSink = mRecordTeeSink;
1764 break;
1765 case TEE_SINK_NO:
1766 default:
1767 break;
1768 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001769#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001770
Dima Zavin799a70e2011-04-18 16:57:27 -07001771 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1772
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001773 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001774 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001775 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001776 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001777 input,
1778 reqSamplingRate,
Glenn Kastenf506e942013-08-06 10:49:43 -07001779 reqChannelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001780 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001781 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001782 *pDevices
1783#ifdef TEE_SINK
1784 , teeSink
1785#endif
1786 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001787 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001788 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001789 if (pSamplingRate != NULL) {
1790 *pSamplingRate = reqSamplingRate;
1791 }
1792 if (pFormat != NULL) {
1793 *pFormat = config.format;
1794 }
1795 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001796 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001797 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001798
Mathias Agopian65ab4712010-07-14 17:59:35 -07001799 // notify client processes of the new input creation
1800 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1801 return id;
1802 }
1803
1804 return 0;
1805}
1806
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001807status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001808{
Glenn Kastend96c5722012-04-25 13:44:49 -07001809 return closeInput_nonvirtual(input);
1810}
1811
1812status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1813{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001814 // keep strong reference on the record thread so that
1815 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001816 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001817 {
1818 Mutex::Autolock _l(mLock);
1819 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001820 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001821 return BAD_VALUE;
1822 }
1823
Steve Block3856b092011-10-20 11:56:00 +01001824 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001825 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001826 mRecordThreads.removeItem(input);
1827 }
1828 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001829 // The thread entity (active unit of execution) is no longer running here,
1830 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001831
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001832 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001833 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001834 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001835 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001836 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001837
1838 return NO_ERROR;
1839}
1840
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001841status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001842{
1843 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001844 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001845
1846 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1847 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001848 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001849 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001850
1851 return NO_ERROR;
1852}
1853
1854
1855int AudioFlinger::newAudioSessionId()
1856{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001857 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001858}
1859
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001860void AudioFlinger::acquireAudioSessionId(int audioSession)
1861{
1862 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001863 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001864 ALOGV("acquiring %d from %d", audioSession, caller);
Eric Laurentd1b28d42013-09-18 18:47:13 -07001865
1866 // Ignore requests received from processes not known as notification client. The request
1867 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1868 // called from a different pid leaving a stale session reference. Also we don't know how
1869 // to clear this reference if the client process dies.
1870 if (mNotificationClients.indexOfKey(caller) < 0) {
1871 ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
1872 return;
1873 }
1874
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001875 size_t num = mAudioSessionRefs.size();
1876 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001877 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001878 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1879 ref->mCnt++;
1880 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001881 return;
1882 }
1883 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001884 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1885 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001886}
1887
1888void AudioFlinger::releaseAudioSessionId(int audioSession)
1889{
1890 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001891 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001892 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001893 size_t num = mAudioSessionRefs.size();
1894 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001895 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001896 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1897 ref->mCnt--;
1898 ALOGV(" decremented refcount to %d", ref->mCnt);
1899 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001900 mAudioSessionRefs.removeAt(i);
1901 delete ref;
1902 purgeStaleEffects_l();
1903 }
1904 return;
1905 }
1906 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001907 // If the caller is mediaserver it is likely that the session being released was acquired
1908 // on behalf of a process not in notification clients and we ignore the warning.
1909 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001910}
1911
1912void AudioFlinger::purgeStaleEffects_l() {
1913
Steve Block3856b092011-10-20 11:56:00 +01001914 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001915
1916 Vector< sp<EffectChain> > chains;
1917
1918 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1919 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1920 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1921 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001922 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1923 chains.push(ec);
1924 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001925 }
1926 }
1927 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1928 sp<RecordThread> t = mRecordThreads.valueAt(i);
1929 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1930 sp<EffectChain> ec = t->mEffectChains[j];
1931 chains.push(ec);
1932 }
1933 }
1934
1935 for (size_t i = 0; i < chains.size(); i++) {
1936 sp<EffectChain> ec = chains[i];
1937 int sessionid = ec->sessionId();
1938 sp<ThreadBase> t = ec->mThread.promote();
1939 if (t == 0) {
1940 continue;
1941 }
1942 size_t numsessionrefs = mAudioSessionRefs.size();
1943 bool found = false;
1944 for (size_t k = 0; k < numsessionrefs; k++) {
1945 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001946 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001947 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001948 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001949 found = true;
1950 break;
1951 }
1952 }
1953 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07001954 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001955 // remove all effects from the chain
1956 while (ec->mEffects.size()) {
1957 sp<EffectModule> effect = ec->mEffects[0];
1958 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001959 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001960 if (effect->purgeHandles()) {
1961 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001962 }
1963 AudioSystem::unregisterEffect(effect->id());
1964 }
1965 }
1966 }
1967 return;
1968}
1969
Mathias Agopian65ab4712010-07-14 17:59:35 -07001970// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001971AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001972{
Glenn Kastena1117922012-01-26 10:53:32 -08001973 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001974}
1975
1976// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001977AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001978{
1979 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001980 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001981}
1982
1983// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001984AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985{
Glenn Kastena1117922012-01-26 10:53:32 -08001986 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001987}
1988
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001989uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001990{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001991 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001992}
1993
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001994AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001995{
1996 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1997 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001998 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001999 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002000 return thread;
2001 }
2002 }
2003 return NULL;
2004}
2005
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002006audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002007{
2008 PlaybackThread *thread = primaryPlaybackThread_l();
2009
2010 if (thread == NULL) {
2011 return 0;
2012 }
2013
Eric Laurentf1c04f92012-08-28 14:26:53 -07002014 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002015}
2016
Eric Laurenta011e352012-03-29 15:51:43 -07002017sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2018 int triggerSession,
2019 int listenerSession,
2020 sync_event_callback_t callBack,
2021 void *cookie)
2022{
2023 Mutex::Autolock _l(mLock);
2024
2025 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2026 status_t playStatus = NAME_NOT_FOUND;
2027 status_t recStatus = NAME_NOT_FOUND;
2028 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2029 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2030 if (playStatus == NO_ERROR) {
2031 return event;
2032 }
2033 }
2034 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2035 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2036 if (recStatus == NO_ERROR) {
2037 return event;
2038 }
2039 }
2040 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2041 mPendingSyncEvents.add(event);
2042 } else {
2043 ALOGV("createSyncEvent() invalid event %d", event->type());
2044 event.clear();
2045 }
2046 return event;
2047}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002048
Mathias Agopian65ab4712010-07-14 17:59:35 -07002049// ----------------------------------------------------------------------------
2050// Effect management
2051// ----------------------------------------------------------------------------
2052
2053
Glenn Kastenf587ba52012-01-26 16:25:10 -08002054status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002055{
2056 Mutex::Autolock _l(mLock);
2057 return EffectQueryNumberEffects(numEffects);
2058}
2059
Glenn Kastenf587ba52012-01-26 16:25:10 -08002060status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002061{
2062 Mutex::Autolock _l(mLock);
2063 return EffectQueryEffect(index, descriptor);
2064}
2065
Glenn Kasten5e92a782012-01-30 07:40:52 -08002066status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002067 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002068{
2069 Mutex::Autolock _l(mLock);
2070 return EffectGetDescriptor(pUuid, descriptor);
2071}
2072
2073
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002074sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002075 effect_descriptor_t *pDesc,
2076 const sp<IEffectClient>& effectClient,
2077 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002078 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002079 int sessionId,
2080 status_t *status,
2081 int *id,
2082 int *enabled)
2083{
2084 status_t lStatus = NO_ERROR;
2085 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002086 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002087
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002088 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002089 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002090 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002091
2092 if (pDesc == NULL) {
2093 lStatus = BAD_VALUE;
2094 goto Exit;
2095 }
2096
Eric Laurent84e9a102010-09-23 16:10:16 -07002097 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002098 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002099 lStatus = PERMISSION_DENIED;
2100 goto Exit;
2101 }
2102
Dima Zavinfce7a472011-04-19 22:30:36 -07002103 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002104 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002105 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002106 lStatus = PERMISSION_DENIED;
2107 goto Exit;
2108 }
2109
Mathias Agopian65ab4712010-07-14 17:59:35 -07002110 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002111 if (!EffectIsNullUuid(&pDesc->uuid)) {
2112 // if uuid is specified, request effect descriptor
2113 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2114 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002115 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002116 goto Exit;
2117 }
2118 } else {
2119 // if uuid is not specified, look for an available implementation
2120 // of the required type in effect factory
2121 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002122 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002123 lStatus = BAD_VALUE;
2124 goto Exit;
2125 }
2126 uint32_t numEffects = 0;
2127 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002128 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002129 bool found = false;
2130
2131 lStatus = EffectQueryNumberEffects(&numEffects);
2132 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002133 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002134 goto Exit;
2135 }
2136 for (uint32_t i = 0; i < numEffects; i++) {
2137 lStatus = EffectQueryEffect(i, &desc);
2138 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002139 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002140 continue;
2141 }
2142 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2143 // If matching type found save effect descriptor. If the session is
2144 // 0 and the effect is not auxiliary, continue enumeration in case
2145 // an auxiliary version of this effect type is available
2146 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002147 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002148 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002149 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2150 break;
2151 }
2152 }
2153 }
2154 if (!found) {
2155 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002156 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002157 goto Exit;
2158 }
2159 // For same effect type, chose auxiliary version over insert version if
2160 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002161 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002162 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002163 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002164 }
2165 }
2166
2167 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002168 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002169 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2170 lStatus = INVALID_OPERATION;
2171 goto Exit;
2172 }
2173
Eric Laurent59255e42011-07-27 19:49:51 -07002174 // check recording permission for visualizer
2175 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2176 !recordingAllowed()) {
2177 lStatus = PERMISSION_DENIED;
2178 goto Exit;
2179 }
2180
Mathias Agopian65ab4712010-07-14 17:59:35 -07002181 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002182 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002183 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2184 // if the output returned by getOutputForEffect() is removed before we lock the
2185 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2186 // and we will exit safely
2187 io = AudioSystem::getOutputForEffect(&desc);
2188 ALOGV("createEffect got output %d", io);
2189 }
2190
2191 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192
2193 // If output is not specified try to find a matching audio session ID in one of the
2194 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002195 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2196 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002197 // Note: io is never 0 when creating an effect on an input
2198 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002199 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2200 // output must be specified by AudioPolicyManager when using session
2201 // AUDIO_SESSION_OUTPUT_STAGE
2202 lStatus = BAD_VALUE;
2203 goto Exit;
2204 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002205 // look for the thread where the specified audio session is present
2206 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2207 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2208 io = mPlaybackThreads.keyAt(i);
2209 break;
2210 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002211 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002212 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002213 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2214 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2215 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002216 break;
2217 }
2218 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002219 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002220 // If no output thread contains the requested session ID, default to
2221 // first output. The effect chain will be moved to the correct output
2222 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002223 if (io == 0 && mPlaybackThreads.size()) {
2224 io = mPlaybackThreads.keyAt(0);
2225 }
Steve Block3856b092011-10-20 11:56:00 +01002226 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002227 }
2228 ThreadBase *thread = checkRecordThread_l(io);
2229 if (thread == NULL) {
2230 thread = checkPlaybackThread_l(io);
2231 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002232 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002233 lStatus = BAD_VALUE;
2234 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002235 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002236 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002237
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002238 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002239
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002240 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002241 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2242 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002243 if (handle != 0 && id != NULL) {
2244 *id = handle->id();
2245 }
2246 }
2247
2248Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002249 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002250 return handle;
2251}
2252
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002253status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2254 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002255{
Steve Block3856b092011-10-20 11:56:00 +01002256 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002257 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002258 Mutex::Autolock _l(mLock);
2259 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002260 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002261 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002262 }
Eric Laurentde070132010-07-13 04:45:46 -07002263 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2264 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002265 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002266 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002267 }
Eric Laurentde070132010-07-13 04:45:46 -07002268 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2269 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002270 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002271 return BAD_VALUE;
2272 }
2273
2274 Mutex::Autolock _dl(dstThread->mLock);
2275 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002276 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002277}
2278
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002279// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002280status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002281 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002282 AudioFlinger::PlaybackThread *dstThread,
2283 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002284{
Steve Block3856b092011-10-20 11:56:00 +01002285 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002286 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002287
Eric Laurent59255e42011-07-27 19:49:51 -07002288 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002289 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002290 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002291 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002292 return INVALID_OPERATION;
2293 }
2294
Eric Laurent39e94f82010-07-28 01:32:47 -07002295 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002296 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002297 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002298 // removed.
2299 srcThread->removeEffectChain_l(chain);
2300
2301 // transfer all effects one by one so that new effect chain is created on new thread with
2302 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002303 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002304 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002305 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002306 Vector< sp<EffectModule> > removed;
2307 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002308 while (effect != 0) {
2309 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002310 removed.add(effect);
2311 status = dstThread->addEffect_l(effect);
2312 if (status != NO_ERROR) {
2313 break;
2314 }
Eric Laurentec35a142011-10-05 17:42:25 -07002315 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2316 if (effect->state() == EffectModule::ACTIVE ||
2317 effect->state() == EffectModule::STOPPING) {
2318 effect->start();
2319 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002320 // if the move request is not received from audio policy manager, the effect must be
2321 // re-registered with the new strategy and output
2322 if (dstChain == 0) {
2323 dstChain = effect->chain().promote();
2324 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002325 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002326 status = NO_INIT;
2327 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002328 }
2329 strategy = dstChain->strategy();
2330 }
2331 if (reRegister) {
2332 AudioSystem::unregisterEffect(effect->id());
2333 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002334 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002335 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002336 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002337 effect->id());
2338 }
Eric Laurentde070132010-07-13 04:45:46 -07002339 effect = chain->getEffectFromId_l(0);
2340 }
2341
Eric Laurent5baf2af2013-09-12 17:37:00 -07002342 if (status != NO_ERROR) {
2343 for (size_t i = 0; i < removed.size(); i++) {
2344 srcThread->addEffect_l(removed[i]);
2345 if (dstChain != 0 && reRegister) {
2346 AudioSystem::unregisterEffect(removed[i]->id());
2347 AudioSystem::registerEffect(&removed[i]->desc(),
2348 srcThread->id(),
2349 strategy,
2350 sessionId,
2351 removed[i]->id());
2352 }
2353 }
2354 }
2355
2356 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002357}
2358
Eric Laurent5baf2af2013-09-12 17:37:00 -07002359bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002360{
2361 if (mGlobalEffectEnableTime != 0 &&
2362 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2363 return true;
2364 }
2365
2366 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2367 sp<EffectChain> ec =
2368 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002369 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002370 return true;
2371 }
2372 }
2373 return false;
2374}
2375
Eric Laurent5baf2af2013-09-12 17:37:00 -07002376void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002377{
2378 Mutex::Autolock _l(mLock);
2379
2380 mGlobalEffectEnableTime = systemTime();
2381
2382 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2383 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2384 if (t->mType == ThreadBase::OFFLOAD) {
2385 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2386 }
2387 }
2388
2389}
2390
Glenn Kastenda6ef132013-01-10 12:31:01 -08002391struct Entry {
2392#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2393 char mName[MAX_NAME];
2394};
2395
2396int comparEntry(const void *p1, const void *p2)
2397{
2398 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2399}
2400
Glenn Kasten46909e72013-02-26 09:20:22 -08002401#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002402void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002403{
Eric Laurent81784c32012-11-19 14:55:58 -08002404 NBAIO_Source *teeSource = source.get();
2405 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002406 // .wav rotation
2407 // There is a benign race condition if 2 threads call this simultaneously.
2408 // They would both traverse the directory, but the result would simply be
2409 // failures at unlink() which are ignored. It's also unlikely since
2410 // normally dumpsys is only done by bugreport or from the command line.
2411 char teePath[32+256];
2412 strcpy(teePath, "/data/misc/media");
2413 size_t teePathLen = strlen(teePath);
2414 DIR *dir = opendir(teePath);
2415 teePath[teePathLen++] = '/';
2416 if (dir != NULL) {
2417#define MAX_SORT 20 // number of entries to sort
2418#define MAX_KEEP 10 // number of entries to keep
2419 struct Entry entries[MAX_SORT];
2420 size_t entryCount = 0;
2421 while (entryCount < MAX_SORT) {
2422 struct dirent de;
2423 struct dirent *result = NULL;
2424 int rc = readdir_r(dir, &de, &result);
2425 if (rc != 0) {
2426 ALOGW("readdir_r failed %d", rc);
2427 break;
2428 }
2429 if (result == NULL) {
2430 break;
2431 }
2432 if (result != &de) {
2433 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2434 break;
2435 }
2436 // ignore non .wav file entries
2437 size_t nameLen = strlen(de.d_name);
2438 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2439 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2440 continue;
2441 }
2442 strcpy(entries[entryCount++].mName, de.d_name);
2443 }
2444 (void) closedir(dir);
2445 if (entryCount > MAX_KEEP) {
2446 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2447 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2448 strcpy(&teePath[teePathLen], entries[i].mName);
2449 (void) unlink(teePath);
2450 }
2451 }
2452 } else {
2453 if (fd >= 0) {
2454 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2455 }
2456 }
Eric Laurent81784c32012-11-19 14:55:58 -08002457 char teeTime[16];
2458 struct timeval tv;
2459 gettimeofday(&tv, NULL);
2460 struct tm tm;
2461 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002462 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2463 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2464 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2465 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002466 if (teeFd >= 0) {
2467 char wavHeader[44];
2468 memcpy(wavHeader,
2469 "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",
2470 sizeof(wavHeader));
2471 NBAIO_Format format = teeSource->format();
2472 unsigned channelCount = Format_channelCount(format);
2473 ALOG_ASSERT(channelCount <= FCC_2);
2474 uint32_t sampleRate = Format_sampleRate(format);
2475 wavHeader[22] = channelCount; // number of channels
2476 wavHeader[24] = sampleRate; // sample rate
2477 wavHeader[25] = sampleRate >> 8;
2478 wavHeader[32] = channelCount * 2; // block alignment
2479 write(teeFd, wavHeader, sizeof(wavHeader));
2480 size_t total = 0;
2481 bool firstRead = true;
2482 for (;;) {
2483#define TEE_SINK_READ 1024
2484 short buffer[TEE_SINK_READ * FCC_2];
2485 size_t count = TEE_SINK_READ;
2486 ssize_t actual = teeSource->read(buffer, count,
2487 AudioBufferProvider::kInvalidPTS);
2488 bool wasFirstRead = firstRead;
2489 firstRead = false;
2490 if (actual <= 0) {
2491 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2492 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002493 }
Eric Laurent81784c32012-11-19 14:55:58 -08002494 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002495 }
Eric Laurent81784c32012-11-19 14:55:58 -08002496 ALOG_ASSERT(actual <= (ssize_t)count);
2497 write(teeFd, buffer, actual * channelCount * sizeof(short));
2498 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002499 }
Eric Laurent81784c32012-11-19 14:55:58 -08002500 lseek(teeFd, (off_t) 4, SEEK_SET);
2501 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2502 write(teeFd, &temp, sizeof(temp));
2503 lseek(teeFd, (off_t) 40, SEEK_SET);
2504 temp = total * channelCount * sizeof(short);
2505 write(teeFd, &temp, sizeof(temp));
2506 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002507 if (fd >= 0) {
2508 fdprintf(fd, "tee copied to %s\n", teePath);
2509 }
Eric Laurent59255e42011-07-27 19:49:51 -07002510 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002511 if (fd >= 0) {
2512 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2513 }
Eric Laurent59255e42011-07-27 19:49:51 -07002514 }
2515 }
2516}
Glenn Kasten46909e72013-02-26 09:20:22 -08002517#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002518
Mathias Agopian65ab4712010-07-14 17:59:35 -07002519// ----------------------------------------------------------------------------
2520
2521status_t AudioFlinger::onTransact(
2522 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2523{
2524 return BnAudioFlinger::onTransact(code, data, reply, flags);
2525}
2526
Mathias Agopian65ab4712010-07-14 17:59:35 -07002527}; // namespace android