blob: 7c9c3ecaca8a4637d4e973cfafe45c0abe268815 [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
Glenn Kasten26c77552012-11-16 12:01:44 -08001115status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_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,
1850 reqSamplingRate,
Glenn Kastenf506e942013-08-06 10:49:43 -07001851 reqChannelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001852 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001853 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001854 *pDevices
1855#ifdef TEE_SINK
1856 , teeSink
1857#endif
1858 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001859 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001860 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001861 if (pSamplingRate != NULL) {
1862 *pSamplingRate = reqSamplingRate;
1863 }
1864 if (pFormat != NULL) {
1865 *pFormat = config.format;
1866 }
1867 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001868 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001869 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001870
Mathias Agopian65ab4712010-07-14 17:59:35 -07001871 // notify client processes of the new input creation
1872 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1873 return id;
1874 }
1875
1876 return 0;
1877}
1878
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001879status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001880{
Glenn Kastend96c5722012-04-25 13:44:49 -07001881 return closeInput_nonvirtual(input);
1882}
1883
1884status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1885{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001886 // keep strong reference on the record thread so that
1887 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001888 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001889 {
1890 Mutex::Autolock _l(mLock);
1891 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001892 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001893 return BAD_VALUE;
1894 }
1895
Steve Block3856b092011-10-20 11:56:00 +01001896 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001897 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001898 mRecordThreads.removeItem(input);
1899 }
1900 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001901 // The thread entity (active unit of execution) is no longer running here,
1902 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001903
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001904 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001905 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001906 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001907 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001908 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001909
1910 return NO_ERROR;
1911}
1912
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001913status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001914{
1915 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001916 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001917
1918 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1919 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001920 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001921 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001922
1923 return NO_ERROR;
1924}
1925
1926
1927int AudioFlinger::newAudioSessionId()
1928{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001929 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001930}
1931
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001932void AudioFlinger::acquireAudioSessionId(int audioSession)
1933{
1934 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001935 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001936 ALOGV("acquiring %d from %d", audioSession, caller);
Eric Laurentd1b28d42013-09-18 18:47:13 -07001937
1938 // Ignore requests received from processes not known as notification client. The request
1939 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1940 // called from a different pid leaving a stale session reference. Also we don't know how
1941 // to clear this reference if the client process dies.
1942 if (mNotificationClients.indexOfKey(caller) < 0) {
1943 ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
1944 return;
1945 }
1946
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001947 size_t num = mAudioSessionRefs.size();
1948 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001949 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001950 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1951 ref->mCnt++;
1952 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001953 return;
1954 }
1955 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001956 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1957 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001958}
1959
1960void AudioFlinger::releaseAudioSessionId(int audioSession)
1961{
1962 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001963 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001964 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001965 size_t num = mAudioSessionRefs.size();
1966 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001967 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001968 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1969 ref->mCnt--;
1970 ALOGV(" decremented refcount to %d", ref->mCnt);
1971 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001972 mAudioSessionRefs.removeAt(i);
1973 delete ref;
1974 purgeStaleEffects_l();
1975 }
1976 return;
1977 }
1978 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001979 // If the caller is mediaserver it is likely that the session being released was acquired
1980 // on behalf of a process not in notification clients and we ignore the warning.
1981 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001982}
1983
1984void AudioFlinger::purgeStaleEffects_l() {
1985
Steve Block3856b092011-10-20 11:56:00 +01001986 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001987
1988 Vector< sp<EffectChain> > chains;
1989
1990 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1991 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1992 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1993 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001994 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1995 chains.push(ec);
1996 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001997 }
1998 }
1999 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2000 sp<RecordThread> t = mRecordThreads.valueAt(i);
2001 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2002 sp<EffectChain> ec = t->mEffectChains[j];
2003 chains.push(ec);
2004 }
2005 }
2006
2007 for (size_t i = 0; i < chains.size(); i++) {
2008 sp<EffectChain> ec = chains[i];
2009 int sessionid = ec->sessionId();
2010 sp<ThreadBase> t = ec->mThread.promote();
2011 if (t == 0) {
2012 continue;
2013 }
2014 size_t numsessionrefs = mAudioSessionRefs.size();
2015 bool found = false;
2016 for (size_t k = 0; k < numsessionrefs; k++) {
2017 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002018 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002019 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002020 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002021 found = true;
2022 break;
2023 }
2024 }
2025 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002026 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002027 // remove all effects from the chain
2028 while (ec->mEffects.size()) {
2029 sp<EffectModule> effect = ec->mEffects[0];
2030 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002031 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002032 if (effect->purgeHandles()) {
2033 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002034 }
2035 AudioSystem::unregisterEffect(effect->id());
2036 }
2037 }
2038 }
2039 return;
2040}
2041
Mathias Agopian65ab4712010-07-14 17:59:35 -07002042// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002043AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002044{
Glenn Kastena1117922012-01-26 10:53:32 -08002045 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002046}
2047
2048// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002049AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002050{
2051 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002052 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002053}
2054
2055// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002056AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002057{
Glenn Kastena1117922012-01-26 10:53:32 -08002058 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002059}
2060
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002061uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002062{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002063 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002064}
2065
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002066AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002067{
2068 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2069 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002070 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002071 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002072 return thread;
2073 }
2074 }
2075 return NULL;
2076}
2077
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002078audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002079{
2080 PlaybackThread *thread = primaryPlaybackThread_l();
2081
2082 if (thread == NULL) {
2083 return 0;
2084 }
2085
Eric Laurentf1c04f92012-08-28 14:26:53 -07002086 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002087}
2088
Eric Laurenta011e352012-03-29 15:51:43 -07002089sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2090 int triggerSession,
2091 int listenerSession,
2092 sync_event_callback_t callBack,
2093 void *cookie)
2094{
2095 Mutex::Autolock _l(mLock);
2096
2097 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2098 status_t playStatus = NAME_NOT_FOUND;
2099 status_t recStatus = NAME_NOT_FOUND;
2100 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2101 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2102 if (playStatus == NO_ERROR) {
2103 return event;
2104 }
2105 }
2106 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2107 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2108 if (recStatus == NO_ERROR) {
2109 return event;
2110 }
2111 }
2112 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2113 mPendingSyncEvents.add(event);
2114 } else {
2115 ALOGV("createSyncEvent() invalid event %d", event->type());
2116 event.clear();
2117 }
2118 return event;
2119}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002120
Mathias Agopian65ab4712010-07-14 17:59:35 -07002121// ----------------------------------------------------------------------------
2122// Effect management
2123// ----------------------------------------------------------------------------
2124
2125
Glenn Kastenf587ba52012-01-26 16:25:10 -08002126status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002127{
2128 Mutex::Autolock _l(mLock);
2129 return EffectQueryNumberEffects(numEffects);
2130}
2131
Glenn Kastenf587ba52012-01-26 16:25:10 -08002132status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002133{
2134 Mutex::Autolock _l(mLock);
2135 return EffectQueryEffect(index, descriptor);
2136}
2137
Glenn Kasten5e92a782012-01-30 07:40:52 -08002138status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002139 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002140{
2141 Mutex::Autolock _l(mLock);
2142 return EffectGetDescriptor(pUuid, descriptor);
2143}
2144
2145
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002146sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002147 effect_descriptor_t *pDesc,
2148 const sp<IEffectClient>& effectClient,
2149 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002150 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002151 int sessionId,
2152 status_t *status,
2153 int *id,
2154 int *enabled)
2155{
2156 status_t lStatus = NO_ERROR;
2157 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002158 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002159
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002160 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002161 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002162 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002163
2164 if (pDesc == NULL) {
2165 lStatus = BAD_VALUE;
2166 goto Exit;
2167 }
2168
Eric Laurent84e9a102010-09-23 16:10:16 -07002169 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002170 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002171 lStatus = PERMISSION_DENIED;
2172 goto Exit;
2173 }
2174
Dima Zavinfce7a472011-04-19 22:30:36 -07002175 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002176 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002177 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002178 lStatus = PERMISSION_DENIED;
2179 goto Exit;
2180 }
2181
Mathias Agopian65ab4712010-07-14 17:59:35 -07002182 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002183 if (!EffectIsNullUuid(&pDesc->uuid)) {
2184 // if uuid is specified, request effect descriptor
2185 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2186 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002187 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002188 goto Exit;
2189 }
2190 } else {
2191 // if uuid is not specified, look for an available implementation
2192 // of the required type in effect factory
2193 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002194 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002195 lStatus = BAD_VALUE;
2196 goto Exit;
2197 }
2198 uint32_t numEffects = 0;
2199 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002200 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002201 bool found = false;
2202
2203 lStatus = EffectQueryNumberEffects(&numEffects);
2204 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002205 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002206 goto Exit;
2207 }
2208 for (uint32_t i = 0; i < numEffects; i++) {
2209 lStatus = EffectQueryEffect(i, &desc);
2210 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002211 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002212 continue;
2213 }
2214 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2215 // If matching type found save effect descriptor. If the session is
2216 // 0 and the effect is not auxiliary, continue enumeration in case
2217 // an auxiliary version of this effect type is available
2218 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002219 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002220 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002221 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2222 break;
2223 }
2224 }
2225 }
2226 if (!found) {
2227 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002228 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002229 goto Exit;
2230 }
2231 // For same effect type, chose auxiliary version over insert version if
2232 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002233 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002234 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002235 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002236 }
2237 }
2238
2239 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002240 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002241 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2242 lStatus = INVALID_OPERATION;
2243 goto Exit;
2244 }
2245
Eric Laurent59255e42011-07-27 19:49:51 -07002246 // check recording permission for visualizer
2247 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2248 !recordingAllowed()) {
2249 lStatus = PERMISSION_DENIED;
2250 goto Exit;
2251 }
2252
Mathias Agopian65ab4712010-07-14 17:59:35 -07002253 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002254 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002255 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2256 // if the output returned by getOutputForEffect() is removed before we lock the
2257 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2258 // and we will exit safely
2259 io = AudioSystem::getOutputForEffect(&desc);
2260 ALOGV("createEffect got output %d", io);
2261 }
2262
2263 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002264
2265 // If output is not specified try to find a matching audio session ID in one of the
2266 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002267 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2268 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002269 // Note: io is never 0 when creating an effect on an input
2270 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002271 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2272 // output must be specified by AudioPolicyManager when using session
2273 // AUDIO_SESSION_OUTPUT_STAGE
2274 lStatus = BAD_VALUE;
2275 goto Exit;
2276 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002277 // look for the thread where the specified audio session is present
2278 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2279 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2280 io = mPlaybackThreads.keyAt(i);
2281 break;
2282 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002283 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002284 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002285 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2286 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2287 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002288 break;
2289 }
2290 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002291 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002292 // If no output thread contains the requested session ID, default to
2293 // first output. The effect chain will be moved to the correct output
2294 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002295 if (io == 0 && mPlaybackThreads.size()) {
2296 io = mPlaybackThreads.keyAt(0);
2297 }
Steve Block3856b092011-10-20 11:56:00 +01002298 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002299 }
2300 ThreadBase *thread = checkRecordThread_l(io);
2301 if (thread == NULL) {
2302 thread = checkPlaybackThread_l(io);
2303 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002304 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002305 lStatus = BAD_VALUE;
2306 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002307 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002308 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002309
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002310 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002311
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002312 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002313 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2314 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002315 if (handle != 0 && id != NULL) {
2316 *id = handle->id();
2317 }
2318 }
2319
2320Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002321 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002322 return handle;
2323}
2324
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002325status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2326 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002327{
Steve Block3856b092011-10-20 11:56:00 +01002328 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002329 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002330 Mutex::Autolock _l(mLock);
2331 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002332 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002333 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002334 }
Eric Laurentde070132010-07-13 04:45:46 -07002335 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2336 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002337 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002338 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002339 }
Eric Laurentde070132010-07-13 04:45:46 -07002340 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2341 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002342 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002343 return BAD_VALUE;
2344 }
2345
2346 Mutex::Autolock _dl(dstThread->mLock);
2347 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002348 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002349}
2350
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002351// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002352status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002353 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002354 AudioFlinger::PlaybackThread *dstThread,
2355 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002356{
Steve Block3856b092011-10-20 11:56:00 +01002357 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002358 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002359
Eric Laurent59255e42011-07-27 19:49:51 -07002360 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002361 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002362 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002363 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002364 return INVALID_OPERATION;
2365 }
2366
Eric Laurent39e94f82010-07-28 01:32:47 -07002367 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002368 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002369 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002370 // removed.
2371 srcThread->removeEffectChain_l(chain);
2372
2373 // transfer all effects one by one so that new effect chain is created on new thread with
2374 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002375 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002376 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002377 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002378 Vector< sp<EffectModule> > removed;
2379 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002380 while (effect != 0) {
2381 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002382 removed.add(effect);
2383 status = dstThread->addEffect_l(effect);
2384 if (status != NO_ERROR) {
2385 break;
2386 }
Eric Laurentec35a142011-10-05 17:42:25 -07002387 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2388 if (effect->state() == EffectModule::ACTIVE ||
2389 effect->state() == EffectModule::STOPPING) {
2390 effect->start();
2391 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002392 // if the move request is not received from audio policy manager, the effect must be
2393 // re-registered with the new strategy and output
2394 if (dstChain == 0) {
2395 dstChain = effect->chain().promote();
2396 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002397 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002398 status = NO_INIT;
2399 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002400 }
2401 strategy = dstChain->strategy();
2402 }
2403 if (reRegister) {
2404 AudioSystem::unregisterEffect(effect->id());
2405 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002406 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002407 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002408 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002409 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002410 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002411 }
Eric Laurentde070132010-07-13 04:45:46 -07002412 effect = chain->getEffectFromId_l(0);
2413 }
2414
Eric Laurent5baf2af2013-09-12 17:37:00 -07002415 if (status != NO_ERROR) {
2416 for (size_t i = 0; i < removed.size(); i++) {
2417 srcThread->addEffect_l(removed[i]);
2418 if (dstChain != 0 && reRegister) {
2419 AudioSystem::unregisterEffect(removed[i]->id());
2420 AudioSystem::registerEffect(&removed[i]->desc(),
2421 srcThread->id(),
2422 strategy,
2423 sessionId,
2424 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002425 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002426 }
2427 }
2428 }
2429
2430 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002431}
2432
Eric Laurent5baf2af2013-09-12 17:37:00 -07002433bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002434{
2435 if (mGlobalEffectEnableTime != 0 &&
2436 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2437 return true;
2438 }
2439
2440 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2441 sp<EffectChain> ec =
2442 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002443 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002444 return true;
2445 }
2446 }
2447 return false;
2448}
2449
Eric Laurent5baf2af2013-09-12 17:37:00 -07002450void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002451{
2452 Mutex::Autolock _l(mLock);
2453
2454 mGlobalEffectEnableTime = systemTime();
2455
2456 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2457 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2458 if (t->mType == ThreadBase::OFFLOAD) {
2459 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2460 }
2461 }
2462
2463}
2464
Glenn Kastenda6ef132013-01-10 12:31:01 -08002465struct Entry {
2466#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2467 char mName[MAX_NAME];
2468};
2469
2470int comparEntry(const void *p1, const void *p2)
2471{
2472 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2473}
2474
Glenn Kasten46909e72013-02-26 09:20:22 -08002475#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002476void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002477{
Eric Laurent81784c32012-11-19 14:55:58 -08002478 NBAIO_Source *teeSource = source.get();
2479 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002480 // .wav rotation
2481 // There is a benign race condition if 2 threads call this simultaneously.
2482 // They would both traverse the directory, but the result would simply be
2483 // failures at unlink() which are ignored. It's also unlikely since
2484 // normally dumpsys is only done by bugreport or from the command line.
2485 char teePath[32+256];
2486 strcpy(teePath, "/data/misc/media");
2487 size_t teePathLen = strlen(teePath);
2488 DIR *dir = opendir(teePath);
2489 teePath[teePathLen++] = '/';
2490 if (dir != NULL) {
2491#define MAX_SORT 20 // number of entries to sort
2492#define MAX_KEEP 10 // number of entries to keep
2493 struct Entry entries[MAX_SORT];
2494 size_t entryCount = 0;
2495 while (entryCount < MAX_SORT) {
2496 struct dirent de;
2497 struct dirent *result = NULL;
2498 int rc = readdir_r(dir, &de, &result);
2499 if (rc != 0) {
2500 ALOGW("readdir_r failed %d", rc);
2501 break;
2502 }
2503 if (result == NULL) {
2504 break;
2505 }
2506 if (result != &de) {
2507 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2508 break;
2509 }
2510 // ignore non .wav file entries
2511 size_t nameLen = strlen(de.d_name);
2512 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2513 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2514 continue;
2515 }
2516 strcpy(entries[entryCount++].mName, de.d_name);
2517 }
2518 (void) closedir(dir);
2519 if (entryCount > MAX_KEEP) {
2520 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2521 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2522 strcpy(&teePath[teePathLen], entries[i].mName);
2523 (void) unlink(teePath);
2524 }
2525 }
2526 } else {
2527 if (fd >= 0) {
2528 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2529 }
2530 }
Eric Laurent81784c32012-11-19 14:55:58 -08002531 char teeTime[16];
2532 struct timeval tv;
2533 gettimeofday(&tv, NULL);
2534 struct tm tm;
2535 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002536 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2537 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2538 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2539 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002540 if (teeFd >= 0) {
2541 char wavHeader[44];
2542 memcpy(wavHeader,
2543 "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",
2544 sizeof(wavHeader));
2545 NBAIO_Format format = teeSource->format();
2546 unsigned channelCount = Format_channelCount(format);
2547 ALOG_ASSERT(channelCount <= FCC_2);
2548 uint32_t sampleRate = Format_sampleRate(format);
2549 wavHeader[22] = channelCount; // number of channels
2550 wavHeader[24] = sampleRate; // sample rate
2551 wavHeader[25] = sampleRate >> 8;
2552 wavHeader[32] = channelCount * 2; // block alignment
2553 write(teeFd, wavHeader, sizeof(wavHeader));
2554 size_t total = 0;
2555 bool firstRead = true;
2556 for (;;) {
2557#define TEE_SINK_READ 1024
2558 short buffer[TEE_SINK_READ * FCC_2];
2559 size_t count = TEE_SINK_READ;
2560 ssize_t actual = teeSource->read(buffer, count,
2561 AudioBufferProvider::kInvalidPTS);
2562 bool wasFirstRead = firstRead;
2563 firstRead = false;
2564 if (actual <= 0) {
2565 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2566 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002567 }
Eric Laurent81784c32012-11-19 14:55:58 -08002568 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002569 }
Eric Laurent81784c32012-11-19 14:55:58 -08002570 ALOG_ASSERT(actual <= (ssize_t)count);
2571 write(teeFd, buffer, actual * channelCount * sizeof(short));
2572 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002573 }
Eric Laurent81784c32012-11-19 14:55:58 -08002574 lseek(teeFd, (off_t) 4, SEEK_SET);
2575 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2576 write(teeFd, &temp, sizeof(temp));
2577 lseek(teeFd, (off_t) 40, SEEK_SET);
2578 temp = total * channelCount * sizeof(short);
2579 write(teeFd, &temp, sizeof(temp));
2580 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002581 if (fd >= 0) {
2582 fdprintf(fd, "tee copied to %s\n", teePath);
2583 }
Eric Laurent59255e42011-07-27 19:49:51 -07002584 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002585 if (fd >= 0) {
2586 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2587 }
Eric Laurent59255e42011-07-27 19:49:51 -07002588 }
2589 }
2590}
Glenn Kasten46909e72013-02-26 09:20:22 -08002591#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002592
Mathias Agopian65ab4712010-07-14 17:59:35 -07002593// ----------------------------------------------------------------------------
2594
2595status_t AudioFlinger::onTransact(
2596 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2597{
2598 return BnAudioFlinger::onTransact(code, data, reply, flags);
2599}
2600
Mathias Agopian65ab4712010-07-14 17:59:35 -07002601}; // namespace android