blob: 6f2dce3d76712524eaba34aa3a474704d2650691 [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 Kasten6e2ebe92013-08-13 09:14:51 -0700186 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800187 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700188 }
189 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800190 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700191 }
192 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800193 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700194 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800195#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700196}
197
198void AudioFlinger::onFirstRef()
199{
Dima Zavin799a70e2011-04-18 16:57:27 -0700200 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700201
Eric Laurent93575202011-01-18 18:39:02 -0800202 Mutex::Autolock _l(mLock);
203
Dima Zavin799a70e2011-04-18 16:57:27 -0700204 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800205 char val_str[PROPERTY_VALUE_MAX] = { 0 };
206 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
207 uint32_t int_val;
208 if (1 == sscanf(val_str, "%u", &int_val)) {
209 mStandbyTimeInNsecs = milliseconds(int_val);
210 ALOGI("Using %u mSec as standby time.", int_val);
211 } else {
212 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
213 ALOGI("Using default %u mSec as standby time.",
214 (uint32_t)(mStandbyTimeInNsecs / 1000000));
215 }
216 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217
Eric Laurenta4c5a552012-03-29 10:12:40 -0700218 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700219}
220
221AudioFlinger::~AudioFlinger()
222{
223 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700224 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700225 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226 }
227 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700228 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700229 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700231
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800232 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
233 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700234 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
235 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700237
238 // Tell media.log service about any old writers that still need to be unregistered
239 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
240 if (binder != 0) {
241 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
242 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
243 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
244 mUnregisteredWriters.pop();
245 mediaLogService->unregisterWriter(iMemory);
246 }
247 }
248
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249}
250
Eric Laurenta4c5a552012-03-29 10:12:40 -0700251static const char * const audio_interfaces[] = {
252 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
253 AUDIO_HARDWARE_MODULE_ID_A2DP,
254 AUDIO_HARDWARE_MODULE_ID_USB,
255};
256#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
257
John Grossmanee578c02012-07-23 17:05:46 -0700258AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
259 audio_module_handle_t module,
260 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700261{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700262 // if module is 0, the request comes from an old policy manager and we should load
263 // well known modules
264 if (module == 0) {
265 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
266 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
267 loadHwModule_l(audio_interfaces[i]);
268 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700269 // then try to find a module supporting the requested device.
270 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
271 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
272 audio_hw_device_t *dev = audioHwDevice->hwDevice();
273 if ((dev->get_supported_devices != NULL) &&
274 (dev->get_supported_devices(dev) & devices) == devices)
275 return audioHwDevice;
276 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700277 } else {
278 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700279 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
280 if (audioHwDevice != NULL) {
281 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700282 }
283 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700284
Dima Zavin799a70e2011-04-18 16:57:27 -0700285 return NULL;
286}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287
Glenn Kasten0f11b512014-01-31 16:18:54 -0800288void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289{
290 const size_t SIZE = 256;
291 char buffer[SIZE];
292 String8 result;
293
294 result.append("Clients:\n");
295 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800296 sp<Client> client = mClients.valueAt(i).promote();
297 if (client != 0) {
298 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
299 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300 }
301 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700302
Eric Laurentd1b28d42013-09-18 18:47:13 -0700303 result.append("Notification Clients:\n");
304 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
305 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
306 result.append(buffer);
307 }
308
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700309 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800310 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700311 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
312 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800313 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700314 result.append(buffer);
315 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317}
318
319
Glenn Kasten0f11b512014-01-31 16:18:54 -0800320void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700321{
322 const size_t SIZE = 256;
323 char buffer[SIZE];
324 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800325 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326
John Grossman4ff14ba2012-02-08 16:37:41 -0800327 snprintf(buffer, SIZE, "Hardware status: %d\n"
328 "Standby Time mSec: %u\n",
329 hardwareStatus,
330 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331 result.append(buffer);
332 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333}
334
Glenn Kasten0f11b512014-01-31 16:18:54 -0800335void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336{
337 const size_t SIZE = 256;
338 char buffer[SIZE];
339 String8 result;
340 snprintf(buffer, SIZE, "Permission Denial: "
341 "can't dump AudioFlinger from pid=%d, uid=%d\n",
342 IPCThreadState::self()->getCallingPid(),
343 IPCThreadState::self()->getCallingUid());
344 result.append(buffer);
345 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346}
347
Eric Laurent81784c32012-11-19 14:55:58 -0800348bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349{
350 bool locked = false;
351 for (int i = 0; i < kDumpLockRetries; ++i) {
352 if (mutex.tryLock() == NO_ERROR) {
353 locked = true;
354 break;
355 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800356 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357 }
358 return locked;
359}
360
361status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
362{
Glenn Kasten44deb052012-02-05 18:09:08 -0800363 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 dumpPermissionDenial(fd, args);
365 } else {
366 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800367 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368 if (!hardwareLocked) {
369 String8 result(kHardwareLockedString);
370 write(fd, result.string(), result.size());
371 } else {
372 mHardwareLock.unlock();
373 }
374
Eric Laurent81784c32012-11-19 14:55:58 -0800375 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376
377 // failed to lock - AudioFlinger is probably deadlocked
378 if (!locked) {
379 String8 result(kDeadlockedString);
380 write(fd, result.string(), result.size());
381 }
382
383 dumpClients(fd, args);
384 dumpInternals(fd, args);
385
386 // dump playback threads
387 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
388 mPlaybackThreads.valueAt(i)->dump(fd, args);
389 }
390
391 // dump record threads
392 for (size_t i = 0; i < mRecordThreads.size(); i++) {
393 mRecordThreads.valueAt(i)->dump(fd, args);
394 }
395
Dima Zavin799a70e2011-04-18 16:57:27 -0700396 // dump all hardware devs
397 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700398 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700399 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700401
Glenn Kasten46909e72013-02-26 09:20:22 -0800402#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700403 // dump the serially shared record tee sink
404 if (mRecordTeeSource != 0) {
405 dumpTee(fd, mRecordTeeSource);
406 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800407#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700408
Glenn Kastend65d73c2012-06-22 17:21:07 -0700409 if (locked) {
410 mLock.unlock();
411 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800412
413 // append a copy of media.log here by forwarding fd to it, but don't attempt
414 // to lookup the service if it's not running, as it will block for a second
415 if (mLogMemoryDealer != 0) {
416 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
417 if (binder != 0) {
418 fdprintf(fd, "\nmedia.log:\n");
419 Vector<String16> args;
420 binder->dump(fd, args);
421 }
422 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700423 }
424 return NO_ERROR;
425}
426
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800427sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
428{
429 // If pid is already in the mClients wp<> map, then use that entry
430 // (for which promote() is always != 0), otherwise create a new entry and Client.
431 sp<Client> client = mClients.valueFor(pid).promote();
432 if (client == 0) {
433 client = new Client(this, pid);
434 mClients.add(pid, client);
435 }
436
437 return client;
438}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439
Glenn Kasten9e58b552013-01-18 15:09:48 -0800440sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
441{
Glenn Kasten481fb672013-09-30 14:39:28 -0700442 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800443 if (mLogMemoryDealer == 0) {
444 return new NBLog::Writer();
445 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800446 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700447 // Similarly if we can't contact the media.log service, also return a dummy writer
448 if (binder == 0) {
449 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800450 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700451 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
452 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
453 // If allocation fails, consult the vector of previously unregistered writers
454 // and garbage-collect one or more them until an allocation succeeds
455 if (shared == 0) {
456 Mutex::Autolock _l(mUnregisteredWritersLock);
457 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
458 {
459 // Pick the oldest stale writer to garbage-collect
460 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
461 mUnregisteredWriters.removeAt(0);
462 mediaLogService->unregisterWriter(iMemory);
463 // Now the media.log remote reference to IMemory is gone. When our last local
464 // reference to IMemory also drops to zero at end of this block,
465 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
466 }
467 // Re-attempt the allocation
468 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
469 if (shared != 0) {
470 goto success;
471 }
472 }
473 // Even after garbage-collecting all old writers, there is still not enough memory,
474 // so return a dummy writer
475 return new NBLog::Writer();
476 }
477success:
478 mediaLogService->registerWriter(shared, size, name);
479 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800480}
481
482void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
483{
Glenn Kasten685ef092013-02-04 08:15:34 -0800484 if (writer == 0) {
485 return;
486 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800487 sp<IMemory> iMemory(writer->getIMemory());
488 if (iMemory == 0) {
489 return;
490 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700491 // Rather than removing the writer immediately, append it to a queue of old writers to
492 // be garbage-collected later. This allows us to continue to view old logs for a while.
493 Mutex::Autolock _l(mUnregisteredWritersLock);
494 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800495}
496
Mathias Agopian65ab4712010-07-14 17:59:35 -0700497// IAudioFlinger interface
498
499
500sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800501 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700502 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800503 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700504 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800505 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800506 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700507 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800508 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800509 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700510 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700511 String8& name,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800512 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700513 status_t *status)
514{
515 sp<PlaybackThread::Track> track;
516 sp<TrackHandle> trackHandle;
517 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700518 status_t lStatus;
519 int lSessionId;
520
Glenn Kasten263709e2012-01-06 08:40:01 -0800521 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
522 // but if someone uses binder directly they could bypass that and cause us to crash
523 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000524 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700525 lStatus = BAD_VALUE;
526 goto Exit;
527 }
528
Glenn Kasten60a83922012-06-21 12:56:37 -0700529 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
530 // and we don't yet support 8.24 or 32-bit PCM
531 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800532 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700533 lStatus = BAD_VALUE;
534 goto Exit;
535 }
536
Glenn Kasten663c2242013-09-24 11:52:37 -0700537 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
538 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
539 lStatus = BAD_VALUE;
540 goto Exit;
541 }
542
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 {
544 Mutex::Autolock _l(mLock);
545 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700546 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800548 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549 lStatus = BAD_VALUE;
550 goto Exit;
551 }
552
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800553 pid_t pid = IPCThreadState::self()->getCallingPid();
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800554
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800555 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556
Steve Block3856b092011-10-20 11:56:00 +0100557 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700558 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700559 // check if an effect chain with the same session ID is present on another
560 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700561 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700562 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
563 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700564 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700565 if (sessions & PlaybackThread::EFFECT_SESSION) {
566 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700567 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700568 }
Eric Laurentde070132010-07-13 04:45:46 -0700569 }
570 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571 lSessionId = *sessionId;
572 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700573 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700574 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575 if (sessionId != NULL) {
576 *sessionId = lSessionId;
577 }
578 }
Steve Block3856b092011-10-20 11:56:00 +0100579 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580
581 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800582 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800583 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700584 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700585
586 // move effect chain to this output thread if an effect on same session was waiting
587 // for a track to be created
588 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700589 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700590 Mutex::Autolock _dl(thread->mLock);
591 Mutex::Autolock _sl(effectThread->mLock);
592 moveEffectChain_l(lSessionId, effectThread, thread, true);
593 }
Eric Laurenta011e352012-03-29 15:51:43 -0700594
595 // Look for sync events awaiting for a session to be used.
596 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
597 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
598 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700599 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700600 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700601 } else {
602 mPendingSyncEvents[i]->cancel();
603 }
Eric Laurenta011e352012-03-29 15:51:43 -0700604 mPendingSyncEvents.removeAt(i);
605 i--;
606 }
607 }
608 }
Glenn Kastene198c362013-08-13 09:13:36 -0700609
Mathias Agopian65ab4712010-07-14 17:59:35 -0700610 }
Glenn Kastene198c362013-08-13 09:13:36 -0700611
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700613 // s for server's pid, n for normal mixer name, f for fast index
614 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
615 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700616 trackHandle = new TrackHandle(track);
617 } else {
618 // remove local strong reference to Client before deleting the Track so that the Client
619 // destructor is called by the TrackBase destructor with mLock held
620 client.clear();
621 track.clear();
622 }
623
624Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700625 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626 return trackHandle;
627}
628
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800629uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630{
631 Mutex::Autolock _l(mLock);
632 PlaybackThread *thread = checkPlaybackThread_l(output);
633 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000634 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635 return 0;
636 }
637 return thread->sampleRate();
638}
639
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800640int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641{
642 Mutex::Autolock _l(mLock);
643 PlaybackThread *thread = checkPlaybackThread_l(output);
644 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000645 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 return 0;
647 }
648 return thread->channelCount();
649}
650
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800651audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652{
653 Mutex::Autolock _l(mLock);
654 PlaybackThread *thread = checkPlaybackThread_l(output);
655 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000656 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800657 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658 }
659 return thread->format();
660}
661
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800662size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663{
664 Mutex::Autolock _l(mLock);
665 PlaybackThread *thread = checkPlaybackThread_l(output);
666 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000667 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668 return 0;
669 }
Glenn Kasten58912562012-04-03 10:45:00 -0700670 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
671 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 return thread->frameCount();
673}
674
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800675uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676{
677 Mutex::Autolock _l(mLock);
678 PlaybackThread *thread = checkPlaybackThread_l(output);
679 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800680 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 return 0;
682 }
683 return thread->latency();
684}
685
686status_t AudioFlinger::setMasterVolume(float value)
687{
Eric Laurenta1884f92011-08-23 08:25:03 -0700688 status_t ret = initCheck();
689 if (ret != NO_ERROR) {
690 return ret;
691 }
692
Mathias Agopian65ab4712010-07-14 17:59:35 -0700693 // check calling permissions
694 if (!settingsAllowed()) {
695 return PERMISSION_DENIED;
696 }
697
Eric Laurenta4c5a552012-03-29 10:12:40 -0700698 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700699 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700700
John Grossmanee578c02012-07-23 17:05:46 -0700701 // Set master volume in the HALs which support it.
702 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
703 AutoMutex lock(mHardwareLock);
704 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800705
John Grossmanee578c02012-07-23 17:05:46 -0700706 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
707 if (dev->canSetMasterVolume()) {
708 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800709 }
John Grossmanee578c02012-07-23 17:05:46 -0700710 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712
John Grossmanee578c02012-07-23 17:05:46 -0700713 // Now set the master volume in each playback thread. Playback threads
714 // assigned to HALs which do not have master volume support will apply
715 // master volume during the mix operation. Threads with HALs which do
716 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800717 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700718 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700719
720 return NO_ERROR;
721}
722
Glenn Kastenf78aee72012-01-04 11:00:47 -0800723status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724{
Eric Laurenta1884f92011-08-23 08:25:03 -0700725 status_t ret = initCheck();
726 if (ret != NO_ERROR) {
727 return ret;
728 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700729
730 // check calling permissions
731 if (!settingsAllowed()) {
732 return PERMISSION_DENIED;
733 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800734 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000735 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700736 return BAD_VALUE;
737 }
738
739 { // scope for the lock
740 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700741 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700743 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700744 mHardwareStatus = AUDIO_HW_IDLE;
745 }
746
747 if (NO_ERROR == ret) {
748 Mutex::Autolock _l(mLock);
749 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800750 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700751 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752 }
753
754 return ret;
755}
756
757status_t AudioFlinger::setMicMute(bool state)
758{
Eric Laurenta1884f92011-08-23 08:25:03 -0700759 status_t ret = initCheck();
760 if (ret != NO_ERROR) {
761 return ret;
762 }
763
Mathias Agopian65ab4712010-07-14 17:59:35 -0700764 // check calling permissions
765 if (!settingsAllowed()) {
766 return PERMISSION_DENIED;
767 }
768
769 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700770 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700772 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773 mHardwareStatus = AUDIO_HW_IDLE;
774 return ret;
775}
776
777bool AudioFlinger::getMicMute() const
778{
Eric Laurenta1884f92011-08-23 08:25:03 -0700779 status_t ret = initCheck();
780 if (ret != NO_ERROR) {
781 return false;
782 }
783
Dima Zavinfce7a472011-04-19 22:30:36 -0700784 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800785 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_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700788 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789 mHardwareStatus = AUDIO_HW_IDLE;
790 return state;
791}
792
793status_t AudioFlinger::setMasterMute(bool muted)
794{
John Grossmand8f178d2012-07-20 14:51:35 -0700795 status_t ret = initCheck();
796 if (ret != NO_ERROR) {
797 return ret;
798 }
799
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 // check calling permissions
801 if (!settingsAllowed()) {
802 return PERMISSION_DENIED;
803 }
804
John Grossmanee578c02012-07-23 17:05:46 -0700805 Mutex::Autolock _l(mLock);
806 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700807
John Grossmanee578c02012-07-23 17:05:46 -0700808 // Set master mute in the HALs which support it.
809 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
810 AutoMutex lock(mHardwareLock);
811 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700812
John Grossmanee578c02012-07-23 17:05:46 -0700813 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
814 if (dev->canSetMasterMute()) {
815 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700816 }
John Grossmanee578c02012-07-23 17:05:46 -0700817 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700818 }
819
John Grossmanee578c02012-07-23 17:05:46 -0700820 // Now set the master mute in each playback thread. Playback threads
821 // assigned to HALs which do not have master mute support will apply master
822 // mute during the mix operation. Threads with HALs which do support master
823 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800824 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700825 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826
827 return NO_ERROR;
828}
829
830float AudioFlinger::masterVolume() const
831{
Glenn Kasten98067102011-12-13 11:47:54 -0800832 Mutex::Autolock _l(mLock);
833 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834}
835
836bool AudioFlinger::masterMute() const
837{
Glenn Kasten98067102011-12-13 11:47:54 -0800838 Mutex::Autolock _l(mLock);
839 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840}
841
John Grossman4ff14ba2012-02-08 16:37:41 -0800842float AudioFlinger::masterVolume_l() const
843{
John Grossman4ff14ba2012-02-08 16:37:41 -0800844 return mMasterVolume;
845}
846
John Grossmand8f178d2012-07-20 14:51:35 -0700847bool AudioFlinger::masterMute_l() const
848{
John Grossmanee578c02012-07-23 17:05:46 -0700849 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700850}
851
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800852status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
853 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854{
855 // check calling permissions
856 if (!settingsAllowed()) {
857 return PERMISSION_DENIED;
858 }
859
Glenn Kasten263709e2012-01-06 08:40:01 -0800860 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000861 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862 return BAD_VALUE;
863 }
864
865 AutoMutex lock(mLock);
866 PlaybackThread *thread = NULL;
867 if (output) {
868 thread = checkPlaybackThread_l(output);
869 if (thread == NULL) {
870 return BAD_VALUE;
871 }
872 }
873
874 mStreamTypes[stream].volume = value;
875
876 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800877 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700878 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879 }
880 } else {
881 thread->setStreamVolume(stream, value);
882 }
883
884 return NO_ERROR;
885}
886
Glenn Kastenfff6d712012-01-12 16:38:12 -0800887status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888{
889 // check calling permissions
890 if (!settingsAllowed()) {
891 return PERMISSION_DENIED;
892 }
893
Glenn Kasten263709e2012-01-06 08:40:01 -0800894 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700895 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000896 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897 return BAD_VALUE;
898 }
899
Eric Laurent93575202011-01-18 18:39:02 -0800900 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700901 mStreamTypes[stream].mute = muted;
902 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700903 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700904
905 return NO_ERROR;
906}
907
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800908float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909{
Glenn Kasten263709e2012-01-06 08:40:01 -0800910 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 return 0.0f;
912 }
913
914 AutoMutex lock(mLock);
915 float volume;
916 if (output) {
917 PlaybackThread *thread = checkPlaybackThread_l(output);
918 if (thread == NULL) {
919 return 0.0f;
920 }
921 volume = thread->streamVolume(stream);
922 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800923 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 }
925
926 return volume;
927}
928
Glenn Kastenfff6d712012-01-12 16:38:12 -0800929bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930{
Glenn Kasten263709e2012-01-06 08:40:01 -0800931 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932 return true;
933 }
934
Glenn Kasten6637baa2012-01-09 09:40:36 -0800935 AutoMutex lock(mLock);
936 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937}
938
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800939status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700941 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
942 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800943
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 // check calling permissions
945 if (!settingsAllowed()) {
946 return PERMISSION_DENIED;
947 }
948
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949 // ioHandle == 0 means the parameters are global to the audio hardware interface
950 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700951 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700952 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800953 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700954 AutoMutex lock(mHardwareLock);
955 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
956 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
957 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
958 status_t result = dev->set_parameters(dev, keyValuePairs.string());
959 final_result = result ?: final_result;
960 }
961 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800962 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700963 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
964 AudioParameter param = AudioParameter(keyValuePairs);
965 String8 value;
966 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700967 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
968 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700969 for (size_t i = 0; i < mRecordThreads.size(); i++) {
970 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700971 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700972 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
973 // collect all of the thread's session IDs
974 KeyedVector<int, bool> ids = thread->sessionIds();
975 // suspend effects associated with those session IDs
976 for (size_t j = 0; j < ids.size(); ++j) {
977 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700978 thread->setEffectSuspended(FX_IID_AEC,
979 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700980 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700981 thread->setEffectSuspended(FX_IID_NS,
982 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700983 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700984 }
985 }
Eric Laurentbee53372011-08-29 12:42:48 -0700986 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700987 }
988 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700989 String8 screenState;
990 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
991 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800992 if (isOff != (AudioFlinger::mScreenState & 1)) {
993 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700994 }
995 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700996 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700997 }
998
999 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1000 // and the thread is exited once the lock is released
1001 sp<ThreadBase> thread;
1002 {
1003 Mutex::Autolock _l(mLock);
1004 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001005 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001006 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001007 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001008 // indicate output device change to all input threads for pre processing
1009 AudioParameter param = AudioParameter(keyValuePairs);
1010 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001011 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1012 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001013 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1014 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1015 }
1016 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001017 }
1018 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001019 if (thread != 0) {
1020 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021 }
1022 return BAD_VALUE;
1023}
1024
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001025String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001027 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1028 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001029
Eric Laurenta4c5a552012-03-29 10:12:40 -07001030 Mutex::Autolock _l(mLock);
1031
Mathias Agopian65ab4712010-07-14 17:59:35 -07001032 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001033 String8 out_s8;
1034
Dima Zavin799a70e2011-04-18 16:57:27 -07001035 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001036 char *s;
1037 {
1038 AutoMutex lock(mHardwareLock);
1039 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001040 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001041 s = dev->get_parameters(dev, keys.string());
1042 mHardwareStatus = AUDIO_HW_IDLE;
1043 }
John Grossmanef7740b2012-02-09 11:28:36 -08001044 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001045 free(s);
1046 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001047 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048 }
1049
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1051 if (playbackThread != NULL) {
1052 return playbackThread->getParameters(keys);
1053 }
1054 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1055 if (recordThread != NULL) {
1056 return recordThread->getParameters(keys);
1057 }
1058 return String8("");
1059}
1060
Glenn Kastendd8104c2012-07-02 12:42:44 -07001061size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1062 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063{
Eric Laurenta1884f92011-08-23 08:25:03 -07001064 status_t ret = initCheck();
1065 if (ret != NO_ERROR) {
1066 return 0;
1067 }
1068
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001069 AutoMutex lock(mHardwareLock);
1070 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001071 struct audio_config config;
1072 memset(&config, 0, sizeof(config));
1073 config.sample_rate = sampleRate;
1074 config.channel_mask = channelMask;
1075 config.format = format;
1076
John Grossmanee578c02012-07-23 17:05:46 -07001077 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1078 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001079 mHardwareStatus = AUDIO_HW_IDLE;
1080 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001081}
1082
Glenn Kasten5f972c02014-01-13 09:59:31 -08001083uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001084{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001085 Mutex::Autolock _l(mLock);
1086
1087 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1088 if (recordThread != NULL) {
1089 return recordThread->getInputFramesLost();
1090 }
1091 return 0;
1092}
1093
1094status_t AudioFlinger::setVoiceVolume(float value)
1095{
Eric Laurenta1884f92011-08-23 08:25:03 -07001096 status_t ret = initCheck();
1097 if (ret != NO_ERROR) {
1098 return ret;
1099 }
1100
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101 // check calling permissions
1102 if (!settingsAllowed()) {
1103 return PERMISSION_DENIED;
1104 }
1105
1106 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001107 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001108 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001109 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110 mHardwareStatus = AUDIO_HW_IDLE;
1111
1112 return ret;
1113}
1114
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001115status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001116 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117{
1118 status_t status;
1119
1120 Mutex::Autolock _l(mLock);
1121
1122 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1123 if (playbackThread != NULL) {
1124 return playbackThread->getRenderPosition(halFrames, dspFrames);
1125 }
1126
1127 return BAD_VALUE;
1128}
1129
1130void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1131{
1132
1133 Mutex::Autolock _l(mLock);
1134
Glenn Kastenbb001922012-02-03 11:10:26 -08001135 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001136 if (mNotificationClients.indexOfKey(pid) < 0) {
1137 sp<NotificationClient> notificationClient = new NotificationClient(this,
1138 client,
1139 pid);
Steve Block3856b092011-10-20 11:56:00 +01001140 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141
1142 mNotificationClients.add(pid, notificationClient);
1143
1144 sp<IBinder> binder = client->asBinder();
1145 binder->linkToDeath(notificationClient);
1146
1147 // the config change is always sent from playback or record threads to avoid deadlock
1148 // with AudioSystem::gLock
1149 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001150 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151 }
1152
1153 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001154 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155 }
1156 }
1157}
1158
1159void AudioFlinger::removeNotificationClient(pid_t pid)
1160{
1161 Mutex::Autolock _l(mLock);
1162
Glenn Kastena3b09252012-01-20 09:19:01 -08001163 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001164
Steve Block3856b092011-10-20 11:56:00 +01001165 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001166 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001167 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001168 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001169 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001170 ALOGV(" pid %d @ %d", ref->mPid, i);
1171 if (ref->mPid == pid) {
1172 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001173 mAudioSessionRefs.removeAt(i);
1174 delete ref;
1175 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001176 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001177 } else {
1178 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001179 }
1180 }
1181 if (removed) {
1182 purgeStaleEffects_l();
1183 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001184}
1185
1186// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001187void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001188{
1189 size_t size = mNotificationClients.size();
1190 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001191 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1192 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001193 }
1194}
1195
1196// removeClient_l() must be called with AudioFlinger::mLock held
1197void AudioFlinger::removeClient_l(pid_t pid)
1198{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001199 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001200 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201 mClients.removeItem(pid);
1202}
1203
Eric Laurent717e1282012-06-29 16:36:52 -07001204// getEffectThread_l() must be called with AudioFlinger::mLock held
1205sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1206{
1207 sp<PlaybackThread> thread;
1208
1209 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1210 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1211 ALOG_ASSERT(thread == 0);
1212 thread = mPlaybackThreads.valueAt(i);
1213 }
1214 }
1215
1216 return thread;
1217}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001218
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220
1221// ----------------------------------------------------------------------------
1222
1223AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1224 : RefBase(),
1225 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001226 // 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 -07001227 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001228 mPid(pid),
1229 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230{
1231 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1232}
1233
1234// Client destructor must be called with AudioFlinger::mLock held
1235AudioFlinger::Client::~Client()
1236{
1237 mAudioFlinger->removeClient_l(mPid);
1238}
1239
Glenn Kasten435dbe62012-01-30 10:15:48 -08001240sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001241{
1242 return mMemoryDealer;
1243}
1244
John Grossman4ff14ba2012-02-08 16:37:41 -08001245// Reserve one of the limited slots for a timed audio track associated
1246// with this client
1247bool AudioFlinger::Client::reserveTimedTrack()
1248{
1249 const int kMaxTimedTracksPerClient = 4;
1250
1251 Mutex::Autolock _l(mTimedTrackLock);
1252
1253 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1254 ALOGW("can not create timed track - pid %d has exceeded the limit",
1255 mPid);
1256 return false;
1257 }
1258
1259 mTimedTrackCount++;
1260 return true;
1261}
1262
1263// Release a slot for a timed audio track
1264void AudioFlinger::Client::releaseTimedTrack()
1265{
1266 Mutex::Autolock _l(mTimedTrackLock);
1267 mTimedTrackCount--;
1268}
1269
Mathias Agopian65ab4712010-07-14 17:59:35 -07001270// ----------------------------------------------------------------------------
1271
1272AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1273 const sp<IAudioFlingerClient>& client,
1274 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001275 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276{
1277}
1278
1279AudioFlinger::NotificationClient::~NotificationClient()
1280{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001281}
1282
Glenn Kasten0f11b512014-01-31 16:18:54 -08001283void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001284{
1285 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001286 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001287}
1288
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289
1290// ----------------------------------------------------------------------------
1291
Jeff Brown893a5642013-08-16 20:19:26 -07001292static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1293 return audio_is_remote_submix_device(inDevice);
1294}
1295
Mathias Agopian65ab4712010-07-14 17:59:35 -07001296sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001297 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001298 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001299 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001300 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001301 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001302 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001303 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001304 int *sessionId,
1305 status_t *status)
1306{
1307 sp<RecordThread::RecordTrack> recordTrack;
1308 sp<RecordHandle> recordHandle;
1309 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310 status_t lStatus;
1311 RecordThread *thread;
1312 size_t inFrameCount;
1313 int lSessionId;
1314
1315 // check calling permissions
1316 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001317 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001318 lStatus = PERMISSION_DENIED;
1319 goto Exit;
1320 }
1321
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001322 if (format != AUDIO_FORMAT_PCM_16_BIT) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001323 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001324 lStatus = BAD_VALUE;
1325 goto Exit;
1326 }
1327
Mathias Agopian65ab4712010-07-14 17:59:35 -07001328 // add client to list
1329 { // scope for mLock
1330 Mutex::Autolock _l(mLock);
1331 thread = checkRecordThread_l(input);
1332 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001333 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001334 lStatus = BAD_VALUE;
1335 goto Exit;
1336 }
1337
Jeff Brown893a5642013-08-16 20:19:26 -07001338 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1339 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001340 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001341 lStatus = PERMISSION_DENIED;
1342 goto Exit;
1343 }
1344
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001345 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001346 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001347
1348 // If no audio session id is provided, create one here
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001349 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350 lSessionId = *sessionId;
1351 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001352 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001353 if (sessionId != NULL) {
1354 *sessionId = lSessionId;
1355 }
1356 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001357 // create new record track.
1358 // The record track uses one track in mHardwareMixerThread by convention.
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001359 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001360 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001361 frameCount, lSessionId,
1362 IPCThreadState::self()->getCallingUid(),
1363 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001364 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365 }
Glenn Kastene198c362013-08-13 09:13:36 -07001366
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001367 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001368 // remove local strong reference to Client before deleting the RecordTrack so that the
1369 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001370 client.clear();
1371 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372 goto Exit;
1373 }
1374
Glenn Kasten2fc14732013-08-05 14:58:14 -07001375 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001376 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001377
1378Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001379 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380 return recordHandle;
1381}
1382
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001383
1384
Mathias Agopian65ab4712010-07-14 17:59:35 -07001385// ----------------------------------------------------------------------------
1386
Eric Laurenta4c5a552012-03-29 10:12:40 -07001387audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1388{
1389 if (!settingsAllowed()) {
1390 return 0;
1391 }
1392 Mutex::Autolock _l(mLock);
1393 return loadHwModule_l(name);
1394}
1395
1396// loadHwModule_l() must be called with AudioFlinger::mLock held
1397audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1398{
1399 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1400 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1401 ALOGW("loadHwModule() module %s already loaded", name);
1402 return mAudioHwDevs.keyAt(i);
1403 }
1404 }
1405
Eric Laurenta4c5a552012-03-29 10:12:40 -07001406 audio_hw_device_t *dev;
1407
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001408 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001409 if (rc) {
1410 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1411 return 0;
1412 }
1413
1414 mHardwareStatus = AUDIO_HW_INIT;
1415 rc = dev->init_check(dev);
1416 mHardwareStatus = AUDIO_HW_IDLE;
1417 if (rc) {
1418 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1419 return 0;
1420 }
1421
John Grossmanee578c02012-07-23 17:05:46 -07001422 // Check and cache this HAL's level of support for master mute and master
1423 // volume. If this is the first HAL opened, and it supports the get
1424 // methods, use the initial values provided by the HAL as the current
1425 // master mute and volume settings.
1426
1427 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1428 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001429 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001430
1431 if (0 == mAudioHwDevs.size()) {
1432 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1433 if (NULL != dev->get_master_volume) {
1434 float mv;
1435 if (OK == dev->get_master_volume(dev, &mv)) {
1436 mMasterVolume = mv;
1437 }
1438 }
1439
1440 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1441 if (NULL != dev->get_master_mute) {
1442 bool mm;
1443 if (OK == dev->get_master_mute(dev, &mm)) {
1444 mMasterMute = mm;
1445 }
1446 }
1447 }
1448
Eric Laurenta4c5a552012-03-29 10:12:40 -07001449 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001450 if ((NULL != dev->set_master_volume) &&
1451 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1452 flags = static_cast<AudioHwDevice::Flags>(flags |
1453 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1454 }
1455
1456 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1457 if ((NULL != dev->set_master_mute) &&
1458 (OK == dev->set_master_mute(dev, mMasterMute))) {
1459 flags = static_cast<AudioHwDevice::Flags>(flags |
1460 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1461 }
1462
Eric Laurenta4c5a552012-03-29 10:12:40 -07001463 mHardwareStatus = AUDIO_HW_IDLE;
1464 }
1465
1466 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001467 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001468
1469 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001470 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001471
1472 return handle;
1473
1474}
1475
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001476// ----------------------------------------------------------------------------
1477
Glenn Kasten3b16c762012-11-14 08:44:39 -08001478uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001479{
1480 Mutex::Autolock _l(mLock);
1481 PlaybackThread *thread = primaryPlaybackThread_l();
1482 return thread != NULL ? thread->sampleRate() : 0;
1483}
1484
Glenn Kastene33054e2012-11-14 12:54:39 -08001485size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001486{
1487 Mutex::Autolock _l(mLock);
1488 PlaybackThread *thread = primaryPlaybackThread_l();
1489 return thread != NULL ? thread->frameCountHAL() : 0;
1490}
1491
1492// ----------------------------------------------------------------------------
1493
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001494status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1495{
1496 uid_t uid = IPCThreadState::self()->getCallingUid();
1497 if (uid != AID_SYSTEM) {
1498 return PERMISSION_DENIED;
1499 }
1500 Mutex::Autolock _l(mLock);
1501 if (mIsDeviceTypeKnown) {
1502 return INVALID_OPERATION;
1503 }
1504 mIsLowRamDevice = isLowRamDevice;
1505 mIsDeviceTypeKnown = true;
1506 return NO_ERROR;
1507}
1508
1509// ----------------------------------------------------------------------------
1510
Eric Laurenta4c5a552012-03-29 10:12:40 -07001511audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1512 audio_devices_t *pDevices,
1513 uint32_t *pSamplingRate,
1514 audio_format_t *pFormat,
1515 audio_channel_mask_t *pChannelMask,
1516 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001517 audio_output_flags_t flags,
1518 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001520 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001521 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001522 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1523 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1524 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001525 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001526 config.offload_info = *offloadInfo;
1527 }
1528
Eric Laurentbfb1b832013-01-07 09:53:42 -08001529 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001530 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001531 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001532 config.sample_rate,
1533 config.format,
1534 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001535 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001536 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001537 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001538
1539 if (pDevices == NULL || *pDevices == 0) {
1540 return 0;
1541 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001542
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543 Mutex::Autolock _l(mLock);
1544
Glenn Kasten32550952013-08-06 10:45:10 -07001545 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001546 if (outHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001547 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001548 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001549
John Grossmanee578c02012-07-23 17:05:46 -07001550 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001551 audio_io_handle_t id = nextUniqueId();
1552
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001553 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001554
Glenn Kasten32550952013-08-06 10:45:10 -07001555 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001556 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001557 id,
1558 *pDevices,
1559 (audio_output_flags_t)flags,
1560 &config,
1561 &outStream);
1562
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001563 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001564 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001565 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001566 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001567 config.sample_rate,
1568 config.format,
1569 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001570 status);
1571
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001572 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001573 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001574
Glenn Kasten32550952013-08-06 10:45:10 -07001575 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001576 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1577 thread = new OffloadThread(this, output, id, *pDevices);
1578 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1579 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001580 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1581 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001582 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001583 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001584 } else {
1585 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001586 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001587 }
1588 mPlaybackThreads.add(id, thread);
1589
Glenn Kasten7c027242012-12-26 14:43:16 -08001590 if (pSamplingRate != NULL) {
1591 *pSamplingRate = config.sample_rate;
1592 }
1593 if (pFormat != NULL) {
1594 *pFormat = config.format;
1595 }
1596 if (pChannelMask != NULL) {
1597 *pChannelMask = config.channel_mask;
1598 }
1599 if (pLatencyMs != NULL) {
1600 *pLatencyMs = thread->latency();
1601 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602
1603 // notify client processes of the new output creation
1604 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001605
1606 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001607 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001608 ALOGI("Using module %d has the primary audio interface", module);
1609 mPrimaryHardwareDev = outHwDev;
1610
1611 AutoMutex lock(mHardwareLock);
1612 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001613 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001614 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001615 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001616 return id;
1617 }
1618
1619 return 0;
1620}
1621
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001622audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1623 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001624{
1625 Mutex::Autolock _l(mLock);
1626 MixerThread *thread1 = checkMixerThread_l(output1);
1627 MixerThread *thread2 = checkMixerThread_l(output2);
1628
1629 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001630 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1631 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001632 return 0;
1633 }
1634
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001635 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001636 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1637 thread->addOutputTrack(thread2);
1638 mPlaybackThreads.add(id, thread);
1639 // notify client processes of the new output creation
1640 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1641 return id;
1642}
1643
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001644status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001645{
Glenn Kastend96c5722012-04-25 13:44:49 -07001646 return closeOutput_nonvirtual(output);
1647}
1648
1649status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1650{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001651 // keep strong reference on the playback thread so that
1652 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001653 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001654 {
1655 Mutex::Autolock _l(mLock);
1656 thread = checkPlaybackThread_l(output);
1657 if (thread == NULL) {
1658 return BAD_VALUE;
1659 }
1660
Steve Block3856b092011-10-20 11:56:00 +01001661 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001662
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001663 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001664 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001665 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001666 DuplicatingThread *dupThread =
1667 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001668 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001669
1670 }
1671 }
1672 }
1673
1674
1675 mPlaybackThreads.removeItem(output);
1676 // save all effects to the default thread
1677 if (mPlaybackThreads.size()) {
1678 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1679 if (dstThread != NULL) {
1680 // audioflinger lock is held here so the acquisition order of thread locks does not
1681 // matter
1682 Mutex::Autolock _dl(dstThread->mLock);
1683 Mutex::Autolock _sl(thread->mLock);
1684 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1685 for (size_t i = 0; i < effectChains.size(); i ++) {
1686 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001687 }
1688 }
1689 }
Glenn Kastena1117922012-01-26 10:53:32 -08001690 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001691 }
1692 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001693 // The thread entity (active unit of execution) is no longer running here,
1694 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001695
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001696 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001697 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001698 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001699 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001700 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001701 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001702 }
1703 return NO_ERROR;
1704}
1705
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001706status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001707{
1708 Mutex::Autolock _l(mLock);
1709 PlaybackThread *thread = checkPlaybackThread_l(output);
1710
1711 if (thread == NULL) {
1712 return BAD_VALUE;
1713 }
1714
Steve Block3856b092011-10-20 11:56:00 +01001715 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001716 thread->suspend();
1717
1718 return NO_ERROR;
1719}
1720
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001721status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001722{
1723 Mutex::Autolock _l(mLock);
1724 PlaybackThread *thread = checkPlaybackThread_l(output);
1725
1726 if (thread == NULL) {
1727 return BAD_VALUE;
1728 }
1729
Steve Block3856b092011-10-20 11:56:00 +01001730 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001731
1732 thread->restore();
1733
1734 return NO_ERROR;
1735}
1736
Eric Laurenta4c5a552012-03-29 10:12:40 -07001737audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1738 audio_devices_t *pDevices,
1739 uint32_t *pSamplingRate,
1740 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001741 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001742{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001743 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001744 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001745 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1746 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1747 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1748
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001749 uint32_t reqSamplingRate = config.sample_rate;
1750 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001751 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001752
1753 if (pDevices == NULL || *pDevices == 0) {
1754 return 0;
1755 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001756
Mathias Agopian65ab4712010-07-14 17:59:35 -07001757 Mutex::Autolock _l(mLock);
1758
Glenn Kasten32550952013-08-06 10:45:10 -07001759 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001760 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001761 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001762 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001763
John Grossmanee578c02012-07-23 17:05:46 -07001764 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001765 audio_io_handle_t id = nextUniqueId();
1766
Glenn Kasten32550952013-08-06 10:45:10 -07001767 audio_stream_in_t *inStream = NULL;
1768 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001769 &inStream);
Glenn Kastencac3daa2014-02-07 09:47:14 -08001770 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %#x, Channels %x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001771 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001772 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001773 config.sample_rate,
1774 config.format,
1775 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001776 status);
1777
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001778 // If the input could not be opened with the requested parameters and we can handle the
1779 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1780 // 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 -07001781 if (status == BAD_VALUE &&
1782 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1783 (config.sample_rate <= 2 * reqSamplingRate) &&
Glenn Kastenf506e942013-08-06 10:49:43 -07001784 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001785 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Glenn Kasten254af182012-07-03 14:59:05 -07001786 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001787 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001788 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07001789 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001790 }
1791
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001792 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001793
Glenn Kasten46909e72013-02-26 09:20:22 -08001794#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001795 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1796 // or (re-)create if current Pipe is idle and does not match the new format
1797 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001798 enum {
1799 TEE_SINK_NO, // don't copy input
1800 TEE_SINK_NEW, // copy input using a new pipe
1801 TEE_SINK_OLD, // copy input using an existing pipe
1802 } kind;
1803 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1804 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001805 if (!mTeeSinkInputEnabled) {
1806 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08001807 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001808 kind = TEE_SINK_NO;
1809 } else if (mRecordTeeSink == 0) {
1810 kind = TEE_SINK_NEW;
1811 } else if (mRecordTeeSink->getStrongCount() != 1) {
1812 kind = TEE_SINK_NO;
1813 } else if (format == mRecordTeeSink->format()) {
1814 kind = TEE_SINK_OLD;
1815 } else {
1816 kind = TEE_SINK_NEW;
1817 }
1818 switch (kind) {
1819 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001820 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001821 size_t numCounterOffers = 0;
1822 const NBAIO_Format offers[1] = {format};
1823 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1824 ALOG_ASSERT(index == 0);
1825 PipeReader *pipeReader = new PipeReader(*pipe);
1826 numCounterOffers = 0;
1827 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1828 ALOG_ASSERT(index == 0);
1829 mRecordTeeSink = pipe;
1830 mRecordTeeSource = pipeReader;
1831 teeSink = pipe;
1832 }
1833 break;
1834 case TEE_SINK_OLD:
1835 teeSink = mRecordTeeSink;
1836 break;
1837 case TEE_SINK_NO:
1838 default:
1839 break;
1840 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001841#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001842
Dima Zavin799a70e2011-04-18 16:57:27 -07001843 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1844
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001845 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001846 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001847 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001848 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001849 input,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001850 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001851 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001852 *pDevices
1853#ifdef TEE_SINK
1854 , teeSink
1855#endif
1856 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001857 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001858 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001859 if (pSamplingRate != NULL) {
1860 *pSamplingRate = reqSamplingRate;
1861 }
1862 if (pFormat != NULL) {
1863 *pFormat = config.format;
1864 }
1865 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001866 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001867 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001868
Mathias Agopian65ab4712010-07-14 17:59:35 -07001869 // notify client processes of the new input creation
1870 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1871 return id;
1872 }
1873
1874 return 0;
1875}
1876
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001877status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878{
Glenn Kastend96c5722012-04-25 13:44:49 -07001879 return closeInput_nonvirtual(input);
1880}
1881
1882status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1883{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001884 // keep strong reference on the record thread so that
1885 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001886 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001887 {
1888 Mutex::Autolock _l(mLock);
1889 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001890 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001891 return BAD_VALUE;
1892 }
1893
Steve Block3856b092011-10-20 11:56:00 +01001894 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001895 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001896 mRecordThreads.removeItem(input);
1897 }
1898 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001899 // The thread entity (active unit of execution) is no longer running here,
1900 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001902 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001903 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001904 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001905 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001906 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001907
1908 return NO_ERROR;
1909}
1910
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001911status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001912{
1913 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001914 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001915
1916 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1917 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001918 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001919 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001920
1921 return NO_ERROR;
1922}
1923
1924
1925int AudioFlinger::newAudioSessionId()
1926{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001927 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928}
1929
Marco Nelissend457c972014-02-11 08:47:07 -08001930void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001931{
1932 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001933 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08001934 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
1935 if (pid != -1 && (caller == getpid_cached)) {
1936 caller = pid;
1937 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001938
1939 // Ignore requests received from processes not known as notification client. The request
1940 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1941 // called from a different pid leaving a stale session reference. Also we don't know how
1942 // to clear this reference if the client process dies.
1943 if (mNotificationClients.indexOfKey(caller) < 0) {
Marco Nelissend457c972014-02-11 08:47:07 -08001944 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
Eric Laurentd1b28d42013-09-18 18:47:13 -07001945 return;
1946 }
1947
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001948 size_t num = mAudioSessionRefs.size();
1949 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001950 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001951 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1952 ref->mCnt++;
1953 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001954 return;
1955 }
1956 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001957 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1958 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001959}
1960
Marco Nelissend457c972014-02-11 08:47:07 -08001961void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001962{
1963 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001964 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08001965 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
1966 if (pid != -1 && (caller == getpid_cached)) {
1967 caller = pid;
1968 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001969 size_t num = mAudioSessionRefs.size();
1970 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001971 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001972 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1973 ref->mCnt--;
1974 ALOGV(" decremented refcount to %d", ref->mCnt);
1975 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001976 mAudioSessionRefs.removeAt(i);
1977 delete ref;
1978 purgeStaleEffects_l();
1979 }
1980 return;
1981 }
1982 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001983 // If the caller is mediaserver it is likely that the session being released was acquired
1984 // on behalf of a process not in notification clients and we ignore the warning.
1985 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001986}
1987
1988void AudioFlinger::purgeStaleEffects_l() {
1989
Steve Block3856b092011-10-20 11:56:00 +01001990 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001991
1992 Vector< sp<EffectChain> > chains;
1993
1994 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1995 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1996 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1997 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001998 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1999 chains.push(ec);
2000 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002001 }
2002 }
2003 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2004 sp<RecordThread> t = mRecordThreads.valueAt(i);
2005 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2006 sp<EffectChain> ec = t->mEffectChains[j];
2007 chains.push(ec);
2008 }
2009 }
2010
2011 for (size_t i = 0; i < chains.size(); i++) {
2012 sp<EffectChain> ec = chains[i];
2013 int sessionid = ec->sessionId();
2014 sp<ThreadBase> t = ec->mThread.promote();
2015 if (t == 0) {
2016 continue;
2017 }
2018 size_t numsessionrefs = mAudioSessionRefs.size();
2019 bool found = false;
2020 for (size_t k = 0; k < numsessionrefs; k++) {
2021 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002022 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002023 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002024 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002025 found = true;
2026 break;
2027 }
2028 }
2029 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002030 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002031 // remove all effects from the chain
2032 while (ec->mEffects.size()) {
2033 sp<EffectModule> effect = ec->mEffects[0];
2034 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002035 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002036 if (effect->purgeHandles()) {
2037 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002038 }
2039 AudioSystem::unregisterEffect(effect->id());
2040 }
2041 }
2042 }
2043 return;
2044}
2045
Mathias Agopian65ab4712010-07-14 17:59:35 -07002046// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002047AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002048{
Glenn Kastena1117922012-01-26 10:53:32 -08002049 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002050}
2051
2052// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002053AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002054{
2055 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002056 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002057}
2058
2059// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002060AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002061{
Glenn Kastena1117922012-01-26 10:53:32 -08002062 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002063}
2064
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002065uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002066{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002067 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002068}
2069
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002070AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002071{
2072 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2073 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002074 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002075 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002076 return thread;
2077 }
2078 }
2079 return NULL;
2080}
2081
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002082audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002083{
2084 PlaybackThread *thread = primaryPlaybackThread_l();
2085
2086 if (thread == NULL) {
2087 return 0;
2088 }
2089
Eric Laurentf1c04f92012-08-28 14:26:53 -07002090 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002091}
2092
Eric Laurenta011e352012-03-29 15:51:43 -07002093sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2094 int triggerSession,
2095 int listenerSession,
2096 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002097 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002098{
2099 Mutex::Autolock _l(mLock);
2100
2101 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2102 status_t playStatus = NAME_NOT_FOUND;
2103 status_t recStatus = NAME_NOT_FOUND;
2104 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2105 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2106 if (playStatus == NO_ERROR) {
2107 return event;
2108 }
2109 }
2110 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2111 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2112 if (recStatus == NO_ERROR) {
2113 return event;
2114 }
2115 }
2116 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2117 mPendingSyncEvents.add(event);
2118 } else {
2119 ALOGV("createSyncEvent() invalid event %d", event->type());
2120 event.clear();
2121 }
2122 return event;
2123}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002124
Mathias Agopian65ab4712010-07-14 17:59:35 -07002125// ----------------------------------------------------------------------------
2126// Effect management
2127// ----------------------------------------------------------------------------
2128
2129
Glenn Kastenf587ba52012-01-26 16:25:10 -08002130status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002131{
2132 Mutex::Autolock _l(mLock);
2133 return EffectQueryNumberEffects(numEffects);
2134}
2135
Glenn Kastenf587ba52012-01-26 16:25:10 -08002136status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002137{
2138 Mutex::Autolock _l(mLock);
2139 return EffectQueryEffect(index, descriptor);
2140}
2141
Glenn Kasten5e92a782012-01-30 07:40:52 -08002142status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002143 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002144{
2145 Mutex::Autolock _l(mLock);
2146 return EffectGetDescriptor(pUuid, descriptor);
2147}
2148
2149
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002150sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002151 effect_descriptor_t *pDesc,
2152 const sp<IEffectClient>& effectClient,
2153 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002154 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002155 int sessionId,
2156 status_t *status,
2157 int *id,
2158 int *enabled)
2159{
2160 status_t lStatus = NO_ERROR;
2161 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002162 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002163
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002164 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002165 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002166 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002167
2168 if (pDesc == NULL) {
2169 lStatus = BAD_VALUE;
2170 goto Exit;
2171 }
2172
Eric Laurent84e9a102010-09-23 16:10:16 -07002173 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002174 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002175 lStatus = PERMISSION_DENIED;
2176 goto Exit;
2177 }
2178
Dima Zavinfce7a472011-04-19 22:30:36 -07002179 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002180 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002181 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002182 lStatus = PERMISSION_DENIED;
2183 goto Exit;
2184 }
2185
Mathias Agopian65ab4712010-07-14 17:59:35 -07002186 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002187 if (!EffectIsNullUuid(&pDesc->uuid)) {
2188 // if uuid is specified, request effect descriptor
2189 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2190 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002191 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192 goto Exit;
2193 }
2194 } else {
2195 // if uuid is not specified, look for an available implementation
2196 // of the required type in effect factory
2197 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002198 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002199 lStatus = BAD_VALUE;
2200 goto Exit;
2201 }
2202 uint32_t numEffects = 0;
2203 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002204 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002205 bool found = false;
2206
2207 lStatus = EffectQueryNumberEffects(&numEffects);
2208 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002209 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002210 goto Exit;
2211 }
2212 for (uint32_t i = 0; i < numEffects; i++) {
2213 lStatus = EffectQueryEffect(i, &desc);
2214 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002215 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002216 continue;
2217 }
2218 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2219 // If matching type found save effect descriptor. If the session is
2220 // 0 and the effect is not auxiliary, continue enumeration in case
2221 // an auxiliary version of this effect type is available
2222 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002223 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002224 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002225 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2226 break;
2227 }
2228 }
2229 }
2230 if (!found) {
2231 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002232 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002233 goto Exit;
2234 }
2235 // For same effect type, chose auxiliary version over insert version if
2236 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002237 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002238 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002239 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002240 }
2241 }
2242
2243 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002244 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002245 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2246 lStatus = INVALID_OPERATION;
2247 goto Exit;
2248 }
2249
Eric Laurent59255e42011-07-27 19:49:51 -07002250 // check recording permission for visualizer
2251 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2252 !recordingAllowed()) {
2253 lStatus = PERMISSION_DENIED;
2254 goto Exit;
2255 }
2256
Mathias Agopian65ab4712010-07-14 17:59:35 -07002257 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002258 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002259 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2260 // if the output returned by getOutputForEffect() is removed before we lock the
2261 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2262 // and we will exit safely
2263 io = AudioSystem::getOutputForEffect(&desc);
2264 ALOGV("createEffect got output %d", io);
2265 }
2266
2267 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002268
2269 // If output is not specified try to find a matching audio session ID in one of the
2270 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002271 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2272 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002273 // Note: io is never 0 when creating an effect on an input
2274 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002275 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2276 // output must be specified by AudioPolicyManager when using session
2277 // AUDIO_SESSION_OUTPUT_STAGE
2278 lStatus = BAD_VALUE;
2279 goto Exit;
2280 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002281 // look for the thread where the specified audio session is present
2282 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2283 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2284 io = mPlaybackThreads.keyAt(i);
2285 break;
2286 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002287 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002288 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002289 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2290 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2291 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002292 break;
2293 }
2294 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002295 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002296 // If no output thread contains the requested session ID, default to
2297 // first output. The effect chain will be moved to the correct output
2298 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002299 if (io == 0 && mPlaybackThreads.size()) {
2300 io = mPlaybackThreads.keyAt(0);
2301 }
Steve Block3856b092011-10-20 11:56:00 +01002302 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002303 }
2304 ThreadBase *thread = checkRecordThread_l(io);
2305 if (thread == NULL) {
2306 thread = checkPlaybackThread_l(io);
2307 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002308 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002309 lStatus = BAD_VALUE;
2310 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002311 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002312 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002313
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002314 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002315
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002316 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002317 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2318 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002319 if (handle != 0 && id != NULL) {
2320 *id = handle->id();
2321 }
2322 }
2323
2324Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002325 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002326 return handle;
2327}
2328
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002329status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2330 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002331{
Steve Block3856b092011-10-20 11:56:00 +01002332 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002333 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002334 Mutex::Autolock _l(mLock);
2335 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002336 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002337 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002338 }
Eric Laurentde070132010-07-13 04:45:46 -07002339 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2340 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002341 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002342 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002343 }
Eric Laurentde070132010-07-13 04:45:46 -07002344 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2345 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002346 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002347 return BAD_VALUE;
2348 }
2349
2350 Mutex::Autolock _dl(dstThread->mLock);
2351 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002352 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002353}
2354
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002355// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002356status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002357 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002358 AudioFlinger::PlaybackThread *dstThread,
2359 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002360{
Steve Block3856b092011-10-20 11:56:00 +01002361 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002362 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002363
Eric Laurent59255e42011-07-27 19:49:51 -07002364 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002365 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002366 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002367 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002368 return INVALID_OPERATION;
2369 }
2370
Eric Laurent39e94f82010-07-28 01:32:47 -07002371 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002372 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002373 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002374 // removed.
2375 srcThread->removeEffectChain_l(chain);
2376
2377 // transfer all effects one by one so that new effect chain is created on new thread with
2378 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002379 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002380 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002381 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002382 Vector< sp<EffectModule> > removed;
2383 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002384 while (effect != 0) {
2385 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002386 removed.add(effect);
2387 status = dstThread->addEffect_l(effect);
2388 if (status != NO_ERROR) {
2389 break;
2390 }
Eric Laurentec35a142011-10-05 17:42:25 -07002391 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2392 if (effect->state() == EffectModule::ACTIVE ||
2393 effect->state() == EffectModule::STOPPING) {
2394 effect->start();
2395 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002396 // if the move request is not received from audio policy manager, the effect must be
2397 // re-registered with the new strategy and output
2398 if (dstChain == 0) {
2399 dstChain = effect->chain().promote();
2400 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002401 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002402 status = NO_INIT;
2403 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002404 }
2405 strategy = dstChain->strategy();
2406 }
2407 if (reRegister) {
2408 AudioSystem::unregisterEffect(effect->id());
2409 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002410 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002411 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002412 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002413 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002414 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002415 }
Eric Laurentde070132010-07-13 04:45:46 -07002416 effect = chain->getEffectFromId_l(0);
2417 }
2418
Eric Laurent5baf2af2013-09-12 17:37:00 -07002419 if (status != NO_ERROR) {
2420 for (size_t i = 0; i < removed.size(); i++) {
2421 srcThread->addEffect_l(removed[i]);
2422 if (dstChain != 0 && reRegister) {
2423 AudioSystem::unregisterEffect(removed[i]->id());
2424 AudioSystem::registerEffect(&removed[i]->desc(),
2425 srcThread->id(),
2426 strategy,
2427 sessionId,
2428 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002429 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002430 }
2431 }
2432 }
2433
2434 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002435}
2436
Eric Laurent5baf2af2013-09-12 17:37:00 -07002437bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002438{
2439 if (mGlobalEffectEnableTime != 0 &&
2440 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2441 return true;
2442 }
2443
2444 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2445 sp<EffectChain> ec =
2446 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002447 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002448 return true;
2449 }
2450 }
2451 return false;
2452}
2453
Eric Laurent5baf2af2013-09-12 17:37:00 -07002454void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002455{
2456 Mutex::Autolock _l(mLock);
2457
2458 mGlobalEffectEnableTime = systemTime();
2459
2460 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2461 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2462 if (t->mType == ThreadBase::OFFLOAD) {
2463 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2464 }
2465 }
2466
2467}
2468
Glenn Kastenda6ef132013-01-10 12:31:01 -08002469struct Entry {
2470#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2471 char mName[MAX_NAME];
2472};
2473
2474int comparEntry(const void *p1, const void *p2)
2475{
2476 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2477}
2478
Glenn Kasten46909e72013-02-26 09:20:22 -08002479#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002480void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002481{
Eric Laurent81784c32012-11-19 14:55:58 -08002482 NBAIO_Source *teeSource = source.get();
2483 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002484 // .wav rotation
2485 // There is a benign race condition if 2 threads call this simultaneously.
2486 // They would both traverse the directory, but the result would simply be
2487 // failures at unlink() which are ignored. It's also unlikely since
2488 // normally dumpsys is only done by bugreport or from the command line.
2489 char teePath[32+256];
2490 strcpy(teePath, "/data/misc/media");
2491 size_t teePathLen = strlen(teePath);
2492 DIR *dir = opendir(teePath);
2493 teePath[teePathLen++] = '/';
2494 if (dir != NULL) {
2495#define MAX_SORT 20 // number of entries to sort
2496#define MAX_KEEP 10 // number of entries to keep
2497 struct Entry entries[MAX_SORT];
2498 size_t entryCount = 0;
2499 while (entryCount < MAX_SORT) {
2500 struct dirent de;
2501 struct dirent *result = NULL;
2502 int rc = readdir_r(dir, &de, &result);
2503 if (rc != 0) {
2504 ALOGW("readdir_r failed %d", rc);
2505 break;
2506 }
2507 if (result == NULL) {
2508 break;
2509 }
2510 if (result != &de) {
2511 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2512 break;
2513 }
2514 // ignore non .wav file entries
2515 size_t nameLen = strlen(de.d_name);
2516 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2517 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2518 continue;
2519 }
2520 strcpy(entries[entryCount++].mName, de.d_name);
2521 }
2522 (void) closedir(dir);
2523 if (entryCount > MAX_KEEP) {
2524 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2525 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2526 strcpy(&teePath[teePathLen], entries[i].mName);
2527 (void) unlink(teePath);
2528 }
2529 }
2530 } else {
2531 if (fd >= 0) {
2532 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2533 }
2534 }
Eric Laurent81784c32012-11-19 14:55:58 -08002535 char teeTime[16];
2536 struct timeval tv;
2537 gettimeofday(&tv, NULL);
2538 struct tm tm;
2539 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002540 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2541 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2542 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2543 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002544 if (teeFd >= 0) {
2545 char wavHeader[44];
2546 memcpy(wavHeader,
2547 "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",
2548 sizeof(wavHeader));
2549 NBAIO_Format format = teeSource->format();
2550 unsigned channelCount = Format_channelCount(format);
2551 ALOG_ASSERT(channelCount <= FCC_2);
2552 uint32_t sampleRate = Format_sampleRate(format);
2553 wavHeader[22] = channelCount; // number of channels
2554 wavHeader[24] = sampleRate; // sample rate
2555 wavHeader[25] = sampleRate >> 8;
2556 wavHeader[32] = channelCount * 2; // block alignment
2557 write(teeFd, wavHeader, sizeof(wavHeader));
2558 size_t total = 0;
2559 bool firstRead = true;
2560 for (;;) {
2561#define TEE_SINK_READ 1024
2562 short buffer[TEE_SINK_READ * FCC_2];
2563 size_t count = TEE_SINK_READ;
2564 ssize_t actual = teeSource->read(buffer, count,
2565 AudioBufferProvider::kInvalidPTS);
2566 bool wasFirstRead = firstRead;
2567 firstRead = false;
2568 if (actual <= 0) {
2569 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2570 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002571 }
Eric Laurent81784c32012-11-19 14:55:58 -08002572 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002573 }
Eric Laurent81784c32012-11-19 14:55:58 -08002574 ALOG_ASSERT(actual <= (ssize_t)count);
2575 write(teeFd, buffer, actual * channelCount * sizeof(short));
2576 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002577 }
Eric Laurent81784c32012-11-19 14:55:58 -08002578 lseek(teeFd, (off_t) 4, SEEK_SET);
2579 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2580 write(teeFd, &temp, sizeof(temp));
2581 lseek(teeFd, (off_t) 40, SEEK_SET);
2582 temp = total * channelCount * sizeof(short);
2583 write(teeFd, &temp, sizeof(temp));
2584 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002585 if (fd >= 0) {
2586 fdprintf(fd, "tee copied to %s\n", teePath);
2587 }
Eric Laurent59255e42011-07-27 19:49:51 -07002588 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002589 if (fd >= 0) {
2590 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2591 }
Eric Laurent59255e42011-07-27 19:49:51 -07002592 }
2593 }
2594}
Glenn Kasten46909e72013-02-26 09:20:22 -08002595#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002596
Mathias Agopian65ab4712010-07-14 17:59:35 -07002597// ----------------------------------------------------------------------------
2598
2599status_t AudioFlinger::onTransact(
2600 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2601{
2602 return BnAudioFlinger::onTransact(code, data, reply, flags);
2603}
2604
Mathias Agopian65ab4712010-07-14 17:59:35 -07002605}; // namespace android