blob: 7bc1c7d9755bfd2e6aadb72529ea30ccb78af946 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Alok Kediya601e9b52013-09-27 11:55:10 +05302** Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
3** Not a Contribution.
Mathias Agopian65ab4712010-07-14 17:59:35 -07004** Copyright 2007, The Android Open Source Project
5**
6** Licensed under the Apache License, Version 2.0 (the "License");
7** you may not use this file except in compliance with the License.
8** You may obtain a copy of the License at
9**
10** http://www.apache.org/licenses/LICENSE-2.0
11**
12** Unless required by applicable law or agreed to in writing, software
13** distributed under the License is distributed on an "AS IS" BASIS,
14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15** See the License for the specific language governing permissions and
16** limitations under the License.
17*/
18
19
20#define LOG_TAG "AudioFlinger"
21//#define LOG_NDEBUG 0
22
Glenn Kasten153b9fe2013-07-15 11:23:36 -070023#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080024#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <math.h>
26#include <signal.h>
27#include <sys/time.h>
28#include <sys/resource.h>
29
Gloria Wang9ee159b2011-02-24 14:51:45 -080030#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070031#include <binder/IServiceManager.h>
32#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070033#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/String16.h>
36#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070037#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
Dima Zavinfce7a472011-04-19 22:30:36 -070039#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070040#include <cutils/properties.h>
41
Dima Zavin64760242011-05-11 14:15:23 -070042#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070043#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
45#include "AudioMixer.h"
46#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080047#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
Mathias Agopian65ab4712010-07-14 17:59:35 -070049#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070050#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070051#include <audio_effects/effect_ns.h>
52#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Glenn Kasten3b21c502011-12-15 09:52:39 -080054#include <audio_utils/primitives.h>
55
Eric Laurentfeb0db62011-07-22 09:04:31 -070056#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080057
John Grossman4ff14ba2012-02-08 16:37:41 -080058#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070059
Glenn Kasten9e58b552013-01-18 15:09:48 -080060#include <media/IMediaLogService.h>
61
Glenn Kastenda6ef132013-01-10 12:31:01 -080062#include <media/nbaio/Pipe.h>
63#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070064#include <media/AudioParameter.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070065#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080066
Alok Kediya601e9b52013-09-27 11:55:10 +053067#ifdef SRS_PROCESSING
68#include "srs_processing.h"
69#include "postpro_patch_ics.h"
70#endif
71
Mathias Agopian65ab4712010-07-14 17:59:35 -070072// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070073
John Grossman1c345192012-03-27 14:00:17 -070074// Note: the following macro is used for extremely verbose logging message. In
75// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
76// 0; but one side effect of this is to turn all LOGV's as well. Some messages
77// are so verbose that we want to suppress them even when we have ALOG_ASSERT
78// turned on. Do not uncomment the #def below unless you really know what you
79// are doing and want to see all of the extremely verbose messages.
80//#define VERY_VERY_VERBOSE_LOGGING
81#ifdef VERY_VERY_VERBOSE_LOGGING
82#define ALOGVV ALOGV
83#else
84#define ALOGVV(a...) do { } while(0)
85#endif
Eric Laurentde070132010-07-13 04:45:46 -070086
Mathias Agopian65ab4712010-07-14 17:59:35 -070087namespace android {
88
Glenn Kastenec1d6b52011-12-12 09:04:45 -080089static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
90static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070091
Glenn Kasten58912562012-04-03 10:45:00 -070092
John Grossman4ff14ba2012-02-08 16:37:41 -080093nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080094
Eric Laurent81784c32012-11-19 14:55:58 -080095uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070096
Glenn Kasten46909e72013-02-26 09:20:22 -080097#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080098bool AudioFlinger::mTeeSinkInputEnabled = false;
99bool AudioFlinger::mTeeSinkOutputEnabled = false;
100bool AudioFlinger::mTeeSinkTrackEnabled = false;
101
102size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
103size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
104size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800105#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800106
Eric Laurent813e2a72013-08-31 12:59:48 -0700107// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
108// we define a minimum time during which a global effect is considered enabled.
109static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
110
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111// ----------------------------------------------------------------------------
112
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700113static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700114{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700115 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700116 int rc;
117
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700118 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
119 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
120 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
121 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700122 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700123 }
124 rc = audio_hw_device_open(mod, dev);
125 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
126 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
127 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700128 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700129 }
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +0000130#if !defined(ICS_AUDIO_BLOB) && !defined(MR0_AUDIO_BLOB)
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700131 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
132 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
133 rc = BAD_VALUE;
134 goto out;
135 }
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +0000136#endif
Dima Zavin799a70e2011-04-18 16:57:27 -0700137 return 0;
138
139out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700140 *dev = NULL;
141 return rc;
142}
143
Mathias Agopian65ab4712010-07-14 17:59:35 -0700144// ----------------------------------------------------------------------------
145
146AudioFlinger::AudioFlinger()
147 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800148 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700149 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800150 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800151 mMasterMute(false),
152 mNextUniqueId(1),
153 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700154 mBtNrecIsOff(false),
155 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700156 mIsDeviceTypeKnown(false),
Ethan Chen8d420ed2013-11-14 00:40:54 -0800157#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530158 mGlobalEffectEnableTime(0),
159 mAllChainsLocked(false)
Ethan Chen8d420ed2013-11-14 00:40:54 -0800160#else
161 mGlobalEffectEnableTime(0)
162#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163{
Glenn Kasten949a9262013-04-16 12:35:20 -0700164 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800165 char value[PROPERTY_VALUE_MAX];
166 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
167 if (doLog) {
168 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
169 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800170#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800171 (void) property_get("ro.debuggable", value, "0");
172 int debuggable = atoi(value);
173 int teeEnabled = 0;
174 if (debuggable) {
175 (void) property_get("af.tee", value, "0");
176 teeEnabled = atoi(value);
177 }
178 if (teeEnabled & 1)
179 mTeeSinkInputEnabled = true;
180 if (teeEnabled & 2)
181 mTeeSinkOutputEnabled = true;
182 if (teeEnabled & 4)
183 mTeeSinkTrackEnabled = true;
Glenn Kasten46909e72013-02-26 09:20:22 -0800184#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700185}
186
187void AudioFlinger::onFirstRef()
188{
Dima Zavin799a70e2011-04-18 16:57:27 -0700189 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700190
Eric Laurent93575202011-01-18 18:39:02 -0800191 Mutex::Autolock _l(mLock);
192
Dima Zavin799a70e2011-04-18 16:57:27 -0700193 /* TODO: move all this work into an Init() function */
Steve Kondik70d6eb92013-11-27 00:11:13 -0800194#ifdef QCOM_HARDWARE
Santhosh Kumar Thimmanna Bhattar03fd1d52013-10-30 11:29:19 +0530195 mLPASessionId = -2; // -2 is invalid session ID
196 mIsEffectConfigChanged = false;
197 mLPAEffectChain = NULL;
Steve Kondik70d6eb92013-11-27 00:11:13 -0800198#endif
John Grossman4ff14ba2012-02-08 16:37:41 -0800199 char val_str[PROPERTY_VALUE_MAX] = { 0 };
200 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
201 uint32_t int_val;
202 if (1 == sscanf(val_str, "%u", &int_val)) {
203 mStandbyTimeInNsecs = milliseconds(int_val);
204 ALOGI("Using %u mSec as standby time.", int_val);
205 } else {
206 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
207 ALOGI("Using default %u mSec as standby time.",
208 (uint32_t)(mStandbyTimeInNsecs / 1000000));
209 }
210 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211
Eric Laurenta4c5a552012-03-29 10:12:40 -0700212 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213}
214
215AudioFlinger::~AudioFlinger()
216{
217 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700218 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700219 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220 }
221 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700222 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700223 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700224 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700225
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800226 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
227 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700228 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
229 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230 }
231}
232
Eric Laurenta4c5a552012-03-29 10:12:40 -0700233static const char * const audio_interfaces[] = {
234 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
235 AUDIO_HARDWARE_MODULE_ID_A2DP,
236 AUDIO_HARDWARE_MODULE_ID_USB,
237};
238#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
239
John Grossmanee578c02012-07-23 17:05:46 -0700240AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
241 audio_module_handle_t module,
242 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700243{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700244 // if module is 0, the request comes from an old policy manager and we should load
245 // well known modules
246 if (module == 0) {
247 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
248 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
249 loadHwModule_l(audio_interfaces[i]);
250 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700251 // then try to find a module supporting the requested device.
252 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
253 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
254 audio_hw_device_t *dev = audioHwDevice->hwDevice();
255 if ((dev->get_supported_devices != NULL) &&
256 (dev->get_supported_devices(dev) & devices) == devices)
257 return audioHwDevice;
Ricardo Cerqueira2b3c7972013-01-10 23:53:09 +0000258#ifdef ICS_AUDIO_BLOB
259 else if (dev->get_supported_devices == NULL && i != 0 &&
260 devices == 0x80)
261 // Reasonably safe assumption: A non-primary HAL without
262 // get_supported_devices is a locally-built A2DP binary
263 return audioHwDevice;
264#endif
Eric Laurentf1c04f92012-08-28 14:26:53 -0700265 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700266 } else {
267 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700268 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
269 if (audioHwDevice != NULL) {
270 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700271 }
272 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700273
Dima Zavin799a70e2011-04-18 16:57:27 -0700274 return NULL;
275}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700277void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278{
279 const size_t SIZE = 256;
280 char buffer[SIZE];
281 String8 result;
282
283 result.append("Clients:\n");
284 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800285 sp<Client> client = mClients.valueAt(i).promote();
286 if (client != 0) {
287 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
288 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289 }
290 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700291
Eric Laurentd1b28d42013-09-18 18:47:13 -0700292 result.append("Notification Clients:\n");
293 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
294 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
295 result.append(buffer);
296 }
297
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700298 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800299 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700300 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
301 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800302 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700303 result.append(buffer);
304 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306}
307
308
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700309void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310{
311 const size_t SIZE = 256;
312 char buffer[SIZE];
313 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800314 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315
John Grossman4ff14ba2012-02-08 16:37:41 -0800316 snprintf(buffer, SIZE, "Hardware status: %d\n"
317 "Standby Time mSec: %u\n",
318 hardwareStatus,
319 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 result.append(buffer);
321 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322}
323
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700324void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325{
326 const size_t SIZE = 256;
327 char buffer[SIZE];
328 String8 result;
329 snprintf(buffer, SIZE, "Permission Denial: "
330 "can't dump AudioFlinger from pid=%d, uid=%d\n",
331 IPCThreadState::self()->getCallingPid(),
332 IPCThreadState::self()->getCallingUid());
333 result.append(buffer);
334 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335}
336
Eric Laurent81784c32012-11-19 14:55:58 -0800337bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338{
339 bool locked = false;
340 for (int i = 0; i < kDumpLockRetries; ++i) {
341 if (mutex.tryLock() == NO_ERROR) {
342 locked = true;
343 break;
344 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800345 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346 }
347 return locked;
348}
349
350status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
351{
Glenn Kasten44deb052012-02-05 18:09:08 -0800352 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 dumpPermissionDenial(fd, args);
354 } else {
355 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800356 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357 if (!hardwareLocked) {
358 String8 result(kHardwareLockedString);
359 write(fd, result.string(), result.size());
360 } else {
361 mHardwareLock.unlock();
362 }
363
Eric Laurent81784c32012-11-19 14:55:58 -0800364 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365
366 // failed to lock - AudioFlinger is probably deadlocked
367 if (!locked) {
368 String8 result(kDeadlockedString);
369 write(fd, result.string(), result.size());
370 }
371
372 dumpClients(fd, args);
373 dumpInternals(fd, args);
374
375 // dump playback threads
376 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
377 mPlaybackThreads.valueAt(i)->dump(fd, args);
378 }
379
380 // dump record threads
381 for (size_t i = 0; i < mRecordThreads.size(); i++) {
382 mRecordThreads.valueAt(i)->dump(fd, args);
383 }
384
Dima Zavin799a70e2011-04-18 16:57:27 -0700385 // dump all hardware devs
386 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700387 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700388 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700390
Glenn Kasten46909e72013-02-26 09:20:22 -0800391#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700392 // dump the serially shared record tee sink
393 if (mRecordTeeSource != 0) {
394 dumpTee(fd, mRecordTeeSource);
395 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800396#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700397
Glenn Kastend65d73c2012-06-22 17:21:07 -0700398 if (locked) {
399 mLock.unlock();
400 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800401
402 // append a copy of media.log here by forwarding fd to it, but don't attempt
403 // to lookup the service if it's not running, as it will block for a second
404 if (mLogMemoryDealer != 0) {
405 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
406 if (binder != 0) {
407 fdprintf(fd, "\nmedia.log:\n");
408 Vector<String16> args;
409 binder->dump(fd, args);
410 }
411 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 }
413 return NO_ERROR;
414}
415
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800416sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
417{
418 // If pid is already in the mClients wp<> map, then use that entry
419 // (for which promote() is always != 0), otherwise create a new entry and Client.
420 sp<Client> client = mClients.valueFor(pid).promote();
421 if (client == 0) {
422 client = new Client(this, pid);
423 mClients.add(pid, client);
424 }
425
426 return client;
427}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428
Glenn Kasten9e58b552013-01-18 15:09:48 -0800429sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
430{
431 if (mLogMemoryDealer == 0) {
432 return new NBLog::Writer();
433 }
434 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
435 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
436 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
437 if (binder != 0) {
438 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
439 }
440 return writer;
441}
442
443void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
444{
Glenn Kasten685ef092013-02-04 08:15:34 -0800445 if (writer == 0) {
446 return;
447 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800448 sp<IMemory> iMemory(writer->getIMemory());
449 if (iMemory == 0) {
450 return;
451 }
452 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
453 if (binder != 0) {
454 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
455 // Now the media.log remote reference to IMemory is gone.
456 // When our last local reference to IMemory also drops to zero,
457 // the IMemory destructor will deallocate the region from mMemoryDealer.
458 }
459}
460
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461// IAudioFlinger interface
462
463
464sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800465 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800467 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700468 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800469 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800470 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700471 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800472 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800473 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700474 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700475 String8& name,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476 status_t *status)
477{
478 sp<PlaybackThread::Track> track;
479 sp<TrackHandle> trackHandle;
480 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700481 status_t lStatus;
482 int lSessionId;
483
Glenn Kasten263709e2012-01-06 08:40:01 -0800484 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
485 // but if someone uses binder directly they could bypass that and cause us to crash
486 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000487 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700488 lStatus = BAD_VALUE;
489 goto Exit;
490 }
491
Glenn Kasten60a83922012-06-21 12:56:37 -0700492 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
493 // and we don't yet support 8.24 or 32-bit PCM
494 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
495 ALOGE("createTrack() invalid format %d", format);
496 lStatus = BAD_VALUE;
497 goto Exit;
498 }
499
Mathias Agopian65ab4712010-07-14 17:59:35 -0700500 {
501 Mutex::Autolock _l(mLock);
502 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700503 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700504 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800505 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700506 lStatus = BAD_VALUE;
507 goto Exit;
508 }
509
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800510 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800511 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512
Steve Block3856b092011-10-20 11:56:00 +0100513 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700514 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700515 // check if an effect chain with the same session ID is present on another
516 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700517 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700518 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
519 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700520 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700521 if (sessions & PlaybackThread::EFFECT_SESSION) {
522 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700523 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700524 }
Eric Laurentde070132010-07-13 04:45:46 -0700525 }
526 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700527 lSessionId = *sessionId;
528 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700529 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700530 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 if (sessionId != NULL) {
532 *sessionId = lSessionId;
533 }
534 }
Steve Block3856b092011-10-20 11:56:00 +0100535 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700536
537 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800538 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700539
540 // move effect chain to this output thread if an effect on same session was waiting
541 // for a track to be created
542 if (lStatus == NO_ERROR && effectThread != NULL) {
543 Mutex::Autolock _dl(thread->mLock);
544 Mutex::Autolock _sl(effectThread->mLock);
545 moveEffectChain_l(lSessionId, effectThread, thread, true);
546 }
Eric Laurenta011e352012-03-29 15:51:43 -0700547
548 // Look for sync events awaiting for a session to be used.
549 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
550 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
551 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700552 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700553 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700554 } else {
555 mPendingSyncEvents[i]->cancel();
556 }
Eric Laurenta011e352012-03-29 15:51:43 -0700557 mPendingSyncEvents.removeAt(i);
558 i--;
559 }
560 }
561 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562 }
563 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700564 // s for server's pid, n for normal mixer name, f for fast index
565 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
566 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567 trackHandle = new TrackHandle(track);
568 } else {
569 // remove local strong reference to Client before deleting the Track so that the Client
570 // destructor is called by the TrackBase destructor with mLock held
571 client.clear();
572 track.clear();
573 }
574
575Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700576 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577 *status = lStatus;
578 }
579 return trackHandle;
580}
581
Ethan Chen8d420ed2013-11-14 00:40:54 -0800582#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530583sp<IDirectTrack> AudioFlinger::createDirectTrack(
584 pid_t pid,
585 uint32_t sampleRate,
586 audio_channel_mask_t channelMask,
587 audio_io_handle_t output,
588 int *sessionId,
589 IDirectTrackClient *client,
590 audio_stream_type_t streamType,
591 status_t *status)
592{
593 *status = NO_ERROR;
594 status_t lStatus = NO_ERROR;
595 sp<IDirectTrack> track = NULL;
596 DirectAudioTrack* directTrack = NULL;
597 Mutex::Autolock _l(mLock);
598
599 ALOGV("createDirectTrack() sessionId: %d sampleRate %d channelMask %d",
600 *sessionId, sampleRate, channelMask);
601 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
602 if(desc == NULL) {
603 ALOGE("Error: Invalid output (%d) to create direct audio track", output);
604 lStatus = BAD_VALUE;
605 goto Exit;
606 }
607 desc->mStreamType = streamType;
608 if (desc->flag & AUDIO_OUTPUT_FLAG_LPA) {
609 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
610 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
611 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
612 // Check if the session ID is already associated with a track
613 uint32_t sessions = t->hasAudioSession(*sessionId);
614
615 // check if an effect with same session ID is waiting for a ssession to be created
616 ALOGV("check if an effect with same session ID is waiting for a ssession to be created");
617 if ((mLPAEffectChain == NULL) && (sessions & PlaybackThread::EFFECT_SESSION)) {
618 // Clear reference to previous effect chain if any
619 t->mLock.lock();
620 ALOGV("getting the LPA effect chain and setting LPA flag to true.");
621 mLPAEffectChain = t->getEffectChain_l(*sessionId);
622 t->mLock.unlock();
623 }
624 }
625 mLPASessionId = *sessionId;
626 if (mLPAEffectChain != NULL) {
627 mLPAEffectChain->setLPAFlag(true);
628 // For LPA, the volume will be applied in DSP. No need for volume
629 // control in the Effect chain, so setting it to unity.
630 uint32_t volume = 0x1000000; // Equals to 1.0 in 8.24 format
631 mLPAEffectChain->setVolume_l(&volume,&volume);
632 } else {
633 ALOGW("There was no effectChain created for the sessionId(%d)", mLPASessionId);
634 }
635 mLPASampleRate = sampleRate;
636 mLPANumChannels = popcount(channelMask);
637 } else {
638 if(sessionId != NULL) {
639 ALOGE("Error: Invalid sessionID (%d) for direct audio track", *sessionId);
640 }
641 }
642 }
643 mLock.unlock();
644 directTrack = new DirectAudioTrack(this, output, desc, client, desc->flag);
645 desc->trackRefPtr = dynamic_cast<void *>(directTrack);
646 mLock.lock();
647 if (directTrack != 0) {
648 track = dynamic_cast<IDirectTrack *>(directTrack);
649 AudioEventObserver* obv = dynamic_cast<AudioEventObserver *>(directTrack);
650 ALOGE("setting observer mOutputDesc track %p, obv %p", track.get(), obv);
651 desc->stream->set_observer(desc->stream, reinterpret_cast<void *>(obv));
652 } else {
653 lStatus = BAD_VALUE;
654 }
655Exit:
656 if(lStatus) {
657 if (track != NULL) {
658 track.clear();
659 }
660 *status = lStatus;
661 }
662 return track;
663}
664
665void AudioFlinger::deleteEffectSession()
666{
667 ALOGV("deleteSession");
668 // -2 is invalid session ID
669 mLPASessionId = -2;
670 if (mLPAEffectChain != NULL) {
671 mLPAEffectChain->lock();
672 mLPAEffectChain->setLPAFlag(false);
673 size_t i, numEffects = mLPAEffectChain->getNumEffects();
674 for(i = 0; i < numEffects; i++) {
675 sp<EffectModule> effect = mLPAEffectChain->getEffectFromIndex_l(i);
676 effect->setInBuffer(mLPAEffectChain->inBuffer());
677 if (i == numEffects-1) {
678 effect->setOutBuffer(mLPAEffectChain->outBuffer());
679 } else {
680 effect->setOutBuffer(mLPAEffectChain->inBuffer());
681 }
682 effect->configure();
683 }
684 mLPAEffectChain->unlock();
685 mLPAEffectChain.clear();
686 mLPAEffectChain = NULL;
687 }
688}
Ethan Chen8d420ed2013-11-14 00:40:54 -0800689#endif
Alok Kediya601e9b52013-09-27 11:55:10 +0530690
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800691uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692{
693 Mutex::Autolock _l(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800694#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530695 if (!mDirectAudioTracks.isEmpty()) {
696 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
697 if(desc != NULL) {
698 return desc->stream->common.get_sample_rate(&desc->stream->common);
699 }
700 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800701#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702 PlaybackThread *thread = checkPlaybackThread_l(output);
703 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000704 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705 return 0;
706 }
707 return thread->sampleRate();
708}
709
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800710int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711{
712 Mutex::Autolock _l(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800713#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530714 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
715 if(desc != NULL) {
716 return desc->stream->common.get_channels(&desc->stream->common);
717 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800718#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700719 PlaybackThread *thread = checkPlaybackThread_l(output);
720 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000721 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 return 0;
723 }
724 return thread->channelCount();
725}
726
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800727audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728{
729 Mutex::Autolock _l(mLock);
730 PlaybackThread *thread = checkPlaybackThread_l(output);
731 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000732 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800733 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734 }
735 return thread->format();
736}
737
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800738size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700739{
740 Mutex::Autolock _l(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800741#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530742 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
743 if(desc != NULL) {
744 return desc->stream->common.get_buffer_size(&desc->stream->common);
745 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800746#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747 PlaybackThread *thread = checkPlaybackThread_l(output);
748 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000749 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750 return 0;
751 }
Glenn Kasten58912562012-04-03 10:45:00 -0700752 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
753 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754 return thread->frameCount();
755}
756
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800757uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758{
759 Mutex::Autolock _l(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800760#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530761 if (!mDirectAudioTracks.isEmpty()) {
762 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
763 if (desc != NULL) {
764 return desc->stream->get_latency(desc->stream);
765 }
766 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800767#endif
Alok Kediya601e9b52013-09-27 11:55:10 +0530768
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769 PlaybackThread *thread = checkPlaybackThread_l(output);
770 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800771 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700772 return 0;
773 }
774 return thread->latency();
775}
776
777status_t AudioFlinger::setMasterVolume(float value)
778{
Eric Laurenta1884f92011-08-23 08:25:03 -0700779 status_t ret = initCheck();
780 if (ret != NO_ERROR) {
781 return ret;
782 }
783
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784 // check calling permissions
785 if (!settingsAllowed()) {
786 return PERMISSION_DENIED;
787 }
788
Eric Laurenta4c5a552012-03-29 10:12:40 -0700789 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700790 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700791
John Grossmanee578c02012-07-23 17:05:46 -0700792 // Set master volume in the HALs which support it.
793 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
794 AutoMutex lock(mHardwareLock);
795 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800796
John Grossmanee578c02012-07-23 17:05:46 -0700797 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
798 if (dev->canSetMasterVolume()) {
799 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800800 }
John Grossmanee578c02012-07-23 17:05:46 -0700801 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700803
John Grossmanee578c02012-07-23 17:05:46 -0700804 // Now set the master volume in each playback thread. Playback threads
805 // assigned to HALs which do not have master volume support will apply
806 // master volume during the mix operation. Threads with HALs which do
807 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800808 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700809 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700810
811 return NO_ERROR;
812}
813
Glenn Kastenf78aee72012-01-04 11:00:47 -0800814status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815{
Eric Laurenta1884f92011-08-23 08:25:03 -0700816 status_t ret = initCheck();
817 if (ret != NO_ERROR) {
818 return ret;
819 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820
821 // check calling permissions
822 if (!settingsAllowed()) {
823 return PERMISSION_DENIED;
824 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800825 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000826 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700827 return BAD_VALUE;
828 }
829
830 { // scope for the lock
831 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700832 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700834 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835 mHardwareStatus = AUDIO_HW_IDLE;
836 }
837
838 if (NO_ERROR == ret) {
839 Mutex::Autolock _l(mLock);
840 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800841 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700842 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843 }
844
845 return ret;
846}
847
848status_t AudioFlinger::setMicMute(bool state)
849{
Eric Laurenta1884f92011-08-23 08:25:03 -0700850 status_t ret = initCheck();
851 if (ret != NO_ERROR) {
852 return ret;
853 }
854
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 // check calling permissions
856 if (!settingsAllowed()) {
857 return PERMISSION_DENIED;
858 }
859
860 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700861 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700863 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700864 mHardwareStatus = AUDIO_HW_IDLE;
865 return ret;
866}
867
868bool AudioFlinger::getMicMute() const
869{
Eric Laurenta1884f92011-08-23 08:25:03 -0700870 status_t ret = initCheck();
871 if (ret != NO_ERROR) {
872 return false;
873 }
874
Dima Zavinfce7a472011-04-19 22:30:36 -0700875 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800876 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700877 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700879 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880 mHardwareStatus = AUDIO_HW_IDLE;
881 return state;
882}
883
884status_t AudioFlinger::setMasterMute(bool muted)
885{
John Grossmand8f178d2012-07-20 14:51:35 -0700886 status_t ret = initCheck();
887 if (ret != NO_ERROR) {
888 return ret;
889 }
890
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891 // check calling permissions
892 if (!settingsAllowed()) {
893 return PERMISSION_DENIED;
894 }
895
John Grossmanee578c02012-07-23 17:05:46 -0700896 Mutex::Autolock _l(mLock);
897 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700898
Emerson Pinter6d327d52013-07-28 19:34:16 -0300899#ifndef ICS_AUDIO_BLOB
John Grossmanee578c02012-07-23 17:05:46 -0700900 // Set master mute in the HALs which support it.
901 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
902 AutoMutex lock(mHardwareLock);
903 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700904
John Grossmanee578c02012-07-23 17:05:46 -0700905 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
906 if (dev->canSetMasterMute()) {
907 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700908 }
John Grossmanee578c02012-07-23 17:05:46 -0700909 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700910 }
911
John Grossmanee578c02012-07-23 17:05:46 -0700912 // Now set the master mute in each playback thread. Playback threads
913 // assigned to HALs which do not have master mute support will apply master
914 // mute during the mix operation. Threads with HALs which do support master
915 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800916 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700917 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +0000918#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919
920 return NO_ERROR;
921}
922
923float AudioFlinger::masterVolume() const
924{
Glenn Kasten98067102011-12-13 11:47:54 -0800925 Mutex::Autolock _l(mLock);
926 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927}
928
929bool AudioFlinger::masterMute() const
930{
Glenn Kasten98067102011-12-13 11:47:54 -0800931 Mutex::Autolock _l(mLock);
932 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933}
934
John Grossman4ff14ba2012-02-08 16:37:41 -0800935float AudioFlinger::masterVolume_l() const
936{
John Grossman4ff14ba2012-02-08 16:37:41 -0800937 return mMasterVolume;
938}
939
John Grossmand8f178d2012-07-20 14:51:35 -0700940bool AudioFlinger::masterMute_l() const
941{
John Grossmanee578c02012-07-23 17:05:46 -0700942 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700943}
944
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800945status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
946 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947{
948 // check calling permissions
949 if (!settingsAllowed()) {
950 return PERMISSION_DENIED;
951 }
952
Glenn Kasten263709e2012-01-06 08:40:01 -0800953 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000954 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 return BAD_VALUE;
956 }
957
958 AutoMutex lock(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800959#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530960 ALOGV("setStreamVolume stream %d, output %d, value %f",stream, output, value);
961 AudioSessionDescriptor *desc = NULL;
962 if (!mDirectAudioTracks.isEmpty()) {
963 desc = mDirectAudioTracks.valueFor(output);
964 if (desc != NULL) {
965 ALOGV("setStreamVolume for mAudioTracks size %d desc %p",mDirectAudioTracks.size(),desc);
966 if (desc->mStreamType == stream) {
967 mStreamTypes[stream].volume = value;
968 desc->mVolumeScale = value;
969 desc->stream->set_volume(desc->stream,
970 desc->mVolumeLeft * mStreamTypes[stream].volume,
971 desc->mVolumeRight* mStreamTypes[stream].volume);
972 return NO_ERROR;
973 }
974 }
975 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800976#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977 PlaybackThread *thread = NULL;
978 if (output) {
979 thread = checkPlaybackThread_l(output);
980 if (thread == NULL) {
Ethan Chen8d420ed2013-11-14 00:40:54 -0800981#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530982 if (desc != NULL) {
983 return NO_ERROR;
984 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800985#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700986 return BAD_VALUE;
987 }
988 }
989
990 mStreamTypes[stream].volume = value;
991
992 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800993 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700994 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995 }
996 } else {
997 thread->setStreamVolume(stream, value);
998 }
999
1000 return NO_ERROR;
1001}
1002
Glenn Kastenfff6d712012-01-12 16:38:12 -08001003status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004{
1005 // check calling permissions
1006 if (!settingsAllowed()) {
1007 return PERMISSION_DENIED;
1008 }
1009
Glenn Kasten263709e2012-01-06 08:40:01 -08001010 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -07001011 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001012 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001013 return BAD_VALUE;
1014 }
1015
Eric Laurent93575202011-01-18 18:39:02 -08001016 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001017 mStreamTypes[stream].mute = muted;
1018 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001019 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001020
1021 return NO_ERROR;
1022}
1023
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001024float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001025{
Glenn Kasten263709e2012-01-06 08:40:01 -08001026 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001027 return 0.0f;
1028 }
1029
1030 AutoMutex lock(mLock);
1031 float volume;
1032 if (output) {
1033 PlaybackThread *thread = checkPlaybackThread_l(output);
1034 if (thread == NULL) {
1035 return 0.0f;
1036 }
1037 volume = thread->streamVolume(stream);
1038 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001039 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040 }
1041
1042 return volume;
1043}
1044
Glenn Kastenfff6d712012-01-12 16:38:12 -08001045bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046{
Glenn Kasten263709e2012-01-06 08:40:01 -08001047 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048 return true;
1049 }
1050
Glenn Kasten6637baa2012-01-09 09:40:36 -08001051 AutoMutex lock(mLock);
1052 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001053}
1054
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001055status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001056{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001057 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1058 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001059
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060 // check calling permissions
1061 if (!settingsAllowed()) {
1062 return PERMISSION_DENIED;
1063 }
1064
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065 // ioHandle == 0 means the parameters are global to the audio hardware interface
1066 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001067 Mutex::Autolock _l(mLock);
Alok Kediya601e9b52013-09-27 11:55:10 +05301068#ifdef SRS_PROCESSING
1069 POSTPRO_PATCH_ICS_PARAMS_SET(keyValuePairs);
1070 if (!mDirectAudioTracks.isEmpty())
1071 audioConfigChanged_l(AudioSystem::EFFECT_CONFIG_CHANGED, 0, NULL);
1072#endif
Dima Zavin799a70e2011-04-18 16:57:27 -07001073 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001074 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001075 AutoMutex lock(mHardwareLock);
1076 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1077 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1078 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1079 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1080 final_result = result ?: final_result;
1081 }
1082 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001083 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001084 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1085 AudioParameter param = AudioParameter(keyValuePairs);
1086 String8 value;
1087 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001088 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1089 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001090 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1091 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001092 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001093 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1094 // collect all of the thread's session IDs
1095 KeyedVector<int, bool> ids = thread->sessionIds();
1096 // suspend effects associated with those session IDs
1097 for (size_t j = 0; j < ids.size(); ++j) {
1098 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001099 thread->setEffectSuspended(FX_IID_AEC,
1100 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001101 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001102 thread->setEffectSuspended(FX_IID_NS,
1103 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001104 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001105 }
1106 }
Eric Laurentbee53372011-08-29 12:42:48 -07001107 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001108 }
1109 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001110 String8 screenState;
1111 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1112 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001113 if (isOff != (AudioFlinger::mScreenState & 1)) {
1114 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001115 }
1116 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001117 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001118 }
1119
Ethan Chen8d420ed2013-11-14 00:40:54 -08001120#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301121 AudioSessionDescriptor *desc = NULL;
1122 if (!mDirectAudioTracks.isEmpty()) {
1123 desc = mDirectAudioTracks.valueFor(ioHandle);
1124 if (desc != NULL) {
1125 ALOGV("setParameters for mAudioTracks size %d desc %p",mDirectAudioTracks.size(),desc);
1126 desc->stream->common.set_parameters(&desc->stream->common, keyValuePairs.string());
1127 AudioParameter param = AudioParameter(keyValuePairs);
1128 String8 key = String8(AudioParameter::keyRouting);
1129 int device;
1130 if (param.getInt(key, device) == NO_ERROR) {
1131 mDirectDevice = device;
1132#ifdef SRS_PROCESSING
1133 ALOGV("setParameters:: routing change to device %d", device);
1134 POSTPRO_PATCH_ICS_OUTPROC_MIX_ROUTE(desc->trackRefPtr, param, device);
Santhosh Kumar Thimmanna Bhattar03fd1d52013-10-30 11:29:19 +05301135 if(desc->flag & AUDIO_OUTPUT_FLAG_TUNNEL)
1136 audioConfigChanged_l(AudioSystem::EFFECT_CONFIG_CHANGED, 0, NULL);
Alok Kediya601e9b52013-09-27 11:55:10 +05301137#endif
1138 if(mLPAEffectChain != NULL){
1139 mLPAEffectChain->setDevice_l(device);
1140 audioConfigChanged_l(AudioSystem::EFFECT_CONFIG_CHANGED, 0, NULL);
1141 }
1142 }
1143 }
1144 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001145#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301146
Mathias Agopian65ab4712010-07-14 17:59:35 -07001147 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1148 // and the thread is exited once the lock is released
1149 sp<ThreadBase> thread;
1150 {
1151 Mutex::Autolock _l(mLock);
1152 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001153 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001155 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001156 // indicate output device change to all input threads for pre processing
1157 AudioParameter param = AudioParameter(keyValuePairs);
1158 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001159 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1160 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001161 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1162 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1163 }
1164 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001165 }
1166 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001167 if (thread != 0) {
1168 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001169 }
1170 return BAD_VALUE;
1171}
1172
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001173String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001174{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001175 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1176 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001177
Eric Laurenta4c5a552012-03-29 10:12:40 -07001178 Mutex::Autolock _l(mLock);
1179
Mathias Agopian65ab4712010-07-14 17:59:35 -07001180 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001181 String8 out_s8;
1182
Alok Kediya601e9b52013-09-27 11:55:10 +05301183#ifdef SRS_PROCESSING
1184 POSTPRO_PATCH_ICS_PARAMS_GET(keys, out_s8);
1185#endif
Dima Zavin799a70e2011-04-18 16:57:27 -07001186 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001187 char *s;
1188 {
1189 AutoMutex lock(mHardwareLock);
1190 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001191 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001192 s = dev->get_parameters(dev, keys.string());
1193 mHardwareStatus = AUDIO_HW_IDLE;
1194 }
John Grossmanef7740b2012-02-09 11:28:36 -08001195 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001196 free(s);
1197 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001198 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199 }
1200
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1202 if (playbackThread != NULL) {
1203 return playbackThread->getParameters(keys);
1204 }
1205 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1206 if (recordThread != NULL) {
1207 return recordThread->getParameters(keys);
1208 }
1209 return String8("");
1210}
1211
Glenn Kastendd8104c2012-07-02 12:42:44 -07001212size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1213 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001214{
Eric Laurenta1884f92011-08-23 08:25:03 -07001215 status_t ret = initCheck();
1216 if (ret != NO_ERROR) {
1217 return 0;
1218 }
1219
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001220 AutoMutex lock(mHardwareLock);
1221 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001222 struct audio_config config;
1223 memset(&config, 0, sizeof(config));
1224 config.sample_rate = sampleRate;
1225 config.channel_mask = channelMask;
1226 config.format = format;
1227
John Grossmanee578c02012-07-23 17:05:46 -07001228 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001229#ifndef ICS_AUDIO_BLOB
John Grossmanee578c02012-07-23 17:05:46 -07001230 size_t size = dev->get_input_buffer_size(dev, &config);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001231#else
1232 size_t size = dev->get_input_buffer_size(dev, sampleRate, format, popcount(channelMask));
1233#endif
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001234 mHardwareStatus = AUDIO_HW_IDLE;
1235 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236}
1237
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001238unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001239{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001240 Mutex::Autolock _l(mLock);
1241
1242 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1243 if (recordThread != NULL) {
1244 return recordThread->getInputFramesLost();
1245 }
1246 return 0;
1247}
1248
1249status_t AudioFlinger::setVoiceVolume(float value)
1250{
Eric Laurenta1884f92011-08-23 08:25:03 -07001251 status_t ret = initCheck();
1252 if (ret != NO_ERROR) {
1253 return ret;
1254 }
1255
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256 // check calling permissions
1257 if (!settingsAllowed()) {
1258 return PERMISSION_DENIED;
1259 }
1260
1261 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001262 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001263 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001264 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265 mHardwareStatus = AUDIO_HW_IDLE;
1266
1267 return ret;
1268}
1269
Glenn Kasten26c77552012-11-16 12:01:44 -08001270status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001271 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272{
1273 status_t status;
1274
1275 Mutex::Autolock _l(mLock);
1276
1277 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1278 if (playbackThread != NULL) {
1279 return playbackThread->getRenderPosition(halFrames, dspFrames);
1280 }
1281
1282 return BAD_VALUE;
1283}
1284
1285void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1286{
1287
1288 Mutex::Autolock _l(mLock);
1289
Glenn Kastenbb001922012-02-03 11:10:26 -08001290 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001291 if (mNotificationClients.indexOfKey(pid) < 0) {
1292 sp<NotificationClient> notificationClient = new NotificationClient(this,
1293 client,
1294 pid);
Steve Block3856b092011-10-20 11:56:00 +01001295 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001296
1297 mNotificationClients.add(pid, notificationClient);
1298
1299 sp<IBinder> binder = client->asBinder();
1300 binder->linkToDeath(notificationClient);
1301
1302 // the config change is always sent from playback or record threads to avoid deadlock
1303 // with AudioSystem::gLock
1304 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001305 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001306 }
1307
1308 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001309 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310 }
1311 }
1312}
1313
Ethan Chen8d420ed2013-11-14 00:40:54 -08001314#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301315status_t AudioFlinger::deregisterClient(const sp<IAudioFlingerClient>& client)
1316{
1317 ALOGV("deregisterClient() %p, tid %d, calling tid %d", client.get(), gettid(), IPCThreadState::self()->getCallingPid());
1318 Mutex::Autolock _l(mLock);
1319
1320 pid_t pid = IPCThreadState::self()->getCallingPid();
1321 int index = mNotificationClients.indexOfKey(pid);
1322 if (index >= 0) {
1323 mNotificationClients.removeItemsAt(index);
1324 return true;
1325 }
1326
1327 return false;
1328}
Ethan Chen8d420ed2013-11-14 00:40:54 -08001329#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301330
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331void AudioFlinger::removeNotificationClient(pid_t pid)
1332{
1333 Mutex::Autolock _l(mLock);
1334
Glenn Kastena3b09252012-01-20 09:19:01 -08001335 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001336
Steve Block3856b092011-10-20 11:56:00 +01001337 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001338 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001339 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001340 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001341 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001342 ALOGV(" pid %d @ %d", ref->mPid, i);
1343 if (ref->mPid == pid) {
1344 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001345 mAudioSessionRefs.removeAt(i);
1346 delete ref;
1347 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001348 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001349 } else {
1350 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001351 }
1352 }
1353 if (removed) {
1354 purgeStaleEffects_l();
1355 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001356}
1357
1358// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001359void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360{
Ethan Chen8d420ed2013-11-14 00:40:54 -08001361#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301362 ALOGV("AudioFlinger::audioConfigChanged_l: event %d", event);
1363 if (event == AudioSystem::EFFECT_CONFIG_CHANGED) {
1364 mIsEffectConfigChanged = true;
1365 }
1366 if (!mNotificationClients.isEmpty()){
Ethan Chen8d420ed2013-11-14 00:40:54 -08001367#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301368 size_t size = mNotificationClients.size();
1369 for (size_t i = 0; i < size; i++) {
1370 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1371 param2);
1372 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001373#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301374 }
1375 if ((!mDirectAudioTracks.isEmpty())&& (event == AudioSystem::EFFECT_CONFIG_CHANGED)){
1376 size_t dsize = mDirectAudioTracks.size();
1377 for(size_t i = 0; i < dsize; i++) {
1378 AudioSessionDescriptor *desc = mDirectAudioTracks.valueAt(i);
1379 if(desc && ((DirectAudioTrack*)desc->trackRefPtr)) {
1380 ALOGV("signalling directAudioTrack ");
1381 ((DirectAudioTrack*)desc->trackRefPtr)->signalEffect();
1382 } else{
1383 ALOGV("not found track to signal directAudioTrack ");
1384 }
1385 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001387#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388}
1389
1390// removeClient_l() must be called with AudioFlinger::mLock held
1391void AudioFlinger::removeClient_l(pid_t pid)
1392{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001393 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001394 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001395 mClients.removeItem(pid);
1396}
1397
Eric Laurent717e1282012-06-29 16:36:52 -07001398// getEffectThread_l() must be called with AudioFlinger::mLock held
1399sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1400{
1401 sp<PlaybackThread> thread;
1402
1403 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1404 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1405 ALOG_ASSERT(thread == 0);
1406 thread = mPlaybackThreads.valueAt(i);
1407 }
1408 }
1409
1410 return thread;
1411}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414
1415// ----------------------------------------------------------------------------
1416
1417AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1418 : RefBase(),
1419 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001420 // 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 -07001421 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001422 mPid(pid),
1423 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424{
1425 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1426}
1427
1428// Client destructor must be called with AudioFlinger::mLock held
1429AudioFlinger::Client::~Client()
1430{
1431 mAudioFlinger->removeClient_l(mPid);
1432}
1433
Glenn Kasten435dbe62012-01-30 10:15:48 -08001434sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435{
1436 return mMemoryDealer;
1437}
1438
John Grossman4ff14ba2012-02-08 16:37:41 -08001439// Reserve one of the limited slots for a timed audio track associated
1440// with this client
1441bool AudioFlinger::Client::reserveTimedTrack()
1442{
1443 const int kMaxTimedTracksPerClient = 4;
1444
1445 Mutex::Autolock _l(mTimedTrackLock);
1446
1447 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1448 ALOGW("can not create timed track - pid %d has exceeded the limit",
1449 mPid);
1450 return false;
1451 }
1452
1453 mTimedTrackCount++;
1454 return true;
1455}
1456
1457// Release a slot for a timed audio track
1458void AudioFlinger::Client::releaseTimedTrack()
1459{
1460 Mutex::Autolock _l(mTimedTrackLock);
1461 mTimedTrackCount--;
1462}
1463
Mathias Agopian65ab4712010-07-14 17:59:35 -07001464// ----------------------------------------------------------------------------
1465
1466AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1467 const sp<IAudioFlingerClient>& client,
1468 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001469 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001470{
1471}
1472
1473AudioFlinger::NotificationClient::~NotificationClient()
1474{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001475}
1476
1477void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1478{
1479 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001480 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001481}
1482
Mathias Agopian65ab4712010-07-14 17:59:35 -07001483
1484// ----------------------------------------------------------------------------
1485
Jeff Brown893a5642013-08-16 20:19:26 -07001486static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1487 return audio_is_remote_submix_device(inDevice);
1488}
1489
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001491 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001493 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001494 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001495 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001496 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001497 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498 int *sessionId,
1499 status_t *status)
1500{
1501 sp<RecordThread::RecordTrack> recordTrack;
1502 sp<RecordHandle> recordHandle;
1503 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001504 status_t lStatus;
1505 RecordThread *thread;
1506 size_t inFrameCount;
1507 int lSessionId;
Ethan Chen8d420ed2013-11-14 00:40:54 -08001508#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301509 size_t inputBufferSize = 0;
1510 uint32_t channelCount = popcount(channelMask);
Ethan Chen8d420ed2013-11-14 00:40:54 -08001511#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512
1513 // check calling permissions
1514 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001515 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516 lStatus = PERMISSION_DENIED;
1517 goto Exit;
1518 }
1519
Ethan Chen8d420ed2013-11-14 00:40:54 -08001520#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301521 // Check that audio input stream accepts requested audio parameters
1522 inputBufferSize = getInputBufferSize(sampleRate, format, channelCount);
1523 if (inputBufferSize == 0) {
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001524 lStatus = BAD_VALUE;
Alok Kediya601e9b52013-09-27 11:55:10 +05301525 ALOGE("Bad audio input parameters: sampling rate %u, format %d, channels %d", sampleRate, format, channelCount);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001526 goto Exit;
1527 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001528#else
1529 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1530 ALOGE("openRecord() invalid format %d", format);
1531 lStatus = BAD_VALUE;
1532 goto Exit;
1533 }
1534#endif
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001535
Mathias Agopian65ab4712010-07-14 17:59:35 -07001536 // add client to list
1537 { // scope for mLock
1538 Mutex::Autolock _l(mLock);
1539 thread = checkRecordThread_l(input);
1540 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001541 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542 lStatus = BAD_VALUE;
1543 goto Exit;
1544 }
1545
Jeff Brown893a5642013-08-16 20:19:26 -07001546 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1547 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001548 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001549 lStatus = PERMISSION_DENIED;
1550 goto Exit;
1551 }
1552
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001553 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001554 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001555
1556 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001557 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001558 lSessionId = *sessionId;
1559 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001560 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001561 if (sessionId != NULL) {
1562 *sessionId = lSessionId;
1563 }
1564 }
Alok Kediya601e9b52013-09-27 11:55:10 +05301565
Ethan Chen8d420ed2013-11-14 00:40:54 -08001566#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301567 // frameCount must be a multiple of input buffer size
1568 // Change for Codec type
1569 uint8_t channelCount = popcount(channelMask);
1570 if (*flags & IAudioFlinger::TRACK_VOICE_COMMUNICATION) {
1571 inFrameCount = inputBufferSize/channelCount/sizeof(short);
1572 } else {
1573 if ((format == AUDIO_FORMAT_PCM_16_BIT) ||
1574 (format == AUDIO_FORMAT_PCM_8_BIT)) {
1575 inFrameCount = inputBufferSize/channelCount/sizeof(short);
1576 } else if (format == AUDIO_FORMAT_AMR_NB) {
1577 inFrameCount = inputBufferSize/channelCount/AMR_FRAMESIZE;
1578 } else if (format == AUDIO_FORMAT_EVRC) {
1579 inFrameCount = inputBufferSize/channelCount/EVRC_FRAMESIZE;
1580 } else if (format == AUDIO_FORMAT_QCELP) {
1581 inFrameCount = inputBufferSize/channelCount/QCELP_FRAMESIZE;
1582 } else if (format == AUDIO_FORMAT_AAC) {
1583 inFrameCount = inputBufferSize/AAC_FRAMESIZE;
1584 } else if (format == AUDIO_FORMAT_AMR_WB) {
1585 inFrameCount = inputBufferSize/channelCount/AMR_WB_FRAMESIZE;
1586 }
1587 }
1588 frameCount = ((frameCount - 1)/inFrameCount + 1) * inFrameCount;
Ethan Chen8d420ed2013-11-14 00:40:54 -08001589#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301590
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001591 // create new record track.
1592 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001593 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kastenddb0ccf2013-07-31 16:14:50 -07001594 frameCount, lSessionId, flags, tid, &lStatus);
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001595 LOG_ALWAYS_FATAL_IF((recordTrack != 0) != (lStatus == NO_ERROR));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001596 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001597 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001598 // remove local strong reference to Client before deleting the RecordTrack so that the
1599 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600 client.clear();
1601 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602 goto Exit;
1603 }
1604
1605 // return to handle to client
1606 recordHandle = new RecordHandle(recordTrack);
1607 lStatus = NO_ERROR;
1608
1609Exit:
1610 if (status) {
1611 *status = lStatus;
1612 }
1613 return recordHandle;
1614}
1615
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001616
1617
Mathias Agopian65ab4712010-07-14 17:59:35 -07001618// ----------------------------------------------------------------------------
1619
Eric Laurenta4c5a552012-03-29 10:12:40 -07001620audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1621{
1622 if (!settingsAllowed()) {
1623 return 0;
1624 }
1625 Mutex::Autolock _l(mLock);
1626 return loadHwModule_l(name);
1627}
1628
1629// loadHwModule_l() must be called with AudioFlinger::mLock held
1630audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1631{
1632 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1633 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1634 ALOGW("loadHwModule() module %s already loaded", name);
1635 return mAudioHwDevs.keyAt(i);
1636 }
1637 }
1638
Eric Laurenta4c5a552012-03-29 10:12:40 -07001639 audio_hw_device_t *dev;
1640
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001641 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001642 if (rc) {
1643 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1644 return 0;
1645 }
1646
1647 mHardwareStatus = AUDIO_HW_INIT;
1648 rc = dev->init_check(dev);
1649 mHardwareStatus = AUDIO_HW_IDLE;
1650 if (rc) {
1651 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1652 return 0;
1653 }
1654
John Grossmanee578c02012-07-23 17:05:46 -07001655 // Check and cache this HAL's level of support for master mute and master
1656 // volume. If this is the first HAL opened, and it supports the get
1657 // methods, use the initial values provided by the HAL as the current
1658 // master mute and volume settings.
1659
1660 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1661 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001662 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001663
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001664#if !defined(ICS_AUDIO_BLOB) && !defined(MR0_AUDIO_BLOB)
John Grossmanee578c02012-07-23 17:05:46 -07001665 if (0 == mAudioHwDevs.size()) {
1666 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1667 if (NULL != dev->get_master_volume) {
1668 float mv;
1669 if (OK == dev->get_master_volume(dev, &mv)) {
1670 mMasterVolume = mv;
1671 }
1672 }
1673
1674 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1675 if (NULL != dev->get_master_mute) {
1676 bool mm;
1677 if (OK == dev->get_master_mute(dev, &mm)) {
1678 mMasterMute = mm;
1679 }
1680 }
1681 }
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001682#endif
John Grossmanee578c02012-07-23 17:05:46 -07001683
Eric Laurenta4c5a552012-03-29 10:12:40 -07001684 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001685 if ((NULL != dev->set_master_volume) &&
1686 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1687 flags = static_cast<AudioHwDevice::Flags>(flags |
1688 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1689 }
1690
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001691#if !defined(ICS_AUDIO_BLOB) && !defined(MR0_AUDIO_BLOB)
John Grossmanee578c02012-07-23 17:05:46 -07001692 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1693 if ((NULL != dev->set_master_mute) &&
1694 (OK == dev->set_master_mute(dev, mMasterMute))) {
1695 flags = static_cast<AudioHwDevice::Flags>(flags |
1696 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1697 }
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001698#endif
John Grossmanee578c02012-07-23 17:05:46 -07001699
Eric Laurenta4c5a552012-03-29 10:12:40 -07001700 mHardwareStatus = AUDIO_HW_IDLE;
1701 }
1702
1703 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001704 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001705
1706 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001707 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001708
1709 return handle;
1710
1711}
1712
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001713// ----------------------------------------------------------------------------
1714
Glenn Kasten3b16c762012-11-14 08:44:39 -08001715uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001716{
1717 Mutex::Autolock _l(mLock);
1718 PlaybackThread *thread = primaryPlaybackThread_l();
1719 return thread != NULL ? thread->sampleRate() : 0;
1720}
1721
Glenn Kastene33054e2012-11-14 12:54:39 -08001722size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001723{
1724 Mutex::Autolock _l(mLock);
1725 PlaybackThread *thread = primaryPlaybackThread_l();
1726 return thread != NULL ? thread->frameCountHAL() : 0;
1727}
1728
1729// ----------------------------------------------------------------------------
1730
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001731status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1732{
1733 uid_t uid = IPCThreadState::self()->getCallingUid();
1734 if (uid != AID_SYSTEM) {
1735 return PERMISSION_DENIED;
1736 }
1737 Mutex::Autolock _l(mLock);
1738 if (mIsDeviceTypeKnown) {
1739 return INVALID_OPERATION;
1740 }
1741 mIsLowRamDevice = isLowRamDevice;
1742 mIsDeviceTypeKnown = true;
1743 return NO_ERROR;
1744}
1745
1746// ----------------------------------------------------------------------------
1747
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001748#ifdef HAVE_PRE_KITKAT_AUDIO_BLOB
1749audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1750 audio_devices_t *pDevices,
1751 uint32_t *pSamplingRate,
1752 audio_format_t *pFormat,
1753 audio_channel_mask_t *pChannelMask,
1754 uint32_t *pLatencyMs,
1755 audio_output_flags_t flags)
1756{
1757 return openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
1758 pLatencyMs, flags, NULL);
1759}
1760#endif
1761
Eric Laurenta4c5a552012-03-29 10:12:40 -07001762audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1763 audio_devices_t *pDevices,
1764 uint32_t *pSamplingRate,
1765 audio_format_t *pFormat,
1766 audio_channel_mask_t *pChannelMask,
1767 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001768 audio_output_flags_t flags,
1769 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001770{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001771 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001772 struct audio_config config;
1773 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1774 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1775 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001776#ifndef HAVE_PRE_KITKAT_AUDIO_BLOB
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001777 if (offloadInfo) {
1778 config.offload_info = *offloadInfo;
1779 }
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001780#endif
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001781
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001782 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001783 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001784
Eric Laurentbfb1b832013-01-07 09:53:42 -08001785 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001786 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001787 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001788 config.sample_rate,
1789 config.format,
1790 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001791 flags);
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001792#ifndef HAVE_PRE_KITKAT_AUDIO_BLOB
Eric Laurentbfb1b832013-01-07 09:53:42 -08001793 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1794 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version );
Jorge Ruesgad09a3762013-11-09 15:34:50 +01001795#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001796
1797 if (pDevices == NULL || *pDevices == 0) {
1798 return 0;
1799 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001800
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801 Mutex::Autolock _l(mLock);
1802
Eric Laurenta4c5a552012-03-29 10:12:40 -07001803 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001804 if (outHwDev == NULL)
1805 return 0;
1806
John Grossmanee578c02012-07-23 17:05:46 -07001807 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001808 audio_io_handle_t id = nextUniqueId();
1809
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001810 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001811
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001812 status_t status;
1813#ifndef ICS_AUDIO_BLOB
1814 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001815 id,
1816 *pDevices,
1817 (audio_output_flags_t)flags,
1818 &config,
1819 &outStream);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00001820#else
1821 status = hwDevHal->open_output_stream(hwDevHal,
1822 *pDevices,
1823 (int *)&config.format,
1824 &config.channel_mask,
1825 &config.sample_rate,
1826 &outStream);
1827 uint32_t newflags = flags | AUDIO_OUTPUT_FLAG_PRIMARY;
1828 flags = (audio_output_flags_t)newflags;
1829#endif
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001830
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001831 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001832 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001833 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001834 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001835 config.sample_rate,
1836 config.format,
1837 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001838 status);
1839
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001840 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001841 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001842 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1843 thread = new OffloadThread(this, output, id, *pDevices);
1844 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
Ethan Chen8d420ed2013-11-14 00:40:54 -08001845#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301846 }
1847 if (flags & AUDIO_OUTPUT_FLAG_LPA || flags & AUDIO_OUTPUT_FLAG_TUNNEL ) {
1848 AudioSessionDescriptor *desc = new AudioSessionDescriptor(hwDevHal, outStream, flags);
1849 desc->mActive = true;
1850 //TODO: no stream type
1851 //desc->mStreamType = streamType;
1852 desc->mVolumeLeft = 1.0;
1853 desc->mVolumeRight = 1.0;
1854 desc->device = *pDevices;
1855 desc->trackRefPtr = NULL;
1856 mDirectAudioTracks.add(id, desc);
1857 mDirectDevice = desc->device;
Ethan Chen8d420ed2013-11-14 00:40:54 -08001858#endif
Eric Laurentbfb1b832013-01-07 09:53:42 -08001859 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001860 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1861 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001862 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001863 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001864 } else {
1865 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001866 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001867 }
Alok Kediya601e9b52013-09-27 11:55:10 +05301868 if (thread != NULL) {
1869 mPlaybackThreads.add(id, thread);
1870 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001871
Glenn Kasten7c027242012-12-26 14:43:16 -08001872 if (pSamplingRate != NULL) {
1873 *pSamplingRate = config.sample_rate;
1874 }
1875 if (pFormat != NULL) {
1876 *pFormat = config.format;
1877 }
1878 if (pChannelMask != NULL) {
1879 *pChannelMask = config.channel_mask;
1880 }
Alok Kediya601e9b52013-09-27 11:55:10 +05301881 if (thread != NULL) {
1882 if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
1883 // notify client processes of the new output creation
1884 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1885 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001886#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301887 else {
1888 *pLatencyMs = 0;
1889 if ((flags & AUDIO_OUTPUT_FLAG_LPA) || (flags & AUDIO_OUTPUT_FLAG_TUNNEL)) {
1890 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(id);
1891 *pLatencyMs = desc->stream->get_latency(desc->stream);
1892 }
Glenn Kasten7c027242012-12-26 14:43:16 -08001893 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001894#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001895
Eric Laurenta4c5a552012-03-29 10:12:40 -07001896
1897 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001898 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001899 ALOGI("Using module %d has the primary audio interface", module);
1900 mPrimaryHardwareDev = outHwDev;
Alok Kediya601e9b52013-09-27 11:55:10 +05301901#ifdef SRS_PROCESSING
1902 SRS_Processing::RawDataSet(NULL, "qdsp hook", &mPrimaryHardwareDev,
1903 sizeof(&mPrimaryHardwareDev));
1904#endif
Eric Laurenta4c5a552012-03-29 10:12:40 -07001905 AutoMutex lock(mHardwareLock);
1906 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001907 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001908 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001909 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001910 return id;
1911 }
1912
1913 return 0;
1914}
1915
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001916audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1917 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001918{
1919 Mutex::Autolock _l(mLock);
1920 MixerThread *thread1 = checkMixerThread_l(output1);
1921 MixerThread *thread2 = checkMixerThread_l(output2);
1922
1923 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001924 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1925 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001926 return 0;
1927 }
1928
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001929 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001930 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1931 thread->addOutputTrack(thread2);
1932 mPlaybackThreads.add(id, thread);
1933 // notify client processes of the new output creation
1934 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1935 return id;
1936}
1937
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001938status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939{
Glenn Kastend96c5722012-04-25 13:44:49 -07001940 return closeOutput_nonvirtual(output);
1941}
1942
1943status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1944{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001945 // keep strong reference on the playback thread so that
1946 // it is not destroyed while exit() is executed
Ethan Chen8d420ed2013-11-14 00:40:54 -08001947#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301948 AudioSessionDescriptor *desc = mDirectAudioTracks.valueFor(output);
1949 if (desc) {
1950 ALOGV("Closing DirectTrack output %d", output);
1951 desc->mActive = false;
1952 desc->stream->common.standby(&desc->stream->common);
1953 desc->hwDev->close_output_stream(desc->hwDev, desc->stream);
1954 desc->trackRefPtr = NULL;
1955 desc->stream = NULL;
1956 mDirectAudioTracks.removeItem(output);
1957 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
1958 delete desc;
1959 return NO_ERROR;
1960 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001961#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05301962
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001963 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001964 {
1965 Mutex::Autolock _l(mLock);
1966 thread = checkPlaybackThread_l(output);
1967 if (thread == NULL) {
1968 return BAD_VALUE;
1969 }
1970
Steve Block3856b092011-10-20 11:56:00 +01001971 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001972
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001973 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001974 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001975 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001976 DuplicatingThread *dupThread =
1977 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001978 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001979
1980 }
1981 }
1982 }
1983
1984
1985 mPlaybackThreads.removeItem(output);
1986 // save all effects to the default thread
1987 if (mPlaybackThreads.size()) {
1988 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1989 if (dstThread != NULL) {
1990 // audioflinger lock is held here so the acquisition order of thread locks does not
1991 // matter
1992 Mutex::Autolock _dl(dstThread->mLock);
1993 Mutex::Autolock _sl(thread->mLock);
1994 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1995 for (size_t i = 0; i < effectChains.size(); i ++) {
1996 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001997 }
1998 }
1999 }
Glenn Kastena1117922012-01-26 10:53:32 -08002000 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002001 }
2002 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08002003 // The thread entity (active unit of execution) is no longer running here,
2004 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002005
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002006 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002007 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002008 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002009 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002010 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002011 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002012 }
2013 return NO_ERROR;
2014}
2015
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002016status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002017{
2018 Mutex::Autolock _l(mLock);
2019 PlaybackThread *thread = checkPlaybackThread_l(output);
2020
2021 if (thread == NULL) {
2022 return BAD_VALUE;
2023 }
2024
Steve Block3856b092011-10-20 11:56:00 +01002025 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002026 thread->suspend();
2027
2028 return NO_ERROR;
2029}
2030
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002031status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002032{
2033 Mutex::Autolock _l(mLock);
2034 PlaybackThread *thread = checkPlaybackThread_l(output);
2035
2036 if (thread == NULL) {
2037 return BAD_VALUE;
2038 }
2039
Steve Block3856b092011-10-20 11:56:00 +01002040 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002041
2042 thread->restore();
2043
2044 return NO_ERROR;
2045}
2046
Eric Laurenta4c5a552012-03-29 10:12:40 -07002047audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
2048 audio_devices_t *pDevices,
2049 uint32_t *pSamplingRate,
2050 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07002051 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002052{
2053 status_t status;
2054 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002055 struct audio_config config;
2056 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
2057 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
2058 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
2059
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002060 uint32_t reqSamplingRate = config.sample_rate;
2061 audio_format_t reqFormat = config.format;
2062 audio_channel_mask_t reqChannels = config.channel_mask;
2063 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07002064 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002065
2066 if (pDevices == NULL || *pDevices == 0) {
2067 return 0;
2068 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002069
Mathias Agopian65ab4712010-07-14 17:59:35 -07002070 Mutex::Autolock _l(mLock);
2071
Eric Laurenta4c5a552012-03-29 10:12:40 -07002072 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07002073 if (inHwDev == NULL)
2074 return 0;
2075
John Grossmanee578c02012-07-23 17:05:46 -07002076 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002077 audio_io_handle_t id = nextUniqueId();
2078
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00002079#ifndef ICS_AUDIO_BLOB
John Grossmanee578c02012-07-23 17:05:46 -07002080 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07002081 &inStream);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00002082#else
2083 status = inHwHal->open_input_stream(inHwHal, *pDevices,
2084 (int *)&config.format,
2085 &config.channel_mask,
2086 &config.sample_rate, (audio_in_acoustics_t)0,
2087 &inStream);
2088#endif
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002089 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
2090 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07002091 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002092 config.sample_rate,
2093 config.format,
2094 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002095 status);
2096
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002097 // If the input could not be opened with the requested parameters and we can handle the
2098 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
2099 // 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 -07002100 if (status == BAD_VALUE &&
2101 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
2102 (config.sample_rate <= 2 * reqSamplingRate) &&
Alok Kediya601e9b52013-09-27 11:55:10 +05302103 (getInputChannelCount(config.channel_mask) <= FCC_2) && (getInputChannelCount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07002104 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002105 inStream = NULL;
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00002106#ifndef ICS_AUDIO_BLOB
John Grossmanee578c02012-07-23 17:05:46 -07002107 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +00002108#else
2109 status = inHwHal->open_input_stream(inHwHal, *pDevices,
2110 (int *)&config.format,
2111 &config.channel_mask,
2112 &config.sample_rate, (audio_in_acoustics_t)0,
2113 &inStream);
2114#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07002115 }
2116
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002117 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002118
Glenn Kasten46909e72013-02-26 09:20:22 -08002119#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002120 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2121 // or (re-)create if current Pipe is idle and does not match the new format
2122 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002123 enum {
2124 TEE_SINK_NO, // don't copy input
2125 TEE_SINK_NEW, // copy input using a new pipe
2126 TEE_SINK_OLD, // copy input using an existing pipe
2127 } kind;
2128 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
2129 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002130 if (!mTeeSinkInputEnabled) {
2131 kind = TEE_SINK_NO;
2132 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002133 kind = TEE_SINK_NO;
2134 } else if (mRecordTeeSink == 0) {
2135 kind = TEE_SINK_NEW;
2136 } else if (mRecordTeeSink->getStrongCount() != 1) {
2137 kind = TEE_SINK_NO;
2138 } else if (format == mRecordTeeSink->format()) {
2139 kind = TEE_SINK_OLD;
2140 } else {
2141 kind = TEE_SINK_NEW;
2142 }
2143 switch (kind) {
2144 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002145 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002146 size_t numCounterOffers = 0;
2147 const NBAIO_Format offers[1] = {format};
2148 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2149 ALOG_ASSERT(index == 0);
2150 PipeReader *pipeReader = new PipeReader(*pipe);
2151 numCounterOffers = 0;
2152 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2153 ALOG_ASSERT(index == 0);
2154 mRecordTeeSink = pipe;
2155 mRecordTeeSource = pipeReader;
2156 teeSink = pipe;
2157 }
2158 break;
2159 case TEE_SINK_OLD:
2160 teeSink = mRecordTeeSink;
2161 break;
2162 case TEE_SINK_NO:
2163 default:
2164 break;
2165 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002166#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002167
Dima Zavin799a70e2011-04-18 16:57:27 -07002168 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
2169
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002170 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002171 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002172 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002173 thread = new RecordThread(this,
2174 input,
2175 reqSamplingRate,
2176 reqChannels,
2177 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08002178 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08002179 *pDevices
2180#ifdef TEE_SINK
2181 , teeSink
2182#endif
2183 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01002185 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08002186 if (pSamplingRate != NULL) {
2187 *pSamplingRate = reqSamplingRate;
2188 }
2189 if (pFormat != NULL) {
2190 *pFormat = config.format;
2191 }
2192 if (pChannelMask != NULL) {
2193 *pChannelMask = reqChannels;
2194 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002195
Mathias Agopian65ab4712010-07-14 17:59:35 -07002196 // notify client processes of the new input creation
2197 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
2198 return id;
2199 }
2200
2201 return 0;
2202}
2203
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002204status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002205{
Glenn Kastend96c5722012-04-25 13:44:49 -07002206 return closeInput_nonvirtual(input);
2207}
2208
2209status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2210{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002211 // keep strong reference on the record thread so that
2212 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002213 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002214 {
2215 Mutex::Autolock _l(mLock);
2216 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002217 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218 return BAD_VALUE;
2219 }
2220
Steve Block3856b092011-10-20 11:56:00 +01002221 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08002222 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002223 mRecordThreads.removeItem(input);
2224 }
2225 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08002226 // The thread entity (active unit of execution) is no longer running here,
2227 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002228
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002229 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002230 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002231 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002232 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002233 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002234
2235 return NO_ERROR;
2236}
2237
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002238status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002239{
2240 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01002241 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002242
2243 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2244 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002245 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002246 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002247
2248 return NO_ERROR;
2249}
2250
2251
2252int AudioFlinger::newAudioSessionId()
2253{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002254 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002255}
2256
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002257void AudioFlinger::acquireAudioSessionId(int audioSession)
2258{
2259 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002260 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01002261 ALOGV("acquiring %d from %d", audioSession, caller);
Eric Laurentd1b28d42013-09-18 18:47:13 -07002262
2263 // Ignore requests received from processes not known as notification client. The request
2264 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2265 // called from a different pid leaving a stale session reference. Also we don't know how
2266 // to clear this reference if the client process dies.
2267 if (mNotificationClients.indexOfKey(caller) < 0) {
2268 ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2269 return;
2270 }
2271
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002272 size_t num = mAudioSessionRefs.size();
2273 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002274 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002275 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2276 ref->mCnt++;
2277 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002278 return;
2279 }
2280 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002281 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2282 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002283}
2284
2285void AudioFlinger::releaseAudioSessionId(int audioSession)
2286{
2287 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002288 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01002289 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002290 size_t num = mAudioSessionRefs.size();
2291 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002292 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002293 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2294 ref->mCnt--;
2295 ALOGV(" decremented refcount to %d", ref->mCnt);
2296 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002297 mAudioSessionRefs.removeAt(i);
2298 delete ref;
2299 purgeStaleEffects_l();
2300 }
2301 return;
2302 }
2303 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002304 // If the caller is mediaserver it is likely that the session being released was acquired
2305 // on behalf of a process not in notification clients and we ignore the warning.
2306 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002307}
2308
2309void AudioFlinger::purgeStaleEffects_l() {
2310
Steve Block3856b092011-10-20 11:56:00 +01002311 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002312
2313 Vector< sp<EffectChain> > chains;
2314
2315 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2316 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2317 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2318 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002319 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2320 chains.push(ec);
2321 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002322 }
2323 }
2324 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2325 sp<RecordThread> t = mRecordThreads.valueAt(i);
2326 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2327 sp<EffectChain> ec = t->mEffectChains[j];
2328 chains.push(ec);
2329 }
2330 }
2331
2332 for (size_t i = 0; i < chains.size(); i++) {
2333 sp<EffectChain> ec = chains[i];
2334 int sessionid = ec->sessionId();
2335 sp<ThreadBase> t = ec->mThread.promote();
2336 if (t == 0) {
2337 continue;
2338 }
2339 size_t numsessionrefs = mAudioSessionRefs.size();
2340 bool found = false;
2341 for (size_t k = 0; k < numsessionrefs; k++) {
2342 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002343 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002344 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002345 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002346 found = true;
2347 break;
2348 }
2349 }
2350 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002351 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002352 // remove all effects from the chain
2353 while (ec->mEffects.size()) {
2354 sp<EffectModule> effect = ec->mEffects[0];
2355 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002356 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002357 if (effect->purgeHandles()) {
2358 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002359 }
2360 AudioSystem::unregisterEffect(effect->id());
2361 }
2362 }
2363 }
2364 return;
2365}
2366
Mathias Agopian65ab4712010-07-14 17:59:35 -07002367// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002368AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002369{
Glenn Kastena1117922012-01-26 10:53:32 -08002370 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002371}
2372
2373// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002374AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002375{
2376 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002377 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002378}
2379
2380// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002381AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002382{
Glenn Kastena1117922012-01-26 10:53:32 -08002383 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002384}
2385
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002386uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002388 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002389}
2390
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002391AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002392{
2393 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2394 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002395 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002396 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002397 return thread;
2398 }
2399 }
2400 return NULL;
2401}
2402
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002403audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002404{
2405 PlaybackThread *thread = primaryPlaybackThread_l();
2406
2407 if (thread == NULL) {
2408 return 0;
2409 }
2410
Eric Laurentf1c04f92012-08-28 14:26:53 -07002411 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002412}
2413
Eric Laurenta011e352012-03-29 15:51:43 -07002414sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2415 int triggerSession,
2416 int listenerSession,
2417 sync_event_callback_t callBack,
2418 void *cookie)
2419{
2420 Mutex::Autolock _l(mLock);
2421
2422 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2423 status_t playStatus = NAME_NOT_FOUND;
2424 status_t recStatus = NAME_NOT_FOUND;
2425 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2426 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2427 if (playStatus == NO_ERROR) {
2428 return event;
2429 }
2430 }
2431 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2432 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2433 if (recStatus == NO_ERROR) {
2434 return event;
2435 }
2436 }
2437 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2438 mPendingSyncEvents.add(event);
2439 } else {
2440 ALOGV("createSyncEvent() invalid event %d", event->type());
2441 event.clear();
2442 }
2443 return event;
2444}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002445
Mathias Agopian65ab4712010-07-14 17:59:35 -07002446// ----------------------------------------------------------------------------
2447// Effect management
2448// ----------------------------------------------------------------------------
2449
2450
Glenn Kastenf587ba52012-01-26 16:25:10 -08002451status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002452{
2453 Mutex::Autolock _l(mLock);
2454 return EffectQueryNumberEffects(numEffects);
2455}
2456
Glenn Kastenf587ba52012-01-26 16:25:10 -08002457status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002458{
2459 Mutex::Autolock _l(mLock);
2460 return EffectQueryEffect(index, descriptor);
2461}
2462
Glenn Kasten5e92a782012-01-30 07:40:52 -08002463status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002464 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002465{
2466 Mutex::Autolock _l(mLock);
2467 return EffectGetDescriptor(pUuid, descriptor);
2468}
2469
2470
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002471sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002472 effect_descriptor_t *pDesc,
2473 const sp<IEffectClient>& effectClient,
2474 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002475 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002476 int sessionId,
2477 status_t *status,
2478 int *id,
2479 int *enabled)
2480{
2481 status_t lStatus = NO_ERROR;
2482 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002483 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002484
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002485 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002486 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002487 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002488
2489 if (pDesc == NULL) {
2490 lStatus = BAD_VALUE;
2491 goto Exit;
2492 }
2493
Eric Laurent84e9a102010-09-23 16:10:16 -07002494 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002495 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002496 lStatus = PERMISSION_DENIED;
2497 goto Exit;
2498 }
2499
Dima Zavinfce7a472011-04-19 22:30:36 -07002500 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002501 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002502 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002503 lStatus = PERMISSION_DENIED;
2504 goto Exit;
2505 }
2506
Mathias Agopian65ab4712010-07-14 17:59:35 -07002507 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002508 if (!EffectIsNullUuid(&pDesc->uuid)) {
2509 // if uuid is specified, request effect descriptor
2510 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2511 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002512 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002513 goto Exit;
2514 }
2515 } else {
2516 // if uuid is not specified, look for an available implementation
2517 // of the required type in effect factory
2518 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002519 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002520 lStatus = BAD_VALUE;
2521 goto Exit;
2522 }
2523 uint32_t numEffects = 0;
2524 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002525 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002526 bool found = false;
2527
2528 lStatus = EffectQueryNumberEffects(&numEffects);
2529 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002530 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002531 goto Exit;
2532 }
2533 for (uint32_t i = 0; i < numEffects; i++) {
2534 lStatus = EffectQueryEffect(i, &desc);
2535 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002536 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002537 continue;
2538 }
2539 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2540 // If matching type found save effect descriptor. If the session is
2541 // 0 and the effect is not auxiliary, continue enumeration in case
2542 // an auxiliary version of this effect type is available
2543 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002544 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002545 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002546 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2547 break;
2548 }
2549 }
2550 }
2551 if (!found) {
2552 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002553 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002554 goto Exit;
2555 }
2556 // For same effect type, chose auxiliary version over insert version if
2557 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002558 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002559 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002560 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002561 }
2562 }
2563
2564 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002565 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002566 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2567 lStatus = INVALID_OPERATION;
2568 goto Exit;
2569 }
2570
Eric Laurent59255e42011-07-27 19:49:51 -07002571 // check recording permission for visualizer
2572 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2573 !recordingAllowed()) {
2574 lStatus = PERMISSION_DENIED;
2575 goto Exit;
2576 }
2577
Mathias Agopian65ab4712010-07-14 17:59:35 -07002578 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002579 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002580 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2581 // if the output returned by getOutputForEffect() is removed before we lock the
2582 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2583 // and we will exit safely
2584 io = AudioSystem::getOutputForEffect(&desc);
2585 ALOGV("createEffect got output %d", io);
2586 }
2587
2588 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002589
2590 // If output is not specified try to find a matching audio session ID in one of the
2591 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002592 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2593 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002594 // Note: io is never 0 when creating an effect on an input
2595 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002596 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2597 // output must be specified by AudioPolicyManager when using session
2598 // AUDIO_SESSION_OUTPUT_STAGE
2599 lStatus = BAD_VALUE;
2600 goto Exit;
2601 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002602 // look for the thread where the specified audio session is present
2603 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2604 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2605 io = mPlaybackThreads.keyAt(i);
2606 break;
2607 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002608 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002609 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002610 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2611 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2612 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002613 break;
2614 }
2615 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002616 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002617 // If no output thread contains the requested session ID, default to
2618 // first output. The effect chain will be moved to the correct output
2619 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002620 if (io == 0 && mPlaybackThreads.size()) {
2621 io = mPlaybackThreads.keyAt(0);
2622 }
Steve Block3856b092011-10-20 11:56:00 +01002623 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002624 }
2625 ThreadBase *thread = checkRecordThread_l(io);
2626 if (thread == NULL) {
2627 thread = checkPlaybackThread_l(io);
2628 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002629 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002630 lStatus = BAD_VALUE;
2631 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002632 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002633 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002634
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002635 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002636
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002637 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002638 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2639 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002640 if (handle != 0 && id != NULL) {
2641 *id = handle->id();
2642 }
2643 }
2644
2645Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002646 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002647 *status = lStatus;
2648 }
2649 return handle;
2650}
2651
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002652status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2653 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002654{
Steve Block3856b092011-10-20 11:56:00 +01002655 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002656 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002657 Mutex::Autolock _l(mLock);
2658 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002659 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002660 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002661 }
Eric Laurentde070132010-07-13 04:45:46 -07002662 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2663 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002664 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002665 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002666 }
Eric Laurentde070132010-07-13 04:45:46 -07002667 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2668 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002669 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002670 return BAD_VALUE;
2671 }
2672
2673 Mutex::Autolock _dl(dstThread->mLock);
2674 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002675 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002676}
2677
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002678// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002679status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002680 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002681 AudioFlinger::PlaybackThread *dstThread,
2682 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002683{
Steve Block3856b092011-10-20 11:56:00 +01002684 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002685 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002686
Eric Laurent59255e42011-07-27 19:49:51 -07002687 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002688 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002689 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002690 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002691 return INVALID_OPERATION;
2692 }
2693
Eric Laurent39e94f82010-07-28 01:32:47 -07002694 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002695 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002696 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002697 // removed.
2698 srcThread->removeEffectChain_l(chain);
2699
2700 // transfer all effects one by one so that new effect chain is created on new thread with
2701 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002702 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002703 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002704 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002705 Vector< sp<EffectModule> > removed;
2706 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002707 while (effect != 0) {
2708 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002709 removed.add(effect);
2710 status = dstThread->addEffect_l(effect);
2711 if (status != NO_ERROR) {
2712 break;
2713 }
Eric Laurentec35a142011-10-05 17:42:25 -07002714 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2715 if (effect->state() == EffectModule::ACTIVE ||
2716 effect->state() == EffectModule::STOPPING) {
2717 effect->start();
2718 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002719 // if the move request is not received from audio policy manager, the effect must be
2720 // re-registered with the new strategy and output
2721 if (dstChain == 0) {
2722 dstChain = effect->chain().promote();
2723 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002724 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002725 status = NO_INIT;
2726 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002727 }
2728 strategy = dstChain->strategy();
2729 }
2730 if (reRegister) {
2731 AudioSystem::unregisterEffect(effect->id());
2732 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002733 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002734 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002735 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002736 effect->id());
2737 }
Eric Laurentde070132010-07-13 04:45:46 -07002738 effect = chain->getEffectFromId_l(0);
2739 }
2740
Eric Laurent5baf2af2013-09-12 17:37:00 -07002741 if (status != NO_ERROR) {
2742 for (size_t i = 0; i < removed.size(); i++) {
2743 srcThread->addEffect_l(removed[i]);
2744 if (dstChain != 0 && reRegister) {
2745 AudioSystem::unregisterEffect(removed[i]->id());
2746 AudioSystem::registerEffect(&removed[i]->desc(),
2747 srcThread->id(),
2748 strategy,
2749 sessionId,
2750 removed[i]->id());
2751 }
2752 }
2753 }
2754
2755 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002756}
2757
Eric Laurent5baf2af2013-09-12 17:37:00 -07002758bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002759{
2760 if (mGlobalEffectEnableTime != 0 &&
2761 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2762 return true;
2763 }
2764
2765 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2766 sp<EffectChain> ec =
2767 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002768 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002769 return true;
2770 }
2771 }
2772 return false;
2773}
2774
Eric Laurent5baf2af2013-09-12 17:37:00 -07002775void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002776{
2777 Mutex::Autolock _l(mLock);
2778
2779 mGlobalEffectEnableTime = systemTime();
2780
2781 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2782 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2783 if (t->mType == ThreadBase::OFFLOAD) {
2784 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2785 }
2786 }
2787
2788}
2789
Glenn Kastenda6ef132013-01-10 12:31:01 -08002790struct Entry {
2791#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2792 char mName[MAX_NAME];
2793};
2794
2795int comparEntry(const void *p1, const void *p2)
2796{
2797 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2798}
2799
Glenn Kasten46909e72013-02-26 09:20:22 -08002800#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002801void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002802{
Eric Laurent81784c32012-11-19 14:55:58 -08002803 NBAIO_Source *teeSource = source.get();
2804 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002805 // .wav rotation
2806 // There is a benign race condition if 2 threads call this simultaneously.
2807 // They would both traverse the directory, but the result would simply be
2808 // failures at unlink() which are ignored. It's also unlikely since
2809 // normally dumpsys is only done by bugreport or from the command line.
2810 char teePath[32+256];
2811 strcpy(teePath, "/data/misc/media");
2812 size_t teePathLen = strlen(teePath);
2813 DIR *dir = opendir(teePath);
2814 teePath[teePathLen++] = '/';
2815 if (dir != NULL) {
2816#define MAX_SORT 20 // number of entries to sort
2817#define MAX_KEEP 10 // number of entries to keep
2818 struct Entry entries[MAX_SORT];
2819 size_t entryCount = 0;
2820 while (entryCount < MAX_SORT) {
2821 struct dirent de;
2822 struct dirent *result = NULL;
2823 int rc = readdir_r(dir, &de, &result);
2824 if (rc != 0) {
2825 ALOGW("readdir_r failed %d", rc);
2826 break;
2827 }
2828 if (result == NULL) {
2829 break;
2830 }
2831 if (result != &de) {
2832 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2833 break;
2834 }
2835 // ignore non .wav file entries
2836 size_t nameLen = strlen(de.d_name);
2837 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2838 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2839 continue;
2840 }
2841 strcpy(entries[entryCount++].mName, de.d_name);
2842 }
2843 (void) closedir(dir);
2844 if (entryCount > MAX_KEEP) {
2845 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2846 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2847 strcpy(&teePath[teePathLen], entries[i].mName);
2848 (void) unlink(teePath);
2849 }
2850 }
2851 } else {
2852 if (fd >= 0) {
2853 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2854 }
2855 }
Eric Laurent81784c32012-11-19 14:55:58 -08002856 char teeTime[16];
2857 struct timeval tv;
2858 gettimeofday(&tv, NULL);
2859 struct tm tm;
2860 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002861 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2862 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2863 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2864 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002865 if (teeFd >= 0) {
2866 char wavHeader[44];
2867 memcpy(wavHeader,
2868 "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",
2869 sizeof(wavHeader));
2870 NBAIO_Format format = teeSource->format();
2871 unsigned channelCount = Format_channelCount(format);
2872 ALOG_ASSERT(channelCount <= FCC_2);
2873 uint32_t sampleRate = Format_sampleRate(format);
2874 wavHeader[22] = channelCount; // number of channels
2875 wavHeader[24] = sampleRate; // sample rate
2876 wavHeader[25] = sampleRate >> 8;
2877 wavHeader[32] = channelCount * 2; // block alignment
2878 write(teeFd, wavHeader, sizeof(wavHeader));
2879 size_t total = 0;
2880 bool firstRead = true;
2881 for (;;) {
2882#define TEE_SINK_READ 1024
2883 short buffer[TEE_SINK_READ * FCC_2];
2884 size_t count = TEE_SINK_READ;
2885 ssize_t actual = teeSource->read(buffer, count,
2886 AudioBufferProvider::kInvalidPTS);
2887 bool wasFirstRead = firstRead;
2888 firstRead = false;
2889 if (actual <= 0) {
2890 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2891 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002892 }
Eric Laurent81784c32012-11-19 14:55:58 -08002893 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002894 }
Eric Laurent81784c32012-11-19 14:55:58 -08002895 ALOG_ASSERT(actual <= (ssize_t)count);
2896 write(teeFd, buffer, actual * channelCount * sizeof(short));
2897 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002898 }
Eric Laurent81784c32012-11-19 14:55:58 -08002899 lseek(teeFd, (off_t) 4, SEEK_SET);
2900 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2901 write(teeFd, &temp, sizeof(temp));
2902 lseek(teeFd, (off_t) 40, SEEK_SET);
2903 temp = total * channelCount * sizeof(short);
2904 write(teeFd, &temp, sizeof(temp));
2905 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002906 if (fd >= 0) {
2907 fdprintf(fd, "tee copied to %s\n", teePath);
2908 }
Eric Laurent59255e42011-07-27 19:49:51 -07002909 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002910 if (fd >= 0) {
2911 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2912 }
Eric Laurent59255e42011-07-27 19:49:51 -07002913 }
2914 }
2915}
Glenn Kasten46909e72013-02-26 09:20:22 -08002916#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002917
Mathias Agopian65ab4712010-07-14 17:59:35 -07002918status_t AudioFlinger::onTransact(
2919 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2920{
2921 return BnAudioFlinger::onTransact(code, data, reply, flags);
2922}
2923
Mathias Agopian65ab4712010-07-14 17:59:35 -07002924}; // namespace android