blob: 92ee30eed86bca4e91bc96106c5e3367888a00ea [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
Marco Nelissenb2208842014-02-07 14:00:50 -0800107const char *formatToString(audio_format_t format) {
108 switch(format) {
109 case AUDIO_FORMAT_PCM_SUB_8_BIT: return "pcm8";
110 case AUDIO_FORMAT_PCM_SUB_16_BIT: return "pcm16";
111 case AUDIO_FORMAT_PCM_SUB_32_BIT: return "pcm32";
112 case AUDIO_FORMAT_PCM_SUB_8_24_BIT: return "pcm8.24";
113 case AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED: return "pcm24";
114 case AUDIO_FORMAT_PCM_SUB_FLOAT: return "pcmfloat";
115 case AUDIO_FORMAT_MP3: return "mp3";
116 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
117 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
118 case AUDIO_FORMAT_AAC: return "aac";
119 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
120 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
121 case AUDIO_FORMAT_VORBIS: return "vorbis";
122 default:
123 break;
124 }
125 return "unknown";
126}
127
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700128static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700129{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700130 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700131 int rc;
132
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700133 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
134 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
135 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
136 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700137 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700138 }
139 rc = audio_hw_device_open(mod, dev);
140 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
141 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
142 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700143 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700144 }
145 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
146 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
147 rc = BAD_VALUE;
148 goto out;
149 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700150 return 0;
151
152out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700153 *dev = NULL;
154 return rc;
155}
156
Mathias Agopian65ab4712010-07-14 17:59:35 -0700157// ----------------------------------------------------------------------------
158
159AudioFlinger::AudioFlinger()
160 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800161 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700162 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800163 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800164 mMasterMute(false),
165 mNextUniqueId(1),
166 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700167 mBtNrecIsOff(false),
168 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700169 mIsDeviceTypeKnown(false),
170 mGlobalEffectEnableTime(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700171{
Glenn Kasten949a9262013-04-16 12:35:20 -0700172 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800173 char value[PROPERTY_VALUE_MAX];
174 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
175 if (doLog) {
176 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
177 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800178#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800179 (void) property_get("ro.debuggable", value, "0");
180 int debuggable = atoi(value);
181 int teeEnabled = 0;
182 if (debuggable) {
183 (void) property_get("af.tee", value, "0");
184 teeEnabled = atoi(value);
185 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800186 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700187 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800188 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700189 }
190 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800191 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700192 }
193 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800194 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700195 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800196#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700197}
198
199void AudioFlinger::onFirstRef()
200{
Dima Zavin799a70e2011-04-18 16:57:27 -0700201 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700202
Eric Laurent93575202011-01-18 18:39:02 -0800203 Mutex::Autolock _l(mLock);
204
Dima Zavin799a70e2011-04-18 16:57:27 -0700205 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800206 char val_str[PROPERTY_VALUE_MAX] = { 0 };
207 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
208 uint32_t int_val;
209 if (1 == sscanf(val_str, "%u", &int_val)) {
210 mStandbyTimeInNsecs = milliseconds(int_val);
211 ALOGI("Using %u mSec as standby time.", int_val);
212 } else {
213 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
214 ALOGI("Using default %u mSec as standby time.",
215 (uint32_t)(mStandbyTimeInNsecs / 1000000));
216 }
217 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700218
Eric Laurenta4c5a552012-03-29 10:12:40 -0700219 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220}
221
222AudioFlinger::~AudioFlinger()
223{
224 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700225 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700226 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700227 }
228 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700229 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700230 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700232
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800233 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
234 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700235 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
236 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700237 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700238
239 // Tell media.log service about any old writers that still need to be unregistered
240 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
241 if (binder != 0) {
242 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
243 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
244 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
245 mUnregisteredWriters.pop();
246 mediaLogService->unregisterWriter(iMemory);
247 }
248 }
249
Mathias Agopian65ab4712010-07-14 17:59:35 -0700250}
251
Eric Laurenta4c5a552012-03-29 10:12:40 -0700252static const char * const audio_interfaces[] = {
253 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
254 AUDIO_HARDWARE_MODULE_ID_A2DP,
255 AUDIO_HARDWARE_MODULE_ID_USB,
256};
257#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
258
John Grossmanee578c02012-07-23 17:05:46 -0700259AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
260 audio_module_handle_t module,
261 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700262{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700263 // if module is 0, the request comes from an old policy manager and we should load
264 // well known modules
265 if (module == 0) {
266 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
267 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
268 loadHwModule_l(audio_interfaces[i]);
269 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700270 // then try to find a module supporting the requested device.
271 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
272 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
273 audio_hw_device_t *dev = audioHwDevice->hwDevice();
274 if ((dev->get_supported_devices != NULL) &&
275 (dev->get_supported_devices(dev) & devices) == devices)
276 return audioHwDevice;
277 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700278 } else {
279 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700280 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
281 if (audioHwDevice != NULL) {
282 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700283 }
284 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700285
Dima Zavin799a70e2011-04-18 16:57:27 -0700286 return NULL;
287}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700288
Glenn Kasten0f11b512014-01-31 16:18:54 -0800289void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700290{
291 const size_t SIZE = 256;
292 char buffer[SIZE];
293 String8 result;
294
295 result.append("Clients:\n");
296 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800297 sp<Client> client = mClients.valueAt(i).promote();
298 if (client != 0) {
299 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
300 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700301 }
302 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700303
Eric Laurentd1b28d42013-09-18 18:47:13 -0700304 result.append("Notification Clients:\n");
305 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
306 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
307 result.append(buffer);
308 }
309
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700310 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800311 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700312 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
313 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800314 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700315 result.append(buffer);
316 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700318}
319
320
Glenn Kasten0f11b512014-01-31 16:18:54 -0800321void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322{
323 const size_t SIZE = 256;
324 char buffer[SIZE];
325 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800326 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327
John Grossman4ff14ba2012-02-08 16:37:41 -0800328 snprintf(buffer, SIZE, "Hardware status: %d\n"
329 "Standby Time mSec: %u\n",
330 hardwareStatus,
331 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332 result.append(buffer);
333 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334}
335
Glenn Kasten0f11b512014-01-31 16:18:54 -0800336void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337{
338 const size_t SIZE = 256;
339 char buffer[SIZE];
340 String8 result;
341 snprintf(buffer, SIZE, "Permission Denial: "
342 "can't dump AudioFlinger from pid=%d, uid=%d\n",
343 IPCThreadState::self()->getCallingPid(),
344 IPCThreadState::self()->getCallingUid());
345 result.append(buffer);
346 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347}
348
Eric Laurent81784c32012-11-19 14:55:58 -0800349bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350{
351 bool locked = false;
352 for (int i = 0; i < kDumpLockRetries; ++i) {
353 if (mutex.tryLock() == NO_ERROR) {
354 locked = true;
355 break;
356 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800357 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358 }
359 return locked;
360}
361
362status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
363{
Glenn Kasten44deb052012-02-05 18:09:08 -0800364 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365 dumpPermissionDenial(fd, args);
366 } else {
367 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800368 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369 if (!hardwareLocked) {
370 String8 result(kHardwareLockedString);
371 write(fd, result.string(), result.size());
372 } else {
373 mHardwareLock.unlock();
374 }
375
Eric Laurent81784c32012-11-19 14:55:58 -0800376 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377
378 // failed to lock - AudioFlinger is probably deadlocked
379 if (!locked) {
380 String8 result(kDeadlockedString);
381 write(fd, result.string(), result.size());
382 }
383
384 dumpClients(fd, args);
385 dumpInternals(fd, args);
386
387 // dump playback threads
388 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
389 mPlaybackThreads.valueAt(i)->dump(fd, args);
390 }
391
392 // dump record threads
393 for (size_t i = 0; i < mRecordThreads.size(); i++) {
394 mRecordThreads.valueAt(i)->dump(fd, args);
395 }
396
Dima Zavin799a70e2011-04-18 16:57:27 -0700397 // dump all hardware devs
398 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700399 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700400 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700401 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700402
Glenn Kasten46909e72013-02-26 09:20:22 -0800403#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700404 // dump the serially shared record tee sink
405 if (mRecordTeeSource != 0) {
406 dumpTee(fd, mRecordTeeSource);
407 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800408#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700409
Glenn Kastend65d73c2012-06-22 17:21:07 -0700410 if (locked) {
411 mLock.unlock();
412 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800413
414 // append a copy of media.log here by forwarding fd to it, but don't attempt
415 // to lookup the service if it's not running, as it will block for a second
416 if (mLogMemoryDealer != 0) {
417 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
418 if (binder != 0) {
419 fdprintf(fd, "\nmedia.log:\n");
420 Vector<String16> args;
421 binder->dump(fd, args);
422 }
423 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424 }
425 return NO_ERROR;
426}
427
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800428sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
429{
430 // If pid is already in the mClients wp<> map, then use that entry
431 // (for which promote() is always != 0), otherwise create a new entry and Client.
432 sp<Client> client = mClients.valueFor(pid).promote();
433 if (client == 0) {
434 client = new Client(this, pid);
435 mClients.add(pid, client);
436 }
437
438 return client;
439}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440
Glenn Kasten9e58b552013-01-18 15:09:48 -0800441sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
442{
Glenn Kasten481fb672013-09-30 14:39:28 -0700443 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800444 if (mLogMemoryDealer == 0) {
445 return new NBLog::Writer();
446 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800447 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700448 // Similarly if we can't contact the media.log service, also return a dummy writer
449 if (binder == 0) {
450 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800451 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700452 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
453 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
454 // If allocation fails, consult the vector of previously unregistered writers
455 // and garbage-collect one or more them until an allocation succeeds
456 if (shared == 0) {
457 Mutex::Autolock _l(mUnregisteredWritersLock);
458 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
459 {
460 // Pick the oldest stale writer to garbage-collect
461 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
462 mUnregisteredWriters.removeAt(0);
463 mediaLogService->unregisterWriter(iMemory);
464 // Now the media.log remote reference to IMemory is gone. When our last local
465 // reference to IMemory also drops to zero at end of this block,
466 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
467 }
468 // Re-attempt the allocation
469 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
470 if (shared != 0) {
471 goto success;
472 }
473 }
474 // Even after garbage-collecting all old writers, there is still not enough memory,
475 // so return a dummy writer
476 return new NBLog::Writer();
477 }
478success:
479 mediaLogService->registerWriter(shared, size, name);
480 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800481}
482
483void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
484{
Glenn Kasten685ef092013-02-04 08:15:34 -0800485 if (writer == 0) {
486 return;
487 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800488 sp<IMemory> iMemory(writer->getIMemory());
489 if (iMemory == 0) {
490 return;
491 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700492 // Rather than removing the writer immediately, append it to a queue of old writers to
493 // be garbage-collected later. This allows us to continue to view old logs for a while.
494 Mutex::Autolock _l(mUnregisteredWritersLock);
495 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800496}
497
Mathias Agopian65ab4712010-07-14 17:59:35 -0700498// IAudioFlinger interface
499
500
501sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800502 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800504 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700505 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800506 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800507 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700508 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800509 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800510 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700512 String8& name,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800513 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700514 status_t *status)
515{
516 sp<PlaybackThread::Track> track;
517 sp<TrackHandle> trackHandle;
518 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519 status_t lStatus;
520 int lSessionId;
521
Glenn Kasten263709e2012-01-06 08:40:01 -0800522 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
523 // but if someone uses binder directly they could bypass that and cause us to crash
524 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000525 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526 lStatus = BAD_VALUE;
527 goto Exit;
528 }
529
Glenn Kasten53b5d092014-02-05 10:00:23 -0800530 // further sample rate checks are performed by createTrack_l() depending on the thread type
531 if (sampleRate == 0) {
532 ALOGE("createTrack() invalid sample rate %u", sampleRate);
533 lStatus = BAD_VALUE;
534 goto Exit;
535 }
536
537 // further channel mask checks are performed by createTrack_l() depending on the thread type
538 if (!audio_is_output_channel(channelMask)) {
539 ALOGE("createTrack() invalid channel mask %#x", channelMask);
540 lStatus = BAD_VALUE;
541 goto Exit;
542 }
543
Glenn Kasten60a83922012-06-21 12:56:37 -0700544 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
545 // and we don't yet support 8.24 or 32-bit PCM
Glenn Kasten53b5d092014-02-05 10:00:23 -0800546 if (!audio_is_valid_format(format) ||
547 (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800548 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700549 lStatus = BAD_VALUE;
550 goto Exit;
551 }
552
Glenn Kasten663c2242013-09-24 11:52:37 -0700553 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
554 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
555 lStatus = BAD_VALUE;
556 goto Exit;
557 }
558
Mathias Agopian65ab4712010-07-14 17:59:35 -0700559 {
560 Mutex::Autolock _l(mLock);
561 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700562 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800564 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700565 lStatus = BAD_VALUE;
566 goto Exit;
567 }
568
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800569 pid_t pid = IPCThreadState::self()->getCallingPid();
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800570
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800571 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572
Steve Block3856b092011-10-20 11:56:00 +0100573 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700574 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700575 // check if an effect chain with the same session ID is present on another
576 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700577 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700578 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
579 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700580 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700581 if (sessions & PlaybackThread::EFFECT_SESSION) {
582 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700583 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700584 }
Eric Laurentde070132010-07-13 04:45:46 -0700585 }
586 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 lSessionId = *sessionId;
588 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700589 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700590 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 if (sessionId != NULL) {
592 *sessionId = lSessionId;
593 }
594 }
Steve Block3856b092011-10-20 11:56:00 +0100595 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596
597 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800598 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800599 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700600 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700601
602 // move effect chain to this output thread if an effect on same session was waiting
603 // for a track to be created
604 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700605 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700606 Mutex::Autolock _dl(thread->mLock);
607 Mutex::Autolock _sl(effectThread->mLock);
608 moveEffectChain_l(lSessionId, effectThread, thread, true);
609 }
Eric Laurenta011e352012-03-29 15:51:43 -0700610
611 // Look for sync events awaiting for a session to be used.
612 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
613 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
614 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700615 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700616 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700617 } else {
618 mPendingSyncEvents[i]->cancel();
619 }
Eric Laurenta011e352012-03-29 15:51:43 -0700620 mPendingSyncEvents.removeAt(i);
621 i--;
622 }
623 }
624 }
Glenn Kastene198c362013-08-13 09:13:36 -0700625
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626 }
Glenn Kastene198c362013-08-13 09:13:36 -0700627
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700629 // s for server's pid, n for normal mixer name, f for fast index
630 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
631 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632 trackHandle = new TrackHandle(track);
633 } else {
634 // remove local strong reference to Client before deleting the Track so that the Client
635 // destructor is called by the TrackBase destructor with mLock held
636 client.clear();
637 track.clear();
638 }
639
640Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700641 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642 return trackHandle;
643}
644
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800645uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646{
647 Mutex::Autolock _l(mLock);
648 PlaybackThread *thread = checkPlaybackThread_l(output);
649 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000650 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651 return 0;
652 }
653 return thread->sampleRate();
654}
655
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800656int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657{
658 Mutex::Autolock _l(mLock);
659 PlaybackThread *thread = checkPlaybackThread_l(output);
660 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000661 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700662 return 0;
663 }
664 return thread->channelCount();
665}
666
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800667audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668{
669 Mutex::Autolock _l(mLock);
670 PlaybackThread *thread = checkPlaybackThread_l(output);
671 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000672 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800673 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 }
675 return thread->format();
676}
677
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800678size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679{
680 Mutex::Autolock _l(mLock);
681 PlaybackThread *thread = checkPlaybackThread_l(output);
682 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000683 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684 return 0;
685 }
Glenn Kasten58912562012-04-03 10:45:00 -0700686 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
687 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700688 return thread->frameCount();
689}
690
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800691uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692{
693 Mutex::Autolock _l(mLock);
694 PlaybackThread *thread = checkPlaybackThread_l(output);
695 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800696 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697 return 0;
698 }
699 return thread->latency();
700}
701
702status_t AudioFlinger::setMasterVolume(float value)
703{
Eric Laurenta1884f92011-08-23 08:25:03 -0700704 status_t ret = initCheck();
705 if (ret != NO_ERROR) {
706 return ret;
707 }
708
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709 // check calling permissions
710 if (!settingsAllowed()) {
711 return PERMISSION_DENIED;
712 }
713
Eric Laurenta4c5a552012-03-29 10:12:40 -0700714 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700715 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700716
John Grossmanee578c02012-07-23 17:05:46 -0700717 // Set master volume in the HALs which support it.
718 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
719 AutoMutex lock(mHardwareLock);
720 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800721
John Grossmanee578c02012-07-23 17:05:46 -0700722 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
723 if (dev->canSetMasterVolume()) {
724 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800725 }
John Grossmanee578c02012-07-23 17:05:46 -0700726 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728
John Grossmanee578c02012-07-23 17:05:46 -0700729 // Now set the master volume in each playback thread. Playback threads
730 // assigned to HALs which do not have master volume support will apply
731 // master volume during the mix operation. Threads with HALs which do
732 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800733 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700734 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735
736 return NO_ERROR;
737}
738
Glenn Kastenf78aee72012-01-04 11:00:47 -0800739status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740{
Eric Laurenta1884f92011-08-23 08:25:03 -0700741 status_t ret = initCheck();
742 if (ret != NO_ERROR) {
743 return ret;
744 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700745
746 // check calling permissions
747 if (!settingsAllowed()) {
748 return PERMISSION_DENIED;
749 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800750 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000751 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752 return BAD_VALUE;
753 }
754
755 { // scope for the lock
756 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700757 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700759 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760 mHardwareStatus = AUDIO_HW_IDLE;
761 }
762
763 if (NO_ERROR == ret) {
764 Mutex::Autolock _l(mLock);
765 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800766 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700767 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768 }
769
770 return ret;
771}
772
773status_t AudioFlinger::setMicMute(bool state)
774{
Eric Laurenta1884f92011-08-23 08:25:03 -0700775 status_t ret = initCheck();
776 if (ret != NO_ERROR) {
777 return ret;
778 }
779
Mathias Agopian65ab4712010-07-14 17:59:35 -0700780 // check calling permissions
781 if (!settingsAllowed()) {
782 return PERMISSION_DENIED;
783 }
784
785 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700786 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700787 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700788 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789 mHardwareStatus = AUDIO_HW_IDLE;
790 return ret;
791}
792
793bool AudioFlinger::getMicMute() const
794{
Eric Laurenta1884f92011-08-23 08:25:03 -0700795 status_t ret = initCheck();
796 if (ret != NO_ERROR) {
797 return false;
798 }
799
Dima Zavinfce7a472011-04-19 22:30:36 -0700800 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800801 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700802 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700803 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700804 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805 mHardwareStatus = AUDIO_HW_IDLE;
806 return state;
807}
808
809status_t AudioFlinger::setMasterMute(bool muted)
810{
John Grossmand8f178d2012-07-20 14:51:35 -0700811 status_t ret = initCheck();
812 if (ret != NO_ERROR) {
813 return ret;
814 }
815
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 // check calling permissions
817 if (!settingsAllowed()) {
818 return PERMISSION_DENIED;
819 }
820
John Grossmanee578c02012-07-23 17:05:46 -0700821 Mutex::Autolock _l(mLock);
822 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700823
John Grossmanee578c02012-07-23 17:05:46 -0700824 // Set master mute in the HALs which support it.
825 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
826 AutoMutex lock(mHardwareLock);
827 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700828
John Grossmanee578c02012-07-23 17:05:46 -0700829 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
830 if (dev->canSetMasterMute()) {
831 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700832 }
John Grossmanee578c02012-07-23 17:05:46 -0700833 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700834 }
835
John Grossmanee578c02012-07-23 17:05:46 -0700836 // Now set the master mute in each playback thread. Playback threads
837 // assigned to HALs which do not have master mute support will apply master
838 // mute during the mix operation. Threads with HALs which do support master
839 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800840 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700841 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700842
843 return NO_ERROR;
844}
845
846float AudioFlinger::masterVolume() const
847{
Glenn Kasten98067102011-12-13 11:47:54 -0800848 Mutex::Autolock _l(mLock);
849 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850}
851
852bool AudioFlinger::masterMute() const
853{
Glenn Kasten98067102011-12-13 11:47:54 -0800854 Mutex::Autolock _l(mLock);
855 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856}
857
John Grossman4ff14ba2012-02-08 16:37:41 -0800858float AudioFlinger::masterVolume_l() const
859{
John Grossman4ff14ba2012-02-08 16:37:41 -0800860 return mMasterVolume;
861}
862
John Grossmand8f178d2012-07-20 14:51:35 -0700863bool AudioFlinger::masterMute_l() const
864{
John Grossmanee578c02012-07-23 17:05:46 -0700865 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700866}
867
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800868status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
869 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870{
871 // check calling permissions
872 if (!settingsAllowed()) {
873 return PERMISSION_DENIED;
874 }
875
Glenn Kasten263709e2012-01-06 08:40:01 -0800876 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000877 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878 return BAD_VALUE;
879 }
880
881 AutoMutex lock(mLock);
882 PlaybackThread *thread = NULL;
883 if (output) {
884 thread = checkPlaybackThread_l(output);
885 if (thread == NULL) {
886 return BAD_VALUE;
887 }
888 }
889
890 mStreamTypes[stream].volume = value;
891
892 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800893 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700894 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895 }
896 } else {
897 thread->setStreamVolume(stream, value);
898 }
899
900 return NO_ERROR;
901}
902
Glenn Kastenfff6d712012-01-12 16:38:12 -0800903status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700904{
905 // check calling permissions
906 if (!settingsAllowed()) {
907 return PERMISSION_DENIED;
908 }
909
Glenn Kasten263709e2012-01-06 08:40:01 -0800910 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700911 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000912 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913 return BAD_VALUE;
914 }
915
Eric Laurent93575202011-01-18 18:39:02 -0800916 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917 mStreamTypes[stream].mute = muted;
918 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700919 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920
921 return NO_ERROR;
922}
923
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800924float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925{
Glenn Kasten263709e2012-01-06 08:40:01 -0800926 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927 return 0.0f;
928 }
929
930 AutoMutex lock(mLock);
931 float volume;
932 if (output) {
933 PlaybackThread *thread = checkPlaybackThread_l(output);
934 if (thread == NULL) {
935 return 0.0f;
936 }
937 volume = thread->streamVolume(stream);
938 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800939 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 }
941
942 return volume;
943}
944
Glenn Kastenfff6d712012-01-12 16:38:12 -0800945bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946{
Glenn Kasten263709e2012-01-06 08:40:01 -0800947 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948 return true;
949 }
950
Glenn Kasten6637baa2012-01-09 09:40:36 -0800951 AutoMutex lock(mLock);
952 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953}
954
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800955status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700957 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
958 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800959
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 // check calling permissions
961 if (!settingsAllowed()) {
962 return PERMISSION_DENIED;
963 }
964
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965 // ioHandle == 0 means the parameters are global to the audio hardware interface
966 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700967 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700968 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800969 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700970 AutoMutex lock(mHardwareLock);
971 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
972 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
973 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
974 status_t result = dev->set_parameters(dev, keyValuePairs.string());
975 final_result = result ?: final_result;
976 }
977 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800978 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700979 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
980 AudioParameter param = AudioParameter(keyValuePairs);
981 String8 value;
982 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700983 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
984 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700985 for (size_t i = 0; i < mRecordThreads.size(); i++) {
986 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700987 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700988 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
989 // collect all of the thread's session IDs
990 KeyedVector<int, bool> ids = thread->sessionIds();
991 // suspend effects associated with those session IDs
992 for (size_t j = 0; j < ids.size(); ++j) {
993 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700994 thread->setEffectSuspended(FX_IID_AEC,
995 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700996 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700997 thread->setEffectSuspended(FX_IID_NS,
998 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700999 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001000 }
1001 }
Eric Laurentbee53372011-08-29 12:42:48 -07001002 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001003 }
1004 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001005 String8 screenState;
1006 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1007 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001008 if (isOff != (AudioFlinger::mScreenState & 1)) {
1009 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001010 }
1011 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001012 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001013 }
1014
1015 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1016 // and the thread is exited once the lock is released
1017 sp<ThreadBase> thread;
1018 {
1019 Mutex::Autolock _l(mLock);
1020 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001021 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001023 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001024 // indicate output device change to all input threads for pre processing
1025 AudioParameter param = AudioParameter(keyValuePairs);
1026 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001027 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1028 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001029 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1030 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1031 }
1032 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001033 }
1034 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001035 if (thread != 0) {
1036 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001037 }
1038 return BAD_VALUE;
1039}
1040
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001041String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001043 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1044 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045
Eric Laurenta4c5a552012-03-29 10:12:40 -07001046 Mutex::Autolock _l(mLock);
1047
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001049 String8 out_s8;
1050
Dima Zavin799a70e2011-04-18 16:57:27 -07001051 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001052 char *s;
1053 {
1054 AutoMutex lock(mHardwareLock);
1055 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001056 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001057 s = dev->get_parameters(dev, keys.string());
1058 mHardwareStatus = AUDIO_HW_IDLE;
1059 }
John Grossmanef7740b2012-02-09 11:28:36 -08001060 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001061 free(s);
1062 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001063 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064 }
1065
Mathias Agopian65ab4712010-07-14 17:59:35 -07001066 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1067 if (playbackThread != NULL) {
1068 return playbackThread->getParameters(keys);
1069 }
1070 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1071 if (recordThread != NULL) {
1072 return recordThread->getParameters(keys);
1073 }
1074 return String8("");
1075}
1076
Glenn Kastendd8104c2012-07-02 12:42:44 -07001077size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1078 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079{
Eric Laurenta1884f92011-08-23 08:25:03 -07001080 status_t ret = initCheck();
1081 if (ret != NO_ERROR) {
1082 return 0;
1083 }
1084
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001085 AutoMutex lock(mHardwareLock);
1086 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001087 struct audio_config config;
1088 memset(&config, 0, sizeof(config));
1089 config.sample_rate = sampleRate;
1090 config.channel_mask = channelMask;
1091 config.format = format;
1092
John Grossmanee578c02012-07-23 17:05:46 -07001093 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1094 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001095 mHardwareStatus = AUDIO_HW_IDLE;
1096 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001097}
1098
Glenn Kasten5f972c02014-01-13 09:59:31 -08001099uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001100{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101 Mutex::Autolock _l(mLock);
1102
1103 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1104 if (recordThread != NULL) {
1105 return recordThread->getInputFramesLost();
1106 }
1107 return 0;
1108}
1109
1110status_t AudioFlinger::setVoiceVolume(float value)
1111{
Eric Laurenta1884f92011-08-23 08:25:03 -07001112 status_t ret = initCheck();
1113 if (ret != NO_ERROR) {
1114 return ret;
1115 }
1116
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117 // check calling permissions
1118 if (!settingsAllowed()) {
1119 return PERMISSION_DENIED;
1120 }
1121
1122 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001123 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001124 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001125 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001126 mHardwareStatus = AUDIO_HW_IDLE;
1127
1128 return ret;
1129}
1130
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001131status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001132 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133{
1134 status_t status;
1135
1136 Mutex::Autolock _l(mLock);
1137
1138 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1139 if (playbackThread != NULL) {
1140 return playbackThread->getRenderPosition(halFrames, dspFrames);
1141 }
1142
1143 return BAD_VALUE;
1144}
1145
1146void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1147{
1148
1149 Mutex::Autolock _l(mLock);
1150
Glenn Kastenbb001922012-02-03 11:10:26 -08001151 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152 if (mNotificationClients.indexOfKey(pid) < 0) {
1153 sp<NotificationClient> notificationClient = new NotificationClient(this,
1154 client,
1155 pid);
Steve Block3856b092011-10-20 11:56:00 +01001156 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001157
1158 mNotificationClients.add(pid, notificationClient);
1159
1160 sp<IBinder> binder = client->asBinder();
1161 binder->linkToDeath(notificationClient);
1162
1163 // the config change is always sent from playback or record threads to avoid deadlock
1164 // with AudioSystem::gLock
1165 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001166 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167 }
1168
1169 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001170 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001171 }
1172 }
1173}
1174
1175void AudioFlinger::removeNotificationClient(pid_t pid)
1176{
1177 Mutex::Autolock _l(mLock);
1178
Glenn Kastena3b09252012-01-20 09:19:01 -08001179 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001180
Steve Block3856b092011-10-20 11:56:00 +01001181 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001182 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001183 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001184 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001185 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001186 ALOGV(" pid %d @ %d", ref->mPid, i);
1187 if (ref->mPid == pid) {
1188 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001189 mAudioSessionRefs.removeAt(i);
1190 delete ref;
1191 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001192 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001193 } else {
1194 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001195 }
1196 }
1197 if (removed) {
1198 purgeStaleEffects_l();
1199 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001200}
1201
1202// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001203void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204{
1205 size_t size = mNotificationClients.size();
1206 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001207 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1208 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001209 }
1210}
1211
1212// removeClient_l() must be called with AudioFlinger::mLock held
1213void AudioFlinger::removeClient_l(pid_t pid)
1214{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001215 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001216 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217 mClients.removeItem(pid);
1218}
1219
Eric Laurent717e1282012-06-29 16:36:52 -07001220// getEffectThread_l() must be called with AudioFlinger::mLock held
1221sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1222{
1223 sp<PlaybackThread> thread;
1224
1225 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1226 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1227 ALOG_ASSERT(thread == 0);
1228 thread = mPlaybackThreads.valueAt(i);
1229 }
1230 }
1231
1232 return thread;
1233}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001234
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236
1237// ----------------------------------------------------------------------------
1238
1239AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1240 : RefBase(),
1241 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001242 // 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 -07001243 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001244 mPid(pid),
1245 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001246{
1247 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1248}
1249
1250// Client destructor must be called with AudioFlinger::mLock held
1251AudioFlinger::Client::~Client()
1252{
1253 mAudioFlinger->removeClient_l(mPid);
1254}
1255
Glenn Kasten435dbe62012-01-30 10:15:48 -08001256sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257{
1258 return mMemoryDealer;
1259}
1260
John Grossman4ff14ba2012-02-08 16:37:41 -08001261// Reserve one of the limited slots for a timed audio track associated
1262// with this client
1263bool AudioFlinger::Client::reserveTimedTrack()
1264{
1265 const int kMaxTimedTracksPerClient = 4;
1266
1267 Mutex::Autolock _l(mTimedTrackLock);
1268
1269 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1270 ALOGW("can not create timed track - pid %d has exceeded the limit",
1271 mPid);
1272 return false;
1273 }
1274
1275 mTimedTrackCount++;
1276 return true;
1277}
1278
1279// Release a slot for a timed audio track
1280void AudioFlinger::Client::releaseTimedTrack()
1281{
1282 Mutex::Autolock _l(mTimedTrackLock);
1283 mTimedTrackCount--;
1284}
1285
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286// ----------------------------------------------------------------------------
1287
1288AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1289 const sp<IAudioFlingerClient>& client,
1290 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001291 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001292{
1293}
1294
1295AudioFlinger::NotificationClient::~NotificationClient()
1296{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001297}
1298
Glenn Kasten0f11b512014-01-31 16:18:54 -08001299void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300{
1301 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001302 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001303}
1304
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305
1306// ----------------------------------------------------------------------------
1307
Jeff Brown893a5642013-08-16 20:19:26 -07001308static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1309 return audio_is_remote_submix_device(inDevice);
1310}
1311
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001313 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001315 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001316 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001317 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001318 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001319 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320 int *sessionId,
1321 status_t *status)
1322{
1323 sp<RecordThread::RecordTrack> recordTrack;
1324 sp<RecordHandle> recordHandle;
1325 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001326 status_t lStatus;
1327 RecordThread *thread;
1328 size_t inFrameCount;
1329 int lSessionId;
1330
1331 // check calling permissions
1332 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001333 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001334 lStatus = PERMISSION_DENIED;
1335 goto Exit;
1336 }
1337
Glenn Kasten53b5d092014-02-05 10:00:23 -08001338 // further sample rate checks are performed by createRecordTrack_l()
1339 if (sampleRate == 0) {
1340 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1341 lStatus = BAD_VALUE;
1342 goto Exit;
1343 }
1344
1345 // FIXME when we support more formats, add audio_is_valid_format(format)
1346 // and any explicit restrictions if audio_is_linear_pcm(format)
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001347 if (format != AUDIO_FORMAT_PCM_16_BIT) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001348 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001349 lStatus = BAD_VALUE;
1350 goto Exit;
1351 }
1352
Glenn Kasten53b5d092014-02-05 10:00:23 -08001353 // further channel mask checks are performed by createRecordTrack_l()
1354 if (!audio_is_input_channel(channelMask)) {
1355 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1356 lStatus = BAD_VALUE;
1357 goto Exit;
1358 }
1359
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360 // add client to list
1361 { // scope for mLock
1362 Mutex::Autolock _l(mLock);
1363 thread = checkRecordThread_l(input);
1364 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001365 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001366 lStatus = BAD_VALUE;
1367 goto Exit;
1368 }
1369
Jeff Brown893a5642013-08-16 20:19:26 -07001370 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1371 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001372 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001373 lStatus = PERMISSION_DENIED;
1374 goto Exit;
1375 }
1376
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001377 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001378 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001379
1380 // If no audio session id is provided, create one here
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001381 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001382 lSessionId = *sessionId;
1383 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001384 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001385 if (sessionId != NULL) {
1386 *sessionId = lSessionId;
1387 }
1388 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001389 // create new record track.
1390 // The record track uses one track in mHardwareMixerThread by convention.
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001391 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001392 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001393 frameCount, lSessionId,
1394 IPCThreadState::self()->getCallingUid(),
1395 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001396 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001397 }
Glenn Kastene198c362013-08-13 09:13:36 -07001398
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001399 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001400 // remove local strong reference to Client before deleting the RecordTrack so that the
1401 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402 client.clear();
1403 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404 goto Exit;
1405 }
1406
Glenn Kasten2fc14732013-08-05 14:58:14 -07001407 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409
1410Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001411 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412 return recordHandle;
1413}
1414
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001415
1416
Mathias Agopian65ab4712010-07-14 17:59:35 -07001417// ----------------------------------------------------------------------------
1418
Eric Laurenta4c5a552012-03-29 10:12:40 -07001419audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1420{
1421 if (!settingsAllowed()) {
1422 return 0;
1423 }
1424 Mutex::Autolock _l(mLock);
1425 return loadHwModule_l(name);
1426}
1427
1428// loadHwModule_l() must be called with AudioFlinger::mLock held
1429audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1430{
1431 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1432 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1433 ALOGW("loadHwModule() module %s already loaded", name);
1434 return mAudioHwDevs.keyAt(i);
1435 }
1436 }
1437
Eric Laurenta4c5a552012-03-29 10:12:40 -07001438 audio_hw_device_t *dev;
1439
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001440 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001441 if (rc) {
1442 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1443 return 0;
1444 }
1445
1446 mHardwareStatus = AUDIO_HW_INIT;
1447 rc = dev->init_check(dev);
1448 mHardwareStatus = AUDIO_HW_IDLE;
1449 if (rc) {
1450 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1451 return 0;
1452 }
1453
John Grossmanee578c02012-07-23 17:05:46 -07001454 // Check and cache this HAL's level of support for master mute and master
1455 // volume. If this is the first HAL opened, and it supports the get
1456 // methods, use the initial values provided by the HAL as the current
1457 // master mute and volume settings.
1458
1459 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1460 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001461 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001462
1463 if (0 == mAudioHwDevs.size()) {
1464 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1465 if (NULL != dev->get_master_volume) {
1466 float mv;
1467 if (OK == dev->get_master_volume(dev, &mv)) {
1468 mMasterVolume = mv;
1469 }
1470 }
1471
1472 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1473 if (NULL != dev->get_master_mute) {
1474 bool mm;
1475 if (OK == dev->get_master_mute(dev, &mm)) {
1476 mMasterMute = mm;
1477 }
1478 }
1479 }
1480
Eric Laurenta4c5a552012-03-29 10:12:40 -07001481 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001482 if ((NULL != dev->set_master_volume) &&
1483 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1484 flags = static_cast<AudioHwDevice::Flags>(flags |
1485 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1486 }
1487
1488 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1489 if ((NULL != dev->set_master_mute) &&
1490 (OK == dev->set_master_mute(dev, mMasterMute))) {
1491 flags = static_cast<AudioHwDevice::Flags>(flags |
1492 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1493 }
1494
Eric Laurenta4c5a552012-03-29 10:12:40 -07001495 mHardwareStatus = AUDIO_HW_IDLE;
1496 }
1497
1498 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001499 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001500
1501 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001502 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001503
1504 return handle;
1505
1506}
1507
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001508// ----------------------------------------------------------------------------
1509
Glenn Kasten3b16c762012-11-14 08:44:39 -08001510uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001511{
1512 Mutex::Autolock _l(mLock);
1513 PlaybackThread *thread = primaryPlaybackThread_l();
1514 return thread != NULL ? thread->sampleRate() : 0;
1515}
1516
Glenn Kastene33054e2012-11-14 12:54:39 -08001517size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001518{
1519 Mutex::Autolock _l(mLock);
1520 PlaybackThread *thread = primaryPlaybackThread_l();
1521 return thread != NULL ? thread->frameCountHAL() : 0;
1522}
1523
1524// ----------------------------------------------------------------------------
1525
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001526status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1527{
1528 uid_t uid = IPCThreadState::self()->getCallingUid();
1529 if (uid != AID_SYSTEM) {
1530 return PERMISSION_DENIED;
1531 }
1532 Mutex::Autolock _l(mLock);
1533 if (mIsDeviceTypeKnown) {
1534 return INVALID_OPERATION;
1535 }
1536 mIsLowRamDevice = isLowRamDevice;
1537 mIsDeviceTypeKnown = true;
1538 return NO_ERROR;
1539}
1540
1541// ----------------------------------------------------------------------------
1542
Eric Laurenta4c5a552012-03-29 10:12:40 -07001543audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1544 audio_devices_t *pDevices,
1545 uint32_t *pSamplingRate,
1546 audio_format_t *pFormat,
1547 audio_channel_mask_t *pChannelMask,
1548 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001549 audio_output_flags_t flags,
1550 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001552 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001553 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001554 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1555 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1556 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001557 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001558 config.offload_info = *offloadInfo;
1559 }
1560
Eric Laurentbfb1b832013-01-07 09:53:42 -08001561 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001562 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001563 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001564 config.sample_rate,
1565 config.format,
1566 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001567 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001568 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001569 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001570
1571 if (pDevices == NULL || *pDevices == 0) {
1572 return 0;
1573 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001574
Mathias Agopian65ab4712010-07-14 17:59:35 -07001575 Mutex::Autolock _l(mLock);
1576
Glenn Kasten32550952013-08-06 10:45:10 -07001577 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001578 if (outHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001579 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001580 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001581
John Grossmanee578c02012-07-23 17:05:46 -07001582 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001583 audio_io_handle_t id = nextUniqueId();
1584
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001585 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001586
Glenn Kasten32550952013-08-06 10:45:10 -07001587 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001588 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001589 id,
1590 *pDevices,
1591 (audio_output_flags_t)flags,
1592 &config,
1593 &outStream);
1594
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001595 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001596 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001597 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001598 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001599 config.sample_rate,
1600 config.format,
1601 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602 status);
1603
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001604 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001605 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001606
Glenn Kasten32550952013-08-06 10:45:10 -07001607 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001608 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1609 thread = new OffloadThread(this, output, id, *pDevices);
1610 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1611 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001612 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1613 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001615 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001616 } else {
1617 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001618 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001619 }
1620 mPlaybackThreads.add(id, thread);
1621
Glenn Kasten7c027242012-12-26 14:43:16 -08001622 if (pSamplingRate != NULL) {
1623 *pSamplingRate = config.sample_rate;
1624 }
1625 if (pFormat != NULL) {
1626 *pFormat = config.format;
1627 }
1628 if (pChannelMask != NULL) {
1629 *pChannelMask = config.channel_mask;
1630 }
1631 if (pLatencyMs != NULL) {
1632 *pLatencyMs = thread->latency();
1633 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001634
1635 // notify client processes of the new output creation
1636 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001637
1638 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001639 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001640 ALOGI("Using module %d has the primary audio interface", module);
1641 mPrimaryHardwareDev = outHwDev;
1642
1643 AutoMutex lock(mHardwareLock);
1644 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001645 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001646 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001647 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648 return id;
1649 }
1650
1651 return 0;
1652}
1653
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001654audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1655 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001656{
1657 Mutex::Autolock _l(mLock);
1658 MixerThread *thread1 = checkMixerThread_l(output1);
1659 MixerThread *thread2 = checkMixerThread_l(output2);
1660
1661 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001662 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1663 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001664 return 0;
1665 }
1666
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001667 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001668 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1669 thread->addOutputTrack(thread2);
1670 mPlaybackThreads.add(id, thread);
1671 // notify client processes of the new output creation
1672 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1673 return id;
1674}
1675
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001676status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001677{
Glenn Kastend96c5722012-04-25 13:44:49 -07001678 return closeOutput_nonvirtual(output);
1679}
1680
1681status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1682{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001683 // keep strong reference on the playback thread so that
1684 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001685 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001686 {
1687 Mutex::Autolock _l(mLock);
1688 thread = checkPlaybackThread_l(output);
1689 if (thread == NULL) {
1690 return BAD_VALUE;
1691 }
1692
Steve Block3856b092011-10-20 11:56:00 +01001693 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001694
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001695 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001696 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001697 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001698 DuplicatingThread *dupThread =
1699 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001700 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001701
1702 }
1703 }
1704 }
1705
1706
1707 mPlaybackThreads.removeItem(output);
1708 // save all effects to the default thread
1709 if (mPlaybackThreads.size()) {
1710 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1711 if (dstThread != NULL) {
1712 // audioflinger lock is held here so the acquisition order of thread locks does not
1713 // matter
1714 Mutex::Autolock _dl(dstThread->mLock);
1715 Mutex::Autolock _sl(thread->mLock);
1716 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1717 for (size_t i = 0; i < effectChains.size(); i ++) {
1718 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001719 }
1720 }
1721 }
Glenn Kastena1117922012-01-26 10:53:32 -08001722 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001723 }
1724 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001725 // The thread entity (active unit of execution) is no longer running here,
1726 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001727
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001728 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001729 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001730 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001731 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001732 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001733 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001734 }
1735 return NO_ERROR;
1736}
1737
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001738status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001739{
1740 Mutex::Autolock _l(mLock);
1741 PlaybackThread *thread = checkPlaybackThread_l(output);
1742
1743 if (thread == NULL) {
1744 return BAD_VALUE;
1745 }
1746
Steve Block3856b092011-10-20 11:56:00 +01001747 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001748 thread->suspend();
1749
1750 return NO_ERROR;
1751}
1752
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001753status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001754{
1755 Mutex::Autolock _l(mLock);
1756 PlaybackThread *thread = checkPlaybackThread_l(output);
1757
1758 if (thread == NULL) {
1759 return BAD_VALUE;
1760 }
1761
Steve Block3856b092011-10-20 11:56:00 +01001762 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001763
1764 thread->restore();
1765
1766 return NO_ERROR;
1767}
1768
Eric Laurenta4c5a552012-03-29 10:12:40 -07001769audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1770 audio_devices_t *pDevices,
1771 uint32_t *pSamplingRate,
1772 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001773 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001774{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001775 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001776 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001777 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1778 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1779 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1780
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001781 uint32_t reqSamplingRate = config.sample_rate;
1782 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001783 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001784
1785 if (pDevices == NULL || *pDevices == 0) {
1786 return 0;
1787 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001788
Mathias Agopian65ab4712010-07-14 17:59:35 -07001789 Mutex::Autolock _l(mLock);
1790
Glenn Kasten32550952013-08-06 10:45:10 -07001791 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001792 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001793 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001794 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001795
John Grossmanee578c02012-07-23 17:05:46 -07001796 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001797 audio_io_handle_t id = nextUniqueId();
1798
Glenn Kasten32550952013-08-06 10:45:10 -07001799 audio_stream_in_t *inStream = NULL;
1800 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001801 &inStream);
Glenn Kastencac3daa2014-02-07 09:47:14 -08001802 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %#x, Channels %x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001803 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001804 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001805 config.sample_rate,
1806 config.format,
1807 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001808 status);
1809
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001810 // If the input could not be opened with the requested parameters and we can handle the
1811 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1812 // 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 -07001813 if (status == BAD_VALUE &&
1814 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1815 (config.sample_rate <= 2 * reqSamplingRate) &&
Glenn Kastenf506e942013-08-06 10:49:43 -07001816 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001817 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Glenn Kasten254af182012-07-03 14:59:05 -07001818 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001819 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001820 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07001821 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001822 }
1823
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001824 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001825
Glenn Kasten46909e72013-02-26 09:20:22 -08001826#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001827 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1828 // or (re-)create if current Pipe is idle and does not match the new format
1829 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001830 enum {
1831 TEE_SINK_NO, // don't copy input
1832 TEE_SINK_NEW, // copy input using a new pipe
1833 TEE_SINK_OLD, // copy input using an existing pipe
1834 } kind;
1835 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1836 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001837 if (!mTeeSinkInputEnabled) {
1838 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08001839 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001840 kind = TEE_SINK_NO;
1841 } else if (mRecordTeeSink == 0) {
1842 kind = TEE_SINK_NEW;
1843 } else if (mRecordTeeSink->getStrongCount() != 1) {
1844 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08001845 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001846 kind = TEE_SINK_OLD;
1847 } else {
1848 kind = TEE_SINK_NEW;
1849 }
1850 switch (kind) {
1851 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001852 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001853 size_t numCounterOffers = 0;
1854 const NBAIO_Format offers[1] = {format};
1855 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1856 ALOG_ASSERT(index == 0);
1857 PipeReader *pipeReader = new PipeReader(*pipe);
1858 numCounterOffers = 0;
1859 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1860 ALOG_ASSERT(index == 0);
1861 mRecordTeeSink = pipe;
1862 mRecordTeeSource = pipeReader;
1863 teeSink = pipe;
1864 }
1865 break;
1866 case TEE_SINK_OLD:
1867 teeSink = mRecordTeeSink;
1868 break;
1869 case TEE_SINK_NO:
1870 default:
1871 break;
1872 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001873#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001874
Dima Zavin799a70e2011-04-18 16:57:27 -07001875 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1876
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001877 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001878 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001879 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001880 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001881 input,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001882 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001883 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001884 *pDevices
1885#ifdef TEE_SINK
1886 , teeSink
1887#endif
1888 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001889 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001890 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001891 if (pSamplingRate != NULL) {
1892 *pSamplingRate = reqSamplingRate;
1893 }
1894 if (pFormat != NULL) {
1895 *pFormat = config.format;
1896 }
1897 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001898 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001899 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901 // notify client processes of the new input creation
1902 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1903 return id;
1904 }
1905
1906 return 0;
1907}
1908
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001909status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001910{
Glenn Kastend96c5722012-04-25 13:44:49 -07001911 return closeInput_nonvirtual(input);
1912}
1913
1914status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1915{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001916 // keep strong reference on the record thread so that
1917 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001918 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001919 {
1920 Mutex::Autolock _l(mLock);
1921 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001922 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923 return BAD_VALUE;
1924 }
1925
Steve Block3856b092011-10-20 11:56:00 +01001926 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001927 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928 mRecordThreads.removeItem(input);
1929 }
1930 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001931 // The thread entity (active unit of execution) is no longer running here,
1932 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001934 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001935 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001936 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001937 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001938 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939
1940 return NO_ERROR;
1941}
1942
Glenn Kastend2304db2014-02-03 07:40:31 -08001943status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944{
1945 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08001946 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001947
1948 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1949 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001950 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001951 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001952
1953 return NO_ERROR;
1954}
1955
1956
1957int AudioFlinger::newAudioSessionId()
1958{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001959 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001960}
1961
Marco Nelissend457c972014-02-11 08:47:07 -08001962void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001963{
1964 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001965 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08001966 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
1967 if (pid != -1 && (caller == getpid_cached)) {
1968 caller = pid;
1969 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001970
1971 // Ignore requests received from processes not known as notification client. The request
1972 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1973 // called from a different pid leaving a stale session reference. Also we don't know how
1974 // to clear this reference if the client process dies.
1975 if (mNotificationClients.indexOfKey(caller) < 0) {
Marco Nelissend457c972014-02-11 08:47:07 -08001976 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
Eric Laurentd1b28d42013-09-18 18:47:13 -07001977 return;
1978 }
1979
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001980 size_t num = mAudioSessionRefs.size();
1981 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001982 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001983 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1984 ref->mCnt++;
1985 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001986 return;
1987 }
1988 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001989 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1990 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001991}
1992
Marco Nelissend457c972014-02-11 08:47:07 -08001993void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001994{
1995 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001996 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08001997 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
1998 if (pid != -1 && (caller == getpid_cached)) {
1999 caller = pid;
2000 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002001 size_t num = mAudioSessionRefs.size();
2002 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002003 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002004 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2005 ref->mCnt--;
2006 ALOGV(" decremented refcount to %d", ref->mCnt);
2007 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002008 mAudioSessionRefs.removeAt(i);
2009 delete ref;
2010 purgeStaleEffects_l();
2011 }
2012 return;
2013 }
2014 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002015 // If the caller is mediaserver it is likely that the session being released was acquired
2016 // on behalf of a process not in notification clients and we ignore the warning.
2017 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002018}
2019
2020void AudioFlinger::purgeStaleEffects_l() {
2021
Steve Block3856b092011-10-20 11:56:00 +01002022 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002023
2024 Vector< sp<EffectChain> > chains;
2025
2026 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2027 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2028 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2029 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002030 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2031 chains.push(ec);
2032 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002033 }
2034 }
2035 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2036 sp<RecordThread> t = mRecordThreads.valueAt(i);
2037 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2038 sp<EffectChain> ec = t->mEffectChains[j];
2039 chains.push(ec);
2040 }
2041 }
2042
2043 for (size_t i = 0; i < chains.size(); i++) {
2044 sp<EffectChain> ec = chains[i];
2045 int sessionid = ec->sessionId();
2046 sp<ThreadBase> t = ec->mThread.promote();
2047 if (t == 0) {
2048 continue;
2049 }
2050 size_t numsessionrefs = mAudioSessionRefs.size();
2051 bool found = false;
2052 for (size_t k = 0; k < numsessionrefs; k++) {
2053 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002054 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002055 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002056 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002057 found = true;
2058 break;
2059 }
2060 }
2061 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002062 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002063 // remove all effects from the chain
2064 while (ec->mEffects.size()) {
2065 sp<EffectModule> effect = ec->mEffects[0];
2066 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002067 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002068 if (effect->purgeHandles()) {
2069 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002070 }
2071 AudioSystem::unregisterEffect(effect->id());
2072 }
2073 }
2074 }
2075 return;
2076}
2077
Mathias Agopian65ab4712010-07-14 17:59:35 -07002078// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002079AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002080{
Glenn Kastena1117922012-01-26 10:53:32 -08002081 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002082}
2083
2084// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002085AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002086{
2087 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002088 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002089}
2090
2091// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002092AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002093{
Glenn Kastena1117922012-01-26 10:53:32 -08002094 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002095}
2096
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002097uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002098{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002099 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002100}
2101
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002102AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002103{
2104 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2105 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002106 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002107 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002108 return thread;
2109 }
2110 }
2111 return NULL;
2112}
2113
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002114audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002115{
2116 PlaybackThread *thread = primaryPlaybackThread_l();
2117
2118 if (thread == NULL) {
2119 return 0;
2120 }
2121
Eric Laurentf1c04f92012-08-28 14:26:53 -07002122 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002123}
2124
Eric Laurenta011e352012-03-29 15:51:43 -07002125sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2126 int triggerSession,
2127 int listenerSession,
2128 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002129 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002130{
2131 Mutex::Autolock _l(mLock);
2132
2133 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2134 status_t playStatus = NAME_NOT_FOUND;
2135 status_t recStatus = NAME_NOT_FOUND;
2136 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2137 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2138 if (playStatus == NO_ERROR) {
2139 return event;
2140 }
2141 }
2142 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2143 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2144 if (recStatus == NO_ERROR) {
2145 return event;
2146 }
2147 }
2148 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2149 mPendingSyncEvents.add(event);
2150 } else {
2151 ALOGV("createSyncEvent() invalid event %d", event->type());
2152 event.clear();
2153 }
2154 return event;
2155}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002156
Mathias Agopian65ab4712010-07-14 17:59:35 -07002157// ----------------------------------------------------------------------------
2158// Effect management
2159// ----------------------------------------------------------------------------
2160
2161
Glenn Kastenf587ba52012-01-26 16:25:10 -08002162status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002163{
2164 Mutex::Autolock _l(mLock);
2165 return EffectQueryNumberEffects(numEffects);
2166}
2167
Glenn Kastenf587ba52012-01-26 16:25:10 -08002168status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002169{
2170 Mutex::Autolock _l(mLock);
2171 return EffectQueryEffect(index, descriptor);
2172}
2173
Glenn Kasten5e92a782012-01-30 07:40:52 -08002174status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002175 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002176{
2177 Mutex::Autolock _l(mLock);
2178 return EffectGetDescriptor(pUuid, descriptor);
2179}
2180
2181
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002182sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002183 effect_descriptor_t *pDesc,
2184 const sp<IEffectClient>& effectClient,
2185 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002186 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002187 int sessionId,
2188 status_t *status,
2189 int *id,
2190 int *enabled)
2191{
2192 status_t lStatus = NO_ERROR;
2193 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002194 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002195
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002196 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002197 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002198 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002199
2200 if (pDesc == NULL) {
2201 lStatus = BAD_VALUE;
2202 goto Exit;
2203 }
2204
Eric Laurent84e9a102010-09-23 16:10:16 -07002205 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002206 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002207 lStatus = PERMISSION_DENIED;
2208 goto Exit;
2209 }
2210
Dima Zavinfce7a472011-04-19 22:30:36 -07002211 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002212 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002213 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002214 lStatus = PERMISSION_DENIED;
2215 goto Exit;
2216 }
2217
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002219 if (!EffectIsNullUuid(&pDesc->uuid)) {
2220 // if uuid is specified, request effect descriptor
2221 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2222 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002223 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002224 goto Exit;
2225 }
2226 } else {
2227 // if uuid is not specified, look for an available implementation
2228 // of the required type in effect factory
2229 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002230 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002231 lStatus = BAD_VALUE;
2232 goto Exit;
2233 }
2234 uint32_t numEffects = 0;
2235 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002236 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237 bool found = false;
2238
2239 lStatus = EffectQueryNumberEffects(&numEffects);
2240 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002241 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002242 goto Exit;
2243 }
2244 for (uint32_t i = 0; i < numEffects; i++) {
2245 lStatus = EffectQueryEffect(i, &desc);
2246 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002247 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002248 continue;
2249 }
2250 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2251 // If matching type found save effect descriptor. If the session is
2252 // 0 and the effect is not auxiliary, continue enumeration in case
2253 // an auxiliary version of this effect type is available
2254 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002255 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002256 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002257 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2258 break;
2259 }
2260 }
2261 }
2262 if (!found) {
2263 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002264 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002265 goto Exit;
2266 }
2267 // For same effect type, chose auxiliary version over insert version if
2268 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002269 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002270 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002271 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002272 }
2273 }
2274
2275 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002276 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002277 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2278 lStatus = INVALID_OPERATION;
2279 goto Exit;
2280 }
2281
Eric Laurent59255e42011-07-27 19:49:51 -07002282 // check recording permission for visualizer
2283 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2284 !recordingAllowed()) {
2285 lStatus = PERMISSION_DENIED;
2286 goto Exit;
2287 }
2288
Mathias Agopian65ab4712010-07-14 17:59:35 -07002289 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002290 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002291 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2292 // if the output returned by getOutputForEffect() is removed before we lock the
2293 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2294 // and we will exit safely
2295 io = AudioSystem::getOutputForEffect(&desc);
2296 ALOGV("createEffect got output %d", io);
2297 }
2298
2299 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002300
2301 // If output is not specified try to find a matching audio session ID in one of the
2302 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002303 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2304 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002305 // Note: io is never 0 when creating an effect on an input
2306 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002307 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2308 // output must be specified by AudioPolicyManager when using session
2309 // AUDIO_SESSION_OUTPUT_STAGE
2310 lStatus = BAD_VALUE;
2311 goto Exit;
2312 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002313 // look for the thread where the specified audio session is present
2314 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2315 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2316 io = mPlaybackThreads.keyAt(i);
2317 break;
2318 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002319 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002320 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002321 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2322 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2323 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002324 break;
2325 }
2326 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002327 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002328 // If no output thread contains the requested session ID, default to
2329 // first output. The effect chain will be moved to the correct output
2330 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002331 if (io == 0 && mPlaybackThreads.size()) {
2332 io = mPlaybackThreads.keyAt(0);
2333 }
Steve Block3856b092011-10-20 11:56:00 +01002334 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002335 }
2336 ThreadBase *thread = checkRecordThread_l(io);
2337 if (thread == NULL) {
2338 thread = checkPlaybackThread_l(io);
2339 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002340 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002341 lStatus = BAD_VALUE;
2342 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002343 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002344 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002345
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002346 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002347
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002348 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002349 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2350 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002351 if (handle != 0 && id != NULL) {
2352 *id = handle->id();
2353 }
2354 }
2355
2356Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002357 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002358 return handle;
2359}
2360
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002361status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2362 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002363{
Steve Block3856b092011-10-20 11:56:00 +01002364 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002365 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002366 Mutex::Autolock _l(mLock);
2367 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002368 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002369 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002370 }
Eric Laurentde070132010-07-13 04:45:46 -07002371 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2372 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002373 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002374 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002375 }
Eric Laurentde070132010-07-13 04:45:46 -07002376 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2377 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002378 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002379 return BAD_VALUE;
2380 }
2381
2382 Mutex::Autolock _dl(dstThread->mLock);
2383 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002384 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002385}
2386
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002387// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002388status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002389 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002390 AudioFlinger::PlaybackThread *dstThread,
2391 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002392{
Steve Block3856b092011-10-20 11:56:00 +01002393 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002394 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002395
Eric Laurent59255e42011-07-27 19:49:51 -07002396 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002397 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002398 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002399 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002400 return INVALID_OPERATION;
2401 }
2402
Eric Laurent39e94f82010-07-28 01:32:47 -07002403 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002404 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002405 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002406 // removed.
2407 srcThread->removeEffectChain_l(chain);
2408
2409 // transfer all effects one by one so that new effect chain is created on new thread with
2410 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002411 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002412 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002413 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002414 Vector< sp<EffectModule> > removed;
2415 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002416 while (effect != 0) {
2417 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002418 removed.add(effect);
2419 status = dstThread->addEffect_l(effect);
2420 if (status != NO_ERROR) {
2421 break;
2422 }
Eric Laurentec35a142011-10-05 17:42:25 -07002423 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2424 if (effect->state() == EffectModule::ACTIVE ||
2425 effect->state() == EffectModule::STOPPING) {
2426 effect->start();
2427 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002428 // if the move request is not received from audio policy manager, the effect must be
2429 // re-registered with the new strategy and output
2430 if (dstChain == 0) {
2431 dstChain = effect->chain().promote();
2432 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002433 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002434 status = NO_INIT;
2435 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002436 }
2437 strategy = dstChain->strategy();
2438 }
2439 if (reRegister) {
2440 AudioSystem::unregisterEffect(effect->id());
2441 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002442 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002443 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002444 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002445 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002446 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002447 }
Eric Laurentde070132010-07-13 04:45:46 -07002448 effect = chain->getEffectFromId_l(0);
2449 }
2450
Eric Laurent5baf2af2013-09-12 17:37:00 -07002451 if (status != NO_ERROR) {
2452 for (size_t i = 0; i < removed.size(); i++) {
2453 srcThread->addEffect_l(removed[i]);
2454 if (dstChain != 0 && reRegister) {
2455 AudioSystem::unregisterEffect(removed[i]->id());
2456 AudioSystem::registerEffect(&removed[i]->desc(),
2457 srcThread->id(),
2458 strategy,
2459 sessionId,
2460 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002461 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002462 }
2463 }
2464 }
2465
2466 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002467}
2468
Eric Laurent5baf2af2013-09-12 17:37:00 -07002469bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002470{
2471 if (mGlobalEffectEnableTime != 0 &&
2472 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2473 return true;
2474 }
2475
2476 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2477 sp<EffectChain> ec =
2478 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002479 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002480 return true;
2481 }
2482 }
2483 return false;
2484}
2485
Eric Laurent5baf2af2013-09-12 17:37:00 -07002486void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002487{
2488 Mutex::Autolock _l(mLock);
2489
2490 mGlobalEffectEnableTime = systemTime();
2491
2492 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2493 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2494 if (t->mType == ThreadBase::OFFLOAD) {
2495 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2496 }
2497 }
2498
2499}
2500
Glenn Kastenda6ef132013-01-10 12:31:01 -08002501struct Entry {
2502#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2503 char mName[MAX_NAME];
2504};
2505
2506int comparEntry(const void *p1, const void *p2)
2507{
2508 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2509}
2510
Glenn Kasten46909e72013-02-26 09:20:22 -08002511#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002512void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002513{
Eric Laurent81784c32012-11-19 14:55:58 -08002514 NBAIO_Source *teeSource = source.get();
2515 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002516 // .wav rotation
2517 // There is a benign race condition if 2 threads call this simultaneously.
2518 // They would both traverse the directory, but the result would simply be
2519 // failures at unlink() which are ignored. It's also unlikely since
2520 // normally dumpsys is only done by bugreport or from the command line.
2521 char teePath[32+256];
2522 strcpy(teePath, "/data/misc/media");
2523 size_t teePathLen = strlen(teePath);
2524 DIR *dir = opendir(teePath);
2525 teePath[teePathLen++] = '/';
2526 if (dir != NULL) {
2527#define MAX_SORT 20 // number of entries to sort
2528#define MAX_KEEP 10 // number of entries to keep
2529 struct Entry entries[MAX_SORT];
2530 size_t entryCount = 0;
2531 while (entryCount < MAX_SORT) {
2532 struct dirent de;
2533 struct dirent *result = NULL;
2534 int rc = readdir_r(dir, &de, &result);
2535 if (rc != 0) {
2536 ALOGW("readdir_r failed %d", rc);
2537 break;
2538 }
2539 if (result == NULL) {
2540 break;
2541 }
2542 if (result != &de) {
2543 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2544 break;
2545 }
2546 // ignore non .wav file entries
2547 size_t nameLen = strlen(de.d_name);
2548 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2549 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2550 continue;
2551 }
2552 strcpy(entries[entryCount++].mName, de.d_name);
2553 }
2554 (void) closedir(dir);
2555 if (entryCount > MAX_KEEP) {
2556 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2557 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2558 strcpy(&teePath[teePathLen], entries[i].mName);
2559 (void) unlink(teePath);
2560 }
2561 }
2562 } else {
2563 if (fd >= 0) {
2564 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2565 }
2566 }
Eric Laurent81784c32012-11-19 14:55:58 -08002567 char teeTime[16];
2568 struct timeval tv;
2569 gettimeofday(&tv, NULL);
2570 struct tm tm;
2571 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002572 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2573 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2574 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2575 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002576 if (teeFd >= 0) {
2577 char wavHeader[44];
2578 memcpy(wavHeader,
2579 "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",
2580 sizeof(wavHeader));
2581 NBAIO_Format format = teeSource->format();
2582 unsigned channelCount = Format_channelCount(format);
2583 ALOG_ASSERT(channelCount <= FCC_2);
2584 uint32_t sampleRate = Format_sampleRate(format);
2585 wavHeader[22] = channelCount; // number of channels
2586 wavHeader[24] = sampleRate; // sample rate
2587 wavHeader[25] = sampleRate >> 8;
2588 wavHeader[32] = channelCount * 2; // block alignment
2589 write(teeFd, wavHeader, sizeof(wavHeader));
2590 size_t total = 0;
2591 bool firstRead = true;
2592 for (;;) {
2593#define TEE_SINK_READ 1024
2594 short buffer[TEE_SINK_READ * FCC_2];
2595 size_t count = TEE_SINK_READ;
2596 ssize_t actual = teeSource->read(buffer, count,
2597 AudioBufferProvider::kInvalidPTS);
2598 bool wasFirstRead = firstRead;
2599 firstRead = false;
2600 if (actual <= 0) {
2601 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2602 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002603 }
Eric Laurent81784c32012-11-19 14:55:58 -08002604 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002605 }
Eric Laurent81784c32012-11-19 14:55:58 -08002606 ALOG_ASSERT(actual <= (ssize_t)count);
2607 write(teeFd, buffer, actual * channelCount * sizeof(short));
2608 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002609 }
Eric Laurent81784c32012-11-19 14:55:58 -08002610 lseek(teeFd, (off_t) 4, SEEK_SET);
2611 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2612 write(teeFd, &temp, sizeof(temp));
2613 lseek(teeFd, (off_t) 40, SEEK_SET);
2614 temp = total * channelCount * sizeof(short);
2615 write(teeFd, &temp, sizeof(temp));
2616 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002617 if (fd >= 0) {
2618 fdprintf(fd, "tee copied to %s\n", teePath);
2619 }
Eric Laurent59255e42011-07-27 19:49:51 -07002620 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002621 if (fd >= 0) {
2622 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2623 }
Eric Laurent59255e42011-07-27 19:49:51 -07002624 }
2625 }
2626}
Glenn Kasten46909e72013-02-26 09:20:22 -08002627#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002628
Mathias Agopian65ab4712010-07-14 17:59:35 -07002629// ----------------------------------------------------------------------------
2630
2631status_t AudioFlinger::onTransact(
2632 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2633{
2634 return BnAudioFlinger::onTransact(code, data, reply, flags);
2635}
2636
Mathias Agopian65ab4712010-07-14 17:59:35 -07002637}; // namespace android