blob: f10c8e0374976d8a606bf7b1a9bf25c2083b025b [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Alok Kediya601e9b52013-09-27 11:55:10 +05302** Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
3** Not a Contribution.
Mathias Agopian65ab4712010-07-14 17:59:35 -07004** Copyright 2007, The Android Open Source Project
5**
6** Licensed under the Apache License, Version 2.0 (the "License");
7** you may not use this file except in compliance with the License.
8** You may obtain a copy of the License at
9**
10** http://www.apache.org/licenses/LICENSE-2.0
11**
12** Unless required by applicable law or agreed to in writing, software
13** distributed under the License is distributed on an "AS IS" BASIS,
14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15** See the License for the specific language governing permissions and
16** limitations under the License.
17*/
18
19
20#define LOG_TAG "AudioFlinger"
21//#define LOG_NDEBUG 0
22
Glenn Kasten153b9fe2013-07-15 11:23:36 -070023#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080024#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <math.h>
26#include <signal.h>
27#include <sys/time.h>
28#include <sys/resource.h>
29
Gloria Wang9ee159b2011-02-24 14:51:45 -080030#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070031#include <binder/IServiceManager.h>
32#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070033#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/String16.h>
36#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070037#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
Dima Zavinfce7a472011-04-19 22:30:36 -070039#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070040#include <cutils/properties.h>
41
Dima Zavin64760242011-05-11 14:15:23 -070042#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070043#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
45#include "AudioMixer.h"
46#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080047#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
Mathias Agopian65ab4712010-07-14 17:59:35 -070049#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070050#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070051#include <audio_effects/effect_ns.h>
52#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Glenn Kasten3b21c502011-12-15 09:52:39 -080054#include <audio_utils/primitives.h>
55
Eric Laurentfeb0db62011-07-22 09:04:31 -070056#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080057
John Grossman4ff14ba2012-02-08 16:37:41 -080058#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070059
Glenn Kasten9e58b552013-01-18 15:09:48 -080060#include <media/IMediaLogService.h>
61
Glenn Kastenda6ef132013-01-10 12:31:01 -080062#include <media/nbaio/Pipe.h>
63#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070064#include <media/AudioParameter.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070065#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080066
Alok Kediya601e9b52013-09-27 11:55:10 +053067#ifdef SRS_PROCESSING
68#include "srs_processing.h"
69#include "postpro_patch_ics.h"
70#endif
71
Mathias Agopian65ab4712010-07-14 17:59:35 -070072// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070073
John Grossman1c345192012-03-27 14:00:17 -070074// Note: the following macro is used for extremely verbose logging message. In
75// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
76// 0; but one side effect of this is to turn all LOGV's as well. Some messages
77// are so verbose that we want to suppress them even when we have ALOG_ASSERT
78// turned on. Do not uncomment the #def below unless you really know what you
79// are doing and want to see all of the extremely verbose messages.
80//#define VERY_VERY_VERBOSE_LOGGING
81#ifdef VERY_VERY_VERBOSE_LOGGING
82#define ALOGVV ALOGV
83#else
84#define ALOGVV(a...) do { } while(0)
85#endif
Eric Laurentde070132010-07-13 04:45:46 -070086
Mathias Agopian65ab4712010-07-14 17:59:35 -070087namespace android {
88
Glenn Kastenec1d6b52011-12-12 09:04:45 -080089static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
90static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070091
Glenn Kasten58912562012-04-03 10:45:00 -070092
John Grossman4ff14ba2012-02-08 16:37:41 -080093nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080094
Eric Laurent81784c32012-11-19 14:55:58 -080095uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070096
Glenn Kasten46909e72013-02-26 09:20:22 -080097#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080098bool AudioFlinger::mTeeSinkInputEnabled = false;
99bool AudioFlinger::mTeeSinkOutputEnabled = false;
100bool AudioFlinger::mTeeSinkTrackEnabled = false;
101
102size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
103size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
104size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800105#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800106
Eric Laurent813e2a72013-08-31 12:59:48 -0700107// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
108// we define a minimum time during which a global effect is considered enabled.
109static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
110
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111// ----------------------------------------------------------------------------
112
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700113static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700114{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700115 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700116 int rc;
117
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700118 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
119 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
120 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
121 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700122 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700123 }
124 rc = audio_hw_device_open(mod, dev);
125 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
126 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
127 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700128 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700129 }
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +0000130#if !defined(ICS_AUDIO_BLOB) && !defined(MR0_AUDIO_BLOB)
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700131 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
132 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
133 rc = BAD_VALUE;
134 goto out;
135 }
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +0000136#endif
Dima Zavin799a70e2011-04-18 16:57:27 -0700137 return 0;
138
139out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700140 *dev = NULL;
141 return rc;
142}
143
Mathias Agopian65ab4712010-07-14 17:59:35 -0700144// ----------------------------------------------------------------------------
145
146AudioFlinger::AudioFlinger()
147 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800148 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700149 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800150 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800151 mMasterMute(false),
152 mNextUniqueId(1),
153 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700154 mBtNrecIsOff(false),
155 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700156 mIsDeviceTypeKnown(false),
Ethan Chen8d420ed2013-11-14 00:40:54 -0800157#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530158 mGlobalEffectEnableTime(0),
159 mAllChainsLocked(false)
Ethan Chen8d420ed2013-11-14 00:40:54 -0800160#else
161 mGlobalEffectEnableTime(0)
162#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163{
Glenn Kasten949a9262013-04-16 12:35:20 -0700164 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800165 char value[PROPERTY_VALUE_MAX];
166 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
167 if (doLog) {
168 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
169 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800170#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800171 (void) property_get("ro.debuggable", value, "0");
172 int debuggable = atoi(value);
173 int teeEnabled = 0;
174 if (debuggable) {
175 (void) property_get("af.tee", value, "0");
176 teeEnabled = atoi(value);
177 }
178 if (teeEnabled & 1)
179 mTeeSinkInputEnabled = true;
180 if (teeEnabled & 2)
181 mTeeSinkOutputEnabled = true;
182 if (teeEnabled & 4)
183 mTeeSinkTrackEnabled = true;
Glenn Kasten46909e72013-02-26 09:20:22 -0800184#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700185}
186
187void AudioFlinger::onFirstRef()
188{
Dima Zavin799a70e2011-04-18 16:57:27 -0700189 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700190
Eric Laurent93575202011-01-18 18:39:02 -0800191 Mutex::Autolock _l(mLock);
192
Dima Zavin799a70e2011-04-18 16:57:27 -0700193 /* TODO: move all this work into an Init() function */
Steve Kondik70d6eb92013-11-27 00:11:13 -0800194#ifdef QCOM_HARDWARE
Santhosh Kumar Thimmanna Bhattar03fd1d52013-10-30 11:29:19 +0530195 mLPASessionId = -2; // -2 is invalid session ID
196 mIsEffectConfigChanged = false;
197 mLPAEffectChain = NULL;
Steve Kondik70d6eb92013-11-27 00:11:13 -0800198#endif
John Grossman4ff14ba2012-02-08 16:37:41 -0800199 char val_str[PROPERTY_VALUE_MAX] = { 0 };
200 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
201 uint32_t int_val;
202 if (1 == sscanf(val_str, "%u", &int_val)) {
203 mStandbyTimeInNsecs = milliseconds(int_val);
204 ALOGI("Using %u mSec as standby time.", int_val);
205 } else {
206 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
207 ALOGI("Using default %u mSec as standby time.",
208 (uint32_t)(mStandbyTimeInNsecs / 1000000));
209 }
210 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211
Eric Laurenta4c5a552012-03-29 10:12:40 -0700212 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213}
214
215AudioFlinger::~AudioFlinger()
216{
217 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700218 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700219 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220 }
221 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700222 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700223 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700224 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700225
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800226 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
227 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700228 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
229 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230 }
231}
232
Eric Laurenta4c5a552012-03-29 10:12:40 -0700233static const char * const audio_interfaces[] = {
234 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
235 AUDIO_HARDWARE_MODULE_ID_A2DP,
236 AUDIO_HARDWARE_MODULE_ID_USB,
237};
238#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
239
John Grossmanee578c02012-07-23 17:05:46 -0700240AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
241 audio_module_handle_t module,
242 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700243{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700244 // if module is 0, the request comes from an old policy manager and we should load
245 // well known modules
246 if (module == 0) {
247 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
248 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
249 loadHwModule_l(audio_interfaces[i]);
250 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700251 // then try to find a module supporting the requested device.
252 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
253 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
254 audio_hw_device_t *dev = audioHwDevice->hwDevice();
255 if ((dev->get_supported_devices != NULL) &&
256 (dev->get_supported_devices(dev) & devices) == devices)
257 return audioHwDevice;
Ricardo Cerqueira2b3c7972013-01-10 23:53:09 +0000258#ifdef ICS_AUDIO_BLOB
259 else if (dev->get_supported_devices == NULL && i != 0 &&
260 devices == 0x80)
261 // Reasonably safe assumption: A non-primary HAL without
262 // get_supported_devices is a locally-built A2DP binary
263 return audioHwDevice;
264#endif
Eric Laurentf1c04f92012-08-28 14:26:53 -0700265 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700266 } else {
267 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700268 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
269 if (audioHwDevice != NULL) {
270 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700271 }
272 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700273
Dima Zavin799a70e2011-04-18 16:57:27 -0700274 return NULL;
275}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700277void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278{
279 const size_t SIZE = 256;
280 char buffer[SIZE];
281 String8 result;
282
283 result.append("Clients:\n");
284 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800285 sp<Client> client = mClients.valueAt(i).promote();
286 if (client != 0) {
287 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
288 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289 }
290 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700291
Eric Laurentd1b28d42013-09-18 18:47:13 -0700292 result.append("Notification Clients:\n");
293 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
294 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
295 result.append(buffer);
296 }
297
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700298 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800299 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700300 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
301 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800302 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700303 result.append(buffer);
304 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306}
307
308
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700309void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310{
311 const size_t SIZE = 256;
312 char buffer[SIZE];
313 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800314 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315
John Grossman4ff14ba2012-02-08 16:37:41 -0800316 snprintf(buffer, SIZE, "Hardware status: %d\n"
317 "Standby Time mSec: %u\n",
318 hardwareStatus,
319 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 result.append(buffer);
321 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322}
323
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700324void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325{
326 const size_t SIZE = 256;
327 char buffer[SIZE];
328 String8 result;
329 snprintf(buffer, SIZE, "Permission Denial: "
330 "can't dump AudioFlinger from pid=%d, uid=%d\n",
331 IPCThreadState::self()->getCallingPid(),
332 IPCThreadState::self()->getCallingUid());
333 result.append(buffer);
334 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335}
336
Eric Laurent81784c32012-11-19 14:55:58 -0800337bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338{
339 bool locked = false;
340 for (int i = 0; i < kDumpLockRetries; ++i) {
341 if (mutex.tryLock() == NO_ERROR) {
342 locked = true;
343 break;
344 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800345 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346 }
347 return locked;
348}
349
350status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
351{
Glenn Kasten44deb052012-02-05 18:09:08 -0800352 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 dumpPermissionDenial(fd, args);
354 } else {
355 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800356 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357 if (!hardwareLocked) {
358 String8 result(kHardwareLockedString);
359 write(fd, result.string(), result.size());
360 } else {
361 mHardwareLock.unlock();
362 }
363
Eric Laurent81784c32012-11-19 14:55:58 -0800364 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365
366 // failed to lock - AudioFlinger is probably deadlocked
367 if (!locked) {
368 String8 result(kDeadlockedString);
369 write(fd, result.string(), result.size());
370 }
371
372 dumpClients(fd, args);
373 dumpInternals(fd, args);
374
375 // dump playback threads
376 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
377 mPlaybackThreads.valueAt(i)->dump(fd, args);
378 }
379
380 // dump record threads
381 for (size_t i = 0; i < mRecordThreads.size(); i++) {
382 mRecordThreads.valueAt(i)->dump(fd, args);
383 }
384
Dima Zavin799a70e2011-04-18 16:57:27 -0700385 // dump all hardware devs
386 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700387 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700388 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700390
Glenn Kasten46909e72013-02-26 09:20:22 -0800391#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700392 // dump the serially shared record tee sink
393 if (mRecordTeeSource != 0) {
394 dumpTee(fd, mRecordTeeSource);
395 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800396#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700397
Glenn Kastend65d73c2012-06-22 17:21:07 -0700398 if (locked) {
399 mLock.unlock();
400 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800401
402 // append a copy of media.log here by forwarding fd to it, but don't attempt
403 // to lookup the service if it's not running, as it will block for a second
404 if (mLogMemoryDealer != 0) {
405 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
406 if (binder != 0) {
407 fdprintf(fd, "\nmedia.log:\n");
408 Vector<String16> args;
409 binder->dump(fd, args);
410 }
411 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 }
413 return NO_ERROR;
414}
415
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800416sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
417{
418 // If pid is already in the mClients wp<> map, then use that entry
419 // (for which promote() is always != 0), otherwise create a new entry and Client.
420 sp<Client> client = mClients.valueFor(pid).promote();
421 if (client == 0) {
422 client = new Client(this, pid);
423 mClients.add(pid, client);
424 }
425
426 return client;
427}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428
Glenn Kasten9e58b552013-01-18 15:09:48 -0800429sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
430{
431 if (mLogMemoryDealer == 0) {
432 return new NBLog::Writer();
433 }
434 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
435 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
436 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
437 if (binder != 0) {
438 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
439 }
440 return writer;
441}
442
443void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
444{
Glenn Kasten685ef092013-02-04 08:15:34 -0800445 if (writer == 0) {
446 return;
447 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800448 sp<IMemory> iMemory(writer->getIMemory());
449 if (iMemory == 0) {
450 return;
451 }
452 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
453 if (binder != 0) {
454 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
455 // Now the media.log remote reference to IMemory is gone.
456 // When our last local reference to IMemory also drops to zero,
457 // the IMemory destructor will deallocate the region from mMemoryDealer.
458 }
459}
460
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461// IAudioFlinger interface
462
463
464sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800465 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800467 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700468 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800469 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800470 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700471 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800472 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800473 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700474 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700475 String8& name,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800476 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700477 status_t *status)
478{
479 sp<PlaybackThread::Track> track;
480 sp<TrackHandle> trackHandle;
481 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700482 status_t lStatus;
483 int lSessionId;
484
Glenn Kasten263709e2012-01-06 08:40:01 -0800485 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
486 // but if someone uses binder directly they could bypass that and cause us to crash
487 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000488 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700489 lStatus = BAD_VALUE;
490 goto Exit;
491 }
492
Glenn Kasten60a83922012-06-21 12:56:37 -0700493 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
494 // and we don't yet support 8.24 or 32-bit PCM
495 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
496 ALOGE("createTrack() invalid format %d", format);
497 lStatus = BAD_VALUE;
498 goto Exit;
499 }
500
Mathias Agopian65ab4712010-07-14 17:59:35 -0700501 {
502 Mutex::Autolock _l(mLock);
503 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700504 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800506 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700507 lStatus = BAD_VALUE;
508 goto Exit;
509 }
510
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800511 pid_t pid = IPCThreadState::self()->getCallingPid();
Marco Nelissen9cae2172013-01-14 14:12:05 -0800512
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800513 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700514
Steve Block3856b092011-10-20 11:56:00 +0100515 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700516 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700517 // check if an effect chain with the same session ID is present on another
518 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700519 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700520 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
521 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700522 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700523 if (sessions & PlaybackThread::EFFECT_SESSION) {
524 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700525 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700526 }
Eric Laurentde070132010-07-13 04:45:46 -0700527 }
528 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700529 lSessionId = *sessionId;
530 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700531 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700532 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700533 if (sessionId != NULL) {
534 *sessionId = lSessionId;
535 }
536 }
Steve Block3856b092011-10-20 11:56:00 +0100537 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538
539 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800540 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew Georgee010f652013-12-13 15:40:13 -0800541 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
542 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700543
544 // move effect chain to this output thread if an effect on same session was waiting
545 // for a track to be created
546 if (lStatus == NO_ERROR && effectThread != NULL) {
547 Mutex::Autolock _dl(thread->mLock);
548 Mutex::Autolock _sl(effectThread->mLock);
549 moveEffectChain_l(lSessionId, effectThread, thread, true);
550 }
Eric Laurenta011e352012-03-29 15:51:43 -0700551
552 // Look for sync events awaiting for a session to be used.
553 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
554 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
555 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700556 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700557 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700558 } else {
559 mPendingSyncEvents[i]->cancel();
560 }
Eric Laurenta011e352012-03-29 15:51:43 -0700561 mPendingSyncEvents.removeAt(i);
562 i--;
563 }
564 }
565 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700566 }
567 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700568 // s for server's pid, n for normal mixer name, f for fast index
569 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
570 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571 trackHandle = new TrackHandle(track);
572 } else {
573 // remove local strong reference to Client before deleting the Track so that the Client
574 // destructor is called by the TrackBase destructor with mLock held
575 client.clear();
576 track.clear();
577 }
578
579Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700580 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581 *status = lStatus;
582 }
583 return trackHandle;
584}
585
Ethan Chen8d420ed2013-11-14 00:40:54 -0800586#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530587sp<IDirectTrack> AudioFlinger::createDirectTrack(
588 pid_t pid,
589 uint32_t sampleRate,
590 audio_channel_mask_t channelMask,
591 audio_io_handle_t output,
592 int *sessionId,
593 IDirectTrackClient *client,
594 audio_stream_type_t streamType,
595 status_t *status)
596{
597 *status = NO_ERROR;
598 status_t lStatus = NO_ERROR;
599 sp<IDirectTrack> track = NULL;
600 DirectAudioTrack* directTrack = NULL;
601 Mutex::Autolock _l(mLock);
602
603 ALOGV("createDirectTrack() sessionId: %d sampleRate %d channelMask %d",
604 *sessionId, sampleRate, channelMask);
605 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
606 if(desc == NULL) {
607 ALOGE("Error: Invalid output (%d) to create direct audio track", output);
608 lStatus = BAD_VALUE;
609 goto Exit;
610 }
611 desc->mStreamType = streamType;
612 if (desc->flag & AUDIO_OUTPUT_FLAG_LPA) {
613 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
614 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
615 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
616 // Check if the session ID is already associated with a track
617 uint32_t sessions = t->hasAudioSession(*sessionId);
618
619 // check if an effect with same session ID is waiting for a ssession to be created
620 ALOGV("check if an effect with same session ID is waiting for a ssession to be created");
621 if ((mLPAEffectChain == NULL) && (sessions & PlaybackThread::EFFECT_SESSION)) {
622 // Clear reference to previous effect chain if any
623 t->mLock.lock();
624 ALOGV("getting the LPA effect chain and setting LPA flag to true.");
625 mLPAEffectChain = t->getEffectChain_l(*sessionId);
626 t->mLock.unlock();
627 }
628 }
629 mLPASessionId = *sessionId;
630 if (mLPAEffectChain != NULL) {
631 mLPAEffectChain->setLPAFlag(true);
632 // For LPA, the volume will be applied in DSP. No need for volume
633 // control in the Effect chain, so setting it to unity.
634 uint32_t volume = 0x1000000; // Equals to 1.0 in 8.24 format
635 mLPAEffectChain->setVolume_l(&volume,&volume);
636 } else {
637 ALOGW("There was no effectChain created for the sessionId(%d)", mLPASessionId);
638 }
639 mLPASampleRate = sampleRate;
640 mLPANumChannels = popcount(channelMask);
641 } else {
642 if(sessionId != NULL) {
643 ALOGE("Error: Invalid sessionID (%d) for direct audio track", *sessionId);
644 }
645 }
646 }
647 mLock.unlock();
648 directTrack = new DirectAudioTrack(this, output, desc, client, desc->flag);
649 desc->trackRefPtr = dynamic_cast<void *>(directTrack);
650 mLock.lock();
651 if (directTrack != 0) {
652 track = dynamic_cast<IDirectTrack *>(directTrack);
653 AudioEventObserver* obv = dynamic_cast<AudioEventObserver *>(directTrack);
654 ALOGE("setting observer mOutputDesc track %p, obv %p", track.get(), obv);
655 desc->stream->set_observer(desc->stream, reinterpret_cast<void *>(obv));
656 } else {
657 lStatus = BAD_VALUE;
658 }
659Exit:
660 if(lStatus) {
661 if (track != NULL) {
662 track.clear();
663 }
664 *status = lStatus;
665 }
666 return track;
667}
668
669void AudioFlinger::deleteEffectSession()
670{
671 ALOGV("deleteSession");
672 // -2 is invalid session ID
673 mLPASessionId = -2;
674 if (mLPAEffectChain != NULL) {
675 mLPAEffectChain->lock();
676 mLPAEffectChain->setLPAFlag(false);
677 size_t i, numEffects = mLPAEffectChain->getNumEffects();
678 for(i = 0; i < numEffects; i++) {
679 sp<EffectModule> effect = mLPAEffectChain->getEffectFromIndex_l(i);
680 effect->setInBuffer(mLPAEffectChain->inBuffer());
681 if (i == numEffects-1) {
682 effect->setOutBuffer(mLPAEffectChain->outBuffer());
683 } else {
684 effect->setOutBuffer(mLPAEffectChain->inBuffer());
685 }
686 effect->configure();
687 }
688 mLPAEffectChain->unlock();
689 mLPAEffectChain.clear();
690 mLPAEffectChain = NULL;
691 }
692}
Ethan Chen8d420ed2013-11-14 00:40:54 -0800693#endif
Alok Kediya601e9b52013-09-27 11:55:10 +0530694
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800695uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696{
697 Mutex::Autolock _l(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800698#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530699 if (!mDirectAudioTracks.isEmpty()) {
700 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
701 if(desc != NULL) {
702 return desc->stream->common.get_sample_rate(&desc->stream->common);
703 }
704 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800705#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706 PlaybackThread *thread = checkPlaybackThread_l(output);
707 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000708 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709 return 0;
710 }
711 return thread->sampleRate();
712}
713
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800714int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715{
716 Mutex::Autolock _l(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800717#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530718 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
719 if(desc != NULL) {
720 return desc->stream->common.get_channels(&desc->stream->common);
721 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800722#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723 PlaybackThread *thread = checkPlaybackThread_l(output);
724 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000725 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726 return 0;
727 }
728 return thread->channelCount();
729}
730
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800731audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732{
733 Mutex::Autolock _l(mLock);
734 PlaybackThread *thread = checkPlaybackThread_l(output);
735 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000736 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800737 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700738 }
739 return thread->format();
740}
741
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800742size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743{
744 Mutex::Autolock _l(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800745#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530746 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
747 if(desc != NULL) {
748 return desc->stream->common.get_buffer_size(&desc->stream->common);
749 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800750#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751 PlaybackThread *thread = checkPlaybackThread_l(output);
752 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000753 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754 return 0;
755 }
Glenn Kasten58912562012-04-03 10:45:00 -0700756 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
757 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758 return thread->frameCount();
759}
760
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800761uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700762{
763 Mutex::Autolock _l(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800764#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530765 if (!mDirectAudioTracks.isEmpty()) {
766 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
767 if (desc != NULL) {
768 return desc->stream->get_latency(desc->stream);
769 }
770 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800771#endif
Alok Kediya601e9b52013-09-27 11:55:10 +0530772
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773 PlaybackThread *thread = checkPlaybackThread_l(output);
774 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800775 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776 return 0;
777 }
778 return thread->latency();
779}
780
781status_t AudioFlinger::setMasterVolume(float value)
782{
Eric Laurenta1884f92011-08-23 08:25:03 -0700783 status_t ret = initCheck();
784 if (ret != NO_ERROR) {
785 return ret;
786 }
787
Mathias Agopian65ab4712010-07-14 17:59:35 -0700788 // check calling permissions
789 if (!settingsAllowed()) {
790 return PERMISSION_DENIED;
791 }
792
Eric Laurenta4c5a552012-03-29 10:12:40 -0700793 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700794 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700795
John Grossmanee578c02012-07-23 17:05:46 -0700796 // Set master volume in the HALs which support it.
797 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
798 AutoMutex lock(mHardwareLock);
799 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800800
John Grossmanee578c02012-07-23 17:05:46 -0700801 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
802 if (dev->canSetMasterVolume()) {
803 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800804 }
John Grossmanee578c02012-07-23 17:05:46 -0700805 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700806 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807
John Grossmanee578c02012-07-23 17:05:46 -0700808 // Now set the master volume in each playback thread. Playback threads
809 // assigned to HALs which do not have master volume support will apply
810 // master volume during the mix operation. Threads with HALs which do
811 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800812 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700813 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814
815 return NO_ERROR;
816}
817
Glenn Kastenf78aee72012-01-04 11:00:47 -0800818status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819{
Eric Laurenta1884f92011-08-23 08:25:03 -0700820 status_t ret = initCheck();
821 if (ret != NO_ERROR) {
822 return ret;
823 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824
825 // check calling permissions
826 if (!settingsAllowed()) {
827 return PERMISSION_DENIED;
828 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800829 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000830 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831 return BAD_VALUE;
832 }
833
834 { // scope for the lock
835 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700836 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700838 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839 mHardwareStatus = AUDIO_HW_IDLE;
840 }
841
842 if (NO_ERROR == ret) {
843 Mutex::Autolock _l(mLock);
844 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800845 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700846 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847 }
848
849 return ret;
850}
851
852status_t AudioFlinger::setMicMute(bool state)
853{
Eric Laurenta1884f92011-08-23 08:25:03 -0700854 status_t ret = initCheck();
855 if (ret != NO_ERROR) {
856 return ret;
857 }
858
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 // check calling permissions
860 if (!settingsAllowed()) {
861 return PERMISSION_DENIED;
862 }
863
864 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700865 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700867 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700868 mHardwareStatus = AUDIO_HW_IDLE;
869 return ret;
870}
871
872bool AudioFlinger::getMicMute() const
873{
Eric Laurenta1884f92011-08-23 08:25:03 -0700874 status_t ret = initCheck();
875 if (ret != NO_ERROR) {
876 return false;
877 }
878
Dima Zavinfce7a472011-04-19 22:30:36 -0700879 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800880 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700881 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700883 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884 mHardwareStatus = AUDIO_HW_IDLE;
885 return state;
886}
887
888status_t AudioFlinger::setMasterMute(bool muted)
889{
John Grossmand8f178d2012-07-20 14:51:35 -0700890 status_t ret = initCheck();
891 if (ret != NO_ERROR) {
892 return ret;
893 }
894
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895 // check calling permissions
896 if (!settingsAllowed()) {
897 return PERMISSION_DENIED;
898 }
899
John Grossmanee578c02012-07-23 17:05:46 -0700900 Mutex::Autolock _l(mLock);
901 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700902
Emerson Pinter6d327d52013-07-28 19:34:16 -0300903#ifndef ICS_AUDIO_BLOB
John Grossmanee578c02012-07-23 17:05:46 -0700904 // Set master mute in the HALs which support it.
905 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
906 AutoMutex lock(mHardwareLock);
907 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700908
John Grossmanee578c02012-07-23 17:05:46 -0700909 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
910 if (dev->canSetMasterMute()) {
911 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700912 }
John Grossmanee578c02012-07-23 17:05:46 -0700913 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700914 }
915
John Grossmanee578c02012-07-23 17:05:46 -0700916 // Now set the master mute in each playback thread. Playback threads
917 // assigned to HALs which do not have master mute support will apply master
918 // mute during the mix operation. Threads with HALs which do support master
919 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800920 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700921 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +0000922#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923
924 return NO_ERROR;
925}
926
927float AudioFlinger::masterVolume() const
928{
Glenn Kasten98067102011-12-13 11:47:54 -0800929 Mutex::Autolock _l(mLock);
930 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931}
932
933bool AudioFlinger::masterMute() const
934{
Glenn Kasten98067102011-12-13 11:47:54 -0800935 Mutex::Autolock _l(mLock);
936 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937}
938
John Grossman4ff14ba2012-02-08 16:37:41 -0800939float AudioFlinger::masterVolume_l() const
940{
John Grossman4ff14ba2012-02-08 16:37:41 -0800941 return mMasterVolume;
942}
943
John Grossmand8f178d2012-07-20 14:51:35 -0700944bool AudioFlinger::masterMute_l() const
945{
John Grossmanee578c02012-07-23 17:05:46 -0700946 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700947}
948
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800949status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
950 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951{
952 // check calling permissions
953 if (!settingsAllowed()) {
954 return PERMISSION_DENIED;
955 }
956
Glenn Kasten263709e2012-01-06 08:40:01 -0800957 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000958 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700959 return BAD_VALUE;
960 }
961
962 AutoMutex lock(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800963#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530964 ALOGV("setStreamVolume stream %d, output %d, value %f",stream, output, value);
965 AudioSessionDescriptor *desc = NULL;
966 if (!mDirectAudioTracks.isEmpty()) {
967 desc = mDirectAudioTracks.valueFor(output);
968 if (desc != NULL) {
969 ALOGV("setStreamVolume for mAudioTracks size %d desc %p",mDirectAudioTracks.size(),desc);
970 if (desc->mStreamType == stream) {
971 mStreamTypes[stream].volume = value;
972 desc->mVolumeScale = value;
973 desc->stream->set_volume(desc->stream,
974 desc->mVolumeLeft * mStreamTypes[stream].volume,
975 desc->mVolumeRight* mStreamTypes[stream].volume);
976 return NO_ERROR;
977 }
978 }
979 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800980#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 PlaybackThread *thread = NULL;
982 if (output) {
983 thread = checkPlaybackThread_l(output);
984 if (thread == NULL) {
Ethan Chen8d420ed2013-11-14 00:40:54 -0800985#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530986 if (desc != NULL) {
987 return NO_ERROR;
988 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800989#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990 return BAD_VALUE;
991 }
992 }
993
994 mStreamTypes[stream].volume = value;
995
996 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800997 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700998 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999 }
1000 } else {
1001 thread->setStreamVolume(stream, value);
1002 }
1003
1004 return NO_ERROR;
1005}
1006
Glenn Kastenfff6d712012-01-12 16:38:12 -08001007status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008{
1009 // check calling permissions
1010 if (!settingsAllowed()) {
1011 return PERMISSION_DENIED;
1012 }
1013
Glenn Kasten263709e2012-01-06 08:40:01 -08001014 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -07001015 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001016 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001017 return BAD_VALUE;
1018 }
1019
Eric Laurent93575202011-01-18 18:39:02 -08001020 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021 mStreamTypes[stream].mute = muted;
1022 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001023 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001024
1025 return NO_ERROR;
1026}
1027
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001028float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001029{
Glenn Kasten263709e2012-01-06 08:40:01 -08001030 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001031 return 0.0f;
1032 }
1033
1034 AutoMutex lock(mLock);
1035 float volume;
1036 if (output) {
1037 PlaybackThread *thread = checkPlaybackThread_l(output);
1038 if (thread == NULL) {
1039 return 0.0f;
1040 }
1041 volume = thread->streamVolume(stream);
1042 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001043 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001044 }
1045
1046 return volume;
1047}
1048
Glenn Kastenfff6d712012-01-12 16:38:12 -08001049bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050{
Glenn Kasten263709e2012-01-06 08:40:01 -08001051 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052 return true;
1053 }
1054
Glenn Kasten6637baa2012-01-09 09:40:36 -08001055 AutoMutex lock(mLock);
1056 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001057}
1058
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001059status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001061 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1062 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001063
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064 // check calling permissions
1065 if (!settingsAllowed()) {
1066 return PERMISSION_DENIED;
1067 }
1068
Mathias Agopian65ab4712010-07-14 17:59:35 -07001069 // ioHandle == 0 means the parameters are global to the audio hardware interface
1070 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001071 Mutex::Autolock _l(mLock);
Alok Kediya601e9b52013-09-27 11:55:10 +05301072#ifdef SRS_PROCESSING
1073 POSTPRO_PATCH_ICS_PARAMS_SET(keyValuePairs);
1074 if (!mDirectAudioTracks.isEmpty())
1075 audioConfigChanged_l(AudioSystem::EFFECT_CONFIG_CHANGED, 0, NULL);
1076#endif
Dima Zavin799a70e2011-04-18 16:57:27 -07001077 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001078 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001079 AutoMutex lock(mHardwareLock);
1080 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1081 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1082 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1083 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1084 final_result = result ?: final_result;
1085 }
1086 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001087 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001088 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1089 AudioParameter param = AudioParameter(keyValuePairs);
1090 String8 value;
1091 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001092 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1093 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001094 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1095 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001096 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001097 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1098 // collect all of the thread's session IDs
1099 KeyedVector<int, bool> ids = thread->sessionIds();
1100 // suspend effects associated with those session IDs
1101 for (size_t j = 0; j < ids.size(); ++j) {
1102 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001103 thread->setEffectSuspended(FX_IID_AEC,
1104 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001105 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001106 thread->setEffectSuspended(FX_IID_NS,
1107 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001108 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001109 }
1110 }
Eric Laurentbee53372011-08-29 12:42:48 -07001111 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001112 }
1113 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001114 String8 screenState;
1115 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1116 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001117 if (isOff != (AudioFlinger::mScreenState & 1)) {
1118 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001119 }
1120 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001121 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001122 }
1123
Ethan Chen8d420ed2013-11-14 00:40:54 -08001124#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301125 AudioSessionDescriptor *desc = NULL;
1126 if (!mDirectAudioTracks.isEmpty()) {
1127 desc = mDirectAudioTracks.valueFor(ioHandle);
1128 if (desc != NULL) {
1129 ALOGV("setParameters for mAudioTracks size %d desc %p",mDirectAudioTracks.size(),desc);
1130 desc->stream->common.set_parameters(&desc->stream->common, keyValuePairs.string());
1131 AudioParameter param = AudioParameter(keyValuePairs);
1132 String8 key = String8(AudioParameter::keyRouting);
1133 int device;
1134 if (param.getInt(key, device) == NO_ERROR) {
1135 mDirectDevice = device;
1136#ifdef SRS_PROCESSING
1137 ALOGV("setParameters:: routing change to device %d", device);
1138 POSTPRO_PATCH_ICS_OUTPROC_MIX_ROUTE(desc->trackRefPtr, param, device);
Santhosh Kumar Thimmanna Bhattar03fd1d52013-10-30 11:29:19 +05301139 if(desc->flag & AUDIO_OUTPUT_FLAG_TUNNEL)
1140 audioConfigChanged_l(AudioSystem::EFFECT_CONFIG_CHANGED, 0, NULL);
Alok Kediya601e9b52013-09-27 11:55:10 +05301141#endif
1142 if(mLPAEffectChain != NULL){
1143 mLPAEffectChain->setDevice_l(device);
1144 audioConfigChanged_l(AudioSystem::EFFECT_CONFIG_CHANGED, 0, NULL);
1145 }
1146 }
1147 }
1148 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001149#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301150
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1152 // and the thread is exited once the lock is released
1153 sp<ThreadBase> thread;
1154 {
1155 Mutex::Autolock _l(mLock);
1156 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001157 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001159 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001160 // indicate output device change to all input threads for pre processing
1161 AudioParameter param = AudioParameter(keyValuePairs);
1162 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001163 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1164 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001165 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1166 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1167 }
1168 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001169 }
1170 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001171 if (thread != 0) {
1172 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001173 }
1174 return BAD_VALUE;
1175}
1176
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001177String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001179 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1180 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001181
Eric Laurenta4c5a552012-03-29 10:12:40 -07001182 Mutex::Autolock _l(mLock);
1183
Mathias Agopian65ab4712010-07-14 17:59:35 -07001184 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001185 String8 out_s8;
1186
Alok Kediya601e9b52013-09-27 11:55:10 +05301187#ifdef SRS_PROCESSING
1188 POSTPRO_PATCH_ICS_PARAMS_GET(keys, out_s8);
1189#endif
Dima Zavin799a70e2011-04-18 16:57:27 -07001190 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001191 char *s;
1192 {
1193 AutoMutex lock(mHardwareLock);
1194 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001195 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001196 s = dev->get_parameters(dev, keys.string());
1197 mHardwareStatus = AUDIO_HW_IDLE;
1198 }
John Grossmanef7740b2012-02-09 11:28:36 -08001199 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001200 free(s);
1201 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001202 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203 }
1204
Mathias Agopian65ab4712010-07-14 17:59:35 -07001205 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1206 if (playbackThread != NULL) {
1207 return playbackThread->getParameters(keys);
1208 }
1209 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1210 if (recordThread != NULL) {
1211 return recordThread->getParameters(keys);
1212 }
1213 return String8("");
1214}
1215
Glenn Kastendd8104c2012-07-02 12:42:44 -07001216size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1217 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001218{
Eric Laurenta1884f92011-08-23 08:25:03 -07001219 status_t ret = initCheck();
1220 if (ret != NO_ERROR) {
1221 return 0;
1222 }
1223
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001224 AutoMutex lock(mHardwareLock);
1225 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001226 struct audio_config config;
1227 memset(&config, 0, sizeof(config));
1228 config.sample_rate = sampleRate;
1229 config.channel_mask = channelMask;
1230 config.format = format;
1231
John Grossmanee578c02012-07-23 17:05:46 -07001232 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001233#ifndef ICS_AUDIO_BLOB
John Grossmanee578c02012-07-23 17:05:46 -07001234 size_t size = dev->get_input_buffer_size(dev, &config);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001235#else
1236 size_t size = dev->get_input_buffer_size(dev, sampleRate, format, popcount(channelMask));
1237#endif
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001238 mHardwareStatus = AUDIO_HW_IDLE;
1239 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001240}
1241
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001242unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001243{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 Mutex::Autolock _l(mLock);
1245
1246 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1247 if (recordThread != NULL) {
1248 return recordThread->getInputFramesLost();
1249 }
1250 return 0;
1251}
1252
1253status_t AudioFlinger::setVoiceVolume(float value)
1254{
Eric Laurenta1884f92011-08-23 08:25:03 -07001255 status_t ret = initCheck();
1256 if (ret != NO_ERROR) {
1257 return ret;
1258 }
1259
Mathias Agopian65ab4712010-07-14 17:59:35 -07001260 // check calling permissions
1261 if (!settingsAllowed()) {
1262 return PERMISSION_DENIED;
1263 }
1264
1265 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001266 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001267 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001268 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269 mHardwareStatus = AUDIO_HW_IDLE;
1270
1271 return ret;
1272}
1273
Glenn Kasten26c77552012-11-16 12:01:44 -08001274status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001275 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276{
1277 status_t status;
1278
1279 Mutex::Autolock _l(mLock);
1280
1281 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1282 if (playbackThread != NULL) {
1283 return playbackThread->getRenderPosition(halFrames, dspFrames);
1284 }
1285
1286 return BAD_VALUE;
1287}
1288
1289void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1290{
1291
1292 Mutex::Autolock _l(mLock);
1293
Glenn Kastenbb001922012-02-03 11:10:26 -08001294 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001295 if (mNotificationClients.indexOfKey(pid) < 0) {
1296 sp<NotificationClient> notificationClient = new NotificationClient(this,
1297 client,
1298 pid);
Steve Block3856b092011-10-20 11:56:00 +01001299 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300
1301 mNotificationClients.add(pid, notificationClient);
1302
1303 sp<IBinder> binder = client->asBinder();
1304 binder->linkToDeath(notificationClient);
1305
1306 // the config change is always sent from playback or record threads to avoid deadlock
1307 // with AudioSystem::gLock
1308 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001309 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310 }
1311
1312 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001313 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314 }
1315 }
1316}
1317
Ethan Chen8d420ed2013-11-14 00:40:54 -08001318#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301319status_t AudioFlinger::deregisterClient(const sp<IAudioFlingerClient>& client)
1320{
1321 ALOGV("deregisterClient() %p, tid %d, calling tid %d", client.get(), gettid(), IPCThreadState::self()->getCallingPid());
1322 Mutex::Autolock _l(mLock);
1323
1324 pid_t pid = IPCThreadState::self()->getCallingPid();
1325 int index = mNotificationClients.indexOfKey(pid);
1326 if (index >= 0) {
1327 mNotificationClients.removeItemsAt(index);
1328 return true;
1329 }
1330
1331 return false;
1332}
Ethan Chen8d420ed2013-11-14 00:40:54 -08001333#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301334
Mathias Agopian65ab4712010-07-14 17:59:35 -07001335void AudioFlinger::removeNotificationClient(pid_t pid)
1336{
1337 Mutex::Autolock _l(mLock);
1338
Glenn Kastena3b09252012-01-20 09:19:01 -08001339 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001340
Steve Block3856b092011-10-20 11:56:00 +01001341 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001342 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001343 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001344 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001345 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001346 ALOGV(" pid %d @ %d", ref->mPid, i);
1347 if (ref->mPid == pid) {
1348 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001349 mAudioSessionRefs.removeAt(i);
1350 delete ref;
1351 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001352 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001353 } else {
1354 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001355 }
1356 }
1357 if (removed) {
1358 purgeStaleEffects_l();
1359 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360}
1361
1362// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001363void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001364{
Ethan Chen8d420ed2013-11-14 00:40:54 -08001365#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301366 ALOGV("AudioFlinger::audioConfigChanged_l: event %d", event);
1367 if (event == AudioSystem::EFFECT_CONFIG_CHANGED) {
1368 mIsEffectConfigChanged = true;
1369 }
1370 if (!mNotificationClients.isEmpty()){
Ethan Chen8d420ed2013-11-14 00:40:54 -08001371#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301372 size_t size = mNotificationClients.size();
1373 for (size_t i = 0; i < size; i++) {
1374 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1375 param2);
1376 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001377#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301378 }
1379 if ((!mDirectAudioTracks.isEmpty())&& (event == AudioSystem::EFFECT_CONFIG_CHANGED)){
1380 size_t dsize = mDirectAudioTracks.size();
1381 for(size_t i = 0; i < dsize; i++) {
1382 AudioSessionDescriptor *desc = mDirectAudioTracks.valueAt(i);
1383 if(desc && ((DirectAudioTrack*)desc->trackRefPtr)) {
1384 ALOGV("signalling directAudioTrack ");
1385 ((DirectAudioTrack*)desc->trackRefPtr)->signalEffect();
1386 } else{
1387 ALOGV("not found track to signal directAudioTrack ");
1388 }
1389 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001390 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001391#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001392}
1393
1394// removeClient_l() must be called with AudioFlinger::mLock held
1395void AudioFlinger::removeClient_l(pid_t pid)
1396{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001397 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001398 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399 mClients.removeItem(pid);
1400}
1401
Eric Laurent717e1282012-06-29 16:36:52 -07001402// getEffectThread_l() must be called with AudioFlinger::mLock held
1403sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1404{
1405 sp<PlaybackThread> thread;
1406
1407 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1408 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1409 ALOG_ASSERT(thread == 0);
1410 thread = mPlaybackThreads.valueAt(i);
1411 }
1412 }
1413
1414 return thread;
1415}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001416
Mathias Agopian65ab4712010-07-14 17:59:35 -07001417
Mathias Agopian65ab4712010-07-14 17:59:35 -07001418
1419// ----------------------------------------------------------------------------
1420
1421AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1422 : RefBase(),
1423 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001424 // 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 -07001425 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001426 mPid(pid),
1427 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428{
1429 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1430}
1431
1432// Client destructor must be called with AudioFlinger::mLock held
1433AudioFlinger::Client::~Client()
1434{
1435 mAudioFlinger->removeClient_l(mPid);
1436}
1437
Glenn Kasten435dbe62012-01-30 10:15:48 -08001438sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439{
1440 return mMemoryDealer;
1441}
1442
John Grossman4ff14ba2012-02-08 16:37:41 -08001443// Reserve one of the limited slots for a timed audio track associated
1444// with this client
1445bool AudioFlinger::Client::reserveTimedTrack()
1446{
1447 const int kMaxTimedTracksPerClient = 4;
1448
1449 Mutex::Autolock _l(mTimedTrackLock);
1450
1451 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1452 ALOGW("can not create timed track - pid %d has exceeded the limit",
1453 mPid);
1454 return false;
1455 }
1456
1457 mTimedTrackCount++;
1458 return true;
1459}
1460
1461// Release a slot for a timed audio track
1462void AudioFlinger::Client::releaseTimedTrack()
1463{
1464 Mutex::Autolock _l(mTimedTrackLock);
1465 mTimedTrackCount--;
1466}
1467
Mathias Agopian65ab4712010-07-14 17:59:35 -07001468// ----------------------------------------------------------------------------
1469
1470AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1471 const sp<IAudioFlingerClient>& client,
1472 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001473 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474{
1475}
1476
1477AudioFlinger::NotificationClient::~NotificationClient()
1478{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001479}
1480
1481void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1482{
1483 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001484 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485}
1486
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487
1488// ----------------------------------------------------------------------------
1489
Jeff Brown893a5642013-08-16 20:19:26 -07001490static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1491 return audio_is_remote_submix_device(inDevice);
1492}
1493
Mathias Agopian65ab4712010-07-14 17:59:35 -07001494sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001495 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001496 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001497 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001498 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001499 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001500 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001501 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001502 int *sessionId,
1503 status_t *status)
1504{
1505 sp<RecordThread::RecordTrack> recordTrack;
1506 sp<RecordHandle> recordHandle;
1507 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001508 status_t lStatus;
1509 RecordThread *thread;
1510 size_t inFrameCount;
1511 int lSessionId;
Ethan Chen8d420ed2013-11-14 00:40:54 -08001512#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301513 size_t inputBufferSize = 0;
1514 uint32_t channelCount = popcount(channelMask);
Ethan Chen8d420ed2013-11-14 00:40:54 -08001515#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516
1517 // check calling permissions
1518 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001519 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520 lStatus = PERMISSION_DENIED;
1521 goto Exit;
1522 }
1523
Ethan Chen8d420ed2013-11-14 00:40:54 -08001524#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301525 // Check that audio input stream accepts requested audio parameters
1526 inputBufferSize = getInputBufferSize(sampleRate, format, channelCount);
1527 if (inputBufferSize == 0) {
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001528 lStatus = BAD_VALUE;
Alok Kediya601e9b52013-09-27 11:55:10 +05301529 ALOGE("Bad audio input parameters: sampling rate %u, format %d, channels %d", sampleRate, format, channelCount);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001530 goto Exit;
1531 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001532#else
1533 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1534 ALOGE("openRecord() invalid format %d", format);
1535 lStatus = BAD_VALUE;
1536 goto Exit;
1537 }
1538#endif
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001539
Mathias Agopian65ab4712010-07-14 17:59:35 -07001540 // add client to list
1541 { // scope for mLock
1542 Mutex::Autolock _l(mLock);
1543 thread = checkRecordThread_l(input);
1544 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001545 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001546 lStatus = BAD_VALUE;
1547 goto Exit;
1548 }
1549
Jeff Brown893a5642013-08-16 20:19:26 -07001550 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1551 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001552 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001553 lStatus = PERMISSION_DENIED;
1554 goto Exit;
1555 }
1556
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001557 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001558 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001559
1560 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001561 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562 lSessionId = *sessionId;
1563 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001564 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001565 if (sessionId != NULL) {
1566 *sessionId = lSessionId;
1567 }
1568 }
Alok Kediya601e9b52013-09-27 11:55:10 +05301569
Ethan Chen8d420ed2013-11-14 00:40:54 -08001570#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301571 // frameCount must be a multiple of input buffer size
1572 // Change for Codec type
1573 uint8_t channelCount = popcount(channelMask);
1574 if (*flags & IAudioFlinger::TRACK_VOICE_COMMUNICATION) {
1575 inFrameCount = inputBufferSize/channelCount/sizeof(short);
1576 } else {
1577 if ((format == AUDIO_FORMAT_PCM_16_BIT) ||
1578 (format == AUDIO_FORMAT_PCM_8_BIT)) {
1579 inFrameCount = inputBufferSize/channelCount/sizeof(short);
1580 } else if (format == AUDIO_FORMAT_AMR_NB) {
1581 inFrameCount = inputBufferSize/channelCount/AMR_FRAMESIZE;
1582 } else if (format == AUDIO_FORMAT_EVRC) {
1583 inFrameCount = inputBufferSize/channelCount/EVRC_FRAMESIZE;
1584 } else if (format == AUDIO_FORMAT_QCELP) {
1585 inFrameCount = inputBufferSize/channelCount/QCELP_FRAMESIZE;
1586 } else if (format == AUDIO_FORMAT_AAC) {
1587 inFrameCount = inputBufferSize/AAC_FRAMESIZE;
1588 } else if (format == AUDIO_FORMAT_AMR_WB) {
1589 inFrameCount = inputBufferSize/channelCount/AMR_WB_FRAMESIZE;
1590 }
1591 }
1592 frameCount = ((frameCount - 1)/inFrameCount + 1) * inFrameCount;
Ethan Chen8d420ed2013-11-14 00:40:54 -08001593#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301594
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001595 // create new record track.
1596 // The record track uses one track in mHardwareMixerThread by convention.
Marco Nelissen9cae2172013-01-14 14:12:05 -08001597 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001598 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen9cae2172013-01-14 14:12:05 -08001599 frameCount, lSessionId,
1600 IPCThreadState::self()->getCallingUid(),
1601 flags, tid, &lStatus);
Haynes Mathew Georgee010f652013-12-13 15:40:13 -08001602 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001603 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001604 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001605 // remove local strong reference to Client before deleting the RecordTrack so that the
1606 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001607 client.clear();
1608 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609 goto Exit;
1610 }
1611
1612 // return to handle to client
1613 recordHandle = new RecordHandle(recordTrack);
1614 lStatus = NO_ERROR;
1615
1616Exit:
1617 if (status) {
1618 *status = lStatus;
1619 }
1620 return recordHandle;
1621}
1622
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001623
1624
Mathias Agopian65ab4712010-07-14 17:59:35 -07001625// ----------------------------------------------------------------------------
1626
Eric Laurenta4c5a552012-03-29 10:12:40 -07001627audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1628{
1629 if (!settingsAllowed()) {
1630 return 0;
1631 }
1632 Mutex::Autolock _l(mLock);
1633 return loadHwModule_l(name);
1634}
1635
1636// loadHwModule_l() must be called with AudioFlinger::mLock held
1637audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1638{
1639 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1640 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1641 ALOGW("loadHwModule() module %s already loaded", name);
1642 return mAudioHwDevs.keyAt(i);
1643 }
1644 }
1645
Eric Laurenta4c5a552012-03-29 10:12:40 -07001646 audio_hw_device_t *dev;
1647
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001648 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001649 if (rc) {
1650 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1651 return 0;
1652 }
1653
1654 mHardwareStatus = AUDIO_HW_INIT;
1655 rc = dev->init_check(dev);
1656 mHardwareStatus = AUDIO_HW_IDLE;
1657 if (rc) {
1658 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1659 return 0;
1660 }
1661
John Grossmanee578c02012-07-23 17:05:46 -07001662 // Check and cache this HAL's level of support for master mute and master
1663 // volume. If this is the first HAL opened, and it supports the get
1664 // methods, use the initial values provided by the HAL as the current
1665 // master mute and volume settings.
1666
1667 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1668 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001669 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001670
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001671#if !defined(ICS_AUDIO_BLOB) && !defined(MR0_AUDIO_BLOB)
John Grossmanee578c02012-07-23 17:05:46 -07001672 if (0 == mAudioHwDevs.size()) {
1673 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1674 if (NULL != dev->get_master_volume) {
1675 float mv;
1676 if (OK == dev->get_master_volume(dev, &mv)) {
1677 mMasterVolume = mv;
1678 }
1679 }
1680
1681 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1682 if (NULL != dev->get_master_mute) {
1683 bool mm;
1684 if (OK == dev->get_master_mute(dev, &mm)) {
1685 mMasterMute = mm;
1686 }
1687 }
1688 }
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001689#endif
John Grossmanee578c02012-07-23 17:05:46 -07001690
Eric Laurenta4c5a552012-03-29 10:12:40 -07001691 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001692 if ((NULL != dev->set_master_volume) &&
1693 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1694 flags = static_cast<AudioHwDevice::Flags>(flags |
1695 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1696 }
1697
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001698#if !defined(ICS_AUDIO_BLOB) && !defined(MR0_AUDIO_BLOB)
John Grossmanee578c02012-07-23 17:05:46 -07001699 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1700 if ((NULL != dev->set_master_mute) &&
1701 (OK == dev->set_master_mute(dev, mMasterMute))) {
1702 flags = static_cast<AudioHwDevice::Flags>(flags |
1703 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1704 }
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001705#endif
John Grossmanee578c02012-07-23 17:05:46 -07001706
Eric Laurenta4c5a552012-03-29 10:12:40 -07001707 mHardwareStatus = AUDIO_HW_IDLE;
1708 }
1709
1710 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001711 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001712
1713 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001714 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001715
1716 return handle;
1717
1718}
1719
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001720// ----------------------------------------------------------------------------
1721
Glenn Kasten3b16c762012-11-14 08:44:39 -08001722uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001723{
1724 Mutex::Autolock _l(mLock);
1725 PlaybackThread *thread = primaryPlaybackThread_l();
1726 return thread != NULL ? thread->sampleRate() : 0;
1727}
1728
Glenn Kastene33054e2012-11-14 12:54:39 -08001729size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001730{
1731 Mutex::Autolock _l(mLock);
1732 PlaybackThread *thread = primaryPlaybackThread_l();
1733 return thread != NULL ? thread->frameCountHAL() : 0;
1734}
1735
1736// ----------------------------------------------------------------------------
1737
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001738status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1739{
1740 uid_t uid = IPCThreadState::self()->getCallingUid();
1741 if (uid != AID_SYSTEM) {
1742 return PERMISSION_DENIED;
1743 }
1744 Mutex::Autolock _l(mLock);
1745 if (mIsDeviceTypeKnown) {
1746 return INVALID_OPERATION;
1747 }
1748 mIsLowRamDevice = isLowRamDevice;
1749 mIsDeviceTypeKnown = true;
1750 return NO_ERROR;
1751}
1752
1753// ----------------------------------------------------------------------------
1754
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001755#ifdef HAVE_PRE_KITKAT_AUDIO_BLOB
1756audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1757 audio_devices_t *pDevices,
1758 uint32_t *pSamplingRate,
1759 audio_format_t *pFormat,
1760 audio_channel_mask_t *pChannelMask,
1761 uint32_t *pLatencyMs,
1762 audio_output_flags_t flags)
1763{
1764 return openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
1765 pLatencyMs, flags, NULL);
1766}
1767#endif
1768
Eric Laurenta4c5a552012-03-29 10:12:40 -07001769audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1770 audio_devices_t *pDevices,
1771 uint32_t *pSamplingRate,
1772 audio_format_t *pFormat,
1773 audio_channel_mask_t *pChannelMask,
1774 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001775 audio_output_flags_t flags,
1776 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001777{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001778 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001779 struct audio_config config;
1780 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1781 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1782 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001783#ifndef HAVE_PRE_KITKAT_AUDIO_BLOB
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001784 if (offloadInfo) {
1785 config.offload_info = *offloadInfo;
1786 }
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001787#endif
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001788
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001789 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001790 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001791
Eric Laurentbfb1b832013-01-07 09:53:42 -08001792 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001793 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001794 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001795 config.sample_rate,
1796 config.format,
1797 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001798 flags);
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001799#ifndef HAVE_PRE_KITKAT_AUDIO_BLOB
Eric Laurentbfb1b832013-01-07 09:53:42 -08001800 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1801 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version );
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001802#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001803
1804 if (pDevices == NULL || *pDevices == 0) {
1805 return 0;
1806 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001807
Mathias Agopian65ab4712010-07-14 17:59:35 -07001808 Mutex::Autolock _l(mLock);
1809
Eric Laurenta4c5a552012-03-29 10:12:40 -07001810 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001811 if (outHwDev == NULL)
1812 return 0;
1813
John Grossmanee578c02012-07-23 17:05:46 -07001814 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001815 audio_io_handle_t id = nextUniqueId();
1816
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001817 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001818
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001819 status_t status;
1820#ifndef ICS_AUDIO_BLOB
1821 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001822 id,
1823 *pDevices,
1824 (audio_output_flags_t)flags,
1825 &config,
1826 &outStream);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001827#else
1828 status = hwDevHal->open_output_stream(hwDevHal,
1829 *pDevices,
1830 (int *)&config.format,
1831 &config.channel_mask,
1832 &config.sample_rate,
1833 &outStream);
1834 uint32_t newflags = flags | AUDIO_OUTPUT_FLAG_PRIMARY;
1835 flags = (audio_output_flags_t)newflags;
1836#endif
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001837
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001838 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001839 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001840 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001841 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001842 config.sample_rate,
1843 config.format,
1844 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001845 status);
1846
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001847 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001848 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001849 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1850 thread = new OffloadThread(this, output, id, *pDevices);
1851 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
Ethan Chen8d420ed2013-11-14 00:40:54 -08001852#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301853 }
1854 if (flags & AUDIO_OUTPUT_FLAG_LPA || flags & AUDIO_OUTPUT_FLAG_TUNNEL ) {
1855 AudioSessionDescriptor *desc = new AudioSessionDescriptor(hwDevHal, outStream, flags);
1856 desc->mActive = true;
1857 //TODO: no stream type
1858 //desc->mStreamType = streamType;
1859 desc->mVolumeLeft = 1.0;
1860 desc->mVolumeRight = 1.0;
1861 desc->device = *pDevices;
1862 desc->trackRefPtr = NULL;
1863 mDirectAudioTracks.add(id, desc);
1864 mDirectDevice = desc->device;
Ethan Chen8d420ed2013-11-14 00:40:54 -08001865#endif
Eric Laurentbfb1b832013-01-07 09:53:42 -08001866 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001867 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1868 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001869 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001870 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001871 } else {
1872 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001873 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001874 }
Alok Kediya601e9b52013-09-27 11:55:10 +05301875 if (thread != NULL) {
1876 mPlaybackThreads.add(id, thread);
1877 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878
Glenn Kasten7c027242012-12-26 14:43:16 -08001879 if (pSamplingRate != NULL) {
1880 *pSamplingRate = config.sample_rate;
1881 }
1882 if (pFormat != NULL) {
1883 *pFormat = config.format;
1884 }
1885 if (pChannelMask != NULL) {
1886 *pChannelMask = config.channel_mask;
1887 }
Alok Kediya601e9b52013-09-27 11:55:10 +05301888 if (thread != NULL) {
1889 if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
1890 // notify client processes of the new output creation
1891 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1892 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001893#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301894 else {
1895 *pLatencyMs = 0;
1896 if ((flags & AUDIO_OUTPUT_FLAG_LPA) || (flags & AUDIO_OUTPUT_FLAG_TUNNEL)) {
1897 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(id);
1898 *pLatencyMs = desc->stream->get_latency(desc->stream);
1899 }
Glenn Kasten7c027242012-12-26 14:43:16 -08001900 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001901#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001902
Eric Laurenta4c5a552012-03-29 10:12:40 -07001903
1904 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001905 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001906 ALOGI("Using module %d has the primary audio interface", module);
1907 mPrimaryHardwareDev = outHwDev;
Alok Kediya601e9b52013-09-27 11:55:10 +05301908#ifdef SRS_PROCESSING
1909 SRS_Processing::RawDataSet(NULL, "qdsp hook", &mPrimaryHardwareDev,
1910 sizeof(&mPrimaryHardwareDev));
1911#endif
Eric Laurenta4c5a552012-03-29 10:12:40 -07001912 AutoMutex lock(mHardwareLock);
1913 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001914 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001915 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001916 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001917 return id;
1918 }
1919
1920 return 0;
1921}
1922
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001923audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1924 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001925{
1926 Mutex::Autolock _l(mLock);
1927 MixerThread *thread1 = checkMixerThread_l(output1);
1928 MixerThread *thread2 = checkMixerThread_l(output2);
1929
1930 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001931 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1932 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933 return 0;
1934 }
1935
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001936 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1938 thread->addOutputTrack(thread2);
1939 mPlaybackThreads.add(id, thread);
1940 // notify client processes of the new output creation
1941 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1942 return id;
1943}
1944
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001945status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001946{
Glenn Kastend96c5722012-04-25 13:44:49 -07001947 return closeOutput_nonvirtual(output);
1948}
1949
1950status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1951{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001952 // keep strong reference on the playback thread so that
1953 // it is not destroyed while exit() is executed
Ethan Chen8d420ed2013-11-14 00:40:54 -08001954#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301955 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
1956 if (desc) {
1957 ALOGV("Closing DirectTrack output %d", output);
1958 desc->mActive = false;
1959 desc->stream->common.standby(&desc->stream->common);
1960 desc->hwDev->close_output_stream(desc->hwDev, desc->stream);
1961 desc->trackRefPtr = NULL;
1962 desc->stream = NULL;
1963 mDirectAudioTracks.removeItem(output);
1964 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
1965 delete desc;
1966 return NO_ERROR;
1967 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001968#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301969
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001970 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001971 {
1972 Mutex::Autolock _l(mLock);
1973 thread = checkPlaybackThread_l(output);
1974 if (thread == NULL) {
1975 return BAD_VALUE;
1976 }
1977
Steve Block3856b092011-10-20 11:56:00 +01001978 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001980 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001981 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001982 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001983 DuplicatingThread *dupThread =
1984 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001986
1987 }
1988 }
1989 }
1990
1991
1992 mPlaybackThreads.removeItem(output);
1993 // save all effects to the default thread
1994 if (mPlaybackThreads.size()) {
1995 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1996 if (dstThread != NULL) {
1997 // audioflinger lock is held here so the acquisition order of thread locks does not
1998 // matter
1999 Mutex::Autolock _dl(dstThread->mLock);
2000 Mutex::Autolock _sl(thread->mLock);
2001 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
2002 for (size_t i = 0; i < effectChains.size(); i ++) {
2003 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002004 }
2005 }
2006 }
Glenn Kastena1117922012-01-26 10:53:32 -08002007 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002008 }
2009 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08002010 // The thread entity (active unit of execution) is no longer running here,
2011 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002012
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002013 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002014 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002015 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002016 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002017 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002018 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002019 }
2020 return NO_ERROR;
2021}
2022
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002023status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024{
2025 Mutex::Autolock _l(mLock);
2026 PlaybackThread *thread = checkPlaybackThread_l(output);
2027
2028 if (thread == NULL) {
2029 return BAD_VALUE;
2030 }
2031
Steve Block3856b092011-10-20 11:56:00 +01002032 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002033 thread->suspend();
2034
2035 return NO_ERROR;
2036}
2037
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002038status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002039{
2040 Mutex::Autolock _l(mLock);
2041 PlaybackThread *thread = checkPlaybackThread_l(output);
2042
2043 if (thread == NULL) {
2044 return BAD_VALUE;
2045 }
2046
Steve Block3856b092011-10-20 11:56:00 +01002047 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002048
2049 thread->restore();
2050
2051 return NO_ERROR;
2052}
2053
Eric Laurenta4c5a552012-03-29 10:12:40 -07002054audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
2055 audio_devices_t *pDevices,
2056 uint32_t *pSamplingRate,
2057 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07002058 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002059{
2060 status_t status;
2061 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002062 struct audio_config config;
2063 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
2064 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
2065 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
2066
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002067 uint32_t reqSamplingRate = config.sample_rate;
2068 audio_format_t reqFormat = config.format;
2069 audio_channel_mask_t reqChannels = config.channel_mask;
2070 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07002071 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002072
2073 if (pDevices == NULL || *pDevices == 0) {
2074 return 0;
2075 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002076
Mathias Agopian65ab4712010-07-14 17:59:35 -07002077 Mutex::Autolock _l(mLock);
2078
Eric Laurenta4c5a552012-03-29 10:12:40 -07002079 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07002080 if (inHwDev == NULL)
2081 return 0;
2082
John Grossmanee578c02012-07-23 17:05:46 -07002083 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002084 audio_io_handle_t id = nextUniqueId();
2085
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00002086#ifndef ICS_AUDIO_BLOB
John Grossmanee578c02012-07-23 17:05:46 -07002087 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07002088 &inStream);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00002089#else
2090 status = inHwHal->open_input_stream(inHwHal, *pDevices,
2091 (int *)&config.format,
2092 &config.channel_mask,
2093 &config.sample_rate, (audio_in_acoustics_t)0,
2094 &inStream);
2095#endif
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002096 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
2097 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07002098 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002099 config.sample_rate,
2100 config.format,
2101 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002102 status);
2103
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002104 // If the input could not be opened with the requested parameters and we can handle the
2105 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
2106 // 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 -07002107 if (status == BAD_VALUE &&
2108 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
2109 (config.sample_rate <= 2 * reqSamplingRate) &&
Alok Kediya601e9b52013-09-27 11:55:10 +05302110 (getInputChannelCount(config.channel_mask) <= FCC_2) && (getInputChannelCount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07002111 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002112 inStream = NULL;
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00002113#ifndef ICS_AUDIO_BLOB
John Grossmanee578c02012-07-23 17:05:46 -07002114 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00002115#else
2116 status = inHwHal->open_input_stream(inHwHal, *pDevices,
2117 (int *)&config.format,
2118 &config.channel_mask,
2119 &config.sample_rate, (audio_in_acoustics_t)0,
2120 &inStream);
2121#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07002122 }
2123
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002124 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002125
Glenn Kasten46909e72013-02-26 09:20:22 -08002126#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002127 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2128 // or (re-)create if current Pipe is idle and does not match the new format
2129 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002130 enum {
2131 TEE_SINK_NO, // don't copy input
2132 TEE_SINK_NEW, // copy input using a new pipe
2133 TEE_SINK_OLD, // copy input using an existing pipe
2134 } kind;
2135 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
2136 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002137 if (!mTeeSinkInputEnabled) {
2138 kind = TEE_SINK_NO;
2139 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002140 kind = TEE_SINK_NO;
2141 } else if (mRecordTeeSink == 0) {
2142 kind = TEE_SINK_NEW;
2143 } else if (mRecordTeeSink->getStrongCount() != 1) {
2144 kind = TEE_SINK_NO;
2145 } else if (format == mRecordTeeSink->format()) {
2146 kind = TEE_SINK_OLD;
2147 } else {
2148 kind = TEE_SINK_NEW;
2149 }
2150 switch (kind) {
2151 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002152 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002153 size_t numCounterOffers = 0;
2154 const NBAIO_Format offers[1] = {format};
2155 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2156 ALOG_ASSERT(index == 0);
2157 PipeReader *pipeReader = new PipeReader(*pipe);
2158 numCounterOffers = 0;
2159 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2160 ALOG_ASSERT(index == 0);
2161 mRecordTeeSink = pipe;
2162 mRecordTeeSource = pipeReader;
2163 teeSink = pipe;
2164 }
2165 break;
2166 case TEE_SINK_OLD:
2167 teeSink = mRecordTeeSink;
2168 break;
2169 case TEE_SINK_NO:
2170 default:
2171 break;
2172 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002173#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002174
Dima Zavin799a70e2011-04-18 16:57:27 -07002175 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
2176
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002177 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002178 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002179 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002180 thread = new RecordThread(this,
2181 input,
2182 reqSamplingRate,
2183 reqChannels,
2184 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08002185 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08002186 *pDevices
2187#ifdef TEE_SINK
2188 , teeSink
2189#endif
2190 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07002191 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01002192 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08002193 if (pSamplingRate != NULL) {
2194 *pSamplingRate = reqSamplingRate;
2195 }
2196 if (pFormat != NULL) {
2197 *pFormat = config.format;
2198 }
2199 if (pChannelMask != NULL) {
2200 *pChannelMask = reqChannels;
2201 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002202
Mathias Agopian65ab4712010-07-14 17:59:35 -07002203 // notify client processes of the new input creation
2204 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
2205 return id;
2206 }
2207
2208 return 0;
2209}
2210
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002211status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002212{
Glenn Kastend96c5722012-04-25 13:44:49 -07002213 return closeInput_nonvirtual(input);
2214}
2215
2216status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2217{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218 // keep strong reference on the record thread so that
2219 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002220 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002221 {
2222 Mutex::Autolock _l(mLock);
2223 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002224 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002225 return BAD_VALUE;
2226 }
2227
Steve Block3856b092011-10-20 11:56:00 +01002228 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08002229 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002230 mRecordThreads.removeItem(input);
2231 }
2232 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08002233 // The thread entity (active unit of execution) is no longer running here,
2234 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002235
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002236 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002237 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002238 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002239 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002240 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002241
2242 return NO_ERROR;
2243}
2244
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002245status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002246{
2247 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01002248 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002249
2250 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2251 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002252 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002253 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254
2255 return NO_ERROR;
2256}
2257
2258
2259int AudioFlinger::newAudioSessionId()
2260{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002261 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002262}
2263
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002264void AudioFlinger::acquireAudioSessionId(int audioSession)
2265{
2266 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002267 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01002268 ALOGV("acquiring %d from %d", audioSession, caller);
Eric Laurentd1b28d42013-09-18 18:47:13 -07002269
2270 // Ignore requests received from processes not known as notification client. The request
2271 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2272 // called from a different pid leaving a stale session reference. Also we don't know how
2273 // to clear this reference if the client process dies.
2274 if (mNotificationClients.indexOfKey(caller) < 0) {
2275 ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2276 return;
2277 }
2278
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002279 size_t num = mAudioSessionRefs.size();
2280 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002281 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002282 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2283 ref->mCnt++;
2284 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002285 return;
2286 }
2287 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002288 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2289 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002290}
2291
2292void AudioFlinger::releaseAudioSessionId(int audioSession)
2293{
2294 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002295 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01002296 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002297 size_t num = mAudioSessionRefs.size();
2298 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002299 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002300 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2301 ref->mCnt--;
2302 ALOGV(" decremented refcount to %d", ref->mCnt);
2303 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002304 mAudioSessionRefs.removeAt(i);
2305 delete ref;
2306 purgeStaleEffects_l();
2307 }
2308 return;
2309 }
2310 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002311 // If the caller is mediaserver it is likely that the session being released was acquired
2312 // on behalf of a process not in notification clients and we ignore the warning.
2313 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002314}
2315
2316void AudioFlinger::purgeStaleEffects_l() {
2317
Steve Block3856b092011-10-20 11:56:00 +01002318 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002319
2320 Vector< sp<EffectChain> > chains;
2321
2322 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2323 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2324 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2325 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002326 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2327 chains.push(ec);
2328 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002329 }
2330 }
2331 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2332 sp<RecordThread> t = mRecordThreads.valueAt(i);
2333 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2334 sp<EffectChain> ec = t->mEffectChains[j];
2335 chains.push(ec);
2336 }
2337 }
2338
2339 for (size_t i = 0; i < chains.size(); i++) {
2340 sp<EffectChain> ec = chains[i];
2341 int sessionid = ec->sessionId();
2342 sp<ThreadBase> t = ec->mThread.promote();
2343 if (t == 0) {
2344 continue;
2345 }
2346 size_t numsessionrefs = mAudioSessionRefs.size();
2347 bool found = false;
2348 for (size_t k = 0; k < numsessionrefs; k++) {
2349 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002350 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002351 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002352 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002353 found = true;
2354 break;
2355 }
2356 }
2357 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002358 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002359 // remove all effects from the chain
2360 while (ec->mEffects.size()) {
2361 sp<EffectModule> effect = ec->mEffects[0];
2362 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002363 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002364 if (effect->purgeHandles()) {
2365 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002366 }
2367 AudioSystem::unregisterEffect(effect->id());
2368 }
2369 }
2370 }
2371 return;
2372}
2373
Mathias Agopian65ab4712010-07-14 17:59:35 -07002374// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002375AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002376{
Glenn Kastena1117922012-01-26 10:53:32 -08002377 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002378}
2379
2380// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002381AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002382{
2383 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002384 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002385}
2386
2387// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002388AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002389{
Glenn Kastena1117922012-01-26 10:53:32 -08002390 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002391}
2392
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002393uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002394{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002395 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002396}
2397
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002398AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002399{
2400 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2401 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002402 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002403 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002404 return thread;
2405 }
2406 }
2407 return NULL;
2408}
2409
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002410audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002411{
2412 PlaybackThread *thread = primaryPlaybackThread_l();
2413
2414 if (thread == NULL) {
2415 return 0;
2416 }
2417
Eric Laurentf1c04f92012-08-28 14:26:53 -07002418 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002419}
2420
Eric Laurenta011e352012-03-29 15:51:43 -07002421sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2422 int triggerSession,
2423 int listenerSession,
2424 sync_event_callback_t callBack,
2425 void *cookie)
2426{
2427 Mutex::Autolock _l(mLock);
2428
2429 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2430 status_t playStatus = NAME_NOT_FOUND;
2431 status_t recStatus = NAME_NOT_FOUND;
2432 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2433 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2434 if (playStatus == NO_ERROR) {
2435 return event;
2436 }
2437 }
2438 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2439 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2440 if (recStatus == NO_ERROR) {
2441 return event;
2442 }
2443 }
2444 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2445 mPendingSyncEvents.add(event);
2446 } else {
2447 ALOGV("createSyncEvent() invalid event %d", event->type());
2448 event.clear();
2449 }
2450 return event;
2451}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002452
Mathias Agopian65ab4712010-07-14 17:59:35 -07002453// ----------------------------------------------------------------------------
2454// Effect management
2455// ----------------------------------------------------------------------------
2456
2457
Glenn Kastenf587ba52012-01-26 16:25:10 -08002458status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002459{
2460 Mutex::Autolock _l(mLock);
2461 return EffectQueryNumberEffects(numEffects);
2462}
2463
Glenn Kastenf587ba52012-01-26 16:25:10 -08002464status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002465{
2466 Mutex::Autolock _l(mLock);
2467 return EffectQueryEffect(index, descriptor);
2468}
2469
Glenn Kasten5e92a782012-01-30 07:40:52 -08002470status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002471 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002472{
2473 Mutex::Autolock _l(mLock);
2474 return EffectGetDescriptor(pUuid, descriptor);
2475}
2476
2477
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002478sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002479 effect_descriptor_t *pDesc,
2480 const sp<IEffectClient>& effectClient,
2481 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002482 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002483 int sessionId,
2484 status_t *status,
2485 int *id,
2486 int *enabled)
2487{
2488 status_t lStatus = NO_ERROR;
2489 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002490 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002491
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002492 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002493 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002494 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002495
2496 if (pDesc == NULL) {
2497 lStatus = BAD_VALUE;
2498 goto Exit;
2499 }
2500
Eric Laurent84e9a102010-09-23 16:10:16 -07002501 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002502 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002503 lStatus = PERMISSION_DENIED;
2504 goto Exit;
2505 }
2506
Dima Zavinfce7a472011-04-19 22:30:36 -07002507 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002508 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002509 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002510 lStatus = PERMISSION_DENIED;
2511 goto Exit;
2512 }
2513
Mathias Agopian65ab4712010-07-14 17:59:35 -07002514 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002515 if (!EffectIsNullUuid(&pDesc->uuid)) {
2516 // if uuid is specified, request effect descriptor
2517 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2518 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002519 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002520 goto Exit;
2521 }
2522 } else {
2523 // if uuid is not specified, look for an available implementation
2524 // of the required type in effect factory
2525 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002526 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002527 lStatus = BAD_VALUE;
2528 goto Exit;
2529 }
2530 uint32_t numEffects = 0;
2531 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002532 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002533 bool found = false;
2534
2535 lStatus = EffectQueryNumberEffects(&numEffects);
2536 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002537 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002538 goto Exit;
2539 }
2540 for (uint32_t i = 0; i < numEffects; i++) {
2541 lStatus = EffectQueryEffect(i, &desc);
2542 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002543 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002544 continue;
2545 }
2546 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2547 // If matching type found save effect descriptor. If the session is
2548 // 0 and the effect is not auxiliary, continue enumeration in case
2549 // an auxiliary version of this effect type is available
2550 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002551 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002552 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002553 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2554 break;
2555 }
2556 }
2557 }
2558 if (!found) {
2559 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002560 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002561 goto Exit;
2562 }
2563 // For same effect type, chose auxiliary version over insert version if
2564 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002565 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002566 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002567 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002568 }
2569 }
2570
2571 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002572 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002573 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2574 lStatus = INVALID_OPERATION;
2575 goto Exit;
2576 }
2577
Eric Laurent59255e42011-07-27 19:49:51 -07002578 // check recording permission for visualizer
2579 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2580 !recordingAllowed()) {
2581 lStatus = PERMISSION_DENIED;
2582 goto Exit;
2583 }
2584
Mathias Agopian65ab4712010-07-14 17:59:35 -07002585 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002586 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002587 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2588 // if the output returned by getOutputForEffect() is removed before we lock the
2589 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2590 // and we will exit safely
2591 io = AudioSystem::getOutputForEffect(&desc);
2592 ALOGV("createEffect got output %d", io);
2593 }
2594
2595 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002596
2597 // If output is not specified try to find a matching audio session ID in one of the
2598 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002599 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2600 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002601 // Note: io is never 0 when creating an effect on an input
2602 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002603 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2604 // output must be specified by AudioPolicyManager when using session
2605 // AUDIO_SESSION_OUTPUT_STAGE
2606 lStatus = BAD_VALUE;
2607 goto Exit;
2608 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002609 // look for the thread where the specified audio session is present
2610 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2611 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2612 io = mPlaybackThreads.keyAt(i);
2613 break;
2614 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002615 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002616 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002617 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2618 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2619 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002620 break;
2621 }
2622 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002623 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002624 // If no output thread contains the requested session ID, default to
2625 // first output. The effect chain will be moved to the correct output
2626 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002627 if (io == 0 && mPlaybackThreads.size()) {
2628 io = mPlaybackThreads.keyAt(0);
2629 }
Steve Block3856b092011-10-20 11:56:00 +01002630 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002631 }
2632 ThreadBase *thread = checkRecordThread_l(io);
2633 if (thread == NULL) {
2634 thread = checkPlaybackThread_l(io);
2635 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002636 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002637 lStatus = BAD_VALUE;
2638 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002639 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002640 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002641
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002642 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002643
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002644 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002645 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2646 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002647 if (handle != 0 && id != NULL) {
2648 *id = handle->id();
2649 }
2650 }
2651
2652Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002653 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002654 *status = lStatus;
2655 }
2656 return handle;
2657}
2658
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002659status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2660 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002661{
Steve Block3856b092011-10-20 11:56:00 +01002662 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002663 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002664 Mutex::Autolock _l(mLock);
2665 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002666 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002667 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002668 }
Eric Laurentde070132010-07-13 04:45:46 -07002669 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2670 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002671 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002672 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002673 }
Eric Laurentde070132010-07-13 04:45:46 -07002674 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2675 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002676 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002677 return BAD_VALUE;
2678 }
2679
2680 Mutex::Autolock _dl(dstThread->mLock);
2681 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002682 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002683}
2684
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002685// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002686status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002687 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002688 AudioFlinger::PlaybackThread *dstThread,
2689 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002690{
Steve Block3856b092011-10-20 11:56:00 +01002691 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002692 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002693
Eric Laurent59255e42011-07-27 19:49:51 -07002694 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002695 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002696 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002697 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002698 return INVALID_OPERATION;
2699 }
2700
Eric Laurent39e94f82010-07-28 01:32:47 -07002701 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002702 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002703 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002704 // removed.
2705 srcThread->removeEffectChain_l(chain);
2706
2707 // transfer all effects one by one so that new effect chain is created on new thread with
2708 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002709 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002710 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002711 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002712 Vector< sp<EffectModule> > removed;
2713 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002714 while (effect != 0) {
2715 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002716 removed.add(effect);
2717 status = dstThread->addEffect_l(effect);
2718 if (status != NO_ERROR) {
2719 break;
2720 }
Eric Laurentec35a142011-10-05 17:42:25 -07002721 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2722 if (effect->state() == EffectModule::ACTIVE ||
2723 effect->state() == EffectModule::STOPPING) {
2724 effect->start();
2725 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002726 // if the move request is not received from audio policy manager, the effect must be
2727 // re-registered with the new strategy and output
2728 if (dstChain == 0) {
2729 dstChain = effect->chain().promote();
2730 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002731 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002732 status = NO_INIT;
2733 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002734 }
2735 strategy = dstChain->strategy();
2736 }
2737 if (reRegister) {
2738 AudioSystem::unregisterEffect(effect->id());
2739 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002740 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002741 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002742 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002743 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002744 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002745 }
Eric Laurentde070132010-07-13 04:45:46 -07002746 effect = chain->getEffectFromId_l(0);
2747 }
2748
Eric Laurent5baf2af2013-09-12 17:37:00 -07002749 if (status != NO_ERROR) {
2750 for (size_t i = 0; i < removed.size(); i++) {
2751 srcThread->addEffect_l(removed[i]);
2752 if (dstChain != 0 && reRegister) {
2753 AudioSystem::unregisterEffect(removed[i]->id());
2754 AudioSystem::registerEffect(&removed[i]->desc(),
2755 srcThread->id(),
2756 strategy,
2757 sessionId,
2758 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002759 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002760 }
2761 }
2762 }
2763
2764 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002765}
2766
Eric Laurent5baf2af2013-09-12 17:37:00 -07002767bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002768{
2769 if (mGlobalEffectEnableTime != 0 &&
2770 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2771 return true;
2772 }
2773
2774 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2775 sp<EffectChain> ec =
2776 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002777 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002778 return true;
2779 }
2780 }
2781 return false;
2782}
2783
Eric Laurent5baf2af2013-09-12 17:37:00 -07002784void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002785{
2786 Mutex::Autolock _l(mLock);
2787
2788 mGlobalEffectEnableTime = systemTime();
2789
2790 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2791 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2792 if (t->mType == ThreadBase::OFFLOAD) {
2793 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2794 }
2795 }
2796
2797}
2798
Glenn Kastenda6ef132013-01-10 12:31:01 -08002799struct Entry {
2800#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2801 char mName[MAX_NAME];
2802};
2803
2804int comparEntry(const void *p1, const void *p2)
2805{
2806 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2807}
2808
Glenn Kasten46909e72013-02-26 09:20:22 -08002809#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002810void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002811{
Eric Laurent81784c32012-11-19 14:55:58 -08002812 NBAIO_Source *teeSource = source.get();
2813 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002814 // .wav rotation
2815 // There is a benign race condition if 2 threads call this simultaneously.
2816 // They would both traverse the directory, but the result would simply be
2817 // failures at unlink() which are ignored. It's also unlikely since
2818 // normally dumpsys is only done by bugreport or from the command line.
2819 char teePath[32+256];
2820 strcpy(teePath, "/data/misc/media");
2821 size_t teePathLen = strlen(teePath);
2822 DIR *dir = opendir(teePath);
2823 teePath[teePathLen++] = '/';
2824 if (dir != NULL) {
2825#define MAX_SORT 20 // number of entries to sort
2826#define MAX_KEEP 10 // number of entries to keep
2827 struct Entry entries[MAX_SORT];
2828 size_t entryCount = 0;
2829 while (entryCount < MAX_SORT) {
2830 struct dirent de;
2831 struct dirent *result = NULL;
2832 int rc = readdir_r(dir, &de, &result);
2833 if (rc != 0) {
2834 ALOGW("readdir_r failed %d", rc);
2835 break;
2836 }
2837 if (result == NULL) {
2838 break;
2839 }
2840 if (result != &de) {
2841 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2842 break;
2843 }
2844 // ignore non .wav file entries
2845 size_t nameLen = strlen(de.d_name);
2846 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2847 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2848 continue;
2849 }
2850 strcpy(entries[entryCount++].mName, de.d_name);
2851 }
2852 (void) closedir(dir);
2853 if (entryCount > MAX_KEEP) {
2854 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2855 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2856 strcpy(&teePath[teePathLen], entries[i].mName);
2857 (void) unlink(teePath);
2858 }
2859 }
2860 } else {
2861 if (fd >= 0) {
2862 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2863 }
2864 }
Eric Laurent81784c32012-11-19 14:55:58 -08002865 char teeTime[16];
2866 struct timeval tv;
2867 gettimeofday(&tv, NULL);
2868 struct tm tm;
2869 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002870 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2871 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2872 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2873 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002874 if (teeFd >= 0) {
2875 char wavHeader[44];
2876 memcpy(wavHeader,
2877 "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",
2878 sizeof(wavHeader));
2879 NBAIO_Format format = teeSource->format();
2880 unsigned channelCount = Format_channelCount(format);
2881 ALOG_ASSERT(channelCount <= FCC_2);
2882 uint32_t sampleRate = Format_sampleRate(format);
2883 wavHeader[22] = channelCount; // number of channels
2884 wavHeader[24] = sampleRate; // sample rate
2885 wavHeader[25] = sampleRate >> 8;
2886 wavHeader[32] = channelCount * 2; // block alignment
2887 write(teeFd, wavHeader, sizeof(wavHeader));
2888 size_t total = 0;
2889 bool firstRead = true;
2890 for (;;) {
2891#define TEE_SINK_READ 1024
2892 short buffer[TEE_SINK_READ * FCC_2];
2893 size_t count = TEE_SINK_READ;
2894 ssize_t actual = teeSource->read(buffer, count,
2895 AudioBufferProvider::kInvalidPTS);
2896 bool wasFirstRead = firstRead;
2897 firstRead = false;
2898 if (actual <= 0) {
2899 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2900 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002901 }
Eric Laurent81784c32012-11-19 14:55:58 -08002902 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002903 }
Eric Laurent81784c32012-11-19 14:55:58 -08002904 ALOG_ASSERT(actual <= (ssize_t)count);
2905 write(teeFd, buffer, actual * channelCount * sizeof(short));
2906 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002907 }
Eric Laurent81784c32012-11-19 14:55:58 -08002908 lseek(teeFd, (off_t) 4, SEEK_SET);
2909 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2910 write(teeFd, &temp, sizeof(temp));
2911 lseek(teeFd, (off_t) 40, SEEK_SET);
2912 temp = total * channelCount * sizeof(short);
2913 write(teeFd, &temp, sizeof(temp));
2914 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002915 if (fd >= 0) {
2916 fdprintf(fd, "tee copied to %s\n", teePath);
2917 }
Eric Laurent59255e42011-07-27 19:49:51 -07002918 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002919 if (fd >= 0) {
2920 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2921 }
Eric Laurent59255e42011-07-27 19:49:51 -07002922 }
2923 }
2924}
Glenn Kasten46909e72013-02-26 09:20:22 -08002925#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002926
Mathias Agopian65ab4712010-07-14 17:59:35 -07002927status_t AudioFlinger::onTransact(
2928 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2929{
2930 return BnAudioFlinger::onTransact(code, data, reply, flags);
2931}
2932
Mathias Agopian65ab4712010-07-14 17:59:35 -07002933}; // namespace android