blob: a00e42eee89615fa84409c1608f59ede8a8655fc [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 */
Santhosh Kumar Thimmanna Bhattar03fd1d52013-10-30 11:29:19 +0530194 mLPASessionId = -2; // -2 is invalid session ID
195 mIsEffectConfigChanged = false;
196 mLPAEffectChain = NULL;
John Grossman4ff14ba2012-02-08 16:37:41 -0800197 char val_str[PROPERTY_VALUE_MAX] = { 0 };
198 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
199 uint32_t int_val;
200 if (1 == sscanf(val_str, "%u", &int_val)) {
201 mStandbyTimeInNsecs = milliseconds(int_val);
202 ALOGI("Using %u mSec as standby time.", int_val);
203 } else {
204 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
205 ALOGI("Using default %u mSec as standby time.",
206 (uint32_t)(mStandbyTimeInNsecs / 1000000));
207 }
208 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700209
Eric Laurenta4c5a552012-03-29 10:12:40 -0700210 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211}
212
213AudioFlinger::~AudioFlinger()
214{
215 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700216 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700217 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700218 }
219 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700220 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700221 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700222 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700223
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800224 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
225 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700226 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
227 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228 }
229}
230
Eric Laurenta4c5a552012-03-29 10:12:40 -0700231static const char * const audio_interfaces[] = {
232 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
233 AUDIO_HARDWARE_MODULE_ID_A2DP,
234 AUDIO_HARDWARE_MODULE_ID_USB,
235};
236#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
237
John Grossmanee578c02012-07-23 17:05:46 -0700238AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
239 audio_module_handle_t module,
240 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700241{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700242 // if module is 0, the request comes from an old policy manager and we should load
243 // well known modules
244 if (module == 0) {
245 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
246 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
247 loadHwModule_l(audio_interfaces[i]);
248 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700249 // then try to find a module supporting the requested device.
250 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
251 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
252 audio_hw_device_t *dev = audioHwDevice->hwDevice();
253 if ((dev->get_supported_devices != NULL) &&
254 (dev->get_supported_devices(dev) & devices) == devices)
255 return audioHwDevice;
Ricardo Cerqueira2b3c7972013-01-10 23:53:09 +0000256#ifdef ICS_AUDIO_BLOB
257 else if (dev->get_supported_devices == NULL && i != 0 &&
258 devices == 0x80)
259 // Reasonably safe assumption: A non-primary HAL without
260 // get_supported_devices is a locally-built A2DP binary
261 return audioHwDevice;
262#endif
Eric Laurentf1c04f92012-08-28 14:26:53 -0700263 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700264 } else {
265 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700266 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
267 if (audioHwDevice != NULL) {
268 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700269 }
270 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700271
Dima Zavin799a70e2011-04-18 16:57:27 -0700272 return NULL;
273}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700275void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276{
277 const size_t SIZE = 256;
278 char buffer[SIZE];
279 String8 result;
280
281 result.append("Clients:\n");
282 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800283 sp<Client> client = mClients.valueAt(i).promote();
284 if (client != 0) {
285 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
286 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287 }
288 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700289
Eric Laurentd1b28d42013-09-18 18:47:13 -0700290 result.append("Notification Clients:\n");
291 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
292 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
293 result.append(buffer);
294 }
295
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700296 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800297 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700298 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
299 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800300 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700301 result.append(buffer);
302 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304}
305
306
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700307void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700308{
309 const size_t SIZE = 256;
310 char buffer[SIZE];
311 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800312 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313
John Grossman4ff14ba2012-02-08 16:37:41 -0800314 snprintf(buffer, SIZE, "Hardware status: %d\n"
315 "Standby Time mSec: %u\n",
316 hardwareStatus,
317 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700318 result.append(buffer);
319 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320}
321
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700322void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323{
324 const size_t SIZE = 256;
325 char buffer[SIZE];
326 String8 result;
327 snprintf(buffer, SIZE, "Permission Denial: "
328 "can't dump AudioFlinger from pid=%d, uid=%d\n",
329 IPCThreadState::self()->getCallingPid(),
330 IPCThreadState::self()->getCallingUid());
331 result.append(buffer);
332 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333}
334
Eric Laurent81784c32012-11-19 14:55:58 -0800335bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336{
337 bool locked = false;
338 for (int i = 0; i < kDumpLockRetries; ++i) {
339 if (mutex.tryLock() == NO_ERROR) {
340 locked = true;
341 break;
342 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800343 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700344 }
345 return locked;
346}
347
348status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
349{
Glenn Kasten44deb052012-02-05 18:09:08 -0800350 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 dumpPermissionDenial(fd, args);
352 } else {
353 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800354 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355 if (!hardwareLocked) {
356 String8 result(kHardwareLockedString);
357 write(fd, result.string(), result.size());
358 } else {
359 mHardwareLock.unlock();
360 }
361
Eric Laurent81784c32012-11-19 14:55:58 -0800362 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363
364 // failed to lock - AudioFlinger is probably deadlocked
365 if (!locked) {
366 String8 result(kDeadlockedString);
367 write(fd, result.string(), result.size());
368 }
369
370 dumpClients(fd, args);
371 dumpInternals(fd, args);
372
373 // dump playback threads
374 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
375 mPlaybackThreads.valueAt(i)->dump(fd, args);
376 }
377
378 // dump record threads
379 for (size_t i = 0; i < mRecordThreads.size(); i++) {
380 mRecordThreads.valueAt(i)->dump(fd, args);
381 }
382
Dima Zavin799a70e2011-04-18 16:57:27 -0700383 // dump all hardware devs
384 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700385 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700386 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700388
Glenn Kasten46909e72013-02-26 09:20:22 -0800389#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700390 // dump the serially shared record tee sink
391 if (mRecordTeeSource != 0) {
392 dumpTee(fd, mRecordTeeSource);
393 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800394#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700395
Glenn Kastend65d73c2012-06-22 17:21:07 -0700396 if (locked) {
397 mLock.unlock();
398 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800399
400 // append a copy of media.log here by forwarding fd to it, but don't attempt
401 // to lookup the service if it's not running, as it will block for a second
402 if (mLogMemoryDealer != 0) {
403 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
404 if (binder != 0) {
405 fdprintf(fd, "\nmedia.log:\n");
406 Vector<String16> args;
407 binder->dump(fd, args);
408 }
409 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 }
411 return NO_ERROR;
412}
413
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800414sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
415{
416 // If pid is already in the mClients wp<> map, then use that entry
417 // (for which promote() is always != 0), otherwise create a new entry and Client.
418 sp<Client> client = mClients.valueFor(pid).promote();
419 if (client == 0) {
420 client = new Client(this, pid);
421 mClients.add(pid, client);
422 }
423
424 return client;
425}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426
Glenn Kasten9e58b552013-01-18 15:09:48 -0800427sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
428{
429 if (mLogMemoryDealer == 0) {
430 return new NBLog::Writer();
431 }
432 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
433 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
434 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
435 if (binder != 0) {
436 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
437 }
438 return writer;
439}
440
441void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
442{
Glenn Kasten685ef092013-02-04 08:15:34 -0800443 if (writer == 0) {
444 return;
445 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800446 sp<IMemory> iMemory(writer->getIMemory());
447 if (iMemory == 0) {
448 return;
449 }
450 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
451 if (binder != 0) {
452 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
453 // Now the media.log remote reference to IMemory is gone.
454 // When our last local reference to IMemory also drops to zero,
455 // the IMemory destructor will deallocate the region from mMemoryDealer.
456 }
457}
458
Mathias Agopian65ab4712010-07-14 17:59:35 -0700459// IAudioFlinger interface
460
461
462sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800463 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700464 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800465 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700466 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800467 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800468 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700469 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800470 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800471 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700472 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700473 String8& name,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700474 status_t *status)
475{
476 sp<PlaybackThread::Track> track;
477 sp<TrackHandle> trackHandle;
478 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700479 status_t lStatus;
480 int lSessionId;
481
Glenn Kasten263709e2012-01-06 08:40:01 -0800482 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
483 // but if someone uses binder directly they could bypass that and cause us to crash
484 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000485 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700486 lStatus = BAD_VALUE;
487 goto Exit;
488 }
489
Glenn Kasten60a83922012-06-21 12:56:37 -0700490 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
491 // and we don't yet support 8.24 or 32-bit PCM
492 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
493 ALOGE("createTrack() invalid format %d", format);
494 lStatus = BAD_VALUE;
495 goto Exit;
496 }
497
Mathias Agopian65ab4712010-07-14 17:59:35 -0700498 {
499 Mutex::Autolock _l(mLock);
500 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700501 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700502 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800503 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700504 lStatus = BAD_VALUE;
505 goto Exit;
506 }
507
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800508 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800509 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700510
Steve Block3856b092011-10-20 11:56:00 +0100511 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700512 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700513 // check if an effect chain with the same session ID is present on another
514 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700515 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700516 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
517 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700518 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700519 if (sessions & PlaybackThread::EFFECT_SESSION) {
520 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700521 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700522 }
Eric Laurentde070132010-07-13 04:45:46 -0700523 }
524 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700525 lSessionId = *sessionId;
526 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700527 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700528 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700529 if (sessionId != NULL) {
530 *sessionId = lSessionId;
531 }
532 }
Steve Block3856b092011-10-20 11:56:00 +0100533 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700534
535 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800536 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700537
538 // move effect chain to this output thread if an effect on same session was waiting
539 // for a track to be created
540 if (lStatus == NO_ERROR && effectThread != NULL) {
541 Mutex::Autolock _dl(thread->mLock);
542 Mutex::Autolock _sl(effectThread->mLock);
543 moveEffectChain_l(lSessionId, effectThread, thread, true);
544 }
Eric Laurenta011e352012-03-29 15:51:43 -0700545
546 // Look for sync events awaiting for a session to be used.
547 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
548 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
549 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700550 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700551 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700552 } else {
553 mPendingSyncEvents[i]->cancel();
554 }
Eric Laurenta011e352012-03-29 15:51:43 -0700555 mPendingSyncEvents.removeAt(i);
556 i--;
557 }
558 }
559 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560 }
561 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700562 // s for server's pid, n for normal mixer name, f for fast index
563 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
564 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700565 trackHandle = new TrackHandle(track);
566 } else {
567 // remove local strong reference to Client before deleting the Track so that the Client
568 // destructor is called by the TrackBase destructor with mLock held
569 client.clear();
570 track.clear();
571 }
572
573Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700574 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575 *status = lStatus;
576 }
577 return trackHandle;
578}
579
Ethan Chen8d420ed2013-11-14 00:40:54 -0800580#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530581sp<IDirectTrack> AudioFlinger::createDirectTrack(
582 pid_t pid,
583 uint32_t sampleRate,
584 audio_channel_mask_t channelMask,
585 audio_io_handle_t output,
586 int *sessionId,
587 IDirectTrackClient *client,
588 audio_stream_type_t streamType,
589 status_t *status)
590{
591 *status = NO_ERROR;
592 status_t lStatus = NO_ERROR;
593 sp<IDirectTrack> track = NULL;
594 DirectAudioTrack* directTrack = NULL;
595 Mutex::Autolock _l(mLock);
596
597 ALOGV("createDirectTrack() sessionId: %d sampleRate %d channelMask %d",
598 *sessionId, sampleRate, channelMask);
599 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
600 if(desc == NULL) {
601 ALOGE("Error: Invalid output (%d) to create direct audio track", output);
602 lStatus = BAD_VALUE;
603 goto Exit;
604 }
605 desc->mStreamType = streamType;
606 if (desc->flag & AUDIO_OUTPUT_FLAG_LPA) {
607 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
608 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
609 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
610 // Check if the session ID is already associated with a track
611 uint32_t sessions = t->hasAudioSession(*sessionId);
612
613 // check if an effect with same session ID is waiting for a ssession to be created
614 ALOGV("check if an effect with same session ID is waiting for a ssession to be created");
615 if ((mLPAEffectChain == NULL) && (sessions & PlaybackThread::EFFECT_SESSION)) {
616 // Clear reference to previous effect chain if any
617 t->mLock.lock();
618 ALOGV("getting the LPA effect chain and setting LPA flag to true.");
619 mLPAEffectChain = t->getEffectChain_l(*sessionId);
620 t->mLock.unlock();
621 }
622 }
623 mLPASessionId = *sessionId;
624 if (mLPAEffectChain != NULL) {
625 mLPAEffectChain->setLPAFlag(true);
626 // For LPA, the volume will be applied in DSP. No need for volume
627 // control in the Effect chain, so setting it to unity.
628 uint32_t volume = 0x1000000; // Equals to 1.0 in 8.24 format
629 mLPAEffectChain->setVolume_l(&volume,&volume);
630 } else {
631 ALOGW("There was no effectChain created for the sessionId(%d)", mLPASessionId);
632 }
633 mLPASampleRate = sampleRate;
634 mLPANumChannels = popcount(channelMask);
635 } else {
636 if(sessionId != NULL) {
637 ALOGE("Error: Invalid sessionID (%d) for direct audio track", *sessionId);
638 }
639 }
640 }
641 mLock.unlock();
642 directTrack = new DirectAudioTrack(this, output, desc, client, desc->flag);
643 desc->trackRefPtr = dynamic_cast<void *>(directTrack);
644 mLock.lock();
645 if (directTrack != 0) {
646 track = dynamic_cast<IDirectTrack *>(directTrack);
647 AudioEventObserver* obv = dynamic_cast<AudioEventObserver *>(directTrack);
648 ALOGE("setting observer mOutputDesc track %p, obv %p", track.get(), obv);
649 desc->stream->set_observer(desc->stream, reinterpret_cast<void *>(obv));
650 } else {
651 lStatus = BAD_VALUE;
652 }
653Exit:
654 if(lStatus) {
655 if (track != NULL) {
656 track.clear();
657 }
658 *status = lStatus;
659 }
660 return track;
661}
662
663void AudioFlinger::deleteEffectSession()
664{
665 ALOGV("deleteSession");
666 // -2 is invalid session ID
667 mLPASessionId = -2;
668 if (mLPAEffectChain != NULL) {
669 mLPAEffectChain->lock();
670 mLPAEffectChain->setLPAFlag(false);
671 size_t i, numEffects = mLPAEffectChain->getNumEffects();
672 for(i = 0; i < numEffects; i++) {
673 sp<EffectModule> effect = mLPAEffectChain->getEffectFromIndex_l(i);
674 effect->setInBuffer(mLPAEffectChain->inBuffer());
675 if (i == numEffects-1) {
676 effect->setOutBuffer(mLPAEffectChain->outBuffer());
677 } else {
678 effect->setOutBuffer(mLPAEffectChain->inBuffer());
679 }
680 effect->configure();
681 }
682 mLPAEffectChain->unlock();
683 mLPAEffectChain.clear();
684 mLPAEffectChain = NULL;
685 }
686}
Ethan Chen8d420ed2013-11-14 00:40:54 -0800687#endif
Alok Kediya601e9b52013-09-27 11:55:10 +0530688
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800689uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690{
691 Mutex::Autolock _l(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800692#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530693 if (!mDirectAudioTracks.isEmpty()) {
694 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
695 if(desc != NULL) {
696 return desc->stream->common.get_sample_rate(&desc->stream->common);
697 }
698 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800699#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700 PlaybackThread *thread = checkPlaybackThread_l(output);
701 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000702 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703 return 0;
704 }
705 return thread->sampleRate();
706}
707
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800708int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709{
710 Mutex::Autolock _l(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800711#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530712 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
713 if(desc != NULL) {
714 return desc->stream->common.get_channels(&desc->stream->common);
715 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800716#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717 PlaybackThread *thread = checkPlaybackThread_l(output);
718 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000719 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 return 0;
721 }
722 return thread->channelCount();
723}
724
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800725audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726{
727 Mutex::Autolock _l(mLock);
728 PlaybackThread *thread = checkPlaybackThread_l(output);
729 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000730 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800731 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732 }
733 return thread->format();
734}
735
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800736size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700737{
738 Mutex::Autolock _l(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800739#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530740 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
741 if(desc != NULL) {
742 return desc->stream->common.get_buffer_size(&desc->stream->common);
743 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800744#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700745 PlaybackThread *thread = checkPlaybackThread_l(output);
746 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000747 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748 return 0;
749 }
Glenn Kasten58912562012-04-03 10:45:00 -0700750 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
751 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752 return thread->frameCount();
753}
754
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800755uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700756{
757 Mutex::Autolock _l(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800758#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530759 if (!mDirectAudioTracks.isEmpty()) {
760 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
761 if (desc != NULL) {
762 return desc->stream->get_latency(desc->stream);
763 }
764 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800765#endif
Alok Kediya601e9b52013-09-27 11:55:10 +0530766
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767 PlaybackThread *thread = checkPlaybackThread_l(output);
768 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800769 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700770 return 0;
771 }
772 return thread->latency();
773}
774
775status_t AudioFlinger::setMasterVolume(float value)
776{
Eric Laurenta1884f92011-08-23 08:25:03 -0700777 status_t ret = initCheck();
778 if (ret != NO_ERROR) {
779 return ret;
780 }
781
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782 // check calling permissions
783 if (!settingsAllowed()) {
784 return PERMISSION_DENIED;
785 }
786
Eric Laurenta4c5a552012-03-29 10:12:40 -0700787 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700788 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700789
John Grossmanee578c02012-07-23 17:05:46 -0700790 // Set master volume in the HALs which support it.
791 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
792 AutoMutex lock(mHardwareLock);
793 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800794
John Grossmanee578c02012-07-23 17:05:46 -0700795 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
796 if (dev->canSetMasterVolume()) {
797 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800798 }
John Grossmanee578c02012-07-23 17:05:46 -0700799 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801
John Grossmanee578c02012-07-23 17:05:46 -0700802 // Now set the master volume in each playback thread. Playback threads
803 // assigned to HALs which do not have master volume support will apply
804 // master volume during the mix operation. Threads with HALs which do
805 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800806 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700807 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808
809 return NO_ERROR;
810}
811
Glenn Kastenf78aee72012-01-04 11:00:47 -0800812status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700813{
Eric Laurenta1884f92011-08-23 08:25:03 -0700814 status_t ret = initCheck();
815 if (ret != NO_ERROR) {
816 return ret;
817 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818
819 // check calling permissions
820 if (!settingsAllowed()) {
821 return PERMISSION_DENIED;
822 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800823 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000824 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825 return BAD_VALUE;
826 }
827
828 { // scope for the lock
829 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700830 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700832 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 mHardwareStatus = AUDIO_HW_IDLE;
834 }
835
836 if (NO_ERROR == ret) {
837 Mutex::Autolock _l(mLock);
838 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800839 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700840 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841 }
842
843 return ret;
844}
845
846status_t AudioFlinger::setMicMute(bool state)
847{
Eric Laurenta1884f92011-08-23 08:25:03 -0700848 status_t ret = initCheck();
849 if (ret != NO_ERROR) {
850 return ret;
851 }
852
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853 // check calling permissions
854 if (!settingsAllowed()) {
855 return PERMISSION_DENIED;
856 }
857
858 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700859 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700861 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862 mHardwareStatus = AUDIO_HW_IDLE;
863 return ret;
864}
865
866bool AudioFlinger::getMicMute() const
867{
Eric Laurenta1884f92011-08-23 08:25:03 -0700868 status_t ret = initCheck();
869 if (ret != NO_ERROR) {
870 return false;
871 }
872
Dima Zavinfce7a472011-04-19 22:30:36 -0700873 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800874 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700875 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700877 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878 mHardwareStatus = AUDIO_HW_IDLE;
879 return state;
880}
881
882status_t AudioFlinger::setMasterMute(bool muted)
883{
John Grossmand8f178d2012-07-20 14:51:35 -0700884 status_t ret = initCheck();
885 if (ret != NO_ERROR) {
886 return ret;
887 }
888
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889 // check calling permissions
890 if (!settingsAllowed()) {
891 return PERMISSION_DENIED;
892 }
893
John Grossmanee578c02012-07-23 17:05:46 -0700894 Mutex::Autolock _l(mLock);
895 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700896
Emerson Pinter6d327d52013-07-28 19:34:16 -0300897#ifndef ICS_AUDIO_BLOB
John Grossmanee578c02012-07-23 17:05:46 -0700898 // Set master mute in the HALs which support it.
899 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
900 AutoMutex lock(mHardwareLock);
901 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700902
John Grossmanee578c02012-07-23 17:05:46 -0700903 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
904 if (dev->canSetMasterMute()) {
905 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700906 }
John Grossmanee578c02012-07-23 17:05:46 -0700907 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700908 }
909
John Grossmanee578c02012-07-23 17:05:46 -0700910 // Now set the master mute in each playback thread. Playback threads
911 // assigned to HALs which do not have master mute support will apply master
912 // mute during the mix operation. Threads with HALs which do support master
913 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800914 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700915 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +0000916#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917
918 return NO_ERROR;
919}
920
921float AudioFlinger::masterVolume() const
922{
Glenn Kasten98067102011-12-13 11:47:54 -0800923 Mutex::Autolock _l(mLock);
924 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925}
926
927bool AudioFlinger::masterMute() const
928{
Glenn Kasten98067102011-12-13 11:47:54 -0800929 Mutex::Autolock _l(mLock);
930 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931}
932
John Grossman4ff14ba2012-02-08 16:37:41 -0800933float AudioFlinger::masterVolume_l() const
934{
John Grossman4ff14ba2012-02-08 16:37:41 -0800935 return mMasterVolume;
936}
937
John Grossmand8f178d2012-07-20 14:51:35 -0700938bool AudioFlinger::masterMute_l() const
939{
John Grossmanee578c02012-07-23 17:05:46 -0700940 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700941}
942
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800943status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
944 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945{
946 // check calling permissions
947 if (!settingsAllowed()) {
948 return PERMISSION_DENIED;
949 }
950
Glenn Kasten263709e2012-01-06 08:40:01 -0800951 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000952 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953 return BAD_VALUE;
954 }
955
956 AutoMutex lock(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800957#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530958 ALOGV("setStreamVolume stream %d, output %d, value %f",stream, output, value);
959 AudioSessionDescriptor *desc = NULL;
960 if (!mDirectAudioTracks.isEmpty()) {
961 desc = mDirectAudioTracks.valueFor(output);
962 if (desc != NULL) {
963 ALOGV("setStreamVolume for mAudioTracks size %d desc %p",mDirectAudioTracks.size(),desc);
964 if (desc->mStreamType == stream) {
965 mStreamTypes[stream].volume = value;
966 desc->mVolumeScale = value;
967 desc->stream->set_volume(desc->stream,
968 desc->mVolumeLeft * mStreamTypes[stream].volume,
969 desc->mVolumeRight* mStreamTypes[stream].volume);
970 return NO_ERROR;
971 }
972 }
973 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800974#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975 PlaybackThread *thread = NULL;
976 if (output) {
977 thread = checkPlaybackThread_l(output);
978 if (thread == NULL) {
Ethan Chen8d420ed2013-11-14 00:40:54 -0800979#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530980 if (desc != NULL) {
981 return NO_ERROR;
982 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800983#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984 return BAD_VALUE;
985 }
986 }
987
988 mStreamTypes[stream].volume = value;
989
990 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800991 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700992 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993 }
994 } else {
995 thread->setStreamVolume(stream, value);
996 }
997
998 return NO_ERROR;
999}
1000
Glenn Kastenfff6d712012-01-12 16:38:12 -08001001status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001002{
1003 // check calling permissions
1004 if (!settingsAllowed()) {
1005 return PERMISSION_DENIED;
1006 }
1007
Glenn Kasten263709e2012-01-06 08:40:01 -08001008 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -07001009 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001010 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001011 return BAD_VALUE;
1012 }
1013
Eric Laurent93575202011-01-18 18:39:02 -08001014 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015 mStreamTypes[stream].mute = muted;
1016 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001017 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001018
1019 return NO_ERROR;
1020}
1021
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001022float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023{
Glenn Kasten263709e2012-01-06 08:40:01 -08001024 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001025 return 0.0f;
1026 }
1027
1028 AutoMutex lock(mLock);
1029 float volume;
1030 if (output) {
1031 PlaybackThread *thread = checkPlaybackThread_l(output);
1032 if (thread == NULL) {
1033 return 0.0f;
1034 }
1035 volume = thread->streamVolume(stream);
1036 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001037 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001038 }
1039
1040 return volume;
1041}
1042
Glenn Kastenfff6d712012-01-12 16:38:12 -08001043bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001044{
Glenn Kasten263709e2012-01-06 08:40:01 -08001045 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046 return true;
1047 }
1048
Glenn Kasten6637baa2012-01-09 09:40:36 -08001049 AutoMutex lock(mLock);
1050 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051}
1052
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001053status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001055 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1056 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001057
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058 // check calling permissions
1059 if (!settingsAllowed()) {
1060 return PERMISSION_DENIED;
1061 }
1062
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063 // ioHandle == 0 means the parameters are global to the audio hardware interface
1064 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001065 Mutex::Autolock _l(mLock);
Alok Kediya601e9b52013-09-27 11:55:10 +05301066#ifdef SRS_PROCESSING
1067 POSTPRO_PATCH_ICS_PARAMS_SET(keyValuePairs);
1068 if (!mDirectAudioTracks.isEmpty())
1069 audioConfigChanged_l(AudioSystem::EFFECT_CONFIG_CHANGED, 0, NULL);
1070#endif
Dima Zavin799a70e2011-04-18 16:57:27 -07001071 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001072 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001073 AutoMutex lock(mHardwareLock);
1074 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1075 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1076 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1077 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1078 final_result = result ?: final_result;
1079 }
1080 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001081 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001082 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1083 AudioParameter param = AudioParameter(keyValuePairs);
1084 String8 value;
1085 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001086 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1087 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001088 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1089 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001090 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001091 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1092 // collect all of the thread's session IDs
1093 KeyedVector<int, bool> ids = thread->sessionIds();
1094 // suspend effects associated with those session IDs
1095 for (size_t j = 0; j < ids.size(); ++j) {
1096 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001097 thread->setEffectSuspended(FX_IID_AEC,
1098 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001099 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001100 thread->setEffectSuspended(FX_IID_NS,
1101 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001102 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001103 }
1104 }
Eric Laurentbee53372011-08-29 12:42:48 -07001105 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001106 }
1107 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001108 String8 screenState;
1109 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1110 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001111 if (isOff != (AudioFlinger::mScreenState & 1)) {
1112 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001113 }
1114 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001115 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001116 }
1117
Ethan Chen8d420ed2013-11-14 00:40:54 -08001118#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301119 AudioSessionDescriptor *desc = NULL;
1120 if (!mDirectAudioTracks.isEmpty()) {
1121 desc = mDirectAudioTracks.valueFor(ioHandle);
1122 if (desc != NULL) {
1123 ALOGV("setParameters for mAudioTracks size %d desc %p",mDirectAudioTracks.size(),desc);
1124 desc->stream->common.set_parameters(&desc->stream->common, keyValuePairs.string());
1125 AudioParameter param = AudioParameter(keyValuePairs);
1126 String8 key = String8(AudioParameter::keyRouting);
1127 int device;
1128 if (param.getInt(key, device) == NO_ERROR) {
1129 mDirectDevice = device;
1130#ifdef SRS_PROCESSING
1131 ALOGV("setParameters:: routing change to device %d", device);
1132 POSTPRO_PATCH_ICS_OUTPROC_MIX_ROUTE(desc->trackRefPtr, param, device);
Santhosh Kumar Thimmanna Bhattar03fd1d52013-10-30 11:29:19 +05301133 if(desc->flag & AUDIO_OUTPUT_FLAG_TUNNEL)
1134 audioConfigChanged_l(AudioSystem::EFFECT_CONFIG_CHANGED, 0, NULL);
Alok Kediya601e9b52013-09-27 11:55:10 +05301135#endif
1136 if(mLPAEffectChain != NULL){
1137 mLPAEffectChain->setDevice_l(device);
1138 audioConfigChanged_l(AudioSystem::EFFECT_CONFIG_CHANGED, 0, NULL);
1139 }
1140 }
1141 }
1142 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001143#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301144
Mathias Agopian65ab4712010-07-14 17:59:35 -07001145 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1146 // and the thread is exited once the lock is released
1147 sp<ThreadBase> thread;
1148 {
1149 Mutex::Autolock _l(mLock);
1150 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001151 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001153 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001154 // indicate output device change to all input threads for pre processing
1155 AudioParameter param = AudioParameter(keyValuePairs);
1156 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001157 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1158 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001159 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1160 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1161 }
1162 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163 }
1164 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001165 if (thread != 0) {
1166 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167 }
1168 return BAD_VALUE;
1169}
1170
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001171String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001172{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001173 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1174 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001175
Eric Laurenta4c5a552012-03-29 10:12:40 -07001176 Mutex::Autolock _l(mLock);
1177
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001179 String8 out_s8;
1180
Alok Kediya601e9b52013-09-27 11:55:10 +05301181#ifdef SRS_PROCESSING
1182 POSTPRO_PATCH_ICS_PARAMS_GET(keys, out_s8);
1183#endif
Dima Zavin799a70e2011-04-18 16:57:27 -07001184 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001185 char *s;
1186 {
1187 AutoMutex lock(mHardwareLock);
1188 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001189 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001190 s = dev->get_parameters(dev, keys.string());
1191 mHardwareStatus = AUDIO_HW_IDLE;
1192 }
John Grossmanef7740b2012-02-09 11:28:36 -08001193 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001194 free(s);
1195 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001196 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001197 }
1198
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1200 if (playbackThread != NULL) {
1201 return playbackThread->getParameters(keys);
1202 }
1203 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1204 if (recordThread != NULL) {
1205 return recordThread->getParameters(keys);
1206 }
1207 return String8("");
1208}
1209
Glenn Kastendd8104c2012-07-02 12:42:44 -07001210size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1211 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001212{
Eric Laurenta1884f92011-08-23 08:25:03 -07001213 status_t ret = initCheck();
1214 if (ret != NO_ERROR) {
1215 return 0;
1216 }
1217
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001218 AutoMutex lock(mHardwareLock);
1219 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001220 struct audio_config config;
1221 memset(&config, 0, sizeof(config));
1222 config.sample_rate = sampleRate;
1223 config.channel_mask = channelMask;
1224 config.format = format;
1225
John Grossmanee578c02012-07-23 17:05:46 -07001226 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001227#ifndef ICS_AUDIO_BLOB
John Grossmanee578c02012-07-23 17:05:46 -07001228 size_t size = dev->get_input_buffer_size(dev, &config);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001229#else
1230 size_t size = dev->get_input_buffer_size(dev, sampleRate, format, popcount(channelMask));
1231#endif
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001232 mHardwareStatus = AUDIO_HW_IDLE;
1233 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001234}
1235
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001236unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238 Mutex::Autolock _l(mLock);
1239
1240 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1241 if (recordThread != NULL) {
1242 return recordThread->getInputFramesLost();
1243 }
1244 return 0;
1245}
1246
1247status_t AudioFlinger::setVoiceVolume(float value)
1248{
Eric Laurenta1884f92011-08-23 08:25:03 -07001249 status_t ret = initCheck();
1250 if (ret != NO_ERROR) {
1251 return ret;
1252 }
1253
Mathias Agopian65ab4712010-07-14 17:59:35 -07001254 // check calling permissions
1255 if (!settingsAllowed()) {
1256 return PERMISSION_DENIED;
1257 }
1258
1259 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001260 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001261 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001262 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263 mHardwareStatus = AUDIO_HW_IDLE;
1264
1265 return ret;
1266}
1267
Glenn Kasten26c77552012-11-16 12:01:44 -08001268status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001269 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001270{
1271 status_t status;
1272
1273 Mutex::Autolock _l(mLock);
1274
1275 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1276 if (playbackThread != NULL) {
1277 return playbackThread->getRenderPosition(halFrames, dspFrames);
1278 }
1279
1280 return BAD_VALUE;
1281}
1282
1283void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1284{
1285
1286 Mutex::Autolock _l(mLock);
1287
Glenn Kastenbb001922012-02-03 11:10:26 -08001288 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289 if (mNotificationClients.indexOfKey(pid) < 0) {
1290 sp<NotificationClient> notificationClient = new NotificationClient(this,
1291 client,
1292 pid);
Steve Block3856b092011-10-20 11:56:00 +01001293 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001294
1295 mNotificationClients.add(pid, notificationClient);
1296
1297 sp<IBinder> binder = client->asBinder();
1298 binder->linkToDeath(notificationClient);
1299
1300 // the config change is always sent from playback or record threads to avoid deadlock
1301 // with AudioSystem::gLock
1302 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001303 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001304 }
1305
1306 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001307 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001308 }
1309 }
1310}
1311
Ethan Chen8d420ed2013-11-14 00:40:54 -08001312#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301313status_t AudioFlinger::deregisterClient(const sp<IAudioFlingerClient>& client)
1314{
1315 ALOGV("deregisterClient() %p, tid %d, calling tid %d", client.get(), gettid(), IPCThreadState::self()->getCallingPid());
1316 Mutex::Autolock _l(mLock);
1317
1318 pid_t pid = IPCThreadState::self()->getCallingPid();
1319 int index = mNotificationClients.indexOfKey(pid);
1320 if (index >= 0) {
1321 mNotificationClients.removeItemsAt(index);
1322 return true;
1323 }
1324
1325 return false;
1326}
Ethan Chen8d420ed2013-11-14 00:40:54 -08001327#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301328
Mathias Agopian65ab4712010-07-14 17:59:35 -07001329void AudioFlinger::removeNotificationClient(pid_t pid)
1330{
1331 Mutex::Autolock _l(mLock);
1332
Glenn Kastena3b09252012-01-20 09:19:01 -08001333 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001334
Steve Block3856b092011-10-20 11:56:00 +01001335 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001336 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001337 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001338 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001339 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001340 ALOGV(" pid %d @ %d", ref->mPid, i);
1341 if (ref->mPid == pid) {
1342 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001343 mAudioSessionRefs.removeAt(i);
1344 delete ref;
1345 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001346 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001347 } else {
1348 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001349 }
1350 }
1351 if (removed) {
1352 purgeStaleEffects_l();
1353 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001354}
1355
1356// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001357void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001358{
Ethan Chen8d420ed2013-11-14 00:40:54 -08001359#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301360 ALOGV("AudioFlinger::audioConfigChanged_l: event %d", event);
1361 if (event == AudioSystem::EFFECT_CONFIG_CHANGED) {
1362 mIsEffectConfigChanged = true;
1363 }
1364 if (!mNotificationClients.isEmpty()){
Ethan Chen8d420ed2013-11-14 00:40:54 -08001365#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301366 size_t size = mNotificationClients.size();
1367 for (size_t i = 0; i < size; i++) {
1368 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1369 param2);
1370 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001371#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301372 }
1373 if ((!mDirectAudioTracks.isEmpty())&& (event == AudioSystem::EFFECT_CONFIG_CHANGED)){
1374 size_t dsize = mDirectAudioTracks.size();
1375 for(size_t i = 0; i < dsize; i++) {
1376 AudioSessionDescriptor *desc = mDirectAudioTracks.valueAt(i);
1377 if(desc && ((DirectAudioTrack*)desc->trackRefPtr)) {
1378 ALOGV("signalling directAudioTrack ");
1379 ((DirectAudioTrack*)desc->trackRefPtr)->signalEffect();
1380 } else{
1381 ALOGV("not found track to signal directAudioTrack ");
1382 }
1383 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001384 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001385#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386}
1387
1388// removeClient_l() must be called with AudioFlinger::mLock held
1389void AudioFlinger::removeClient_l(pid_t pid)
1390{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001391 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001392 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001393 mClients.removeItem(pid);
1394}
1395
Eric Laurent717e1282012-06-29 16:36:52 -07001396// getEffectThread_l() must be called with AudioFlinger::mLock held
1397sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1398{
1399 sp<PlaybackThread> thread;
1400
1401 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1402 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1403 ALOG_ASSERT(thread == 0);
1404 thread = mPlaybackThreads.valueAt(i);
1405 }
1406 }
1407
1408 return thread;
1409}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001410
Mathias Agopian65ab4712010-07-14 17:59:35 -07001411
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412
1413// ----------------------------------------------------------------------------
1414
1415AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1416 : RefBase(),
1417 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001418 // 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 -07001419 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001420 mPid(pid),
1421 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001422{
1423 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1424}
1425
1426// Client destructor must be called with AudioFlinger::mLock held
1427AudioFlinger::Client::~Client()
1428{
1429 mAudioFlinger->removeClient_l(mPid);
1430}
1431
Glenn Kasten435dbe62012-01-30 10:15:48 -08001432sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001433{
1434 return mMemoryDealer;
1435}
1436
John Grossman4ff14ba2012-02-08 16:37:41 -08001437// Reserve one of the limited slots for a timed audio track associated
1438// with this client
1439bool AudioFlinger::Client::reserveTimedTrack()
1440{
1441 const int kMaxTimedTracksPerClient = 4;
1442
1443 Mutex::Autolock _l(mTimedTrackLock);
1444
1445 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1446 ALOGW("can not create timed track - pid %d has exceeded the limit",
1447 mPid);
1448 return false;
1449 }
1450
1451 mTimedTrackCount++;
1452 return true;
1453}
1454
1455// Release a slot for a timed audio track
1456void AudioFlinger::Client::releaseTimedTrack()
1457{
1458 Mutex::Autolock _l(mTimedTrackLock);
1459 mTimedTrackCount--;
1460}
1461
Mathias Agopian65ab4712010-07-14 17:59:35 -07001462// ----------------------------------------------------------------------------
1463
1464AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1465 const sp<IAudioFlingerClient>& client,
1466 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001467 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001468{
1469}
1470
1471AudioFlinger::NotificationClient::~NotificationClient()
1472{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473}
1474
1475void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1476{
1477 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001478 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001479}
1480
Mathias Agopian65ab4712010-07-14 17:59:35 -07001481
1482// ----------------------------------------------------------------------------
1483
Jeff Brown893a5642013-08-16 20:19:26 -07001484static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1485 return audio_is_remote_submix_device(inDevice);
1486}
1487
Mathias Agopian65ab4712010-07-14 17:59:35 -07001488sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001489 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001491 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001492 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001493 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001494 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001495 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001496 int *sessionId,
1497 status_t *status)
1498{
1499 sp<RecordThread::RecordTrack> recordTrack;
1500 sp<RecordHandle> recordHandle;
1501 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001502 status_t lStatus;
1503 RecordThread *thread;
1504 size_t inFrameCount;
1505 int lSessionId;
Ethan Chen8d420ed2013-11-14 00:40:54 -08001506#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301507 size_t inputBufferSize = 0;
1508 uint32_t channelCount = popcount(channelMask);
Ethan Chen8d420ed2013-11-14 00:40:54 -08001509#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001510
1511 // check calling permissions
1512 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001513 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001514 lStatus = PERMISSION_DENIED;
1515 goto Exit;
1516 }
1517
Ethan Chen8d420ed2013-11-14 00:40:54 -08001518#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301519 // Check that audio input stream accepts requested audio parameters
1520 inputBufferSize = getInputBufferSize(sampleRate, format, channelCount);
1521 if (inputBufferSize == 0) {
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001522 lStatus = BAD_VALUE;
Alok Kediya601e9b52013-09-27 11:55:10 +05301523 ALOGE("Bad audio input parameters: sampling rate %u, format %d, channels %d", sampleRate, format, channelCount);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001524 goto Exit;
1525 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001526#else
1527 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1528 ALOGE("openRecord() invalid format %d", format);
1529 lStatus = BAD_VALUE;
1530 goto Exit;
1531 }
1532#endif
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001533
Mathias Agopian65ab4712010-07-14 17:59:35 -07001534 // add client to list
1535 { // scope for mLock
1536 Mutex::Autolock _l(mLock);
1537 thread = checkRecordThread_l(input);
1538 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001539 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001540 lStatus = BAD_VALUE;
1541 goto Exit;
1542 }
1543
Jeff Brown893a5642013-08-16 20:19:26 -07001544 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1545 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001546 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001547 lStatus = PERMISSION_DENIED;
1548 goto Exit;
1549 }
1550
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001551 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001552 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001553
1554 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001555 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556 lSessionId = *sessionId;
1557 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001558 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001559 if (sessionId != NULL) {
1560 *sessionId = lSessionId;
1561 }
1562 }
Alok Kediya601e9b52013-09-27 11:55:10 +05301563
Ethan Chen8d420ed2013-11-14 00:40:54 -08001564#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301565 // frameCount must be a multiple of input buffer size
1566 // Change for Codec type
1567 uint8_t channelCount = popcount(channelMask);
1568 if (*flags & IAudioFlinger::TRACK_VOICE_COMMUNICATION) {
1569 inFrameCount = inputBufferSize/channelCount/sizeof(short);
1570 } else {
1571 if ((format == AUDIO_FORMAT_PCM_16_BIT) ||
1572 (format == AUDIO_FORMAT_PCM_8_BIT)) {
1573 inFrameCount = inputBufferSize/channelCount/sizeof(short);
1574 } else if (format == AUDIO_FORMAT_AMR_NB) {
1575 inFrameCount = inputBufferSize/channelCount/AMR_FRAMESIZE;
1576 } else if (format == AUDIO_FORMAT_EVRC) {
1577 inFrameCount = inputBufferSize/channelCount/EVRC_FRAMESIZE;
1578 } else if (format == AUDIO_FORMAT_QCELP) {
1579 inFrameCount = inputBufferSize/channelCount/QCELP_FRAMESIZE;
1580 } else if (format == AUDIO_FORMAT_AAC) {
1581 inFrameCount = inputBufferSize/AAC_FRAMESIZE;
1582 } else if (format == AUDIO_FORMAT_AMR_WB) {
1583 inFrameCount = inputBufferSize/channelCount/AMR_WB_FRAMESIZE;
1584 }
1585 }
1586 frameCount = ((frameCount - 1)/inFrameCount + 1) * inFrameCount;
Ethan Chen8d420ed2013-11-14 00:40:54 -08001587#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301588
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001589 // create new record track.
1590 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001591 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kastenddb0ccf2013-07-31 16:14:50 -07001592 frameCount, lSessionId, flags, tid, &lStatus);
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001593 LOG_ALWAYS_FATAL_IF((recordTrack != 0) != (lStatus == NO_ERROR));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001595 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001596 // remove local strong reference to Client before deleting the RecordTrack so that the
1597 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001598 client.clear();
1599 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600 goto Exit;
1601 }
1602
1603 // return to handle to client
1604 recordHandle = new RecordHandle(recordTrack);
1605 lStatus = NO_ERROR;
1606
1607Exit:
1608 if (status) {
1609 *status = lStatus;
1610 }
1611 return recordHandle;
1612}
1613
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001614
1615
Mathias Agopian65ab4712010-07-14 17:59:35 -07001616// ----------------------------------------------------------------------------
1617
Eric Laurenta4c5a552012-03-29 10:12:40 -07001618audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1619{
1620 if (!settingsAllowed()) {
1621 return 0;
1622 }
1623 Mutex::Autolock _l(mLock);
1624 return loadHwModule_l(name);
1625}
1626
1627// loadHwModule_l() must be called with AudioFlinger::mLock held
1628audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1629{
1630 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1631 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1632 ALOGW("loadHwModule() module %s already loaded", name);
1633 return mAudioHwDevs.keyAt(i);
1634 }
1635 }
1636
Eric Laurenta4c5a552012-03-29 10:12:40 -07001637 audio_hw_device_t *dev;
1638
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001639 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001640 if (rc) {
1641 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1642 return 0;
1643 }
1644
1645 mHardwareStatus = AUDIO_HW_INIT;
1646 rc = dev->init_check(dev);
1647 mHardwareStatus = AUDIO_HW_IDLE;
1648 if (rc) {
1649 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1650 return 0;
1651 }
1652
John Grossmanee578c02012-07-23 17:05:46 -07001653 // Check and cache this HAL's level of support for master mute and master
1654 // volume. If this is the first HAL opened, and it supports the get
1655 // methods, use the initial values provided by the HAL as the current
1656 // master mute and volume settings.
1657
1658 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1659 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001660 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001661
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001662#if !defined(ICS_AUDIO_BLOB) && !defined(MR0_AUDIO_BLOB)
John Grossmanee578c02012-07-23 17:05:46 -07001663 if (0 == mAudioHwDevs.size()) {
1664 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1665 if (NULL != dev->get_master_volume) {
1666 float mv;
1667 if (OK == dev->get_master_volume(dev, &mv)) {
1668 mMasterVolume = mv;
1669 }
1670 }
1671
1672 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1673 if (NULL != dev->get_master_mute) {
1674 bool mm;
1675 if (OK == dev->get_master_mute(dev, &mm)) {
1676 mMasterMute = mm;
1677 }
1678 }
1679 }
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001680#endif
John Grossmanee578c02012-07-23 17:05:46 -07001681
Eric Laurenta4c5a552012-03-29 10:12:40 -07001682 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001683 if ((NULL != dev->set_master_volume) &&
1684 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1685 flags = static_cast<AudioHwDevice::Flags>(flags |
1686 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1687 }
1688
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001689#if !defined(ICS_AUDIO_BLOB) && !defined(MR0_AUDIO_BLOB)
John Grossmanee578c02012-07-23 17:05:46 -07001690 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1691 if ((NULL != dev->set_master_mute) &&
1692 (OK == dev->set_master_mute(dev, mMasterMute))) {
1693 flags = static_cast<AudioHwDevice::Flags>(flags |
1694 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1695 }
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001696#endif
John Grossmanee578c02012-07-23 17:05:46 -07001697
Eric Laurenta4c5a552012-03-29 10:12:40 -07001698 mHardwareStatus = AUDIO_HW_IDLE;
1699 }
1700
1701 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001702 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001703
1704 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001705 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001706
1707 return handle;
1708
1709}
1710
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001711// ----------------------------------------------------------------------------
1712
Glenn Kasten3b16c762012-11-14 08:44:39 -08001713uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001714{
1715 Mutex::Autolock _l(mLock);
1716 PlaybackThread *thread = primaryPlaybackThread_l();
1717 return thread != NULL ? thread->sampleRate() : 0;
1718}
1719
Glenn Kastene33054e2012-11-14 12:54:39 -08001720size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001721{
1722 Mutex::Autolock _l(mLock);
1723 PlaybackThread *thread = primaryPlaybackThread_l();
1724 return thread != NULL ? thread->frameCountHAL() : 0;
1725}
1726
1727// ----------------------------------------------------------------------------
1728
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001729status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1730{
1731 uid_t uid = IPCThreadState::self()->getCallingUid();
1732 if (uid != AID_SYSTEM) {
1733 return PERMISSION_DENIED;
1734 }
1735 Mutex::Autolock _l(mLock);
1736 if (mIsDeviceTypeKnown) {
1737 return INVALID_OPERATION;
1738 }
1739 mIsLowRamDevice = isLowRamDevice;
1740 mIsDeviceTypeKnown = true;
1741 return NO_ERROR;
1742}
1743
1744// ----------------------------------------------------------------------------
1745
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001746#ifdef HAVE_PRE_KITKAT_AUDIO_BLOB
1747audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1748 audio_devices_t *pDevices,
1749 uint32_t *pSamplingRate,
1750 audio_format_t *pFormat,
1751 audio_channel_mask_t *pChannelMask,
1752 uint32_t *pLatencyMs,
1753 audio_output_flags_t flags)
1754{
1755 return openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
1756 pLatencyMs, flags, NULL);
1757}
1758#endif
1759
Eric Laurenta4c5a552012-03-29 10:12:40 -07001760audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1761 audio_devices_t *pDevices,
1762 uint32_t *pSamplingRate,
1763 audio_format_t *pFormat,
1764 audio_channel_mask_t *pChannelMask,
1765 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001766 audio_output_flags_t flags,
1767 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001768{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001769 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001770 struct audio_config config;
1771 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1772 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1773 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001774#ifndef HAVE_PRE_KITKAT_AUDIO_BLOB
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001775 if (offloadInfo) {
1776 config.offload_info = *offloadInfo;
1777 }
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001778#endif
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001779
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001780 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001781 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001782
Eric Laurentbfb1b832013-01-07 09:53:42 -08001783 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001784 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001785 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001786 config.sample_rate,
1787 config.format,
1788 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001789 flags);
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001790#ifndef HAVE_PRE_KITKAT_AUDIO_BLOB
Eric Laurentbfb1b832013-01-07 09:53:42 -08001791 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1792 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version );
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001793#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001794
1795 if (pDevices == NULL || *pDevices == 0) {
1796 return 0;
1797 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001798
Mathias Agopian65ab4712010-07-14 17:59:35 -07001799 Mutex::Autolock _l(mLock);
1800
Eric Laurenta4c5a552012-03-29 10:12:40 -07001801 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001802 if (outHwDev == NULL)
1803 return 0;
1804
John Grossmanee578c02012-07-23 17:05:46 -07001805 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001806 audio_io_handle_t id = nextUniqueId();
1807
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001808 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001809
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001810 status_t status;
1811#ifndef ICS_AUDIO_BLOB
1812 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001813 id,
1814 *pDevices,
1815 (audio_output_flags_t)flags,
1816 &config,
1817 &outStream);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001818#else
1819 status = hwDevHal->open_output_stream(hwDevHal,
1820 *pDevices,
1821 (int *)&config.format,
1822 &config.channel_mask,
1823 &config.sample_rate,
1824 &outStream);
1825 uint32_t newflags = flags | AUDIO_OUTPUT_FLAG_PRIMARY;
1826 flags = (audio_output_flags_t)newflags;
1827#endif
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001828
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001829 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001830 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001831 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001832 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001833 config.sample_rate,
1834 config.format,
1835 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001836 status);
1837
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001838 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001839 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001840 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1841 thread = new OffloadThread(this, output, id, *pDevices);
1842 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
Ethan Chen8d420ed2013-11-14 00:40:54 -08001843#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301844 }
1845 if (flags & AUDIO_OUTPUT_FLAG_LPA || flags & AUDIO_OUTPUT_FLAG_TUNNEL ) {
1846 AudioSessionDescriptor *desc = new AudioSessionDescriptor(hwDevHal, outStream, flags);
1847 desc->mActive = true;
1848 //TODO: no stream type
1849 //desc->mStreamType = streamType;
1850 desc->mVolumeLeft = 1.0;
1851 desc->mVolumeRight = 1.0;
1852 desc->device = *pDevices;
1853 desc->trackRefPtr = NULL;
1854 mDirectAudioTracks.add(id, desc);
1855 mDirectDevice = desc->device;
Ethan Chen8d420ed2013-11-14 00:40:54 -08001856#endif
Eric Laurentbfb1b832013-01-07 09:53:42 -08001857 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001858 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1859 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001860 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001861 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001862 } else {
1863 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001864 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001865 }
Alok Kediya601e9b52013-09-27 11:55:10 +05301866 if (thread != NULL) {
1867 mPlaybackThreads.add(id, thread);
1868 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001869
Glenn Kasten7c027242012-12-26 14:43:16 -08001870 if (pSamplingRate != NULL) {
1871 *pSamplingRate = config.sample_rate;
1872 }
1873 if (pFormat != NULL) {
1874 *pFormat = config.format;
1875 }
1876 if (pChannelMask != NULL) {
1877 *pChannelMask = config.channel_mask;
1878 }
Alok Kediya601e9b52013-09-27 11:55:10 +05301879 if (thread != NULL) {
1880 if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
1881 // notify client processes of the new output creation
1882 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1883 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001884#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301885 else {
1886 *pLatencyMs = 0;
1887 if ((flags & AUDIO_OUTPUT_FLAG_LPA) || (flags & AUDIO_OUTPUT_FLAG_TUNNEL)) {
1888 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(id);
1889 *pLatencyMs = desc->stream->get_latency(desc->stream);
1890 }
Glenn Kasten7c027242012-12-26 14:43:16 -08001891 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001892#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001893
Eric Laurenta4c5a552012-03-29 10:12:40 -07001894
1895 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001896 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001897 ALOGI("Using module %d has the primary audio interface", module);
1898 mPrimaryHardwareDev = outHwDev;
Alok Kediya601e9b52013-09-27 11:55:10 +05301899#ifdef SRS_PROCESSING
1900 SRS_Processing::RawDataSet(NULL, "qdsp hook", &mPrimaryHardwareDev,
1901 sizeof(&mPrimaryHardwareDev));
1902#endif
Eric Laurenta4c5a552012-03-29 10:12:40 -07001903 AutoMutex lock(mHardwareLock);
1904 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001905 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001906 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001907 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001908 return id;
1909 }
1910
1911 return 0;
1912}
1913
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001914audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1915 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001916{
1917 Mutex::Autolock _l(mLock);
1918 MixerThread *thread1 = checkMixerThread_l(output1);
1919 MixerThread *thread2 = checkMixerThread_l(output2);
1920
1921 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001922 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1923 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001924 return 0;
1925 }
1926
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001927 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1929 thread->addOutputTrack(thread2);
1930 mPlaybackThreads.add(id, thread);
1931 // notify client processes of the new output creation
1932 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1933 return id;
1934}
1935
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001936status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937{
Glenn Kastend96c5722012-04-25 13:44:49 -07001938 return closeOutput_nonvirtual(output);
1939}
1940
1941status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1942{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001943 // keep strong reference on the playback thread so that
1944 // it is not destroyed while exit() is executed
Ethan Chen8d420ed2013-11-14 00:40:54 -08001945#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301946 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
1947 if (desc) {
1948 ALOGV("Closing DirectTrack output %d", output);
1949 desc->mActive = false;
1950 desc->stream->common.standby(&desc->stream->common);
1951 desc->hwDev->close_output_stream(desc->hwDev, desc->stream);
1952 desc->trackRefPtr = NULL;
1953 desc->stream = NULL;
1954 mDirectAudioTracks.removeItem(output);
1955 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
1956 delete desc;
1957 return NO_ERROR;
1958 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001959#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301960
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001961 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001962 {
1963 Mutex::Autolock _l(mLock);
1964 thread = checkPlaybackThread_l(output);
1965 if (thread == NULL) {
1966 return BAD_VALUE;
1967 }
1968
Steve Block3856b092011-10-20 11:56:00 +01001969 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001970
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001971 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001972 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001973 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001974 DuplicatingThread *dupThread =
1975 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001976 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001977
1978 }
1979 }
1980 }
1981
1982
1983 mPlaybackThreads.removeItem(output);
1984 // save all effects to the default thread
1985 if (mPlaybackThreads.size()) {
1986 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1987 if (dstThread != NULL) {
1988 // audioflinger lock is held here so the acquisition order of thread locks does not
1989 // matter
1990 Mutex::Autolock _dl(dstThread->mLock);
1991 Mutex::Autolock _sl(thread->mLock);
1992 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1993 for (size_t i = 0; i < effectChains.size(); i ++) {
1994 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001995 }
1996 }
1997 }
Glenn Kastena1117922012-01-26 10:53:32 -08001998 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001999 }
2000 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08002001 // The thread entity (active unit of execution) is no longer running here,
2002 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002003
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002004 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002005 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002006 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002007 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002008 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002009 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002010 }
2011 return NO_ERROR;
2012}
2013
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002014status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002015{
2016 Mutex::Autolock _l(mLock);
2017 PlaybackThread *thread = checkPlaybackThread_l(output);
2018
2019 if (thread == NULL) {
2020 return BAD_VALUE;
2021 }
2022
Steve Block3856b092011-10-20 11:56:00 +01002023 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024 thread->suspend();
2025
2026 return NO_ERROR;
2027}
2028
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002029status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002030{
2031 Mutex::Autolock _l(mLock);
2032 PlaybackThread *thread = checkPlaybackThread_l(output);
2033
2034 if (thread == NULL) {
2035 return BAD_VALUE;
2036 }
2037
Steve Block3856b092011-10-20 11:56:00 +01002038 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002039
2040 thread->restore();
2041
2042 return NO_ERROR;
2043}
2044
Eric Laurenta4c5a552012-03-29 10:12:40 -07002045audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
2046 audio_devices_t *pDevices,
2047 uint32_t *pSamplingRate,
2048 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07002049 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002050{
2051 status_t status;
2052 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002053 struct audio_config config;
2054 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
2055 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
2056 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
2057
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002058 uint32_t reqSamplingRate = config.sample_rate;
2059 audio_format_t reqFormat = config.format;
2060 audio_channel_mask_t reqChannels = config.channel_mask;
2061 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07002062 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002063
2064 if (pDevices == NULL || *pDevices == 0) {
2065 return 0;
2066 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002067
Mathias Agopian65ab4712010-07-14 17:59:35 -07002068 Mutex::Autolock _l(mLock);
2069
Eric Laurenta4c5a552012-03-29 10:12:40 -07002070 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07002071 if (inHwDev == NULL)
2072 return 0;
2073
John Grossmanee578c02012-07-23 17:05:46 -07002074 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002075 audio_io_handle_t id = nextUniqueId();
2076
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00002077#ifndef ICS_AUDIO_BLOB
John Grossmanee578c02012-07-23 17:05:46 -07002078 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07002079 &inStream);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00002080#else
2081 status = inHwHal->open_input_stream(inHwHal, *pDevices,
2082 (int *)&config.format,
2083 &config.channel_mask,
2084 &config.sample_rate, (audio_in_acoustics_t)0,
2085 &inStream);
2086#endif
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002087 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
2088 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07002089 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002090 config.sample_rate,
2091 config.format,
2092 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002093 status);
2094
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002095 // If the input could not be opened with the requested parameters and we can handle the
2096 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
2097 // 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 -07002098 if (status == BAD_VALUE &&
2099 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
2100 (config.sample_rate <= 2 * reqSamplingRate) &&
Alok Kediya601e9b52013-09-27 11:55:10 +05302101 (getInputChannelCount(config.channel_mask) <= FCC_2) && (getInputChannelCount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07002102 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002103 inStream = NULL;
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00002104#ifndef ICS_AUDIO_BLOB
John Grossmanee578c02012-07-23 17:05:46 -07002105 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00002106#else
2107 status = inHwHal->open_input_stream(inHwHal, *pDevices,
2108 (int *)&config.format,
2109 &config.channel_mask,
2110 &config.sample_rate, (audio_in_acoustics_t)0,
2111 &inStream);
2112#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07002113 }
2114
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002115 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002116
Glenn Kasten46909e72013-02-26 09:20:22 -08002117#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002118 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2119 // or (re-)create if current Pipe is idle and does not match the new format
2120 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002121 enum {
2122 TEE_SINK_NO, // don't copy input
2123 TEE_SINK_NEW, // copy input using a new pipe
2124 TEE_SINK_OLD, // copy input using an existing pipe
2125 } kind;
2126 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
2127 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002128 if (!mTeeSinkInputEnabled) {
2129 kind = TEE_SINK_NO;
2130 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002131 kind = TEE_SINK_NO;
2132 } else if (mRecordTeeSink == 0) {
2133 kind = TEE_SINK_NEW;
2134 } else if (mRecordTeeSink->getStrongCount() != 1) {
2135 kind = TEE_SINK_NO;
2136 } else if (format == mRecordTeeSink->format()) {
2137 kind = TEE_SINK_OLD;
2138 } else {
2139 kind = TEE_SINK_NEW;
2140 }
2141 switch (kind) {
2142 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002143 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002144 size_t numCounterOffers = 0;
2145 const NBAIO_Format offers[1] = {format};
2146 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2147 ALOG_ASSERT(index == 0);
2148 PipeReader *pipeReader = new PipeReader(*pipe);
2149 numCounterOffers = 0;
2150 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2151 ALOG_ASSERT(index == 0);
2152 mRecordTeeSink = pipe;
2153 mRecordTeeSource = pipeReader;
2154 teeSink = pipe;
2155 }
2156 break;
2157 case TEE_SINK_OLD:
2158 teeSink = mRecordTeeSink;
2159 break;
2160 case TEE_SINK_NO:
2161 default:
2162 break;
2163 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002164#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002165
Dima Zavin799a70e2011-04-18 16:57:27 -07002166 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
2167
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002168 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002169 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002170 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002171 thread = new RecordThread(this,
2172 input,
2173 reqSamplingRate,
2174 reqChannels,
2175 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08002176 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08002177 *pDevices
2178#ifdef TEE_SINK
2179 , teeSink
2180#endif
2181 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07002182 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01002183 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08002184 if (pSamplingRate != NULL) {
2185 *pSamplingRate = reqSamplingRate;
2186 }
2187 if (pFormat != NULL) {
2188 *pFormat = config.format;
2189 }
2190 if (pChannelMask != NULL) {
2191 *pChannelMask = reqChannels;
2192 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002193
Mathias Agopian65ab4712010-07-14 17:59:35 -07002194 // notify client processes of the new input creation
2195 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
2196 return id;
2197 }
2198
2199 return 0;
2200}
2201
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002202status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002203{
Glenn Kastend96c5722012-04-25 13:44:49 -07002204 return closeInput_nonvirtual(input);
2205}
2206
2207status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2208{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002209 // keep strong reference on the record thread so that
2210 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002211 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002212 {
2213 Mutex::Autolock _l(mLock);
2214 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002215 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002216 return BAD_VALUE;
2217 }
2218
Steve Block3856b092011-10-20 11:56:00 +01002219 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08002220 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002221 mRecordThreads.removeItem(input);
2222 }
2223 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08002224 // The thread entity (active unit of execution) is no longer running here,
2225 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002226
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002227 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002228 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002229 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002230 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002231 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002232
2233 return NO_ERROR;
2234}
2235
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002236status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237{
2238 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01002239 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002240
2241 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2242 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002243 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002244 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002245
2246 return NO_ERROR;
2247}
2248
2249
2250int AudioFlinger::newAudioSessionId()
2251{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002252 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002253}
2254
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002255void AudioFlinger::acquireAudioSessionId(int audioSession)
2256{
2257 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002258 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01002259 ALOGV("acquiring %d from %d", audioSession, caller);
Eric Laurentd1b28d42013-09-18 18:47:13 -07002260
2261 // Ignore requests received from processes not known as notification client. The request
2262 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2263 // called from a different pid leaving a stale session reference. Also we don't know how
2264 // to clear this reference if the client process dies.
2265 if (mNotificationClients.indexOfKey(caller) < 0) {
2266 ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2267 return;
2268 }
2269
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002270 size_t num = mAudioSessionRefs.size();
2271 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002272 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002273 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2274 ref->mCnt++;
2275 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002276 return;
2277 }
2278 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002279 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2280 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002281}
2282
2283void AudioFlinger::releaseAudioSessionId(int audioSession)
2284{
2285 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002286 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01002287 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002288 size_t num = mAudioSessionRefs.size();
2289 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002290 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002291 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2292 ref->mCnt--;
2293 ALOGV(" decremented refcount to %d", ref->mCnt);
2294 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002295 mAudioSessionRefs.removeAt(i);
2296 delete ref;
2297 purgeStaleEffects_l();
2298 }
2299 return;
2300 }
2301 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002302 // If the caller is mediaserver it is likely that the session being released was acquired
2303 // on behalf of a process not in notification clients and we ignore the warning.
2304 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002305}
2306
2307void AudioFlinger::purgeStaleEffects_l() {
2308
Steve Block3856b092011-10-20 11:56:00 +01002309 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002310
2311 Vector< sp<EffectChain> > chains;
2312
2313 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2314 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2315 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2316 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002317 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2318 chains.push(ec);
2319 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002320 }
2321 }
2322 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2323 sp<RecordThread> t = mRecordThreads.valueAt(i);
2324 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2325 sp<EffectChain> ec = t->mEffectChains[j];
2326 chains.push(ec);
2327 }
2328 }
2329
2330 for (size_t i = 0; i < chains.size(); i++) {
2331 sp<EffectChain> ec = chains[i];
2332 int sessionid = ec->sessionId();
2333 sp<ThreadBase> t = ec->mThread.promote();
2334 if (t == 0) {
2335 continue;
2336 }
2337 size_t numsessionrefs = mAudioSessionRefs.size();
2338 bool found = false;
2339 for (size_t k = 0; k < numsessionrefs; k++) {
2340 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002341 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002342 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002343 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002344 found = true;
2345 break;
2346 }
2347 }
2348 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002349 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002350 // remove all effects from the chain
2351 while (ec->mEffects.size()) {
2352 sp<EffectModule> effect = ec->mEffects[0];
2353 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002354 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002355 if (effect->purgeHandles()) {
2356 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002357 }
2358 AudioSystem::unregisterEffect(effect->id());
2359 }
2360 }
2361 }
2362 return;
2363}
2364
Mathias Agopian65ab4712010-07-14 17:59:35 -07002365// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002366AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002367{
Glenn Kastena1117922012-01-26 10:53:32 -08002368 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002369}
2370
2371// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002372AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002373{
2374 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002375 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002376}
2377
2378// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002379AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002380{
Glenn Kastena1117922012-01-26 10:53:32 -08002381 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002382}
2383
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002384uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002385{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002386 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387}
2388
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002389AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002390{
2391 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2392 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002393 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002394 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002395 return thread;
2396 }
2397 }
2398 return NULL;
2399}
2400
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002401audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002402{
2403 PlaybackThread *thread = primaryPlaybackThread_l();
2404
2405 if (thread == NULL) {
2406 return 0;
2407 }
2408
Eric Laurentf1c04f92012-08-28 14:26:53 -07002409 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002410}
2411
Eric Laurenta011e352012-03-29 15:51:43 -07002412sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2413 int triggerSession,
2414 int listenerSession,
2415 sync_event_callback_t callBack,
2416 void *cookie)
2417{
2418 Mutex::Autolock _l(mLock);
2419
2420 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2421 status_t playStatus = NAME_NOT_FOUND;
2422 status_t recStatus = NAME_NOT_FOUND;
2423 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2424 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2425 if (playStatus == NO_ERROR) {
2426 return event;
2427 }
2428 }
2429 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2430 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2431 if (recStatus == NO_ERROR) {
2432 return event;
2433 }
2434 }
2435 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2436 mPendingSyncEvents.add(event);
2437 } else {
2438 ALOGV("createSyncEvent() invalid event %d", event->type());
2439 event.clear();
2440 }
2441 return event;
2442}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002443
Mathias Agopian65ab4712010-07-14 17:59:35 -07002444// ----------------------------------------------------------------------------
2445// Effect management
2446// ----------------------------------------------------------------------------
2447
2448
Glenn Kastenf587ba52012-01-26 16:25:10 -08002449status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002450{
2451 Mutex::Autolock _l(mLock);
2452 return EffectQueryNumberEffects(numEffects);
2453}
2454
Glenn Kastenf587ba52012-01-26 16:25:10 -08002455status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002456{
2457 Mutex::Autolock _l(mLock);
2458 return EffectQueryEffect(index, descriptor);
2459}
2460
Glenn Kasten5e92a782012-01-30 07:40:52 -08002461status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002462 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002463{
2464 Mutex::Autolock _l(mLock);
2465 return EffectGetDescriptor(pUuid, descriptor);
2466}
2467
2468
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002469sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002470 effect_descriptor_t *pDesc,
2471 const sp<IEffectClient>& effectClient,
2472 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002473 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002474 int sessionId,
2475 status_t *status,
2476 int *id,
2477 int *enabled)
2478{
2479 status_t lStatus = NO_ERROR;
2480 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002481 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002482
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002483 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002484 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002485 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002486
2487 if (pDesc == NULL) {
2488 lStatus = BAD_VALUE;
2489 goto Exit;
2490 }
2491
Eric Laurent84e9a102010-09-23 16:10:16 -07002492 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002493 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002494 lStatus = PERMISSION_DENIED;
2495 goto Exit;
2496 }
2497
Dima Zavinfce7a472011-04-19 22:30:36 -07002498 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002499 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002500 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002501 lStatus = PERMISSION_DENIED;
2502 goto Exit;
2503 }
2504
Mathias Agopian65ab4712010-07-14 17:59:35 -07002505 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002506 if (!EffectIsNullUuid(&pDesc->uuid)) {
2507 // if uuid is specified, request effect descriptor
2508 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2509 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002510 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002511 goto Exit;
2512 }
2513 } else {
2514 // if uuid is not specified, look for an available implementation
2515 // of the required type in effect factory
2516 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002517 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002518 lStatus = BAD_VALUE;
2519 goto Exit;
2520 }
2521 uint32_t numEffects = 0;
2522 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002523 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002524 bool found = false;
2525
2526 lStatus = EffectQueryNumberEffects(&numEffects);
2527 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002528 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002529 goto Exit;
2530 }
2531 for (uint32_t i = 0; i < numEffects; i++) {
2532 lStatus = EffectQueryEffect(i, &desc);
2533 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002534 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002535 continue;
2536 }
2537 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2538 // If matching type found save effect descriptor. If the session is
2539 // 0 and the effect is not auxiliary, continue enumeration in case
2540 // an auxiliary version of this effect type is available
2541 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002542 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002543 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002544 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2545 break;
2546 }
2547 }
2548 }
2549 if (!found) {
2550 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002551 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002552 goto Exit;
2553 }
2554 // For same effect type, chose auxiliary version over insert version if
2555 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002556 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002557 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002558 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002559 }
2560 }
2561
2562 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002563 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002564 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2565 lStatus = INVALID_OPERATION;
2566 goto Exit;
2567 }
2568
Eric Laurent59255e42011-07-27 19:49:51 -07002569 // check recording permission for visualizer
2570 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2571 !recordingAllowed()) {
2572 lStatus = PERMISSION_DENIED;
2573 goto Exit;
2574 }
2575
Mathias Agopian65ab4712010-07-14 17:59:35 -07002576 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002577 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002578 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2579 // if the output returned by getOutputForEffect() is removed before we lock the
2580 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2581 // and we will exit safely
2582 io = AudioSystem::getOutputForEffect(&desc);
2583 ALOGV("createEffect got output %d", io);
2584 }
2585
2586 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002587
2588 // If output is not specified try to find a matching audio session ID in one of the
2589 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002590 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2591 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002592 // Note: io is never 0 when creating an effect on an input
2593 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002594 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2595 // output must be specified by AudioPolicyManager when using session
2596 // AUDIO_SESSION_OUTPUT_STAGE
2597 lStatus = BAD_VALUE;
2598 goto Exit;
2599 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002600 // look for the thread where the specified audio session is present
2601 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2602 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2603 io = mPlaybackThreads.keyAt(i);
2604 break;
2605 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002606 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002607 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002608 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2609 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2610 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002611 break;
2612 }
2613 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002614 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002615 // If no output thread contains the requested session ID, default to
2616 // first output. The effect chain will be moved to the correct output
2617 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002618 if (io == 0 && mPlaybackThreads.size()) {
2619 io = mPlaybackThreads.keyAt(0);
2620 }
Steve Block3856b092011-10-20 11:56:00 +01002621 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002622 }
2623 ThreadBase *thread = checkRecordThread_l(io);
2624 if (thread == NULL) {
2625 thread = checkPlaybackThread_l(io);
2626 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002627 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002628 lStatus = BAD_VALUE;
2629 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002630 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002631 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002632
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002633 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002634
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002635 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002636 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2637 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002638 if (handle != 0 && id != NULL) {
2639 *id = handle->id();
2640 }
2641 }
2642
2643Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002644 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002645 *status = lStatus;
2646 }
2647 return handle;
2648}
2649
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002650status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2651 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002652{
Steve Block3856b092011-10-20 11:56:00 +01002653 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002654 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002655 Mutex::Autolock _l(mLock);
2656 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002657 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002658 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002659 }
Eric Laurentde070132010-07-13 04:45:46 -07002660 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2661 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002662 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002663 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002664 }
Eric Laurentde070132010-07-13 04:45:46 -07002665 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2666 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002667 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002668 return BAD_VALUE;
2669 }
2670
2671 Mutex::Autolock _dl(dstThread->mLock);
2672 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002673 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002674}
2675
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002676// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002677status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002678 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002679 AudioFlinger::PlaybackThread *dstThread,
2680 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002681{
Steve Block3856b092011-10-20 11:56:00 +01002682 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002683 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002684
Eric Laurent59255e42011-07-27 19:49:51 -07002685 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002686 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002687 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002688 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002689 return INVALID_OPERATION;
2690 }
2691
Eric Laurent39e94f82010-07-28 01:32:47 -07002692 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002693 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002694 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002695 // removed.
2696 srcThread->removeEffectChain_l(chain);
2697
2698 // transfer all effects one by one so that new effect chain is created on new thread with
2699 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002700 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002701 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002702 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002703 Vector< sp<EffectModule> > removed;
2704 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002705 while (effect != 0) {
2706 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002707 removed.add(effect);
2708 status = dstThread->addEffect_l(effect);
2709 if (status != NO_ERROR) {
2710 break;
2711 }
Eric Laurentec35a142011-10-05 17:42:25 -07002712 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2713 if (effect->state() == EffectModule::ACTIVE ||
2714 effect->state() == EffectModule::STOPPING) {
2715 effect->start();
2716 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002717 // if the move request is not received from audio policy manager, the effect must be
2718 // re-registered with the new strategy and output
2719 if (dstChain == 0) {
2720 dstChain = effect->chain().promote();
2721 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002722 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002723 status = NO_INIT;
2724 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002725 }
2726 strategy = dstChain->strategy();
2727 }
2728 if (reRegister) {
2729 AudioSystem::unregisterEffect(effect->id());
2730 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002731 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002732 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002733 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002734 effect->id());
2735 }
Eric Laurentde070132010-07-13 04:45:46 -07002736 effect = chain->getEffectFromId_l(0);
2737 }
2738
Eric Laurent5baf2af2013-09-12 17:37:00 -07002739 if (status != NO_ERROR) {
2740 for (size_t i = 0; i < removed.size(); i++) {
2741 srcThread->addEffect_l(removed[i]);
2742 if (dstChain != 0 && reRegister) {
2743 AudioSystem::unregisterEffect(removed[i]->id());
2744 AudioSystem::registerEffect(&removed[i]->desc(),
2745 srcThread->id(),
2746 strategy,
2747 sessionId,
2748 removed[i]->id());
2749 }
2750 }
2751 }
2752
2753 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002754}
2755
Eric Laurent5baf2af2013-09-12 17:37:00 -07002756bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002757{
2758 if (mGlobalEffectEnableTime != 0 &&
2759 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2760 return true;
2761 }
2762
2763 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2764 sp<EffectChain> ec =
2765 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002766 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002767 return true;
2768 }
2769 }
2770 return false;
2771}
2772
Eric Laurent5baf2af2013-09-12 17:37:00 -07002773void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002774{
2775 Mutex::Autolock _l(mLock);
2776
2777 mGlobalEffectEnableTime = systemTime();
2778
2779 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2780 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2781 if (t->mType == ThreadBase::OFFLOAD) {
2782 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2783 }
2784 }
2785
2786}
2787
Glenn Kastenda6ef132013-01-10 12:31:01 -08002788struct Entry {
2789#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2790 char mName[MAX_NAME];
2791};
2792
2793int comparEntry(const void *p1, const void *p2)
2794{
2795 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2796}
2797
Glenn Kasten46909e72013-02-26 09:20:22 -08002798#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002799void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002800{
Eric Laurent81784c32012-11-19 14:55:58 -08002801 NBAIO_Source *teeSource = source.get();
2802 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002803 // .wav rotation
2804 // There is a benign race condition if 2 threads call this simultaneously.
2805 // They would both traverse the directory, but the result would simply be
2806 // failures at unlink() which are ignored. It's also unlikely since
2807 // normally dumpsys is only done by bugreport or from the command line.
2808 char teePath[32+256];
2809 strcpy(teePath, "/data/misc/media");
2810 size_t teePathLen = strlen(teePath);
2811 DIR *dir = opendir(teePath);
2812 teePath[teePathLen++] = '/';
2813 if (dir != NULL) {
2814#define MAX_SORT 20 // number of entries to sort
2815#define MAX_KEEP 10 // number of entries to keep
2816 struct Entry entries[MAX_SORT];
2817 size_t entryCount = 0;
2818 while (entryCount < MAX_SORT) {
2819 struct dirent de;
2820 struct dirent *result = NULL;
2821 int rc = readdir_r(dir, &de, &result);
2822 if (rc != 0) {
2823 ALOGW("readdir_r failed %d", rc);
2824 break;
2825 }
2826 if (result == NULL) {
2827 break;
2828 }
2829 if (result != &de) {
2830 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2831 break;
2832 }
2833 // ignore non .wav file entries
2834 size_t nameLen = strlen(de.d_name);
2835 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2836 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2837 continue;
2838 }
2839 strcpy(entries[entryCount++].mName, de.d_name);
2840 }
2841 (void) closedir(dir);
2842 if (entryCount > MAX_KEEP) {
2843 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2844 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2845 strcpy(&teePath[teePathLen], entries[i].mName);
2846 (void) unlink(teePath);
2847 }
2848 }
2849 } else {
2850 if (fd >= 0) {
2851 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2852 }
2853 }
Eric Laurent81784c32012-11-19 14:55:58 -08002854 char teeTime[16];
2855 struct timeval tv;
2856 gettimeofday(&tv, NULL);
2857 struct tm tm;
2858 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002859 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2860 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2861 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2862 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002863 if (teeFd >= 0) {
2864 char wavHeader[44];
2865 memcpy(wavHeader,
2866 "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",
2867 sizeof(wavHeader));
2868 NBAIO_Format format = teeSource->format();
2869 unsigned channelCount = Format_channelCount(format);
2870 ALOG_ASSERT(channelCount <= FCC_2);
2871 uint32_t sampleRate = Format_sampleRate(format);
2872 wavHeader[22] = channelCount; // number of channels
2873 wavHeader[24] = sampleRate; // sample rate
2874 wavHeader[25] = sampleRate >> 8;
2875 wavHeader[32] = channelCount * 2; // block alignment
2876 write(teeFd, wavHeader, sizeof(wavHeader));
2877 size_t total = 0;
2878 bool firstRead = true;
2879 for (;;) {
2880#define TEE_SINK_READ 1024
2881 short buffer[TEE_SINK_READ * FCC_2];
2882 size_t count = TEE_SINK_READ;
2883 ssize_t actual = teeSource->read(buffer, count,
2884 AudioBufferProvider::kInvalidPTS);
2885 bool wasFirstRead = firstRead;
2886 firstRead = false;
2887 if (actual <= 0) {
2888 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2889 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002890 }
Eric Laurent81784c32012-11-19 14:55:58 -08002891 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002892 }
Eric Laurent81784c32012-11-19 14:55:58 -08002893 ALOG_ASSERT(actual <= (ssize_t)count);
2894 write(teeFd, buffer, actual * channelCount * sizeof(short));
2895 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002896 }
Eric Laurent81784c32012-11-19 14:55:58 -08002897 lseek(teeFd, (off_t) 4, SEEK_SET);
2898 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2899 write(teeFd, &temp, sizeof(temp));
2900 lseek(teeFd, (off_t) 40, SEEK_SET);
2901 temp = total * channelCount * sizeof(short);
2902 write(teeFd, &temp, sizeof(temp));
2903 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002904 if (fd >= 0) {
2905 fdprintf(fd, "tee copied to %s\n", teePath);
2906 }
Eric Laurent59255e42011-07-27 19:49:51 -07002907 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002908 if (fd >= 0) {
2909 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2910 }
Eric Laurent59255e42011-07-27 19:49:51 -07002911 }
2912 }
2913}
Glenn Kasten46909e72013-02-26 09:20:22 -08002914#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002915
Mathias Agopian65ab4712010-07-14 17:59:35 -07002916status_t AudioFlinger::onTransact(
2917 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2918{
2919 return BnAudioFlinger::onTransact(code, data, reply, flags);
2920}
2921
Mathias Agopian65ab4712010-07-14 17:59:35 -07002922}; // namespace android