blob: c5ad0f5d974ddb7b668c031fbffbf1b3f2ac17ad [file] [log] [blame]
Glenn Kastenb3db2132012-01-19 08:59:58 -08001/*
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
Eric Laurent7d850f22010-07-09 13:34:17 -070020//#define LOG_NDEBUG 0
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070021
22#include <math.h>
23#include <signal.h>
24#include <sys/time.h>
25#include <sys/resource.h>
26
Gloria Wang9b3f1522011-02-24 14:51:45 -080027#include <binder/IPCThreadState.h>
Mathias Agopian07952722009-05-19 19:08:10 -070028#include <binder/IServiceManager.h>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070029#include <utils/Log.h>
Mathias Agopian07952722009-05-19 19:08:10 -070030#include <binder/Parcel.h>
31#include <binder/IPCThreadState.h>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070032#include <utils/String16.h>
33#include <utils/threads.h>
Eric Laurentae29b762011-03-28 18:37:07 -070034#include <utils/Atomic.h>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070035
Dima Zavin24fc2fb2011-04-19 22:30:36 -070036#include <cutils/bitops.h>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080037#include <cutils/properties.h>
Glenn Kastene80a4cc2011-12-15 09:51:17 -080038#include <cutils/compiler.h>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080039
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -070040#undef ADD_BATTERY_DATA
41
42#ifdef ADD_BATTERY_DATA
Gloria Wang9b3f1522011-02-24 14:51:45 -080043#include <media/IMediaPlayerService.h>
Glenn Kasten26f260a2012-01-03 15:28:29 -080044#include <media/IMediaDeathNotifier.h>
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -070045#endif
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070046
47#include <private/media/AudioTrackShared.h>
Eric Laurent65b65452010-06-01 23:49:17 -070048#include <private/media/AudioEffectShared.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070049
Dima Zavin34bb4192011-05-11 14:15:23 -070050#include <system/audio.h>
Dima Zavin290029d2011-06-13 18:16:26 -070051#include <hardware/audio.h>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070052
53#include "AudioMixer.h"
54#include "AudioFlinger.h"
Glenn Kasten81211142012-02-05 18:09:08 -080055#include "ServiceUtilities.h"
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070056
Eric Laurent53334cd2010-06-23 17:38:20 -070057#include <media/EffectsFactoryApi.h>
Eric Laurent5cc05262011-06-24 07:01:31 -070058#include <audio_effects/effect_visualizer.h>
Eric Laurent6639b552011-08-01 09:52:20 -070059#include <audio_effects/effect_ns.h>
60#include <audio_effects/effect_aec.h>
Eric Laurent65b65452010-06-01 23:49:17 -070061
Glenn Kasten490909d2011-12-15 09:52:39 -080062#include <audio_utils/primitives.h>
63
Eric Laurent6dbdc402011-07-22 09:04:31 -070064#include <powermanager/PowerManager.h>
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080065
Glenn Kastenda494f92011-07-08 15:26:12 -070066// #define DEBUG_CPU_USAGE 10 // log statistics every n wall clock seconds
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080067#ifdef DEBUG_CPU_USAGE
68#include <cpustats/CentralTendencyStatistics.h>
69#include <cpustats/ThreadCpuUsage.h>
70#endif
Glenn Kastenda494f92011-07-08 15:26:12 -070071
John Grossmand8cf2962012-02-08 16:37:41 -080072#include <common_time/cc_helper.h>
73#include <common_time/local_clock.h>
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
Eric Laurent8ed6ed02010-07-13 04:45:46 -070077
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070078namespace android {
79
Glenn Kasten62de3db2011-12-12 09:04:45 -080080static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
81static const char kHardwareLockedString[] = "Hardware lock is taken\n";
The Android Open Source Project10592532009-03-18 17:39:46 -070082
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070083static const float MAX_GAIN = 4096.0f;
Glenn Kasten0632bad2012-01-17 12:20:54 -080084static const uint32_t MAX_GAIN_INT = 0x1000;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070085
86// retry counts for buffer fill timeout
87// 50 * ~20msecs = 1 second
88static const int8_t kMaxTrackRetries = 50;
89static const int8_t kMaxTrackStartupRetries = 50;
Eric Laurentef9500f2010-03-11 14:47:00 -080090// allow less retry attempts on direct output thread.
91// direct outputs can be a scarce resource in audio hardware and should
92// be released as quickly as possible.
93static const int8_t kMaxTrackRetriesDirect = 2;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070094
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070095static const int kDumpLockRetries = 50;
Glenn Kastencbfe2e12011-12-13 11:04:14 -080096static const int kDumpLockSleepUs = 20000;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070097
Glenn Kastencbfe2e12011-12-13 11:04:14 -080098// don't warn about blocked writes or record buffer overflows more often than this
99static const nsecs_t kWarningThrottleNs = seconds(5);
Dave Sparksd0ac8c02009-09-30 03:09:03 -0700100
Eric Laurent464d5b32011-06-17 21:29:58 -0700101// RecordThread loop sleep time upon application overrun or audio HAL read error
102static const int kRecordThreadSleepUs = 5000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
Glenn Kastencbfe2e12011-12-13 11:04:14 -0800104// maximum time to wait for setParameters to complete
105static const nsecs_t kSetParametersTimeoutNs = seconds(2);
Eric Laurent5f37be32011-09-13 11:40:21 -0700106
Eric Laurentf1f5fc82011-11-22 18:50:29 -0800107// minimum sleep time for the mixer thread loop when tracks are active but in underrun
108static const uint32_t kMinThreadSleepTimeUs = 5000;
109// maximum divider applied to the active sleep time in the mixer thread loop
110static const uint32_t kMaxThreadSleepTimeShift = 2;
111
John Grossmand8cf2962012-02-08 16:37:41 -0800112nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurentf1f5fc82011-11-22 18:50:29 -0800113
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700114// ----------------------------------------------------------------------------
115
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -0700116#ifdef ADD_BATTERY_DATA
Gloria Wang9b3f1522011-02-24 14:51:45 -0800117// To collect the amplifier usage
118static void addBatteryData(uint32_t params) {
Glenn Kasten26f260a2012-01-03 15:28:29 -0800119 sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService();
120 if (service == NULL) {
121 // it already logged
Gloria Wang9b3f1522011-02-24 14:51:45 -0800122 return;
123 }
124
125 service->addBatteryData(params);
126}
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -0700127#endif
Gloria Wang9b3f1522011-02-24 14:51:45 -0800128
Dima Zavin31f188892011-04-18 16:57:27 -0700129static int load_audio_interface(const char *if_name, const hw_module_t **mod,
130 audio_hw_device_t **dev)
131{
132 int rc;
133
134 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, mod);
135 if (rc)
136 goto out;
137
138 rc = audio_hw_device_open(*mod, dev);
Steve Block3762c312012-01-06 19:20:56 +0000139 ALOGE_IF(rc, "couldn't open audio hw device in %s.%s (%s)",
Dima Zavin31f188892011-04-18 16:57:27 -0700140 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
141 if (rc)
142 goto out;
143
144 return 0;
145
146out:
147 *mod = NULL;
148 *dev = NULL;
149 return rc;
150}
151
Glenn Kasten62de3db2011-12-12 09:04:45 -0800152static const char * const audio_interfaces[] = {
Dima Zavin31f188892011-04-18 16:57:27 -0700153 "primary",
154 "a2dp",
155 "usb",
156};
157#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
158
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700159// ----------------------------------------------------------------------------
160
161AudioFlinger::AudioFlinger()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 : BnAudioFlinger(),
John Grossmand8cf2962012-02-08 16:37:41 -0800163 mPrimaryHardwareDev(NULL),
164 mHardwareStatus(AUDIO_HW_IDLE), // see also onFirstRef()
165 mMasterVolume(1.0f),
166 mMasterVolumeSupportLvl(MVS_NONE),
167 mMasterMute(false),
168 mNextUniqueId(1),
169 mMode(AUDIO_MODE_INVALID),
170 mBtNrecIsOff(false)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700171{
Dima Zavin2986f5b2011-04-19 19:04:32 -0700172}
173
174void AudioFlinger::onFirstRef()
175{
Dima Zavin31f188892011-04-18 16:57:27 -0700176 int rc = 0;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700177
Eric Laurent01635942011-01-18 18:39:02 -0800178 Mutex::Autolock _l(mLock);
179
Dima Zavin31f188892011-04-18 16:57:27 -0700180 /* TODO: move all this work into an Init() function */
John Grossmand8cf2962012-02-08 16:37:41 -0800181 char val_str[PROPERTY_VALUE_MAX] = { 0 };
182 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
183 uint32_t int_val;
184 if (1 == sscanf(val_str, "%u", &int_val)) {
185 mStandbyTimeInNsecs = milliseconds(int_val);
186 ALOGI("Using %u mSec as standby time.", int_val);
187 } else {
188 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
189 ALOGI("Using default %u mSec as standby time.",
190 (uint32_t)(mStandbyTimeInNsecs / 1000000));
191 }
192 }
Eric Laurenta553c252009-07-17 12:17:14 -0700193
Dima Zavin31f188892011-04-18 16:57:27 -0700194 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
195 const hw_module_t *mod;
196 audio_hw_device_t *dev;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700197
Dima Zavin31f188892011-04-18 16:57:27 -0700198 rc = load_audio_interface(audio_interfaces[i], &mod, &dev);
199 if (rc)
200 continue;
201
Steve Block6215d3f2012-01-04 20:05:49 +0000202 ALOGI("Loaded %s audio interface from %s (%s)", audio_interfaces[i],
Glenn Kasten18db49a2012-03-12 16:29:55 -0700203 mod->name, mod->id);
Dima Zavin31f188892011-04-18 16:57:27 -0700204 mAudioHwDevs.push(dev);
205
Glenn Kastenad8d1752012-02-02 14:05:20 -0800206 if (mPrimaryHardwareDev == NULL) {
Dima Zavin31f188892011-04-18 16:57:27 -0700207 mPrimaryHardwareDev = dev;
Steve Block6215d3f2012-01-04 20:05:49 +0000208 ALOGI("Using '%s' (%s.%s) as the primary audio interface",
Glenn Kasten18db49a2012-03-12 16:29:55 -0700209 mod->name, mod->id, audio_interfaces[i]);
Dima Zavin31f188892011-04-18 16:57:27 -0700210 }
211 }
Eric Laurenta553c252009-07-17 12:17:14 -0700212
Glenn Kastenad8d1752012-02-02 14:05:20 -0800213 if (mPrimaryHardwareDev == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000214 ALOGE("Primary audio interface not found");
Glenn Kastenad8d1752012-02-02 14:05:20 -0800215 // proceed, all later accesses to mPrimaryHardwareDev verify it's safe with initCheck()
Dima Zavin31f188892011-04-18 16:57:27 -0700216 }
217
Glenn Kastenad8d1752012-02-02 14:05:20 -0800218 // Currently (mPrimaryHardwareDev == NULL) == (mAudioHwDevs.size() == 0), but the way the
219 // primary HW dev is selected can change so these conditions might not always be equivalent.
220 // When that happens, re-visit all the code that assumes this.
221
222 AutoMutex lock(mHardwareLock);
223
John Grossmand8cf2962012-02-08 16:37:41 -0800224 // Determine the level of master volume support the primary audio HAL has,
225 // and set the initial master volume at the same time.
226 float initialVolume = 1.0;
227 mMasterVolumeSupportLvl = MVS_NONE;
228 if (0 == mPrimaryHardwareDev->init_check(mPrimaryHardwareDev)) {
229 audio_hw_device_t *dev = mPrimaryHardwareDev;
230
231 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
232 if ((NULL != dev->get_master_volume) &&
233 (NO_ERROR == dev->get_master_volume(dev, &initialVolume))) {
234 mMasterVolumeSupportLvl = MVS_FULL;
235 } else {
236 mMasterVolumeSupportLvl = MVS_SETONLY;
237 initialVolume = 1.0;
238 }
239
240 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
241 if ((NULL == dev->set_master_volume) ||
242 (NO_ERROR != dev->set_master_volume(dev, initialVolume))) {
243 mMasterVolumeSupportLvl = MVS_NONE;
244 }
Glenn Kasten23c9c742012-02-02 14:16:03 -0800245 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8cf2962012-02-08 16:37:41 -0800246 }
247
248 // Set the mode for each audio HAL, and try to set the initial volume (if
249 // supported) for all of the non-primary audio HALs.
Dima Zavin31f188892011-04-18 16:57:27 -0700250 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
251 audio_hw_device_t *dev = mAudioHwDevs[i];
252
253 mHardwareStatus = AUDIO_HW_INIT;
254 rc = dev->init_check(dev);
Glenn Kastenad8d1752012-02-02 14:05:20 -0800255 mHardwareStatus = AUDIO_HW_IDLE;
Dima Zavin31f188892011-04-18 16:57:27 -0700256 if (rc == 0) {
Glenn Kastenad8d1752012-02-02 14:05:20 -0800257 mMode = AUDIO_MODE_NORMAL; // assigned multiple times with same value
Dima Zavin31f188892011-04-18 16:57:27 -0700258 mHardwareStatus = AUDIO_HW_SET_MODE;
259 dev->set_mode(dev, mMode);
John Grossmand8cf2962012-02-08 16:37:41 -0800260
261 if ((dev != mPrimaryHardwareDev) &&
262 (NULL != dev->set_master_volume)) {
263 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
264 dev->set_master_volume(dev, initialVolume);
265 }
266
Glenn Kasten23c9c742012-02-02 14:16:03 -0800267 mHardwareStatus = AUDIO_HW_IDLE;
Dima Zavin31f188892011-04-18 16:57:27 -0700268 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700269 }
John Grossmand8cf2962012-02-08 16:37:41 -0800270
271 mMasterVolumeSW = (MVS_NONE == mMasterVolumeSupportLvl)
272 ? initialVolume
273 : 1.0;
274 mMasterVolume = initialVolume;
275 mHardwareStatus = AUDIO_HW_IDLE;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700276}
277
278AudioFlinger::~AudioFlinger()
279{
Dima Zavin31f188892011-04-18 16:57:27 -0700280
Eric Laurent7954c462009-08-28 10:39:03 -0700281 while (!mRecordThreads.isEmpty()) {
282 // closeInput() will remove first entry from mRecordThreads
283 closeInput(mRecordThreads.keyAt(0));
284 }
285 while (!mPlaybackThreads.isEmpty()) {
286 // closeOutput() will remove first entry from mPlaybackThreads
287 closeOutput(mPlaybackThreads.keyAt(0));
288 }
Dima Zavin31f188892011-04-18 16:57:27 -0700289
Glenn Kastenad8d1752012-02-02 14:05:20 -0800290 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
291 // no mHardwareLock needed, as there are no other references to this
292 audio_hw_device_close(mAudioHwDevs[i]);
Eric Laurent7954c462009-08-28 10:39:03 -0700293 }
The Android Open Source Projectb7986892009-01-09 17:51:23 -0800294}
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800295
Dima Zavin31f188892011-04-18 16:57:27 -0700296audio_hw_device_t* AudioFlinger::findSuitableHwDev_l(uint32_t devices)
297{
298 /* first matching HW device is returned */
299 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
300 audio_hw_device_t *dev = mAudioHwDevs[i];
301 if ((dev->get_supported_devices(dev) & devices) == devices)
302 return dev;
303 }
304 return NULL;
305}
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700306
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700307status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
308{
309 const size_t SIZE = 256;
310 char buffer[SIZE];
311 String8 result;
312
313 result.append("Clients:\n");
314 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kastene4787422012-01-25 14:27:41 -0800315 sp<Client> client = mClients.valueAt(i).promote();
316 if (client != 0) {
317 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
318 result.append(buffer);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700319 }
320 }
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700321
322 result.append("Global session refs:\n");
Glenn Kasten9a42ac92012-03-06 11:22:01 -0800323 result.append(" session pid count\n");
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700324 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
325 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten9a42ac92012-03-06 11:22:01 -0800326 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700327 result.append(buffer);
328 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700329 write(fd, result.string(), result.size());
330 return NO_ERROR;
331}
332
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700333
334status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
335{
336 const size_t SIZE = 256;
337 char buffer[SIZE];
338 String8 result;
Glenn Kastena934c2c2012-01-04 11:02:33 -0800339 hardware_call_state hardwareStatus = mHardwareStatus;
Eric Laurenta553c252009-07-17 12:17:14 -0700340
John Grossmand8cf2962012-02-08 16:37:41 -0800341 snprintf(buffer, SIZE, "Hardware status: %d\n"
342 "Standby Time mSec: %u\n",
343 hardwareStatus,
344 (uint32_t)(mStandbyTimeInNsecs / 1000000));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700345 result.append(buffer);
346 write(fd, result.string(), result.size());
347 return NO_ERROR;
348}
349
350status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
351{
352 const size_t SIZE = 256;
353 char buffer[SIZE];
354 String8 result;
355 snprintf(buffer, SIZE, "Permission Denial: "
356 "can't dump AudioFlinger from pid=%d, uid=%d\n",
357 IPCThreadState::self()->getCallingPid(),
358 IPCThreadState::self()->getCallingUid());
359 result.append(buffer);
360 write(fd, result.string(), result.size());
361 return NO_ERROR;
362}
363
The Android Open Source Project10592532009-03-18 17:39:46 -0700364static bool tryLock(Mutex& mutex)
365{
366 bool locked = false;
367 for (int i = 0; i < kDumpLockRetries; ++i) {
368 if (mutex.tryLock() == NO_ERROR) {
369 locked = true;
370 break;
371 }
Glenn Kastencbfe2e12011-12-13 11:04:14 -0800372 usleep(kDumpLockSleepUs);
The Android Open Source Project10592532009-03-18 17:39:46 -0700373 }
374 return locked;
375}
376
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700377status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
378{
Glenn Kasten81211142012-02-05 18:09:08 -0800379 if (!dumpAllowed()) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700380 dumpPermissionDenial(fd, args);
381 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -0700382 // get state of hardware lock
383 bool hardwareLocked = tryLock(mHardwareLock);
384 if (!hardwareLocked) {
385 String8 result(kHardwareLockedString);
386 write(fd, result.string(), result.size());
387 } else {
388 mHardwareLock.unlock();
389 }
390
391 bool locked = tryLock(mLock);
392
393 // failed to lock - AudioFlinger is probably deadlocked
394 if (!locked) {
395 String8 result(kDeadlockedString);
396 write(fd, result.string(), result.size());
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700397 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700398
399 dumpClients(fd, args);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700400 dumpInternals(fd, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401
Eric Laurenta553c252009-07-17 12:17:14 -0700402 // dump playback threads
403 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700404 mPlaybackThreads.valueAt(i)->dump(fd, args);
Eric Laurenta553c252009-07-17 12:17:14 -0700405 }
406
407 // dump record threads
Eric Laurent102313a2009-07-23 13:35:01 -0700408 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700409 mRecordThreads.valueAt(i)->dump(fd, args);
Eric Laurenta553c252009-07-17 12:17:14 -0700410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411
Dima Zavin31f188892011-04-18 16:57:27 -0700412 // dump all hardware devs
413 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
414 audio_hw_device_t *dev = mAudioHwDevs[i];
415 dev->dump(dev, fd);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700416 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700417 if (locked) mLock.unlock();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700418 }
419 return NO_ERROR;
420}
421
Glenn Kasten803a86a2012-01-25 14:28:29 -0800422sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
423{
424 // If pid is already in the mClients wp<> map, then use that entry
425 // (for which promote() is always != 0), otherwise create a new entry and Client.
426 sp<Client> client = mClients.valueFor(pid).promote();
427 if (client == 0) {
428 client = new Client(this, pid);
429 mClients.add(pid, client);
430 }
431
432 return client;
433}
Eric Laurenta553c252009-07-17 12:17:14 -0700434
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700435// IAudioFlinger interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436
437
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700438sp<IAudioTrack> AudioFlinger::createTrack(
439 pid_t pid,
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800440 audio_stream_type_t streamType,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700441 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -0800442 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700443 uint32_t channelMask,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800444 int frameCount,
Glenn Kasten84569cc2012-03-06 11:22:44 -0800445 IAudioFlinger::track_flags_t flags,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800446 const sp<IMemory>& sharedBuffer,
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800447 audio_io_handle_t output,
Eric Laurent65b65452010-06-01 23:49:17 -0700448 int *sessionId,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800449 status_t *status)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700450{
Eric Laurenta553c252009-07-17 12:17:14 -0700451 sp<PlaybackThread::Track> track;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700452 sp<TrackHandle> trackHandle;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700453 sp<Client> client;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800454 status_t lStatus;
Eric Laurent65b65452010-06-01 23:49:17 -0700455 int lSessionId;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700456
Glenn Kasten6e987a42012-01-06 08:40:01 -0800457 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
458 // but if someone uses binder directly they could bypass that and cause us to crash
459 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block3762c312012-01-06 19:20:56 +0000460 ALOGE("createTrack() invalid stream type %d", streamType);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800461 lStatus = BAD_VALUE;
462 goto Exit;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700463 }
464
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800465 {
466 Mutex::Autolock _l(mLock);
Eric Laurenta553c252009-07-17 12:17:14 -0700467 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent493941b2010-07-28 01:32:47 -0700468 PlaybackThread *effectThread = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -0700469 if (thread == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000470 ALOGE("unknown output thread");
Eric Laurenta553c252009-07-17 12:17:14 -0700471 lStatus = BAD_VALUE;
472 goto Exit;
473 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800474
Glenn Kasten803a86a2012-01-25 14:28:29 -0800475 client = registerPid_l(pid);
Eric Laurent65b65452010-06-01 23:49:17 -0700476
Steve Block71f2cf12011-10-20 11:56:00 +0100477 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700478 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700479 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent493941b2010-07-28 01:32:47 -0700480 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
481 if (mPlaybackThreads.keyAt(i) != output) {
482 // prevent same audio session on different output threads
483 uint32_t sessions = t->hasAudioSession(*sessionId);
484 if (sessions & PlaybackThread::TRACK_SESSION) {
Steve Block3762c312012-01-06 19:20:56 +0000485 ALOGE("createTrack() session ID %d already in use", *sessionId);
Eric Laurent493941b2010-07-28 01:32:47 -0700486 lStatus = BAD_VALUE;
487 goto Exit;
488 }
489 // check if an effect with same session ID is waiting for a track to be created
490 if (sessions & PlaybackThread::EFFECT_SESSION) {
491 effectThread = t.get();
492 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700493 }
494 }
Eric Laurent65b65452010-06-01 23:49:17 -0700495 lSessionId = *sessionId;
496 } else {
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700497 // if no audio session id is provided, create one here
Eric Laurent464d5b32011-06-17 21:29:58 -0700498 lSessionId = nextUniqueId();
Eric Laurent65b65452010-06-01 23:49:17 -0700499 if (sessionId != NULL) {
500 *sessionId = lSessionId;
501 }
502 }
Steve Block71f2cf12011-10-20 11:56:00 +0100503 ALOGV("createTrack() lSessionId: %d", lSessionId);
Eric Laurent65b65452010-06-01 23:49:17 -0700504
Glenn Kasten84569cc2012-03-06 11:22:44 -0800505 bool isTimed = (flags & IAudioFlinger::TRACK_TIMED) != 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700506 track = thread->createTrack_l(client, streamType, sampleRate, format,
John Grossmand8cf2962012-02-08 16:37:41 -0800507 channelMask, frameCount, sharedBuffer, lSessionId, isTimed, &lStatus);
Eric Laurent493941b2010-07-28 01:32:47 -0700508
509 // move effect chain to this output thread if an effect on same session was waiting
510 // for a track to be created
511 if (lStatus == NO_ERROR && effectThread != NULL) {
512 Mutex::Autolock _dl(thread->mLock);
513 Mutex::Autolock _sl(effectThread->mLock);
514 moveEffectChain_l(lSessionId, effectThread, thread, true);
515 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700516 }
517 if (lStatus == NO_ERROR) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800518 trackHandle = new TrackHandle(track);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700519 } else {
Eric Laurentb9481d82009-09-17 05:12:56 -0700520 // remove local strong reference to Client before deleting the Track so that the Client
521 // destructor is called by the TrackBase destructor with mLock held
522 client.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700523 track.clear();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800524 }
525
526Exit:
Glenn Kasten18db49a2012-03-12 16:29:55 -0700527 if (status != NULL) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800528 *status = lStatus;
529 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700530 return trackHandle;
531}
532
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800533uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700534{
Eric Laurenta553c252009-07-17 12:17:14 -0700535 Mutex::Autolock _l(mLock);
536 PlaybackThread *thread = checkPlaybackThread_l(output);
537 if (thread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000538 ALOGW("sampleRate() unknown thread %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700539 return 0;
540 }
541 return thread->sampleRate();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700542}
543
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800544int AudioFlinger::channelCount(audio_io_handle_t output) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700545{
Eric Laurenta553c252009-07-17 12:17:14 -0700546 Mutex::Autolock _l(mLock);
547 PlaybackThread *thread = checkPlaybackThread_l(output);
548 if (thread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000549 ALOGW("channelCount() unknown thread %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700550 return 0;
551 }
552 return thread->channelCount();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700553}
554
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800555audio_format_t AudioFlinger::format(audio_io_handle_t output) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700556{
Eric Laurenta553c252009-07-17 12:17:14 -0700557 Mutex::Autolock _l(mLock);
558 PlaybackThread *thread = checkPlaybackThread_l(output);
559 if (thread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000560 ALOGW("format() unknown thread %d", output);
Glenn Kasten0a204ed2012-01-12 12:27:51 -0800561 return AUDIO_FORMAT_INVALID;
Eric Laurenta553c252009-07-17 12:17:14 -0700562 }
563 return thread->format();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700564}
565
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800566size_t AudioFlinger::frameCount(audio_io_handle_t output) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700567{
Eric Laurenta553c252009-07-17 12:17:14 -0700568 Mutex::Autolock _l(mLock);
569 PlaybackThread *thread = checkPlaybackThread_l(output);
570 if (thread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000571 ALOGW("frameCount() unknown thread %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700572 return 0;
573 }
574 return thread->frameCount();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700575}
576
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800577uint32_t AudioFlinger::latency(audio_io_handle_t output) const
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800578{
Eric Laurenta553c252009-07-17 12:17:14 -0700579 Mutex::Autolock _l(mLock);
580 PlaybackThread *thread = checkPlaybackThread_l(output);
581 if (thread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000582 ALOGW("latency() unknown thread %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700583 return 0;
584 }
585 return thread->latency();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800586}
587
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700588status_t AudioFlinger::setMasterVolume(float value)
589{
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700590 status_t ret = initCheck();
591 if (ret != NO_ERROR) {
592 return ret;
593 }
594
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700595 // check calling permissions
596 if (!settingsAllowed()) {
597 return PERMISSION_DENIED;
598 }
599
John Grossmand8cf2962012-02-08 16:37:41 -0800600 float swmv = value;
601
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700602 // when hw supports master volume, don't scale in sw mixer
John Grossmand8cf2962012-02-08 16:37:41 -0800603 if (MVS_NONE != mMasterVolumeSupportLvl) {
604 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
605 AutoMutex lock(mHardwareLock);
606 audio_hw_device_t *dev = mAudioHwDevs[i];
607
608 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
609 if (NULL != dev->set_master_volume) {
610 dev->set_master_volume(dev, value);
611 }
612 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent01635942011-01-18 18:39:02 -0800613 }
John Grossmand8cf2962012-02-08 16:37:41 -0800614
615 swmv = 1.0;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700616 }
Eric Laurenta553c252009-07-17 12:17:14 -0700617
Eric Laurent01635942011-01-18 18:39:02 -0800618 Mutex::Autolock _l(mLock);
John Grossmand8cf2962012-02-08 16:37:41 -0800619 mMasterVolume = value;
620 mMasterVolumeSW = swmv;
Glenn Kasten150d2382012-02-08 14:04:28 -0800621 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kasten18db49a2012-03-12 16:29:55 -0700622 mPlaybackThreads.valueAt(i)->setMasterVolume(swmv);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700623
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700624 return NO_ERROR;
625}
626
Glenn Kastenaccb1142012-01-04 11:00:47 -0800627status_t AudioFlinger::setMode(audio_mode_t mode)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700628{
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700629 status_t ret = initCheck();
630 if (ret != NO_ERROR) {
631 return ret;
632 }
Eric Laurent53334cd2010-06-23 17:38:20 -0700633
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700634 // check calling permissions
635 if (!settingsAllowed()) {
636 return PERMISSION_DENIED;
637 }
Glenn Kasten01aaf2c2012-01-06 16:47:31 -0800638 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block8564c8d2012-01-05 23:22:43 +0000639 ALOGW("Illegal value: setMode(%d)", mode);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700640 return BAD_VALUE;
641 }
642
Eric Laurent53334cd2010-06-23 17:38:20 -0700643 { // scope for the lock
644 AutoMutex lock(mHardwareLock);
645 mHardwareStatus = AUDIO_HW_SET_MODE;
Dima Zavin31f188892011-04-18 16:57:27 -0700646 ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode);
Eric Laurent53334cd2010-06-23 17:38:20 -0700647 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten871c16c2010-03-05 12:18:01 -0800648 }
Eric Laurent53334cd2010-06-23 17:38:20 -0700649
650 if (NO_ERROR == ret) {
651 Mutex::Autolock _l(mLock);
652 mMode = mode;
Glenn Kasten150d2382012-02-08 14:04:28 -0800653 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kasten18db49a2012-03-12 16:29:55 -0700654 mPlaybackThreads.valueAt(i)->setMode(mode);
Eric Laurent53334cd2010-06-23 17:38:20 -0700655 }
656
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700657 return ret;
658}
659
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700660status_t AudioFlinger::setMicMute(bool state)
661{
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700662 status_t ret = initCheck();
663 if (ret != NO_ERROR) {
664 return ret;
665 }
666
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700667 // check calling permissions
668 if (!settingsAllowed()) {
669 return PERMISSION_DENIED;
670 }
671
672 AutoMutex lock(mHardwareLock);
673 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700674 ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700675 mHardwareStatus = AUDIO_HW_IDLE;
676 return ret;
677}
678
679bool AudioFlinger::getMicMute() const
680{
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700681 status_t ret = initCheck();
682 if (ret != NO_ERROR) {
683 return false;
684 }
685
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700686 bool state = AUDIO_MODE_INVALID;
Glenn Kastenad8d1752012-02-02 14:05:20 -0800687 AutoMutex lock(mHardwareLock);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700688 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Dima Zavin31f188892011-04-18 16:57:27 -0700689 mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700690 mHardwareStatus = AUDIO_HW_IDLE;
691 return state;
692}
693
694status_t AudioFlinger::setMasterMute(bool muted)
695{
696 // check calling permissions
697 if (!settingsAllowed()) {
698 return PERMISSION_DENIED;
699 }
Eric Laurenta553c252009-07-17 12:17:14 -0700700
Eric Laurent01635942011-01-18 18:39:02 -0800701 Mutex::Autolock _l(mLock);
Glenn Kastenb3db2132012-01-19 08:59:58 -0800702 // This is an optimization, so PlaybackThread doesn't have to look at the one from AudioFlinger
Eric Laurenta553c252009-07-17 12:17:14 -0700703 mMasterMute = muted;
Glenn Kasten150d2382012-02-08 14:04:28 -0800704 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kasten18db49a2012-03-12 16:29:55 -0700705 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurenta553c252009-07-17 12:17:14 -0700706
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700707 return NO_ERROR;
708}
709
710float AudioFlinger::masterVolume() const
711{
Glenn Kastene6f8a422011-12-13 11:47:54 -0800712 Mutex::Autolock _l(mLock);
713 return masterVolume_l();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700714}
715
John Grossmand8cf2962012-02-08 16:37:41 -0800716float AudioFlinger::masterVolumeSW() const
717{
718 Mutex::Autolock _l(mLock);
719 return masterVolumeSW_l();
720}
721
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700722bool AudioFlinger::masterMute() const
723{
Glenn Kastene6f8a422011-12-13 11:47:54 -0800724 Mutex::Autolock _l(mLock);
725 return masterMute_l();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700726}
727
John Grossmand8cf2962012-02-08 16:37:41 -0800728float AudioFlinger::masterVolume_l() const
729{
730 if (MVS_FULL == mMasterVolumeSupportLvl) {
731 float ret_val;
732 AutoMutex lock(mHardwareLock);
733
734 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Glenn Kasten81916df2012-03-08 12:18:35 -0800735 ALOG_ASSERT((NULL != mPrimaryHardwareDev) &&
736 (NULL != mPrimaryHardwareDev->get_master_volume),
737 "can't get master volume");
John Grossmand8cf2962012-02-08 16:37:41 -0800738
739 mPrimaryHardwareDev->get_master_volume(mPrimaryHardwareDev, &ret_val);
740 mHardwareStatus = AUDIO_HW_IDLE;
741 return ret_val;
742 }
743
744 return mMasterVolume;
745}
746
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800747status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
748 audio_io_handle_t output)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700749{
750 // check calling permissions
751 if (!settingsAllowed()) {
752 return PERMISSION_DENIED;
753 }
754
Glenn Kasten6e987a42012-01-06 08:40:01 -0800755 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block3762c312012-01-06 19:20:56 +0000756 ALOGE("setStreamVolume() invalid stream %d", stream);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700757 return BAD_VALUE;
758 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800759
Eric Laurenta553c252009-07-17 12:17:14 -0700760 AutoMutex lock(mLock);
761 PlaybackThread *thread = NULL;
762 if (output) {
763 thread = checkPlaybackThread_l(output);
764 if (thread == NULL) {
765 return BAD_VALUE;
766 }
767 }
768
Eric Laurenta553c252009-07-17 12:17:14 -0700769 mStreamTypes[stream].volume = value;
770
771 if (thread == NULL) {
Glenn Kasten150d2382012-02-08 14:04:28 -0800772 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kasten18db49a2012-03-12 16:29:55 -0700773 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Eric Laurent415f3e22009-10-21 08:14:22 -0700774 }
Eric Laurenta553c252009-07-17 12:17:14 -0700775 } else {
776 thread->setStreamVolume(stream, value);
777 }
Eric Laurentef028272009-04-21 07:56:33 -0700778
Eric Laurent415f3e22009-10-21 08:14:22 -0700779 return NO_ERROR;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700780}
781
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800782status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700783{
784 // check calling permissions
785 if (!settingsAllowed()) {
786 return PERMISSION_DENIED;
787 }
788
Glenn Kasten6e987a42012-01-06 08:40:01 -0800789 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700790 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block3762c312012-01-06 19:20:56 +0000791 ALOGE("setStreamMute() invalid stream %d", stream);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700792 return BAD_VALUE;
793 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700794
Eric Laurent01635942011-01-18 18:39:02 -0800795 AutoMutex lock(mLock);
Eric Laurenta553c252009-07-17 12:17:14 -0700796 mStreamTypes[stream].mute = muted;
797 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kasten18db49a2012-03-12 16:29:55 -0700798 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700800 return NO_ERROR;
801}
802
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800803float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700804{
Glenn Kasten6e987a42012-01-06 08:40:01 -0800805 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700806 return 0.0f;
807 }
Eric Laurenta553c252009-07-17 12:17:14 -0700808
809 AutoMutex lock(mLock);
810 float volume;
811 if (output) {
812 PlaybackThread *thread = checkPlaybackThread_l(output);
813 if (thread == NULL) {
814 return 0.0f;
815 }
816 volume = thread->streamVolume(stream);
817 } else {
Glenn Kasten8b02b992012-01-09 09:40:36 -0800818 volume = streamVolume_l(stream);
Eric Laurenta553c252009-07-17 12:17:14 -0700819 }
820
Eric Laurentef028272009-04-21 07:56:33 -0700821 return volume;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700822}
823
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800824bool AudioFlinger::streamMute(audio_stream_type_t stream) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700825{
Glenn Kasten6e987a42012-01-06 08:40:01 -0800826 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700827 return true;
828 }
Eric Laurenta553c252009-07-17 12:17:14 -0700829
Glenn Kasten8b02b992012-01-09 09:40:36 -0800830 AutoMutex lock(mLock);
831 return streamMute_l(stream);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700832}
833
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800834status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700835{
Glenn Kasten7ed4f0c2012-02-03 11:10:00 -0800836 ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling pid %d",
Eric Laurenta553c252009-07-17 12:17:14 -0700837 ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid());
838 // check calling permissions
839 if (!settingsAllowed()) {
840 return PERMISSION_DENIED;
The Android Open Source Project9266c552009-01-15 16:12:10 -0800841 }
Eric Laurenta553c252009-07-17 12:17:14 -0700842
843 // ioHandle == 0 means the parameters are global to the audio hardware interface
844 if (ioHandle == 0) {
Dima Zavin31f188892011-04-18 16:57:27 -0700845 status_t final_result = NO_ERROR;
Glenn Kasten23c9c742012-02-02 14:16:03 -0800846 {
847 AutoMutex lock(mHardwareLock);
848 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
Dima Zavin31f188892011-04-18 16:57:27 -0700849 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
850 audio_hw_device_t *dev = mAudioHwDevs[i];
Glenn Kasten23c9c742012-02-02 14:16:03 -0800851 status_t result = dev->set_parameters(dev, keyValuePairs.string());
Dima Zavin31f188892011-04-18 16:57:27 -0700852 final_result = result ?: final_result;
853 }
Eric Laurenta553c252009-07-17 12:17:14 -0700854 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten23c9c742012-02-02 14:16:03 -0800855 }
Eric Laurent6639b552011-08-01 09:52:20 -0700856 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
857 AudioParameter param = AudioParameter(keyValuePairs);
858 String8 value;
859 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
860 Mutex::Autolock _l(mLock);
Eric Laurent2d95dfb2011-08-29 12:42:48 -0700861 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
862 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent6639b552011-08-01 09:52:20 -0700863 for (size_t i = 0; i < mRecordThreads.size(); i++) {
864 sp<RecordThread> thread = mRecordThreads.valueAt(i);
865 RecordThread::RecordTrack *track = thread->track();
866 if (track != NULL) {
867 audio_devices_t device = (audio_devices_t)(
868 thread->device() & AUDIO_DEVICE_IN_ALL);
Eric Laurent2d95dfb2011-08-29 12:42:48 -0700869 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
Eric Laurent6639b552011-08-01 09:52:20 -0700870 thread->setEffectSuspended(FX_IID_AEC,
871 suspend,
872 track->sessionId());
873 thread->setEffectSuspended(FX_IID_NS,
874 suspend,
875 track->sessionId());
876 }
877 }
Eric Laurent2d95dfb2011-08-29 12:42:48 -0700878 mBtNrecIsOff = btNrecIsOff;
Eric Laurent6639b552011-08-01 09:52:20 -0700879 }
880 }
Dima Zavin31f188892011-04-18 16:57:27 -0700881 return final_result;
Eric Laurenta553c252009-07-17 12:17:14 -0700882 }
883
Eric Laurentb7d94602009-09-29 11:12:57 -0700884 // hold a strong ref on thread in case closeOutput() or closeInput() is called
885 // and the thread is exited once the lock is released
886 sp<ThreadBase> thread;
887 {
888 Mutex::Autolock _l(mLock);
889 thread = checkPlaybackThread_l(ioHandle);
890 if (thread == NULL) {
891 thread = checkRecordThread_l(ioHandle);
Glenn Kasten70ed6b72012-01-10 10:46:34 -0800892 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent464d5b32011-06-17 21:29:58 -0700893 // indicate output device change to all input threads for pre processing
894 AudioParameter param = AudioParameter(keyValuePairs);
895 int value;
Eric Laurent10c3fc82012-03-16 20:37:59 -0700896 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
897 (value != 0)) {
Eric Laurent464d5b32011-06-17 21:29:58 -0700898 for (size_t i = 0; i < mRecordThreads.size(); i++) {
899 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
900 }
901 }
Eric Laurentb7d94602009-09-29 11:12:57 -0700902 }
Eric Laurenta553c252009-07-17 12:17:14 -0700903 }
Glenn Kasten706b6182012-01-20 13:44:40 -0800904 if (thread != 0) {
905 return thread->setParameters(keyValuePairs);
Eric Laurenta553c252009-07-17 12:17:14 -0700906 }
Eric Laurenta553c252009-07-17 12:17:14 -0700907 return BAD_VALUE;
908}
909
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800910String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Eric Laurenta553c252009-07-17 12:17:14 -0700911{
Glenn Kasten7ed4f0c2012-02-03 11:10:00 -0800912// ALOGV("getParameters() io %d, keys %s, tid %d, calling pid %d",
Eric Laurenta553c252009-07-17 12:17:14 -0700913// ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid());
914
915 if (ioHandle == 0) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700916 String8 out_s8;
917
Dima Zavin31f188892011-04-18 16:57:27 -0700918 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten23c9c742012-02-02 14:16:03 -0800919 char *s;
920 {
921 AutoMutex lock(mHardwareLock);
922 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Dima Zavin31f188892011-04-18 16:57:27 -0700923 audio_hw_device_t *dev = mAudioHwDevs[i];
Glenn Kasten23c9c742012-02-02 14:16:03 -0800924 s = dev->get_parameters(dev, keys.string());
925 mHardwareStatus = AUDIO_HW_IDLE;
926 }
John Grossman4fbe95e2012-02-09 11:28:36 -0800927 out_s8 += String8(s ? s : "");
Dima Zavin31f188892011-04-18 16:57:27 -0700928 free(s);
929 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700930 return out_s8;
Eric Laurenta553c252009-07-17 12:17:14 -0700931 }
Eric Laurentb7d94602009-09-29 11:12:57 -0700932
933 Mutex::Autolock _l(mLock);
934
Eric Laurenta553c252009-07-17 12:17:14 -0700935 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
936 if (playbackThread != NULL) {
937 return playbackThread->getParameters(keys);
938 }
939 RecordThread *recordThread = checkRecordThread_l(ioHandle);
940 if (recordThread != NULL) {
941 return recordThread->getParameters(keys);
942 }
943 return String8("");
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700944}
945
Glenn Kasten3f6d83a2012-01-26 16:25:10 -0800946size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, int channelCount) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947{
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700948 status_t ret = initCheck();
949 if (ret != NO_ERROR) {
950 return 0;
951 }
952
Glenn Kastenad8d1752012-02-02 14:05:20 -0800953 AutoMutex lock(mHardwareLock);
954 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
955 size_t size = mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, sampleRate, format, channelCount);
956 mHardwareStatus = AUDIO_HW_IDLE;
957 return size;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958}
959
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800960unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Eric Laurent47d0a922010-02-26 02:47:27 -0800961{
962 if (ioHandle == 0) {
963 return 0;
964 }
965
966 Mutex::Autolock _l(mLock);
967
968 RecordThread *recordThread = checkRecordThread_l(ioHandle);
969 if (recordThread != NULL) {
970 return recordThread->getInputFramesLost();
971 }
972 return 0;
973}
974
Eric Laurent415f3e22009-10-21 08:14:22 -0700975status_t AudioFlinger::setVoiceVolume(float value)
976{
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700977 status_t ret = initCheck();
978 if (ret != NO_ERROR) {
979 return ret;
980 }
981
Eric Laurent415f3e22009-10-21 08:14:22 -0700982 // check calling permissions
983 if (!settingsAllowed()) {
984 return PERMISSION_DENIED;
985 }
986
987 AutoMutex lock(mHardwareLock);
Glenn Kasten23c9c742012-02-02 14:16:03 -0800988 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700989 ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value);
Eric Laurent415f3e22009-10-21 08:14:22 -0700990 mHardwareStatus = AUDIO_HW_IDLE;
991
992 return ret;
993}
994
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800995status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
996 audio_io_handle_t output) const
Eric Laurent0986e792010-01-19 17:37:09 -0800997{
998 status_t status;
999
1000 Mutex::Autolock _l(mLock);
1001
1002 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1003 if (playbackThread != NULL) {
1004 return playbackThread->getRenderPosition(halFrames, dspFrames);
1005 }
1006
1007 return BAD_VALUE;
1008}
1009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1011{
Eric Laurenta553c252009-07-17 12:17:14 -07001012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 Mutex::Autolock _l(mLock);
1014
Glenn Kastend153b1f2012-02-03 11:10:26 -08001015 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001016 if (mNotificationClients.indexOfKey(pid) < 0) {
1017 sp<NotificationClient> notificationClient = new NotificationClient(this,
1018 client,
1019 pid);
Steve Block71f2cf12011-10-20 11:56:00 +01001020 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Eric Laurenta553c252009-07-17 12:17:14 -07001021
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001022 mNotificationClients.add(pid, notificationClient);
Eric Laurenta553c252009-07-17 12:17:14 -07001023
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001024 sp<IBinder> binder = client->asBinder();
1025 binder->linkToDeath(notificationClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001027 // the config change is always sent from playback or record threads to avoid deadlock
1028 // with AudioSystem::gLock
1029 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1030 mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED);
1031 }
Eric Laurenta553c252009-07-17 12:17:14 -07001032
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001033 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1034 mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 }
1036 }
1037}
1038
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001039void AudioFlinger::removeNotificationClient(pid_t pid)
1040{
1041 Mutex::Autolock _l(mLock);
1042
Glenn Kasten538529b2012-01-20 09:19:01 -08001043 mNotificationClients.removeItem(pid);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07001044
Steve Block71f2cf12011-10-20 11:56:00 +01001045 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten150d2382012-02-08 14:04:28 -08001046 size_t num = mAudioSessionRefs.size();
Marco Nelissenc74b93f2011-08-02 13:33:41 -07001047 bool removed = false;
Glenn Kasten150d2382012-02-08 14:04:28 -08001048 for (size_t i = 0; i< num; ) {
Marco Nelissenc74b93f2011-08-02 13:33:41 -07001049 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten9a42ac92012-03-06 11:22:01 -08001050 ALOGV(" pid %d @ %d", ref->mPid, i);
1051 if (ref->mPid == pid) {
1052 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07001053 mAudioSessionRefs.removeAt(i);
1054 delete ref;
1055 removed = true;
Marco Nelissenc74b93f2011-08-02 13:33:41 -07001056 num--;
Glenn Kasten150d2382012-02-08 14:04:28 -08001057 } else {
1058 i++;
Marco Nelissenc74b93f2011-08-02 13:33:41 -07001059 }
1060 }
1061 if (removed) {
1062 purgeStaleEffects_l();
1063 }
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001064}
1065
Eric Laurent296a0ec2009-09-15 07:10:12 -07001066// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenffed04a2012-03-01 09:14:51 -08001067void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001068{
Eric Laurent49f02be2009-11-19 09:00:56 -08001069 size_t size = mNotificationClients.size();
1070 for (size_t i = 0; i < size; i++) {
Glenn Kastendc3ac852012-01-25 15:28:08 -08001071 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1072 param2);
Eric Laurenta553c252009-07-17 12:17:14 -07001073 }
1074}
1075
Eric Laurentb9481d82009-09-17 05:12:56 -07001076// removeClient_l() must be called with AudioFlinger::mLock held
1077void AudioFlinger::removeClient_l(pid_t pid)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001078{
Steve Block71f2cf12011-10-20 11:56:00 +01001079 ALOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001080 mClients.removeItem(pid);
1081}
1082
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001083
Eric Laurenta553c252009-07-17 12:17:14 -07001084// ----------------------------------------------------------------------------
1085
Glenn Kasten39d00cb2012-01-17 11:09:42 -08001086AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
1087 uint32_t device, type_t type)
Eric Laurenta553c252009-07-17 12:17:14 -07001088 : Thread(false),
Glenn Kastenefd511a2012-01-26 10:38:26 -08001089 mType(type),
Glenn Kastendc3ac852012-01-25 15:28:08 -08001090 mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0),
1091 // mChannelMask
1092 mChannelCount(0),
1093 mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID),
1094 mParamStatus(NO_ERROR),
Glenn Kasten761286f2012-01-06 08:39:38 -08001095 mStandby(false), mId(id),
Glenn Kastendc3ac852012-01-25 15:28:08 -08001096 mDevice(device),
1097 mDeathRecipient(new PMDeathRecipient(this))
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001098{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099}
1100
Eric Laurenta553c252009-07-17 12:17:14 -07001101AudioFlinger::ThreadBase::~ThreadBase()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102{
Eric Laurent8fce46a2009-08-04 09:45:33 -07001103 mParamCond.broadcast();
Eric Laurent6dbdc402011-07-22 09:04:31 -07001104 // do not lock the mutex in destructor
1105 releaseWakeLock_l();
Eric Laurent4a64a6e2011-09-27 12:07:15 -07001106 if (mPowerManager != 0) {
1107 sp<IBinder> binder = mPowerManager->asBinder();
1108 binder->unlinkToDeath(mDeathRecipient);
1109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110}
1111
Eric Laurenta553c252009-07-17 12:17:14 -07001112void AudioFlinger::ThreadBase::exit()
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001113{
Steve Block71f2cf12011-10-20 11:56:00 +01001114 ALOGV("ThreadBase::exit");
Eric Laurenta553c252009-07-17 12:17:14 -07001115 {
Glenn Kasten761286f2012-01-06 08:39:38 -08001116 // This lock prevents the following race in thread (uniprocessor for illustration):
1117 // if (!exitPending()) {
1118 // // context switch from here to exit()
1119 // // exit() calls requestExit(), what exitPending() observes
1120 // // exit() calls signal(), which is dropped since no waiters
1121 // // context switch back from exit() to here
1122 // mWaitWorkCV.wait(...);
1123 // // now thread is hung
1124 // }
Glenn Kasten325ee1c2012-01-05 15:41:56 -08001125 AutoMutex lock(mLock);
Eric Laurenta553c252009-07-17 12:17:14 -07001126 requestExit();
1127 mWaitWorkCV.signal();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001128 }
Glenn Kasten761286f2012-01-06 08:39:38 -08001129 // When Thread::requestExitAndWait is made virtual and this method is renamed to
1130 // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();"
Eric Laurenta553c252009-07-17 12:17:14 -07001131 requestExitAndWait();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001132}
Eric Laurenta553c252009-07-17 12:17:14 -07001133
Eric Laurenta553c252009-07-17 12:17:14 -07001134status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs)
1135{
Eric Laurent8fce46a2009-08-04 09:45:33 -07001136 status_t status;
Eric Laurenta553c252009-07-17 12:17:14 -07001137
Steve Block71f2cf12011-10-20 11:56:00 +01001138 ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string());
Eric Laurenta553c252009-07-17 12:17:14 -07001139 Mutex::Autolock _l(mLock);
Eric Laurenta553c252009-07-17 12:17:14 -07001140
Eric Laurent8fce46a2009-08-04 09:45:33 -07001141 mNewParameters.add(keyValuePairs);
Eric Laurenta553c252009-07-17 12:17:14 -07001142 mWaitWorkCV.signal();
Eric Laurentb7d94602009-09-29 11:12:57 -07001143 // wait condition with timeout in case the thread loop has exited
1144 // before the request could be processed
Glenn Kastencbfe2e12011-12-13 11:04:14 -08001145 if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) {
Eric Laurentb7d94602009-09-29 11:12:57 -07001146 status = mParamStatus;
1147 mWaitWorkCV.signal();
1148 } else {
1149 status = TIMED_OUT;
1150 }
Eric Laurent8fce46a2009-08-04 09:45:33 -07001151 return status;
Eric Laurenta553c252009-07-17 12:17:14 -07001152}
1153
1154void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param)
1155{
1156 Mutex::Autolock _l(mLock);
Eric Laurent8fce46a2009-08-04 09:45:33 -07001157 sendConfigEvent_l(event, param);
1158}
1159
1160// sendConfigEvent_l() must be called with ThreadBase::mLock held
1161void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param)
1162{
Glenn Kasten4b220f02011-12-13 11:50:00 -08001163 ConfigEvent configEvent;
1164 configEvent.mEvent = event;
1165 configEvent.mParam = param;
Eric Laurenta553c252009-07-17 12:17:14 -07001166 mConfigEvents.add(configEvent);
Steve Block71f2cf12011-10-20 11:56:00 +01001167 ALOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param);
Eric Laurenta553c252009-07-17 12:17:14 -07001168 mWaitWorkCV.signal();
1169}
1170
1171void AudioFlinger::ThreadBase::processConfigEvents()
1172{
1173 mLock.lock();
Glenn Kasten18db49a2012-03-12 16:29:55 -07001174 while (!mConfigEvents.isEmpty()) {
Steve Block71f2cf12011-10-20 11:56:00 +01001175 ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size());
Glenn Kasten4b220f02011-12-13 11:50:00 -08001176 ConfigEvent configEvent = mConfigEvents[0];
Eric Laurenta553c252009-07-17 12:17:14 -07001177 mConfigEvents.removeAt(0);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001178 // release mLock before locking AudioFlinger mLock: lock order is always
1179 // AudioFlinger then ThreadBase to avoid cross deadlock
Eric Laurenta553c252009-07-17 12:17:14 -07001180 mLock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001181 mAudioFlinger->mLock.lock();
Glenn Kasten4b220f02011-12-13 11:50:00 -08001182 audioConfigChanged_l(configEvent.mEvent, configEvent.mParam);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001183 mAudioFlinger->mLock.unlock();
Eric Laurenta553c252009-07-17 12:17:14 -07001184 mLock.lock();
1185 }
1186 mLock.unlock();
1187}
1188
Eric Laurent3fdb1262009-11-07 00:01:32 -08001189status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args)
1190{
1191 const size_t SIZE = 256;
1192 char buffer[SIZE];
1193 String8 result;
1194
1195 bool locked = tryLock(mLock);
1196 if (!locked) {
1197 snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this);
1198 write(fd, buffer, strlen(buffer));
1199 }
1200
Eric Laurent0194a0d2012-03-14 15:03:26 -07001201 snprintf(buffer, SIZE, "io handle: %d\n", mId);
1202 result.append(buffer);
1203 snprintf(buffer, SIZE, "TID: %d\n", getTid());
1204 result.append(buffer);
Eric Laurent3fdb1262009-11-07 00:01:32 -08001205 snprintf(buffer, SIZE, "standby: %d\n", mStandby);
1206 result.append(buffer);
1207 snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate);
1208 result.append(buffer);
1209 snprintf(buffer, SIZE, "Frame count: %d\n", mFrameCount);
1210 result.append(buffer);
1211 snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount);
1212 result.append(buffer);
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001213 snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask);
1214 result.append(buffer);
Eric Laurent3fdb1262009-11-07 00:01:32 -08001215 snprintf(buffer, SIZE, "Format: %d\n", mFormat);
1216 result.append(buffer);
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001217 snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize);
Eric Laurent3fdb1262009-11-07 00:01:32 -08001218 result.append(buffer);
1219
1220 snprintf(buffer, SIZE, "\nPending setParameters commands: \n");
1221 result.append(buffer);
1222 result.append(" Index Command");
1223 for (size_t i = 0; i < mNewParameters.size(); ++i) {
1224 snprintf(buffer, SIZE, "\n %02d ", i);
1225 result.append(buffer);
1226 result.append(mNewParameters[i]);
1227 }
1228
1229 snprintf(buffer, SIZE, "\n\nPending config events: \n");
1230 result.append(buffer);
1231 snprintf(buffer, SIZE, " Index event param\n");
1232 result.append(buffer);
1233 for (size_t i = 0; i < mConfigEvents.size(); i++) {
Glenn Kasten4b220f02011-12-13 11:50:00 -08001234 snprintf(buffer, SIZE, " %02d %02d %d\n", i, mConfigEvents[i].mEvent, mConfigEvents[i].mParam);
Eric Laurent3fdb1262009-11-07 00:01:32 -08001235 result.append(buffer);
1236 }
1237 result.append("\n");
1238
1239 write(fd, result.string(), result.size());
1240
1241 if (locked) {
1242 mLock.unlock();
1243 }
1244 return NO_ERROR;
1245}
1246
Eric Laurent1345d332011-07-24 17:49:51 -07001247status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args)
1248{
1249 const size_t SIZE = 256;
1250 char buffer[SIZE];
1251 String8 result;
1252
1253 snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size());
1254 write(fd, buffer, strlen(buffer));
1255
1256 for (size_t i = 0; i < mEffectChains.size(); ++i) {
1257 sp<EffectChain> chain = mEffectChains[i];
1258 if (chain != 0) {
1259 chain->dump(fd, args);
1260 }
1261 }
1262 return NO_ERROR;
1263}
1264
Eric Laurent6dbdc402011-07-22 09:04:31 -07001265void AudioFlinger::ThreadBase::acquireWakeLock()
1266{
1267 Mutex::Autolock _l(mLock);
1268 acquireWakeLock_l();
1269}
1270
1271void AudioFlinger::ThreadBase::acquireWakeLock_l()
1272{
1273 if (mPowerManager == 0) {
1274 // use checkService() to avoid blocking if power service is not up yet
1275 sp<IBinder> binder =
1276 defaultServiceManager()->checkService(String16("power"));
1277 if (binder == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001278 ALOGW("Thread %s cannot connect to the power manager service", mName);
Eric Laurent6dbdc402011-07-22 09:04:31 -07001279 } else {
1280 mPowerManager = interface_cast<IPowerManager>(binder);
1281 binder->linkToDeath(mDeathRecipient);
1282 }
1283 }
1284 if (mPowerManager != 0) {
1285 sp<IBinder> binder = new BBinder();
1286 status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
1287 binder,
1288 String16(mName));
1289 if (status == NO_ERROR) {
1290 mWakeLockToken = binder;
1291 }
Steve Block71f2cf12011-10-20 11:56:00 +01001292 ALOGV("acquireWakeLock_l() %s status %d", mName, status);
Eric Laurent6dbdc402011-07-22 09:04:31 -07001293 }
1294}
1295
1296void AudioFlinger::ThreadBase::releaseWakeLock()
1297{
1298 Mutex::Autolock _l(mLock);
Eric Laurentd49ead62011-07-28 13:59:02 -07001299 releaseWakeLock_l();
Eric Laurent6dbdc402011-07-22 09:04:31 -07001300}
1301
1302void AudioFlinger::ThreadBase::releaseWakeLock_l()
1303{
1304 if (mWakeLockToken != 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001305 ALOGV("releaseWakeLock_l() %s", mName);
Eric Laurent6dbdc402011-07-22 09:04:31 -07001306 if (mPowerManager != 0) {
1307 mPowerManager->releaseWakeLock(mWakeLockToken, 0);
1308 }
1309 mWakeLockToken.clear();
1310 }
1311}
1312
1313void AudioFlinger::ThreadBase::clearPowerManager()
1314{
1315 Mutex::Autolock _l(mLock);
1316 releaseWakeLock_l();
1317 mPowerManager.clear();
1318}
1319
1320void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who)
1321{
1322 sp<ThreadBase> thread = mThread.promote();
1323 if (thread != 0) {
1324 thread->clearPowerManager();
1325 }
Steve Block8564c8d2012-01-05 23:22:43 +00001326 ALOGW("power manager service died !!!");
Eric Laurent6dbdc402011-07-22 09:04:31 -07001327}
Eric Laurent1345d332011-07-24 17:49:51 -07001328
Eric Laurentf82fccd2011-07-27 19:49:51 -07001329void AudioFlinger::ThreadBase::setEffectSuspended(
1330 const effect_uuid_t *type, bool suspend, int sessionId)
1331{
1332 Mutex::Autolock _l(mLock);
1333 setEffectSuspended_l(type, suspend, sessionId);
1334}
1335
1336void AudioFlinger::ThreadBase::setEffectSuspended_l(
1337 const effect_uuid_t *type, bool suspend, int sessionId)
1338{
Glenn Kastenc64a6f02012-01-30 13:00:02 -08001339 sp<EffectChain> chain = getEffectChain_l(sessionId);
Eric Laurentf82fccd2011-07-27 19:49:51 -07001340 if (chain != 0) {
1341 if (type != NULL) {
1342 chain->setEffectSuspended_l(type, suspend);
1343 } else {
1344 chain->setEffectSuspendedAll_l(suspend);
1345 }
1346 }
1347
1348 updateSuspendedSessions_l(type, suspend, sessionId);
1349}
1350
1351void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain)
1352{
Glenn Kasten150d2382012-02-08 14:04:28 -08001353 ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId());
Eric Laurentf82fccd2011-07-27 19:49:51 -07001354 if (index < 0) {
1355 return;
1356 }
1357
1358 KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects =
1359 mSuspendedSessions.editValueAt(index);
1360
1361 for (size_t i = 0; i < sessionEffects.size(); i++) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07001362 sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i);
Eric Laurentf82fccd2011-07-27 19:49:51 -07001363 for (int j = 0; j < desc->mRefCount; j++) {
1364 if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) {
1365 chain->setEffectSuspendedAll_l(true);
1366 } else {
Steve Block71f2cf12011-10-20 11:56:00 +01001367 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x",
Glenn Kasten18db49a2012-03-12 16:29:55 -07001368 desc->mType.timeLow);
Eric Laurentf82fccd2011-07-27 19:49:51 -07001369 chain->setEffectSuspended_l(&desc->mType, true);
1370 }
1371 }
1372 }
1373}
1374
Eric Laurentf82fccd2011-07-27 19:49:51 -07001375void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type,
1376 bool suspend,
1377 int sessionId)
1378{
Glenn Kasten150d2382012-02-08 14:04:28 -08001379 ssize_t index = mSuspendedSessions.indexOfKey(sessionId);
Eric Laurentf82fccd2011-07-27 19:49:51 -07001380
1381 KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects;
1382
1383 if (suspend) {
1384 if (index >= 0) {
1385 sessionEffects = mSuspendedSessions.editValueAt(index);
1386 } else {
1387 mSuspendedSessions.add(sessionId, sessionEffects);
1388 }
1389 } else {
1390 if (index < 0) {
1391 return;
1392 }
1393 sessionEffects = mSuspendedSessions.editValueAt(index);
1394 }
1395
1396
1397 int key = EffectChain::kKeyForSuspendAll;
1398 if (type != NULL) {
1399 key = type->timeLow;
1400 }
1401 index = sessionEffects.indexOfKey(key);
1402
Glenn Kasten18db49a2012-03-12 16:29:55 -07001403 sp<SuspendedSessionDesc> desc;
Eric Laurentf82fccd2011-07-27 19:49:51 -07001404 if (suspend) {
1405 if (index >= 0) {
1406 desc = sessionEffects.valueAt(index);
1407 } else {
1408 desc = new SuspendedSessionDesc();
1409 if (type != NULL) {
1410 memcpy(&desc->mType, type, sizeof(effect_uuid_t));
1411 }
1412 sessionEffects.add(key, desc);
Steve Block71f2cf12011-10-20 11:56:00 +01001413 ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key);
Eric Laurentf82fccd2011-07-27 19:49:51 -07001414 }
1415 desc->mRefCount++;
1416 } else {
1417 if (index < 0) {
1418 return;
1419 }
1420 desc = sessionEffects.valueAt(index);
1421 if (--desc->mRefCount == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001422 ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key);
Eric Laurentf82fccd2011-07-27 19:49:51 -07001423 sessionEffects.removeItemsAt(index);
1424 if (sessionEffects.isEmpty()) {
Steve Block71f2cf12011-10-20 11:56:00 +01001425 ALOGV("updateSuspendedSessions_l() restore removing session %d",
Eric Laurentf82fccd2011-07-27 19:49:51 -07001426 sessionId);
1427 mSuspendedSessions.removeItem(sessionId);
1428 }
1429 }
1430 }
1431 if (!sessionEffects.isEmpty()) {
1432 mSuspendedSessions.replaceValueFor(sessionId, sessionEffects);
1433 }
1434}
1435
1436void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1437 bool enabled,
1438 int sessionId)
1439{
1440 Mutex::Autolock _l(mLock);
Eric Laurent7fa1cee2011-10-19 11:44:54 -07001441 checkSuspendOnEffectEnabled_l(effect, enabled, sessionId);
1442}
Eric Laurentf82fccd2011-07-27 19:49:51 -07001443
Eric Laurent7fa1cee2011-10-19 11:44:54 -07001444void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
1445 bool enabled,
1446 int sessionId)
1447{
Eric Laurent6752ec82011-08-10 10:37:50 -07001448 if (mType != RECORD) {
1449 // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
1450 // another session. This gives the priority to well behaved effect control panels
1451 // and applications not using global effects.
1452 if (sessionId != AUDIO_SESSION_OUTPUT_MIX) {
1453 setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
1454 }
1455 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07001456
1457 sp<EffectChain> chain = getEffectChain_l(sessionId);
1458 if (chain != 0) {
1459 chain->checkSuspendOnEffectEnabled(effect, enabled);
1460 }
1461}
1462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463// ----------------------------------------------------------------------------
1464
Eric Laurent464d5b32011-06-17 21:29:58 -07001465AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger,
1466 AudioStreamOut* output,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08001467 audio_io_handle_t id,
Glenn Kastenefd511a2012-01-26 10:38:26 -08001468 uint32_t device,
1469 type_t type)
1470 : ThreadBase(audioFlinger, id, device, type),
Glenn Kastendc3ac852012-01-25 15:28:08 -08001471 mMixBuffer(NULL), mSuspended(0), mBytesWritten(0),
1472 // Assumes constructor is called by AudioFlinger with it's mLock held,
1473 // but it would be safer to explicitly pass initial masterMute as parameter
1474 mMasterMute(audioFlinger->masterMute_l()),
1475 // mStreamTypes[] initialized in constructor body
1476 mOutput(output),
1477 // Assumes constructor is called by AudioFlinger with it's mLock held,
1478 // but it would be safer to explicitly pass initial masterVolume as parameter
John Grossmand8cf2962012-02-08 16:37:41 -08001479 mMasterVolume(audioFlinger->masterVolumeSW_l()),
Glenn Kastena13446a2012-03-08 07:47:15 -08001480 mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false),
Glenn Kasten6cb5e642012-03-12 18:13:59 -07001481 mMixerStatus(MIXER_IDLE),
Glenn Kasten0fe52c82012-02-24 14:59:21 -08001482 mPrevMixerStatus(MIXER_IDLE),
1483 standbyDelay(AudioFlinger::mStandbyTimeInNsecs)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484{
Glenn Kasten86e33622012-02-28 12:30:08 -08001485 snprintf(mName, kNameLength, "AudioOut_%X", id);
Eric Laurent6dbdc402011-07-22 09:04:31 -07001486
Eric Laurenta553c252009-07-17 12:17:14 -07001487 readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488
Glenn Kasten6e987a42012-01-06 08:40:01 -08001489 // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08001490 // There is no AUDIO_STREAM_MIN, and ++ operator does not compile
1491 for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT;
1492 stream = (audio_stream_type_t) (stream + 1)) {
Glenn Kasten8b02b992012-01-09 09:40:36 -08001493 mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream);
1494 mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream);
Glenn Kasten6e987a42012-01-06 08:40:01 -08001495 // initialized by stream_type_t default constructor
1496 // mStreamTypes[stream].valid = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 }
Glenn Kasten8b02b992012-01-09 09:40:36 -08001498 // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here,
1499 // because mAudioFlinger doesn't have one to copy from
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500}
1501
Eric Laurenta553c252009-07-17 12:17:14 -07001502AudioFlinger::PlaybackThread::~PlaybackThread()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503{
1504 delete [] mMixBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505}
1506
Eric Laurenta553c252009-07-17 12:17:14 -07001507status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508{
1509 dumpInternals(fd, args);
1510 dumpTracks(fd, args);
Eric Laurent65b65452010-06-01 23:49:17 -07001511 dumpEffectChains(fd, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 return NO_ERROR;
1513}
1514
Eric Laurenta553c252009-07-17 12:17:14 -07001515status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516{
1517 const size_t SIZE = 256;
1518 char buffer[SIZE];
1519 String8 result;
1520
Eric Laurenta553c252009-07-17 12:17:14 -07001521 snprintf(buffer, SIZE, "Output thread %p tracks\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 result.append(buffer);
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001523 result.append(" Name Clien Typ Fmt Chn mask Session Buf S M F SRate LeftV RighV Serv User Main buf Aux Buf\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 for (size_t i = 0; i < mTracks.size(); ++i) {
Eric Laurentd3b4d0c2009-03-31 14:34:35 -07001525 sp<Track> track = mTracks[i];
1526 if (track != 0) {
1527 track->dump(buffer, SIZE);
1528 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 }
1530 }
1531
Eric Laurenta553c252009-07-17 12:17:14 -07001532 snprintf(buffer, SIZE, "Output thread %p active tracks\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 result.append(buffer);
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001534 result.append(" Name Clien Typ Fmt Chn mask Session Buf S M F SRate LeftV RighV Serv User Main buf Aux Buf\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 for (size_t i = 0; i < mActiveTracks.size(); ++i) {
Glenn Kastene4787422012-01-25 14:27:41 -08001536 sp<Track> track = mActiveTracks[i].promote();
1537 if (track != 0) {
1538 track->dump(buffer, SIZE);
1539 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 }
1541 }
1542 write(fd, result.string(), result.size());
1543 return NO_ERROR;
1544}
1545
Eric Laurenta553c252009-07-17 12:17:14 -07001546status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547{
1548 const size_t SIZE = 256;
1549 char buffer[SIZE];
1550 String8 result;
1551
Eric Laurent3fdb1262009-11-07 00:01:32 -08001552 snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 result.append(buffer);
1554 snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime));
1555 result.append(buffer);
1556 snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites);
1557 result.append(buffer);
1558 snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites);
1559 result.append(buffer);
1560 snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite);
1561 result.append(buffer);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08001562 snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended);
1563 result.append(buffer);
Eric Laurent65b65452010-06-01 23:49:17 -07001564 snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer);
1565 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 write(fd, result.string(), result.size());
Eric Laurent3fdb1262009-11-07 00:01:32 -08001567
1568 dumpBase(fd, args);
1569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 return NO_ERROR;
1571}
1572
1573// Thread virtuals
Eric Laurenta553c252009-07-17 12:17:14 -07001574status_t AudioFlinger::PlaybackThread::readyToRun()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575{
Eric Laurent828b9772011-08-07 16:32:26 -07001576 status_t status = initCheck();
1577 if (status == NO_ERROR) {
Steve Block6215d3f2012-01-04 20:05:49 +00001578 ALOGI("AudioFlinger's thread %p ready to run", this);
Eric Laurent828b9772011-08-07 16:32:26 -07001579 } else {
Steve Block3762c312012-01-06 19:20:56 +00001580 ALOGE("No working audio driver found.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
Eric Laurent828b9772011-08-07 16:32:26 -07001582 return status;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583}
1584
Eric Laurenta553c252009-07-17 12:17:14 -07001585void AudioFlinger::PlaybackThread::onFirstRef()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586{
Eric Laurent6dbdc402011-07-22 09:04:31 -07001587 run(mName, ANDROID_PRIORITY_URGENT_AUDIO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588}
1589
Eric Laurenta553c252009-07-17 12:17:14 -07001590// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
Glenn Kastenf743e1f2012-03-14 12:56:26 -07001591sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 const sp<AudioFlinger::Client>& client,
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08001593 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08001595 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001596 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 int frameCount,
1598 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -07001599 int sessionId,
John Grossmand8cf2962012-02-08 16:37:41 -08001600 bool isTimed,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 status_t *status)
1602{
1603 sp<Track> track;
1604 status_t lStatus;
Eric Laurenta553c252009-07-17 12:17:14 -07001605
1606 if (mType == DIRECT) {
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001607 if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) {
1608 if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
Steve Block3762c312012-01-06 19:20:56 +00001609 ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x \""
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001610 "for output %p with format %d",
1611 sampleRate, format, channelMask, mOutput, mFormat);
1612 lStatus = BAD_VALUE;
1613 goto Exit;
1614 }
Eric Laurenta553c252009-07-17 12:17:14 -07001615 }
1616 } else {
1617 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
1618 if (sampleRate > mSampleRate*2) {
Steve Block3762c312012-01-06 19:20:56 +00001619 ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate);
Eric Laurenta553c252009-07-17 12:17:14 -07001620 lStatus = BAD_VALUE;
1621 goto Exit;
1622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 }
1624
Eric Laurent464d5b32011-06-17 21:29:58 -07001625 lStatus = initCheck();
1626 if (lStatus != NO_ERROR) {
Steve Block3762c312012-01-06 19:20:56 +00001627 ALOGE("Audio driver not initialized.");
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001628 goto Exit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 }
1630
Eric Laurenta553c252009-07-17 12:17:14 -07001631 { // scope for mLock
1632 Mutex::Autolock _l(mLock);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001633
1634 // all tracks in same audio session must share the same routing strategy otherwise
1635 // conflicts will happen when tracks are moved from one output to another by audio policy
1636 // manager
Glenn Kastenaae26c82012-02-08 12:35:35 -08001637 uint32_t strategy = AudioSystem::getStrategyForStream(streamType);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001638 for (size_t i = 0; i < mTracks.size(); ++i) {
1639 sp<Track> t = mTracks[i];
Glenn Kasten12f32d62012-03-07 12:26:34 -08001640 if (t != 0 && !t->isOutputTrack()) {
Glenn Kastenaae26c82012-02-08 12:35:35 -08001641 uint32_t actual = AudioSystem::getStrategyForStream(t->streamType());
Glenn Kasten9818a9d2011-10-28 10:31:42 -07001642 if (sessionId == t->sessionId() && strategy != actual) {
Steve Block3762c312012-01-06 19:20:56 +00001643 ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",
Glenn Kasten9818a9d2011-10-28 10:31:42 -07001644 strategy, actual);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001645 lStatus = BAD_VALUE;
1646 goto Exit;
1647 }
1648 }
1649 }
1650
John Grossmand8cf2962012-02-08 16:37:41 -08001651 if (!isTimed) {
1652 track = new Track(this, client, streamType, sampleRate, format,
1653 channelMask, frameCount, sharedBuffer, sessionId);
1654 } else {
1655 track = TimedTrack::create(this, client, streamType, sampleRate, format,
1656 channelMask, frameCount, sharedBuffer, sessionId);
1657 }
1658 if (track == NULL || track->getCblk() == NULL || track->name() < 0) {
Eric Laurenta553c252009-07-17 12:17:14 -07001659 lStatus = NO_MEMORY;
1660 goto Exit;
1661 }
1662 mTracks.add(track);
Eric Laurent65b65452010-06-01 23:49:17 -07001663
1664 sp<EffectChain> chain = getEffectChain_l(sessionId);
1665 if (chain != 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001666 ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
Eric Laurent65b65452010-06-01 23:49:17 -07001667 track->setMainBuffer(chain->inBuffer());
Glenn Kastenaae26c82012-02-08 12:35:35 -08001668 chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType()));
Eric Laurent90681d62011-05-09 12:09:06 -07001669 chain->incTrackCnt();
Eric Laurent65b65452010-06-01 23:49:17 -07001670 }
Eric Laurent05ce0942011-08-30 10:18:54 -07001671
1672 // invalidate track immediately if the stream type was moved to another thread since
1673 // createTrack() was called by the client process.
1674 if (!mStreamTypes[streamType].valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00001675 ALOGW("createTrack_l() on thread %p: invalidating track on stream %d",
Glenn Kasten18db49a2012-03-12 16:29:55 -07001676 this, streamType);
Eric Laurent05ce0942011-08-30 10:18:54 -07001677 android_atomic_or(CBLK_INVALID_ON, &track->mCblk->flags);
1678 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001679 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001680 lStatus = NO_ERROR;
1681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682Exit:
Glenn Kasten18db49a2012-03-12 16:29:55 -07001683 if (status) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 *status = lStatus;
1685 }
1686 return track;
1687}
1688
Eric Laurenta553c252009-07-17 12:17:14 -07001689uint32_t AudioFlinger::PlaybackThread::latency() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690{
Eric Laurent828b9772011-08-07 16:32:26 -07001691 Mutex::Autolock _l(mLock);
1692 if (initCheck() == NO_ERROR) {
Dima Zavin31f188892011-04-18 16:57:27 -07001693 return mOutput->stream->get_latency(mOutput->stream);
Eric Laurent828b9772011-08-07 16:32:26 -07001694 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 return 0;
1696 }
1697}
1698
Glenn Kasten8b02b992012-01-09 09:40:36 -08001699void AudioFlinger::PlaybackThread::setMasterVolume(float value)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700{
Glenn Kasten8b02b992012-01-09 09:40:36 -08001701 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 mMasterVolume = value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703}
1704
Glenn Kasten8b02b992012-01-09 09:40:36 -08001705void AudioFlinger::PlaybackThread::setMasterMute(bool muted)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706{
Glenn Kasten8b02b992012-01-09 09:40:36 -08001707 Mutex::Autolock _l(mLock);
1708 setMasterMute_l(muted);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709}
1710
Glenn Kasten8b02b992012-01-09 09:40:36 -08001711void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712{
Glenn Kasten8b02b992012-01-09 09:40:36 -08001713 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 mStreamTypes[stream].volume = value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715}
1716
Glenn Kasten8b02b992012-01-09 09:40:36 -08001717void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718{
Glenn Kasten8b02b992012-01-09 09:40:36 -08001719 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 mStreamTypes[stream].mute = muted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721}
1722
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08001723float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724{
Glenn Kasten8b02b992012-01-09 09:40:36 -08001725 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 return mStreamTypes[stream].volume;
1727}
1728
Eric Laurenta553c252009-07-17 12:17:14 -07001729// addTrack_l() must be called with ThreadBase::mLock held
1730status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731{
1732 status_t status = ALREADY_EXISTS;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001733
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001734 // set retry count for buffer fill
1735 track->mRetryCount = kMaxTrackStartupRetries;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001736 if (mActiveTracks.indexOf(track) < 0) {
1737 // the track is newly added, make sure it fills up all its
1738 // buffers before playing. This is to ensure the client will
1739 // effectively get the latency it requested.
1740 track->mFillingUpStatus = Track::FS_FILLING;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08001741 track->mResetDone = false;
Eric Laurenta553c252009-07-17 12:17:14 -07001742 mActiveTracks.add(track);
Eric Laurent65b65452010-06-01 23:49:17 -07001743 if (track->mainBuffer() != mMixBuffer) {
1744 sp<EffectChain> chain = getEffectChain_l(track->sessionId());
1745 if (chain != 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001746 ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId());
Eric Laurent90681d62011-05-09 12:09:06 -07001747 chain->incActiveTrackCnt();
Eric Laurent65b65452010-06-01 23:49:17 -07001748 }
1749 }
1750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 status = NO_ERROR;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001752 }
Eric Laurenta553c252009-07-17 12:17:14 -07001753
Steve Block71f2cf12011-10-20 11:56:00 +01001754 ALOGV("mWaitWorkCV.broadcast");
Eric Laurenta553c252009-07-17 12:17:14 -07001755 mWaitWorkCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756
1757 return status;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001758}
1759
Eric Laurenta553c252009-07-17 12:17:14 -07001760// destroyTrack_l() must be called with ThreadBase::mLock held
1761void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001762{
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001763 track->mState = TrackBase::TERMINATED;
1764 if (mActiveTracks.indexOf(track) < 0) {
Eric Laurent90681d62011-05-09 12:09:06 -07001765 removeTrack_l(track);
1766 }
1767}
1768
1769void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track)
1770{
1771 mTracks.remove(track);
1772 deleteTrackName_l(track->name());
1773 sp<EffectChain> chain = getEffectChain_l(track->sessionId());
1774 if (chain != 0) {
1775 chain->decTrackCnt();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001776 }
1777}
1778
Eric Laurenta553c252009-07-17 12:17:14 -07001779String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
The Android Open Source Projectf1e484a2009-01-22 00:13:42 -08001780{
Eric Laurent828b9772011-08-07 16:32:26 -07001781 String8 out_s8 = String8("");
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001782 char *s;
1783
Eric Laurent828b9772011-08-07 16:32:26 -07001784 Mutex::Autolock _l(mLock);
1785 if (initCheck() != NO_ERROR) {
1786 return out_s8;
1787 }
1788
Dima Zavin31f188892011-04-18 16:57:27 -07001789 s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string());
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001790 out_s8 = String8(s);
1791 free(s);
1792 return out_s8;
Eric Laurenta553c252009-07-17 12:17:14 -07001793}
The Android Open Source Projectf1e484a2009-01-22 00:13:42 -08001794
Eric Laurent828b9772011-08-07 16:32:26 -07001795// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001796void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) {
Eric Laurenta553c252009-07-17 12:17:14 -07001797 AudioSystem::OutputDescriptor desc;
Glenn Kasten3694ec12012-01-27 16:47:15 -08001798 void *param2 = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07001799
Steve Block71f2cf12011-10-20 11:56:00 +01001800 ALOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param);
Eric Laurenta553c252009-07-17 12:17:14 -07001801
1802 switch (event) {
1803 case AudioSystem::OUTPUT_OPENED:
1804 case AudioSystem::OUTPUT_CONFIG_CHANGED:
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001805 desc.channels = mChannelMask;
Eric Laurenta553c252009-07-17 12:17:14 -07001806 desc.samplingRate = mSampleRate;
1807 desc.format = mFormat;
1808 desc.frameCount = mFrameCount;
1809 desc.latency = latency();
1810 param2 = &desc;
1811 break;
1812
1813 case AudioSystem::STREAM_CONFIG_CHANGED:
1814 param2 = &param;
1815 case AudioSystem::OUTPUT_CLOSED:
1816 default:
1817 break;
1818 }
Eric Laurent49f02be2009-11-19 09:00:56 -08001819 mAudioFlinger->audioConfigChanged_l(event, mId, param2);
Eric Laurenta553c252009-07-17 12:17:14 -07001820}
1821
1822void AudioFlinger::PlaybackThread::readOutputParameters()
1823{
Dima Zavin31f188892011-04-18 16:57:27 -07001824 mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common);
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001825 mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common);
1826 mChannelCount = (uint16_t)popcount(mChannelMask);
Dima Zavin31f188892011-04-18 16:57:27 -07001827 mFormat = mOutput->stream->common.get_format(&mOutput->stream->common);
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001828 mFrameSize = audio_stream_frame_size(&mOutput->stream->common);
Dima Zavin31f188892011-04-18 16:57:27 -07001829 mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize;
Eric Laurenta553c252009-07-17 12:17:14 -07001830
Eric Laurenta553c252009-07-17 12:17:14 -07001831 // FIXME - Current mixer implementation only supports stereo output: Always
1832 // Allocate a stereo buffer even if HW output is mono.
Glenn Kasten2589cf92012-01-27 18:08:45 -08001833 delete[] mMixBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -07001834 mMixBuffer = new int16_t[mFrameCount * 2];
1835 memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t));
Eric Laurent65b65452010-06-01 23:49:17 -07001836
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001837 // force reconfiguration of effect chains and engines to take new buffer size and audio
1838 // parameters into account
1839 // Note that mLock is not held when readOutputParameters() is called from the constructor
1840 // but in this case nothing is done below as no audio sessions have effect yet so it doesn't
1841 // matter.
1842 // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains
1843 Vector< sp<EffectChain> > effectChains = mEffectChains;
1844 for (size_t i = 0; i < effectChains.size(); i ++) {
Eric Laurent493941b2010-07-28 01:32:47 -07001845 mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001846 }
Eric Laurenta553c252009-07-17 12:17:14 -07001847}
1848
Eric Laurent0986e792010-01-19 17:37:09 -08001849status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames)
1850{
Glenn Kasten3694ec12012-01-27 16:47:15 -08001851 if (halFrames == NULL || dspFrames == NULL) {
Eric Laurent0986e792010-01-19 17:37:09 -08001852 return BAD_VALUE;
1853 }
Eric Laurent828b9772011-08-07 16:32:26 -07001854 Mutex::Autolock _l(mLock);
Eric Laurent464d5b32011-06-17 21:29:58 -07001855 if (initCheck() != NO_ERROR) {
Eric Laurent0986e792010-01-19 17:37:09 -08001856 return INVALID_OPERATION;
1857 }
Dima Zavin31f188892011-04-18 16:57:27 -07001858 *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common);
Eric Laurent0986e792010-01-19 17:37:09 -08001859
Dima Zavin31f188892011-04-18 16:57:27 -07001860 return mOutput->stream->get_render_position(mOutput->stream, dspFrames);
Eric Laurent0986e792010-01-19 17:37:09 -08001861}
1862
Eric Laurent493941b2010-07-28 01:32:47 -07001863uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId)
Eric Laurent65b65452010-06-01 23:49:17 -07001864{
1865 Mutex::Autolock _l(mLock);
Eric Laurent493941b2010-07-28 01:32:47 -07001866 uint32_t result = 0;
Eric Laurent65b65452010-06-01 23:49:17 -07001867 if (getEffectChain_l(sessionId) != 0) {
Eric Laurent493941b2010-07-28 01:32:47 -07001868 result = EFFECT_SESSION;
Eric Laurent65b65452010-06-01 23:49:17 -07001869 }
1870
1871 for (size_t i = 0; i < mTracks.size(); ++i) {
1872 sp<Track> track = mTracks[i];
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001873 if (sessionId == track->sessionId() &&
1874 !(track->mCblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent493941b2010-07-28 01:32:47 -07001875 result |= TRACK_SESSION;
1876 break;
Eric Laurent65b65452010-06-01 23:49:17 -07001877 }
1878 }
1879
Eric Laurent493941b2010-07-28 01:32:47 -07001880 return result;
Eric Laurent65b65452010-06-01 23:49:17 -07001881}
1882
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001883uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId)
1884{
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001885 // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001886 // it is moved to correct output by audio policy manager when A2DP is connected or disconnected
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001887 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
1888 return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001889 }
1890 for (size_t i = 0; i < mTracks.size(); i++) {
1891 sp<Track> track = mTracks[i];
1892 if (sessionId == track->sessionId() &&
1893 !(track->mCblk->flags & CBLK_INVALID_MSK)) {
Glenn Kastenaae26c82012-02-08 12:35:35 -08001894 return AudioSystem::getStrategyForStream(track->streamType());
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001895 }
1896 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001897 return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001898}
1899
Eric Laurent53334cd2010-06-23 17:38:20 -07001900
Glenn Kasten5b0135e2012-01-26 09:46:34 -08001901AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const
Eric Laurent828b9772011-08-07 16:32:26 -07001902{
1903 Mutex::Autolock _l(mLock);
1904 return mOutput;
1905}
1906
1907AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput()
1908{
1909 Mutex::Autolock _l(mLock);
1910 AudioStreamOut *output = mOutput;
1911 mOutput = NULL;
1912 return output;
1913}
1914
1915// this method must always be called either with ThreadBase mLock held or inside the thread loop
1916audio_stream_t* AudioFlinger::PlaybackThread::stream()
1917{
1918 if (mOutput == NULL) {
1919 return NULL;
1920 }
1921 return &mOutput->stream->common;
1922}
1923
Eric Laurent44331692011-12-05 09:47:19 -08001924uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs()
1925{
1926 // A2DP output latency is not due only to buffering capacity. It also reflects encoding,
1927 // decoding and transfer time. So sleeping for half of the latency would likely cause
1928 // underruns
1929 if (audio_is_a2dp_device((audio_devices_t)mDevice)) {
1930 return (uint32_t)((uint32_t)((mFrameCount * 1000) / mSampleRate) * 1000);
1931 } else {
1932 return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2;
1933 }
1934}
1935
Eric Laurenta553c252009-07-17 12:17:14 -07001936// ----------------------------------------------------------------------------
1937
Glenn Kastenefd511a2012-01-26 10:38:26 -08001938AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08001939 audio_io_handle_t id, uint32_t device, type_t type)
Glenn Kasten67cb3122012-03-01 17:10:56 -08001940 : PlaybackThread(audioFlinger, output, id, device, type)
Eric Laurenta553c252009-07-17 12:17:14 -07001941{
Glenn Kasten67cb3122012-03-01 17:10:56 -08001942 mAudioMixer = new AudioMixer(mFrameCount, mSampleRate);
Eric Laurenta553c252009-07-17 12:17:14 -07001943 // FIXME - Current mixer implementation only supports stereo output
1944 if (mChannelCount == 1) {
Steve Block3762c312012-01-06 19:20:56 +00001945 ALOGE("Invalid audio hardware channel count");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 }
The Android Open Source Projectf1e484a2009-01-22 00:13:42 -08001947}
1948
Eric Laurenta553c252009-07-17 12:17:14 -07001949AudioFlinger::MixerThread::~MixerThread()
The Android Open Source Projectf1e484a2009-01-22 00:13:42 -08001950{
Eric Laurenta553c252009-07-17 12:17:14 -07001951 delete mAudioMixer;
1952}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953
Glenn Kasten6ec27552012-02-24 07:21:32 -08001954class CpuStats {
1955public:
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08001956 CpuStats();
1957 void sample(const String8 &title);
Glenn Kasten6ec27552012-02-24 07:21:32 -08001958#ifdef DEBUG_CPU_USAGE
1959private:
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08001960 ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns
1961 CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns
1962
1963 CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles
1964
1965 int mCpuNum; // thread's current CPU number
1966 int mCpukHz; // frequency of thread's current CPU in kHz
Glenn Kasten6ec27552012-02-24 07:21:32 -08001967#endif
1968};
1969
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08001970CpuStats::CpuStats()
Glenn Kasten6ec27552012-02-24 07:21:32 -08001971#ifdef DEBUG_CPU_USAGE
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08001972 : mCpuNum(-1), mCpukHz(-1)
1973#endif
1974{
1975}
1976
1977void CpuStats::sample(const String8 &title) {
1978#ifdef DEBUG_CPU_USAGE
1979 // get current thread's delta CPU time in wall clock ns
1980 double wcNs;
1981 bool valid = mCpuUsage.sampleAndEnable(wcNs);
1982
1983 // record sample for wall clock statistics
1984 if (valid) {
1985 mWcStats.sample(wcNs);
1986 }
1987
1988 // get the current CPU number
1989 int cpuNum = sched_getcpu();
1990
1991 // get the current CPU frequency in kHz
1992 int cpukHz = mCpuUsage.getCpukHz(cpuNum);
1993
1994 // check if either CPU number or frequency changed
1995 if (cpuNum != mCpuNum || cpukHz != mCpukHz) {
1996 mCpuNum = cpuNum;
1997 mCpukHz = cpukHz;
1998 // ignore sample for purposes of cycles
1999 valid = false;
2000 }
2001
2002 // if no change in CPU number or frequency, then record sample for cycle statistics
2003 if (valid && mCpukHz > 0) {
2004 double cycles = wcNs * cpukHz * 0.000001;
2005 mHzStats.sample(cycles);
2006 }
2007
2008 unsigned n = mWcStats.n();
2009 // mCpuUsage.elapsed() is expensive, so don't call it every loop
Glenn Kasten6ec27552012-02-24 07:21:32 -08002010 if ((n & 127) == 1) {
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002011 long long elapsed = mCpuUsage.elapsed();
Glenn Kasten6ec27552012-02-24 07:21:32 -08002012 if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) {
2013 double perLoop = elapsed / (double) n;
2014 double perLoop100 = perLoop * 0.01;
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002015 double perLoop1k = perLoop * 0.001;
2016 double mean = mWcStats.mean();
2017 double stddev = mWcStats.stddev();
2018 double minimum = mWcStats.minimum();
2019 double maximum = mWcStats.maximum();
2020 double meanCycles = mHzStats.mean();
2021 double stddevCycles = mHzStats.stddev();
2022 double minCycles = mHzStats.minimum();
2023 double maxCycles = mHzStats.maximum();
2024 mCpuUsage.resetElapsed();
2025 mWcStats.reset();
2026 mHzStats.reset();
2027 ALOGD("CPU usage for %s over past %.1f secs\n"
2028 " (%u mixer loops at %.1f mean ms per loop):\n"
2029 " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n"
2030 " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n"
2031 " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f",
2032 title.string(),
Glenn Kasten6ec27552012-02-24 07:21:32 -08002033 elapsed * .000000001, n, perLoop * .000001,
2034 mean * .001,
2035 stddev * .001,
2036 minimum * .001,
2037 maximum * .001,
2038 mean / perLoop100,
2039 stddev / perLoop100,
2040 minimum / perLoop100,
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002041 maximum / perLoop100,
2042 meanCycles / perLoop1k,
2043 stddevCycles / perLoop1k,
2044 minCycles / perLoop1k,
2045 maxCycles / perLoop1k);
2046
Glenn Kasten6ec27552012-02-24 07:21:32 -08002047 }
2048 }
2049#endif
2050};
2051
Glenn Kasten2521a012012-02-24 07:21:48 -08002052void AudioFlinger::PlaybackThread::checkSilentMode_l()
2053{
2054 if (!mMasterMute) {
2055 char value[PROPERTY_VALUE_MAX];
2056 if (property_get("ro.audio.silent", value, "0") > 0) {
2057 char *endptr;
2058 unsigned long ul = strtoul(value, &endptr, 0);
2059 if (*endptr == '\0' && ul != 0) {
2060 ALOGD("Silence is golden");
2061 // The setprop command will not allow a property to be changed after
2062 // the first time it is set, so we don't have to worry about un-muting.
2063 setMasterMute_l(true);
2064 }
2065 }
2066 }
2067}
2068
Glenn Kasten67cb3122012-03-01 17:10:56 -08002069bool AudioFlinger::PlaybackThread::threadLoop()
Eric Laurenta553c252009-07-17 12:17:14 -07002070{
Eric Laurenta553c252009-07-17 12:17:14 -07002071 Vector< sp<Track> > tracksToRemove;
Glenn Kastenbea6b952012-02-29 07:57:06 -08002072
Glenn Kasten67cb3122012-03-01 17:10:56 -08002073 standbyTime = systemTime();
Glenn Kasten67cb3122012-03-01 17:10:56 -08002074
2075 // MIXER
Dave Sparksd0ac8c02009-09-30 03:09:03 -07002076 nsecs_t lastWarning = 0;
Glenn Kasten67cb3122012-03-01 17:10:56 -08002077if (mType == MIXER) {
2078 longStandbyExit = false;
2079}
Glenn Kastenbea6b952012-02-29 07:57:06 -08002080
Glenn Kasten67cb3122012-03-01 17:10:56 -08002081 // DUPLICATING
2082 // FIXME could this be made local to while loop?
2083 writeFrames = 0;
Glenn Kastenbea6b952012-02-29 07:57:06 -08002084
Glenn Kasten0fe52c82012-02-24 14:59:21 -08002085 cacheParameters_l();
Glenn Kasten67cb3122012-03-01 17:10:56 -08002086 sleepTime = idleSleepTime;
2087
2088if (mType == MIXER) {
2089 sleepTimeShift = 0;
2090}
2091
Glenn Kasten6ec27552012-02-24 07:21:32 -08002092 CpuStats cpuStats;
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002093 const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094
Eric Laurent6dbdc402011-07-22 09:04:31 -07002095 acquireWakeLock();
2096
Eric Laurenta553c252009-07-17 12:17:14 -07002097 while (!exitPending())
2098 {
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002099 cpuStats.sample(myName);
Glenn Kastenbea6b952012-02-29 07:57:06 -08002100
Glenn Kasten3c4f0dc2012-02-29 07:56:15 -08002101 Vector< sp<EffectChain> > effectChains;
2102
Eric Laurenta553c252009-07-17 12:17:14 -07002103 processConfigEvents();
2104
Eric Laurenta553c252009-07-17 12:17:14 -07002105 { // scope for mLock
2106
2107 Mutex::Autolock _l(mLock);
2108
2109 if (checkForNewParameters_l()) {
Glenn Kasten0fe52c82012-02-24 14:59:21 -08002110 cacheParameters_l();
Eric Laurenta553c252009-07-17 12:17:14 -07002111 }
2112
Glenn Kasten08d2c042012-03-06 11:28:04 -08002113 saveOutputTracks();
Glenn Kasten67cb3122012-03-01 17:10:56 -08002114
Eric Laurenta553c252009-07-17 12:17:14 -07002115 // put audio hardware into standby after short delay
Glenn Kastend9cccfa2012-02-28 18:40:35 -08002116 if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) ||
Glenn Kastene70583e2012-02-29 07:07:30 -08002117 mSuspended > 0)) {
Eric Laurenta553c252009-07-17 12:17:14 -07002118 if (!mStandby) {
Glenn Kasten67cb3122012-03-01 17:10:56 -08002119
2120 threadLoop_standby();
2121
Eric Laurenta553c252009-07-17 12:17:14 -07002122 mStandby = true;
2123 mBytesWritten = 0;
2124 }
2125
Glenn Kastend9cccfa2012-02-28 18:40:35 -08002126 if (!mActiveTracks.size() && mConfigEvents.isEmpty()) {
Eric Laurenta553c252009-07-17 12:17:14 -07002127 // we're about to wait, flush the binder command buffer
2128 IPCThreadState::self()->flushCommands();
2129
Glenn Kasten08d2c042012-03-06 11:28:04 -08002130 clearOutputTracks();
Glenn Kasten67cb3122012-03-01 17:10:56 -08002131
Eric Laurenta553c252009-07-17 12:17:14 -07002132 if (exitPending()) break;
2133
Eric Laurent6dbdc402011-07-22 09:04:31 -07002134 releaseWakeLock_l();
Eric Laurenta553c252009-07-17 12:17:14 -07002135 // wait until we have something to do...
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002136 ALOGV("%s going to sleep", myName.string());
Eric Laurenta553c252009-07-17 12:17:14 -07002137 mWaitWorkCV.wait(mLock);
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002138 ALOGV("%s waking up", myName.string());
Eric Laurent6dbdc402011-07-22 09:04:31 -07002139 acquireWakeLock_l();
Eric Laurenta553c252009-07-17 12:17:14 -07002140
Eric Laurent71c44962012-01-17 19:20:12 -08002141 mPrevMixerStatus = MIXER_IDLE;
Glenn Kasten67cb3122012-03-01 17:10:56 -08002142
Glenn Kasten2521a012012-02-24 07:21:48 -08002143 checkSilentMode_l();
Eric Laurenta553c252009-07-17 12:17:14 -07002144
Glenn Kasten67cb3122012-03-01 17:10:56 -08002145 standbyTime = systemTime() + standbyDelay;
Eric Laurent059b4be2009-11-09 23:32:22 -08002146 sleepTime = idleSleepTime;
Glenn Kasten0fe52c82012-02-24 14:59:21 -08002147 if (mType == MIXER) {
2148 sleepTimeShift = 0;
2149 }
Glenn Kasten67cb3122012-03-01 17:10:56 -08002150
Eric Laurenta553c252009-07-17 12:17:14 -07002151 continue;
2152 }
2153 }
2154
Glenn Kastena13446a2012-03-08 07:47:15 -08002155 mixer_state newMixerStatus = prepareTracks_l(&tracksToRemove);
2156 // Shift in the new status; this could be a queue if it's
2157 // useful to filter the mixer status over several cycles.
2158 mPrevMixerStatus = mMixerStatus;
2159 mMixerStatus = newMixerStatus;
Eric Laurent65b65452010-06-01 23:49:17 -07002160
2161 // prevent any changes in effect chain list and in each effect chain
2162 // during mixing and effect process as the audio buffers could be deleted
2163 // or modified if an effect is created or deleted
Eric Laurent8ed6ed02010-07-13 04:45:46 -07002164 lockEffectChains_l(effectChains);
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -08002165 }
Eric Laurenta553c252009-07-17 12:17:14 -07002166
Glenn Kastena13446a2012-03-08 07:47:15 -08002167 if (CC_LIKELY(mMixerStatus == MIXER_TRACKS_READY)) {
Glenn Kasten67cb3122012-03-01 17:10:56 -08002168 threadLoop_mix();
2169 } else {
2170 threadLoop_sleepTime();
2171 }
2172
2173 if (mSuspended > 0) {
2174 sleepTime = suspendSleepTimeUs();
2175 }
2176
2177 // only process effects if we're going to write
2178 if (sleepTime == 0) {
Glenn Kasten67cb3122012-03-01 17:10:56 -08002179 for (size_t i = 0; i < effectChains.size(); i ++) {
2180 effectChains[i]->process_l();
2181 }
2182 }
2183
2184 // enable changes in effect chain
2185 unlockEffectChains(effectChains);
2186
2187 // sleepTime == 0 means we must write to audio hardware
2188 if (sleepTime == 0) {
2189
2190 threadLoop_write();
2191
2192if (mType == MIXER) {
2193 // write blocked detection
2194 nsecs_t now = systemTime();
2195 nsecs_t delta = now - mLastWriteTime;
2196 if (!mStandby && delta > maxPeriod) {
2197 mNumDelayedWrites++;
2198 if ((now - lastWarning) > kWarningThrottleNs) {
2199 ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p",
2200 ns2ms(delta), mNumDelayedWrites, this);
2201 lastWarning = now;
2202 }
2203 // FIXME this is broken: longStandbyExit should be handled out of the if() and with
2204 // a different threshold. Or completely removed for what it is worth anyway...
2205 if (mStandby) {
2206 longStandbyExit = true;
2207 }
2208 }
2209}
2210
2211 mStandby = false;
2212 } else {
2213 usleep(sleepTime);
2214 }
2215
2216 // finally let go of removed track(s), without the lock held
2217 // since we can't guarantee the destructors won't acquire that
2218 // same lock.
Glenn Kasten761415b2012-03-06 11:23:32 -08002219 tracksToRemove.clear();
Glenn Kasten67cb3122012-03-01 17:10:56 -08002220
Glenn Kasten08d2c042012-03-06 11:28:04 -08002221 // FIXME I don't understand the need for this here;
2222 // it was in the original code but maybe the
2223 // assignment in saveOutputTracks() makes this unnecessary?
2224 clearOutputTracks();
Glenn Kasten67cb3122012-03-01 17:10:56 -08002225
2226 // Effect chains will be actually deleted here if they were removed from
2227 // mEffectChains list during mixing or effects processing
2228 effectChains.clear();
2229
2230 // FIXME Note that the above .clear() is no longer necessary since effectChains
2231 // is now local to this block, but will keep it for now (at least until merge done).
2232 }
2233
2234if (mType == MIXER || mType == DIRECT) {
2235 // put output stream into standby mode
2236 if (!mStandby) {
2237 mOutput->stream->common.standby(&mOutput->stream->common);
2238 }
2239}
2240if (mType == DUPLICATING) {
2241 // for DuplicatingThread, standby mode is handled by the outputTracks
2242}
2243
2244 releaseWakeLock();
2245
2246 ALOGV("Thread %p type %d exiting", this, mType);
2247 return false;
2248}
2249
2250// shared by MIXER and DIRECT, overridden by DUPLICATING
2251void AudioFlinger::PlaybackThread::threadLoop_write()
2252{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002253 // FIXME rewrite to reduce number of system calls
2254 mLastWriteTime = systemTime();
2255 mInWrite = true;
2256 mBytesWritten += mixBufferSize;
2257 int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize);
2258 if (bytesWritten < 0) mBytesWritten -= mixBufferSize;
2259 mNumWrites++;
2260 mInWrite = false;
Glenn Kasten67cb3122012-03-01 17:10:56 -08002261}
2262
2263// shared by MIXER and DIRECT, overridden by DUPLICATING
2264void AudioFlinger::PlaybackThread::threadLoop_standby()
2265{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002266 ALOGV("Audio hardware entering standby, mixer %p, suspend count %u", this, mSuspended);
2267 mOutput->stream->common.standby(&mOutput->stream->common);
Glenn Kasten67cb3122012-03-01 17:10:56 -08002268}
2269
2270void AudioFlinger::MixerThread::threadLoop_mix()
2271{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002272 // obtain the presentation timestamp of the next output buffer
2273 int64_t pts;
2274 status_t status = INVALID_OPERATION;
John Grossmand8cf2962012-02-08 16:37:41 -08002275
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002276 if (NULL != mOutput->stream->get_next_write_timestamp) {
2277 status = mOutput->stream->get_next_write_timestamp(
2278 mOutput->stream, &pts);
2279 }
John Grossmand8cf2962012-02-08 16:37:41 -08002280
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002281 if (status != NO_ERROR) {
2282 pts = AudioBufferProvider::kInvalidPTS;
2283 }
John Grossmand8cf2962012-02-08 16:37:41 -08002284
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002285 // mix buffers...
2286 mAudioMixer->process(pts);
2287 // increase sleep time progressively when application underrun condition clears.
2288 // Only increase sleep time if the mixer is ready for two consecutive times to avoid
2289 // that a steady state of alternating ready/not ready conditions keeps the sleep time
2290 // such that we would underrun the audio HAL.
2291 if ((sleepTime == 0) && (sleepTimeShift > 0)) {
2292 sleepTimeShift--;
2293 }
2294 sleepTime = 0;
Glenn Kasten0fe52c82012-02-24 14:59:21 -08002295 standbyTime = systemTime() + standbyDelay;
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002296 //TODO: delay standby when effects have a tail
Glenn Kasten67cb3122012-03-01 17:10:56 -08002297}
2298
2299void AudioFlinger::MixerThread::threadLoop_sleepTime()
2300{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002301 // If no tracks are ready, sleep once for the duration of an output
2302 // buffer size, then write 0s to the output
2303 if (sleepTime == 0) {
Glenn Kastena13446a2012-03-08 07:47:15 -08002304 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002305 sleepTime = activeSleepTime >> sleepTimeShift;
2306 if (sleepTime < kMinThreadSleepTimeUs) {
2307 sleepTime = kMinThreadSleepTimeUs;
Eric Laurent96c08a62009-09-07 08:38:38 -07002308 }
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002309 // reduce sleep time in case of consecutive application underruns to avoid
2310 // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer
2311 // duration we would end up writing less data than needed by the audio HAL if
2312 // the condition persists.
2313 if (sleepTimeShift < kMaxThreadSleepTimeShift) {
2314 sleepTimeShift++;
2315 }
2316 } else {
2317 sleepTime = idleSleepTime;
2318 }
2319 } else if (mBytesWritten != 0 ||
Glenn Kastena13446a2012-03-08 07:47:15 -08002320 (mMixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) {
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002321 memset (mMixBuffer, 0, mixBufferSize);
2322 sleepTime = 0;
Glenn Kastena13446a2012-03-08 07:47:15 -08002323 ALOGV_IF((mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start");
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002324 }
2325 // TODO add standby time extension fct of effect tail
Eric Laurenta553c252009-07-17 12:17:14 -07002326}
2327
2328// prepareTracks_l() must be called with ThreadBase::mLock held
Glenn Kasten789fef12012-01-26 13:37:52 -08002329AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l(
Glenn Kastend9cccfa2012-02-28 18:40:35 -08002330 Vector< sp<Track> > *tracksToRemove)
Eric Laurenta553c252009-07-17 12:17:14 -07002331{
2332
Glenn Kasten789fef12012-01-26 13:37:52 -08002333 mixer_state mixerStatus = MIXER_IDLE;
Eric Laurenta553c252009-07-17 12:17:14 -07002334 // find out which tracks need to be processed
Glenn Kastend9cccfa2012-02-28 18:40:35 -08002335 size_t count = mActiveTracks.size();
Eric Laurent65b65452010-06-01 23:49:17 -07002336 size_t mixedTracks = 0;
2337 size_t tracksWithEffect = 0;
Glenn Kasten871c16c2010-03-05 12:18:01 -08002338
2339 float masterVolume = mMasterVolume;
Glenn Kastenf743e1f2012-03-14 12:56:26 -07002340 bool masterMute = mMasterMute;
Glenn Kasten871c16c2010-03-05 12:18:01 -08002341
Eric Laurent8cc93b92010-08-11 05:20:11 -07002342 if (masterMute) {
2343 masterVolume = 0;
2344 }
Eric Laurent65b65452010-06-01 23:49:17 -07002345 // Delegate master volume control to effect in output mix effect chain if needed
Dima Zavin24fc2fb2011-04-19 22:30:36 -07002346 sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent65b65452010-06-01 23:49:17 -07002347 if (chain != 0) {
Eric Laurent8cc93b92010-08-11 05:20:11 -07002348 uint32_t v = (uint32_t)(masterVolume * (1 << 24));
Eric Laurent76c40f72010-07-15 12:50:15 -07002349 chain->setVolume_l(&v, &v);
Eric Laurent65b65452010-06-01 23:49:17 -07002350 masterVolume = (float)((v + (1 << 23)) >> 24);
2351 chain.clear();
2352 }
Glenn Kasten871c16c2010-03-05 12:18:01 -08002353
Eric Laurenta553c252009-07-17 12:17:14 -07002354 for (size_t i=0 ; i<count ; i++) {
Glenn Kastend9cccfa2012-02-28 18:40:35 -08002355 sp<Track> t = mActiveTracks[i].promote();
Eric Laurenta553c252009-07-17 12:17:14 -07002356 if (t == 0) continue;
2357
Glenn Kasten68deb152011-12-19 15:06:39 -08002358 // this const just means the local variable doesn't change
Eric Laurenta553c252009-07-17 12:17:14 -07002359 Track* const track = t.get();
2360 audio_track_cblk_t* cblk = track->cblk();
2361
2362 // The first time a track is added we wait
2363 // for all its buffers to be filled before processing it
Glenn Kasten68deb152011-12-19 15:06:39 -08002364 int name = track->name();
Eric Laurent8a04fe02011-11-08 18:10:16 -08002365 // make sure that we have enough frames to mix one full buffer.
2366 // enforce this condition only once to enable draining the buffer in case the client
2367 // app does not call stop() and relies on underrun to stop:
Eric Laurent71c44962012-01-17 19:20:12 -08002368 // hence the test on (mPrevMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed
Eric Laurent8a04fe02011-11-08 18:10:16 -08002369 // during last round
Eric Laurent19ddf0e2011-11-03 12:16:05 -07002370 uint32_t minFrames = 1;
Eric Laurent8a04fe02011-11-08 18:10:16 -08002371 if (!track->isStopped() && !track->isPausing() &&
Eric Laurent71c44962012-01-17 19:20:12 -08002372 (mPrevMixerStatus == MIXER_TRACKS_READY)) {
Eric Laurent19ddf0e2011-11-03 12:16:05 -07002373 if (t->sampleRate() == (int)mSampleRate) {
2374 minFrames = mFrameCount;
2375 } else {
Eric Laurent72dafb22011-12-22 16:08:41 -08002376 // +1 for rounding and +1 for additional sample needed for interpolation
2377 minFrames = (mFrameCount * t->sampleRate()) / mSampleRate + 1 + 1;
2378 // add frames already consumed but not yet released by the resampler
Glenn Kastenf743e1f2012-03-14 12:56:26 -07002379 // because cblk->framesReady() will include these frames
Eric Laurent72dafb22011-12-22 16:08:41 -08002380 minFrames += mAudioMixer->getUnreleasedFrames(track->name());
2381 // the minimum track buffer size is normally twice the number of frames necessary
2382 // to fill one buffer and the resampler should not leave more than one buffer worth
2383 // of unreleased frames after each pass, but just in case...
Steve Blockec193de2012-01-09 18:35:44 +00002384 ALOG_ASSERT(minFrames <= cblk->frameCount);
Eric Laurent19ddf0e2011-11-03 12:16:05 -07002385 }
2386 }
John Grossmand8cf2962012-02-08 16:37:41 -08002387 if ((track->framesReady() >= minFrames) && track->isReady() &&
Eric Laurent71f37cd2010-03-31 12:21:17 -07002388 !track->isPaused() && !track->isTerminated())
Eric Laurenta553c252009-07-17 12:17:14 -07002389 {
Glenn Kasten68deb152011-12-19 15:06:39 -08002390 //ALOGV("track %d u=%08x, s=%08x [OK] on thread %p", name, cblk->user, cblk->server, this);
Eric Laurenta553c252009-07-17 12:17:14 -07002391
Eric Laurent65b65452010-06-01 23:49:17 -07002392 mixedTracks++;
2393
2394 // track->mainBuffer() != mMixBuffer means there is an effect chain
2395 // connected to the track
2396 chain.clear();
2397 if (track->mainBuffer() != mMixBuffer) {
2398 chain = getEffectChain_l(track->sessionId());
2399 // Delegate volume control to effect in track effect chain if needed
2400 if (chain != 0) {
2401 tracksWithEffect++;
2402 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00002403 ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on session %d",
Glenn Kasten68deb152011-12-19 15:06:39 -08002404 name, track->sessionId());
Eric Laurent65b65452010-06-01 23:49:17 -07002405 }
2406 }
2407
2408
2409 int param = AudioMixer::VOLUME;
2410 if (track->mFillingUpStatus == Track::FS_FILLED) {
2411 // no ramp for the first volume setting
2412 track->mFillingUpStatus = Track::FS_ACTIVE;
2413 if (track->mState == TrackBase::RESUMING) {
2414 track->mState = TrackBase::ACTIVE;
2415 param = AudioMixer::RAMP_VOLUME;
2416 }
Glenn Kasten68deb152011-12-19 15:06:39 -08002417 mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL);
Eric Laurent65b65452010-06-01 23:49:17 -07002418 } else if (cblk->server != 0) {
2419 // If the track is stopped before the first frame was mixed,
2420 // do not apply ramp
2421 param = AudioMixer::RAMP_VOLUME;
2422 }
2423
Eric Laurenta553c252009-07-17 12:17:14 -07002424 // compute volume for this track
Eric Laurent27a2fdf2010-09-10 17:44:44 -07002425 uint32_t vl, vr, va;
Eric Laurent2a6b80b2010-07-29 23:43:43 -07002426 if (track->isMuted() || track->isPausing() ||
Glenn Kastenaae26c82012-02-08 12:35:35 -08002427 mStreamTypes[track->streamType()].mute) {
Eric Laurent27a2fdf2010-09-10 17:44:44 -07002428 vl = vr = va = 0;
Eric Laurenta553c252009-07-17 12:17:14 -07002429 if (track->isPausing()) {
2430 track->setPaused();
2431 }
2432 } else {
Eric Laurent27a2fdf2010-09-10 17:44:44 -07002433
Glenn Kasten871c16c2010-03-05 12:18:01 -08002434 // read original volumes with volume control
Glenn Kastenaae26c82012-02-08 12:35:35 -08002435 float typeVolume = mStreamTypes[track->streamType()].volume;
Glenn Kasten871c16c2010-03-05 12:18:01 -08002436 float v = masterVolume * typeVolume;
Glenn Kastenbc4de882012-01-17 14:39:34 -08002437 uint32_t vlr = cblk->getVolumeLR();
Glenn Kasten0632bad2012-01-17 12:20:54 -08002438 vl = vlr & 0xFFFF;
2439 vr = vlr >> 16;
2440 // track volumes come from shared memory, so can't be trusted and must be clamped
2441 if (vl > MAX_GAIN_INT) {
2442 ALOGV("Track left volume out of range: %04X", vl);
2443 vl = MAX_GAIN_INT;
2444 }
2445 if (vr > MAX_GAIN_INT) {
2446 ALOGV("Track right volume out of range: %04X", vr);
2447 vr = MAX_GAIN_INT;
2448 }
2449 // now apply the master volume and stream type volume
2450 vl = (uint32_t)(v * vl) << 12;
2451 vr = (uint32_t)(v * vr) << 12;
2452 // assuming master volume and stream type volume each go up to 1.0,
2453 // vl and vr are now in 8.24 format
Eric Laurenta553c252009-07-17 12:17:14 -07002454
Glenn Kasten4790bd82012-01-03 14:22:33 -08002455 uint16_t sendLevel = cblk->getSendLevel_U4_12();
2456 // send level comes from shared memory and so may be corrupt
Glenn Kasten4f22c052012-01-27 15:26:23 -08002457 if (sendLevel > MAX_GAIN_INT) {
Glenn Kasten4790bd82012-01-03 14:22:33 -08002458 ALOGV("Track send level out of range: %04X", sendLevel);
Glenn Kasten0632bad2012-01-17 12:20:54 -08002459 sendLevel = MAX_GAIN_INT;
Glenn Kasten4790bd82012-01-03 14:22:33 -08002460 }
2461 va = (uint32_t)(v * sendLevel);
Eric Laurenta553c252009-07-17 12:17:14 -07002462 }
Eric Laurent27a2fdf2010-09-10 17:44:44 -07002463 // Delegate volume control to effect in track effect chain if needed
2464 if (chain != 0 && chain->setVolume_l(&vl, &vr)) {
2465 // Do not ramp volume if volume is controlled by effect
2466 param = AudioMixer::VOLUME;
2467 track->mHasVolumeController = true;
2468 } else {
2469 // force no volume ramp when volume controller was just disabled or removed
2470 // from effect chain to avoid volume spike
2471 if (track->mHasVolumeController) {
2472 param = AudioMixer::VOLUME;
2473 }
2474 track->mHasVolumeController = false;
2475 }
2476
2477 // Convert volumes from 8.24 to 4.12 format
Glenn Kasten0632bad2012-01-17 12:20:54 -08002478 // This additional clamping is needed in case chain->setVolume_l() overshot
Glenn Kasten4f22c052012-01-27 15:26:23 -08002479 vl = (vl + (1 << 11)) >> 12;
2480 if (vl > MAX_GAIN_INT) vl = MAX_GAIN_INT;
2481 vr = (vr + (1 << 11)) >> 12;
2482 if (vr > MAX_GAIN_INT) vr = MAX_GAIN_INT;
Eric Laurent27a2fdf2010-09-10 17:44:44 -07002483
Glenn Kasten4f22c052012-01-27 15:26:23 -08002484 if (va > MAX_GAIN_INT) va = MAX_GAIN_INT; // va is uint32_t, so no need to check for -
Eric Laurent65b65452010-06-01 23:49:17 -07002485
Eric Laurent65b65452010-06-01 23:49:17 -07002486 // XXX: these things DON'T need to be done each time
Glenn Kasten68deb152011-12-19 15:06:39 -08002487 mAudioMixer->setBufferProvider(name, track);
2488 mAudioMixer->enable(name);
Eric Laurent65b65452010-06-01 23:49:17 -07002489
Glenn Kasten4f22c052012-01-27 15:26:23 -08002490 mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)vl);
2491 mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)vr);
2492 mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)va);
Eric Laurenta553c252009-07-17 12:17:14 -07002493 mAudioMixer->setParameter(
Glenn Kasten68deb152011-12-19 15:06:39 -08002494 name,
Eric Laurenta553c252009-07-17 12:17:14 -07002495 AudioMixer::TRACK,
Eric Laurent65b65452010-06-01 23:49:17 -07002496 AudioMixer::FORMAT, (void *)track->format());
Eric Laurenta553c252009-07-17 12:17:14 -07002497 mAudioMixer->setParameter(
Glenn Kasten68deb152011-12-19 15:06:39 -08002498 name,
Eric Laurenta553c252009-07-17 12:17:14 -07002499 AudioMixer::TRACK,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07002500 AudioMixer::CHANNEL_MASK, (void *)track->channelMask());
Eric Laurenta553c252009-07-17 12:17:14 -07002501 mAudioMixer->setParameter(
Glenn Kasten68deb152011-12-19 15:06:39 -08002502 name,
Eric Laurenta553c252009-07-17 12:17:14 -07002503 AudioMixer::RESAMPLE,
2504 AudioMixer::SAMPLE_RATE,
Eric Laurent65b65452010-06-01 23:49:17 -07002505 (void *)(cblk->sampleRate));
2506 mAudioMixer->setParameter(
Glenn Kasten68deb152011-12-19 15:06:39 -08002507 name,
Eric Laurent65b65452010-06-01 23:49:17 -07002508 AudioMixer::TRACK,
2509 AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer());
2510 mAudioMixer->setParameter(
Glenn Kasten68deb152011-12-19 15:06:39 -08002511 name,
Eric Laurent65b65452010-06-01 23:49:17 -07002512 AudioMixer::TRACK,
2513 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer());
Eric Laurenta553c252009-07-17 12:17:14 -07002514
2515 // reset retry count
2516 track->mRetryCount = kMaxTrackRetries;
Glenn Kastenf743e1f2012-03-14 12:56:26 -07002517
Eric Laurent71c44962012-01-17 19:20:12 -08002518 // If one track is ready, set the mixer ready if:
2519 // - the mixer was not ready during previous round OR
2520 // - no other track is not ready
2521 if (mPrevMixerStatus != MIXER_TRACKS_READY ||
2522 mixerStatus != MIXER_TRACKS_ENABLED) {
2523 mixerStatus = MIXER_TRACKS_READY;
2524 }
Eric Laurenta553c252009-07-17 12:17:14 -07002525 } else {
Glenn Kasten68deb152011-12-19 15:06:39 -08002526 //ALOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", name, cblk->user, cblk->server, this);
Eric Laurenta553c252009-07-17 12:17:14 -07002527 if (track->isStopped()) {
2528 track->reset();
2529 }
2530 if (track->isTerminated() || track->isStopped() || track->isPaused()) {
2531 // We have consumed all the buffers of this track.
2532 // Remove it from the list of active tracks.
2533 tracksToRemove->add(track);
Eric Laurenta553c252009-07-17 12:17:14 -07002534 } else {
2535 // No buffers for this track. Give it a few chances to
2536 // fill a buffer, then remove it from active list.
2537 if (--(track->mRetryCount) <= 0) {
Glenn Kasten68deb152011-12-19 15:06:39 -08002538 ALOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this);
Eric Laurenta553c252009-07-17 12:17:14 -07002539 tracksToRemove->add(track);
Eric Laurent4712baa2010-09-30 16:12:31 -07002540 // indicate to client process that the track was disabled because of underrun
Eric Laurentae29b762011-03-28 18:37:07 -07002541 android_atomic_or(CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent71c44962012-01-17 19:20:12 -08002542 // If one track is not ready, mark the mixer also not ready if:
2543 // - the mixer was ready during previous round OR
2544 // - no other track is ready
2545 } else if (mPrevMixerStatus == MIXER_TRACKS_READY ||
2546 mixerStatus != MIXER_TRACKS_READY) {
Eric Laurent059b4be2009-11-09 23:32:22 -08002547 mixerStatus = MIXER_TRACKS_ENABLED;
Eric Laurenta553c252009-07-17 12:17:14 -07002548 }
Eric Laurenta553c252009-07-17 12:17:14 -07002549 }
Glenn Kasten68deb152011-12-19 15:06:39 -08002550 mAudioMixer->disable(name);
Eric Laurenta553c252009-07-17 12:17:14 -07002551 }
2552 }
2553
2554 // remove all the tracks that need to be...
2555 count = tracksToRemove->size();
Glenn Kastene80a4cc2011-12-15 09:51:17 -08002556 if (CC_UNLIKELY(count)) {
Eric Laurenta553c252009-07-17 12:17:14 -07002557 for (size_t i=0 ; i<count ; i++) {
2558 const sp<Track>& track = tracksToRemove->itemAt(i);
2559 mActiveTracks.remove(track);
Eric Laurent65b65452010-06-01 23:49:17 -07002560 if (track->mainBuffer() != mMixBuffer) {
2561 chain = getEffectChain_l(track->sessionId());
2562 if (chain != 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01002563 ALOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId());
Eric Laurent90681d62011-05-09 12:09:06 -07002564 chain->decActiveTrackCnt();
Eric Laurent65b65452010-06-01 23:49:17 -07002565 }
2566 }
Eric Laurenta553c252009-07-17 12:17:14 -07002567 if (track->isTerminated()) {
Eric Laurent90681d62011-05-09 12:09:06 -07002568 removeTrack_l(track);
Eric Laurenta553c252009-07-17 12:17:14 -07002569 }
2570 }
2571 }
2572
Eric Laurent65b65452010-06-01 23:49:17 -07002573 // mix buffer must be cleared if all tracks are connected to an
2574 // effect chain as in this case the mixer will not write to
2575 // mix buffer and track effects will accumulate into it
2576 if (mixedTracks != 0 && mixedTracks == tracksWithEffect) {
2577 memset(mMixBuffer, 0, mFrameCount * mChannelCount * sizeof(int16_t));
2578 }
2579
Eric Laurent059b4be2009-11-09 23:32:22 -08002580 return mixerStatus;
Eric Laurenta553c252009-07-17 12:17:14 -07002581}
2582
Glenn Kasten0fe52c82012-02-24 14:59:21 -08002583/*
2584The derived values that are cached:
2585 - mixBufferSize from frame count * frame size
2586 - activeSleepTime from activeSleepTimeUs()
2587 - idleSleepTime from idleSleepTimeUs()
2588 - standbyDelay from mActiveSleepTimeUs (DIRECT only)
2589 - maxPeriod from frame count and sample rate (MIXER only)
2590
2591The parameters that affect these derived values are:
2592 - frame count
2593 - frame size
2594 - sample rate
2595 - device type: A2DP or not
2596 - device latency
2597 - format: PCM or not
2598 - active sleep time
2599 - idle sleep time
2600*/
2601
2602void AudioFlinger::PlaybackThread::cacheParameters_l()
2603{
2604 mixBufferSize = mFrameCount * mFrameSize;
2605 activeSleepTime = activeSleepTimeUs();
2606 idleSleepTime = idleSleepTimeUs();
2607}
2608
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08002609void AudioFlinger::MixerThread::invalidateTracks(audio_stream_type_t streamType)
Eric Laurenta553c252009-07-17 12:17:14 -07002610{
Steve Block71f2cf12011-10-20 11:56:00 +01002611 ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d",
Eric Laurent8ed6ed02010-07-13 04:45:46 -07002612 this, streamType, mTracks.size());
Eric Laurenta553c252009-07-17 12:17:14 -07002613 Mutex::Autolock _l(mLock);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07002614
Eric Laurenta553c252009-07-17 12:17:14 -07002615 size_t size = mTracks.size();
2616 for (size_t i = 0; i < size; i++) {
2617 sp<Track> t = mTracks[i];
Glenn Kastenaae26c82012-02-08 12:35:35 -08002618 if (t->streamType() == streamType) {
Eric Laurentae29b762011-03-28 18:37:07 -07002619 android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07002620 t->mCblk->cv.signal();
Eric Laurenta553c252009-07-17 12:17:14 -07002621 }
Eric Laurent53334cd2010-06-23 17:38:20 -07002622 }
2623}
Eric Laurenta553c252009-07-17 12:17:14 -07002624
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08002625void AudioFlinger::PlaybackThread::setStreamValid(audio_stream_type_t streamType, bool valid)
Eric Laurent05ce0942011-08-30 10:18:54 -07002626{
Steve Block71f2cf12011-10-20 11:56:00 +01002627 ALOGV ("PlaybackThread::setStreamValid() thread %p, streamType %d, valid %d",
Eric Laurent05ce0942011-08-30 10:18:54 -07002628 this, streamType, valid);
2629 Mutex::Autolock _l(mLock);
2630
2631 mStreamTypes[streamType].valid = valid;
2632}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633
Eric Laurenta553c252009-07-17 12:17:14 -07002634// getTrackName_l() must be called with ThreadBase::mLock held
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002635int AudioFlinger::MixerThread::getTrackName_l()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636{
2637 return mAudioMixer->getTrackName();
2638}
2639
Eric Laurenta553c252009-07-17 12:17:14 -07002640// deleteTrackName_l() must be called with ThreadBase::mLock held
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002641void AudioFlinger::MixerThread::deleteTrackName_l(int name)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642{
Steve Block71f2cf12011-10-20 11:56:00 +01002643 ALOGV("remove track (%d) and delete from mixer", name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 mAudioMixer->deleteTrackName(name);
2645}
2646
Eric Laurenta553c252009-07-17 12:17:14 -07002647// checkForNewParameters_l() must be called with ThreadBase::mLock held
2648bool AudioFlinger::MixerThread::checkForNewParameters_l()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649{
Eric Laurenta553c252009-07-17 12:17:14 -07002650 bool reconfig = false;
2651
Eric Laurent8fce46a2009-08-04 09:45:33 -07002652 while (!mNewParameters.isEmpty()) {
Eric Laurenta553c252009-07-17 12:17:14 -07002653 status_t status = NO_ERROR;
Eric Laurent8fce46a2009-08-04 09:45:33 -07002654 String8 keyValuePair = mNewParameters[0];
2655 AudioParameter param = AudioParameter(keyValuePair);
Eric Laurenta553c252009-07-17 12:17:14 -07002656 int value;
Eric Laurent8fce46a2009-08-04 09:45:33 -07002657
Eric Laurenta553c252009-07-17 12:17:14 -07002658 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
2659 reconfig = true;
2660 }
2661 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
Glenn Kasten0a204ed2012-01-12 12:27:51 -08002662 if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) {
Eric Laurenta553c252009-07-17 12:17:14 -07002663 status = BAD_VALUE;
2664 } else {
2665 reconfig = true;
2666 }
2667 }
2668 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -07002669 if (value != AUDIO_CHANNEL_OUT_STEREO) {
Eric Laurenta553c252009-07-17 12:17:14 -07002670 status = BAD_VALUE;
2671 } else {
2672 reconfig = true;
2673 }
2674 }
2675 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
2676 // do not accept frame count changes if tracks are open as the track buffer
Glenn Kastene5fb2632011-12-14 10:28:06 -08002677 // size depends on frame count and correct behavior would not be guaranteed
Eric Laurenta553c252009-07-17 12:17:14 -07002678 // if frame count is changed after track creation
2679 if (!mTracks.isEmpty()) {
2680 status = INVALID_OPERATION;
2681 } else {
2682 reconfig = true;
2683 }
2684 }
Eric Laurent65b65452010-06-01 23:49:17 -07002685 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07002686#ifdef ADD_BATTERY_DATA
Gloria Wang9b3f1522011-02-24 14:51:45 -08002687 // when changing the audio output device, call addBatteryData to notify
2688 // the change
Eric Laurent90681d62011-05-09 12:09:06 -07002689 if ((int)mDevice != value) {
Gloria Wang9b3f1522011-02-24 14:51:45 -08002690 uint32_t params = 0;
2691 // check whether speaker is on
Dima Zavin24fc2fb2011-04-19 22:30:36 -07002692 if (value & AUDIO_DEVICE_OUT_SPEAKER) {
Gloria Wang9b3f1522011-02-24 14:51:45 -08002693 params |= IMediaPlayerService::kBatteryDataSpeakerOn;
2694 }
2695
2696 int deviceWithoutSpeaker
Dima Zavin24fc2fb2011-04-19 22:30:36 -07002697 = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER;
Gloria Wang9b3f1522011-02-24 14:51:45 -08002698 // check if any other device (except speaker) is on
2699 if (value & deviceWithoutSpeaker ) {
2700 params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
2701 }
2702
2703 if (params != 0) {
2704 addBatteryData(params);
2705 }
2706 }
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07002707#endif
Gloria Wang9b3f1522011-02-24 14:51:45 -08002708
Eric Laurent65b65452010-06-01 23:49:17 -07002709 // forward device change to effects that have requested to be
2710 // aware of attached audio device.
2711 mDevice = (uint32_t)value;
2712 for (size_t i = 0; i < mEffectChains.size(); i++) {
Eric Laurent76c40f72010-07-15 12:50:15 -07002713 mEffectChains[i]->setDevice_l(mDevice);
Eric Laurent65b65452010-06-01 23:49:17 -07002714 }
2715 }
2716
Eric Laurenta553c252009-07-17 12:17:14 -07002717 if (status == NO_ERROR) {
Dima Zavin31f188892011-04-18 16:57:27 -07002718 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07002719 keyValuePair.string());
Eric Laurenta553c252009-07-17 12:17:14 -07002720 if (!mStandby && status == INVALID_OPERATION) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07002721 mOutput->stream->common.standby(&mOutput->stream->common);
2722 mStandby = true;
2723 mBytesWritten = 0;
2724 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07002725 keyValuePair.string());
Eric Laurenta553c252009-07-17 12:17:14 -07002726 }
2727 if (status == NO_ERROR && reconfig) {
2728 delete mAudioMixer;
Glenn Kasten2589cf92012-01-27 18:08:45 -08002729 // for safety in case readOutputParameters() accesses mAudioMixer (it doesn't)
2730 mAudioMixer = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07002731 readOutputParameters();
2732 mAudioMixer = new AudioMixer(mFrameCount, mSampleRate);
2733 for (size_t i = 0; i < mTracks.size() ; i++) {
2734 int name = getTrackName_l();
2735 if (name < 0) break;
2736 mTracks[i]->mName = name;
Eric Laurent6f7e0972009-08-10 08:15:12 -07002737 // limit track sample rate to 2 x new output sample rate
2738 if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) {
2739 mTracks[i]->mCblk->sampleRate = 2 * sampleRate();
2740 }
Eric Laurenta553c252009-07-17 12:17:14 -07002741 }
Eric Laurent8fce46a2009-08-04 09:45:33 -07002742 sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
Eric Laurenta553c252009-07-17 12:17:14 -07002743 }
2744 }
Eric Laurent3fdb1262009-11-07 00:01:32 -08002745
2746 mNewParameters.removeAt(0);
2747
Eric Laurenta553c252009-07-17 12:17:14 -07002748 mParamStatus = status;
Eric Laurenta553c252009-07-17 12:17:14 -07002749 mParamCond.signal();
Eric Laurent5f37be32011-09-13 11:40:21 -07002750 // wait for condition with time out in case the thread calling ThreadBase::setParameters()
2751 // already timed out waiting for the status and will never signal the condition.
Glenn Kastencbfe2e12011-12-13 11:04:14 -08002752 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
Eric Laurenta553c252009-07-17 12:17:14 -07002753 }
2754 return reconfig;
2755}
2756
2757status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args)
2758{
2759 const size_t SIZE = 256;
2760 char buffer[SIZE];
2761 String8 result;
2762
2763 PlaybackThread::dumpInternals(fd, args);
2764
2765 snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames());
2766 result.append(buffer);
2767 write(fd, result.string(), result.size());
2768 return NO_ERROR;
2769}
2770
Eric Laurent059b4be2009-11-09 23:32:22 -08002771uint32_t AudioFlinger::MixerThread::idleSleepTimeUs()
2772{
Eric Laurenta54d7d32010-07-29 06:50:24 -07002773 return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
Eric Laurent62443f52009-10-05 20:29:18 -07002774}
2775
Eric Laurent8448a792010-08-18 18:13:17 -07002776uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs()
2777{
2778 return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
2779}
2780
Glenn Kasten0fe52c82012-02-24 14:59:21 -08002781void AudioFlinger::MixerThread::cacheParameters_l()
2782{
2783 PlaybackThread::cacheParameters_l();
2784
2785 // FIXME: Relaxed timing because of a certain device that can't meet latency
2786 // Should be reduced to 2x after the vendor fixes the driver issue
2787 // increase threshold again due to low power audio mode. The way this warning
2788 // threshold is calculated and its usefulness should be reconsidered anyway.
2789 maxPeriod = seconds(mFrameCount) / mSampleRate * 15;
2790}
2791
Eric Laurenta553c252009-07-17 12:17:14 -07002792// ----------------------------------------------------------------------------
Glenn Kasten39d00cb2012-01-17 11:09:42 -08002793AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger,
2794 AudioStreamOut* output, audio_io_handle_t id, uint32_t device)
Glenn Kastenefd511a2012-01-26 10:38:26 -08002795 : PlaybackThread(audioFlinger, output, id, device, DIRECT)
Glenn Kastendc3ac852012-01-25 15:28:08 -08002796 // mLeftVolFloat, mRightVolFloat
2797 // mLeftVolShort, mRightVolShort
Eric Laurenta553c252009-07-17 12:17:14 -07002798{
Eric Laurenta553c252009-07-17 12:17:14 -07002799}
2800
2801AudioFlinger::DirectOutputThread::~DirectOutputThread()
2802{
2803}
2804
Glenn Kasten761415b2012-03-06 11:23:32 -08002805AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l(
2806 Vector< sp<Track> > *tracksToRemove
Glenn Kasten67cb3122012-03-01 17:10:56 -08002807)
Eric Laurenta553c252009-07-17 12:17:14 -07002808{
Glenn Kasten761415b2012-03-06 11:23:32 -08002809 sp<Track> trackToRemove;
2810
Glenn Kastena13446a2012-03-08 07:47:15 -08002811 mixer_state mixerStatus = MIXER_IDLE;
Eric Laurent65b65452010-06-01 23:49:17 -07002812
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002813 // find out which tracks need to be processed
2814 if (mActiveTracks.size() != 0) {
2815 sp<Track> t = mActiveTracks[0].promote();
Glenn Kastena13446a2012-03-08 07:47:15 -08002816 // The track died recently
2817 if (t == 0) return MIXER_IDLE;
Eric Laurenta553c252009-07-17 12:17:14 -07002818
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002819 Track* const track = t.get();
2820 audio_track_cblk_t* cblk = track->cblk();
Eric Laurenta553c252009-07-17 12:17:14 -07002821
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002822 // The first time a track is added we wait
2823 // for all its buffers to be filled before processing it
2824 if (cblk->framesReady() && track->isReady() &&
2825 !track->isPaused() && !track->isTerminated())
2826 {
2827 //ALOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server);
Eric Laurenta553c252009-07-17 12:17:14 -07002828
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002829 if (track->mFillingUpStatus == Track::FS_FILLED) {
2830 track->mFillingUpStatus = Track::FS_ACTIVE;
2831 mLeftVolFloat = mRightVolFloat = 0;
2832 mLeftVolShort = mRightVolShort = 0;
2833 if (track->mState == TrackBase::RESUMING) {
2834 track->mState = TrackBase::ACTIVE;
2835 rampVolume = true;
2836 }
2837 } else if (cblk->server != 0) {
2838 // If the track is stopped before the first frame was mixed,
2839 // do not apply ramp
2840 rampVolume = true;
2841 }
2842 // compute volume for this track
2843 float left, right;
2844 if (track->isMuted() || mMasterMute || track->isPausing() ||
2845 mStreamTypes[track->streamType()].mute) {
2846 left = right = 0;
2847 if (track->isPausing()) {
2848 track->setPaused();
2849 }
2850 } else {
2851 float typeVolume = mStreamTypes[track->streamType()].volume;
2852 float v = mMasterVolume * typeVolume;
2853 uint32_t vlr = cblk->getVolumeLR();
2854 float v_clamped = v * (vlr & 0xFFFF);
2855 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
2856 left = v_clamped/MAX_GAIN;
2857 v_clamped = v * (vlr >> 16);
2858 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
2859 right = v_clamped/MAX_GAIN;
2860 }
Eric Laurenta553c252009-07-17 12:17:14 -07002861
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002862 if (left != mLeftVolFloat || right != mRightVolFloat) {
2863 mLeftVolFloat = left;
2864 mRightVolFloat = right;
Eric Laurenta553c252009-07-17 12:17:14 -07002865
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002866 // If audio HAL implements volume control,
2867 // force software volume to nominal value
2868 if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) {
2869 left = 1.0f;
2870 right = 1.0f;
2871 }
Eric Laurent65b65452010-06-01 23:49:17 -07002872
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002873 // Convert volumes from float to 8.24
2874 uint32_t vl = (uint32_t)(left * (1 << 24));
2875 uint32_t vr = (uint32_t)(right * (1 << 24));
Eric Laurent65b65452010-06-01 23:49:17 -07002876
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002877 // Delegate volume control to effect in track effect chain if needed
2878 // only one effect chain can be present on DirectOutputThread, so if
2879 // there is one, the track is connected to it
2880 if (!mEffectChains.isEmpty()) {
2881 // Do not ramp volume if volume is controlled by effect
2882 if (mEffectChains[0]->setVolume_l(&vl, &vr)) {
Eric Laurent65b65452010-06-01 23:49:17 -07002883 rampVolume = false;
Eric Laurenta553c252009-07-17 12:17:14 -07002884 }
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002885 }
Eric Laurenta553c252009-07-17 12:17:14 -07002886
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002887 // Convert volumes from 8.24 to 4.12 format
2888 uint32_t v_clamped = (vl + (1 << 11)) >> 12;
2889 if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT;
2890 leftVol = (uint16_t)v_clamped;
2891 v_clamped = (vr + (1 << 11)) >> 12;
2892 if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT;
2893 rightVol = (uint16_t)v_clamped;
2894 } else {
2895 leftVol = mLeftVolShort;
2896 rightVol = mRightVolShort;
2897 rampVolume = false;
2898 }
2899
2900 // reset retry count
2901 track->mRetryCount = kMaxTrackRetriesDirect;
Glenn Kastenb8f96762012-03-07 17:05:59 -08002902 mActiveTrack = t;
Glenn Kastena13446a2012-03-08 07:47:15 -08002903 mixerStatus = MIXER_TRACKS_READY;
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002904 } else {
2905 //ALOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server);
2906 if (track->isStopped()) {
2907 track->reset();
2908 }
2909 if (track->isTerminated() || track->isStopped() || track->isPaused()) {
2910 // We have consumed all the buffers of this track.
2911 // Remove it from the list of active tracks.
2912 trackToRemove = track;
2913 } else {
2914 // No buffers for this track. Give it a few chances to
2915 // fill a buffer, then remove it from active list.
2916 if (--(track->mRetryCount) <= 0) {
2917 ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name());
2918 trackToRemove = track;
Eric Laurenta553c252009-07-17 12:17:14 -07002919 } else {
Glenn Kastena13446a2012-03-08 07:47:15 -08002920 mixerStatus = MIXER_TRACKS_ENABLED;
Eric Laurenta553c252009-07-17 12:17:14 -07002921 }
2922 }
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002923 }
2924 }
Eric Laurenta553c252009-07-17 12:17:14 -07002925
Glenn Kasten761415b2012-03-06 11:23:32 -08002926 // FIXME merge this with similar code for removing multiple tracks
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002927 // remove all the tracks that need to be...
2928 if (CC_UNLIKELY(trackToRemove != 0)) {
Glenn Kasten761415b2012-03-06 11:23:32 -08002929 tracksToRemove->add(trackToRemove);
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002930 mActiveTracks.remove(trackToRemove);
2931 if (!mEffectChains.isEmpty()) {
Glenn Kasten81916df2012-03-08 12:18:35 -08002932 ALOGV("stopping track on chain %p for session Id: %d", mEffectChains[0].get(),
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002933 trackToRemove->sessionId());
2934 mEffectChains[0]->decActiveTrackCnt();
2935 }
2936 if (trackToRemove->isTerminated()) {
2937 removeTrack_l(trackToRemove);
2938 }
2939 }
Eric Laurent65b65452010-06-01 23:49:17 -07002940
Glenn Kastena13446a2012-03-08 07:47:15 -08002941 return mixerStatus;
Glenn Kasten67cb3122012-03-01 17:10:56 -08002942}
Eric Laurenta553c252009-07-17 12:17:14 -07002943
Glenn Kasten67cb3122012-03-01 17:10:56 -08002944void AudioFlinger::DirectOutputThread::threadLoop_mix()
2945{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002946 AudioBufferProvider::Buffer buffer;
2947 size_t frameCount = mFrameCount;
2948 int8_t *curBuf = (int8_t *)mMixBuffer;
2949 // output audio to hardware
2950 while (frameCount) {
2951 buffer.frameCount = frameCount;
Glenn Kastenb8f96762012-03-07 17:05:59 -08002952 mActiveTrack->getNextBuffer(&buffer);
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002953 if (CC_UNLIKELY(buffer.raw == NULL)) {
2954 memset(curBuf, 0, frameCount * mFrameSize);
2955 break;
2956 }
2957 memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
2958 frameCount -= buffer.frameCount;
2959 curBuf += buffer.frameCount * mFrameSize;
Glenn Kastenb8f96762012-03-07 17:05:59 -08002960 mActiveTrack->releaseBuffer(&buffer);
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002961 }
2962 sleepTime = 0;
2963 standbyTime = systemTime() + standbyDelay;
Glenn Kastenb8f96762012-03-07 17:05:59 -08002964 mActiveTrack.clear();
Glenn Kasten9779e9f2012-03-09 12:08:48 -08002965
2966 // apply volume
2967
2968 // Do not apply volume on compressed audio
2969 if (!audio_is_linear_pcm(mFormat)) {
2970 return;
2971 }
2972
2973 // convert to signed 16 bit before volume calculation
2974 if (mFormat == AUDIO_FORMAT_PCM_8_BIT) {
2975 size_t count = mFrameCount * mChannelCount;
2976 uint8_t *src = (uint8_t *)mMixBuffer + count-1;
2977 int16_t *dst = mMixBuffer + count-1;
Glenn Kasten18db49a2012-03-12 16:29:55 -07002978 while (count--) {
Glenn Kasten9779e9f2012-03-09 12:08:48 -08002979 *dst-- = (int16_t)(*src--^0x80) << 8;
2980 }
2981 }
2982
2983 frameCount = mFrameCount;
2984 int16_t *out = mMixBuffer;
2985 if (rampVolume) {
2986 if (mChannelCount == 1) {
2987 int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16;
2988 int32_t vlInc = d / (int32_t)frameCount;
2989 int32_t vl = ((int32_t)mLeftVolShort << 16);
2990 do {
2991 out[0] = clamp16(mul(out[0], vl >> 16) >> 12);
2992 out++;
2993 vl += vlInc;
2994 } while (--frameCount);
2995
2996 } else {
2997 int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16;
2998 int32_t vlInc = d / (int32_t)frameCount;
2999 d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16;
3000 int32_t vrInc = d / (int32_t)frameCount;
3001 int32_t vl = ((int32_t)mLeftVolShort << 16);
3002 int32_t vr = ((int32_t)mRightVolShort << 16);
3003 do {
3004 out[0] = clamp16(mul(out[0], vl >> 16) >> 12);
3005 out[1] = clamp16(mul(out[1], vr >> 16) >> 12);
3006 out += 2;
3007 vl += vlInc;
3008 vr += vrInc;
3009 } while (--frameCount);
3010 }
3011 } else {
3012 if (mChannelCount == 1) {
3013 do {
3014 out[0] = clamp16(mul(out[0], leftVol) >> 12);
3015 out++;
3016 } while (--frameCount);
3017 } else {
3018 do {
3019 out[0] = clamp16(mul(out[0], leftVol) >> 12);
3020 out[1] = clamp16(mul(out[1], rightVol) >> 12);
3021 out += 2;
3022 } while (--frameCount);
3023 }
3024 }
3025
3026 // convert back to unsigned 8 bit after volume calculation
3027 if (mFormat == AUDIO_FORMAT_PCM_8_BIT) {
3028 size_t count = mFrameCount * mChannelCount;
3029 int16_t *src = mMixBuffer;
3030 uint8_t *dst = (uint8_t *)mMixBuffer;
Glenn Kasten18db49a2012-03-12 16:29:55 -07003031 while (count--) {
Glenn Kasten9779e9f2012-03-09 12:08:48 -08003032 *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80;
3033 }
3034 }
3035
3036 mLeftVolShort = leftVol;
3037 mRightVolShort = rightVol;
Glenn Kasten67cb3122012-03-01 17:10:56 -08003038}
3039
3040void AudioFlinger::DirectOutputThread::threadLoop_sleepTime()
3041{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003042 if (sleepTime == 0) {
Glenn Kastena13446a2012-03-08 07:47:15 -08003043 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003044 sleepTime = activeSleepTime;
3045 } else {
3046 sleepTime = idleSleepTime;
3047 }
3048 } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) {
3049 memset (mMixBuffer, 0, mFrameCount * mFrameSize);
3050 sleepTime = 0;
3051 }
Eric Laurenta553c252009-07-17 12:17:14 -07003052}
3053
3054// getTrackName_l() must be called with ThreadBase::mLock held
3055int AudioFlinger::DirectOutputThread::getTrackName_l()
3056{
3057 return 0;
3058}
3059
3060// deleteTrackName_l() must be called with ThreadBase::mLock held
3061void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name)
3062{
3063}
3064
3065// checkForNewParameters_l() must be called with ThreadBase::mLock held
3066bool AudioFlinger::DirectOutputThread::checkForNewParameters_l()
3067{
3068 bool reconfig = false;
3069
Eric Laurent8fce46a2009-08-04 09:45:33 -07003070 while (!mNewParameters.isEmpty()) {
Eric Laurenta553c252009-07-17 12:17:14 -07003071 status_t status = NO_ERROR;
Eric Laurent8fce46a2009-08-04 09:45:33 -07003072 String8 keyValuePair = mNewParameters[0];
3073 AudioParameter param = AudioParameter(keyValuePair);
Eric Laurenta553c252009-07-17 12:17:14 -07003074 int value;
Eric Laurent8fce46a2009-08-04 09:45:33 -07003075
Eric Laurenta553c252009-07-17 12:17:14 -07003076 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
3077 // do not accept frame count changes if tracks are open as the track buffer
3078 // size depends on frame count and correct behavior would not be garantied
3079 // if frame count is changed after track creation
3080 if (!mTracks.isEmpty()) {
3081 status = INVALID_OPERATION;
3082 } else {
3083 reconfig = true;
3084 }
3085 }
3086 if (status == NO_ERROR) {
Dima Zavin31f188892011-04-18 16:57:27 -07003087 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07003088 keyValuePair.string());
Eric Laurenta553c252009-07-17 12:17:14 -07003089 if (!mStandby && status == INVALID_OPERATION) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07003090 mOutput->stream->common.standby(&mOutput->stream->common);
3091 mStandby = true;
3092 mBytesWritten = 0;
3093 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07003094 keyValuePair.string());
Eric Laurenta553c252009-07-17 12:17:14 -07003095 }
3096 if (status == NO_ERROR && reconfig) {
3097 readOutputParameters();
Eric Laurent8fce46a2009-08-04 09:45:33 -07003098 sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
Eric Laurenta553c252009-07-17 12:17:14 -07003099 }
3100 }
Eric Laurent3fdb1262009-11-07 00:01:32 -08003101
3102 mNewParameters.removeAt(0);
3103
Eric Laurenta553c252009-07-17 12:17:14 -07003104 mParamStatus = status;
Eric Laurenta553c252009-07-17 12:17:14 -07003105 mParamCond.signal();
Eric Laurent5f37be32011-09-13 11:40:21 -07003106 // wait for condition with time out in case the thread calling ThreadBase::setParameters()
3107 // already timed out waiting for the status and will never signal the condition.
Glenn Kastencbfe2e12011-12-13 11:04:14 -08003108 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
Eric Laurenta553c252009-07-17 12:17:14 -07003109 }
3110 return reconfig;
The Android Open Source Projectf1e484a2009-01-22 00:13:42 -08003111}
3112
Eric Laurent059b4be2009-11-09 23:32:22 -08003113uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs()
Eric Laurent62443f52009-10-05 20:29:18 -07003114{
3115 uint32_t time;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07003116 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent44331692011-12-05 09:47:19 -08003117 time = PlaybackThread::activeSleepTimeUs();
Eric Laurent059b4be2009-11-09 23:32:22 -08003118 } else {
3119 time = 10000;
3120 }
3121 return time;
3122}
3123
3124uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs()
3125{
3126 uint32_t time;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07003127 if (audio_is_linear_pcm(mFormat)) {
Eric Laurenta54d7d32010-07-29 06:50:24 -07003128 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
Eric Laurent62443f52009-10-05 20:29:18 -07003129 } else {
3130 time = 10000;
3131 }
3132 return time;
3133}
3134
Eric Laurent8448a792010-08-18 18:13:17 -07003135uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs()
3136{
3137 uint32_t time;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07003138 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent8448a792010-08-18 18:13:17 -07003139 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
3140 } else {
3141 time = 10000;
3142 }
3143 return time;
3144}
3145
Glenn Kasten0fe52c82012-02-24 14:59:21 -08003146void AudioFlinger::DirectOutputThread::cacheParameters_l()
3147{
3148 PlaybackThread::cacheParameters_l();
3149
3150 // use shorter standby delay as on normal output to release
3151 // hardware resources as soon as possible
3152 standbyDelay = microseconds(activeSleepTime*2);
3153}
Eric Laurent8448a792010-08-18 18:13:17 -07003154
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003155// ----------------------------------------------------------------------------
3156
Glenn Kastenefd511a2012-01-26 10:38:26 -08003157AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08003158 AudioFlinger::MixerThread* mainThread, audio_io_handle_t id)
Glenn Kastenefd511a2012-01-26 10:38:26 -08003159 : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device(), DUPLICATING),
3160 mWaitTimeMs(UINT_MAX)
Eric Laurenta553c252009-07-17 12:17:14 -07003161{
Eric Laurenta553c252009-07-17 12:17:14 -07003162 addOutputTrack(mainThread);
3163}
3164
3165AudioFlinger::DuplicatingThread::~DuplicatingThread()
3166{
Eric Laurent0a080292009-12-07 10:53:10 -08003167 for (size_t i = 0; i < mOutputTracks.size(); i++) {
3168 mOutputTracks[i]->destroy();
3169 }
Eric Laurenta553c252009-07-17 12:17:14 -07003170}
3171
Glenn Kasten67cb3122012-03-01 17:10:56 -08003172void AudioFlinger::DuplicatingThread::threadLoop_mix()
Eric Laurenta553c252009-07-17 12:17:14 -07003173{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003174 // mix buffers...
3175 if (outputsReady(outputTracks)) {
3176 mAudioMixer->process(AudioBufferProvider::kInvalidPTS);
3177 } else {
3178 memset(mMixBuffer, 0, mixBufferSize);
3179 }
3180 sleepTime = 0;
3181 writeFrames = mFrameCount;
Glenn Kasten67cb3122012-03-01 17:10:56 -08003182}
3183
3184void AudioFlinger::DuplicatingThread::threadLoop_sleepTime()
3185{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003186 if (sleepTime == 0) {
Glenn Kastena13446a2012-03-08 07:47:15 -08003187 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003188 sleepTime = activeSleepTime;
3189 } else {
3190 sleepTime = idleSleepTime;
3191 }
3192 } else if (mBytesWritten != 0) {
3193 // flush remaining overflow buffers in output tracks
3194 for (size_t i = 0; i < outputTracks.size(); i++) {
3195 if (outputTracks[i]->isActive()) {
3196 sleepTime = 0;
3197 writeFrames = 0;
3198 memset(mMixBuffer, 0, mixBufferSize);
3199 break;
Eric Laurenta553c252009-07-17 12:17:14 -07003200 }
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003201 }
3202 }
Glenn Kasten67cb3122012-03-01 17:10:56 -08003203}
Eric Laurentf69a3f82009-09-22 00:35:48 -07003204
Glenn Kasten67cb3122012-03-01 17:10:56 -08003205void AudioFlinger::DuplicatingThread::threadLoop_write()
3206{
Glenn Kasten0fe52c82012-02-24 14:59:21 -08003207 standbyTime = systemTime() + standbyDelay;
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003208 for (size_t i = 0; i < outputTracks.size(); i++) {
3209 outputTracks[i]->write(mMixBuffer, writeFrames);
3210 }
3211 mBytesWritten += mixBufferSize;
Glenn Kasten67cb3122012-03-01 17:10:56 -08003212}
Glenn Kastenbea6b952012-02-29 07:57:06 -08003213
Glenn Kasten67cb3122012-03-01 17:10:56 -08003214void AudioFlinger::DuplicatingThread::threadLoop_standby()
3215{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003216 // DuplicatingThread implements standby by stopping all tracks
3217 for (size_t i = 0; i < outputTracks.size(); i++) {
3218 outputTracks[i]->stop();
3219 }
Eric Laurenta553c252009-07-17 12:17:14 -07003220}
3221
Glenn Kasten08d2c042012-03-06 11:28:04 -08003222void AudioFlinger::DuplicatingThread::saveOutputTracks()
3223{
3224 outputTracks = mOutputTracks;
3225}
3226
3227void AudioFlinger::DuplicatingThread::clearOutputTracks()
3228{
3229 outputTracks.clear();
3230}
3231
Eric Laurenta553c252009-07-17 12:17:14 -07003232void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread)
3233{
Glenn Kasten91540ae2012-02-24 14:12:20 -08003234 Mutex::Autolock _l(mLock);
Glenn Kastenb3db2132012-01-19 08:59:58 -08003235 // FIXME explain this formula
Eric Laurenta553c252009-07-17 12:17:14 -07003236 int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate();
Glenn Kasten685c9ce2012-01-20 13:32:16 -08003237 OutputTrack *outputTrack = new OutputTrack(thread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003238 this,
Eric Laurenta553c252009-07-17 12:17:14 -07003239 mSampleRate,
3240 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003241 mChannelMask,
Eric Laurenta553c252009-07-17 12:17:14 -07003242 frameCount);
Eric Laurent6c30a712009-08-10 23:22:32 -07003243 if (outputTrack->cblk() != NULL) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -07003244 thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f);
Eric Laurent6c30a712009-08-10 23:22:32 -07003245 mOutputTracks.add(outputTrack);
Steve Block71f2cf12011-10-20 11:56:00 +01003246 ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread);
Glenn Kasten2bad67f2012-03-06 11:24:48 -08003247 updateWaitTime_l();
Eric Laurent6c30a712009-08-10 23:22:32 -07003248 }
Eric Laurenta553c252009-07-17 12:17:14 -07003249}
3250
3251void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
3252{
3253 Mutex::Autolock _l(mLock);
3254 for (size_t i = 0; i < mOutputTracks.size(); i++) {
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08003255 if (mOutputTracks[i]->thread() == thread) {
Eric Laurent6c30a712009-08-10 23:22:32 -07003256 mOutputTracks[i]->destroy();
Eric Laurenta553c252009-07-17 12:17:14 -07003257 mOutputTracks.removeAt(i);
Glenn Kasten2bad67f2012-03-06 11:24:48 -08003258 updateWaitTime_l();
Eric Laurenta553c252009-07-17 12:17:14 -07003259 return;
3260 }
3261 }
Steve Block71f2cf12011-10-20 11:56:00 +01003262 ALOGV("removeOutputTrack(): unkonwn thread: %p", thread);
Eric Laurenta553c252009-07-17 12:17:14 -07003263}
3264
Glenn Kasten2bad67f2012-03-06 11:24:48 -08003265// caller must hold mLock
3266void AudioFlinger::DuplicatingThread::updateWaitTime_l()
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003267{
3268 mWaitTimeMs = UINT_MAX;
3269 for (size_t i = 0; i < mOutputTracks.size(); i++) {
3270 sp<ThreadBase> strong = mOutputTracks[i]->thread().promote();
Glenn Kasten706b6182012-01-20 13:44:40 -08003271 if (strong != 0) {
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003272 uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate();
3273 if (waitTimeMs < mWaitTimeMs) {
3274 mWaitTimeMs = waitTimeMs;
3275 }
3276 }
3277 }
3278}
3279
3280
Glenn Kasten0fccd352012-02-24 15:42:17 -08003281bool AudioFlinger::DuplicatingThread::outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks)
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003282{
3283 for (size_t i = 0; i < outputTracks.size(); i++) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07003284 sp<ThreadBase> thread = outputTracks[i]->thread().promote();
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003285 if (thread == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003286 ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get());
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003287 return false;
3288 }
3289 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3290 if (playbackThread->standby() && !playbackThread->isSuspended()) {
Steve Block71f2cf12011-10-20 11:56:00 +01003291 ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get());
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003292 return false;
3293 }
3294 }
3295 return true;
3296}
3297
3298uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs()
3299{
3300 return (mWaitTimeMs * 1000) / 2;
3301}
3302
Glenn Kasten0fe52c82012-02-24 14:59:21 -08003303void AudioFlinger::DuplicatingThread::cacheParameters_l()
3304{
3305 // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first
3306 updateWaitTime_l();
3307
3308 MixerThread::cacheParameters_l();
3309}
3310
Eric Laurenta553c252009-07-17 12:17:14 -07003311// ----------------------------------------------------------------------------
3312
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003313// TrackBase constructor must be called with AudioFlinger::mLock held
Eric Laurenta553c252009-07-17 12:17:14 -07003314AudioFlinger::ThreadBase::TrackBase::TrackBase(
Glenn Kasten685c9ce2012-01-20 13:32:16 -08003315 ThreadBase *thread,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003316 const sp<Client>& client,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003317 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08003318 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003319 uint32_t channelMask,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003320 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -07003321 const sp<IMemory>& sharedBuffer,
3322 int sessionId)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003323 : RefBase(),
Eric Laurenta553c252009-07-17 12:17:14 -07003324 mThread(thread),
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003325 mClient(client),
Glenn Kastendc3ac852012-01-25 15:28:08 -08003326 mCblk(NULL),
3327 // mBuffer
3328 // mBufferEnd
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003329 mFrameCount(0),
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003330 mState(IDLE),
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003331 mFormat(format),
Glenn Kasten35269822012-02-21 10:35:56 -08003332 mStepServerFailed(false),
Eric Laurent65b65452010-06-01 23:49:17 -07003333 mSessionId(sessionId)
Glenn Kastendc3ac852012-01-25 15:28:08 -08003334 // mChannelCount
3335 // mChannelMask
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003336{
Steve Block71f2cf12011-10-20 11:56:00 +01003337 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003338
Steve Block5baa3a62011-12-20 16:23:08 +00003339 // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize);
Glenn Kasten18db49a2012-03-12 16:29:55 -07003340 size_t size = sizeof(audio_track_cblk_t);
3341 uint8_t channelCount = popcount(channelMask);
3342 size_t bufferSize = frameCount*channelCount*sizeof(int16_t);
3343 if (sharedBuffer == 0) {
3344 size += bufferSize;
3345 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003346
Glenn Kasten18db49a2012-03-12 16:29:55 -07003347 if (client != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 mCblkMemory = client->heap()->allocate(size);
3349 if (mCblkMemory != 0) {
3350 mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer());
Glenn Kasten3694ec12012-01-27 16:47:15 -08003351 if (mCblk != NULL) { // construct the shared structure in-place.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 new(mCblk) audio_track_cblk_t();
3353 // clear all buffers
3354 mCblk->frameCount = frameCount;
Eric Laurent88e209d2009-07-07 07:10:45 -07003355 mCblk->sampleRate = sampleRate;
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003356 mChannelCount = channelCount;
3357 mChannelMask = channelMask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 if (sharedBuffer == 0) {
3359 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
3360 memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
3361 // Force underrun condition to avoid false underrun callback until first data is
Eric Laurent4712baa2010-09-30 16:12:31 -07003362 // written to buffer (other flags are cleared)
Eric Laurenteb8f850d2010-05-14 03:26:45 -07003363 mCblk->flags = CBLK_UNDERRUN_ON;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 } else {
3365 mBuffer = sharedBuffer->pointer();
3366 }
3367 mBufferEnd = (uint8_t *)mBuffer + bufferSize;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 } else {
Steve Block3762c312012-01-06 19:20:56 +00003370 ALOGE("not enough memory for AudioTrack size=%u", size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 client->heap()->dump("AudioTrack");
3372 return;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003373 }
Glenn Kasten18db49a2012-03-12 16:29:55 -07003374 } else {
3375 mCblk = (audio_track_cblk_t *)(new uint8_t[size]);
Glenn Kastenf743e1f2012-03-14 12:56:26 -07003376 // construct the shared structure in-place.
3377 new(mCblk) audio_track_cblk_t();
3378 // clear all buffers
3379 mCblk->frameCount = frameCount;
3380 mCblk->sampleRate = sampleRate;
3381 mChannelCount = channelCount;
3382 mChannelMask = channelMask;
3383 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
3384 memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
3385 // Force underrun condition to avoid false underrun callback until first data is
3386 // written to buffer (other flags are cleared)
3387 mCblk->flags = CBLK_UNDERRUN_ON;
3388 mBufferEnd = (uint8_t *)mBuffer + bufferSize;
Glenn Kasten18db49a2012-03-12 16:29:55 -07003389 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003390}
3391
Eric Laurent2bb6b2a2009-09-16 06:02:45 -07003392AudioFlinger::ThreadBase::TrackBase::~TrackBase()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003393{
Glenn Kasten3694ec12012-01-27 16:47:15 -08003394 if (mCblk != NULL) {
Glenn Kastend9d68dc2012-02-03 10:24:48 -08003395 if (mClient == 0) {
Eric Laurenta553c252009-07-17 12:17:14 -07003396 delete mCblk;
Glenn Kastend9d68dc2012-02-03 10:24:48 -08003397 } else {
3398 mCblk->~audio_track_cblk_t(); // destroy our shared-structure.
Eric Laurenta553c252009-07-17 12:17:14 -07003399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 }
Glenn Kastenf31d0502012-01-25 15:27:15 -08003401 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Glenn Kasten706b6182012-01-20 13:44:40 -08003402 if (mClient != 0) {
Glenn Kastendc3ac852012-01-25 15:28:08 -08003403 // Client destructor must run with AudioFlinger mutex locked
Eric Laurentb9481d82009-09-17 05:12:56 -07003404 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
Glenn Kasten706b6182012-01-20 13:44:40 -08003405 // If the client's reference count drops to zero, the associated destructor
3406 // must run with AudioFlinger lock held. Thus the explicit clear() rather than
3407 // relying on the automatic clear() at end of scope.
Eric Laurentb9481d82009-09-17 05:12:56 -07003408 mClient.clear();
3409 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003410}
3411
Glenn Kastenc2db1192012-02-22 10:47:35 -08003412// AudioBufferProvider interface
3413// getNextBuffer() = 0;
3414// This implementation of releaseBuffer() is used by Track and RecordTrack, but not TimedTrack
Eric Laurent2bb6b2a2009-09-16 06:02:45 -07003415void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003416{
Glenn Kastenc434c902011-12-13 11:53:26 -08003417 buffer->raw = NULL;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003418 mFrameCount = buffer->frameCount;
Glenn Kastenc2db1192012-02-22 10:47:35 -08003419 (void) step(); // ignore return value of step()
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003420 buffer->frameCount = 0;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003421}
3422
Eric Laurent2bb6b2a2009-09-16 06:02:45 -07003423bool AudioFlinger::ThreadBase::TrackBase::step() {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003424 bool result;
3425 audio_track_cblk_t* cblk = this->cblk();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003426
3427 result = cblk->stepServer(mFrameCount);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003428 if (!result) {
Steve Block71f2cf12011-10-20 11:56:00 +01003429 ALOGV("stepServer failed acquiring cblk mutex");
Glenn Kasten35269822012-02-21 10:35:56 -08003430 mStepServerFailed = true;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003431 }
3432 return result;
3433}
3434
Eric Laurent2bb6b2a2009-09-16 06:02:45 -07003435void AudioFlinger::ThreadBase::TrackBase::reset() {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003436 audio_track_cblk_t* cblk = this->cblk();
3437
3438 cblk->user = 0;
3439 cblk->server = 0;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003440 cblk->userBase = 0;
3441 cblk->serverBase = 0;
Glenn Kasten35269822012-02-21 10:35:56 -08003442 mStepServerFailed = false;
Steve Block71f2cf12011-10-20 11:56:00 +01003443 ALOGV("TrackBase::reset");
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003444}
3445
Eric Laurent2bb6b2a2009-09-16 06:02:45 -07003446int AudioFlinger::ThreadBase::TrackBase::sampleRate() const {
The Android Open Source Project10592532009-03-18 17:39:46 -07003447 return (int)mCblk->sampleRate;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003448}
3449
Eric Laurent2bb6b2a2009-09-16 06:02:45 -07003450void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003451 audio_track_cblk_t* cblk = this->cblk();
Glenn Kastenfaf354d2012-01-11 09:48:27 -08003452 size_t frameSize = cblk->frameSize;
3453 int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize;
3454 int8_t *bufferEnd = bufferStart + frames * frameSize;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003455
3456 // Check validity of returned pointer in case the track control block would have been corrupted.
Eric Laurenta553c252009-07-17 12:17:14 -07003457 if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd ||
Glenn Kastenfaf354d2012-01-11 09:48:27 -08003458 ((unsigned long)bufferStart & (unsigned long)(frameSize - 1))) {
Steve Block3762c312012-01-06 19:20:56 +00003459 ALOGE("TrackBase::getBuffer buffer out of range:\n start: %p, end %p , mBuffer %p mBufferEnd %p\n \
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003460 server %d, serverBase %d, user %d, userBase %d",
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003461 bufferStart, bufferEnd, mBuffer, mBufferEnd,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003462 cblk->server, cblk->serverBase, cblk->user, cblk->userBase);
Glenn Kasten3694ec12012-01-27 16:47:15 -08003463 return NULL;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003464 }
3465
3466 return bufferStart;
3467}
3468
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003469// ----------------------------------------------------------------------------
3470
Eric Laurenta553c252009-07-17 12:17:14 -07003471// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
3472AudioFlinger::PlaybackThread::Track::Track(
Glenn Kasten685c9ce2012-01-20 13:32:16 -08003473 PlaybackThread *thread,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003474 const sp<Client>& client,
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08003475 audio_stream_type_t streamType,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003476 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08003477 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003478 uint32_t channelMask,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003479 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -07003480 const sp<IMemory>& sharedBuffer,
3481 int sessionId)
Glenn Kasten35269822012-02-21 10:35:56 -08003482 : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer, sessionId),
Glenn Kastene9b2b3b2012-03-19 11:14:37 -07003483 mMute(false),
3484 // mFillingUpStatus ?
3485 // mRetryCount initialized later when needed
3486 mSharedBuffer(sharedBuffer),
3487 mStreamType(streamType),
3488 mName(-1), // see note below
3489 mMainBuffer(thread->mixBuffer()),
3490 mAuxBuffer(NULL),
Eric Laurenta92ebfa2010-08-31 13:50:07 -07003491 mAuxEffectId(0), mHasVolumeController(false)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003492{
Eric Laurent8a77a992009-09-09 05:16:08 -07003493 if (mCblk != NULL) {
Eric Laurent8a77a992009-09-09 05:16:08 -07003494 // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of
3495 // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack
Eric Laurent09508612011-07-21 19:35:01 -07003496 mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t);
Glenn Kastene9b2b3b2012-03-19 11:14:37 -07003497 // to avoid leaking a track name, do not allocate one unless there is an mCblk
3498 mName = thread->getTrackName_l();
3499 if (mName < 0) {
3500 ALOGE("no more track names available");
3501 }
Eric Laurenta553c252009-07-17 12:17:14 -07003502 }
Glenn Kastene9b2b3b2012-03-19 11:14:37 -07003503 ALOGV("Track constructor name %d, calling pid %d", mName, IPCThreadState::self()->getCallingPid());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003504}
3505
Eric Laurenta553c252009-07-17 12:17:14 -07003506AudioFlinger::PlaybackThread::Track::~Track()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003507{
Steve Block71f2cf12011-10-20 11:56:00 +01003508 ALOGV("PlaybackThread::Track destructor");
Eric Laurenta553c252009-07-17 12:17:14 -07003509 sp<ThreadBase> thread = mThread.promote();
3510 if (thread != 0) {
Eric Laurentac196e12009-12-01 02:17:41 -08003511 Mutex::Autolock _l(thread->mLock);
Eric Laurenta553c252009-07-17 12:17:14 -07003512 mState = TERMINATED;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003513 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003514}
3515
Eric Laurenta553c252009-07-17 12:17:14 -07003516void AudioFlinger::PlaybackThread::Track::destroy()
3517{
3518 // NOTE: destroyTrack_l() can remove a strong reference to this Track
3519 // by removing it from mTracks vector, so there is a risk that this Tracks's
Glenn Kastenb3db2132012-01-19 08:59:58 -08003520 // destructor is called. As the destructor needs to lock mLock,
Eric Laurenta553c252009-07-17 12:17:14 -07003521 // we must acquire a strong reference on this Track before locking mLock
3522 // here so that the destructor is called only when exiting this function.
3523 // On the other hand, as long as Track::destroy() is only called by
3524 // TrackHandle destructor, the TrackHandle still holds a strong ref on
3525 // this Track with its member mTrack.
3526 sp<Track> keep(this);
3527 { // scope for mLock
3528 sp<ThreadBase> thread = mThread.promote();
3529 if (thread != 0) {
Eric Laurentac196e12009-12-01 02:17:41 -08003530 if (!isOutputTrack()) {
3531 if (mState == ACTIVE || mState == RESUMING) {
Glenn Kastenaae26c82012-02-08 12:35:35 -08003532 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
Gloria Wang9b3f1522011-02-24 14:51:45 -08003533
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003534#ifdef ADD_BATTERY_DATA
Gloria Wang9b3f1522011-02-24 14:51:45 -08003535 // to track the speaker usage
3536 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003537#endif
Eric Laurentac196e12009-12-01 02:17:41 -08003538 }
3539 AudioSystem::releaseOutput(thread->id());
Eric Laurent49f02be2009-11-19 09:00:56 -08003540 }
Eric Laurenta553c252009-07-17 12:17:14 -07003541 Mutex::Autolock _l(thread->mLock);
3542 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3543 playbackThread->destroyTrack_l(this);
3544 }
3545 }
3546}
3547
3548void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003549{
Glenn Kastenbc4de882012-01-17 14:39:34 -08003550 uint32_t vlr = mCblk->getVolumeLR();
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003551 snprintf(buffer, size, " %05d %05d %03u %03u 0x%08x %05u %04u %1d %1d %1d %05u %05u %05u 0x%08x 0x%08x 0x%08x 0x%08x\n",
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003552 mName - AudioMixer::TRACK0,
Glenn Kasten81211142012-02-05 18:09:08 -08003553 (mClient == 0) ? getpid_cached : mClient->pid(),
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003554 mStreamType,
3555 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003556 mChannelMask,
Eric Laurent65b65452010-06-01 23:49:17 -07003557 mSessionId,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003558 mFrameCount,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003559 mState,
3560 mMute,
3561 mFillingUpStatus,
3562 mCblk->sampleRate,
Glenn Kasten0632bad2012-01-17 12:20:54 -08003563 vlr & 0xFFFF,
3564 vlr >> 16,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003565 mCblk->server,
Eric Laurent65b65452010-06-01 23:49:17 -07003566 mCblk->user,
3567 (int)mMainBuffer,
3568 (int)mAuxBuffer);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003569}
3570
Glenn Kastenc2db1192012-02-22 10:47:35 -08003571// AudioBufferProvider interface
John Grossmand8cf2962012-02-08 16:37:41 -08003572status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(
Glenn Kasten18db49a2012-03-12 16:29:55 -07003573 AudioBufferProvider::Buffer* buffer, int64_t pts)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003574{
Glenn Kasten18db49a2012-03-12 16:29:55 -07003575 audio_track_cblk_t* cblk = this->cblk();
3576 uint32_t framesReady;
3577 uint32_t framesReq = buffer->frameCount;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003578
Glenn Kasten18db49a2012-03-12 16:29:55 -07003579 // Check if last stepServer failed, try to step now
3580 if (mStepServerFailed) {
3581 if (!step()) goto getNextBuffer_exit;
3582 ALOGV("stepServer recovered");
3583 mStepServerFailed = false;
3584 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003585
Glenn Kasten18db49a2012-03-12 16:29:55 -07003586 framesReady = cblk->framesReady();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003587
Glenn Kasten18db49a2012-03-12 16:29:55 -07003588 if (CC_LIKELY(framesReady)) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003589 uint32_t s = cblk->server;
3590 uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
3591
3592 bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd;
3593 if (framesReq > framesReady) {
3594 framesReq = framesReady;
3595 }
3596 if (s + framesReq > bufferEnd) {
3597 framesReq = bufferEnd - s;
3598 }
3599
Glenn Kasten18db49a2012-03-12 16:29:55 -07003600 buffer->raw = getBuffer(s, framesReq);
3601 if (buffer->raw == NULL) goto getNextBuffer_exit;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003602
Glenn Kasten18db49a2012-03-12 16:29:55 -07003603 buffer->frameCount = framesReq;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003604 return NO_ERROR;
Glenn Kasten18db49a2012-03-12 16:29:55 -07003605 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003606
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003607getNextBuffer_exit:
Glenn Kasten18db49a2012-03-12 16:29:55 -07003608 buffer->raw = NULL;
3609 buffer->frameCount = 0;
3610 ALOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get());
3611 return NOT_ENOUGH_DATA;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003612}
3613
Glenn Kasten18db49a2012-03-12 16:29:55 -07003614uint32_t AudioFlinger::PlaybackThread::Track::framesReady() const {
John Grossmand8cf2962012-02-08 16:37:41 -08003615 return mCblk->framesReady();
3616}
3617
Eric Laurenta553c252009-07-17 12:17:14 -07003618bool AudioFlinger::PlaybackThread::Track::isReady() const {
Eric Laurent9a30fc12010-10-05 14:41:42 -07003619 if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003620
John Grossmand8cf2962012-02-08 16:37:41 -08003621 if (framesReady() >= mCblk->frameCount ||
Eric Laurenteb8f850d2010-05-14 03:26:45 -07003622 (mCblk->flags & CBLK_FORCEREADY_MSK)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003623 mFillingUpStatus = FS_FILLED;
Eric Laurentae29b762011-03-28 18:37:07 -07003624 android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003625 return true;
3626 }
3627 return false;
3628}
3629
Glenn Kasten6a20b262012-02-02 10:56:47 -08003630status_t AudioFlinger::PlaybackThread::Track::start(pid_t tid)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003631{
Eric Laurent49f02be2009-11-19 09:00:56 -08003632 status_t status = NO_ERROR;
Glenn Kasten6a20b262012-02-02 10:56:47 -08003633 ALOGV("start(%d), calling pid %d session %d tid %d",
3634 mName, IPCThreadState::self()->getCallingPid(), mSessionId, tid);
Eric Laurenta553c252009-07-17 12:17:14 -07003635 sp<ThreadBase> thread = mThread.promote();
3636 if (thread != 0) {
3637 Mutex::Autolock _l(thread->mLock);
Glenn Kasten56356202012-01-26 13:39:18 -08003638 track_state state = mState;
Eric Laurent49f02be2009-11-19 09:00:56 -08003639 // here the track could be either new, or restarted
3640 // in both cases "unstop" the track
3641 if (mState == PAUSED) {
3642 mState = TrackBase::RESUMING;
Steve Block71f2cf12011-10-20 11:56:00 +01003643 ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this);
Eric Laurent49f02be2009-11-19 09:00:56 -08003644 } else {
3645 mState = TrackBase::ACTIVE;
Steve Block71f2cf12011-10-20 11:56:00 +01003646 ALOGV("? => ACTIVE (%d) on thread %p", mName, this);
Eric Laurent49f02be2009-11-19 09:00:56 -08003647 }
3648
3649 if (!isOutputTrack() && state != ACTIVE && state != RESUMING) {
3650 thread->mLock.unlock();
Glenn Kastenaae26c82012-02-08 12:35:35 -08003651 status = AudioSystem::startOutput(thread->id(), mStreamType, mSessionId);
Eric Laurent49f02be2009-11-19 09:00:56 -08003652 thread->mLock.lock();
Gloria Wang9b3f1522011-02-24 14:51:45 -08003653
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003654#ifdef ADD_BATTERY_DATA
Gloria Wang9b3f1522011-02-24 14:51:45 -08003655 // to track the speaker usage
3656 if (status == NO_ERROR) {
3657 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart);
3658 }
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003659#endif
Eric Laurent49f02be2009-11-19 09:00:56 -08003660 }
3661 if (status == NO_ERROR) {
3662 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3663 playbackThread->addTrack_l(this);
3664 } else {
3665 mState = state;
3666 }
3667 } else {
3668 status = BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -07003669 }
Eric Laurent49f02be2009-11-19 09:00:56 -08003670 return status;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003671}
3672
Eric Laurenta553c252009-07-17 12:17:14 -07003673void AudioFlinger::PlaybackThread::Track::stop()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003674{
Glenn Kasten7ed4f0c2012-02-03 11:10:00 -08003675 ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
Eric Laurenta553c252009-07-17 12:17:14 -07003676 sp<ThreadBase> thread = mThread.promote();
3677 if (thread != 0) {
3678 Mutex::Autolock _l(thread->mLock);
Glenn Kasten56356202012-01-26 13:39:18 -08003679 track_state state = mState;
Eric Laurenta553c252009-07-17 12:17:14 -07003680 if (mState > STOPPED) {
3681 mState = STOPPED;
3682 // If the track is not active (PAUSED and buffers full), flush buffers
3683 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3684 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
3685 reset();
3686 }
Steve Block71f2cf12011-10-20 11:56:00 +01003687 ALOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003688 }
Eric Laurent49f02be2009-11-19 09:00:56 -08003689 if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) {
3690 thread->mLock.unlock();
Glenn Kastenaae26c82012-02-08 12:35:35 -08003691 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
Eric Laurent49f02be2009-11-19 09:00:56 -08003692 thread->mLock.lock();
Gloria Wang9b3f1522011-02-24 14:51:45 -08003693
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003694#ifdef ADD_BATTERY_DATA
Gloria Wang9b3f1522011-02-24 14:51:45 -08003695 // to track the speaker usage
3696 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003697#endif
Eric Laurent49f02be2009-11-19 09:00:56 -08003698 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003699 }
3700}
3701
Eric Laurenta553c252009-07-17 12:17:14 -07003702void AudioFlinger::PlaybackThread::Track::pause()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003703{
Glenn Kasten7ed4f0c2012-02-03 11:10:00 -08003704 ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
Eric Laurenta553c252009-07-17 12:17:14 -07003705 sp<ThreadBase> thread = mThread.promote();
3706 if (thread != 0) {
3707 Mutex::Autolock _l(thread->mLock);
3708 if (mState == ACTIVE || mState == RESUMING) {
3709 mState = PAUSING;
Steve Block71f2cf12011-10-20 11:56:00 +01003710 ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get());
Eric Laurent49f02be2009-11-19 09:00:56 -08003711 if (!isOutputTrack()) {
3712 thread->mLock.unlock();
Glenn Kastenaae26c82012-02-08 12:35:35 -08003713 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
Eric Laurent49f02be2009-11-19 09:00:56 -08003714 thread->mLock.lock();
Gloria Wang9b3f1522011-02-24 14:51:45 -08003715
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003716#ifdef ADD_BATTERY_DATA
Gloria Wang9b3f1522011-02-24 14:51:45 -08003717 // to track the speaker usage
3718 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003719#endif
Eric Laurent49f02be2009-11-19 09:00:56 -08003720 }
Eric Laurenta553c252009-07-17 12:17:14 -07003721 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003722 }
3723}
3724
Eric Laurenta553c252009-07-17 12:17:14 -07003725void AudioFlinger::PlaybackThread::Track::flush()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003726{
Steve Block71f2cf12011-10-20 11:56:00 +01003727 ALOGV("flush(%d)", mName);
Eric Laurenta553c252009-07-17 12:17:14 -07003728 sp<ThreadBase> thread = mThread.promote();
3729 if (thread != 0) {
3730 Mutex::Autolock _l(thread->mLock);
3731 if (mState != STOPPED && mState != PAUSED && mState != PAUSING) {
3732 return;
3733 }
3734 // No point remaining in PAUSED state after a flush => go to
3735 // STOPPED state
3736 mState = STOPPED;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003737
Eric Laurentae29b762011-03-28 18:37:07 -07003738 // do not reset the track if it is still in the process of being stopped or paused.
3739 // this will be done by prepareTracks_l() when the track is stopped.
3740 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3741 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
3742 reset();
3743 }
Eric Laurenta553c252009-07-17 12:17:14 -07003744 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003745}
3746
Eric Laurenta553c252009-07-17 12:17:14 -07003747void AudioFlinger::PlaybackThread::Track::reset()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003748{
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003749 // Do not reset twice to avoid discarding data written just after a flush and before
3750 // the audioflinger thread detects the track is stopped.
3751 if (!mResetDone) {
3752 TrackBase::reset();
3753 // Force underrun condition to avoid false underrun callback until first data is
3754 // written to buffer
Eric Laurentae29b762011-03-28 18:37:07 -07003755 android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags);
3756 android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags);
Eric Laurenta553c252009-07-17 12:17:14 -07003757 mFillingUpStatus = FS_FILLING;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003758 mResetDone = true;
3759 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003760}
3761
Eric Laurenta553c252009-07-17 12:17:14 -07003762void AudioFlinger::PlaybackThread::Track::mute(bool muted)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003763{
3764 mMute = muted;
3765}
3766
Eric Laurent65b65452010-06-01 23:49:17 -07003767status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId)
3768{
3769 status_t status = DEAD_OBJECT;
3770 sp<ThreadBase> thread = mThread.promote();
3771 if (thread != 0) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07003772 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3773 status = playbackThread->attachAuxEffect(this, EffectId);
Eric Laurent65b65452010-06-01 23:49:17 -07003774 }
3775 return status;
3776}
3777
3778void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer)
3779{
3780 mAuxEffectId = EffectId;
3781 mAuxBuffer = buffer;
3782}
3783
John Grossmand8cf2962012-02-08 16:37:41 -08003784// timed audio tracks
3785
3786sp<AudioFlinger::PlaybackThread::TimedTrack>
3787AudioFlinger::PlaybackThread::TimedTrack::create(
Glenn Kasten685c9ce2012-01-20 13:32:16 -08003788 PlaybackThread *thread,
John Grossmand8cf2962012-02-08 16:37:41 -08003789 const sp<Client>& client,
3790 audio_stream_type_t streamType,
3791 uint32_t sampleRate,
3792 audio_format_t format,
3793 uint32_t channelMask,
3794 int frameCount,
3795 const sp<IMemory>& sharedBuffer,
3796 int sessionId) {
3797 if (!client->reserveTimedTrack())
3798 return NULL;
3799
Glenn Kasten44c71fa2012-03-19 10:38:51 -07003800 return new TimedTrack(
John Grossmand8cf2962012-02-08 16:37:41 -08003801 thread, client, streamType, sampleRate, format, channelMask, frameCount,
3802 sharedBuffer, sessionId);
John Grossmand8cf2962012-02-08 16:37:41 -08003803}
3804
3805AudioFlinger::PlaybackThread::TimedTrack::TimedTrack(
Glenn Kasten685c9ce2012-01-20 13:32:16 -08003806 PlaybackThread *thread,
John Grossmand8cf2962012-02-08 16:37:41 -08003807 const sp<Client>& client,
3808 audio_stream_type_t streamType,
3809 uint32_t sampleRate,
3810 audio_format_t format,
3811 uint32_t channelMask,
3812 int frameCount,
3813 const sp<IMemory>& sharedBuffer,
3814 int sessionId)
3815 : Track(thread, client, streamType, sampleRate, format, channelMask,
3816 frameCount, sharedBuffer, sessionId),
3817 mTimedSilenceBuffer(NULL),
3818 mTimedSilenceBufferSize(0),
3819 mTimedAudioOutputOnTime(false),
3820 mMediaTimeTransformValid(false)
3821{
3822 LocalClock lc;
3823 mLocalTimeFreq = lc.getLocalFreq();
3824
3825 mLocalTimeToSampleTransform.a_zero = 0;
3826 mLocalTimeToSampleTransform.b_zero = 0;
3827 mLocalTimeToSampleTransform.a_to_b_numer = sampleRate;
3828 mLocalTimeToSampleTransform.a_to_b_denom = mLocalTimeFreq;
3829 LinearTransform::reduce(&mLocalTimeToSampleTransform.a_to_b_numer,
3830 &mLocalTimeToSampleTransform.a_to_b_denom);
3831}
3832
3833AudioFlinger::PlaybackThread::TimedTrack::~TimedTrack() {
3834 mClient->releaseTimedTrack();
3835 delete [] mTimedSilenceBuffer;
3836}
3837
3838status_t AudioFlinger::PlaybackThread::TimedTrack::allocateTimedBuffer(
3839 size_t size, sp<IMemory>* buffer) {
3840
3841 Mutex::Autolock _l(mTimedBufferQueueLock);
3842
3843 trimTimedBufferQueue_l();
3844
3845 // lazily initialize the shared memory heap for timed buffers
3846 if (mTimedMemoryDealer == NULL) {
3847 const int kTimedBufferHeapSize = 512 << 10;
3848
3849 mTimedMemoryDealer = new MemoryDealer(kTimedBufferHeapSize,
3850 "AudioFlingerTimed");
3851 if (mTimedMemoryDealer == NULL)
3852 return NO_MEMORY;
3853 }
3854
3855 sp<IMemory> newBuffer = mTimedMemoryDealer->allocate(size);
3856 if (newBuffer == NULL) {
3857 newBuffer = mTimedMemoryDealer->allocate(size);
3858 if (newBuffer == NULL)
3859 return NO_MEMORY;
3860 }
3861
3862 *buffer = newBuffer;
3863 return NO_ERROR;
3864}
3865
3866// caller must hold mTimedBufferQueueLock
3867void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() {
3868 int64_t mediaTimeNow;
3869 {
3870 Mutex::Autolock mttLock(mMediaTimeTransformLock);
3871 if (!mMediaTimeTransformValid)
3872 return;
3873
3874 int64_t targetTimeNow;
3875 status_t res = (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME)
3876 ? mCCHelper.getCommonTime(&targetTimeNow)
3877 : mCCHelper.getLocalTime(&targetTimeNow);
3878
3879 if (OK != res)
3880 return;
3881
3882 if (!mMediaTimeTransform.doReverseTransform(targetTimeNow,
3883 &mediaTimeNow)) {
3884 return;
3885 }
3886 }
3887
3888 size_t trimIndex;
3889 for (trimIndex = 0; trimIndex < mTimedBufferQueue.size(); trimIndex++) {
3890 if (mTimedBufferQueue[trimIndex].pts() > mediaTimeNow)
3891 break;
3892 }
3893
3894 if (trimIndex) {
3895 mTimedBufferQueue.removeItemsAt(0, trimIndex);
3896 }
3897}
3898
3899status_t AudioFlinger::PlaybackThread::TimedTrack::queueTimedBuffer(
3900 const sp<IMemory>& buffer, int64_t pts) {
3901
3902 {
3903 Mutex::Autolock mttLock(mMediaTimeTransformLock);
3904 if (!mMediaTimeTransformValid)
3905 return INVALID_OPERATION;
3906 }
3907
3908 Mutex::Autolock _l(mTimedBufferQueueLock);
3909
3910 mTimedBufferQueue.add(TimedBuffer(buffer, pts));
3911
3912 return NO_ERROR;
3913}
3914
3915status_t AudioFlinger::PlaybackThread::TimedTrack::setMediaTimeTransform(
3916 const LinearTransform& xform, TimedAudioTrack::TargetTimeline target) {
3917
3918 ALOGV("%s az=%lld bz=%lld n=%d d=%u tgt=%d", __PRETTY_FUNCTION__,
3919 xform.a_zero, xform.b_zero, xform.a_to_b_numer, xform.a_to_b_denom,
3920 target);
3921
3922 if (!(target == TimedAudioTrack::LOCAL_TIME ||
3923 target == TimedAudioTrack::COMMON_TIME)) {
3924 return BAD_VALUE;
3925 }
3926
3927 Mutex::Autolock lock(mMediaTimeTransformLock);
3928 mMediaTimeTransform = xform;
3929 mMediaTimeTransformTarget = target;
3930 mMediaTimeTransformValid = true;
3931
3932 return NO_ERROR;
3933}
3934
3935#define min(a, b) ((a) < (b) ? (a) : (b))
3936
3937// implementation of getNextBuffer for tracks whose buffers have timestamps
3938status_t AudioFlinger::PlaybackThread::TimedTrack::getNextBuffer(
3939 AudioBufferProvider::Buffer* buffer, int64_t pts)
3940{
3941 if (pts == AudioBufferProvider::kInvalidPTS) {
3942 buffer->raw = 0;
3943 buffer->frameCount = 0;
3944 return INVALID_OPERATION;
3945 }
3946
John Grossmand8cf2962012-02-08 16:37:41 -08003947 Mutex::Autolock _l(mTimedBufferQueueLock);
3948
3949 while (true) {
3950
3951 // if we have no timed buffers, then fail
3952 if (mTimedBufferQueue.isEmpty()) {
3953 buffer->raw = 0;
3954 buffer->frameCount = 0;
3955 return NOT_ENOUGH_DATA;
3956 }
3957
3958 TimedBuffer& head = mTimedBufferQueue.editItemAt(0);
3959
3960 // calculate the PTS of the head of the timed buffer queue expressed in
3961 // local time
3962 int64_t headLocalPTS;
3963 {
3964 Mutex::Autolock mttLock(mMediaTimeTransformLock);
3965
Glenn Kasten81916df2012-03-08 12:18:35 -08003966 ALOG_ASSERT(mMediaTimeTransformValid, "media time transform invalid");
John Grossmand8cf2962012-02-08 16:37:41 -08003967
3968 if (mMediaTimeTransform.a_to_b_denom == 0) {
3969 // the transform represents a pause, so yield silence
3970 timedYieldSilence(buffer->frameCount, buffer);
3971 return NO_ERROR;
3972 }
3973
3974 int64_t transformedPTS;
3975 if (!mMediaTimeTransform.doForwardTransform(head.pts(),
3976 &transformedPTS)) {
3977 // the transform failed. this shouldn't happen, but if it does
3978 // then just drop this buffer
3979 ALOGW("timedGetNextBuffer transform failed");
3980 buffer->raw = 0;
3981 buffer->frameCount = 0;
3982 mTimedBufferQueue.removeAt(0);
3983 return NO_ERROR;
3984 }
3985
3986 if (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) {
3987 if (OK != mCCHelper.commonTimeToLocalTime(transformedPTS,
3988 &headLocalPTS)) {
3989 buffer->raw = 0;
3990 buffer->frameCount = 0;
3991 return INVALID_OPERATION;
3992 }
3993 } else {
3994 headLocalPTS = transformedPTS;
3995 }
3996 }
3997
3998 // adjust the head buffer's PTS to reflect the portion of the head buffer
3999 // that has already been consumed
4000 int64_t effectivePTS = headLocalPTS +
4001 ((head.position() / mCblk->frameSize) * mLocalTimeFreq / sampleRate());
4002
4003 // Calculate the delta in samples between the head of the input buffer
4004 // queue and the start of the next output buffer that will be written.
4005 // If the transformation fails because of over or underflow, it means
4006 // that the sample's position in the output stream is so far out of
4007 // whack that it should just be dropped.
4008 int64_t sampleDelta;
4009 if (llabs(effectivePTS - pts) >= (static_cast<int64_t>(1) << 31)) {
4010 ALOGV("*** head buffer is too far from PTS: dropped buffer");
4011 mTimedBufferQueue.removeAt(0);
4012 continue;
4013 }
4014 if (!mLocalTimeToSampleTransform.doForwardTransform(
4015 (effectivePTS - pts) << 32, &sampleDelta)) {
4016 ALOGV("*** too late during sample rate transform: dropped buffer");
4017 mTimedBufferQueue.removeAt(0);
4018 continue;
4019 }
4020
4021 ALOGV("*** %s head.pts=%lld head.pos=%d pts=%lld sampleDelta=[%d.%08x]",
4022 __PRETTY_FUNCTION__, head.pts(), head.position(), pts,
4023 static_cast<int32_t>((sampleDelta >= 0 ? 0 : 1) + (sampleDelta >> 32)),
4024 static_cast<uint32_t>(sampleDelta & 0xFFFFFFFF));
4025
4026 // if the delta between the ideal placement for the next input sample and
4027 // the current output position is within this threshold, then we will
4028 // concatenate the next input samples to the previous output
4029 const int64_t kSampleContinuityThreshold =
4030 (static_cast<int64_t>(sampleRate()) << 32) / 10;
4031
4032 // if this is the first buffer of audio that we're emitting from this track
4033 // then it should be almost exactly on time.
4034 const int64_t kSampleStartupThreshold = 1LL << 32;
4035
4036 if ((mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleContinuityThreshold) ||
4037 (!mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleStartupThreshold)) {
4038 // the next input is close enough to being on time, so concatenate it
4039 // with the last output
4040 timedYieldSamples(buffer);
4041
4042 ALOGV("*** on time: head.pos=%d frameCount=%u", head.position(), buffer->frameCount);
4043 return NO_ERROR;
4044 } else if (sampleDelta > 0) {
4045 // the gap between the current output position and the proper start of
4046 // the next input sample is too big, so fill it with silence
4047 uint32_t framesUntilNextInput = (sampleDelta + 0x80000000) >> 32;
4048
4049 timedYieldSilence(framesUntilNextInput, buffer);
4050 ALOGV("*** silence: frameCount=%u", buffer->frameCount);
4051 return NO_ERROR;
4052 } else {
4053 // the next input sample is late
4054 uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32));
4055 size_t onTimeSamplePosition =
4056 head.position() + lateFrames * mCblk->frameSize;
4057
4058 if (onTimeSamplePosition > head.buffer()->size()) {
4059 // all the remaining samples in the head are too late, so
4060 // drop it and move on
4061 ALOGV("*** too late: dropped buffer");
4062 mTimedBufferQueue.removeAt(0);
4063 continue;
4064 } else {
4065 // skip over the late samples
4066 head.setPosition(onTimeSamplePosition);
4067
4068 // yield the available samples
4069 timedYieldSamples(buffer);
4070
4071 ALOGV("*** late: head.pos=%d frameCount=%u", head.position(), buffer->frameCount);
4072 return NO_ERROR;
4073 }
4074 }
4075 }
4076}
4077
4078// Yield samples from the timed buffer queue head up to the given output
4079// buffer's capacity.
4080//
4081// Caller must hold mTimedBufferQueueLock
4082void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSamples(
4083 AudioBufferProvider::Buffer* buffer) {
4084
4085 const TimedBuffer& head = mTimedBufferQueue[0];
4086
4087 buffer->raw = (static_cast<uint8_t*>(head.buffer()->pointer()) +
4088 head.position());
4089
4090 uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) /
4091 mCblk->frameSize);
4092 size_t framesRequested = buffer->frameCount;
4093 buffer->frameCount = min(framesLeftInHead, framesRequested);
4094
4095 mTimedAudioOutputOnTime = true;
4096}
4097
4098// Yield samples of silence up to the given output buffer's capacity
4099//
4100// Caller must hold mTimedBufferQueueLock
4101void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSilence(
4102 uint32_t numFrames, AudioBufferProvider::Buffer* buffer) {
4103
4104 // lazily allocate a buffer filled with silence
4105 if (mTimedSilenceBufferSize < numFrames * mCblk->frameSize) {
4106 delete [] mTimedSilenceBuffer;
4107 mTimedSilenceBufferSize = numFrames * mCblk->frameSize;
4108 mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize];
4109 memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize);
4110 }
4111
4112 buffer->raw = mTimedSilenceBuffer;
4113 size_t framesRequested = buffer->frameCount;
4114 buffer->frameCount = min(numFrames, framesRequested);
4115
4116 mTimedAudioOutputOnTime = false;
4117}
4118
Glenn Kastenc2db1192012-02-22 10:47:35 -08004119// AudioBufferProvider interface
John Grossmand8cf2962012-02-08 16:37:41 -08004120void AudioFlinger::PlaybackThread::TimedTrack::releaseBuffer(
4121 AudioBufferProvider::Buffer* buffer) {
4122
4123 Mutex::Autolock _l(mTimedBufferQueueLock);
4124
John Grossman8c010612012-02-12 17:51:21 -08004125 // If the buffer which was just released is part of the buffer at the head
4126 // of the queue, be sure to update the amt of the buffer which has been
4127 // consumed. If the buffer being returned is not part of the head of the
4128 // queue, its either because the buffer is part of the silence buffer, or
4129 // because the head of the timed queue was trimmed after the mixer called
4130 // getNextBuffer but before the mixer called releaseBuffer.
4131 if ((buffer->raw != mTimedSilenceBuffer) && mTimedBufferQueue.size()) {
John Grossmand8cf2962012-02-08 16:37:41 -08004132 TimedBuffer& head = mTimedBufferQueue.editItemAt(0);
John Grossman8c010612012-02-12 17:51:21 -08004133
4134 void* start = head.buffer()->pointer();
Glenn Kasten175b2be2012-02-17 16:24:10 -08004135 void* end = (char *) head.buffer()->pointer() + head.buffer()->size();
John Grossman8c010612012-02-12 17:51:21 -08004136
4137 if ((buffer->raw >= start) && (buffer->raw <= end)) {
4138 head.setPosition(head.position() +
4139 (buffer->frameCount * mCblk->frameSize));
4140 if (static_cast<size_t>(head.position()) >= head.buffer()->size()) {
4141 mTimedBufferQueue.removeAt(0);
4142 }
John Grossmand8cf2962012-02-08 16:37:41 -08004143 }
4144 }
4145
4146 buffer->raw = 0;
4147 buffer->frameCount = 0;
4148}
4149
4150uint32_t AudioFlinger::PlaybackThread::TimedTrack::framesReady() const {
4151 Mutex::Autolock _l(mTimedBufferQueueLock);
4152
4153 uint32_t frames = 0;
4154 for (size_t i = 0; i < mTimedBufferQueue.size(); i++) {
4155 const TimedBuffer& tb = mTimedBufferQueue[i];
4156 frames += (tb.buffer()->size() - tb.position()) / mCblk->frameSize;
4157 }
4158
4159 return frames;
4160}
4161
4162AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer()
4163 : mPTS(0), mPosition(0) {}
4164
4165AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer(
4166 const sp<IMemory>& buffer, int64_t pts)
4167 : mBuffer(buffer), mPTS(pts), mPosition(0) {}
4168
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004169// ----------------------------------------------------------------------------
4170
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004171// RecordTrack constructor must be called with AudioFlinger::mLock held
Eric Laurenta553c252009-07-17 12:17:14 -07004172AudioFlinger::RecordThread::RecordTrack::RecordTrack(
Glenn Kasten685c9ce2012-01-20 13:32:16 -08004173 RecordThread *thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08004176 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004177 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -07004179 int sessionId)
Eric Laurenta553c252009-07-17 12:17:14 -07004180 : TrackBase(thread, client, sampleRate, format,
Glenn Kasten35269822012-02-21 10:35:56 -08004181 channelMask, frameCount, 0 /*sharedBuffer*/, sessionId),
Eric Laurenta553c252009-07-17 12:17:14 -07004182 mOverflow(false)
4183{
Eric Laurent8a77a992009-09-09 05:16:08 -07004184 if (mCblk != NULL) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07004185 ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer);
4186 if (format == AUDIO_FORMAT_PCM_16_BIT) {
4187 mCblk->frameSize = mChannelCount * sizeof(int16_t);
4188 } else if (format == AUDIO_FORMAT_PCM_8_BIT) {
4189 mCblk->frameSize = mChannelCount * sizeof(int8_t);
4190 } else {
4191 mCblk->frameSize = sizeof(int8_t);
4192 }
Eric Laurent8a77a992009-09-09 05:16:08 -07004193 }
Eric Laurenta553c252009-07-17 12:17:14 -07004194}
4195
4196AudioFlinger::RecordThread::RecordTrack::~RecordTrack()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197{
Eric Laurent49f02be2009-11-19 09:00:56 -08004198 sp<ThreadBase> thread = mThread.promote();
4199 if (thread != 0) {
4200 AudioSystem::releaseInput(thread->id());
4201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202}
4203
Glenn Kastenc2db1192012-02-22 10:47:35 -08004204// AudioBufferProvider interface
John Grossmand8cf2962012-02-08 16:37:41 -08004205status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206{
4207 audio_track_cblk_t* cblk = this->cblk();
4208 uint32_t framesAvail;
4209 uint32_t framesReq = buffer->frameCount;
4210
Glenn Kasten18db49a2012-03-12 16:29:55 -07004211 // Check if last stepServer failed, try to step now
Glenn Kasten35269822012-02-21 10:35:56 -08004212 if (mStepServerFailed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 if (!step()) goto getNextBuffer_exit;
Steve Block71f2cf12011-10-20 11:56:00 +01004214 ALOGV("stepServer recovered");
Glenn Kasten35269822012-02-21 10:35:56 -08004215 mStepServerFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 }
4217
4218 framesAvail = cblk->framesAvailable_l();
4219
Glenn Kastene80a4cc2011-12-15 09:51:17 -08004220 if (CC_LIKELY(framesAvail)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 uint32_t s = cblk->server;
4222 uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
4223
4224 if (framesReq > framesAvail) {
4225 framesReq = framesAvail;
4226 }
4227 if (s + framesReq > bufferEnd) {
4228 framesReq = bufferEnd - s;
4229 }
4230
4231 buffer->raw = getBuffer(s, framesReq);
Glenn Kastenc434c902011-12-13 11:53:26 -08004232 if (buffer->raw == NULL) goto getNextBuffer_exit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233
4234 buffer->frameCount = framesReq;
4235 return NO_ERROR;
4236 }
4237
4238getNextBuffer_exit:
Glenn Kastenc434c902011-12-13 11:53:26 -08004239 buffer->raw = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240 buffer->frameCount = 0;
4241 return NOT_ENOUGH_DATA;
4242}
4243
Glenn Kasten6a20b262012-02-02 10:56:47 -08004244status_t AudioFlinger::RecordThread::RecordTrack::start(pid_t tid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245{
Eric Laurenta553c252009-07-17 12:17:14 -07004246 sp<ThreadBase> thread = mThread.promote();
4247 if (thread != 0) {
4248 RecordThread *recordThread = (RecordThread *)thread.get();
Glenn Kasten6a20b262012-02-02 10:56:47 -08004249 return recordThread->start(this, tid);
Eric Laurent49f02be2009-11-19 09:00:56 -08004250 } else {
4251 return BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -07004252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253}
4254
Eric Laurenta553c252009-07-17 12:17:14 -07004255void AudioFlinger::RecordThread::RecordTrack::stop()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256{
Eric Laurenta553c252009-07-17 12:17:14 -07004257 sp<ThreadBase> thread = mThread.promote();
4258 if (thread != 0) {
4259 RecordThread *recordThread = (RecordThread *)thread.get();
4260 recordThread->stop(this);
Eric Laurentae29b762011-03-28 18:37:07 -07004261 TrackBase::reset();
Glenn Kasten76b6c0c2012-02-14 08:52:15 -08004262 // Force overrun condition to avoid false overrun callback until first data is
Eric Laurentae29b762011-03-28 18:37:07 -07004263 // read from buffer
4264 android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags);
Eric Laurenta553c252009-07-17 12:17:14 -07004265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004266}
4267
Eric Laurent3fdb1262009-11-07 00:01:32 -08004268void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size)
4269{
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004270 snprintf(buffer, size, " %05d %03u 0x%08x %05d %04u %01d %05u %08x %08x\n",
Glenn Kasten81211142012-02-05 18:09:08 -08004271 (mClient == 0) ? getpid_cached : mClient->pid(),
Eric Laurent3fdb1262009-11-07 00:01:32 -08004272 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004273 mChannelMask,
Eric Laurent65b65452010-06-01 23:49:17 -07004274 mSessionId,
Eric Laurent3fdb1262009-11-07 00:01:32 -08004275 mFrameCount,
4276 mState,
4277 mCblk->sampleRate,
4278 mCblk->server,
4279 mCblk->user);
4280}
4281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004282
4283// ----------------------------------------------------------------------------
4284
Eric Laurenta553c252009-07-17 12:17:14 -07004285AudioFlinger::PlaybackThread::OutputTrack::OutputTrack(
Glenn Kasten685c9ce2012-01-20 13:32:16 -08004286 PlaybackThread *playbackThread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004287 DuplicatingThread *sourceThread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08004289 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004290 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 int frameCount)
Glenn Kasten685c9ce2012-01-20 13:32:16 -08004292 : Track(playbackThread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0),
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004293 mActive(false), mSourceThread(sourceThread)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294{
Eric Laurenta553c252009-07-17 12:17:14 -07004295
Eric Laurent6c30a712009-08-10 23:22:32 -07004296 if (mCblk != NULL) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -07004297 mCblk->flags |= CBLK_DIRECTION_OUT;
Eric Laurent6c30a712009-08-10 23:22:32 -07004298 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
Eric Laurent6c30a712009-08-10 23:22:32 -07004299 mOutBuffer.frameCount = 0;
Eric Laurent6c30a712009-08-10 23:22:32 -07004300 playbackThread->mTracks.add(this);
Steve Block71f2cf12011-10-20 11:56:00 +01004301 ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004302 "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p",
4303 mCblk, mBuffer, mCblk->buffers,
4304 mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd);
Eric Laurent6c30a712009-08-10 23:22:32 -07004305 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00004306 ALOGW("Error creating output track on thread %p", playbackThread);
Eric Laurent6c30a712009-08-10 23:22:32 -07004307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004308}
4309
Eric Laurenta553c252009-07-17 12:17:14 -07004310AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311{
Eric Laurent6c30a712009-08-10 23:22:32 -07004312 clearBufferQueue();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004313}
4314
Glenn Kasten6a20b262012-02-02 10:56:47 -08004315status_t AudioFlinger::PlaybackThread::OutputTrack::start(pid_t tid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316{
Glenn Kasten6a20b262012-02-02 10:56:47 -08004317 status_t status = Track::start(tid);
Eric Laurenta553c252009-07-17 12:17:14 -07004318 if (status != NO_ERROR) {
4319 return status;
4320 }
4321
4322 mActive = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 mRetryCount = 127;
4324 return status;
4325}
4326
Eric Laurenta553c252009-07-17 12:17:14 -07004327void AudioFlinger::PlaybackThread::OutputTrack::stop()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004328{
4329 Track::stop();
4330 clearBufferQueue();
4331 mOutBuffer.frameCount = 0;
Eric Laurenta553c252009-07-17 12:17:14 -07004332 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333}
4334
Eric Laurenta553c252009-07-17 12:17:14 -07004335bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336{
4337 Buffer *pInBuffer;
4338 Buffer inBuffer;
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004339 uint32_t channelCount = mChannelCount;
Eric Laurenta553c252009-07-17 12:17:14 -07004340 bool outputBufferFull = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004341 inBuffer.frameCount = frames;
4342 inBuffer.i16 = data;
Eric Laurenta553c252009-07-17 12:17:14 -07004343
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004344 uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs();
Eric Laurenta553c252009-07-17 12:17:14 -07004345
Eric Laurent62443f52009-10-05 20:29:18 -07004346 if (!mActive && frames != 0) {
Glenn Kasten6a20b262012-02-02 10:56:47 -08004347 start(0);
Eric Laurenta553c252009-07-17 12:17:14 -07004348 sp<ThreadBase> thread = mThread.promote();
4349 if (thread != 0) {
4350 MixerThread *mixerThread = (MixerThread *)thread.get();
4351 if (mCblk->frameCount > frames){
4352 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
4353 uint32_t startFrames = (mCblk->frameCount - frames);
4354 pInBuffer = new Buffer;
Eric Laurentb0a01472010-05-14 05:45:46 -07004355 pInBuffer->mBuffer = new int16_t[startFrames * channelCount];
Eric Laurenta553c252009-07-17 12:17:14 -07004356 pInBuffer->frameCount = startFrames;
4357 pInBuffer->i16 = pInBuffer->mBuffer;
Eric Laurentb0a01472010-05-14 05:45:46 -07004358 memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t));
Eric Laurenta553c252009-07-17 12:17:14 -07004359 mBufferQueue.add(pInBuffer);
4360 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00004361 ALOGW ("OutputTrack::write() %p no more buffers in queue", this);
Eric Laurenta553c252009-07-17 12:17:14 -07004362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 }
Eric Laurenta553c252009-07-17 12:17:14 -07004364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 }
4366
Eric Laurenta553c252009-07-17 12:17:14 -07004367 while (waitTimeLeftMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 // First write pending buffers, then new data
4369 if (mBufferQueue.size()) {
4370 pInBuffer = mBufferQueue.itemAt(0);
4371 } else {
4372 pInBuffer = &inBuffer;
4373 }
Eric Laurenta553c252009-07-17 12:17:14 -07004374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 if (pInBuffer->frameCount == 0) {
4376 break;
4377 }
Eric Laurenta553c252009-07-17 12:17:14 -07004378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 if (mOutBuffer.frameCount == 0) {
4380 mOutBuffer.frameCount = pInBuffer->frameCount;
Eric Laurenta553c252009-07-17 12:17:14 -07004381 nsecs_t startTime = systemTime();
Glenn Kasten34f9f8b2012-01-20 17:00:00 -08004382 if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)NO_MORE_BUFFERS) {
Steve Block71f2cf12011-10-20 11:56:00 +01004383 ALOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get());
Eric Laurenta553c252009-07-17 12:17:14 -07004384 outputBufferFull = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004385 break;
4386 }
Eric Laurenta553c252009-07-17 12:17:14 -07004387 uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime);
Eric Laurenta553c252009-07-17 12:17:14 -07004388 if (waitTimeLeftMs >= waitTimeMs) {
4389 waitTimeLeftMs -= waitTimeMs;
4390 } else {
4391 waitTimeLeftMs = 0;
4392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 }
Eric Laurenta553c252009-07-17 12:17:14 -07004394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount;
Eric Laurentb0a01472010-05-14 05:45:46 -07004396 memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004397 mCblk->stepUser(outFrames);
4398 pInBuffer->frameCount -= outFrames;
Eric Laurentb0a01472010-05-14 05:45:46 -07004399 pInBuffer->i16 += outFrames * channelCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 mOutBuffer.frameCount -= outFrames;
Eric Laurentb0a01472010-05-14 05:45:46 -07004401 mOutBuffer.i16 += outFrames * channelCount;
Eric Laurenta553c252009-07-17 12:17:14 -07004402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 if (pInBuffer->frameCount == 0) {
4404 if (mBufferQueue.size()) {
4405 mBufferQueue.removeAt(0);
4406 delete [] pInBuffer->mBuffer;
4407 delete pInBuffer;
Steve Block71f2cf12011-10-20 11:56:00 +01004408 ALOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 } else {
4410 break;
4411 }
4412 }
4413 }
Eric Laurenta553c252009-07-17 12:17:14 -07004414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004415 // If we could not write all frames, allocate a buffer and queue it for next time.
4416 if (inBuffer.frameCount) {
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004417 sp<ThreadBase> thread = mThread.promote();
4418 if (thread != 0 && !thread->standby()) {
4419 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
4420 pInBuffer = new Buffer;
Eric Laurentb0a01472010-05-14 05:45:46 -07004421 pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount];
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004422 pInBuffer->frameCount = inBuffer.frameCount;
4423 pInBuffer->i16 = pInBuffer->mBuffer;
Eric Laurentb0a01472010-05-14 05:45:46 -07004424 memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t));
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004425 mBufferQueue.add(pInBuffer);
Steve Block71f2cf12011-10-20 11:56:00 +01004426 ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size());
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004427 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00004428 ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 }
4431 }
Eric Laurenta553c252009-07-17 12:17:14 -07004432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 // Calling write() with a 0 length buffer, means that no more data will be written:
Eric Laurenta553c252009-07-17 12:17:14 -07004434 // If no more buffers are pending, fill output track buffer to make sure it is started
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 // by output mixer.
Eric Laurenta553c252009-07-17 12:17:14 -07004436 if (frames == 0 && mBufferQueue.size() == 0) {
4437 if (mCblk->user < mCblk->frameCount) {
4438 frames = mCblk->frameCount - mCblk->user;
4439 pInBuffer = new Buffer;
Eric Laurentb0a01472010-05-14 05:45:46 -07004440 pInBuffer->mBuffer = new int16_t[frames * channelCount];
Eric Laurenta553c252009-07-17 12:17:14 -07004441 pInBuffer->frameCount = frames;
4442 pInBuffer->i16 = pInBuffer->mBuffer;
Eric Laurentb0a01472010-05-14 05:45:46 -07004443 memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t));
Eric Laurenta553c252009-07-17 12:17:14 -07004444 mBufferQueue.add(pInBuffer);
Eric Laurent62443f52009-10-05 20:29:18 -07004445 } else if (mActive) {
Eric Laurenta553c252009-07-17 12:17:14 -07004446 stop();
4447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 }
4449
Eric Laurenta553c252009-07-17 12:17:14 -07004450 return outputBufferFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451}
4452
Eric Laurenta553c252009-07-17 12:17:14 -07004453status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454{
4455 int active;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 status_t result;
4457 audio_track_cblk_t* cblk = mCblk;
4458 uint32_t framesReq = buffer->frameCount;
4459
Steve Block71f2cf12011-10-20 11:56:00 +01004460// ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 buffer->frameCount = 0;
Eric Laurenta553c252009-07-17 12:17:14 -07004462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 uint32_t framesAvail = cblk->framesAvailable();
4464
Eric Laurenta553c252009-07-17 12:17:14 -07004465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 if (framesAvail == 0) {
Eric Laurenta553c252009-07-17 12:17:14 -07004467 Mutex::Autolock _l(cblk->lock);
4468 goto start_loop_here;
4469 while (framesAvail == 0) {
4470 active = mActive;
Glenn Kastene80a4cc2011-12-15 09:51:17 -08004471 if (CC_UNLIKELY(!active)) {
Steve Block71f2cf12011-10-20 11:56:00 +01004472 ALOGV("Not active and NO_MORE_BUFFERS");
Glenn Kasten34f9f8b2012-01-20 17:00:00 -08004473 return NO_MORE_BUFFERS;
Eric Laurenta553c252009-07-17 12:17:14 -07004474 }
4475 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
4476 if (result != NO_ERROR) {
Glenn Kasten34f9f8b2012-01-20 17:00:00 -08004477 return NO_MORE_BUFFERS;
Eric Laurenta553c252009-07-17 12:17:14 -07004478 }
4479 // read the server count again
4480 start_loop_here:
4481 framesAvail = cblk->framesAvailable_l();
4482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 }
4484
Eric Laurenta553c252009-07-17 12:17:14 -07004485// if (framesAvail < framesReq) {
Glenn Kasten34f9f8b2012-01-20 17:00:00 -08004486// return NO_MORE_BUFFERS;
Eric Laurenta553c252009-07-17 12:17:14 -07004487// }
4488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 if (framesReq > framesAvail) {
4490 framesReq = framesAvail;
4491 }
4492
4493 uint32_t u = cblk->user;
4494 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
4495
4496 if (u + framesReq > bufferEnd) {
4497 framesReq = bufferEnd - u;
4498 }
4499
4500 buffer->frameCount = framesReq;
4501 buffer->raw = (void *)cblk->buffer(u);
4502 return NO_ERROR;
4503}
4504
4505
Eric Laurenta553c252009-07-17 12:17:14 -07004506void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507{
4508 size_t size = mBufferQueue.size();
Eric Laurenta553c252009-07-17 12:17:14 -07004509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004510 for (size_t i = 0; i < size; i++) {
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08004511 Buffer *pBuffer = mBufferQueue.itemAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 delete [] pBuffer->mBuffer;
4513 delete pBuffer;
4514 }
4515 mBufferQueue.clear();
4516}
4517
4518// ----------------------------------------------------------------------------
4519
4520AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
4521 : RefBase(),
4522 mAudioFlinger(audioFlinger),
Glenn Kastenb3db2132012-01-19 08:59:58 -08004523 // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below
Mathias Agopian6faf7892010-01-25 19:00:00 -08004524 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossmand8cf2962012-02-08 16:37:41 -08004525 mPid(pid),
4526 mTimedTrackCount(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004527{
4528 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
4529}
4530
Eric Laurentb9481d82009-09-17 05:12:56 -07004531// Client destructor must be called with AudioFlinger::mLock held
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532AudioFlinger::Client::~Client()
4533{
Eric Laurentb9481d82009-09-17 05:12:56 -07004534 mAudioFlinger->removeClient_l(mPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535}
4536
Glenn Kasten1f812f72012-01-30 10:15:48 -08004537sp<MemoryDealer> AudioFlinger::Client::heap() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538{
4539 return mMemoryDealer;
4540}
4541
John Grossmand8cf2962012-02-08 16:37:41 -08004542// Reserve one of the limited slots for a timed audio track associated
4543// with this client
4544bool AudioFlinger::Client::reserveTimedTrack()
4545{
4546 const int kMaxTimedTracksPerClient = 4;
4547
4548 Mutex::Autolock _l(mTimedTrackLock);
4549
4550 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
4551 ALOGW("can not create timed track - pid %d has exceeded the limit",
4552 mPid);
4553 return false;
4554 }
4555
4556 mTimedTrackCount++;
4557 return true;
4558}
4559
4560// Release a slot for a timed audio track
4561void AudioFlinger::Client::releaseTimedTrack()
4562{
4563 Mutex::Autolock _l(mTimedTrackLock);
4564 mTimedTrackCount--;
4565}
4566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567// ----------------------------------------------------------------------------
4568
Eric Laurent4f0f17d2010-05-12 02:05:53 -07004569AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
4570 const sp<IAudioFlingerClient>& client,
4571 pid_t pid)
Glenn Kastendc3ac852012-01-25 15:28:08 -08004572 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Eric Laurent4f0f17d2010-05-12 02:05:53 -07004573{
4574}
4575
4576AudioFlinger::NotificationClient::~NotificationClient()
4577{
Eric Laurent4f0f17d2010-05-12 02:05:53 -07004578}
4579
4580void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
4581{
4582 sp<NotificationClient> keep(this);
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08004583 mAudioFlinger->removeNotificationClient(mPid);
Eric Laurent4f0f17d2010-05-12 02:05:53 -07004584}
4585
4586// ----------------------------------------------------------------------------
4587
Eric Laurenta553c252009-07-17 12:17:14 -07004588AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004589 : BnAudioTrack(),
4590 mTrack(track)
4591{
4592}
4593
4594AudioFlinger::TrackHandle::~TrackHandle() {
4595 // just stop the track on deletion, associated resources
4596 // will be freed from the main thread once all pending buffers have
4597 // been played. Unless it's not in the active track list, in which
4598 // case we free everything now...
4599 mTrack->destroy();
4600}
4601
Glenn Kasten0ae4d972012-01-26 13:40:12 -08004602sp<IMemory> AudioFlinger::TrackHandle::getCblk() const {
4603 return mTrack->getCblk();
4604}
4605
Glenn Kasten6a20b262012-02-02 10:56:47 -08004606status_t AudioFlinger::TrackHandle::start(pid_t tid) {
4607 return mTrack->start(tid);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004608}
4609
4610void AudioFlinger::TrackHandle::stop() {
4611 mTrack->stop();
4612}
4613
4614void AudioFlinger::TrackHandle::flush() {
4615 mTrack->flush();
4616}
4617
4618void AudioFlinger::TrackHandle::mute(bool e) {
4619 mTrack->mute(e);
4620}
4621
4622void AudioFlinger::TrackHandle::pause() {
4623 mTrack->pause();
4624}
4625
Eric Laurent65b65452010-06-01 23:49:17 -07004626status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId)
4627{
4628 return mTrack->attachAuxEffect(EffectId);
4629}
4630
John Grossmand8cf2962012-02-08 16:37:41 -08004631status_t AudioFlinger::TrackHandle::allocateTimedBuffer(size_t size,
4632 sp<IMemory>* buffer) {
4633 if (!mTrack->isTimedTrack())
4634 return INVALID_OPERATION;
4635
4636 PlaybackThread::TimedTrack* tt =
4637 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
4638 return tt->allocateTimedBuffer(size, buffer);
4639}
4640
4641status_t AudioFlinger::TrackHandle::queueTimedBuffer(const sp<IMemory>& buffer,
4642 int64_t pts) {
4643 if (!mTrack->isTimedTrack())
4644 return INVALID_OPERATION;
4645
4646 PlaybackThread::TimedTrack* tt =
4647 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
4648 return tt->queueTimedBuffer(buffer, pts);
4649}
4650
4651status_t AudioFlinger::TrackHandle::setMediaTimeTransform(
4652 const LinearTransform& xform, int target) {
4653
4654 if (!mTrack->isTimedTrack())
4655 return INVALID_OPERATION;
4656
4657 PlaybackThread::TimedTrack* tt =
4658 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
4659 return tt->setMediaTimeTransform(
4660 xform, static_cast<TimedAudioTrack::TargetTimeline>(target));
4661}
4662
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004663status_t AudioFlinger::TrackHandle::onTransact(
4664 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
4665{
4666 return BnAudioTrack::onTransact(code, data, reply, flags);
4667}
4668
4669// ----------------------------------------------------------------------------
4670
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004671sp<IAudioRecord> AudioFlinger::openRecord(
4672 pid_t pid,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08004673 audio_io_handle_t input,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004674 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08004675 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004676 uint32_t channelMask,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004677 int frameCount,
Glenn Kasten84569cc2012-03-06 11:22:44 -08004678 IAudioFlinger::track_flags_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -07004679 int *sessionId,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004680 status_t *status)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004681{
Eric Laurenta553c252009-07-17 12:17:14 -07004682 sp<RecordThread::RecordTrack> recordTrack;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004683 sp<RecordHandle> recordHandle;
4684 sp<Client> client;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004685 status_t lStatus;
Eric Laurenta553c252009-07-17 12:17:14 -07004686 RecordThread *thread;
4687 size_t inFrameCount;
Eric Laurent65b65452010-06-01 23:49:17 -07004688 int lSessionId;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004689
4690 // check calling permissions
4691 if (!recordingAllowed()) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004692 lStatus = PERMISSION_DENIED;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004693 goto Exit;
4694 }
4695
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004696 // add client to list
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004697 { // scope for mLock
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004698 Mutex::Autolock _l(mLock);
Eric Laurenta553c252009-07-17 12:17:14 -07004699 thread = checkRecordThread_l(input);
4700 if (thread == NULL) {
4701 lStatus = BAD_VALUE;
4702 goto Exit;
4703 }
4704
Glenn Kasten803a86a2012-01-25 14:28:29 -08004705 client = registerPid_l(pid);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004706
Eric Laurent65b65452010-06-01 23:49:17 -07004707 // If no audio session id is provided, create one here
Dima Zavin24fc2fb2011-04-19 22:30:36 -07004708 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent65b65452010-06-01 23:49:17 -07004709 lSessionId = *sessionId;
4710 } else {
Eric Laurent464d5b32011-06-17 21:29:58 -07004711 lSessionId = nextUniqueId();
Eric Laurent65b65452010-06-01 23:49:17 -07004712 if (sessionId != NULL) {
4713 *sessionId = lSessionId;
4714 }
4715 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004716 // create new record track. The record track uses one track in mHardwareMixerThread by convention.
Eric Laurent464d5b32011-06-17 21:29:58 -07004717 recordTrack = thread->createRecordTrack_l(client,
4718 sampleRate,
4719 format,
4720 channelMask,
4721 frameCount,
Eric Laurent464d5b32011-06-17 21:29:58 -07004722 lSessionId,
4723 &lStatus);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004724 }
Eric Laurent464d5b32011-06-17 21:29:58 -07004725 if (lStatus != NO_ERROR) {
Eric Laurentb9481d82009-09-17 05:12:56 -07004726 // remove local strong reference to Client before deleting the RecordTrack so that the Client
4727 // destructor is called by the TrackBase destructor with mLock held
4728 client.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 recordTrack.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004730 goto Exit;
4731 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004732
4733 // return to handle to client
4734 recordHandle = new RecordHandle(recordTrack);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004735 lStatus = NO_ERROR;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004736
4737Exit:
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004738 if (status) {
4739 *status = lStatus;
4740 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004741 return recordHandle;
4742}
4743
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004744// ----------------------------------------------------------------------------
4745
Eric Laurenta553c252009-07-17 12:17:14 -07004746AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004747 : BnAudioRecord(),
4748 mRecordTrack(recordTrack)
4749{
4750}
4751
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004752AudioFlinger::RecordHandle::~RecordHandle() {
4753 stop();
4754}
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004755
Glenn Kasten0ae4d972012-01-26 13:40:12 -08004756sp<IMemory> AudioFlinger::RecordHandle::getCblk() const {
4757 return mRecordTrack->getCblk();
4758}
4759
Glenn Kasten6a20b262012-02-02 10:56:47 -08004760status_t AudioFlinger::RecordHandle::start(pid_t tid) {
Steve Block71f2cf12011-10-20 11:56:00 +01004761 ALOGV("RecordHandle::start()");
Glenn Kasten6a20b262012-02-02 10:56:47 -08004762 return mRecordTrack->start(tid);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004763}
4764
4765void AudioFlinger::RecordHandle::stop() {
Steve Block71f2cf12011-10-20 11:56:00 +01004766 ALOGV("RecordHandle::stop()");
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004767 mRecordTrack->stop();
4768}
4769
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004770status_t AudioFlinger::RecordHandle::onTransact(
4771 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
4772{
4773 return BnAudioRecord::onTransact(code, data, reply, flags);
4774}
4775
4776// ----------------------------------------------------------------------------
4777
Eric Laurent464d5b32011-06-17 21:29:58 -07004778AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger,
4779 AudioStreamIn *input,
4780 uint32_t sampleRate,
4781 uint32_t channels,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08004782 audio_io_handle_t id,
Eric Laurent464d5b32011-06-17 21:29:58 -07004783 uint32_t device) :
Glenn Kastenefd511a2012-01-26 10:38:26 -08004784 ThreadBase(audioFlinger, id, device, RECORD),
Glenn Kastendc3ac852012-01-25 15:28:08 -08004785 mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL),
4786 // mRsmpInIndex and mInputBytes set by readInputParameters()
4787 mReqChannelCount(popcount(channels)),
4788 mReqSampleRate(sampleRate)
4789 // mBytesRead is only meaningful while active, and so is cleared in start()
4790 // (but might be better to also clear here for dump?)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004791{
Glenn Kasten86e33622012-02-28 12:30:08 -08004792 snprintf(mName, kNameLength, "AudioIn_%X", id);
Eric Laurent6dbdc402011-07-22 09:04:31 -07004793
Eric Laurenta553c252009-07-17 12:17:14 -07004794 readInputParameters();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004795}
4796
Eric Laurenta553c252009-07-17 12:17:14 -07004797
4798AudioFlinger::RecordThread::~RecordThread()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004799{
Eric Laurenta553c252009-07-17 12:17:14 -07004800 delete[] mRsmpInBuffer;
Glenn Kasten2589cf92012-01-27 18:08:45 -08004801 delete mResampler;
4802 delete[] mRsmpOutBuffer;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004803}
4804
Eric Laurenta553c252009-07-17 12:17:14 -07004805void AudioFlinger::RecordThread::onFirstRef()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004806{
Eric Laurent6dbdc402011-07-22 09:04:31 -07004807 run(mName, PRIORITY_URGENT_AUDIO);
Eric Laurenta553c252009-07-17 12:17:14 -07004808}
Eric Laurent49f02be2009-11-19 09:00:56 -08004809
Eric Laurent828b9772011-08-07 16:32:26 -07004810status_t AudioFlinger::RecordThread::readyToRun()
4811{
4812 status_t status = initCheck();
Steve Block8564c8d2012-01-05 23:22:43 +00004813 ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this);
Eric Laurent828b9772011-08-07 16:32:26 -07004814 return status;
4815}
4816
Eric Laurenta553c252009-07-17 12:17:14 -07004817bool AudioFlinger::RecordThread::threadLoop()
4818{
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004819 AudioBufferProvider::Buffer buffer;
Eric Laurenta553c252009-07-17 12:17:14 -07004820 sp<RecordTrack> activeTrack;
Eric Laurent464d5b32011-06-17 21:29:58 -07004821 Vector< sp<EffectChain> > effectChains;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004822
Eric Laurent4712baa2010-09-30 16:12:31 -07004823 nsecs_t lastWarning = 0;
4824
Eric Laurent6dbdc402011-07-22 09:04:31 -07004825 acquireWakeLock();
4826
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004827 // start recording
4828 while (!exitPending()) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004829
Eric Laurenta553c252009-07-17 12:17:14 -07004830 processConfigEvents();
4831
4832 { // scope for mLock
4833 Mutex::Autolock _l(mLock);
4834 checkForNewParameters_l();
4835 if (mActiveTrack == 0 && mConfigEvents.isEmpty()) {
4836 if (!mStandby) {
Dima Zavin31f188892011-04-18 16:57:27 -07004837 mInput->stream->common.standby(&mInput->stream->common);
Eric Laurenta553c252009-07-17 12:17:14 -07004838 mStandby = true;
4839 }
4840
4841 if (exitPending()) break;
4842
Eric Laurent6dbdc402011-07-22 09:04:31 -07004843 releaseWakeLock_l();
Steve Block71f2cf12011-10-20 11:56:00 +01004844 ALOGV("RecordThread: loop stopping");
Eric Laurenta553c252009-07-17 12:17:14 -07004845 // go to sleep
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004846 mWaitWorkCV.wait(mLock);
Steve Block71f2cf12011-10-20 11:56:00 +01004847 ALOGV("RecordThread: loop starting");
Eric Laurent6dbdc402011-07-22 09:04:31 -07004848 acquireWakeLock_l();
Eric Laurenta553c252009-07-17 12:17:14 -07004849 continue;
4850 }
4851 if (mActiveTrack != 0) {
4852 if (mActiveTrack->mState == TrackBase::PAUSING) {
Eric Laurent9cc489a22009-12-05 05:20:01 -08004853 if (!mStandby) {
Dima Zavin31f188892011-04-18 16:57:27 -07004854 mInput->stream->common.standby(&mInput->stream->common);
Eric Laurent9cc489a22009-12-05 05:20:01 -08004855 mStandby = true;
4856 }
Eric Laurenta553c252009-07-17 12:17:14 -07004857 mActiveTrack.clear();
4858 mStartStopCond.broadcast();
4859 } else if (mActiveTrack->mState == TrackBase::RESUMING) {
Eric Laurenta553c252009-07-17 12:17:14 -07004860 if (mReqChannelCount != mActiveTrack->channelCount()) {
4861 mActiveTrack.clear();
Eric Laurent9cc489a22009-12-05 05:20:01 -08004862 mStartStopCond.broadcast();
4863 } else if (mBytesRead != 0) {
4864 // record start succeeds only if first read from audio input
4865 // succeeds
4866 if (mBytesRead > 0) {
4867 mActiveTrack->mState = TrackBase::ACTIVE;
4868 } else {
4869 mActiveTrack.clear();
4870 }
4871 mStartStopCond.broadcast();
Eric Laurenta553c252009-07-17 12:17:14 -07004872 }
Eric Laurent9cc489a22009-12-05 05:20:01 -08004873 mStandby = false;
Eric Laurenta553c252009-07-17 12:17:14 -07004874 }
Eric Laurenta553c252009-07-17 12:17:14 -07004875 }
Eric Laurent464d5b32011-06-17 21:29:58 -07004876 lockEffectChains_l(effectChains);
Eric Laurenta553c252009-07-17 12:17:14 -07004877 }
4878
4879 if (mActiveTrack != 0) {
Eric Laurent9cc489a22009-12-05 05:20:01 -08004880 if (mActiveTrack->mState != TrackBase::ACTIVE &&
4881 mActiveTrack->mState != TrackBase::RESUMING) {
Eric Laurent464d5b32011-06-17 21:29:58 -07004882 unlockEffectChains(effectChains);
4883 usleep(kRecordThreadSleepUs);
Eric Laurent49f02be2009-11-19 09:00:56 -08004884 continue;
4885 }
Eric Laurent464d5b32011-06-17 21:29:58 -07004886 for (size_t i = 0; i < effectChains.size(); i ++) {
4887 effectChains[i]->process_l();
4888 }
Eric Laurent464d5b32011-06-17 21:29:58 -07004889
Eric Laurenta553c252009-07-17 12:17:14 -07004890 buffer.frameCount = mFrameCount;
Glenn Kastenc2db1192012-02-22 10:47:35 -08004891 if (CC_LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) {
Eric Laurenta553c252009-07-17 12:17:14 -07004892 size_t framesOut = buffer.frameCount;
Glenn Kastenc434c902011-12-13 11:53:26 -08004893 if (mResampler == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -07004894 // no resampling
4895 while (framesOut) {
4896 size_t framesIn = mFrameCount - mRsmpInIndex;
4897 if (framesIn) {
4898 int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize;
4899 int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize;
4900 if (framesIn > framesOut)
4901 framesIn = framesOut;
4902 mRsmpInIndex += framesIn;
4903 framesOut -= framesIn;
Eric Laurentb0a01472010-05-14 05:45:46 -07004904 if ((int)mChannelCount == mReqChannelCount ||
Dima Zavin24fc2fb2011-04-19 22:30:36 -07004905 mFormat != AUDIO_FORMAT_PCM_16_BIT) {
Eric Laurenta553c252009-07-17 12:17:14 -07004906 memcpy(dst, src, framesIn * mFrameSize);
4907 } else {
4908 int16_t *src16 = (int16_t *)src;
4909 int16_t *dst16 = (int16_t *)dst;
4910 if (mChannelCount == 1) {
4911 while (framesIn--) {
4912 *dst16++ = *src16;
4913 *dst16++ = *src16++;
4914 }
4915 } else {
4916 while (framesIn--) {
4917 *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1);
4918 src16 += 2;
4919 }
4920 }
4921 }
4922 }
4923 if (framesOut && mFrameCount == mRsmpInIndex) {
Eric Laurenta553c252009-07-17 12:17:14 -07004924 if (framesOut == mFrameCount &&
Dima Zavin24fc2fb2011-04-19 22:30:36 -07004925 ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) {
Dima Zavin31f188892011-04-18 16:57:27 -07004926 mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes);
Eric Laurenta553c252009-07-17 12:17:14 -07004927 framesOut = 0;
4928 } else {
Dima Zavin31f188892011-04-18 16:57:27 -07004929 mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes);
Eric Laurenta553c252009-07-17 12:17:14 -07004930 mRsmpInIndex = 0;
4931 }
Eric Laurent9cc489a22009-12-05 05:20:01 -08004932 if (mBytesRead < 0) {
Steve Block3762c312012-01-06 19:20:56 +00004933 ALOGE("Error reading audio input");
Eric Laurent9cc489a22009-12-05 05:20:01 -08004934 if (mActiveTrack->mState == TrackBase::ACTIVE) {
Eric Laurentba8811f2010-03-02 18:38:06 -08004935 // Force input into standby so that it tries to
4936 // recover at next read attempt
Dima Zavin31f188892011-04-18 16:57:27 -07004937 mInput->stream->common.standby(&mInput->stream->common);
Eric Laurent464d5b32011-06-17 21:29:58 -07004938 usleep(kRecordThreadSleepUs);
Eric Laurent9cc489a22009-12-05 05:20:01 -08004939 }
Eric Laurenta553c252009-07-17 12:17:14 -07004940 mRsmpInIndex = mFrameCount;
4941 framesOut = 0;
4942 buffer.frameCount = 0;
4943 }
4944 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004945 }
4946 } else {
Eric Laurenta553c252009-07-17 12:17:14 -07004947 // resampling
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004948
Eric Laurenta553c252009-07-17 12:17:14 -07004949 memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t));
4950 // alter output frame count as if we were expecting stereo samples
4951 if (mChannelCount == 1 && mReqChannelCount == 1) {
4952 framesOut >>= 1;
4953 }
4954 mResampler->resample(mRsmpOutBuffer, framesOut, this);
4955 // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer()
4956 // are 32 bit aligned which should be always true.
4957 if (mChannelCount == 2 && mReqChannelCount == 1) {
Glenn Kasten490909d2011-12-15 09:52:39 -08004958 ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut);
Eric Laurenta553c252009-07-17 12:17:14 -07004959 // the resampler always outputs stereo samples: do post stereo to mono conversion
4960 int16_t *src = (int16_t *)mRsmpOutBuffer;
4961 int16_t *dst = buffer.i16;
4962 while (framesOut--) {
4963 *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1);
4964 src += 2;
4965 }
4966 } else {
Glenn Kasten490909d2011-12-15 09:52:39 -08004967 ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut);
Eric Laurenta553c252009-07-17 12:17:14 -07004968 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004969
Eric Laurenta553c252009-07-17 12:17:14 -07004970 }
4971 mActiveTrack->releaseBuffer(&buffer);
4972 mActiveTrack->overflow();
4973 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004974 // client isn't retrieving buffers fast enough
4975 else {
Eric Laurent4712baa2010-09-30 16:12:31 -07004976 if (!mActiveTrack->setOverflow()) {
4977 nsecs_t now = systemTime();
Glenn Kastencbfe2e12011-12-13 11:04:14 -08004978 if ((now - lastWarning) > kWarningThrottleNs) {
Steve Block8564c8d2012-01-05 23:22:43 +00004979 ALOGW("RecordThread: buffer overflow");
Eric Laurent4712baa2010-09-30 16:12:31 -07004980 lastWarning = now;
4981 }
4982 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004983 // Release the processor for a while before asking for a new buffer.
4984 // This will give the application more chance to read from the buffer and
4985 // clear the overflow.
Eric Laurent464d5b32011-06-17 21:29:58 -07004986 usleep(kRecordThreadSleepUs);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004987 }
4988 }
Eric Laurent21b5c472011-07-26 20:54:46 -07004989 // enable changes in effect chain
4990 unlockEffectChains(effectChains);
Eric Laurent464d5b32011-06-17 21:29:58 -07004991 effectChains.clear();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004992 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004993
Eric Laurenta553c252009-07-17 12:17:14 -07004994 if (!mStandby) {
Dima Zavin31f188892011-04-18 16:57:27 -07004995 mInput->stream->common.standby(&mInput->stream->common);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004996 }
Eric Laurenta553c252009-07-17 12:17:14 -07004997 mActiveTrack.clear();
4998
Eric Laurent49f02be2009-11-19 09:00:56 -08004999 mStartStopCond.broadcast();
5000
Eric Laurent6dbdc402011-07-22 09:04:31 -07005001 releaseWakeLock();
5002
Steve Block71f2cf12011-10-20 11:56:00 +01005003 ALOGV("RecordThread %p exiting", this);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07005004 return false;
5005}
5006
Eric Laurent464d5b32011-06-17 21:29:58 -07005007
5008sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l(
5009 const sp<AudioFlinger::Client>& client,
5010 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005011 audio_format_t format,
Eric Laurent464d5b32011-06-17 21:29:58 -07005012 int channelMask,
5013 int frameCount,
Eric Laurent464d5b32011-06-17 21:29:58 -07005014 int sessionId,
5015 status_t *status)
5016{
5017 sp<RecordTrack> track;
5018 status_t lStatus;
5019
5020 lStatus = initCheck();
5021 if (lStatus != NO_ERROR) {
Steve Block3762c312012-01-06 19:20:56 +00005022 ALOGE("Audio driver not initialized.");
Eric Laurent464d5b32011-06-17 21:29:58 -07005023 goto Exit;
5024 }
5025
5026 { // scope for mLock
5027 Mutex::Autolock _l(mLock);
5028
5029 track = new RecordTrack(this, client, sampleRate,
Glenn Kasten35269822012-02-21 10:35:56 -08005030 format, channelMask, frameCount, sessionId);
Eric Laurent464d5b32011-06-17 21:29:58 -07005031
Glenn Kasten706b6182012-01-20 13:44:40 -08005032 if (track->getCblk() == 0) {
Eric Laurent464d5b32011-06-17 21:29:58 -07005033 lStatus = NO_MEMORY;
5034 goto Exit;
5035 }
5036
5037 mTrack = track.get();
Eric Laurent6639b552011-08-01 09:52:20 -07005038 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
5039 bool suspend = audio_is_bluetooth_sco_device(
Eric Laurent2d95dfb2011-08-29 12:42:48 -07005040 (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff();
Eric Laurent6639b552011-08-01 09:52:20 -07005041 setEffectSuspended_l(FX_IID_AEC, suspend, sessionId);
5042 setEffectSuspended_l(FX_IID_NS, suspend, sessionId);
Eric Laurent464d5b32011-06-17 21:29:58 -07005043 }
5044 lStatus = NO_ERROR;
5045
5046Exit:
5047 if (status) {
5048 *status = lStatus;
5049 }
5050 return track;
5051}
5052
Glenn Kasten6a20b262012-02-02 10:56:47 -08005053status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, pid_t tid)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07005054{
Glenn Kasten6a20b262012-02-02 10:56:47 -08005055 ALOGV("RecordThread::start tid=%d", tid);
Glenn Kasten18db49a2012-03-12 16:29:55 -07005056 sp<ThreadBase> strongMe = this;
Eric Laurent49f02be2009-11-19 09:00:56 -08005057 status_t status = NO_ERROR;
5058 {
Glenn Kasten325ee1c2012-01-05 15:41:56 -08005059 AutoMutex lock(mLock);
Eric Laurent49f02be2009-11-19 09:00:56 -08005060 if (mActiveTrack != 0) {
5061 if (recordTrack != mActiveTrack.get()) {
5062 status = -EBUSY;
5063 } else if (mActiveTrack->mState == TrackBase::PAUSING) {
Eric Laurent9cc489a22009-12-05 05:20:01 -08005064 mActiveTrack->mState = TrackBase::ACTIVE;
Eric Laurent49f02be2009-11-19 09:00:56 -08005065 }
5066 return status;
5067 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07005068
Eric Laurent49f02be2009-11-19 09:00:56 -08005069 recordTrack->mState = TrackBase::IDLE;
5070 mActiveTrack = recordTrack;
5071 mLock.unlock();
5072 status_t status = AudioSystem::startInput(mId);
5073 mLock.lock();
5074 if (status != NO_ERROR) {
5075 mActiveTrack.clear();
5076 return status;
5077 }
Eric Laurent9cc489a22009-12-05 05:20:01 -08005078 mRsmpInIndex = mFrameCount;
5079 mBytesRead = 0;
Eric Laurent4bb21c42011-02-28 16:52:51 -08005080 if (mResampler != NULL) {
5081 mResampler->reset();
5082 }
5083 mActiveTrack->mState = TrackBase::RESUMING;
Eric Laurent49f02be2009-11-19 09:00:56 -08005084 // signal thread to start
Steve Block71f2cf12011-10-20 11:56:00 +01005085 ALOGV("Signal record thread");
Eric Laurent49f02be2009-11-19 09:00:56 -08005086 mWaitWorkCV.signal();
5087 // do not wait for mStartStopCond if exiting
Glenn Kasten761286f2012-01-06 08:39:38 -08005088 if (exitPending()) {
Eric Laurent49f02be2009-11-19 09:00:56 -08005089 mActiveTrack.clear();
5090 status = INVALID_OPERATION;
5091 goto startError;
5092 }
5093 mStartStopCond.wait(mLock);
5094 if (mActiveTrack == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01005095 ALOGV("Record failed to start");
Eric Laurent49f02be2009-11-19 09:00:56 -08005096 status = BAD_VALUE;
5097 goto startError;
5098 }
Steve Block71f2cf12011-10-20 11:56:00 +01005099 ALOGV("Record started OK");
Eric Laurent49f02be2009-11-19 09:00:56 -08005100 return status;
Eric Laurenta553c252009-07-17 12:17:14 -07005101 }
Eric Laurent49f02be2009-11-19 09:00:56 -08005102startError:
5103 AudioSystem::stopInput(mId);
5104 return status;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07005105}
5106
Eric Laurenta553c252009-07-17 12:17:14 -07005107void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) {
Steve Block71f2cf12011-10-20 11:56:00 +01005108 ALOGV("RecordThread::stop");
Glenn Kasten18db49a2012-03-12 16:29:55 -07005109 sp<ThreadBase> strongMe = this;
Eric Laurent49f02be2009-11-19 09:00:56 -08005110 {
Glenn Kasten325ee1c2012-01-05 15:41:56 -08005111 AutoMutex lock(mLock);
Eric Laurent49f02be2009-11-19 09:00:56 -08005112 if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) {
5113 mActiveTrack->mState = TrackBase::PAUSING;
5114 // do not wait for mStartStopCond if exiting
Glenn Kasten761286f2012-01-06 08:39:38 -08005115 if (exitPending()) {
Eric Laurent49f02be2009-11-19 09:00:56 -08005116 return;
5117 }
5118 mStartStopCond.wait(mLock);
5119 // if we have been restarted, recordTrack == mActiveTrack.get() here
5120 if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) {
5121 mLock.unlock();
5122 AudioSystem::stopInput(mId);
5123 mLock.lock();
Steve Block71f2cf12011-10-20 11:56:00 +01005124 ALOGV("Record stopped OK");
Eric Laurent49f02be2009-11-19 09:00:56 -08005125 }
5126 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07005127 }
5128}
5129
Eric Laurenta553c252009-07-17 12:17:14 -07005130status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131{
5132 const size_t SIZE = 256;
5133 char buffer[SIZE];
5134 String8 result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135
Eric Laurent3fdb1262009-11-07 00:01:32 -08005136 snprintf(buffer, SIZE, "\nInput thread %p internals\n", this);
5137 result.append(buffer);
5138
5139 if (mActiveTrack != 0) {
5140 result.append("Active Track:\n");
Jean-Michel Trivi54392232011-05-24 15:53:33 -07005141 result.append(" Clien Fmt Chn mask Session Buf S SRate Serv User\n");
Eric Laurent3fdb1262009-11-07 00:01:32 -08005142 mActiveTrack->dump(buffer, SIZE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143 result.append(buffer);
Eric Laurent3fdb1262009-11-07 00:01:32 -08005144
5145 snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex);
5146 result.append(buffer);
5147 snprintf(buffer, SIZE, "In size: %d\n", mInputBytes);
5148 result.append(buffer);
Glenn Kastenc434c902011-12-13 11:53:26 -08005149 snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL));
Eric Laurent3fdb1262009-11-07 00:01:32 -08005150 result.append(buffer);
5151 snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount);
5152 result.append(buffer);
5153 snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate);
5154 result.append(buffer);
5155
5156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 } else {
5158 result.append("No record client\n");
5159 }
5160 write(fd, result.string(), result.size());
Eric Laurent3fdb1262009-11-07 00:01:32 -08005161
5162 dumpBase(fd, args);
Eric Laurent1345d332011-07-24 17:49:51 -07005163 dumpEffectChains(fd, args);
Eric Laurent3fdb1262009-11-07 00:01:32 -08005164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005165 return NO_ERROR;
5166}
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07005167
Glenn Kastenc2db1192012-02-22 10:47:35 -08005168// AudioBufferProvider interface
John Grossmand8cf2962012-02-08 16:37:41 -08005169status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts)
Eric Laurenta553c252009-07-17 12:17:14 -07005170{
5171 size_t framesReq = buffer->frameCount;
5172 size_t framesReady = mFrameCount - mRsmpInIndex;
5173 int channelCount;
5174
5175 if (framesReady == 0) {
Dima Zavin31f188892011-04-18 16:57:27 -07005176 mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes);
Eric Laurent9cc489a22009-12-05 05:20:01 -08005177 if (mBytesRead < 0) {
Steve Block3762c312012-01-06 19:20:56 +00005178 ALOGE("RecordThread::getNextBuffer() Error reading audio input");
Eric Laurent9cc489a22009-12-05 05:20:01 -08005179 if (mActiveTrack->mState == TrackBase::ACTIVE) {
Eric Laurentba8811f2010-03-02 18:38:06 -08005180 // Force input into standby so that it tries to
5181 // recover at next read attempt
Dima Zavin31f188892011-04-18 16:57:27 -07005182 mInput->stream->common.standby(&mInput->stream->common);
Eric Laurent464d5b32011-06-17 21:29:58 -07005183 usleep(kRecordThreadSleepUs);
Eric Laurent9cc489a22009-12-05 05:20:01 -08005184 }
Glenn Kastenc434c902011-12-13 11:53:26 -08005185 buffer->raw = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07005186 buffer->frameCount = 0;
5187 return NOT_ENOUGH_DATA;
5188 }
5189 mRsmpInIndex = 0;
5190 framesReady = mFrameCount;
5191 }
5192
5193 if (framesReq > framesReady) {
5194 framesReq = framesReady;
5195 }
5196
5197 if (mChannelCount == 1 && mReqChannelCount == 2) {
5198 channelCount = 1;
5199 } else {
5200 channelCount = 2;
5201 }
5202 buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount;
5203 buffer->frameCount = framesReq;
5204 return NO_ERROR;
5205}
5206
Glenn Kastenc2db1192012-02-22 10:47:35 -08005207// AudioBufferProvider interface
Eric Laurenta553c252009-07-17 12:17:14 -07005208void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer)
5209{
5210 mRsmpInIndex += buffer->frameCount;
5211 buffer->frameCount = 0;
5212}
5213
5214bool AudioFlinger::RecordThread::checkForNewParameters_l()
5215{
5216 bool reconfig = false;
5217
Eric Laurent8fce46a2009-08-04 09:45:33 -07005218 while (!mNewParameters.isEmpty()) {
Eric Laurenta553c252009-07-17 12:17:14 -07005219 status_t status = NO_ERROR;
Eric Laurent8fce46a2009-08-04 09:45:33 -07005220 String8 keyValuePair = mNewParameters[0];
5221 AudioParameter param = AudioParameter(keyValuePair);
Eric Laurenta553c252009-07-17 12:17:14 -07005222 int value;
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005223 audio_format_t reqFormat = mFormat;
Eric Laurenta553c252009-07-17 12:17:14 -07005224 int reqSamplingRate = mReqSampleRate;
5225 int reqChannelCount = mReqChannelCount;
Eric Laurent8fce46a2009-08-04 09:45:33 -07005226
Eric Laurenta553c252009-07-17 12:17:14 -07005227 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
5228 reqSamplingRate = value;
5229 reconfig = true;
5230 }
5231 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005232 reqFormat = (audio_format_t) value;
Eric Laurenta553c252009-07-17 12:17:14 -07005233 reconfig = true;
5234 }
5235 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005236 reqChannelCount = popcount(value);
Eric Laurenta553c252009-07-17 12:17:14 -07005237 reconfig = true;
5238 }
5239 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
5240 // do not accept frame count changes if tracks are open as the track buffer
Glenn Kastenb3db2132012-01-19 08:59:58 -08005241 // size depends on frame count and correct behavior would not be guaranteed
Eric Laurenta553c252009-07-17 12:17:14 -07005242 // if frame count is changed after track creation
5243 if (mActiveTrack != 0) {
5244 status = INVALID_OPERATION;
5245 } else {
5246 reconfig = true;
5247 }
5248 }
Eric Laurent464d5b32011-06-17 21:29:58 -07005249 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
5250 // forward device change to effects that have requested to be
5251 // aware of attached audio device.
5252 for (size_t i = 0; i < mEffectChains.size(); i++) {
5253 mEffectChains[i]->setDevice_l(value);
5254 }
5255 // store input device and output device but do not forward output device to audio HAL.
5256 // Note that status is ignored by the caller for output device
5257 // (see AudioFlinger::setParameters()
5258 if (value & AUDIO_DEVICE_OUT_ALL) {
5259 mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL);
5260 status = BAD_VALUE;
5261 } else {
5262 mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL);
Eric Laurent6639b552011-08-01 09:52:20 -07005263 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
5264 if (mTrack != NULL) {
5265 bool suspend = audio_is_bluetooth_sco_device(
Eric Laurent2d95dfb2011-08-29 12:42:48 -07005266 (audio_devices_t)value) && mAudioFlinger->btNrecIsOff();
Eric Laurent6639b552011-08-01 09:52:20 -07005267 setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId());
5268 setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId());
5269 }
Eric Laurent464d5b32011-06-17 21:29:58 -07005270 }
5271 mDevice |= (uint32_t)value;
5272 }
Eric Laurenta553c252009-07-17 12:17:14 -07005273 if (status == NO_ERROR) {
Dima Zavin31f188892011-04-18 16:57:27 -07005274 status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string());
Eric Laurenta553c252009-07-17 12:17:14 -07005275 if (status == INVALID_OPERATION) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07005276 mInput->stream->common.standby(&mInput->stream->common);
5277 status = mInput->stream->common.set_parameters(&mInput->stream->common,
5278 keyValuePair.string());
Eric Laurenta553c252009-07-17 12:17:14 -07005279 }
5280 if (reconfig) {
5281 if (status == BAD_VALUE &&
Dima Zavin31f188892011-04-18 16:57:27 -07005282 reqFormat == mInput->stream->common.get_format(&mInput->stream->common) &&
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005283 reqFormat == AUDIO_FORMAT_PCM_16_BIT &&
Dima Zavin31f188892011-04-18 16:57:27 -07005284 ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) &&
Glenn Kastend8f32c22012-03-08 12:32:47 -08005285 popcount(mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_2 &&
5286 (reqChannelCount <= FCC_2)) {
Eric Laurenta553c252009-07-17 12:17:14 -07005287 status = NO_ERROR;
5288 }
5289 if (status == NO_ERROR) {
5290 readInputParameters();
Eric Laurent8fce46a2009-08-04 09:45:33 -07005291 sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED);
Eric Laurenta553c252009-07-17 12:17:14 -07005292 }
5293 }
5294 }
Eric Laurent3fdb1262009-11-07 00:01:32 -08005295
5296 mNewParameters.removeAt(0);
5297
Eric Laurenta553c252009-07-17 12:17:14 -07005298 mParamStatus = status;
5299 mParamCond.signal();
Eric Laurent5f37be32011-09-13 11:40:21 -07005300 // wait for condition with time out in case the thread calling ThreadBase::setParameters()
5301 // already timed out waiting for the status and will never signal the condition.
Glenn Kastencbfe2e12011-12-13 11:04:14 -08005302 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
Eric Laurenta553c252009-07-17 12:17:14 -07005303 }
5304 return reconfig;
5305}
5306
5307String8 AudioFlinger::RecordThread::getParameters(const String8& keys)
5308{
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005309 char *s;
Eric Laurent828b9772011-08-07 16:32:26 -07005310 String8 out_s8 = String8();
5311
5312 Mutex::Autolock _l(mLock);
5313 if (initCheck() != NO_ERROR) {
5314 return out_s8;
5315 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005316
Dima Zavin31f188892011-04-18 16:57:27 -07005317 s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string());
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005318 out_s8 = String8(s);
5319 free(s);
5320 return out_s8;
Eric Laurenta553c252009-07-17 12:17:14 -07005321}
5322
Eric Laurenteb8f850d2010-05-14 03:26:45 -07005323void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) {
Eric Laurenta553c252009-07-17 12:17:14 -07005324 AudioSystem::OutputDescriptor desc;
Glenn Kasten3694ec12012-01-27 16:47:15 -08005325 void *param2 = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07005326
5327 switch (event) {
5328 case AudioSystem::INPUT_OPENED:
5329 case AudioSystem::INPUT_CONFIG_CHANGED:
Jean-Michel Trivi54392232011-05-24 15:53:33 -07005330 desc.channels = mChannelMask;
Eric Laurenta553c252009-07-17 12:17:14 -07005331 desc.samplingRate = mSampleRate;
5332 desc.format = mFormat;
5333 desc.frameCount = mFrameCount;
5334 desc.latency = 0;
5335 param2 = &desc;
5336 break;
5337
5338 case AudioSystem::INPUT_CLOSED:
5339 default:
5340 break;
5341 }
Eric Laurent49f02be2009-11-19 09:00:56 -08005342 mAudioFlinger->audioConfigChanged_l(event, mId, param2);
Eric Laurenta553c252009-07-17 12:17:14 -07005343}
5344
5345void AudioFlinger::RecordThread::readInputParameters()
5346{
Glenn Kasten2589cf92012-01-27 18:08:45 -08005347 delete mRsmpInBuffer;
5348 // mRsmpInBuffer is always assigned a new[] below
5349 delete mRsmpOutBuffer;
5350 mRsmpOutBuffer = NULL;
5351 delete mResampler;
Glenn Kastenc434c902011-12-13 11:53:26 -08005352 mResampler = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07005353
Dima Zavin31f188892011-04-18 16:57:27 -07005354 mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common);
Jean-Michel Trivi54392232011-05-24 15:53:33 -07005355 mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common);
5356 mChannelCount = (uint16_t)popcount(mChannelMask);
Dima Zavin31f188892011-04-18 16:57:27 -07005357 mFormat = mInput->stream->common.get_format(&mInput->stream->common);
Glenn Kastenfaf354d2012-01-11 09:48:27 -08005358 mFrameSize = audio_stream_frame_size(&mInput->stream->common);
Dima Zavin31f188892011-04-18 16:57:27 -07005359 mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common);
Eric Laurenta553c252009-07-17 12:17:14 -07005360 mFrameCount = mInputBytes / mFrameSize;
5361 mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount];
5362
Glenn Kastend8f32c22012-03-08 12:32:47 -08005363 if (mSampleRate != mReqSampleRate && mChannelCount <= FCC_2 && mReqChannelCount <= FCC_2)
Eric Laurenta553c252009-07-17 12:17:14 -07005364 {
5365 int channelCount;
Glenn Kasten18db49a2012-03-12 16:29:55 -07005366 // optimization: if mono to mono, use the resampler in stereo to stereo mode to avoid
5367 // stereo to mono post process as the resampler always outputs stereo.
Eric Laurenta553c252009-07-17 12:17:14 -07005368 if (mChannelCount == 1 && mReqChannelCount == 2) {
5369 channelCount = 1;
5370 } else {
5371 channelCount = 2;
5372 }
5373 mResampler = AudioResampler::create(16, channelCount, mReqSampleRate);
5374 mResampler->setSampleRate(mSampleRate);
5375 mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN);
5376 mRsmpOutBuffer = new int32_t[mFrameCount * 2];
5377
5378 // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples
5379 if (mChannelCount == 1 && mReqChannelCount == 1) {
5380 mFrameCount >>= 1;
5381 }
5382
5383 }
5384 mRsmpInIndex = mFrameCount;
5385}
5386
Eric Laurent47d0a922010-02-26 02:47:27 -08005387unsigned int AudioFlinger::RecordThread::getInputFramesLost()
5388{
Eric Laurent828b9772011-08-07 16:32:26 -07005389 Mutex::Autolock _l(mLock);
5390 if (initCheck() != NO_ERROR) {
5391 return 0;
5392 }
5393
Dima Zavin31f188892011-04-18 16:57:27 -07005394 return mInput->stream->get_input_frames_lost(mInput->stream);
Eric Laurent47d0a922010-02-26 02:47:27 -08005395}
5396
Eric Laurent464d5b32011-06-17 21:29:58 -07005397uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId)
5398{
5399 Mutex::Autolock _l(mLock);
5400 uint32_t result = 0;
5401 if (getEffectChain_l(sessionId) != 0) {
5402 result = EFFECT_SESSION;
5403 }
5404
5405 if (mTrack != NULL && sessionId == mTrack->sessionId()) {
5406 result |= TRACK_SESSION;
5407 }
5408
5409 return result;
5410}
5411
Eric Laurent6639b552011-08-01 09:52:20 -07005412AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track()
5413{
5414 Mutex::Autolock _l(mLock);
5415 return mTrack;
5416}
5417
Glenn Kasten5b0135e2012-01-26 09:46:34 -08005418AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() const
Eric Laurent828b9772011-08-07 16:32:26 -07005419{
5420 Mutex::Autolock _l(mLock);
5421 return mInput;
5422}
5423
5424AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput()
5425{
5426 Mutex::Autolock _l(mLock);
5427 AudioStreamIn *input = mInput;
5428 mInput = NULL;
5429 return input;
5430}
5431
5432// this method must always be called either with ThreadBase mLock held or inside the thread loop
5433audio_stream_t* AudioFlinger::RecordThread::stream()
5434{
5435 if (mInput == NULL) {
5436 return NULL;
5437 }
5438 return &mInput->stream->common;
5439}
5440
5441
Eric Laurenta553c252009-07-17 12:17:14 -07005442// ----------------------------------------------------------------------------
5443
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005444audio_io_handle_t AudioFlinger::openOutput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -07005445 uint32_t *pSamplingRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005446 audio_format_t *pFormat,
Eric Laurenta553c252009-07-17 12:17:14 -07005447 uint32_t *pChannels,
5448 uint32_t *pLatencyMs,
Glenn Kasten28b269f2012-03-07 09:15:37 -08005449 audio_policy_output_flags_t flags)
Eric Laurenta553c252009-07-17 12:17:14 -07005450{
5451 status_t status;
5452 PlaybackThread *thread = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07005453 uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005454 audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT;
Eric Laurenta553c252009-07-17 12:17:14 -07005455 uint32_t channels = pChannels ? *pChannels : 0;
5456 uint32_t latency = pLatencyMs ? *pLatencyMs : 0;
Dima Zavin31f188892011-04-18 16:57:27 -07005457 audio_stream_out_t *outStream;
5458 audio_hw_device_t *outHwDev;
Eric Laurenta553c252009-07-17 12:17:14 -07005459
Steve Block71f2cf12011-10-20 11:56:00 +01005460 ALOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
Eric Laurenta553c252009-07-17 12:17:14 -07005461 pDevices ? *pDevices : 0,
5462 samplingRate,
5463 format,
5464 channels,
5465 flags);
5466
5467 if (pDevices == NULL || *pDevices == 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -07005468 return 0;
Eric Laurenta553c252009-07-17 12:17:14 -07005469 }
Dima Zavin31f188892011-04-18 16:57:27 -07005470
Eric Laurenta553c252009-07-17 12:17:14 -07005471 Mutex::Autolock _l(mLock);
5472
Dima Zavin31f188892011-04-18 16:57:27 -07005473 outHwDev = findSuitableHwDev_l(*pDevices);
5474 if (outHwDev == NULL)
5475 return 0;
5476
Glenn Kasten23c9c742012-02-02 14:16:03 -08005477 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005478 status = outHwDev->open_output_stream(outHwDev, *pDevices, &format,
Dima Zavin31f188892011-04-18 16:57:27 -07005479 &channels, &samplingRate, &outStream);
Glenn Kasten23c9c742012-02-02 14:16:03 -08005480 mHardwareStatus = AUDIO_HW_IDLE;
Steve Block71f2cf12011-10-20 11:56:00 +01005481 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d",
Dima Zavin31f188892011-04-18 16:57:27 -07005482 outStream,
Eric Laurenta553c252009-07-17 12:17:14 -07005483 samplingRate,
5484 format,
5485 channels,
5486 status);
5487
Dima Zavin31f188892011-04-18 16:57:27 -07005488 if (outStream != NULL) {
5489 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005490 audio_io_handle_t id = nextUniqueId();
Dima Zavin31f188892011-04-18 16:57:27 -07005491
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005492 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) ||
5493 (format != AUDIO_FORMAT_PCM_16_BIT) ||
5494 (channels != AUDIO_CHANNEL_OUT_STEREO)) {
Eric Laurent65b65452010-06-01 23:49:17 -07005495 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block71f2cf12011-10-20 11:56:00 +01005496 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Eric Laurenta553c252009-07-17 12:17:14 -07005497 } else {
Eric Laurent65b65452010-06-01 23:49:17 -07005498 thread = new MixerThread(this, output, id, *pDevices);
Steve Block71f2cf12011-10-20 11:56:00 +01005499 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Eric Laurenta553c252009-07-17 12:17:14 -07005500 }
Eric Laurent65b65452010-06-01 23:49:17 -07005501 mPlaybackThreads.add(id, thread);
Eric Laurenta553c252009-07-17 12:17:14 -07005502
Glenn Kasten3694ec12012-01-27 16:47:15 -08005503 if (pSamplingRate != NULL) *pSamplingRate = samplingRate;
5504 if (pFormat != NULL) *pFormat = format;
5505 if (pChannels != NULL) *pChannels = channels;
5506 if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
Eric Laurent9cc489a22009-12-05 05:20:01 -08005507
Eric Laurenteb8f850d2010-05-14 03:26:45 -07005508 // notify client processes of the new output creation
5509 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurent65b65452010-06-01 23:49:17 -07005510 return id;
Eric Laurenta553c252009-07-17 12:17:14 -07005511 }
5512
Eric Laurent9cc489a22009-12-05 05:20:01 -08005513 return 0;
Eric Laurenta553c252009-07-17 12:17:14 -07005514}
5515
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005516audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
5517 audio_io_handle_t output2)
Eric Laurenta553c252009-07-17 12:17:14 -07005518{
5519 Mutex::Autolock _l(mLock);
Eric Laurentddb78e72009-07-28 08:44:33 -07005520 MixerThread *thread1 = checkMixerThread_l(output1);
5521 MixerThread *thread2 = checkMixerThread_l(output2);
Eric Laurenta553c252009-07-17 12:17:14 -07005522
Eric Laurentddb78e72009-07-28 08:44:33 -07005523 if (thread1 == NULL || thread2 == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00005524 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2);
Eric Laurentddb78e72009-07-28 08:44:33 -07005525 return 0;
Eric Laurenta553c252009-07-17 12:17:14 -07005526 }
5527
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005528 audio_io_handle_t id = nextUniqueId();
Eric Laurent65b65452010-06-01 23:49:17 -07005529 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
Eric Laurentddb78e72009-07-28 08:44:33 -07005530 thread->addOutputTrack(thread2);
Eric Laurent65b65452010-06-01 23:49:17 -07005531 mPlaybackThreads.add(id, thread);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07005532 // notify client processes of the new output creation
5533 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurent65b65452010-06-01 23:49:17 -07005534 return id;
Eric Laurenta553c252009-07-17 12:17:14 -07005535}
5536
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005537status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Eric Laurenta553c252009-07-17 12:17:14 -07005538{
Eric Laurent49018a52009-08-04 08:37:05 -07005539 // keep strong reference on the playback thread so that
5540 // it is not destroyed while exit() is executed
Glenn Kasten18db49a2012-03-12 16:29:55 -07005541 sp<PlaybackThread> thread;
Eric Laurenta553c252009-07-17 12:17:14 -07005542 {
5543 Mutex::Autolock _l(mLock);
5544 thread = checkPlaybackThread_l(output);
5545 if (thread == NULL) {
5546 return BAD_VALUE;
5547 }
5548
Steve Block71f2cf12011-10-20 11:56:00 +01005549 ALOGV("closeOutput() %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -07005550
Eric Laurent464d5b32011-06-17 21:29:58 -07005551 if (thread->type() == ThreadBase::MIXER) {
Eric Laurenta553c252009-07-17 12:17:14 -07005552 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent464d5b32011-06-17 21:29:58 -07005553 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Eric Laurentddb78e72009-07-28 08:44:33 -07005554 DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Eric Laurent49018a52009-08-04 08:37:05 -07005555 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurenta553c252009-07-17 12:17:14 -07005556 }
5557 }
5558 }
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08005559 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Eric Laurentddb78e72009-07-28 08:44:33 -07005560 mPlaybackThreads.removeItem(output);
Eric Laurenta553c252009-07-17 12:17:14 -07005561 }
5562 thread->exit();
Glenn Kasten761286f2012-01-06 08:39:38 -08005563 // The thread entity (active unit of execution) is no longer running here,
5564 // but the ThreadBase container still exists.
Eric Laurenta553c252009-07-17 12:17:14 -07005565
Eric Laurent464d5b32011-06-17 21:29:58 -07005566 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurent828b9772011-08-07 16:32:26 -07005567 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten81916df2012-03-08 12:18:35 -08005568 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurent828b9772011-08-07 16:32:26 -07005569 // from now on thread->mOutput is NULL
Dima Zavin31f188892011-04-18 16:57:27 -07005570 out->hwDev->close_output_stream(out->hwDev, out->stream);
5571 delete out;
Eric Laurent49018a52009-08-04 08:37:05 -07005572 }
Eric Laurenta553c252009-07-17 12:17:14 -07005573 return NO_ERROR;
5574}
5575
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005576status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Eric Laurenta553c252009-07-17 12:17:14 -07005577{
5578 Mutex::Autolock _l(mLock);
5579 PlaybackThread *thread = checkPlaybackThread_l(output);
5580
5581 if (thread == NULL) {
5582 return BAD_VALUE;
5583 }
5584
Steve Block71f2cf12011-10-20 11:56:00 +01005585 ALOGV("suspendOutput() %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -07005586 thread->suspend();
5587
5588 return NO_ERROR;
5589}
5590
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005591status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Eric Laurenta553c252009-07-17 12:17:14 -07005592{
5593 Mutex::Autolock _l(mLock);
5594 PlaybackThread *thread = checkPlaybackThread_l(output);
5595
5596 if (thread == NULL) {
5597 return BAD_VALUE;
5598 }
5599
Steve Block71f2cf12011-10-20 11:56:00 +01005600 ALOGV("restoreOutput() %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -07005601
5602 thread->restore();
5603
5604 return NO_ERROR;
5605}
5606
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005607audio_io_handle_t AudioFlinger::openInput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -07005608 uint32_t *pSamplingRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005609 audio_format_t *pFormat,
Eric Laurenta553c252009-07-17 12:17:14 -07005610 uint32_t *pChannels,
Glenn Kasten882c0a22012-01-27 12:32:34 -08005611 audio_in_acoustics_t acoustics)
Eric Laurenta553c252009-07-17 12:17:14 -07005612{
5613 status_t status;
5614 RecordThread *thread = NULL;
5615 uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005616 audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT;
Eric Laurenta553c252009-07-17 12:17:14 -07005617 uint32_t channels = pChannels ? *pChannels : 0;
5618 uint32_t reqSamplingRate = samplingRate;
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005619 audio_format_t reqFormat = format;
Eric Laurenta553c252009-07-17 12:17:14 -07005620 uint32_t reqChannels = channels;
Dima Zavin31f188892011-04-18 16:57:27 -07005621 audio_stream_in_t *inStream;
5622 audio_hw_device_t *inHwDev;
Eric Laurenta553c252009-07-17 12:17:14 -07005623
5624 if (pDevices == NULL || *pDevices == 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -07005625 return 0;
Eric Laurenta553c252009-07-17 12:17:14 -07005626 }
Dima Zavin31f188892011-04-18 16:57:27 -07005627
Eric Laurenta553c252009-07-17 12:17:14 -07005628 Mutex::Autolock _l(mLock);
5629
Dima Zavin31f188892011-04-18 16:57:27 -07005630 inHwDev = findSuitableHwDev_l(*pDevices);
5631 if (inHwDev == NULL)
5632 return 0;
5633
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005634 status = inHwDev->open_input_stream(inHwDev, *pDevices, &format,
Dima Zavin31f188892011-04-18 16:57:27 -07005635 &channels, &samplingRate,
Glenn Kasten882c0a22012-01-27 12:32:34 -08005636 acoustics,
Dima Zavin31f188892011-04-18 16:57:27 -07005637 &inStream);
Steve Block71f2cf12011-10-20 11:56:00 +01005638 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, acoustics %x, status %d",
Dima Zavin31f188892011-04-18 16:57:27 -07005639 inStream,
Eric Laurenta553c252009-07-17 12:17:14 -07005640 samplingRate,
5641 format,
5642 channels,
5643 acoustics,
5644 status);
5645
5646 // If the input could not be opened with the requested parameters and we can handle the conversion internally,
5647 // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo
5648 // or stereo to mono conversions on 16 bit PCM inputs.
Dima Zavin31f188892011-04-18 16:57:27 -07005649 if (inStream == NULL && status == BAD_VALUE &&
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005650 reqFormat == format && format == AUDIO_FORMAT_PCM_16_BIT &&
Eric Laurenta553c252009-07-17 12:17:14 -07005651 (samplingRate <= 2 * reqSamplingRate) &&
Glenn Kastend8f32c22012-03-08 12:32:47 -08005652 (popcount(channels) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Steve Block71f2cf12011-10-20 11:56:00 +01005653 ALOGV("openInput() reopening with proposed sampling rate and channels");
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005654 status = inHwDev->open_input_stream(inHwDev, *pDevices, &format,
Dima Zavin31f188892011-04-18 16:57:27 -07005655 &channels, &samplingRate,
Glenn Kasten882c0a22012-01-27 12:32:34 -08005656 acoustics,
Dima Zavin31f188892011-04-18 16:57:27 -07005657 &inStream);
Eric Laurenta553c252009-07-17 12:17:14 -07005658 }
5659
Dima Zavin31f188892011-04-18 16:57:27 -07005660 if (inStream != NULL) {
5661 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
5662
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005663 audio_io_handle_t id = nextUniqueId();
Eric Laurent464d5b32011-06-17 21:29:58 -07005664 // Start record thread
5665 // RecorThread require both input and output device indication to forward to audio
5666 // pre processing modules
5667 uint32_t device = (*pDevices) | primaryOutputDevice_l();
5668 thread = new RecordThread(this,
5669 input,
5670 reqSamplingRate,
5671 reqChannels,
5672 id,
5673 device);
Eric Laurent65b65452010-06-01 23:49:17 -07005674 mRecordThreads.add(id, thread);
Steve Block71f2cf12011-10-20 11:56:00 +01005675 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten3694ec12012-01-27 16:47:15 -08005676 if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate;
5677 if (pFormat != NULL) *pFormat = format;
5678 if (pChannels != NULL) *pChannels = reqChannels;
Eric Laurenta553c252009-07-17 12:17:14 -07005679
Dima Zavin31f188892011-04-18 16:57:27 -07005680 input->stream->common.standby(&input->stream->common);
Eric Laurent9cc489a22009-12-05 05:20:01 -08005681
Eric Laurenteb8f850d2010-05-14 03:26:45 -07005682 // notify client processes of the new input creation
5683 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
Eric Laurent65b65452010-06-01 23:49:17 -07005684 return id;
Eric Laurenta553c252009-07-17 12:17:14 -07005685 }
5686
Eric Laurent9cc489a22009-12-05 05:20:01 -08005687 return 0;
Eric Laurenta553c252009-07-17 12:17:14 -07005688}
5689
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005690status_t AudioFlinger::closeInput(audio_io_handle_t input)
Eric Laurenta553c252009-07-17 12:17:14 -07005691{
Eric Laurent49018a52009-08-04 08:37:05 -07005692 // keep strong reference on the record thread so that
5693 // it is not destroyed while exit() is executed
Glenn Kasten18db49a2012-03-12 16:29:55 -07005694 sp<RecordThread> thread;
Eric Laurenta553c252009-07-17 12:17:14 -07005695 {
5696 Mutex::Autolock _l(mLock);
5697 thread = checkRecordThread_l(input);
5698 if (thread == NULL) {
5699 return BAD_VALUE;
5700 }
5701
Steve Block71f2cf12011-10-20 11:56:00 +01005702 ALOGV("closeInput() %d", input);
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08005703 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Eric Laurentddb78e72009-07-28 08:44:33 -07005704 mRecordThreads.removeItem(input);
Eric Laurenta553c252009-07-17 12:17:14 -07005705 }
5706 thread->exit();
Glenn Kasten761286f2012-01-06 08:39:38 -08005707 // The thread entity (active unit of execution) is no longer running here,
5708 // but the ThreadBase container still exists.
Eric Laurenta553c252009-07-17 12:17:14 -07005709
Eric Laurent828b9772011-08-07 16:32:26 -07005710 AudioStreamIn *in = thread->clearInput();
Glenn Kasten81916df2012-03-08 12:18:35 -08005711 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurent828b9772011-08-07 16:32:26 -07005712 // from now on thread->mInput is NULL
Dima Zavin31f188892011-04-18 16:57:27 -07005713 in->hwDev->close_input_stream(in->hwDev, in->stream);
5714 delete in;
Eric Laurent49018a52009-08-04 08:37:05 -07005715
Eric Laurenta553c252009-07-17 12:17:14 -07005716 return NO_ERROR;
5717}
5718
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005719status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Eric Laurenta553c252009-07-17 12:17:14 -07005720{
5721 Mutex::Autolock _l(mLock);
5722 MixerThread *dstThread = checkMixerThread_l(output);
5723 if (dstThread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00005724 ALOGW("setStreamOutput() bad output id %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -07005725 return BAD_VALUE;
5726 }
5727
Steve Block71f2cf12011-10-20 11:56:00 +01005728 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07005729 audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream);
Eric Laurenta553c252009-07-17 12:17:14 -07005730
Eric Laurent05ce0942011-08-30 10:18:54 -07005731 dstThread->setStreamValid(stream, true);
5732
Eric Laurenta553c252009-07-17 12:17:14 -07005733 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentddb78e72009-07-28 08:44:33 -07005734 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08005735 if (thread != dstThread && thread->type() != ThreadBase::DIRECT) {
Eric Laurenta553c252009-07-17 12:17:14 -07005736 MixerThread *srcThread = (MixerThread *)thread;
Eric Laurent05ce0942011-08-30 10:18:54 -07005737 srcThread->setStreamValid(stream, false);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07005738 srcThread->invalidateTracks(stream);
Eric Laurenta553c252009-07-17 12:17:14 -07005739 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07005740 }
Eric Laurentd069f322009-09-01 05:56:26 -07005741
Eric Laurenta553c252009-07-17 12:17:14 -07005742 return NO_ERROR;
5743}
5744
Eric Laurent65b65452010-06-01 23:49:17 -07005745
5746int AudioFlinger::newAudioSessionId()
5747{
Eric Laurent464d5b32011-06-17 21:29:58 -07005748 return nextUniqueId();
Eric Laurent65b65452010-06-01 23:49:17 -07005749}
5750
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005751void AudioFlinger::acquireAudioSessionId(int audioSession)
5752{
5753 Mutex::Autolock _l(mLock);
Glenn Kastend153b1f2012-02-03 11:10:26 -08005754 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block71f2cf12011-10-20 11:56:00 +01005755 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten150d2382012-02-08 14:04:28 -08005756 size_t num = mAudioSessionRefs.size();
5757 for (size_t i = 0; i< num; i++) {
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005758 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten9a42ac92012-03-06 11:22:01 -08005759 if (ref->mSessionid == audioSession && ref->mPid == caller) {
5760 ref->mCnt++;
5761 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005762 return;
5763 }
5764 }
Glenn Kastendc3ac852012-01-25 15:28:08 -08005765 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
5766 ALOGV(" added new entry for %d", audioSession);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005767}
5768
5769void AudioFlinger::releaseAudioSessionId(int audioSession)
5770{
5771 Mutex::Autolock _l(mLock);
Glenn Kastend153b1f2012-02-03 11:10:26 -08005772 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block71f2cf12011-10-20 11:56:00 +01005773 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten150d2382012-02-08 14:04:28 -08005774 size_t num = mAudioSessionRefs.size();
5775 for (size_t i = 0; i< num; i++) {
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005776 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten9a42ac92012-03-06 11:22:01 -08005777 if (ref->mSessionid == audioSession && ref->mPid == caller) {
5778 ref->mCnt--;
5779 ALOGV(" decremented refcount to %d", ref->mCnt);
5780 if (ref->mCnt == 0) {
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005781 mAudioSessionRefs.removeAt(i);
5782 delete ref;
5783 purgeStaleEffects_l();
5784 }
5785 return;
5786 }
5787 }
Steve Block8564c8d2012-01-05 23:22:43 +00005788 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005789}
5790
5791void AudioFlinger::purgeStaleEffects_l() {
5792
Steve Block71f2cf12011-10-20 11:56:00 +01005793 ALOGV("purging stale effects");
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005794
5795 Vector< sp<EffectChain> > chains;
5796
5797 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
5798 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
5799 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
5800 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen2255a1e2011-08-12 14:14:39 -07005801 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
5802 chains.push(ec);
5803 }
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005804 }
5805 }
5806 for (size_t i = 0; i < mRecordThreads.size(); i++) {
5807 sp<RecordThread> t = mRecordThreads.valueAt(i);
5808 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
5809 sp<EffectChain> ec = t->mEffectChains[j];
5810 chains.push(ec);
5811 }
5812 }
5813
5814 for (size_t i = 0; i < chains.size(); i++) {
5815 sp<EffectChain> ec = chains[i];
5816 int sessionid = ec->sessionId();
5817 sp<ThreadBase> t = ec->mThread.promote();
5818 if (t == 0) {
5819 continue;
5820 }
5821 size_t numsessionrefs = mAudioSessionRefs.size();
5822 bool found = false;
5823 for (size_t k = 0; k < numsessionrefs; k++) {
5824 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten9a42ac92012-03-06 11:22:01 -08005825 if (ref->mSessionid == sessionid) {
Steve Block71f2cf12011-10-20 11:56:00 +01005826 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kasten18db49a2012-03-12 16:29:55 -07005827 sessionid, ref->mPid, ref->mCnt);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005828 found = true;
5829 break;
5830 }
5831 }
5832 if (!found) {
5833 // remove all effects from the chain
5834 while (ec->mEffects.size()) {
5835 sp<EffectModule> effect = ec->mEffects[0];
5836 effect->unPin();
5837 Mutex::Autolock _l (t->mLock);
5838 t->removeEffect_l(effect);
5839 for (size_t j = 0; j < effect->mHandles.size(); j++) {
5840 sp<EffectHandle> handle = effect->mHandles[j].promote();
5841 if (handle != 0) {
5842 handle->mEffect.clear();
Eric Laurent7fa1cee2011-10-19 11:44:54 -07005843 if (handle->mHasControl && handle->mEnabled) {
5844 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
5845 }
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005846 }
5847 }
5848 AudioSystem::unregisterEffect(effect->id());
5849 }
5850 }
5851 }
5852 return;
5853}
5854
Eric Laurenta553c252009-07-17 12:17:14 -07005855// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005856AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Eric Laurenta553c252009-07-17 12:17:14 -07005857{
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08005858 return mPlaybackThreads.valueFor(output).get();
Eric Laurenta553c252009-07-17 12:17:14 -07005859}
5860
5861// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005862AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Eric Laurenta553c252009-07-17 12:17:14 -07005863{
5864 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08005865 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07005866}
5867
5868// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005869AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Eric Laurenta553c252009-07-17 12:17:14 -07005870{
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08005871 return mRecordThreads.valueFor(input).get();
Eric Laurenta553c252009-07-17 12:17:14 -07005872}
5873
Eric Laurent464d5b32011-06-17 21:29:58 -07005874uint32_t AudioFlinger::nextUniqueId()
Eric Laurent65b65452010-06-01 23:49:17 -07005875{
Eric Laurent464d5b32011-06-17 21:29:58 -07005876 return android_atomic_inc(&mNextUniqueId);
Eric Laurent65b65452010-06-01 23:49:17 -07005877}
5878
Glenn Kasten0fccd352012-02-24 15:42:17 -08005879AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent464d5b32011-06-17 21:29:58 -07005880{
5881 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
5882 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent828b9772011-08-07 16:32:26 -07005883 AudioStreamOut *output = thread->getOutput();
5884 if (output != NULL && output->hwDev == mPrimaryHardwareDev) {
Eric Laurent464d5b32011-06-17 21:29:58 -07005885 return thread;
5886 }
5887 }
5888 return NULL;
5889}
5890
Glenn Kasten0fccd352012-02-24 15:42:17 -08005891uint32_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent464d5b32011-06-17 21:29:58 -07005892{
5893 PlaybackThread *thread = primaryPlaybackThread_l();
5894
5895 if (thread == NULL) {
5896 return 0;
5897 }
5898
5899 return thread->device();
5900}
5901
5902
Eric Laurent65b65452010-06-01 23:49:17 -07005903// ----------------------------------------------------------------------------
5904// Effect management
5905// ----------------------------------------------------------------------------
5906
5907
Glenn Kasten3f6d83a2012-01-26 16:25:10 -08005908status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Eric Laurent65b65452010-06-01 23:49:17 -07005909{
5910 Mutex::Autolock _l(mLock);
5911 return EffectQueryNumberEffects(numEffects);
5912}
5913
Glenn Kasten3f6d83a2012-01-26 16:25:10 -08005914status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Eric Laurent65b65452010-06-01 23:49:17 -07005915{
5916 Mutex::Autolock _l(mLock);
Eric Laurent53334cd2010-06-23 17:38:20 -07005917 return EffectQueryEffect(index, descriptor);
Eric Laurent65b65452010-06-01 23:49:17 -07005918}
5919
Glenn Kasten67313332012-01-30 07:40:52 -08005920status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kasten3f6d83a2012-01-26 16:25:10 -08005921 effect_descriptor_t *descriptor) const
Eric Laurent65b65452010-06-01 23:49:17 -07005922{
5923 Mutex::Autolock _l(mLock);
5924 return EffectGetDescriptor(pUuid, descriptor);
5925}
5926
Eric Laurentdf9b81c2010-07-02 08:12:41 -07005927
Eric Laurent65b65452010-06-01 23:49:17 -07005928sp<IEffect> AudioFlinger::createEffect(pid_t pid,
5929 effect_descriptor_t *pDesc,
5930 const sp<IEffectClient>& effectClient,
5931 int32_t priority,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005932 audio_io_handle_t io,
Eric Laurent65b65452010-06-01 23:49:17 -07005933 int sessionId,
5934 status_t *status,
5935 int *id,
5936 int *enabled)
5937{
5938 status_t lStatus = NO_ERROR;
5939 sp<EffectHandle> handle;
Eric Laurent65b65452010-06-01 23:49:17 -07005940 effect_descriptor_t desc;
Eric Laurent65b65452010-06-01 23:49:17 -07005941
Glenn Kasten803a86a2012-01-25 14:28:29 -08005942 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent464d5b32011-06-17 21:29:58 -07005943 pid, effectClient.get(), priority, sessionId, io);
Eric Laurent65b65452010-06-01 23:49:17 -07005944
5945 if (pDesc == NULL) {
5946 lStatus = BAD_VALUE;
5947 goto Exit;
5948 }
5949
Eric Laurent98c92592010-09-23 16:10:16 -07005950 // check audio settings permission for global effects
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005951 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent98c92592010-09-23 16:10:16 -07005952 lStatus = PERMISSION_DENIED;
5953 goto Exit;
5954 }
5955
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005956 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent98c92592010-09-23 16:10:16 -07005957 // that can only be created by audio policy manager (running in same process)
Glenn Kasten81211142012-02-05 18:09:08 -08005958 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent98c92592010-09-23 16:10:16 -07005959 lStatus = PERMISSION_DENIED;
5960 goto Exit;
5961 }
5962
Eric Laurent464d5b32011-06-17 21:29:58 -07005963 if (io == 0) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005964 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent98c92592010-09-23 16:10:16 -07005965 // output must be specified by AudioPolicyManager when using session
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005966 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent98c92592010-09-23 16:10:16 -07005967 lStatus = BAD_VALUE;
5968 goto Exit;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005969 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent98c92592010-09-23 16:10:16 -07005970 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent464d5b32011-06-17 21:29:58 -07005971 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent98c92592010-09-23 16:10:16 -07005972 // and we will exit safely
Eric Laurent464d5b32011-06-17 21:29:58 -07005973 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent98c92592010-09-23 16:10:16 -07005974 }
5975 }
5976
Eric Laurent65b65452010-06-01 23:49:17 -07005977 {
5978 Mutex::Autolock _l(mLock);
5979
Eric Laurentdf9b81c2010-07-02 08:12:41 -07005980
Eric Laurent65b65452010-06-01 23:49:17 -07005981 if (!EffectIsNullUuid(&pDesc->uuid)) {
5982 // if uuid is specified, request effect descriptor
5983 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
5984 if (lStatus < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00005985 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07005986 goto Exit;
5987 }
5988 } else {
5989 // if uuid is not specified, look for an available implementation
5990 // of the required type in effect factory
5991 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block8564c8d2012-01-05 23:22:43 +00005992 ALOGW("createEffect() no effect type");
Eric Laurent65b65452010-06-01 23:49:17 -07005993 lStatus = BAD_VALUE;
5994 goto Exit;
5995 }
5996 uint32_t numEffects = 0;
5997 effect_descriptor_t d;
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005998 d.flags = 0; // prevent compiler warning
Eric Laurent65b65452010-06-01 23:49:17 -07005999 bool found = false;
6000
6001 lStatus = EffectQueryNumberEffects(&numEffects);
6002 if (lStatus < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006003 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006004 goto Exit;
6005 }
Eric Laurent53334cd2010-06-23 17:38:20 -07006006 for (uint32_t i = 0; i < numEffects; i++) {
6007 lStatus = EffectQueryEffect(i, &desc);
Eric Laurent65b65452010-06-01 23:49:17 -07006008 if (lStatus < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006009 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006010 continue;
6011 }
6012 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
6013 // If matching type found save effect descriptor. If the session is
6014 // 0 and the effect is not auxiliary, continue enumeration in case
6015 // an auxiliary version of this effect type is available
6016 found = true;
6017 memcpy(&d, &desc, sizeof(effect_descriptor_t));
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006018 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Eric Laurent65b65452010-06-01 23:49:17 -07006019 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6020 break;
6021 }
6022 }
6023 }
6024 if (!found) {
6025 lStatus = BAD_VALUE;
Steve Block8564c8d2012-01-05 23:22:43 +00006026 ALOGW("createEffect() effect not found");
Eric Laurent65b65452010-06-01 23:49:17 -07006027 goto Exit;
6028 }
6029 // For same effect type, chose auxiliary version over insert version if
6030 // connect to output mix (Compliance to OpenSL ES)
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006031 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Eric Laurent65b65452010-06-01 23:49:17 -07006032 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
6033 memcpy(&desc, &d, sizeof(effect_descriptor_t));
6034 }
6035 }
6036
6037 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006038 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Eric Laurent65b65452010-06-01 23:49:17 -07006039 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6040 lStatus = INVALID_OPERATION;
6041 goto Exit;
6042 }
6043
Eric Laurentf82fccd2011-07-27 19:49:51 -07006044 // check recording permission for visualizer
6045 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
6046 !recordingAllowed()) {
6047 lStatus = PERMISSION_DENIED;
6048 goto Exit;
6049 }
6050
Eric Laurent65b65452010-06-01 23:49:17 -07006051 // return effect descriptor
6052 memcpy(pDesc, &desc, sizeof(effect_descriptor_t));
6053
6054 // If output is not specified try to find a matching audio session ID in one of the
6055 // output threads.
Eric Laurent98c92592010-09-23 16:10:16 -07006056 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
6057 // because of code checking output when entering the function.
Eric Laurent464d5b32011-06-17 21:29:58 -07006058 // Note: io is never 0 when creating an effect on an input
6059 if (io == 0) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07006060 // look for the thread where the specified audio session is present
Eric Laurent98c92592010-09-23 16:10:16 -07006061 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
6062 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent464d5b32011-06-17 21:29:58 -07006063 io = mPlaybackThreads.keyAt(i);
Eric Laurent98c92592010-09-23 16:10:16 -07006064 break;
Eric Laurent493941b2010-07-28 01:32:47 -07006065 }
Eric Laurent65b65452010-06-01 23:49:17 -07006066 }
Eric Laurent464d5b32011-06-17 21:29:58 -07006067 if (io == 0) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07006068 for (size_t i = 0; i < mRecordThreads.size(); i++) {
6069 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
6070 io = mRecordThreads.keyAt(i);
6071 break;
6072 }
6073 }
Eric Laurent464d5b32011-06-17 21:29:58 -07006074 }
Eric Laurent98c92592010-09-23 16:10:16 -07006075 // If no output thread contains the requested session ID, default to
6076 // first output. The effect chain will be moved to the correct output
6077 // thread when a track with the same session ID is created
Eric Laurent464d5b32011-06-17 21:29:58 -07006078 if (io == 0 && mPlaybackThreads.size()) {
6079 io = mPlaybackThreads.keyAt(0);
6080 }
Steve Block71f2cf12011-10-20 11:56:00 +01006081 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent464d5b32011-06-17 21:29:58 -07006082 }
6083 ThreadBase *thread = checkRecordThread_l(io);
6084 if (thread == NULL) {
6085 thread = checkPlaybackThread_l(io);
6086 if (thread == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00006087 ALOGE("createEffect() unknown output thread");
Eric Laurent464d5b32011-06-17 21:29:58 -07006088 lStatus = BAD_VALUE;
6089 goto Exit;
Eric Laurent98c92592010-09-23 16:10:16 -07006090 }
Eric Laurent65b65452010-06-01 23:49:17 -07006091 }
Eric Laurent98c92592010-09-23 16:10:16 -07006092
Glenn Kasten803a86a2012-01-25 14:28:29 -08006093 sp<Client> client = registerPid_l(pid);
Eric Laurent65b65452010-06-01 23:49:17 -07006094
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006095 // create effect on selected output thread
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006096 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
6097 &desc, enabled, &lStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006098 if (handle != 0 && id != NULL) {
6099 *id = handle->id();
6100 }
6101 }
6102
6103Exit:
Glenn Kasten18db49a2012-03-12 16:29:55 -07006104 if (status != NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07006105 *status = lStatus;
6106 }
6107 return handle;
6108}
6109
Glenn Kasten39d00cb2012-01-17 11:09:42 -08006110status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
6111 audio_io_handle_t dstOutput)
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006112{
Steve Block71f2cf12011-10-20 11:56:00 +01006113 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurentf82fccd2011-07-27 19:49:51 -07006114 sessionId, srcOutput, dstOutput);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006115 Mutex::Autolock _l(mLock);
6116 if (srcOutput == dstOutput) {
Steve Block8564c8d2012-01-05 23:22:43 +00006117 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006118 return NO_ERROR;
Eric Laurent53334cd2010-06-23 17:38:20 -07006119 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006120 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
6121 if (srcThread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00006122 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006123 return BAD_VALUE;
Eric Laurent53334cd2010-06-23 17:38:20 -07006124 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006125 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
6126 if (dstThread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00006127 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006128 return BAD_VALUE;
6129 }
6130
6131 Mutex::Autolock _dl(dstThread->mLock);
6132 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurentf82fccd2011-07-27 19:49:51 -07006133 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006134
Eric Laurent53334cd2010-06-23 17:38:20 -07006135 return NO_ERROR;
6136}
6137
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006138// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurentf82fccd2011-07-27 19:49:51 -07006139status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006140 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent493941b2010-07-28 01:32:47 -07006141 AudioFlinger::PlaybackThread *dstThread,
6142 bool reRegister)
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006143{
Steve Block71f2cf12011-10-20 11:56:00 +01006144 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurentf82fccd2011-07-27 19:49:51 -07006145 sessionId, srcThread, dstThread);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006146
Eric Laurentf82fccd2011-07-27 19:49:51 -07006147 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006148 if (chain == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006149 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurentf82fccd2011-07-27 19:49:51 -07006150 sessionId, srcThread);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006151 return INVALID_OPERATION;
6152 }
6153
Eric Laurent493941b2010-07-28 01:32:47 -07006154 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006155 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurent6fccbd02011-10-05 17:42:25 -07006156 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006157 // removed.
6158 srcThread->removeEffectChain_l(chain);
6159
6160 // transfer all effects one by one so that new effect chain is created on new thread with
6161 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten39d00cb2012-01-17 11:09:42 -08006162 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent493941b2010-07-28 01:32:47 -07006163 sp<EffectChain> dstChain;
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006164 uint32_t strategy = 0; // prevent compiler warning
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006165 sp<EffectModule> effect = chain->getEffectFromId_l(0);
6166 while (effect != 0) {
6167 srcThread->removeEffect_l(effect);
6168 dstThread->addEffect_l(effect);
Eric Laurent6fccbd02011-10-05 17:42:25 -07006169 // removeEffect_l() has stopped the effect if it was active so it must be restarted
6170 if (effect->state() == EffectModule::ACTIVE ||
6171 effect->state() == EffectModule::STOPPING) {
6172 effect->start();
6173 }
Eric Laurent493941b2010-07-28 01:32:47 -07006174 // if the move request is not received from audio policy manager, the effect must be
6175 // re-registered with the new strategy and output
6176 if (dstChain == 0) {
6177 dstChain = effect->chain().promote();
6178 if (dstChain == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006179 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent493941b2010-07-28 01:32:47 -07006180 srcThread->addEffect_l(effect);
6181 return NO_INIT;
6182 }
6183 strategy = dstChain->strategy();
6184 }
6185 if (reRegister) {
6186 AudioSystem::unregisterEffect(effect->id());
6187 AudioSystem::registerEffect(&effect->desc(),
6188 dstOutput,
6189 strategy,
Eric Laurentf82fccd2011-07-27 19:49:51 -07006190 sessionId,
Eric Laurent493941b2010-07-28 01:32:47 -07006191 effect->id());
6192 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006193 effect = chain->getEffectFromId_l(0);
6194 }
6195
6196 return NO_ERROR;
Eric Laurent53334cd2010-06-23 17:38:20 -07006197}
6198
Eric Laurent464d5b32011-06-17 21:29:58 -07006199
Eric Laurent65b65452010-06-01 23:49:17 -07006200// PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held
Eric Laurent464d5b32011-06-17 21:29:58 -07006201sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l(
Eric Laurent65b65452010-06-01 23:49:17 -07006202 const sp<AudioFlinger::Client>& client,
6203 const sp<IEffectClient>& effectClient,
6204 int32_t priority,
6205 int sessionId,
6206 effect_descriptor_t *desc,
6207 int *enabled,
6208 status_t *status
6209 )
6210{
6211 sp<EffectModule> effect;
6212 sp<EffectHandle> handle;
6213 status_t lStatus;
Eric Laurent65b65452010-06-01 23:49:17 -07006214 sp<EffectChain> chain;
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006215 bool chainCreated = false;
Eric Laurent65b65452010-06-01 23:49:17 -07006216 bool effectCreated = false;
Eric Laurent53334cd2010-06-23 17:38:20 -07006217 bool effectRegistered = false;
Eric Laurent65b65452010-06-01 23:49:17 -07006218
Eric Laurent464d5b32011-06-17 21:29:58 -07006219 lStatus = initCheck();
6220 if (lStatus != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +00006221 ALOGW("createEffect_l() Audio driver not initialized.");
Eric Laurent65b65452010-06-01 23:49:17 -07006222 goto Exit;
6223 }
6224
6225 // Do not allow effects with session ID 0 on direct output or duplicating threads
6226 // TODO: add rule for hw accelerated effects on direct outputs with non PCM format
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006227 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) {
Steve Block8564c8d2012-01-05 23:22:43 +00006228 ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d",
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006229 desc->name, sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -07006230 lStatus = BAD_VALUE;
6231 goto Exit;
6232 }
Eric Laurent464d5b32011-06-17 21:29:58 -07006233 // Only Pre processor effects are allowed on input threads and only on input threads
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08006234 if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) {
Steve Block8564c8d2012-01-05 23:22:43 +00006235 ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d",
Eric Laurent464d5b32011-06-17 21:29:58 -07006236 desc->name, desc->flags, mType);
6237 lStatus = BAD_VALUE;
6238 goto Exit;
6239 }
Eric Laurent65b65452010-06-01 23:49:17 -07006240
Steve Block71f2cf12011-10-20 11:56:00 +01006241 ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -07006242
6243 { // scope for mLock
6244 Mutex::Autolock _l(mLock);
6245
6246 // check for existing effect chain with the requested audio session
6247 chain = getEffectChain_l(sessionId);
6248 if (chain == 0) {
6249 // create a new chain for this session
Steve Block71f2cf12011-10-20 11:56:00 +01006250 ALOGV("createEffect_l() new effect chain for session %d", sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -07006251 chain = new EffectChain(this, sessionId);
6252 addEffectChain_l(chain);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006253 chain->setStrategy(getStrategyForSession_l(sessionId));
6254 chainCreated = true;
Eric Laurent65b65452010-06-01 23:49:17 -07006255 } else {
Eric Laurent76c40f72010-07-15 12:50:15 -07006256 effect = chain->getEffectFromDesc_l(desc);
Eric Laurent65b65452010-06-01 23:49:17 -07006257 }
6258
Glenn Kasten70ed6b72012-01-10 10:46:34 -08006259 ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get());
Eric Laurent65b65452010-06-01 23:49:17 -07006260
6261 if (effect == 0) {
Eric Laurent464d5b32011-06-17 21:29:58 -07006262 int id = mAudioFlinger->nextUniqueId();
Eric Laurent53334cd2010-06-23 17:38:20 -07006263 // Check CPU and memory usage
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006264 lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id);
Eric Laurent53334cd2010-06-23 17:38:20 -07006265 if (lStatus != NO_ERROR) {
6266 goto Exit;
6267 }
6268 effectRegistered = true;
Eric Laurent65b65452010-06-01 23:49:17 -07006269 // create a new effect module if none present in the chain
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006270 effect = new EffectModule(this, chain, desc, id, sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -07006271 lStatus = effect->status();
6272 if (lStatus != NO_ERROR) {
6273 goto Exit;
6274 }
Eric Laurent76c40f72010-07-15 12:50:15 -07006275 lStatus = chain->addEffect_l(effect);
Eric Laurent65b65452010-06-01 23:49:17 -07006276 if (lStatus != NO_ERROR) {
6277 goto Exit;
6278 }
Eric Laurent53334cd2010-06-23 17:38:20 -07006279 effectCreated = true;
Eric Laurent65b65452010-06-01 23:49:17 -07006280
6281 effect->setDevice(mDevice);
Eric Laurent53334cd2010-06-23 17:38:20 -07006282 effect->setMode(mAudioFlinger->getMode());
Eric Laurent65b65452010-06-01 23:49:17 -07006283 }
6284 // create effect handle and connect it to effect module
6285 handle = new EffectHandle(effect, client, effectClient, priority);
6286 lStatus = effect->addHandle(handle);
Glenn Kasten3694ec12012-01-27 16:47:15 -08006287 if (enabled != NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07006288 *enabled = (int)effect->isEnabled();
6289 }
6290 }
6291
6292Exit:
6293 if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006294 Mutex::Autolock _l(mLock);
Eric Laurent53334cd2010-06-23 17:38:20 -07006295 if (effectCreated) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006296 chain->removeEffect_l(effect);
Eric Laurent65b65452010-06-01 23:49:17 -07006297 }
Eric Laurent53334cd2010-06-23 17:38:20 -07006298 if (effectRegistered) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006299 AudioSystem::unregisterEffect(effect->id());
6300 }
6301 if (chainCreated) {
6302 removeEffectChain_l(chain);
Eric Laurent53334cd2010-06-23 17:38:20 -07006303 }
Eric Laurent65b65452010-06-01 23:49:17 -07006304 handle.clear();
6305 }
6306
Glenn Kasten18db49a2012-03-12 16:29:55 -07006307 if (status != NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07006308 *status = lStatus;
6309 }
6310 return handle;
6311}
6312
Eric Laurent464d5b32011-06-17 21:29:58 -07006313sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId)
6314{
Eric Laurent464d5b32011-06-17 21:29:58 -07006315 sp<EffectChain> chain = getEffectChain_l(sessionId);
Glenn Kastenc64a6f02012-01-30 13:00:02 -08006316 return chain != 0 ? chain->getEffectFromId_l(effectId) : 0;
Eric Laurent464d5b32011-06-17 21:29:58 -07006317}
6318
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006319// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and
6320// PlaybackThread::mLock held
Eric Laurent464d5b32011-06-17 21:29:58 -07006321status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect)
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006322{
6323 // check for existing effect chain with the requested audio session
6324 int sessionId = effect->sessionId();
6325 sp<EffectChain> chain = getEffectChain_l(sessionId);
6326 bool chainCreated = false;
6327
6328 if (chain == 0) {
6329 // create a new chain for this session
Steve Block71f2cf12011-10-20 11:56:00 +01006330 ALOGV("addEffect_l() new effect chain for session %d", sessionId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006331 chain = new EffectChain(this, sessionId);
6332 addEffectChain_l(chain);
6333 chain->setStrategy(getStrategyForSession_l(sessionId));
6334 chainCreated = true;
6335 }
Steve Block71f2cf12011-10-20 11:56:00 +01006336 ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get());
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006337
6338 if (chain->getEffectFromId_l(effect->id()) != 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006339 ALOGW("addEffect_l() %p effect %s already present in chain %p",
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006340 this, effect->desc().name, chain.get());
6341 return BAD_VALUE;
6342 }
6343
6344 status_t status = chain->addEffect_l(effect);
6345 if (status != NO_ERROR) {
6346 if (chainCreated) {
6347 removeEffectChain_l(chain);
6348 }
6349 return status;
6350 }
6351
6352 effect->setDevice(mDevice);
6353 effect->setMode(mAudioFlinger->getMode());
6354 return NO_ERROR;
6355}
6356
Eric Laurent464d5b32011-06-17 21:29:58 -07006357void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006358
Steve Block71f2cf12011-10-20 11:56:00 +01006359 ALOGV("removeEffect_l() %p effect %p", this, effect.get());
Eric Laurent53334cd2010-06-23 17:38:20 -07006360 effect_descriptor_t desc = effect->desc();
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006361 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6362 detachAuxEffect_l(effect->id());
6363 }
6364
6365 sp<EffectChain> chain = effect->chain().promote();
6366 if (chain != 0) {
6367 // remove effect chain if removing last effect
6368 if (chain->removeEffect_l(effect) == 0) {
6369 removeEffectChain_l(chain);
6370 }
6371 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00006372 ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get());
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006373 }
6374}
6375
Eric Laurent464d5b32011-06-17 21:29:58 -07006376void AudioFlinger::ThreadBase::lockEffectChains_l(
Glenn Kasten18db49a2012-03-12 16:29:55 -07006377 Vector< sp<AudioFlinger::EffectChain> >& effectChains)
Eric Laurent464d5b32011-06-17 21:29:58 -07006378{
6379 effectChains = mEffectChains;
6380 for (size_t i = 0; i < mEffectChains.size(); i++) {
6381 mEffectChains[i]->lock();
6382 }
6383}
6384
6385void AudioFlinger::ThreadBase::unlockEffectChains(
Glenn Kasten18db49a2012-03-12 16:29:55 -07006386 const Vector< sp<AudioFlinger::EffectChain> >& effectChains)
Eric Laurent464d5b32011-06-17 21:29:58 -07006387{
6388 for (size_t i = 0; i < effectChains.size(); i++) {
6389 effectChains[i]->unlock();
6390 }
6391}
6392
6393sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId)
6394{
6395 Mutex::Autolock _l(mLock);
6396 return getEffectChain_l(sessionId);
6397}
6398
6399sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId)
6400{
Eric Laurent464d5b32011-06-17 21:29:58 -07006401 size_t size = mEffectChains.size();
6402 for (size_t i = 0; i < size; i++) {
6403 if (mEffectChains[i]->sessionId() == sessionId) {
Glenn Kastenc64a6f02012-01-30 13:00:02 -08006404 return mEffectChains[i];
Eric Laurent464d5b32011-06-17 21:29:58 -07006405 }
6406 }
Glenn Kastenc64a6f02012-01-30 13:00:02 -08006407 return 0;
Eric Laurent464d5b32011-06-17 21:29:58 -07006408}
6409
Glenn Kastenaccb1142012-01-04 11:00:47 -08006410void AudioFlinger::ThreadBase::setMode(audio_mode_t mode)
Eric Laurent464d5b32011-06-17 21:29:58 -07006411{
6412 Mutex::Autolock _l(mLock);
6413 size_t size = mEffectChains.size();
6414 for (size_t i = 0; i < size; i++) {
6415 mEffectChains[i]->setMode_l(mode);
6416 }
6417}
6418
6419void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect,
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006420 const wp<EffectHandle>& handle,
Glenn Kasten29441ff2012-02-03 10:32:24 -08006421 bool unpinIfLast) {
Eric Laurentf82fccd2011-07-27 19:49:51 -07006422
Eric Laurent53334cd2010-06-23 17:38:20 -07006423 Mutex::Autolock _l(mLock);
Steve Block71f2cf12011-10-20 11:56:00 +01006424 ALOGV("disconnectEffect() %p effect %p", this, effect.get());
Eric Laurent53334cd2010-06-23 17:38:20 -07006425 // delete the effect module if removing last handle on it
6426 if (effect->removeHandle(handle) == 0) {
Glenn Kasten29441ff2012-02-03 10:32:24 -08006427 if (!effect->isPinned() || unpinIfLast) {
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006428 removeEffect_l(effect);
6429 AudioSystem::unregisterEffect(effect->id());
6430 }
Eric Laurent53334cd2010-06-23 17:38:20 -07006431 }
6432}
6433
Eric Laurent65b65452010-06-01 23:49:17 -07006434status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain)
6435{
6436 int session = chain->sessionId();
6437 int16_t *buffer = mMixBuffer;
Eric Laurent53334cd2010-06-23 17:38:20 -07006438 bool ownsBuffer = false;
Eric Laurent65b65452010-06-01 23:49:17 -07006439
Steve Block71f2cf12011-10-20 11:56:00 +01006440 ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
Eric Laurent53334cd2010-06-23 17:38:20 -07006441 if (session > 0) {
Eric Laurent65b65452010-06-01 23:49:17 -07006442 // Only one effect chain can be present in direct output thread and it uses
6443 // the mix buffer as input
6444 if (mType != DIRECT) {
6445 size_t numSamples = mFrameCount * mChannelCount;
6446 buffer = new int16_t[numSamples];
6447 memset(buffer, 0, numSamples * sizeof(int16_t));
Steve Block71f2cf12011-10-20 11:56:00 +01006448 ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session);
Eric Laurent65b65452010-06-01 23:49:17 -07006449 ownsBuffer = true;
6450 }
Eric Laurent65b65452010-06-01 23:49:17 -07006451
Eric Laurent53334cd2010-06-23 17:38:20 -07006452 // Attach all tracks with same session ID to this chain.
6453 for (size_t i = 0; i < mTracks.size(); ++i) {
6454 sp<Track> track = mTracks[i];
6455 if (session == track->sessionId()) {
Steve Block71f2cf12011-10-20 11:56:00 +01006456 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer);
Eric Laurent53334cd2010-06-23 17:38:20 -07006457 track->setMainBuffer(buffer);
Eric Laurent90681d62011-05-09 12:09:06 -07006458 chain->incTrackCnt();
Eric Laurent53334cd2010-06-23 17:38:20 -07006459 }
6460 }
6461
6462 // indicate all active tracks in the chain
6463 for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
6464 sp<Track> track = mActiveTracks[i].promote();
6465 if (track == 0) continue;
6466 if (session == track->sessionId()) {
Steve Block71f2cf12011-10-20 11:56:00 +01006467 ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session);
Eric Laurent90681d62011-05-09 12:09:06 -07006468 chain->incActiveTrackCnt();
Eric Laurent53334cd2010-06-23 17:38:20 -07006469 }
Eric Laurent65b65452010-06-01 23:49:17 -07006470 }
6471 }
6472
Eric Laurent53334cd2010-06-23 17:38:20 -07006473 chain->setInBuffer(buffer, ownsBuffer);
6474 chain->setOutBuffer(mMixBuffer);
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006475 // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006476 // chains list in order to be processed last as it contains output stage effects
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006477 // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before
6478 // session AUDIO_SESSION_OUTPUT_STAGE to be processed
Eric Laurent53334cd2010-06-23 17:38:20 -07006479 // after track specific effects and before output stage
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006480 // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and
6481 // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006482 // Effect chain for other sessions are inserted at beginning of effect
6483 // chains list to be processed before output mix effects. Relative order between other
6484 // sessions is not important
Eric Laurent53334cd2010-06-23 17:38:20 -07006485 size_t size = mEffectChains.size();
6486 size_t i = 0;
6487 for (i = 0; i < size; i++) {
6488 if (mEffectChains[i]->sessionId() < session) break;
Eric Laurent65b65452010-06-01 23:49:17 -07006489 }
Eric Laurent53334cd2010-06-23 17:38:20 -07006490 mEffectChains.insertAt(chain, i);
Eric Laurentf82fccd2011-07-27 19:49:51 -07006491 checkSuspendOnAddEffectChain_l(chain);
Eric Laurent65b65452010-06-01 23:49:17 -07006492
6493 return NO_ERROR;
6494}
6495
6496size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain)
6497{
6498 int session = chain->sessionId();
6499
Steve Block71f2cf12011-10-20 11:56:00 +01006500 ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
Eric Laurent65b65452010-06-01 23:49:17 -07006501
6502 for (size_t i = 0; i < mEffectChains.size(); i++) {
6503 if (chain == mEffectChains[i]) {
6504 mEffectChains.removeAt(i);
Eric Laurent90681d62011-05-09 12:09:06 -07006505 // detach all active tracks from the chain
6506 for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
6507 sp<Track> track = mActiveTracks[i].promote();
6508 if (track == 0) continue;
6509 if (session == track->sessionId()) {
Steve Block71f2cf12011-10-20 11:56:00 +01006510 ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d",
Eric Laurent90681d62011-05-09 12:09:06 -07006511 chain.get(), session);
6512 chain->decActiveTrackCnt();
6513 }
6514 }
6515
Eric Laurent65b65452010-06-01 23:49:17 -07006516 // detach all tracks with same session ID from this chain
6517 for (size_t i = 0; i < mTracks.size(); ++i) {
6518 sp<Track> track = mTracks[i];
6519 if (session == track->sessionId()) {
6520 track->setMainBuffer(mMixBuffer);
Eric Laurent90681d62011-05-09 12:09:06 -07006521 chain->decTrackCnt();
Eric Laurent65b65452010-06-01 23:49:17 -07006522 }
6523 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006524 break;
Eric Laurent65b65452010-06-01 23:49:17 -07006525 }
6526 }
6527 return mEffectChains.size();
6528}
6529
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006530status_t AudioFlinger::PlaybackThread::attachAuxEffect(
6531 const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
Eric Laurent65b65452010-06-01 23:49:17 -07006532{
6533 Mutex::Autolock _l(mLock);
6534 return attachAuxEffect_l(track, EffectId);
6535}
6536
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006537status_t AudioFlinger::PlaybackThread::attachAuxEffect_l(
6538 const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
Eric Laurent65b65452010-06-01 23:49:17 -07006539{
6540 status_t status = NO_ERROR;
6541
6542 if (EffectId == 0) {
6543 track->setAuxBuffer(0, NULL);
6544 } else {
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006545 // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX
6546 sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
Eric Laurent65b65452010-06-01 23:49:17 -07006547 if (effect != 0) {
6548 if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6549 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer());
6550 } else {
6551 status = INVALID_OPERATION;
6552 }
6553 } else {
6554 status = BAD_VALUE;
6555 }
6556 }
6557 return status;
6558}
6559
6560void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId)
6561{
Glenn Kasten18db49a2012-03-12 16:29:55 -07006562 for (size_t i = 0; i < mTracks.size(); ++i) {
Eric Laurent65b65452010-06-01 23:49:17 -07006563 sp<Track> track = mTracks[i];
6564 if (track->auxEffectId() == effectId) {
6565 attachAuxEffect_l(track, 0);
6566 }
6567 }
6568}
6569
Eric Laurent464d5b32011-06-17 21:29:58 -07006570status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain)
6571{
6572 // only one chain per input thread
6573 if (mEffectChains.size() != 0) {
6574 return INVALID_OPERATION;
6575 }
Steve Block71f2cf12011-10-20 11:56:00 +01006576 ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this);
Eric Laurent464d5b32011-06-17 21:29:58 -07006577
6578 chain->setInBuffer(NULL);
6579 chain->setOutBuffer(NULL);
6580
Eric Laurentf82fccd2011-07-27 19:49:51 -07006581 checkSuspendOnAddEffectChain_l(chain);
6582
Eric Laurent464d5b32011-06-17 21:29:58 -07006583 mEffectChains.add(chain);
6584
6585 return NO_ERROR;
6586}
6587
6588size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain)
6589{
Steve Block71f2cf12011-10-20 11:56:00 +01006590 ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this);
Steve Block8564c8d2012-01-05 23:22:43 +00006591 ALOGW_IF(mEffectChains.size() != 1,
Eric Laurent464d5b32011-06-17 21:29:58 -07006592 "removeEffectChain_l() %p invalid chain size %d on thread %p",
6593 chain.get(), mEffectChains.size(), this);
6594 if (mEffectChains.size() == 1) {
6595 mEffectChains.removeAt(0);
6596 }
6597 return 0;
6598}
6599
Eric Laurent65b65452010-06-01 23:49:17 -07006600// ----------------------------------------------------------------------------
6601// EffectModule implementation
6602// ----------------------------------------------------------------------------
6603
6604#undef LOG_TAG
6605#define LOG_TAG "AudioFlinger::EffectModule"
6606
Glenn Kasten685c9ce2012-01-20 13:32:16 -08006607AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
Eric Laurent65b65452010-06-01 23:49:17 -07006608 const wp<AudioFlinger::EffectChain>& chain,
6609 effect_descriptor_t *desc,
6610 int id,
6611 int sessionId)
Glenn Kasten685c9ce2012-01-20 13:32:16 -08006612 : mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL),
Eric Laurentf82fccd2011-07-27 19:49:51 -07006613 mStatus(NO_INIT), mState(IDLE), mSuspended(false)
Eric Laurent65b65452010-06-01 23:49:17 -07006614{
Steve Block71f2cf12011-10-20 11:56:00 +01006615 ALOGV("Constructor %p", this);
Eric Laurent65b65452010-06-01 23:49:17 -07006616 int lStatus;
Glenn Kasten685c9ce2012-01-20 13:32:16 -08006617 if (thread == NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07006618 return;
6619 }
Eric Laurent65b65452010-06-01 23:49:17 -07006620
6621 memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t));
6622
6623 // create effect engine from effect factory
Eric Laurent464d5b32011-06-17 21:29:58 -07006624 mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface);
Eric Laurent53334cd2010-06-23 17:38:20 -07006625
Eric Laurent65b65452010-06-01 23:49:17 -07006626 if (mStatus != NO_ERROR) {
6627 return;
6628 }
6629 lStatus = init();
6630 if (lStatus < 0) {
6631 mStatus = lStatus;
6632 goto Error;
6633 }
6634
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006635 if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) {
6636 mPinned = true;
6637 }
Steve Block71f2cf12011-10-20 11:56:00 +01006638 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface);
Eric Laurent65b65452010-06-01 23:49:17 -07006639 return;
6640Error:
6641 EffectRelease(mEffectInterface);
6642 mEffectInterface = NULL;
Steve Block71f2cf12011-10-20 11:56:00 +01006643 ALOGV("Constructor Error %d", mStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006644}
6645
6646AudioFlinger::EffectModule::~EffectModule()
6647{
Steve Block71f2cf12011-10-20 11:56:00 +01006648 ALOGV("Destructor %p", this);
Eric Laurent65b65452010-06-01 23:49:17 -07006649 if (mEffectInterface != NULL) {
Eric Laurent464d5b32011-06-17 21:29:58 -07006650 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
6651 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
6652 sp<ThreadBase> thread = mThread.promote();
6653 if (thread != 0) {
Eric Laurent828b9772011-08-07 16:32:26 -07006654 audio_stream_t *stream = thread->stream();
6655 if (stream != NULL) {
6656 stream->remove_audio_effect(stream, mEffectInterface);
6657 }
Eric Laurent464d5b32011-06-17 21:29:58 -07006658 }
6659 }
Eric Laurent65b65452010-06-01 23:49:17 -07006660 // release effect engine
6661 EffectRelease(mEffectInterface);
6662 }
6663}
6664
Glenn Kasten1f812f72012-01-30 10:15:48 -08006665status_t AudioFlinger::EffectModule::addHandle(const sp<EffectHandle>& handle)
Eric Laurent65b65452010-06-01 23:49:17 -07006666{
6667 status_t status;
6668
6669 Mutex::Autolock _l(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -07006670 int priority = handle->priority();
6671 size_t size = mHandles.size();
6672 sp<EffectHandle> h;
6673 size_t i;
6674 for (i = 0; i < size; i++) {
6675 h = mHandles[i].promote();
6676 if (h == 0) continue;
6677 if (h->priority() <= priority) break;
6678 }
6679 // if inserted in first place, move effect control from previous owner to this handle
6680 if (i == 0) {
Eric Laurentf82fccd2011-07-27 19:49:51 -07006681 bool enabled = false;
Eric Laurent65b65452010-06-01 23:49:17 -07006682 if (h != 0) {
Eric Laurentf82fccd2011-07-27 19:49:51 -07006683 enabled = h->enabled();
6684 h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
Eric Laurent65b65452010-06-01 23:49:17 -07006685 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07006686 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
Eric Laurent65b65452010-06-01 23:49:17 -07006687 status = NO_ERROR;
6688 } else {
6689 status = ALREADY_EXISTS;
6690 }
Steve Block71f2cf12011-10-20 11:56:00 +01006691 ALOGV("addHandle() %p added handle %p in position %d", this, handle.get(), i);
Eric Laurent65b65452010-06-01 23:49:17 -07006692 mHandles.insertAt(handle, i);
6693 return status;
6694}
6695
6696size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle)
6697{
6698 Mutex::Autolock _l(mLock);
6699 size_t size = mHandles.size();
6700 size_t i;
6701 for (i = 0; i < size; i++) {
6702 if (mHandles[i] == handle) break;
6703 }
6704 if (i == size) {
6705 return size;
6706 }
Steve Block71f2cf12011-10-20 11:56:00 +01006707 ALOGV("removeHandle() %p removed handle %p in position %d", this, handle.unsafe_get(), i);
Eric Laurentf82fccd2011-07-27 19:49:51 -07006708
6709 bool enabled = false;
6710 EffectHandle *hdl = handle.unsafe_get();
Glenn Kasten3694ec12012-01-27 16:47:15 -08006711 if (hdl != NULL) {
Steve Block71f2cf12011-10-20 11:56:00 +01006712 ALOGV("removeHandle() unsafe_get OK");
Eric Laurentf82fccd2011-07-27 19:49:51 -07006713 enabled = hdl->enabled();
6714 }
Eric Laurent65b65452010-06-01 23:49:17 -07006715 mHandles.removeAt(i);
6716 size = mHandles.size();
6717 // if removed from first place, move effect control from this handle to next in line
6718 if (i == 0 && size != 0) {
6719 sp<EffectHandle> h = mHandles[0].promote();
6720 if (h != 0) {
Eric Laurentf82fccd2011-07-27 19:49:51 -07006721 h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/);
Eric Laurent65b65452010-06-01 23:49:17 -07006722 }
6723 }
6724
Eric Laurent21b5c472011-07-26 20:54:46 -07006725 // Prevent calls to process() and other functions on effect interface from now on.
6726 // The effect engine will be released by the destructor when the last strong reference on
6727 // this object is released which can happen after next process is called.
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006728 if (size == 0 && !mPinned) {
Eric Laurent21b5c472011-07-26 20:54:46 -07006729 mState = DESTROYED;
Eric Laurent4fd3ecc2010-09-28 14:09:57 -07006730 }
6731
Eric Laurent65b65452010-06-01 23:49:17 -07006732 return size;
6733}
6734
Eric Laurentf82fccd2011-07-27 19:49:51 -07006735sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle()
6736{
6737 Mutex::Autolock _l(mLock);
Glenn Kastenc64a6f02012-01-30 13:00:02 -08006738 return mHandles.size() != 0 ? mHandles[0].promote() : 0;
Eric Laurentf82fccd2011-07-27 19:49:51 -07006739}
6740
Glenn Kasten29441ff2012-02-03 10:32:24 -08006741void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpinIfLast)
Eric Laurent65b65452010-06-01 23:49:17 -07006742{
Glenn Kasten0765c442012-01-27 15:24:38 -08006743 ALOGV("disconnect() %p handle %p", this, handle.unsafe_get());
Eric Laurent65b65452010-06-01 23:49:17 -07006744 // keep a strong reference on this EffectModule to avoid calling the
6745 // destructor before we exit
6746 sp<EffectModule> keep(this);
Eric Laurent53334cd2010-06-23 17:38:20 -07006747 {
6748 sp<ThreadBase> thread = mThread.promote();
6749 if (thread != 0) {
Glenn Kasten29441ff2012-02-03 10:32:24 -08006750 thread->disconnectEffect(keep, handle, unpinIfLast);
Eric Laurent65b65452010-06-01 23:49:17 -07006751 }
6752 }
6753}
6754
Eric Laurent7d850f22010-07-09 13:34:17 -07006755void AudioFlinger::EffectModule::updateState() {
6756 Mutex::Autolock _l(mLock);
6757
6758 switch (mState) {
6759 case RESTART:
6760 reset_l();
6761 // FALL THROUGH
6762
6763 case STARTING:
6764 // clear auxiliary effect input buffer for next accumulation
6765 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6766 memset(mConfig.inputCfg.buffer.raw,
6767 0,
6768 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
6769 }
6770 start_l();
6771 mState = ACTIVE;
6772 break;
6773 case STOPPING:
6774 stop_l();
6775 mDisableWaitCnt = mMaxDisableWaitCnt;
6776 mState = STOPPED;
6777 break;
6778 case STOPPED:
6779 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
6780 // turn off sequence.
6781 if (--mDisableWaitCnt == 0) {
6782 reset_l();
6783 mState = IDLE;
6784 }
6785 break;
Eric Laurent21b5c472011-07-26 20:54:46 -07006786 default: //IDLE , ACTIVE, DESTROYED
Eric Laurent7d850f22010-07-09 13:34:17 -07006787 break;
6788 }
6789}
6790
Eric Laurent65b65452010-06-01 23:49:17 -07006791void AudioFlinger::EffectModule::process()
6792{
6793 Mutex::Autolock _l(mLock);
6794
Eric Laurent21b5c472011-07-26 20:54:46 -07006795 if (mState == DESTROYED || mEffectInterface == NULL ||
Eric Laurent7d850f22010-07-09 13:34:17 -07006796 mConfig.inputCfg.buffer.raw == NULL ||
6797 mConfig.outputCfg.buffer.raw == NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07006798 return;
6799 }
6800
Eric Laurenta92ebfa2010-08-31 13:50:07 -07006801 if (isProcessEnabled()) {
Eric Laurent65b65452010-06-01 23:49:17 -07006802 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
6803 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kasten490909d2011-12-15 09:52:39 -08006804 ditherAndClamp(mConfig.inputCfg.buffer.s32,
Eric Laurent65b65452010-06-01 23:49:17 -07006805 mConfig.inputCfg.buffer.s32,
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006806 mConfig.inputCfg.buffer.frameCount/2);
Eric Laurent65b65452010-06-01 23:49:17 -07006807 }
6808
Eric Laurent65b65452010-06-01 23:49:17 -07006809 // do the actual processing in the effect engine
Eric Laurent7d850f22010-07-09 13:34:17 -07006810 int ret = (*mEffectInterface)->process(mEffectInterface,
6811 &mConfig.inputCfg.buffer,
6812 &mConfig.outputCfg.buffer);
6813
6814 // force transition to IDLE state when engine is ready
6815 if (mState == STOPPED && ret == -ENODATA) {
6816 mDisableWaitCnt = 1;
6817 }
Eric Laurent65b65452010-06-01 23:49:17 -07006818
6819 // clear auxiliary effect input buffer for next accumulation
6820 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurent67b5ed32011-01-19 18:36:13 -08006821 memset(mConfig.inputCfg.buffer.raw, 0,
6822 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
Eric Laurent65b65452010-06-01 23:49:17 -07006823 }
6824 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
Eric Laurent67b5ed32011-01-19 18:36:13 -08006825 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
6826 // If an insert effect is idle and input buffer is different from output buffer,
6827 // accumulate input onto output
Eric Laurent65b65452010-06-01 23:49:17 -07006828 sp<EffectChain> chain = mChain.promote();
Eric Laurent90681d62011-05-09 12:09:06 -07006829 if (chain != 0 && chain->activeTrackCnt() != 0) {
Eric Laurent67b5ed32011-01-19 18:36:13 -08006830 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here
6831 int16_t *in = mConfig.inputCfg.buffer.s16;
6832 int16_t *out = mConfig.outputCfg.buffer.s16;
6833 for (size_t i = 0; i < frameCnt; i++) {
6834 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
Eric Laurent65b65452010-06-01 23:49:17 -07006835 }
Eric Laurent65b65452010-06-01 23:49:17 -07006836 }
6837 }
6838}
6839
Eric Laurentdf9b81c2010-07-02 08:12:41 -07006840void AudioFlinger::EffectModule::reset_l()
Eric Laurent65b65452010-06-01 23:49:17 -07006841{
6842 if (mEffectInterface == NULL) {
6843 return;
6844 }
6845 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
6846}
6847
6848status_t AudioFlinger::EffectModule::configure()
6849{
6850 uint32_t channels;
6851 if (mEffectInterface == NULL) {
6852 return NO_INIT;
6853 }
6854
6855 sp<ThreadBase> thread = mThread.promote();
6856 if (thread == 0) {
6857 return DEAD_OBJECT;
6858 }
6859
6860 // TODO: handle configuration of effects replacing track process
6861 if (thread->channelCount() == 1) {
Eric Laurent0fb66c22011-05-17 19:16:02 -07006862 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurent65b65452010-06-01 23:49:17 -07006863 } else {
Eric Laurent0fb66c22011-05-17 19:16:02 -07006864 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurent65b65452010-06-01 23:49:17 -07006865 }
6866
6867 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurent0fb66c22011-05-17 19:16:02 -07006868 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurent65b65452010-06-01 23:49:17 -07006869 } else {
6870 mConfig.inputCfg.channels = channels;
6871 }
6872 mConfig.outputCfg.channels = channels;
Eric Laurent0fb66c22011-05-17 19:16:02 -07006873 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
6874 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurent65b65452010-06-01 23:49:17 -07006875 mConfig.inputCfg.samplingRate = thread->sampleRate();
6876 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
6877 mConfig.inputCfg.bufferProvider.cookie = NULL;
6878 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
6879 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
6880 mConfig.outputCfg.bufferProvider.cookie = NULL;
6881 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
6882 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
6883 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
6884 // Insert effect:
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006885 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006886 // always overwrites output buffer: input buffer == output buffer
Eric Laurent65b65452010-06-01 23:49:17 -07006887 // - in other sessions:
6888 // last effect in the chain accumulates in output buffer: input buffer != output buffer
6889 // other effect: overwrites output buffer: input buffer == output buffer
6890 // Auxiliary effect:
6891 // accumulates in output buffer: input buffer != output buffer
6892 // Therefore: accumulate <=> input buffer != output buffer
6893 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
6894 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
6895 } else {
6896 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
6897 }
6898 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
6899 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
6900 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
6901 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
6902
Steve Block71f2cf12011-10-20 11:56:00 +01006903 ALOGV("configure() %p thread %p buffer %p framecount %d",
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006904 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
6905
Eric Laurent65b65452010-06-01 23:49:17 -07006906 status_t cmdStatus;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07006907 uint32_t size = sizeof(int);
6908 status_t status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurent4abf8822011-12-16 15:30:36 -08006909 EFFECT_CMD_SET_CONFIG,
Eric Laurenta4c72ac2010-07-28 05:40:18 -07006910 sizeof(effect_config_t),
6911 &mConfig,
6912 &size,
6913 &cmdStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006914 if (status == 0) {
6915 status = cmdStatus;
6916 }
Eric Laurent7d850f22010-07-09 13:34:17 -07006917
6918 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
6919 (1000 * mConfig.outputCfg.buffer.frameCount);
6920
Eric Laurent65b65452010-06-01 23:49:17 -07006921 return status;
6922}
6923
6924status_t AudioFlinger::EffectModule::init()
6925{
Eric Laurentdf9b81c2010-07-02 08:12:41 -07006926 Mutex::Autolock _l(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -07006927 if (mEffectInterface == NULL) {
6928 return NO_INIT;
6929 }
6930 status_t cmdStatus;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07006931 uint32_t size = sizeof(status_t);
6932 status_t status = (*mEffectInterface)->command(mEffectInterface,
6933 EFFECT_CMD_INIT,
6934 0,
6935 NULL,
6936 &size,
6937 &cmdStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006938 if (status == 0) {
6939 status = cmdStatus;
6940 }
6941 return status;
6942}
6943
Eric Laurent6fccbd02011-10-05 17:42:25 -07006944status_t AudioFlinger::EffectModule::start()
6945{
6946 Mutex::Autolock _l(mLock);
6947 return start_l();
6948}
6949
Eric Laurentdf9b81c2010-07-02 08:12:41 -07006950status_t AudioFlinger::EffectModule::start_l()
Eric Laurent65b65452010-06-01 23:49:17 -07006951{
6952 if (mEffectInterface == NULL) {
6953 return NO_INIT;
6954 }
6955 status_t cmdStatus;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07006956 uint32_t size = sizeof(status_t);
6957 status_t status = (*mEffectInterface)->command(mEffectInterface,
6958 EFFECT_CMD_ENABLE,
6959 0,
6960 NULL,
6961 &size,
6962 &cmdStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006963 if (status == 0) {
6964 status = cmdStatus;
6965 }
Eric Laurent464d5b32011-06-17 21:29:58 -07006966 if (status == 0 &&
6967 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
6968 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
6969 sp<ThreadBase> thread = mThread.promote();
6970 if (thread != 0) {
Eric Laurent828b9772011-08-07 16:32:26 -07006971 audio_stream_t *stream = thread->stream();
6972 if (stream != NULL) {
6973 stream->add_audio_effect(stream, mEffectInterface);
6974 }
Eric Laurent464d5b32011-06-17 21:29:58 -07006975 }
6976 }
Eric Laurent65b65452010-06-01 23:49:17 -07006977 return status;
6978}
6979
Eric Laurent21b5c472011-07-26 20:54:46 -07006980status_t AudioFlinger::EffectModule::stop()
6981{
6982 Mutex::Autolock _l(mLock);
6983 return stop_l();
6984}
6985
Eric Laurentdf9b81c2010-07-02 08:12:41 -07006986status_t AudioFlinger::EffectModule::stop_l()
Eric Laurent65b65452010-06-01 23:49:17 -07006987{
6988 if (mEffectInterface == NULL) {
6989 return NO_INIT;
6990 }
6991 status_t cmdStatus;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07006992 uint32_t size = sizeof(status_t);
6993 status_t status = (*mEffectInterface)->command(mEffectInterface,
6994 EFFECT_CMD_DISABLE,
6995 0,
6996 NULL,
6997 &size,
6998 &cmdStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006999 if (status == 0) {
7000 status = cmdStatus;
7001 }
Eric Laurent464d5b32011-06-17 21:29:58 -07007002 if (status == 0 &&
7003 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
7004 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
7005 sp<ThreadBase> thread = mThread.promote();
7006 if (thread != 0) {
Eric Laurent828b9772011-08-07 16:32:26 -07007007 audio_stream_t *stream = thread->stream();
7008 if (stream != NULL) {
7009 stream->remove_audio_effect(stream, mEffectInterface);
7010 }
Eric Laurent464d5b32011-06-17 21:29:58 -07007011 }
7012 }
Eric Laurent65b65452010-06-01 23:49:17 -07007013 return status;
7014}
7015
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007016status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
7017 uint32_t cmdSize,
7018 void *pCmdData,
7019 uint32_t *replySize,
7020 void *pReplyData)
Eric Laurent65b65452010-06-01 23:49:17 -07007021{
Eric Laurentdf9b81c2010-07-02 08:12:41 -07007022 Mutex::Autolock _l(mLock);
Steve Block71f2cf12011-10-20 11:56:00 +01007023// ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
Eric Laurent65b65452010-06-01 23:49:17 -07007024
Eric Laurent21b5c472011-07-26 20:54:46 -07007025 if (mState == DESTROYED || mEffectInterface == NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07007026 return NO_INIT;
7027 }
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007028 status_t status = (*mEffectInterface)->command(mEffectInterface,
7029 cmdCode,
7030 cmdSize,
7031 pCmdData,
7032 replySize,
7033 pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07007034 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007035 uint32_t size = (replySize == NULL) ? 0 : *replySize;
Eric Laurent65b65452010-06-01 23:49:17 -07007036 for (size_t i = 1; i < mHandles.size(); i++) {
7037 sp<EffectHandle> h = mHandles[i].promote();
7038 if (h != 0) {
7039 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
7040 }
7041 }
7042 }
7043 return status;
7044}
7045
7046status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
7047{
Eric Laurent6752ec82011-08-10 10:37:50 -07007048
Eric Laurent65b65452010-06-01 23:49:17 -07007049 Mutex::Autolock _l(mLock);
Steve Block71f2cf12011-10-20 11:56:00 +01007050 ALOGV("setEnabled %p enabled %d", this, enabled);
Eric Laurent65b65452010-06-01 23:49:17 -07007051
7052 if (enabled != isEnabled()) {
Eric Laurent6752ec82011-08-10 10:37:50 -07007053 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
7054 if (enabled && status != NO_ERROR) {
7055 return status;
7056 }
7057
Eric Laurent65b65452010-06-01 23:49:17 -07007058 switch (mState) {
7059 // going from disabled to enabled
7060 case IDLE:
Eric Laurent7d850f22010-07-09 13:34:17 -07007061 mState = STARTING;
7062 break;
7063 case STOPPED:
7064 mState = RESTART;
Eric Laurent65b65452010-06-01 23:49:17 -07007065 break;
7066 case STOPPING:
7067 mState = ACTIVE;
7068 break;
Eric Laurent65b65452010-06-01 23:49:17 -07007069
7070 // going from enabled to disabled
Eric Laurent7d850f22010-07-09 13:34:17 -07007071 case RESTART:
Eric Laurenta92ebfa2010-08-31 13:50:07 -07007072 mState = STOPPED;
7073 break;
Eric Laurent65b65452010-06-01 23:49:17 -07007074 case STARTING:
Eric Laurent7d850f22010-07-09 13:34:17 -07007075 mState = IDLE;
Eric Laurent65b65452010-06-01 23:49:17 -07007076 break;
7077 case ACTIVE:
7078 mState = STOPPING;
7079 break;
Eric Laurent21b5c472011-07-26 20:54:46 -07007080 case DESTROYED:
7081 return NO_ERROR; // simply ignore as we are being destroyed
Eric Laurent65b65452010-06-01 23:49:17 -07007082 }
7083 for (size_t i = 1; i < mHandles.size(); i++) {
7084 sp<EffectHandle> h = mHandles[i].promote();
7085 if (h != 0) {
7086 h->setEnabled(enabled);
7087 }
7088 }
7089 }
7090 return NO_ERROR;
7091}
7092
Glenn Kasteneabd94a2012-02-02 14:06:11 -08007093bool AudioFlinger::EffectModule::isEnabled() const
Eric Laurent65b65452010-06-01 23:49:17 -07007094{
7095 switch (mState) {
Eric Laurent7d850f22010-07-09 13:34:17 -07007096 case RESTART:
Eric Laurent65b65452010-06-01 23:49:17 -07007097 case STARTING:
7098 case ACTIVE:
7099 return true;
7100 case IDLE:
7101 case STOPPING:
7102 case STOPPED:
Eric Laurent21b5c472011-07-26 20:54:46 -07007103 case DESTROYED:
Eric Laurent65b65452010-06-01 23:49:17 -07007104 default:
7105 return false;
7106 }
7107}
7108
Glenn Kasteneabd94a2012-02-02 14:06:11 -08007109bool AudioFlinger::EffectModule::isProcessEnabled() const
Eric Laurenta92ebfa2010-08-31 13:50:07 -07007110{
7111 switch (mState) {
7112 case RESTART:
7113 case ACTIVE:
7114 case STOPPING:
7115 case STOPPED:
7116 return true;
7117 case IDLE:
7118 case STARTING:
Eric Laurent21b5c472011-07-26 20:54:46 -07007119 case DESTROYED:
Eric Laurenta92ebfa2010-08-31 13:50:07 -07007120 default:
7121 return false;
7122 }
7123}
7124
Eric Laurent65b65452010-06-01 23:49:17 -07007125status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
7126{
Eric Laurentdf9b81c2010-07-02 08:12:41 -07007127 Mutex::Autolock _l(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -07007128 status_t status = NO_ERROR;
7129
7130 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
7131 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
Eric Laurenta92ebfa2010-08-31 13:50:07 -07007132 if (isProcessEnabled() &&
Eric Laurent0d7e0482010-07-19 06:24:46 -07007133 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
7134 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurent65b65452010-06-01 23:49:17 -07007135 status_t cmdStatus;
7136 uint32_t volume[2];
7137 uint32_t *pVolume = NULL;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007138 uint32_t size = sizeof(volume);
Eric Laurent65b65452010-06-01 23:49:17 -07007139 volume[0] = *left;
7140 volume[1] = *right;
7141 if (controller) {
7142 pVolume = volume;
7143 }
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007144 status = (*mEffectInterface)->command(mEffectInterface,
7145 EFFECT_CMD_SET_VOLUME,
7146 size,
7147 volume,
7148 &size,
7149 pVolume);
Eric Laurent65b65452010-06-01 23:49:17 -07007150 if (controller && status == NO_ERROR && size == sizeof(volume)) {
7151 *left = volume[0];
7152 *right = volume[1];
7153 }
7154 }
7155 return status;
7156}
7157
7158status_t AudioFlinger::EffectModule::setDevice(uint32_t device)
7159{
Eric Laurentdf9b81c2010-07-02 08:12:41 -07007160 Mutex::Autolock _l(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -07007161 status_t status = NO_ERROR;
Eric Laurent464d5b32011-06-17 21:29:58 -07007162 if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
7163 // audio pre processing modules on RecordThread can receive both output and
7164 // input device indication in the same call
7165 uint32_t dev = device & AUDIO_DEVICE_OUT_ALL;
7166 if (dev) {
7167 status_t cmdStatus;
7168 uint32_t size = sizeof(status_t);
7169
7170 status = (*mEffectInterface)->command(mEffectInterface,
7171 EFFECT_CMD_SET_DEVICE,
7172 sizeof(uint32_t),
7173 &dev,
7174 &size,
7175 &cmdStatus);
7176 if (status == NO_ERROR) {
7177 status = cmdStatus;
7178 }
7179 }
7180 dev = device & AUDIO_DEVICE_IN_ALL;
7181 if (dev) {
7182 status_t cmdStatus;
7183 uint32_t size = sizeof(status_t);
7184
7185 status_t status2 = (*mEffectInterface)->command(mEffectInterface,
7186 EFFECT_CMD_SET_INPUT_DEVICE,
7187 sizeof(uint32_t),
7188 &dev,
7189 &size,
7190 &cmdStatus);
7191 if (status2 == NO_ERROR) {
7192 status2 = cmdStatus;
7193 }
7194 if (status == NO_ERROR) {
7195 status = status2;
7196 }
Eric Laurent65b65452010-06-01 23:49:17 -07007197 }
7198 }
7199 return status;
7200}
7201
Glenn Kastenaccb1142012-01-04 11:00:47 -08007202status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
Eric Laurent53334cd2010-06-23 17:38:20 -07007203{
Eric Laurentdf9b81c2010-07-02 08:12:41 -07007204 Mutex::Autolock _l(mLock);
Eric Laurent53334cd2010-06-23 17:38:20 -07007205 status_t status = NO_ERROR;
7206 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
Eric Laurent53334cd2010-06-23 17:38:20 -07007207 status_t cmdStatus;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007208 uint32_t size = sizeof(status_t);
7209 status = (*mEffectInterface)->command(mEffectInterface,
7210 EFFECT_CMD_SET_AUDIO_MODE,
Glenn Kastenaccb1142012-01-04 11:00:47 -08007211 sizeof(audio_mode_t),
Eric Laurent0fb66c22011-05-17 19:16:02 -07007212 &mode,
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007213 &size,
7214 &cmdStatus);
Eric Laurent53334cd2010-06-23 17:38:20 -07007215 if (status == NO_ERROR) {
7216 status = cmdStatus;
7217 }
7218 }
7219 return status;
7220}
7221
Eric Laurentf82fccd2011-07-27 19:49:51 -07007222void AudioFlinger::EffectModule::setSuspended(bool suspended)
7223{
7224 Mutex::Autolock _l(mLock);
7225 mSuspended = suspended;
7226}
Glenn Kasten1dce8412012-01-04 11:01:11 -08007227
7228bool AudioFlinger::EffectModule::suspended() const
Eric Laurentf82fccd2011-07-27 19:49:51 -07007229{
7230 Mutex::Autolock _l(mLock);
7231 return mSuspended;
7232}
7233
Eric Laurent65b65452010-06-01 23:49:17 -07007234status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
7235{
7236 const size_t SIZE = 256;
7237 char buffer[SIZE];
7238 String8 result;
7239
7240 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
7241 result.append(buffer);
7242
7243 bool locked = tryLock(mLock);
7244 // failed to lock - AudioFlinger is probably deadlocked
7245 if (!locked) {
7246 result.append("\t\tCould not lock Fx mutex:\n");
7247 }
7248
7249 result.append("\t\tSession Status State Engine:\n");
7250 snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n",
7251 mSessionId, mStatus, mState, (uint32_t)mEffectInterface);
7252 result.append(buffer);
7253
7254 result.append("\t\tDescriptor:\n");
7255 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
7256 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
7257 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2],
7258 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
7259 result.append(buffer);
7260 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
7261 mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion,
7262 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2],
7263 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
7264 result.append(buffer);
Eric Laurent0fb66c22011-05-17 19:16:02 -07007265 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n",
Eric Laurent65b65452010-06-01 23:49:17 -07007266 mDescriptor.apiVersion,
7267 mDescriptor.flags);
7268 result.append(buffer);
7269 snprintf(buffer, SIZE, "\t\t- name: %s\n",
7270 mDescriptor.name);
7271 result.append(buffer);
7272 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
7273 mDescriptor.implementor);
7274 result.append(buffer);
7275
7276 result.append("\t\t- Input configuration:\n");
7277 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
7278 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
7279 (uint32_t)mConfig.inputCfg.buffer.raw,
7280 mConfig.inputCfg.buffer.frameCount,
7281 mConfig.inputCfg.samplingRate,
7282 mConfig.inputCfg.channels,
7283 mConfig.inputCfg.format);
7284 result.append(buffer);
7285
7286 result.append("\t\t- Output configuration:\n");
7287 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
7288 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
7289 (uint32_t)mConfig.outputCfg.buffer.raw,
7290 mConfig.outputCfg.buffer.frameCount,
7291 mConfig.outputCfg.samplingRate,
7292 mConfig.outputCfg.channels,
7293 mConfig.outputCfg.format);
7294 result.append(buffer);
7295
7296 snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size());
7297 result.append(buffer);
7298 result.append("\t\t\tPid Priority Ctrl Locked client server\n");
7299 for (size_t i = 0; i < mHandles.size(); ++i) {
7300 sp<EffectHandle> handle = mHandles[i].promote();
7301 if (handle != 0) {
7302 handle->dump(buffer, SIZE);
7303 result.append(buffer);
7304 }
7305 }
7306
7307 result.append("\n");
7308
7309 write(fd, result.string(), result.length());
7310
7311 if (locked) {
7312 mLock.unlock();
7313 }
7314
7315 return NO_ERROR;
7316}
7317
7318// ----------------------------------------------------------------------------
7319// EffectHandle implementation
7320// ----------------------------------------------------------------------------
7321
7322#undef LOG_TAG
7323#define LOG_TAG "AudioFlinger::EffectHandle"
7324
7325AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
7326 const sp<AudioFlinger::Client>& client,
7327 const sp<IEffectClient>& effectClient,
7328 int32_t priority)
7329 : BnEffect(),
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007330 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentf82fccd2011-07-27 19:49:51 -07007331 mPriority(priority), mHasControl(false), mEnabled(false)
Eric Laurent65b65452010-06-01 23:49:17 -07007332{
Steve Block71f2cf12011-10-20 11:56:00 +01007333 ALOGV("constructor %p", this);
Eric Laurent65b65452010-06-01 23:49:17 -07007334
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007335 if (client == 0) {
7336 return;
7337 }
Eric Laurent65b65452010-06-01 23:49:17 -07007338 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
7339 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
7340 if (mCblkMemory != 0) {
7341 mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer());
7342
Glenn Kasten3694ec12012-01-27 16:47:15 -08007343 if (mCblk != NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07007344 new(mCblk) effect_param_cblk_t();
7345 mBuffer = (uint8_t *)mCblk + bufOffset;
Glenn Kasten18db49a2012-03-12 16:29:55 -07007346 }
Eric Laurent65b65452010-06-01 23:49:17 -07007347 } else {
Steve Block3762c312012-01-06 19:20:56 +00007348 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t));
Eric Laurent65b65452010-06-01 23:49:17 -07007349 return;
7350 }
7351}
7352
7353AudioFlinger::EffectHandle::~EffectHandle()
7354{
Steve Block71f2cf12011-10-20 11:56:00 +01007355 ALOGV("Destructor %p", this);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007356 disconnect(false);
Steve Block71f2cf12011-10-20 11:56:00 +01007357 ALOGV("Destructor DONE %p", this);
Eric Laurent65b65452010-06-01 23:49:17 -07007358}
7359
7360status_t AudioFlinger::EffectHandle::enable()
7361{
Steve Block71f2cf12011-10-20 11:56:00 +01007362 ALOGV("enable %p", this);
Eric Laurent65b65452010-06-01 23:49:17 -07007363 if (!mHasControl) return INVALID_OPERATION;
7364 if (mEffect == 0) return DEAD_OBJECT;
7365
Eric Laurent6752ec82011-08-10 10:37:50 -07007366 if (mEnabled) {
7367 return NO_ERROR;
7368 }
7369
Eric Laurentf82fccd2011-07-27 19:49:51 -07007370 mEnabled = true;
7371
7372 sp<ThreadBase> thread = mEffect->thread().promote();
7373 if (thread != 0) {
7374 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
7375 }
7376
7377 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
7378 if (mEffect->suspended()) {
7379 return NO_ERROR;
7380 }
7381
Eric Laurent6752ec82011-08-10 10:37:50 -07007382 status_t status = mEffect->setEnabled(true);
7383 if (status != NO_ERROR) {
7384 if (thread != 0) {
7385 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
7386 }
7387 mEnabled = false;
7388 }
7389 return status;
Eric Laurent65b65452010-06-01 23:49:17 -07007390}
7391
7392status_t AudioFlinger::EffectHandle::disable()
7393{
Steve Block71f2cf12011-10-20 11:56:00 +01007394 ALOGV("disable %p", this);
Eric Laurent65b65452010-06-01 23:49:17 -07007395 if (!mHasControl) return INVALID_OPERATION;
Eric Laurentf82fccd2011-07-27 19:49:51 -07007396 if (mEffect == 0) return DEAD_OBJECT;
Eric Laurent65b65452010-06-01 23:49:17 -07007397
Eric Laurent6752ec82011-08-10 10:37:50 -07007398 if (!mEnabled) {
7399 return NO_ERROR;
7400 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07007401 mEnabled = false;
7402
7403 if (mEffect->suspended()) {
7404 return NO_ERROR;
7405 }
7406
7407 status_t status = mEffect->setEnabled(false);
7408
7409 sp<ThreadBase> thread = mEffect->thread().promote();
7410 if (thread != 0) {
7411 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
7412 }
7413
7414 return status;
Eric Laurent65b65452010-06-01 23:49:17 -07007415}
7416
7417void AudioFlinger::EffectHandle::disconnect()
7418{
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007419 disconnect(true);
7420}
7421
Glenn Kasten29441ff2012-02-03 10:32:24 -08007422void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007423{
Glenn Kasten29441ff2012-02-03 10:32:24 -08007424 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
Eric Laurent65b65452010-06-01 23:49:17 -07007425 if (mEffect == 0) {
7426 return;
7427 }
Glenn Kasten29441ff2012-02-03 10:32:24 -08007428 mEffect->disconnect(this, unpinIfLast);
Eric Laurentf82fccd2011-07-27 19:49:51 -07007429
Eric Laurent7fa1cee2011-10-19 11:44:54 -07007430 if (mHasControl && mEnabled) {
Eric Laurent6752ec82011-08-10 10:37:50 -07007431 sp<ThreadBase> thread = mEffect->thread().promote();
7432 if (thread != 0) {
7433 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
7434 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07007435 }
7436
Eric Laurent65b65452010-06-01 23:49:17 -07007437 // release sp on module => module destructor can be called now
7438 mEffect.clear();
Eric Laurent65b65452010-06-01 23:49:17 -07007439 if (mClient != 0) {
Glenn Kasten3694ec12012-01-27 16:47:15 -08007440 if (mCblk != NULL) {
Glenn Kastend9d68dc2012-02-03 10:24:48 -08007441 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007442 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
7443 }
Glenn Kastenf31d0502012-01-25 15:27:15 -08007444 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Glenn Kasten803a86a2012-01-25 14:28:29 -08007445 // Client destructor must run with AudioFlinger mutex locked
Eric Laurent65b65452010-06-01 23:49:17 -07007446 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
7447 mClient.clear();
7448 }
7449}
7450
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007451status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
7452 uint32_t cmdSize,
7453 void *pCmdData,
7454 uint32_t *replySize,
7455 void *pReplyData)
Eric Laurent65b65452010-06-01 23:49:17 -07007456{
Steve Block71f2cf12011-10-20 11:56:00 +01007457// ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007458// cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
Eric Laurent65b65452010-06-01 23:49:17 -07007459
7460 // only get parameter command is permitted for applications not controlling the effect
7461 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
7462 return INVALID_OPERATION;
7463 }
7464 if (mEffect == 0) return DEAD_OBJECT;
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007465 if (mClient == 0) return INVALID_OPERATION;
Eric Laurent65b65452010-06-01 23:49:17 -07007466
7467 // handle commands that are not forwarded transparently to effect engine
7468 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
7469 // No need to trylock() here as this function is executed in the binder thread serving a particular client process:
7470 // no risk to block the whole media server process or mixer threads is we are stuck here
7471 Mutex::Autolock _l(mCblk->lock);
7472 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
7473 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
7474 mCblk->serverIndex = 0;
7475 mCblk->clientIndex = 0;
7476 return BAD_VALUE;
7477 }
7478 status_t status = NO_ERROR;
7479 while (mCblk->serverIndex < mCblk->clientIndex) {
7480 int reply;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007481 uint32_t rsize = sizeof(int);
Eric Laurent65b65452010-06-01 23:49:17 -07007482 int *p = (int *)(mBuffer + mCblk->serverIndex);
7483 int size = *p++;
Eric Laurent53334cd2010-06-23 17:38:20 -07007484 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
Steve Block8564c8d2012-01-05 23:22:43 +00007485 ALOGW("command(): invalid parameter block size");
Eric Laurent53334cd2010-06-23 17:38:20 -07007486 break;
7487 }
Eric Laurent65b65452010-06-01 23:49:17 -07007488 effect_param_t *param = (effect_param_t *)p;
Eric Laurent53334cd2010-06-23 17:38:20 -07007489 if (param->psize == 0 || param->vsize == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00007490 ALOGW("command(): null parameter or value size");
Eric Laurent53334cd2010-06-23 17:38:20 -07007491 mCblk->serverIndex += size;
7492 continue;
7493 }
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007494 uint32_t psize = sizeof(effect_param_t) +
7495 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
7496 param->vsize;
7497 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
7498 psize,
7499 p,
7500 &rsize,
7501 &reply);
Eric Laurente65280c2010-09-02 11:56:55 -07007502 // stop at first error encountered
7503 if (ret != NO_ERROR) {
Eric Laurent65b65452010-06-01 23:49:17 -07007504 status = ret;
Eric Laurente65280c2010-09-02 11:56:55 -07007505 *(int *)pReplyData = reply;
7506 break;
7507 } else if (reply != NO_ERROR) {
7508 *(int *)pReplyData = reply;
7509 break;
Eric Laurent65b65452010-06-01 23:49:17 -07007510 }
7511 mCblk->serverIndex += size;
7512 }
7513 mCblk->serverIndex = 0;
7514 mCblk->clientIndex = 0;
7515 return status;
7516 } else if (cmdCode == EFFECT_CMD_ENABLE) {
Eric Laurente65280c2010-09-02 11:56:55 -07007517 *(int *)pReplyData = NO_ERROR;
Eric Laurent65b65452010-06-01 23:49:17 -07007518 return enable();
7519 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurente65280c2010-09-02 11:56:55 -07007520 *(int *)pReplyData = NO_ERROR;
Eric Laurent65b65452010-06-01 23:49:17 -07007521 return disable();
7522 }
7523
7524 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
7525}
7526
Eric Laurentf82fccd2011-07-27 19:49:51 -07007527void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
Eric Laurent65b65452010-06-01 23:49:17 -07007528{
Steve Block71f2cf12011-10-20 11:56:00 +01007529 ALOGV("setControl %p control %d", this, hasControl);
Eric Laurent65b65452010-06-01 23:49:17 -07007530
7531 mHasControl = hasControl;
Eric Laurentf82fccd2011-07-27 19:49:51 -07007532 mEnabled = enabled;
7533
Eric Laurent65b65452010-06-01 23:49:17 -07007534 if (signal && mEffectClient != 0) {
7535 mEffectClient->controlStatusChanged(hasControl);
7536 }
7537}
7538
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007539void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
7540 uint32_t cmdSize,
7541 void *pCmdData,
7542 uint32_t replySize,
7543 void *pReplyData)
Eric Laurent65b65452010-06-01 23:49:17 -07007544{
7545 if (mEffectClient != 0) {
7546 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
7547 }
7548}
7549
7550
7551
7552void AudioFlinger::EffectHandle::setEnabled(bool enabled)
7553{
7554 if (mEffectClient != 0) {
7555 mEffectClient->enableStatusChanged(enabled);
7556 }
7557}
7558
7559status_t AudioFlinger::EffectHandle::onTransact(
7560 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
7561{
7562 return BnEffect::onTransact(code, data, reply, flags);
7563}
7564
7565
7566void AudioFlinger::EffectHandle::dump(char* buffer, size_t size)
7567{
Glenn Kasten3694ec12012-01-27 16:47:15 -08007568 bool locked = mCblk != NULL && tryLock(mCblk->lock);
Eric Laurent65b65452010-06-01 23:49:17 -07007569
7570 snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n",
Glenn Kasten81211142012-02-05 18:09:08 -08007571 (mClient == 0) ? getpid_cached : mClient->pid(),
Eric Laurent65b65452010-06-01 23:49:17 -07007572 mPriority,
7573 mHasControl,
7574 !locked,
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007575 mCblk ? mCblk->clientIndex : 0,
7576 mCblk ? mCblk->serverIndex : 0
Eric Laurent65b65452010-06-01 23:49:17 -07007577 );
7578
7579 if (locked) {
7580 mCblk->lock.unlock();
7581 }
7582}
7583
7584#undef LOG_TAG
7585#define LOG_TAG "AudioFlinger::EffectChain"
7586
Glenn Kasten685c9ce2012-01-20 13:32:16 -08007587AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Eric Laurent65b65452010-06-01 23:49:17 -07007588 int sessionId)
Glenn Kasten685c9ce2012-01-20 13:32:16 -08007589 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Eric Laurent90681d62011-05-09 12:09:06 -07007590 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
7591 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurent65b65452010-06-01 23:49:17 -07007592{
Dima Zavin24fc2fb2011-04-19 22:30:36 -07007593 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Glenn Kasten685c9ce2012-01-20 13:32:16 -08007594 if (thread == NULL) {
Eric Laurentf9c361d2011-11-11 15:42:52 -08007595 return;
7596 }
7597 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
7598 thread->frameCount();
Eric Laurent65b65452010-06-01 23:49:17 -07007599}
7600
7601AudioFlinger::EffectChain::~EffectChain()
7602{
7603 if (mOwnInBuffer) {
7604 delete mInBuffer;
7605 }
7606
7607}
7608
Eric Laurentf82fccd2011-07-27 19:49:51 -07007609// getEffectFromDesc_l() must be called with ThreadBase::mLock held
Eric Laurent76c40f72010-07-15 12:50:15 -07007610sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor)
Eric Laurent65b65452010-06-01 23:49:17 -07007611{
Eric Laurent65b65452010-06-01 23:49:17 -07007612 size_t size = mEffects.size();
7613
7614 for (size_t i = 0; i < size; i++) {
7615 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kastenc64a6f02012-01-30 13:00:02 -08007616 return mEffects[i];
Eric Laurent65b65452010-06-01 23:49:17 -07007617 }
7618 }
Glenn Kastenc64a6f02012-01-30 13:00:02 -08007619 return 0;
Eric Laurent65b65452010-06-01 23:49:17 -07007620}
7621
Eric Laurentf82fccd2011-07-27 19:49:51 -07007622// getEffectFromId_l() must be called with ThreadBase::mLock held
Eric Laurent76c40f72010-07-15 12:50:15 -07007623sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
Eric Laurent65b65452010-06-01 23:49:17 -07007624{
Eric Laurent65b65452010-06-01 23:49:17 -07007625 size_t size = mEffects.size();
7626
7627 for (size_t i = 0; i < size; i++) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07007628 // by convention, return first effect if id provided is 0 (0 is never a valid id)
7629 if (id == 0 || mEffects[i]->id() == id) {
Glenn Kastenc64a6f02012-01-30 13:00:02 -08007630 return mEffects[i];
Eric Laurent65b65452010-06-01 23:49:17 -07007631 }
7632 }
Glenn Kastenc64a6f02012-01-30 13:00:02 -08007633 return 0;
Eric Laurent65b65452010-06-01 23:49:17 -07007634}
7635
Eric Laurentf82fccd2011-07-27 19:49:51 -07007636// getEffectFromType_l() must be called with ThreadBase::mLock held
7637sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
7638 const effect_uuid_t *type)
7639{
Eric Laurentf82fccd2011-07-27 19:49:51 -07007640 size_t size = mEffects.size();
7641
7642 for (size_t i = 0; i < size; i++) {
7643 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
Glenn Kastenc64a6f02012-01-30 13:00:02 -08007644 return mEffects[i];
Eric Laurentf82fccd2011-07-27 19:49:51 -07007645 }
7646 }
Glenn Kastenc64a6f02012-01-30 13:00:02 -08007647 return 0;
Eric Laurentf82fccd2011-07-27 19:49:51 -07007648}
7649
Eric Laurent65b65452010-06-01 23:49:17 -07007650// Must be called with EffectChain::mLock locked
7651void AudioFlinger::EffectChain::process_l()
7652{
Eric Laurent4fd3ecc2010-09-28 14:09:57 -07007653 sp<ThreadBase> thread = mThread.promote();
7654 if (thread == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00007655 ALOGW("process_l(): cannot promote mixer thread");
Eric Laurent4fd3ecc2010-09-28 14:09:57 -07007656 return;
7657 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -07007658 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
7659 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Eric Laurentf9c361d2011-11-11 15:42:52 -08007660 // always process effects unless no more tracks are on the session and the effect tail
7661 // has been rendered
7662 bool doProcess = true;
Eric Laurent4fd3ecc2010-09-28 14:09:57 -07007663 if (!isGlobalSession) {
Eric Laurentf9c361d2011-11-11 15:42:52 -08007664 bool tracksOnSession = (trackCnt() != 0);
Eric Laurent90681d62011-05-09 12:09:06 -07007665
Eric Laurentf9c361d2011-11-11 15:42:52 -08007666 if (!tracksOnSession && mTailBufferCount == 0) {
7667 doProcess = false;
7668 }
7669
7670 if (activeTrackCnt() == 0) {
7671 // if no track is active and the effect tail has not been rendered,
7672 // the input buffer must be cleared here as the mixer process will not do it
7673 if (tracksOnSession || mTailBufferCount > 0) {
7674 size_t numSamples = thread->frameCount() * thread->channelCount();
7675 memset(mInBuffer, 0, numSamples * sizeof(int16_t));
7676 if (mTailBufferCount > 0) {
7677 mTailBufferCount--;
7678 }
7679 }
7680 }
Eric Laurent4fd3ecc2010-09-28 14:09:57 -07007681 }
7682
Eric Laurent65b65452010-06-01 23:49:17 -07007683 size_t size = mEffects.size();
Eric Laurentf9c361d2011-11-11 15:42:52 -08007684 if (doProcess) {
Eric Laurent4fd3ecc2010-09-28 14:09:57 -07007685 for (size_t i = 0; i < size; i++) {
7686 mEffects[i]->process();
7687 }
Eric Laurent65b65452010-06-01 23:49:17 -07007688 }
Eric Laurent7d850f22010-07-09 13:34:17 -07007689 for (size_t i = 0; i < size; i++) {
7690 mEffects[i]->updateState();
7691 }
Eric Laurent65b65452010-06-01 23:49:17 -07007692}
7693
Eric Laurent76c40f72010-07-15 12:50:15 -07007694// addEffect_l() must be called with PlaybackThread::mLock held
Eric Laurent8ed6ed02010-07-13 04:45:46 -07007695status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
Eric Laurent65b65452010-06-01 23:49:17 -07007696{
7697 effect_descriptor_t desc = effect->desc();
7698 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
7699
7700 Mutex::Autolock _l(mLock);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07007701 effect->setChain(this);
7702 sp<ThreadBase> thread = mThread.promote();
7703 if (thread == 0) {
7704 return NO_INIT;
7705 }
7706 effect->setThread(thread);
Eric Laurent65b65452010-06-01 23:49:17 -07007707
7708 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
7709 // Auxiliary effects are inserted at the beginning of mEffects vector as
7710 // they are processed first and accumulated in chain input buffer
7711 mEffects.insertAt(effect, 0);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07007712
Eric Laurent65b65452010-06-01 23:49:17 -07007713 // the input buffer for auxiliary effect contains mono samples in
7714 // 32 bit format. This is to avoid saturation in AudoMixer
7715 // accumulation stage. Saturation is done in EffectModule::process() before
7716 // calling the process in effect engine
7717 size_t numSamples = thread->frameCount();
7718 int32_t *buffer = new int32_t[numSamples];
7719 memset(buffer, 0, numSamples * sizeof(int32_t));
7720 effect->setInBuffer((int16_t *)buffer);
7721 // auxiliary effects output samples to chain input buffer for further processing
7722 // by insert effects
7723 effect->setOutBuffer(mInBuffer);
7724 } else {
7725 // Insert effects are inserted at the end of mEffects vector as they are processed
7726 // after track and auxiliary effects.
Eric Laurent53334cd2010-06-23 17:38:20 -07007727 // Insert effect order as a function of indicated preference:
7728 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
7729 // another effect is present
7730 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
7731 // last effect claiming first position
7732 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
7733 // first effect claiming last position
Eric Laurent65b65452010-06-01 23:49:17 -07007734 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
Eric Laurent53334cd2010-06-23 17:38:20 -07007735 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
7736 // already present
Eric Laurent65b65452010-06-01 23:49:17 -07007737
Glenn Kasten150d2382012-02-08 14:04:28 -08007738 size_t size = mEffects.size();
7739 size_t idx_insert = size;
7740 ssize_t idx_insert_first = -1;
7741 ssize_t idx_insert_last = -1;
Eric Laurent65b65452010-06-01 23:49:17 -07007742
Glenn Kasten150d2382012-02-08 14:04:28 -08007743 for (size_t i = 0; i < size; i++) {
Eric Laurent65b65452010-06-01 23:49:17 -07007744 effect_descriptor_t d = mEffects[i]->desc();
7745 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
7746 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
7747 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
7748 // check invalid effect chaining combinations
7749 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
Eric Laurent53334cd2010-06-23 17:38:20 -07007750 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
Steve Block8564c8d2012-01-05 23:22:43 +00007751 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name);
Eric Laurent65b65452010-06-01 23:49:17 -07007752 return INVALID_OPERATION;
7753 }
Eric Laurent53334cd2010-06-23 17:38:20 -07007754 // remember position of first insert effect and by default
7755 // select this as insert position for new effect
Eric Laurent65b65452010-06-01 23:49:17 -07007756 if (idx_insert == size) {
7757 idx_insert = i;
7758 }
Eric Laurent53334cd2010-06-23 17:38:20 -07007759 // remember position of last insert effect claiming
7760 // first position
Eric Laurent65b65452010-06-01 23:49:17 -07007761 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
7762 idx_insert_first = i;
7763 }
Eric Laurent53334cd2010-06-23 17:38:20 -07007764 // remember position of first insert effect claiming
7765 // last position
7766 if (iPref == EFFECT_FLAG_INSERT_LAST &&
7767 idx_insert_last == -1) {
Eric Laurent65b65452010-06-01 23:49:17 -07007768 idx_insert_last = i;
7769 }
7770 }
7771 }
7772
Eric Laurent53334cd2010-06-23 17:38:20 -07007773 // modify idx_insert from first position if needed
7774 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
7775 if (idx_insert_last != -1) {
7776 idx_insert = idx_insert_last;
7777 } else {
7778 idx_insert = size;
7779 }
7780 } else {
7781 if (idx_insert_first != -1) {
7782 idx_insert = idx_insert_first + 1;
7783 }
Eric Laurent65b65452010-06-01 23:49:17 -07007784 }
7785
7786 // always read samples from chain input buffer
7787 effect->setInBuffer(mInBuffer);
7788
7789 // if last effect in the chain, output samples to chain
7790 // output buffer, otherwise to chain input buffer
7791 if (idx_insert == size) {
7792 if (idx_insert != 0) {
7793 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
7794 mEffects[idx_insert-1]->configure();
7795 }
7796 effect->setOutBuffer(mOutBuffer);
7797 } else {
7798 effect->setOutBuffer(mInBuffer);
7799 }
Eric Laurent53334cd2010-06-23 17:38:20 -07007800 mEffects.insertAt(effect, idx_insert);
Eric Laurent65b65452010-06-01 23:49:17 -07007801
Steve Block71f2cf12011-10-20 11:56:00 +01007802 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert);
Eric Laurent65b65452010-06-01 23:49:17 -07007803 }
7804 effect->configure();
7805 return NO_ERROR;
7806}
7807
Eric Laurent76c40f72010-07-15 12:50:15 -07007808// removeEffect_l() must be called with PlaybackThread::mLock held
7809size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
Eric Laurent65b65452010-06-01 23:49:17 -07007810{
7811 Mutex::Autolock _l(mLock);
Glenn Kasten150d2382012-02-08 14:04:28 -08007812 size_t size = mEffects.size();
Eric Laurent65b65452010-06-01 23:49:17 -07007813 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
7814
Glenn Kasten150d2382012-02-08 14:04:28 -08007815 for (size_t i = 0; i < size; i++) {
Eric Laurent65b65452010-06-01 23:49:17 -07007816 if (effect == mEffects[i]) {
Eric Laurent21b5c472011-07-26 20:54:46 -07007817 // calling stop here will remove pre-processing effect from the audio HAL.
7818 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
7819 // the middle of a read from audio HAL
Eric Laurent6fccbd02011-10-05 17:42:25 -07007820 if (mEffects[i]->state() == EffectModule::ACTIVE ||
7821 mEffects[i]->state() == EffectModule::STOPPING) {
7822 mEffects[i]->stop();
7823 }
Eric Laurent65b65452010-06-01 23:49:17 -07007824 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
7825 delete[] effect->inBuffer();
7826 } else {
7827 if (i == size - 1 && i != 0) {
7828 mEffects[i - 1]->setOutBuffer(mOutBuffer);
7829 mEffects[i - 1]->configure();
7830 }
7831 }
7832 mEffects.removeAt(i);
Steve Block71f2cf12011-10-20 11:56:00 +01007833 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i);
Eric Laurent65b65452010-06-01 23:49:17 -07007834 break;
7835 }
7836 }
Eric Laurent65b65452010-06-01 23:49:17 -07007837
7838 return mEffects.size();
7839}
7840
Eric Laurent76c40f72010-07-15 12:50:15 -07007841// setDevice_l() must be called with PlaybackThread::mLock held
7842void AudioFlinger::EffectChain::setDevice_l(uint32_t device)
Eric Laurent65b65452010-06-01 23:49:17 -07007843{
7844 size_t size = mEffects.size();
7845 for (size_t i = 0; i < size; i++) {
7846 mEffects[i]->setDevice(device);
7847 }
7848}
7849
Eric Laurent76c40f72010-07-15 12:50:15 -07007850// setMode_l() must be called with PlaybackThread::mLock held
Glenn Kastenaccb1142012-01-04 11:00:47 -08007851void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
Eric Laurent53334cd2010-06-23 17:38:20 -07007852{
7853 size_t size = mEffects.size();
7854 for (size_t i = 0; i < size; i++) {
7855 mEffects[i]->setMode(mode);
7856 }
7857}
7858
Eric Laurent76c40f72010-07-15 12:50:15 -07007859// setVolume_l() must be called with PlaybackThread::mLock held
7860bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
Eric Laurent65b65452010-06-01 23:49:17 -07007861{
7862 uint32_t newLeft = *left;
7863 uint32_t newRight = *right;
7864 bool hasControl = false;
Eric Laurent76c40f72010-07-15 12:50:15 -07007865 int ctrlIdx = -1;
7866 size_t size = mEffects.size();
Eric Laurent65b65452010-06-01 23:49:17 -07007867
Eric Laurent76c40f72010-07-15 12:50:15 -07007868 // first update volume controller
7869 for (size_t i = size; i > 0; i--) {
Eric Laurenta92ebfa2010-08-31 13:50:07 -07007870 if (mEffects[i - 1]->isProcessEnabled() &&
Eric Laurent76c40f72010-07-15 12:50:15 -07007871 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
7872 ctrlIdx = i - 1;
Eric Laurent0d7e0482010-07-19 06:24:46 -07007873 hasControl = true;
Eric Laurent76c40f72010-07-15 12:50:15 -07007874 break;
7875 }
7876 }
7877
7878 if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) {
Eric Laurent0d7e0482010-07-19 06:24:46 -07007879 if (hasControl) {
7880 *left = mNewLeftVolume;
7881 *right = mNewRightVolume;
7882 }
7883 return hasControl;
Eric Laurent76c40f72010-07-15 12:50:15 -07007884 }
7885
7886 mVolumeCtrlIdx = ctrlIdx;
Eric Laurent0d7e0482010-07-19 06:24:46 -07007887 mLeftVolume = newLeft;
7888 mRightVolume = newRight;
Eric Laurent76c40f72010-07-15 12:50:15 -07007889
7890 // second get volume update from volume controller
7891 if (ctrlIdx >= 0) {
7892 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
Eric Laurent0d7e0482010-07-19 06:24:46 -07007893 mNewLeftVolume = newLeft;
7894 mNewRightVolume = newRight;
Eric Laurent65b65452010-06-01 23:49:17 -07007895 }
7896 // then indicate volume to all other effects in chain.
7897 // Pass altered volume to effects before volume controller
7898 // and requested volume to effects after controller
7899 uint32_t lVol = newLeft;
7900 uint32_t rVol = newRight;
Eric Laurent76c40f72010-07-15 12:50:15 -07007901
Eric Laurent65b65452010-06-01 23:49:17 -07007902 for (size_t i = 0; i < size; i++) {
Eric Laurent76c40f72010-07-15 12:50:15 -07007903 if ((int)i == ctrlIdx) continue;
7904 // this also works for ctrlIdx == -1 when there is no volume controller
7905 if ((int)i > ctrlIdx) {
Eric Laurent65b65452010-06-01 23:49:17 -07007906 lVol = *left;
7907 rVol = *right;
7908 }
7909 mEffects[i]->setVolume(&lVol, &rVol, false);
7910 }
7911 *left = newLeft;
7912 *right = newRight;
7913
7914 return hasControl;
7915}
7916
Eric Laurent65b65452010-06-01 23:49:17 -07007917status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
7918{
7919 const size_t SIZE = 256;
7920 char buffer[SIZE];
7921 String8 result;
7922
7923 snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId);
7924 result.append(buffer);
7925
7926 bool locked = tryLock(mLock);
7927 // failed to lock - AudioFlinger is probably deadlocked
7928 if (!locked) {
7929 result.append("\tCould not lock mutex:\n");
7930 }
7931
Eric Laurent76c40f72010-07-15 12:50:15 -07007932 result.append("\tNum fx In buffer Out buffer Active tracks:\n");
7933 snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n",
Eric Laurent65b65452010-06-01 23:49:17 -07007934 mEffects.size(),
7935 (uint32_t)mInBuffer,
7936 (uint32_t)mOutBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -07007937 mActiveTrackCnt);
7938 result.append(buffer);
7939 write(fd, result.string(), result.size());
7940
7941 for (size_t i = 0; i < mEffects.size(); ++i) {
7942 sp<EffectModule> effect = mEffects[i];
7943 if (effect != 0) {
7944 effect->dump(fd, args);
7945 }
7946 }
7947
7948 if (locked) {
7949 mLock.unlock();
7950 }
7951
7952 return NO_ERROR;
7953}
7954
Eric Laurentf82fccd2011-07-27 19:49:51 -07007955// must be called with ThreadBase::mLock held
7956void AudioFlinger::EffectChain::setEffectSuspended_l(
7957 const effect_uuid_t *type, bool suspend)
7958{
7959 sp<SuspendedEffectDesc> desc;
7960 // use effect type UUID timelow as key as there is no real risk of identical
7961 // timeLow fields among effect type UUIDs.
Glenn Kasten150d2382012-02-08 14:04:28 -08007962 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
Eric Laurentf82fccd2011-07-27 19:49:51 -07007963 if (suspend) {
7964 if (index >= 0) {
7965 desc = mSuspendedEffects.valueAt(index);
7966 } else {
7967 desc = new SuspendedEffectDesc();
7968 memcpy(&desc->mType, type, sizeof(effect_uuid_t));
7969 mSuspendedEffects.add(type->timeLow, desc);
Steve Block71f2cf12011-10-20 11:56:00 +01007970 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
Eric Laurentf82fccd2011-07-27 19:49:51 -07007971 }
7972 if (desc->mRefCount++ == 0) {
7973 sp<EffectModule> effect = getEffectIfEnabled(type);
7974 if (effect != 0) {
7975 desc->mEffect = effect;
7976 effect->setSuspended(true);
7977 effect->setEnabled(false);
7978 }
7979 }
7980 } else {
7981 if (index < 0) {
7982 return;
7983 }
7984 desc = mSuspendedEffects.valueAt(index);
7985 if (desc->mRefCount <= 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00007986 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentf82fccd2011-07-27 19:49:51 -07007987 desc->mRefCount = 1;
7988 }
7989 if (--desc->mRefCount == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01007990 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
Eric Laurentf82fccd2011-07-27 19:49:51 -07007991 if (desc->mEffect != 0) {
7992 sp<EffectModule> effect = desc->mEffect.promote();
7993 if (effect != 0) {
7994 effect->setSuspended(false);
7995 sp<EffectHandle> handle = effect->controlHandle();
7996 if (handle != 0) {
7997 effect->setEnabled(handle->enabled());
7998 }
7999 }
8000 desc->mEffect.clear();
8001 }
8002 mSuspendedEffects.removeItemsAt(index);
8003 }
8004 }
8005}
8006
8007// must be called with ThreadBase::mLock held
8008void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
8009{
8010 sp<SuspendedEffectDesc> desc;
8011
Glenn Kasten150d2382012-02-08 14:04:28 -08008012 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008013 if (suspend) {
8014 if (index >= 0) {
8015 desc = mSuspendedEffects.valueAt(index);
8016 } else {
8017 desc = new SuspendedEffectDesc();
8018 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
Steve Block71f2cf12011-10-20 11:56:00 +01008019 ALOGV("setEffectSuspendedAll_l() add entry for 0");
Eric Laurentf82fccd2011-07-27 19:49:51 -07008020 }
8021 if (desc->mRefCount++ == 0) {
Glenn Kasten97040262012-01-30 12:56:03 -08008022 Vector< sp<EffectModule> > effects;
8023 getSuspendEligibleEffects(effects);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008024 for (size_t i = 0; i < effects.size(); i++) {
8025 setEffectSuspended_l(&effects[i]->desc().type, true);
8026 }
8027 }
8028 } else {
8029 if (index < 0) {
8030 return;
8031 }
8032 desc = mSuspendedEffects.valueAt(index);
8033 if (desc->mRefCount <= 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00008034 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008035 desc->mRefCount = 1;
8036 }
8037 if (--desc->mRefCount == 0) {
8038 Vector<const effect_uuid_t *> types;
8039 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
8040 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
8041 continue;
8042 }
8043 types.add(&mSuspendedEffects.valueAt(i)->mType);
8044 }
8045 for (size_t i = 0; i < types.size(); i++) {
8046 setEffectSuspended_l(types[i], false);
8047 }
Steve Block71f2cf12011-10-20 11:56:00 +01008048 ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
Eric Laurentf82fccd2011-07-27 19:49:51 -07008049 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
8050 }
8051 }
8052}
8053
Eric Laurent5e7acae2011-09-23 08:40:41 -07008054
8055// The volume effect is used for automated tests only
8056#ifndef OPENSL_ES_H_
8057static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
8058 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
8059const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
8060#endif //OPENSL_ES_H_
8061
Eric Laurent6752ec82011-08-10 10:37:50 -07008062bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
8063{
8064 // auxiliary effects and visualizer are never suspended on output mix
8065 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
8066 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
Eric Laurent5e7acae2011-09-23 08:40:41 -07008067 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
8068 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
Eric Laurent6752ec82011-08-10 10:37:50 -07008069 return false;
8070 }
8071 return true;
8072}
8073
Glenn Kasten97040262012-01-30 12:56:03 -08008074void AudioFlinger::EffectChain::getSuspendEligibleEffects(Vector< sp<AudioFlinger::EffectModule> > &effects)
Eric Laurentf82fccd2011-07-27 19:49:51 -07008075{
Glenn Kasten97040262012-01-30 12:56:03 -08008076 effects.clear();
Eric Laurentf82fccd2011-07-27 19:49:51 -07008077 for (size_t i = 0; i < mEffects.size(); i++) {
Glenn Kasten97040262012-01-30 12:56:03 -08008078 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
8079 effects.add(mEffects[i]);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008080 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07008081 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07008082}
8083
8084sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
8085 const effect_uuid_t *type)
8086{
Glenn Kastenc64a6f02012-01-30 13:00:02 -08008087 sp<EffectModule> effect = getEffectFromType_l(type);
8088 return effect != 0 && effect->isEnabled() ? effect : 0;
Eric Laurentf82fccd2011-07-27 19:49:51 -07008089}
8090
8091void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
8092 bool enabled)
8093{
Glenn Kasten150d2382012-02-08 14:04:28 -08008094 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008095 if (enabled) {
8096 if (index < 0) {
8097 // if the effect is not suspend check if all effects are suspended
8098 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
8099 if (index < 0) {
8100 return;
8101 }
Eric Laurent6752ec82011-08-10 10:37:50 -07008102 if (!isEffectEligibleForSuspend(effect->desc())) {
8103 return;
8104 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07008105 setEffectSuspended_l(&effect->desc().type, enabled);
8106 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
Eric Laurent6752ec82011-08-10 10:37:50 -07008107 if (index < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00008108 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
Eric Laurent6752ec82011-08-10 10:37:50 -07008109 return;
8110 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07008111 }
Steve Block71f2cf12011-10-20 11:56:00 +01008112 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
Glenn Kasten18db49a2012-03-12 16:29:55 -07008113 effect->desc().type.timeLow);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008114 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
8115 // if effect is requested to suspended but was not yet enabled, supend it now.
8116 if (desc->mEffect == 0) {
8117 desc->mEffect = effect;
8118 effect->setEnabled(false);
8119 effect->setSuspended(true);
8120 }
8121 } else {
8122 if (index < 0) {
8123 return;
8124 }
Steve Block71f2cf12011-10-20 11:56:00 +01008125 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
Glenn Kasten18db49a2012-03-12 16:29:55 -07008126 effect->desc().type.timeLow);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008127 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
8128 desc->mEffect.clear();
8129 effect->setSuspended(false);
8130 }
8131}
8132
Eric Laurent65b65452010-06-01 23:49:17 -07008133#undef LOG_TAG
8134#define LOG_TAG "AudioFlinger"
8135
Eric Laurenta553c252009-07-17 12:17:14 -07008136// ----------------------------------------------------------------------------
8137
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07008138status_t AudioFlinger::onTransact(
8139 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
8140{
8141 return BnAudioFlinger::onTransact(code, data, reply, flags);
8142}
8143
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07008144}; // namespace android