blob: e92e69cd2e68e41c55d5a104c465ae0f49e0c497 [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 Kasten35269822012-02-21 10:35:56 -0800445 // FIXME dead, remove from IAudioFlinger
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800446 uint32_t flags,
447 const sp<IMemory>& sharedBuffer,
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800448 audio_io_handle_t output,
John Grossmand8cf2962012-02-08 16:37:41 -0800449 bool isTimed,
Eric Laurent65b65452010-06-01 23:49:17 -0700450 int *sessionId,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800451 status_t *status)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700452{
Eric Laurenta553c252009-07-17 12:17:14 -0700453 sp<PlaybackThread::Track> track;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700454 sp<TrackHandle> trackHandle;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700455 sp<Client> client;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800456 status_t lStatus;
Eric Laurent65b65452010-06-01 23:49:17 -0700457 int lSessionId;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700458
Glenn Kasten6e987a42012-01-06 08:40:01 -0800459 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
460 // but if someone uses binder directly they could bypass that and cause us to crash
461 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block3762c312012-01-06 19:20:56 +0000462 ALOGE("createTrack() invalid stream type %d", streamType);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800463 lStatus = BAD_VALUE;
464 goto Exit;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700465 }
466
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800467 {
468 Mutex::Autolock _l(mLock);
Eric Laurenta553c252009-07-17 12:17:14 -0700469 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent493941b2010-07-28 01:32:47 -0700470 PlaybackThread *effectThread = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -0700471 if (thread == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000472 ALOGE("unknown output thread");
Eric Laurenta553c252009-07-17 12:17:14 -0700473 lStatus = BAD_VALUE;
474 goto Exit;
475 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800476
Glenn Kasten803a86a2012-01-25 14:28:29 -0800477 client = registerPid_l(pid);
Eric Laurent65b65452010-06-01 23:49:17 -0700478
Steve Block71f2cf12011-10-20 11:56:00 +0100479 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700480 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700481 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent493941b2010-07-28 01:32:47 -0700482 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
483 if (mPlaybackThreads.keyAt(i) != output) {
484 // prevent same audio session on different output threads
485 uint32_t sessions = t->hasAudioSession(*sessionId);
486 if (sessions & PlaybackThread::TRACK_SESSION) {
Steve Block3762c312012-01-06 19:20:56 +0000487 ALOGE("createTrack() session ID %d already in use", *sessionId);
Eric Laurent493941b2010-07-28 01:32:47 -0700488 lStatus = BAD_VALUE;
489 goto Exit;
490 }
491 // check if an effect with same session ID is waiting for a track to be created
492 if (sessions & PlaybackThread::EFFECT_SESSION) {
493 effectThread = t.get();
494 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700495 }
496 }
Eric Laurent65b65452010-06-01 23:49:17 -0700497 lSessionId = *sessionId;
498 } else {
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700499 // if no audio session id is provided, create one here
Eric Laurent464d5b32011-06-17 21:29:58 -0700500 lSessionId = nextUniqueId();
Eric Laurent65b65452010-06-01 23:49:17 -0700501 if (sessionId != NULL) {
502 *sessionId = lSessionId;
503 }
504 }
Steve Block71f2cf12011-10-20 11:56:00 +0100505 ALOGV("createTrack() lSessionId: %d", lSessionId);
Eric Laurent65b65452010-06-01 23:49:17 -0700506
Eric Laurenta553c252009-07-17 12:17:14 -0700507 track = thread->createTrack_l(client, streamType, sampleRate, format,
John Grossmand8cf2962012-02-08 16:37:41 -0800508 channelMask, frameCount, sharedBuffer, lSessionId, isTimed, &lStatus);
Eric Laurent493941b2010-07-28 01:32:47 -0700509
510 // move effect chain to this output thread if an effect on same session was waiting
511 // for a track to be created
512 if (lStatus == NO_ERROR && effectThread != NULL) {
513 Mutex::Autolock _dl(thread->mLock);
514 Mutex::Autolock _sl(effectThread->mLock);
515 moveEffectChain_l(lSessionId, effectThread, thread, true);
516 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700517 }
518 if (lStatus == NO_ERROR) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800519 trackHandle = new TrackHandle(track);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700520 } else {
Eric Laurentb9481d82009-09-17 05:12:56 -0700521 // remove local strong reference to Client before deleting the Track so that the Client
522 // destructor is called by the TrackBase destructor with mLock held
523 client.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700524 track.clear();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800525 }
526
527Exit:
Glenn Kasten18db49a2012-03-12 16:29:55 -0700528 if (status != NULL) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800529 *status = lStatus;
530 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700531 return trackHandle;
532}
533
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800534uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700535{
Eric Laurenta553c252009-07-17 12:17:14 -0700536 Mutex::Autolock _l(mLock);
537 PlaybackThread *thread = checkPlaybackThread_l(output);
538 if (thread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000539 ALOGW("sampleRate() unknown thread %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700540 return 0;
541 }
542 return thread->sampleRate();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700543}
544
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800545int AudioFlinger::channelCount(audio_io_handle_t output) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700546{
Eric Laurenta553c252009-07-17 12:17:14 -0700547 Mutex::Autolock _l(mLock);
548 PlaybackThread *thread = checkPlaybackThread_l(output);
549 if (thread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000550 ALOGW("channelCount() unknown thread %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700551 return 0;
552 }
553 return thread->channelCount();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700554}
555
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800556audio_format_t AudioFlinger::format(audio_io_handle_t output) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700557{
Eric Laurenta553c252009-07-17 12:17:14 -0700558 Mutex::Autolock _l(mLock);
559 PlaybackThread *thread = checkPlaybackThread_l(output);
560 if (thread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000561 ALOGW("format() unknown thread %d", output);
Glenn Kasten0a204ed2012-01-12 12:27:51 -0800562 return AUDIO_FORMAT_INVALID;
Eric Laurenta553c252009-07-17 12:17:14 -0700563 }
564 return thread->format();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700565}
566
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800567size_t AudioFlinger::frameCount(audio_io_handle_t output) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700568{
Eric Laurenta553c252009-07-17 12:17:14 -0700569 Mutex::Autolock _l(mLock);
570 PlaybackThread *thread = checkPlaybackThread_l(output);
571 if (thread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000572 ALOGW("frameCount() unknown thread %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700573 return 0;
574 }
575 return thread->frameCount();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700576}
577
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800578uint32_t AudioFlinger::latency(audio_io_handle_t output) const
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800579{
Eric Laurenta553c252009-07-17 12:17:14 -0700580 Mutex::Autolock _l(mLock);
581 PlaybackThread *thread = checkPlaybackThread_l(output);
582 if (thread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000583 ALOGW("latency() unknown thread %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700584 return 0;
585 }
586 return thread->latency();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800587}
588
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700589status_t AudioFlinger::setMasterVolume(float value)
590{
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700591 status_t ret = initCheck();
592 if (ret != NO_ERROR) {
593 return ret;
594 }
595
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700596 // check calling permissions
597 if (!settingsAllowed()) {
598 return PERMISSION_DENIED;
599 }
600
John Grossmand8cf2962012-02-08 16:37:41 -0800601 float swmv = value;
602
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700603 // when hw supports master volume, don't scale in sw mixer
John Grossmand8cf2962012-02-08 16:37:41 -0800604 if (MVS_NONE != mMasterVolumeSupportLvl) {
605 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
606 AutoMutex lock(mHardwareLock);
607 audio_hw_device_t *dev = mAudioHwDevs[i];
608
609 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
610 if (NULL != dev->set_master_volume) {
611 dev->set_master_volume(dev, value);
612 }
613 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent01635942011-01-18 18:39:02 -0800614 }
John Grossmand8cf2962012-02-08 16:37:41 -0800615
616 swmv = 1.0;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700617 }
Eric Laurenta553c252009-07-17 12:17:14 -0700618
Eric Laurent01635942011-01-18 18:39:02 -0800619 Mutex::Autolock _l(mLock);
John Grossmand8cf2962012-02-08 16:37:41 -0800620 mMasterVolume = value;
621 mMasterVolumeSW = swmv;
Glenn Kasten150d2382012-02-08 14:04:28 -0800622 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kasten18db49a2012-03-12 16:29:55 -0700623 mPlaybackThreads.valueAt(i)->setMasterVolume(swmv);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700624
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700625 return NO_ERROR;
626}
627
Glenn Kastenaccb1142012-01-04 11:00:47 -0800628status_t AudioFlinger::setMode(audio_mode_t mode)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700629{
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700630 status_t ret = initCheck();
631 if (ret != NO_ERROR) {
632 return ret;
633 }
Eric Laurent53334cd2010-06-23 17:38:20 -0700634
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700635 // check calling permissions
636 if (!settingsAllowed()) {
637 return PERMISSION_DENIED;
638 }
Glenn Kasten01aaf2c2012-01-06 16:47:31 -0800639 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block8564c8d2012-01-05 23:22:43 +0000640 ALOGW("Illegal value: setMode(%d)", mode);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700641 return BAD_VALUE;
642 }
643
Eric Laurent53334cd2010-06-23 17:38:20 -0700644 { // scope for the lock
645 AutoMutex lock(mHardwareLock);
646 mHardwareStatus = AUDIO_HW_SET_MODE;
Dima Zavin31f188892011-04-18 16:57:27 -0700647 ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode);
Eric Laurent53334cd2010-06-23 17:38:20 -0700648 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten871c16c2010-03-05 12:18:01 -0800649 }
Eric Laurent53334cd2010-06-23 17:38:20 -0700650
651 if (NO_ERROR == ret) {
652 Mutex::Autolock _l(mLock);
653 mMode = mode;
Glenn Kasten150d2382012-02-08 14:04:28 -0800654 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kasten18db49a2012-03-12 16:29:55 -0700655 mPlaybackThreads.valueAt(i)->setMode(mode);
Eric Laurent53334cd2010-06-23 17:38:20 -0700656 }
657
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700658 return ret;
659}
660
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700661status_t AudioFlinger::setMicMute(bool state)
662{
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700663 status_t ret = initCheck();
664 if (ret != NO_ERROR) {
665 return ret;
666 }
667
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700668 // check calling permissions
669 if (!settingsAllowed()) {
670 return PERMISSION_DENIED;
671 }
672
673 AutoMutex lock(mHardwareLock);
674 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700675 ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700676 mHardwareStatus = AUDIO_HW_IDLE;
677 return ret;
678}
679
680bool AudioFlinger::getMicMute() const
681{
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700682 status_t ret = initCheck();
683 if (ret != NO_ERROR) {
684 return false;
685 }
686
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700687 bool state = AUDIO_MODE_INVALID;
Glenn Kastenad8d1752012-02-02 14:05:20 -0800688 AutoMutex lock(mHardwareLock);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700689 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Dima Zavin31f188892011-04-18 16:57:27 -0700690 mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700691 mHardwareStatus = AUDIO_HW_IDLE;
692 return state;
693}
694
695status_t AudioFlinger::setMasterMute(bool muted)
696{
697 // check calling permissions
698 if (!settingsAllowed()) {
699 return PERMISSION_DENIED;
700 }
Eric Laurenta553c252009-07-17 12:17:14 -0700701
Eric Laurent01635942011-01-18 18:39:02 -0800702 Mutex::Autolock _l(mLock);
Glenn Kastenb3db2132012-01-19 08:59:58 -0800703 // This is an optimization, so PlaybackThread doesn't have to look at the one from AudioFlinger
Eric Laurenta553c252009-07-17 12:17:14 -0700704 mMasterMute = muted;
Glenn Kasten150d2382012-02-08 14:04:28 -0800705 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kasten18db49a2012-03-12 16:29:55 -0700706 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurenta553c252009-07-17 12:17:14 -0700707
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700708 return NO_ERROR;
709}
710
711float AudioFlinger::masterVolume() const
712{
Glenn Kastene6f8a422011-12-13 11:47:54 -0800713 Mutex::Autolock _l(mLock);
714 return masterVolume_l();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700715}
716
John Grossmand8cf2962012-02-08 16:37:41 -0800717float AudioFlinger::masterVolumeSW() const
718{
719 Mutex::Autolock _l(mLock);
720 return masterVolumeSW_l();
721}
722
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700723bool AudioFlinger::masterMute() const
724{
Glenn Kastene6f8a422011-12-13 11:47:54 -0800725 Mutex::Autolock _l(mLock);
726 return masterMute_l();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700727}
728
John Grossmand8cf2962012-02-08 16:37:41 -0800729float AudioFlinger::masterVolume_l() const
730{
731 if (MVS_FULL == mMasterVolumeSupportLvl) {
732 float ret_val;
733 AutoMutex lock(mHardwareLock);
734
735 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Glenn Kasten81916df2012-03-08 12:18:35 -0800736 ALOG_ASSERT((NULL != mPrimaryHardwareDev) &&
737 (NULL != mPrimaryHardwareDev->get_master_volume),
738 "can't get master volume");
John Grossmand8cf2962012-02-08 16:37:41 -0800739
740 mPrimaryHardwareDev->get_master_volume(mPrimaryHardwareDev, &ret_val);
741 mHardwareStatus = AUDIO_HW_IDLE;
742 return ret_val;
743 }
744
745 return mMasterVolume;
746}
747
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800748status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
749 audio_io_handle_t output)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700750{
751 // check calling permissions
752 if (!settingsAllowed()) {
753 return PERMISSION_DENIED;
754 }
755
Glenn Kasten6e987a42012-01-06 08:40:01 -0800756 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block3762c312012-01-06 19:20:56 +0000757 ALOGE("setStreamVolume() invalid stream %d", stream);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700758 return BAD_VALUE;
759 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800760
Eric Laurenta553c252009-07-17 12:17:14 -0700761 AutoMutex lock(mLock);
762 PlaybackThread *thread = NULL;
763 if (output) {
764 thread = checkPlaybackThread_l(output);
765 if (thread == NULL) {
766 return BAD_VALUE;
767 }
768 }
769
Eric Laurenta553c252009-07-17 12:17:14 -0700770 mStreamTypes[stream].volume = value;
771
772 if (thread == NULL) {
Glenn Kasten150d2382012-02-08 14:04:28 -0800773 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kasten18db49a2012-03-12 16:29:55 -0700774 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Eric Laurent415f3e22009-10-21 08:14:22 -0700775 }
Eric Laurenta553c252009-07-17 12:17:14 -0700776 } else {
777 thread->setStreamVolume(stream, value);
778 }
Eric Laurentef028272009-04-21 07:56:33 -0700779
Eric Laurent415f3e22009-10-21 08:14:22 -0700780 return NO_ERROR;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700781}
782
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800783status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700784{
785 // check calling permissions
786 if (!settingsAllowed()) {
787 return PERMISSION_DENIED;
788 }
789
Glenn Kasten6e987a42012-01-06 08:40:01 -0800790 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700791 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block3762c312012-01-06 19:20:56 +0000792 ALOGE("setStreamMute() invalid stream %d", stream);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700793 return BAD_VALUE;
794 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700795
Eric Laurent01635942011-01-18 18:39:02 -0800796 AutoMutex lock(mLock);
Eric Laurenta553c252009-07-17 12:17:14 -0700797 mStreamTypes[stream].mute = muted;
798 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kasten18db49a2012-03-12 16:29:55 -0700799 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700801 return NO_ERROR;
802}
803
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800804float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700805{
Glenn Kasten6e987a42012-01-06 08:40:01 -0800806 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700807 return 0.0f;
808 }
Eric Laurenta553c252009-07-17 12:17:14 -0700809
810 AutoMutex lock(mLock);
811 float volume;
812 if (output) {
813 PlaybackThread *thread = checkPlaybackThread_l(output);
814 if (thread == NULL) {
815 return 0.0f;
816 }
817 volume = thread->streamVolume(stream);
818 } else {
Glenn Kasten8b02b992012-01-09 09:40:36 -0800819 volume = streamVolume_l(stream);
Eric Laurenta553c252009-07-17 12:17:14 -0700820 }
821
Eric Laurentef028272009-04-21 07:56:33 -0700822 return volume;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700823}
824
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800825bool AudioFlinger::streamMute(audio_stream_type_t stream) const
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700826{
Glenn Kasten6e987a42012-01-06 08:40:01 -0800827 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700828 return true;
829 }
Eric Laurenta553c252009-07-17 12:17:14 -0700830
Glenn Kasten8b02b992012-01-09 09:40:36 -0800831 AutoMutex lock(mLock);
832 return streamMute_l(stream);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700833}
834
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800835status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700836{
Glenn Kasten7ed4f0c2012-02-03 11:10:00 -0800837 ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling pid %d",
Eric Laurenta553c252009-07-17 12:17:14 -0700838 ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid());
839 // check calling permissions
840 if (!settingsAllowed()) {
841 return PERMISSION_DENIED;
The Android Open Source Project9266c552009-01-15 16:12:10 -0800842 }
Eric Laurenta553c252009-07-17 12:17:14 -0700843
844 // ioHandle == 0 means the parameters are global to the audio hardware interface
845 if (ioHandle == 0) {
Dima Zavin31f188892011-04-18 16:57:27 -0700846 status_t final_result = NO_ERROR;
Glenn Kasten23c9c742012-02-02 14:16:03 -0800847 {
848 AutoMutex lock(mHardwareLock);
849 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
Dima Zavin31f188892011-04-18 16:57:27 -0700850 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
851 audio_hw_device_t *dev = mAudioHwDevs[i];
Glenn Kasten23c9c742012-02-02 14:16:03 -0800852 status_t result = dev->set_parameters(dev, keyValuePairs.string());
Dima Zavin31f188892011-04-18 16:57:27 -0700853 final_result = result ?: final_result;
854 }
Eric Laurenta553c252009-07-17 12:17:14 -0700855 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten23c9c742012-02-02 14:16:03 -0800856 }
Eric Laurent6639b552011-08-01 09:52:20 -0700857 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
858 AudioParameter param = AudioParameter(keyValuePairs);
859 String8 value;
860 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
861 Mutex::Autolock _l(mLock);
Eric Laurent2d95dfb2011-08-29 12:42:48 -0700862 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
863 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent6639b552011-08-01 09:52:20 -0700864 for (size_t i = 0; i < mRecordThreads.size(); i++) {
865 sp<RecordThread> thread = mRecordThreads.valueAt(i);
866 RecordThread::RecordTrack *track = thread->track();
867 if (track != NULL) {
868 audio_devices_t device = (audio_devices_t)(
869 thread->device() & AUDIO_DEVICE_IN_ALL);
Eric Laurent2d95dfb2011-08-29 12:42:48 -0700870 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
Eric Laurent6639b552011-08-01 09:52:20 -0700871 thread->setEffectSuspended(FX_IID_AEC,
872 suspend,
873 track->sessionId());
874 thread->setEffectSuspended(FX_IID_NS,
875 suspend,
876 track->sessionId());
877 }
878 }
Eric Laurent2d95dfb2011-08-29 12:42:48 -0700879 mBtNrecIsOff = btNrecIsOff;
Eric Laurent6639b552011-08-01 09:52:20 -0700880 }
881 }
Dima Zavin31f188892011-04-18 16:57:27 -0700882 return final_result;
Eric Laurenta553c252009-07-17 12:17:14 -0700883 }
884
Eric Laurentb7d94602009-09-29 11:12:57 -0700885 // hold a strong ref on thread in case closeOutput() or closeInput() is called
886 // and the thread is exited once the lock is released
887 sp<ThreadBase> thread;
888 {
889 Mutex::Autolock _l(mLock);
890 thread = checkPlaybackThread_l(ioHandle);
891 if (thread == NULL) {
892 thread = checkRecordThread_l(ioHandle);
Glenn Kasten70ed6b72012-01-10 10:46:34 -0800893 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent464d5b32011-06-17 21:29:58 -0700894 // indicate output device change to all input threads for pre processing
895 AudioParameter param = AudioParameter(keyValuePairs);
896 int value;
Eric Laurent10c3fc82012-03-16 20:37:59 -0700897 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
898 (value != 0)) {
Eric Laurent464d5b32011-06-17 21:29:58 -0700899 for (size_t i = 0; i < mRecordThreads.size(); i++) {
900 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
901 }
902 }
Eric Laurentb7d94602009-09-29 11:12:57 -0700903 }
Eric Laurenta553c252009-07-17 12:17:14 -0700904 }
Glenn Kasten706b6182012-01-20 13:44:40 -0800905 if (thread != 0) {
906 return thread->setParameters(keyValuePairs);
Eric Laurenta553c252009-07-17 12:17:14 -0700907 }
Eric Laurenta553c252009-07-17 12:17:14 -0700908 return BAD_VALUE;
909}
910
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800911String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Eric Laurenta553c252009-07-17 12:17:14 -0700912{
Glenn Kasten7ed4f0c2012-02-03 11:10:00 -0800913// ALOGV("getParameters() io %d, keys %s, tid %d, calling pid %d",
Eric Laurenta553c252009-07-17 12:17:14 -0700914// ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid());
915
916 if (ioHandle == 0) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700917 String8 out_s8;
918
Dima Zavin31f188892011-04-18 16:57:27 -0700919 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten23c9c742012-02-02 14:16:03 -0800920 char *s;
921 {
922 AutoMutex lock(mHardwareLock);
923 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Dima Zavin31f188892011-04-18 16:57:27 -0700924 audio_hw_device_t *dev = mAudioHwDevs[i];
Glenn Kasten23c9c742012-02-02 14:16:03 -0800925 s = dev->get_parameters(dev, keys.string());
926 mHardwareStatus = AUDIO_HW_IDLE;
927 }
John Grossman4fbe95e2012-02-09 11:28:36 -0800928 out_s8 += String8(s ? s : "");
Dima Zavin31f188892011-04-18 16:57:27 -0700929 free(s);
930 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700931 return out_s8;
Eric Laurenta553c252009-07-17 12:17:14 -0700932 }
Eric Laurentb7d94602009-09-29 11:12:57 -0700933
934 Mutex::Autolock _l(mLock);
935
Eric Laurenta553c252009-07-17 12:17:14 -0700936 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
937 if (playbackThread != NULL) {
938 return playbackThread->getParameters(keys);
939 }
940 RecordThread *recordThread = checkRecordThread_l(ioHandle);
941 if (recordThread != NULL) {
942 return recordThread->getParameters(keys);
943 }
944 return String8("");
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700945}
946
Glenn Kasten3f6d83a2012-01-26 16:25:10 -0800947size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, int channelCount) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948{
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700949 status_t ret = initCheck();
950 if (ret != NO_ERROR) {
951 return 0;
952 }
953
Glenn Kastenad8d1752012-02-02 14:05:20 -0800954 AutoMutex lock(mHardwareLock);
955 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
956 size_t size = mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, sampleRate, format, channelCount);
957 mHardwareStatus = AUDIO_HW_IDLE;
958 return size;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959}
960
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800961unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Eric Laurent47d0a922010-02-26 02:47:27 -0800962{
963 if (ioHandle == 0) {
964 return 0;
965 }
966
967 Mutex::Autolock _l(mLock);
968
969 RecordThread *recordThread = checkRecordThread_l(ioHandle);
970 if (recordThread != NULL) {
971 return recordThread->getInputFramesLost();
972 }
973 return 0;
974}
975
Eric Laurent415f3e22009-10-21 08:14:22 -0700976status_t AudioFlinger::setVoiceVolume(float value)
977{
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700978 status_t ret = initCheck();
979 if (ret != NO_ERROR) {
980 return ret;
981 }
982
Eric Laurent415f3e22009-10-21 08:14:22 -0700983 // check calling permissions
984 if (!settingsAllowed()) {
985 return PERMISSION_DENIED;
986 }
987
988 AutoMutex lock(mHardwareLock);
Glenn Kasten23c9c742012-02-02 14:16:03 -0800989 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Eric Laurent8b4dbf72011-08-23 08:25:03 -0700990 ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value);
Eric Laurent415f3e22009-10-21 08:14:22 -0700991 mHardwareStatus = AUDIO_HW_IDLE;
992
993 return ret;
994}
995
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800996status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
997 audio_io_handle_t output) const
Eric Laurent0986e792010-01-19 17:37:09 -0800998{
999 status_t status;
1000
1001 Mutex::Autolock _l(mLock);
1002
1003 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1004 if (playbackThread != NULL) {
1005 return playbackThread->getRenderPosition(halFrames, dspFrames);
1006 }
1007
1008 return BAD_VALUE;
1009}
1010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1012{
Eric Laurenta553c252009-07-17 12:17:14 -07001013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 Mutex::Autolock _l(mLock);
1015
Glenn Kastend153b1f2012-02-03 11:10:26 -08001016 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001017 if (mNotificationClients.indexOfKey(pid) < 0) {
1018 sp<NotificationClient> notificationClient = new NotificationClient(this,
1019 client,
1020 pid);
Steve Block71f2cf12011-10-20 11:56:00 +01001021 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Eric Laurenta553c252009-07-17 12:17:14 -07001022
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001023 mNotificationClients.add(pid, notificationClient);
Eric Laurenta553c252009-07-17 12:17:14 -07001024
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001025 sp<IBinder> binder = client->asBinder();
1026 binder->linkToDeath(notificationClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001028 // the config change is always sent from playback or record threads to avoid deadlock
1029 // with AudioSystem::gLock
1030 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1031 mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED);
1032 }
Eric Laurenta553c252009-07-17 12:17:14 -07001033
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001034 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1035 mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 }
1037 }
1038}
1039
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001040void AudioFlinger::removeNotificationClient(pid_t pid)
1041{
1042 Mutex::Autolock _l(mLock);
1043
Glenn Kasten538529b2012-01-20 09:19:01 -08001044 mNotificationClients.removeItem(pid);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07001045
Steve Block71f2cf12011-10-20 11:56:00 +01001046 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten150d2382012-02-08 14:04:28 -08001047 size_t num = mAudioSessionRefs.size();
Marco Nelissenc74b93f2011-08-02 13:33:41 -07001048 bool removed = false;
Glenn Kasten150d2382012-02-08 14:04:28 -08001049 for (size_t i = 0; i< num; ) {
Marco Nelissenc74b93f2011-08-02 13:33:41 -07001050 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten9a42ac92012-03-06 11:22:01 -08001051 ALOGV(" pid %d @ %d", ref->mPid, i);
1052 if (ref->mPid == pid) {
1053 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07001054 mAudioSessionRefs.removeAt(i);
1055 delete ref;
1056 removed = true;
Marco Nelissenc74b93f2011-08-02 13:33:41 -07001057 num--;
Glenn Kasten150d2382012-02-08 14:04:28 -08001058 } else {
1059 i++;
Marco Nelissenc74b93f2011-08-02 13:33:41 -07001060 }
1061 }
1062 if (removed) {
1063 purgeStaleEffects_l();
1064 }
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001065}
1066
Eric Laurent296a0ec2009-09-15 07:10:12 -07001067// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenffed04a2012-03-01 09:14:51 -08001068void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001069{
Eric Laurent49f02be2009-11-19 09:00:56 -08001070 size_t size = mNotificationClients.size();
1071 for (size_t i = 0; i < size; i++) {
Glenn Kastendc3ac852012-01-25 15:28:08 -08001072 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1073 param2);
Eric Laurenta553c252009-07-17 12:17:14 -07001074 }
1075}
1076
Eric Laurentb9481d82009-09-17 05:12:56 -07001077// removeClient_l() must be called with AudioFlinger::mLock held
1078void AudioFlinger::removeClient_l(pid_t pid)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001079{
Steve Block71f2cf12011-10-20 11:56:00 +01001080 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 -07001081 mClients.removeItem(pid);
1082}
1083
Eric Laurent4f0f17d2010-05-12 02:05:53 -07001084
Eric Laurenta553c252009-07-17 12:17:14 -07001085// ----------------------------------------------------------------------------
1086
Glenn Kasten39d00cb2012-01-17 11:09:42 -08001087AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
1088 uint32_t device, type_t type)
Eric Laurenta553c252009-07-17 12:17:14 -07001089 : Thread(false),
Glenn Kastenefd511a2012-01-26 10:38:26 -08001090 mType(type),
Glenn Kastendc3ac852012-01-25 15:28:08 -08001091 mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0),
1092 // mChannelMask
1093 mChannelCount(0),
1094 mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID),
1095 mParamStatus(NO_ERROR),
Glenn Kasten761286f2012-01-06 08:39:38 -08001096 mStandby(false), mId(id),
Glenn Kastendc3ac852012-01-25 15:28:08 -08001097 mDevice(device),
1098 mDeathRecipient(new PMDeathRecipient(this))
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001099{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100}
1101
Eric Laurenta553c252009-07-17 12:17:14 -07001102AudioFlinger::ThreadBase::~ThreadBase()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103{
Eric Laurent8fce46a2009-08-04 09:45:33 -07001104 mParamCond.broadcast();
Eric Laurent6dbdc402011-07-22 09:04:31 -07001105 // do not lock the mutex in destructor
1106 releaseWakeLock_l();
Eric Laurent4a64a6e2011-09-27 12:07:15 -07001107 if (mPowerManager != 0) {
1108 sp<IBinder> binder = mPowerManager->asBinder();
1109 binder->unlinkToDeath(mDeathRecipient);
1110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111}
1112
Eric Laurenta553c252009-07-17 12:17:14 -07001113void AudioFlinger::ThreadBase::exit()
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001114{
Steve Block71f2cf12011-10-20 11:56:00 +01001115 ALOGV("ThreadBase::exit");
Eric Laurenta553c252009-07-17 12:17:14 -07001116 {
Glenn Kasten761286f2012-01-06 08:39:38 -08001117 // This lock prevents the following race in thread (uniprocessor for illustration):
1118 // if (!exitPending()) {
1119 // // context switch from here to exit()
1120 // // exit() calls requestExit(), what exitPending() observes
1121 // // exit() calls signal(), which is dropped since no waiters
1122 // // context switch back from exit() to here
1123 // mWaitWorkCV.wait(...);
1124 // // now thread is hung
1125 // }
Glenn Kasten325ee1c2012-01-05 15:41:56 -08001126 AutoMutex lock(mLock);
Eric Laurenta553c252009-07-17 12:17:14 -07001127 requestExit();
1128 mWaitWorkCV.signal();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001129 }
Glenn Kasten761286f2012-01-06 08:39:38 -08001130 // When Thread::requestExitAndWait is made virtual and this method is renamed to
1131 // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();"
Eric Laurenta553c252009-07-17 12:17:14 -07001132 requestExitAndWait();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001133}
Eric Laurenta553c252009-07-17 12:17:14 -07001134
Eric Laurenta553c252009-07-17 12:17:14 -07001135status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs)
1136{
Eric Laurent8fce46a2009-08-04 09:45:33 -07001137 status_t status;
Eric Laurenta553c252009-07-17 12:17:14 -07001138
Steve Block71f2cf12011-10-20 11:56:00 +01001139 ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string());
Eric Laurenta553c252009-07-17 12:17:14 -07001140 Mutex::Autolock _l(mLock);
Eric Laurenta553c252009-07-17 12:17:14 -07001141
Eric Laurent8fce46a2009-08-04 09:45:33 -07001142 mNewParameters.add(keyValuePairs);
Eric Laurenta553c252009-07-17 12:17:14 -07001143 mWaitWorkCV.signal();
Eric Laurentb7d94602009-09-29 11:12:57 -07001144 // wait condition with timeout in case the thread loop has exited
1145 // before the request could be processed
Glenn Kastencbfe2e12011-12-13 11:04:14 -08001146 if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) {
Eric Laurentb7d94602009-09-29 11:12:57 -07001147 status = mParamStatus;
1148 mWaitWorkCV.signal();
1149 } else {
1150 status = TIMED_OUT;
1151 }
Eric Laurent8fce46a2009-08-04 09:45:33 -07001152 return status;
Eric Laurenta553c252009-07-17 12:17:14 -07001153}
1154
1155void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param)
1156{
1157 Mutex::Autolock _l(mLock);
Eric Laurent8fce46a2009-08-04 09:45:33 -07001158 sendConfigEvent_l(event, param);
1159}
1160
1161// sendConfigEvent_l() must be called with ThreadBase::mLock held
1162void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param)
1163{
Glenn Kasten4b220f02011-12-13 11:50:00 -08001164 ConfigEvent configEvent;
1165 configEvent.mEvent = event;
1166 configEvent.mParam = param;
Eric Laurenta553c252009-07-17 12:17:14 -07001167 mConfigEvents.add(configEvent);
Steve Block71f2cf12011-10-20 11:56:00 +01001168 ALOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param);
Eric Laurenta553c252009-07-17 12:17:14 -07001169 mWaitWorkCV.signal();
1170}
1171
1172void AudioFlinger::ThreadBase::processConfigEvents()
1173{
1174 mLock.lock();
Glenn Kasten18db49a2012-03-12 16:29:55 -07001175 while (!mConfigEvents.isEmpty()) {
Steve Block71f2cf12011-10-20 11:56:00 +01001176 ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size());
Glenn Kasten4b220f02011-12-13 11:50:00 -08001177 ConfigEvent configEvent = mConfigEvents[0];
Eric Laurenta553c252009-07-17 12:17:14 -07001178 mConfigEvents.removeAt(0);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001179 // release mLock before locking AudioFlinger mLock: lock order is always
1180 // AudioFlinger then ThreadBase to avoid cross deadlock
Eric Laurenta553c252009-07-17 12:17:14 -07001181 mLock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001182 mAudioFlinger->mLock.lock();
Glenn Kasten4b220f02011-12-13 11:50:00 -08001183 audioConfigChanged_l(configEvent.mEvent, configEvent.mParam);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001184 mAudioFlinger->mLock.unlock();
Eric Laurenta553c252009-07-17 12:17:14 -07001185 mLock.lock();
1186 }
1187 mLock.unlock();
1188}
1189
Eric Laurent3fdb1262009-11-07 00:01:32 -08001190status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args)
1191{
1192 const size_t SIZE = 256;
1193 char buffer[SIZE];
1194 String8 result;
1195
1196 bool locked = tryLock(mLock);
1197 if (!locked) {
1198 snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this);
1199 write(fd, buffer, strlen(buffer));
1200 }
1201
Eric Laurent0194a0d2012-03-14 15:03:26 -07001202 snprintf(buffer, SIZE, "io handle: %d\n", mId);
1203 result.append(buffer);
1204 snprintf(buffer, SIZE, "TID: %d\n", getTid());
1205 result.append(buffer);
Eric Laurent3fdb1262009-11-07 00:01:32 -08001206 snprintf(buffer, SIZE, "standby: %d\n", mStandby);
1207 result.append(buffer);
1208 snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate);
1209 result.append(buffer);
1210 snprintf(buffer, SIZE, "Frame count: %d\n", mFrameCount);
1211 result.append(buffer);
1212 snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount);
1213 result.append(buffer);
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001214 snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask);
1215 result.append(buffer);
Eric Laurent3fdb1262009-11-07 00:01:32 -08001216 snprintf(buffer, SIZE, "Format: %d\n", mFormat);
1217 result.append(buffer);
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001218 snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize);
Eric Laurent3fdb1262009-11-07 00:01:32 -08001219 result.append(buffer);
1220
1221 snprintf(buffer, SIZE, "\nPending setParameters commands: \n");
1222 result.append(buffer);
1223 result.append(" Index Command");
1224 for (size_t i = 0; i < mNewParameters.size(); ++i) {
1225 snprintf(buffer, SIZE, "\n %02d ", i);
1226 result.append(buffer);
1227 result.append(mNewParameters[i]);
1228 }
1229
1230 snprintf(buffer, SIZE, "\n\nPending config events: \n");
1231 result.append(buffer);
1232 snprintf(buffer, SIZE, " Index event param\n");
1233 result.append(buffer);
1234 for (size_t i = 0; i < mConfigEvents.size(); i++) {
Glenn Kasten4b220f02011-12-13 11:50:00 -08001235 snprintf(buffer, SIZE, " %02d %02d %d\n", i, mConfigEvents[i].mEvent, mConfigEvents[i].mParam);
Eric Laurent3fdb1262009-11-07 00:01:32 -08001236 result.append(buffer);
1237 }
1238 result.append("\n");
1239
1240 write(fd, result.string(), result.size());
1241
1242 if (locked) {
1243 mLock.unlock();
1244 }
1245 return NO_ERROR;
1246}
1247
Eric Laurent1345d332011-07-24 17:49:51 -07001248status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args)
1249{
1250 const size_t SIZE = 256;
1251 char buffer[SIZE];
1252 String8 result;
1253
1254 snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size());
1255 write(fd, buffer, strlen(buffer));
1256
1257 for (size_t i = 0; i < mEffectChains.size(); ++i) {
1258 sp<EffectChain> chain = mEffectChains[i];
1259 if (chain != 0) {
1260 chain->dump(fd, args);
1261 }
1262 }
1263 return NO_ERROR;
1264}
1265
Eric Laurent6dbdc402011-07-22 09:04:31 -07001266void AudioFlinger::ThreadBase::acquireWakeLock()
1267{
1268 Mutex::Autolock _l(mLock);
1269 acquireWakeLock_l();
1270}
1271
1272void AudioFlinger::ThreadBase::acquireWakeLock_l()
1273{
1274 if (mPowerManager == 0) {
1275 // use checkService() to avoid blocking if power service is not up yet
1276 sp<IBinder> binder =
1277 defaultServiceManager()->checkService(String16("power"));
1278 if (binder == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001279 ALOGW("Thread %s cannot connect to the power manager service", mName);
Eric Laurent6dbdc402011-07-22 09:04:31 -07001280 } else {
1281 mPowerManager = interface_cast<IPowerManager>(binder);
1282 binder->linkToDeath(mDeathRecipient);
1283 }
1284 }
1285 if (mPowerManager != 0) {
1286 sp<IBinder> binder = new BBinder();
1287 status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
1288 binder,
1289 String16(mName));
1290 if (status == NO_ERROR) {
1291 mWakeLockToken = binder;
1292 }
Steve Block71f2cf12011-10-20 11:56:00 +01001293 ALOGV("acquireWakeLock_l() %s status %d", mName, status);
Eric Laurent6dbdc402011-07-22 09:04:31 -07001294 }
1295}
1296
1297void AudioFlinger::ThreadBase::releaseWakeLock()
1298{
1299 Mutex::Autolock _l(mLock);
Eric Laurentd49ead62011-07-28 13:59:02 -07001300 releaseWakeLock_l();
Eric Laurent6dbdc402011-07-22 09:04:31 -07001301}
1302
1303void AudioFlinger::ThreadBase::releaseWakeLock_l()
1304{
1305 if (mWakeLockToken != 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001306 ALOGV("releaseWakeLock_l() %s", mName);
Eric Laurent6dbdc402011-07-22 09:04:31 -07001307 if (mPowerManager != 0) {
1308 mPowerManager->releaseWakeLock(mWakeLockToken, 0);
1309 }
1310 mWakeLockToken.clear();
1311 }
1312}
1313
1314void AudioFlinger::ThreadBase::clearPowerManager()
1315{
1316 Mutex::Autolock _l(mLock);
1317 releaseWakeLock_l();
1318 mPowerManager.clear();
1319}
1320
1321void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who)
1322{
1323 sp<ThreadBase> thread = mThread.promote();
1324 if (thread != 0) {
1325 thread->clearPowerManager();
1326 }
Steve Block8564c8d2012-01-05 23:22:43 +00001327 ALOGW("power manager service died !!!");
Eric Laurent6dbdc402011-07-22 09:04:31 -07001328}
Eric Laurent1345d332011-07-24 17:49:51 -07001329
Eric Laurentf82fccd2011-07-27 19:49:51 -07001330void AudioFlinger::ThreadBase::setEffectSuspended(
1331 const effect_uuid_t *type, bool suspend, int sessionId)
1332{
1333 Mutex::Autolock _l(mLock);
1334 setEffectSuspended_l(type, suspend, sessionId);
1335}
1336
1337void AudioFlinger::ThreadBase::setEffectSuspended_l(
1338 const effect_uuid_t *type, bool suspend, int sessionId)
1339{
Glenn Kastenc64a6f02012-01-30 13:00:02 -08001340 sp<EffectChain> chain = getEffectChain_l(sessionId);
Eric Laurentf82fccd2011-07-27 19:49:51 -07001341 if (chain != 0) {
1342 if (type != NULL) {
1343 chain->setEffectSuspended_l(type, suspend);
1344 } else {
1345 chain->setEffectSuspendedAll_l(suspend);
1346 }
1347 }
1348
1349 updateSuspendedSessions_l(type, suspend, sessionId);
1350}
1351
1352void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain)
1353{
Glenn Kasten150d2382012-02-08 14:04:28 -08001354 ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId());
Eric Laurentf82fccd2011-07-27 19:49:51 -07001355 if (index < 0) {
1356 return;
1357 }
1358
1359 KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects =
1360 mSuspendedSessions.editValueAt(index);
1361
1362 for (size_t i = 0; i < sessionEffects.size(); i++) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07001363 sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i);
Eric Laurentf82fccd2011-07-27 19:49:51 -07001364 for (int j = 0; j < desc->mRefCount; j++) {
1365 if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) {
1366 chain->setEffectSuspendedAll_l(true);
1367 } else {
Steve Block71f2cf12011-10-20 11:56:00 +01001368 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x",
Glenn Kasten18db49a2012-03-12 16:29:55 -07001369 desc->mType.timeLow);
Eric Laurentf82fccd2011-07-27 19:49:51 -07001370 chain->setEffectSuspended_l(&desc->mType, true);
1371 }
1372 }
1373 }
1374}
1375
Eric Laurentf82fccd2011-07-27 19:49:51 -07001376void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type,
1377 bool suspend,
1378 int sessionId)
1379{
Glenn Kasten150d2382012-02-08 14:04:28 -08001380 ssize_t index = mSuspendedSessions.indexOfKey(sessionId);
Eric Laurentf82fccd2011-07-27 19:49:51 -07001381
1382 KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects;
1383
1384 if (suspend) {
1385 if (index >= 0) {
1386 sessionEffects = mSuspendedSessions.editValueAt(index);
1387 } else {
1388 mSuspendedSessions.add(sessionId, sessionEffects);
1389 }
1390 } else {
1391 if (index < 0) {
1392 return;
1393 }
1394 sessionEffects = mSuspendedSessions.editValueAt(index);
1395 }
1396
1397
1398 int key = EffectChain::kKeyForSuspendAll;
1399 if (type != NULL) {
1400 key = type->timeLow;
1401 }
1402 index = sessionEffects.indexOfKey(key);
1403
Glenn Kasten18db49a2012-03-12 16:29:55 -07001404 sp<SuspendedSessionDesc> desc;
Eric Laurentf82fccd2011-07-27 19:49:51 -07001405 if (suspend) {
1406 if (index >= 0) {
1407 desc = sessionEffects.valueAt(index);
1408 } else {
1409 desc = new SuspendedSessionDesc();
1410 if (type != NULL) {
1411 memcpy(&desc->mType, type, sizeof(effect_uuid_t));
1412 }
1413 sessionEffects.add(key, desc);
Steve Block71f2cf12011-10-20 11:56:00 +01001414 ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key);
Eric Laurentf82fccd2011-07-27 19:49:51 -07001415 }
1416 desc->mRefCount++;
1417 } else {
1418 if (index < 0) {
1419 return;
1420 }
1421 desc = sessionEffects.valueAt(index);
1422 if (--desc->mRefCount == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001423 ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key);
Eric Laurentf82fccd2011-07-27 19:49:51 -07001424 sessionEffects.removeItemsAt(index);
1425 if (sessionEffects.isEmpty()) {
Steve Block71f2cf12011-10-20 11:56:00 +01001426 ALOGV("updateSuspendedSessions_l() restore removing session %d",
Eric Laurentf82fccd2011-07-27 19:49:51 -07001427 sessionId);
1428 mSuspendedSessions.removeItem(sessionId);
1429 }
1430 }
1431 }
1432 if (!sessionEffects.isEmpty()) {
1433 mSuspendedSessions.replaceValueFor(sessionId, sessionEffects);
1434 }
1435}
1436
1437void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1438 bool enabled,
1439 int sessionId)
1440{
1441 Mutex::Autolock _l(mLock);
Eric Laurent7fa1cee2011-10-19 11:44:54 -07001442 checkSuspendOnEffectEnabled_l(effect, enabled, sessionId);
1443}
Eric Laurentf82fccd2011-07-27 19:49:51 -07001444
Eric Laurent7fa1cee2011-10-19 11:44:54 -07001445void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
1446 bool enabled,
1447 int sessionId)
1448{
Eric Laurent6752ec82011-08-10 10:37:50 -07001449 if (mType != RECORD) {
1450 // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
1451 // another session. This gives the priority to well behaved effect control panels
1452 // and applications not using global effects.
1453 if (sessionId != AUDIO_SESSION_OUTPUT_MIX) {
1454 setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
1455 }
1456 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07001457
1458 sp<EffectChain> chain = getEffectChain_l(sessionId);
1459 if (chain != 0) {
1460 chain->checkSuspendOnEffectEnabled(effect, enabled);
1461 }
1462}
1463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464// ----------------------------------------------------------------------------
1465
Eric Laurent464d5b32011-06-17 21:29:58 -07001466AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger,
1467 AudioStreamOut* output,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08001468 audio_io_handle_t id,
Glenn Kastenefd511a2012-01-26 10:38:26 -08001469 uint32_t device,
1470 type_t type)
1471 : ThreadBase(audioFlinger, id, device, type),
Glenn Kastendc3ac852012-01-25 15:28:08 -08001472 mMixBuffer(NULL), mSuspended(0), mBytesWritten(0),
1473 // Assumes constructor is called by AudioFlinger with it's mLock held,
1474 // but it would be safer to explicitly pass initial masterMute as parameter
1475 mMasterMute(audioFlinger->masterMute_l()),
1476 // mStreamTypes[] initialized in constructor body
1477 mOutput(output),
1478 // Assumes constructor is called by AudioFlinger with it's mLock held,
1479 // but it would be safer to explicitly pass initial masterVolume as parameter
John Grossmand8cf2962012-02-08 16:37:41 -08001480 mMasterVolume(audioFlinger->masterVolumeSW_l()),
Glenn Kastena13446a2012-03-08 07:47:15 -08001481 mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false),
Glenn Kasten6cb5e642012-03-12 18:13:59 -07001482 mMixerStatus(MIXER_IDLE),
Glenn Kasten0fe52c82012-02-24 14:59:21 -08001483 mPrevMixerStatus(MIXER_IDLE),
1484 standbyDelay(AudioFlinger::mStandbyTimeInNsecs)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485{
Glenn Kasten86e33622012-02-28 12:30:08 -08001486 snprintf(mName, kNameLength, "AudioOut_%X", id);
Eric Laurent6dbdc402011-07-22 09:04:31 -07001487
Eric Laurenta553c252009-07-17 12:17:14 -07001488 readOutputParameters();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489
Glenn Kasten6e987a42012-01-06 08:40:01 -08001490 // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08001491 // There is no AUDIO_STREAM_MIN, and ++ operator does not compile
1492 for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT;
1493 stream = (audio_stream_type_t) (stream + 1)) {
Glenn Kasten8b02b992012-01-09 09:40:36 -08001494 mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream);
1495 mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream);
Glenn Kasten6e987a42012-01-06 08:40:01 -08001496 // initialized by stream_type_t default constructor
1497 // mStreamTypes[stream].valid = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 }
Glenn Kasten8b02b992012-01-09 09:40:36 -08001499 // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here,
1500 // because mAudioFlinger doesn't have one to copy from
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501}
1502
Eric Laurenta553c252009-07-17 12:17:14 -07001503AudioFlinger::PlaybackThread::~PlaybackThread()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504{
1505 delete [] mMixBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506}
1507
Eric Laurenta553c252009-07-17 12:17:14 -07001508status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509{
1510 dumpInternals(fd, args);
1511 dumpTracks(fd, args);
Eric Laurent65b65452010-06-01 23:49:17 -07001512 dumpEffectChains(fd, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 return NO_ERROR;
1514}
1515
Eric Laurenta553c252009-07-17 12:17:14 -07001516status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517{
1518 const size_t SIZE = 256;
1519 char buffer[SIZE];
1520 String8 result;
1521
Eric Laurenta553c252009-07-17 12:17:14 -07001522 snprintf(buffer, SIZE, "Output thread %p tracks\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 result.append(buffer);
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001524 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 -08001525 for (size_t i = 0; i < mTracks.size(); ++i) {
Eric Laurentd3b4d0c2009-03-31 14:34:35 -07001526 sp<Track> track = mTracks[i];
1527 if (track != 0) {
1528 track->dump(buffer, SIZE);
1529 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 }
1531 }
1532
Eric Laurenta553c252009-07-17 12:17:14 -07001533 snprintf(buffer, SIZE, "Output thread %p active tracks\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 result.append(buffer);
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001535 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 -08001536 for (size_t i = 0; i < mActiveTracks.size(); ++i) {
Glenn Kastene4787422012-01-25 14:27:41 -08001537 sp<Track> track = mActiveTracks[i].promote();
1538 if (track != 0) {
1539 track->dump(buffer, SIZE);
1540 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
1542 }
1543 write(fd, result.string(), result.size());
1544 return NO_ERROR;
1545}
1546
Eric Laurenta553c252009-07-17 12:17:14 -07001547status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548{
1549 const size_t SIZE = 256;
1550 char buffer[SIZE];
1551 String8 result;
1552
Eric Laurent3fdb1262009-11-07 00:01:32 -08001553 snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 result.append(buffer);
1555 snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime));
1556 result.append(buffer);
1557 snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites);
1558 result.append(buffer);
1559 snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites);
1560 result.append(buffer);
1561 snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite);
1562 result.append(buffer);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08001563 snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended);
1564 result.append(buffer);
Eric Laurent65b65452010-06-01 23:49:17 -07001565 snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer);
1566 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 write(fd, result.string(), result.size());
Eric Laurent3fdb1262009-11-07 00:01:32 -08001568
1569 dumpBase(fd, args);
1570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 return NO_ERROR;
1572}
1573
1574// Thread virtuals
Eric Laurenta553c252009-07-17 12:17:14 -07001575status_t AudioFlinger::PlaybackThread::readyToRun()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576{
Eric Laurent828b9772011-08-07 16:32:26 -07001577 status_t status = initCheck();
1578 if (status == NO_ERROR) {
Steve Block6215d3f2012-01-04 20:05:49 +00001579 ALOGI("AudioFlinger's thread %p ready to run", this);
Eric Laurent828b9772011-08-07 16:32:26 -07001580 } else {
Steve Block3762c312012-01-06 19:20:56 +00001581 ALOGE("No working audio driver found.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 }
Eric Laurent828b9772011-08-07 16:32:26 -07001583 return status;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584}
1585
Eric Laurenta553c252009-07-17 12:17:14 -07001586void AudioFlinger::PlaybackThread::onFirstRef()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587{
Eric Laurent6dbdc402011-07-22 09:04:31 -07001588 run(mName, ANDROID_PRIORITY_URGENT_AUDIO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589}
1590
Eric Laurenta553c252009-07-17 12:17:14 -07001591// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
Glenn Kastenf743e1f2012-03-14 12:56:26 -07001592sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 const sp<AudioFlinger::Client>& client,
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08001594 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08001596 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001597 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 int frameCount,
1599 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -07001600 int sessionId,
John Grossmand8cf2962012-02-08 16:37:41 -08001601 bool isTimed,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 status_t *status)
1603{
1604 sp<Track> track;
1605 status_t lStatus;
Eric Laurenta553c252009-07-17 12:17:14 -07001606
1607 if (mType == DIRECT) {
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001608 if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) {
1609 if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
Steve Block3762c312012-01-06 19:20:56 +00001610 ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x \""
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001611 "for output %p with format %d",
1612 sampleRate, format, channelMask, mOutput, mFormat);
1613 lStatus = BAD_VALUE;
1614 goto Exit;
1615 }
Eric Laurenta553c252009-07-17 12:17:14 -07001616 }
1617 } else {
1618 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
1619 if (sampleRate > mSampleRate*2) {
Steve Block3762c312012-01-06 19:20:56 +00001620 ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate);
Eric Laurenta553c252009-07-17 12:17:14 -07001621 lStatus = BAD_VALUE;
1622 goto Exit;
1623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
1625
Eric Laurent464d5b32011-06-17 21:29:58 -07001626 lStatus = initCheck();
1627 if (lStatus != NO_ERROR) {
Steve Block3762c312012-01-06 19:20:56 +00001628 ALOGE("Audio driver not initialized.");
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001629 goto Exit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
1631
Eric Laurenta553c252009-07-17 12:17:14 -07001632 { // scope for mLock
1633 Mutex::Autolock _l(mLock);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001634
1635 // all tracks in same audio session must share the same routing strategy otherwise
1636 // conflicts will happen when tracks are moved from one output to another by audio policy
1637 // manager
Glenn Kastenaae26c82012-02-08 12:35:35 -08001638 uint32_t strategy = AudioSystem::getStrategyForStream(streamType);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001639 for (size_t i = 0; i < mTracks.size(); ++i) {
1640 sp<Track> t = mTracks[i];
Glenn Kasten12f32d62012-03-07 12:26:34 -08001641 if (t != 0 && !t->isOutputTrack()) {
Glenn Kastenaae26c82012-02-08 12:35:35 -08001642 uint32_t actual = AudioSystem::getStrategyForStream(t->streamType());
Glenn Kasten9818a9d2011-10-28 10:31:42 -07001643 if (sessionId == t->sessionId() && strategy != actual) {
Steve Block3762c312012-01-06 19:20:56 +00001644 ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",
Glenn Kasten9818a9d2011-10-28 10:31:42 -07001645 strategy, actual);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001646 lStatus = BAD_VALUE;
1647 goto Exit;
1648 }
1649 }
1650 }
1651
John Grossmand8cf2962012-02-08 16:37:41 -08001652 if (!isTimed) {
1653 track = new Track(this, client, streamType, sampleRate, format,
1654 channelMask, frameCount, sharedBuffer, sessionId);
1655 } else {
1656 track = TimedTrack::create(this, client, streamType, sampleRate, format,
1657 channelMask, frameCount, sharedBuffer, sessionId);
1658 }
1659 if (track == NULL || track->getCblk() == NULL || track->name() < 0) {
Eric Laurenta553c252009-07-17 12:17:14 -07001660 lStatus = NO_MEMORY;
1661 goto Exit;
1662 }
1663 mTracks.add(track);
Eric Laurent65b65452010-06-01 23:49:17 -07001664
1665 sp<EffectChain> chain = getEffectChain_l(sessionId);
1666 if (chain != 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001667 ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
Eric Laurent65b65452010-06-01 23:49:17 -07001668 track->setMainBuffer(chain->inBuffer());
Glenn Kastenaae26c82012-02-08 12:35:35 -08001669 chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType()));
Eric Laurent90681d62011-05-09 12:09:06 -07001670 chain->incTrackCnt();
Eric Laurent65b65452010-06-01 23:49:17 -07001671 }
Eric Laurent05ce0942011-08-30 10:18:54 -07001672
1673 // invalidate track immediately if the stream type was moved to another thread since
1674 // createTrack() was called by the client process.
1675 if (!mStreamTypes[streamType].valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00001676 ALOGW("createTrack_l() on thread %p: invalidating track on stream %d",
Glenn Kasten18db49a2012-03-12 16:29:55 -07001677 this, streamType);
Eric Laurent05ce0942011-08-30 10:18:54 -07001678 android_atomic_or(CBLK_INVALID_ON, &track->mCblk->flags);
1679 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001680 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001681 lStatus = NO_ERROR;
1682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683Exit:
Glenn Kasten18db49a2012-03-12 16:29:55 -07001684 if (status) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 *status = lStatus;
1686 }
1687 return track;
1688}
1689
Eric Laurenta553c252009-07-17 12:17:14 -07001690uint32_t AudioFlinger::PlaybackThread::latency() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691{
Eric Laurent828b9772011-08-07 16:32:26 -07001692 Mutex::Autolock _l(mLock);
1693 if (initCheck() == NO_ERROR) {
Dima Zavin31f188892011-04-18 16:57:27 -07001694 return mOutput->stream->get_latency(mOutput->stream);
Eric Laurent828b9772011-08-07 16:32:26 -07001695 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 return 0;
1697 }
1698}
1699
Glenn Kasten8b02b992012-01-09 09:40:36 -08001700void AudioFlinger::PlaybackThread::setMasterVolume(float value)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701{
Glenn Kasten8b02b992012-01-09 09:40:36 -08001702 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 mMasterVolume = value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704}
1705
Glenn Kasten8b02b992012-01-09 09:40:36 -08001706void AudioFlinger::PlaybackThread::setMasterMute(bool muted)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707{
Glenn Kasten8b02b992012-01-09 09:40:36 -08001708 Mutex::Autolock _l(mLock);
1709 setMasterMute_l(muted);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710}
1711
Glenn Kasten8b02b992012-01-09 09:40:36 -08001712void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713{
Glenn Kasten8b02b992012-01-09 09:40:36 -08001714 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 mStreamTypes[stream].volume = value;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716}
1717
Glenn Kasten8b02b992012-01-09 09:40:36 -08001718void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719{
Glenn Kasten8b02b992012-01-09 09:40:36 -08001720 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 mStreamTypes[stream].mute = muted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722}
1723
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08001724float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725{
Glenn Kasten8b02b992012-01-09 09:40:36 -08001726 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 return mStreamTypes[stream].volume;
1728}
1729
Eric Laurenta553c252009-07-17 12:17:14 -07001730// addTrack_l() must be called with ThreadBase::mLock held
1731status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732{
1733 status_t status = ALREADY_EXISTS;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001734
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001735 // set retry count for buffer fill
1736 track->mRetryCount = kMaxTrackStartupRetries;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001737 if (mActiveTracks.indexOf(track) < 0) {
1738 // the track is newly added, make sure it fills up all its
1739 // buffers before playing. This is to ensure the client will
1740 // effectively get the latency it requested.
1741 track->mFillingUpStatus = Track::FS_FILLING;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08001742 track->mResetDone = false;
Eric Laurenta553c252009-07-17 12:17:14 -07001743 mActiveTracks.add(track);
Eric Laurent65b65452010-06-01 23:49:17 -07001744 if (track->mainBuffer() != mMixBuffer) {
1745 sp<EffectChain> chain = getEffectChain_l(track->sessionId());
1746 if (chain != 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001747 ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId());
Eric Laurent90681d62011-05-09 12:09:06 -07001748 chain->incActiveTrackCnt();
Eric Laurent65b65452010-06-01 23:49:17 -07001749 }
1750 }
1751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 status = NO_ERROR;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001753 }
Eric Laurenta553c252009-07-17 12:17:14 -07001754
Steve Block71f2cf12011-10-20 11:56:00 +01001755 ALOGV("mWaitWorkCV.broadcast");
Eric Laurenta553c252009-07-17 12:17:14 -07001756 mWaitWorkCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757
1758 return status;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001759}
1760
Eric Laurenta553c252009-07-17 12:17:14 -07001761// destroyTrack_l() must be called with ThreadBase::mLock held
1762void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001763{
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001764 track->mState = TrackBase::TERMINATED;
1765 if (mActiveTracks.indexOf(track) < 0) {
Eric Laurent90681d62011-05-09 12:09:06 -07001766 removeTrack_l(track);
1767 }
1768}
1769
1770void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track)
1771{
1772 mTracks.remove(track);
1773 deleteTrackName_l(track->name());
1774 sp<EffectChain> chain = getEffectChain_l(track->sessionId());
1775 if (chain != 0) {
1776 chain->decTrackCnt();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001777 }
1778}
1779
Eric Laurenta553c252009-07-17 12:17:14 -07001780String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
The Android Open Source Projectf1e484a2009-01-22 00:13:42 -08001781{
Eric Laurent828b9772011-08-07 16:32:26 -07001782 String8 out_s8 = String8("");
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001783 char *s;
1784
Eric Laurent828b9772011-08-07 16:32:26 -07001785 Mutex::Autolock _l(mLock);
1786 if (initCheck() != NO_ERROR) {
1787 return out_s8;
1788 }
1789
Dima Zavin31f188892011-04-18 16:57:27 -07001790 s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string());
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001791 out_s8 = String8(s);
1792 free(s);
1793 return out_s8;
Eric Laurenta553c252009-07-17 12:17:14 -07001794}
The Android Open Source Projectf1e484a2009-01-22 00:13:42 -08001795
Eric Laurent828b9772011-08-07 16:32:26 -07001796// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001797void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) {
Eric Laurenta553c252009-07-17 12:17:14 -07001798 AudioSystem::OutputDescriptor desc;
Glenn Kasten3694ec12012-01-27 16:47:15 -08001799 void *param2 = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07001800
Steve Block71f2cf12011-10-20 11:56:00 +01001801 ALOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param);
Eric Laurenta553c252009-07-17 12:17:14 -07001802
1803 switch (event) {
1804 case AudioSystem::OUTPUT_OPENED:
1805 case AudioSystem::OUTPUT_CONFIG_CHANGED:
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001806 desc.channels = mChannelMask;
Eric Laurenta553c252009-07-17 12:17:14 -07001807 desc.samplingRate = mSampleRate;
1808 desc.format = mFormat;
1809 desc.frameCount = mFrameCount;
1810 desc.latency = latency();
1811 param2 = &desc;
1812 break;
1813
1814 case AudioSystem::STREAM_CONFIG_CHANGED:
1815 param2 = &param;
1816 case AudioSystem::OUTPUT_CLOSED:
1817 default:
1818 break;
1819 }
Eric Laurent49f02be2009-11-19 09:00:56 -08001820 mAudioFlinger->audioConfigChanged_l(event, mId, param2);
Eric Laurenta553c252009-07-17 12:17:14 -07001821}
1822
1823void AudioFlinger::PlaybackThread::readOutputParameters()
1824{
Dima Zavin31f188892011-04-18 16:57:27 -07001825 mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common);
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001826 mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common);
1827 mChannelCount = (uint16_t)popcount(mChannelMask);
Dima Zavin31f188892011-04-18 16:57:27 -07001828 mFormat = mOutput->stream->common.get_format(&mOutput->stream->common);
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001829 mFrameSize = audio_stream_frame_size(&mOutput->stream->common);
Dima Zavin31f188892011-04-18 16:57:27 -07001830 mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize;
Eric Laurenta553c252009-07-17 12:17:14 -07001831
Eric Laurenta553c252009-07-17 12:17:14 -07001832 // FIXME - Current mixer implementation only supports stereo output: Always
1833 // Allocate a stereo buffer even if HW output is mono.
Glenn Kasten2589cf92012-01-27 18:08:45 -08001834 delete[] mMixBuffer;
Eric Laurenta553c252009-07-17 12:17:14 -07001835 mMixBuffer = new int16_t[mFrameCount * 2];
1836 memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t));
Eric Laurent65b65452010-06-01 23:49:17 -07001837
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001838 // force reconfiguration of effect chains and engines to take new buffer size and audio
1839 // parameters into account
1840 // Note that mLock is not held when readOutputParameters() is called from the constructor
1841 // but in this case nothing is done below as no audio sessions have effect yet so it doesn't
1842 // matter.
1843 // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains
1844 Vector< sp<EffectChain> > effectChains = mEffectChains;
1845 for (size_t i = 0; i < effectChains.size(); i ++) {
Eric Laurent493941b2010-07-28 01:32:47 -07001846 mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001847 }
Eric Laurenta553c252009-07-17 12:17:14 -07001848}
1849
Eric Laurent0986e792010-01-19 17:37:09 -08001850status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames)
1851{
Glenn Kasten3694ec12012-01-27 16:47:15 -08001852 if (halFrames == NULL || dspFrames == NULL) {
Eric Laurent0986e792010-01-19 17:37:09 -08001853 return BAD_VALUE;
1854 }
Eric Laurent828b9772011-08-07 16:32:26 -07001855 Mutex::Autolock _l(mLock);
Eric Laurent464d5b32011-06-17 21:29:58 -07001856 if (initCheck() != NO_ERROR) {
Eric Laurent0986e792010-01-19 17:37:09 -08001857 return INVALID_OPERATION;
1858 }
Dima Zavin31f188892011-04-18 16:57:27 -07001859 *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common);
Eric Laurent0986e792010-01-19 17:37:09 -08001860
Dima Zavin31f188892011-04-18 16:57:27 -07001861 return mOutput->stream->get_render_position(mOutput->stream, dspFrames);
Eric Laurent0986e792010-01-19 17:37:09 -08001862}
1863
Eric Laurent493941b2010-07-28 01:32:47 -07001864uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId)
Eric Laurent65b65452010-06-01 23:49:17 -07001865{
1866 Mutex::Autolock _l(mLock);
Eric Laurent493941b2010-07-28 01:32:47 -07001867 uint32_t result = 0;
Eric Laurent65b65452010-06-01 23:49:17 -07001868 if (getEffectChain_l(sessionId) != 0) {
Eric Laurent493941b2010-07-28 01:32:47 -07001869 result = EFFECT_SESSION;
Eric Laurent65b65452010-06-01 23:49:17 -07001870 }
1871
1872 for (size_t i = 0; i < mTracks.size(); ++i) {
1873 sp<Track> track = mTracks[i];
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001874 if (sessionId == track->sessionId() &&
1875 !(track->mCblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent493941b2010-07-28 01:32:47 -07001876 result |= TRACK_SESSION;
1877 break;
Eric Laurent65b65452010-06-01 23:49:17 -07001878 }
1879 }
1880
Eric Laurent493941b2010-07-28 01:32:47 -07001881 return result;
Eric Laurent65b65452010-06-01 23:49:17 -07001882}
1883
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001884uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId)
1885{
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001886 // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001887 // it is moved to correct output by audio policy manager when A2DP is connected or disconnected
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001888 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
1889 return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001890 }
1891 for (size_t i = 0; i < mTracks.size(); i++) {
1892 sp<Track> track = mTracks[i];
1893 if (sessionId == track->sessionId() &&
1894 !(track->mCblk->flags & CBLK_INVALID_MSK)) {
Glenn Kastenaae26c82012-02-08 12:35:35 -08001895 return AudioSystem::getStrategyForStream(track->streamType());
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001896 }
1897 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001898 return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07001899}
1900
Eric Laurent53334cd2010-06-23 17:38:20 -07001901
Glenn Kasten5b0135e2012-01-26 09:46:34 -08001902AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const
Eric Laurent828b9772011-08-07 16:32:26 -07001903{
1904 Mutex::Autolock _l(mLock);
1905 return mOutput;
1906}
1907
1908AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput()
1909{
1910 Mutex::Autolock _l(mLock);
1911 AudioStreamOut *output = mOutput;
1912 mOutput = NULL;
1913 return output;
1914}
1915
1916// this method must always be called either with ThreadBase mLock held or inside the thread loop
1917audio_stream_t* AudioFlinger::PlaybackThread::stream()
1918{
1919 if (mOutput == NULL) {
1920 return NULL;
1921 }
1922 return &mOutput->stream->common;
1923}
1924
Eric Laurent44331692011-12-05 09:47:19 -08001925uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs()
1926{
1927 // A2DP output latency is not due only to buffering capacity. It also reflects encoding,
1928 // decoding and transfer time. So sleeping for half of the latency would likely cause
1929 // underruns
1930 if (audio_is_a2dp_device((audio_devices_t)mDevice)) {
1931 return (uint32_t)((uint32_t)((mFrameCount * 1000) / mSampleRate) * 1000);
1932 } else {
1933 return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2;
1934 }
1935}
1936
Eric Laurenta553c252009-07-17 12:17:14 -07001937// ----------------------------------------------------------------------------
1938
Glenn Kastenefd511a2012-01-26 10:38:26 -08001939AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08001940 audio_io_handle_t id, uint32_t device, type_t type)
Glenn Kasten67cb3122012-03-01 17:10:56 -08001941 : PlaybackThread(audioFlinger, output, id, device, type)
Eric Laurenta553c252009-07-17 12:17:14 -07001942{
Glenn Kasten67cb3122012-03-01 17:10:56 -08001943 mAudioMixer = new AudioMixer(mFrameCount, mSampleRate);
Eric Laurenta553c252009-07-17 12:17:14 -07001944 // FIXME - Current mixer implementation only supports stereo output
1945 if (mChannelCount == 1) {
Steve Block3762c312012-01-06 19:20:56 +00001946 ALOGE("Invalid audio hardware channel count");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 }
The Android Open Source Projectf1e484a2009-01-22 00:13:42 -08001948}
1949
Eric Laurenta553c252009-07-17 12:17:14 -07001950AudioFlinger::MixerThread::~MixerThread()
The Android Open Source Projectf1e484a2009-01-22 00:13:42 -08001951{
Eric Laurenta553c252009-07-17 12:17:14 -07001952 delete mAudioMixer;
1953}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954
Glenn Kasten6ec27552012-02-24 07:21:32 -08001955class CpuStats {
1956public:
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08001957 CpuStats();
1958 void sample(const String8 &title);
Glenn Kasten6ec27552012-02-24 07:21:32 -08001959#ifdef DEBUG_CPU_USAGE
1960private:
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08001961 ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns
1962 CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns
1963
1964 CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles
1965
1966 int mCpuNum; // thread's current CPU number
1967 int mCpukHz; // frequency of thread's current CPU in kHz
Glenn Kasten6ec27552012-02-24 07:21:32 -08001968#endif
1969};
1970
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08001971CpuStats::CpuStats()
Glenn Kasten6ec27552012-02-24 07:21:32 -08001972#ifdef DEBUG_CPU_USAGE
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08001973 : mCpuNum(-1), mCpukHz(-1)
1974#endif
1975{
1976}
1977
1978void CpuStats::sample(const String8 &title) {
1979#ifdef DEBUG_CPU_USAGE
1980 // get current thread's delta CPU time in wall clock ns
1981 double wcNs;
1982 bool valid = mCpuUsage.sampleAndEnable(wcNs);
1983
1984 // record sample for wall clock statistics
1985 if (valid) {
1986 mWcStats.sample(wcNs);
1987 }
1988
1989 // get the current CPU number
1990 int cpuNum = sched_getcpu();
1991
1992 // get the current CPU frequency in kHz
1993 int cpukHz = mCpuUsage.getCpukHz(cpuNum);
1994
1995 // check if either CPU number or frequency changed
1996 if (cpuNum != mCpuNum || cpukHz != mCpukHz) {
1997 mCpuNum = cpuNum;
1998 mCpukHz = cpukHz;
1999 // ignore sample for purposes of cycles
2000 valid = false;
2001 }
2002
2003 // if no change in CPU number or frequency, then record sample for cycle statistics
2004 if (valid && mCpukHz > 0) {
2005 double cycles = wcNs * cpukHz * 0.000001;
2006 mHzStats.sample(cycles);
2007 }
2008
2009 unsigned n = mWcStats.n();
2010 // mCpuUsage.elapsed() is expensive, so don't call it every loop
Glenn Kasten6ec27552012-02-24 07:21:32 -08002011 if ((n & 127) == 1) {
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002012 long long elapsed = mCpuUsage.elapsed();
Glenn Kasten6ec27552012-02-24 07:21:32 -08002013 if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) {
2014 double perLoop = elapsed / (double) n;
2015 double perLoop100 = perLoop * 0.01;
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002016 double perLoop1k = perLoop * 0.001;
2017 double mean = mWcStats.mean();
2018 double stddev = mWcStats.stddev();
2019 double minimum = mWcStats.minimum();
2020 double maximum = mWcStats.maximum();
2021 double meanCycles = mHzStats.mean();
2022 double stddevCycles = mHzStats.stddev();
2023 double minCycles = mHzStats.minimum();
2024 double maxCycles = mHzStats.maximum();
2025 mCpuUsage.resetElapsed();
2026 mWcStats.reset();
2027 mHzStats.reset();
2028 ALOGD("CPU usage for %s over past %.1f secs\n"
2029 " (%u mixer loops at %.1f mean ms per loop):\n"
2030 " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n"
2031 " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n"
2032 " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f",
2033 title.string(),
Glenn Kasten6ec27552012-02-24 07:21:32 -08002034 elapsed * .000000001, n, perLoop * .000001,
2035 mean * .001,
2036 stddev * .001,
2037 minimum * .001,
2038 maximum * .001,
2039 mean / perLoop100,
2040 stddev / perLoop100,
2041 minimum / perLoop100,
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002042 maximum / perLoop100,
2043 meanCycles / perLoop1k,
2044 stddevCycles / perLoop1k,
2045 minCycles / perLoop1k,
2046 maxCycles / perLoop1k);
2047
Glenn Kasten6ec27552012-02-24 07:21:32 -08002048 }
2049 }
2050#endif
2051};
2052
Glenn Kasten2521a012012-02-24 07:21:48 -08002053void AudioFlinger::PlaybackThread::checkSilentMode_l()
2054{
2055 if (!mMasterMute) {
2056 char value[PROPERTY_VALUE_MAX];
2057 if (property_get("ro.audio.silent", value, "0") > 0) {
2058 char *endptr;
2059 unsigned long ul = strtoul(value, &endptr, 0);
2060 if (*endptr == '\0' && ul != 0) {
2061 ALOGD("Silence is golden");
2062 // The setprop command will not allow a property to be changed after
2063 // the first time it is set, so we don't have to worry about un-muting.
2064 setMasterMute_l(true);
2065 }
2066 }
2067 }
2068}
2069
Glenn Kasten67cb3122012-03-01 17:10:56 -08002070bool AudioFlinger::PlaybackThread::threadLoop()
Eric Laurenta553c252009-07-17 12:17:14 -07002071{
Eric Laurenta553c252009-07-17 12:17:14 -07002072 Vector< sp<Track> > tracksToRemove;
Glenn Kastenbea6b952012-02-29 07:57:06 -08002073
Glenn Kasten67cb3122012-03-01 17:10:56 -08002074 standbyTime = systemTime();
Glenn Kasten67cb3122012-03-01 17:10:56 -08002075
2076 // MIXER
Dave Sparksd0ac8c02009-09-30 03:09:03 -07002077 nsecs_t lastWarning = 0;
Glenn Kasten67cb3122012-03-01 17:10:56 -08002078if (mType == MIXER) {
2079 longStandbyExit = false;
2080}
Glenn Kastenbea6b952012-02-29 07:57:06 -08002081
Glenn Kasten67cb3122012-03-01 17:10:56 -08002082 // DUPLICATING
2083 // FIXME could this be made local to while loop?
2084 writeFrames = 0;
Glenn Kastenbea6b952012-02-29 07:57:06 -08002085
Glenn Kasten0fe52c82012-02-24 14:59:21 -08002086 cacheParameters_l();
Glenn Kasten67cb3122012-03-01 17:10:56 -08002087 sleepTime = idleSleepTime;
2088
2089if (mType == MIXER) {
2090 sleepTimeShift = 0;
2091}
2092
Glenn Kasten6ec27552012-02-24 07:21:32 -08002093 CpuStats cpuStats;
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002094 const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095
Eric Laurent6dbdc402011-07-22 09:04:31 -07002096 acquireWakeLock();
2097
Eric Laurenta553c252009-07-17 12:17:14 -07002098 while (!exitPending())
2099 {
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002100 cpuStats.sample(myName);
Glenn Kastenbea6b952012-02-29 07:57:06 -08002101
Glenn Kasten3c4f0dc2012-02-29 07:56:15 -08002102 Vector< sp<EffectChain> > effectChains;
2103
Eric Laurenta553c252009-07-17 12:17:14 -07002104 processConfigEvents();
2105
Eric Laurenta553c252009-07-17 12:17:14 -07002106 { // scope for mLock
2107
2108 Mutex::Autolock _l(mLock);
2109
2110 if (checkForNewParameters_l()) {
Glenn Kasten0fe52c82012-02-24 14:59:21 -08002111 cacheParameters_l();
Eric Laurenta553c252009-07-17 12:17:14 -07002112 }
2113
Glenn Kasten08d2c042012-03-06 11:28:04 -08002114 saveOutputTracks();
Glenn Kasten67cb3122012-03-01 17:10:56 -08002115
Eric Laurenta553c252009-07-17 12:17:14 -07002116 // put audio hardware into standby after short delay
Glenn Kastend9cccfa2012-02-28 18:40:35 -08002117 if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) ||
Glenn Kastene70583e2012-02-29 07:07:30 -08002118 mSuspended > 0)) {
Eric Laurenta553c252009-07-17 12:17:14 -07002119 if (!mStandby) {
Glenn Kasten67cb3122012-03-01 17:10:56 -08002120
2121 threadLoop_standby();
2122
Eric Laurenta553c252009-07-17 12:17:14 -07002123 mStandby = true;
2124 mBytesWritten = 0;
2125 }
2126
Glenn Kastend9cccfa2012-02-28 18:40:35 -08002127 if (!mActiveTracks.size() && mConfigEvents.isEmpty()) {
Eric Laurenta553c252009-07-17 12:17:14 -07002128 // we're about to wait, flush the binder command buffer
2129 IPCThreadState::self()->flushCommands();
2130
Glenn Kasten08d2c042012-03-06 11:28:04 -08002131 clearOutputTracks();
Glenn Kasten67cb3122012-03-01 17:10:56 -08002132
Eric Laurenta553c252009-07-17 12:17:14 -07002133 if (exitPending()) break;
2134
Eric Laurent6dbdc402011-07-22 09:04:31 -07002135 releaseWakeLock_l();
Eric Laurenta553c252009-07-17 12:17:14 -07002136 // wait until we have something to do...
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002137 ALOGV("%s going to sleep", myName.string());
Eric Laurenta553c252009-07-17 12:17:14 -07002138 mWaitWorkCV.wait(mLock);
Glenn Kastenf57e2bc2012-03-06 11:27:10 -08002139 ALOGV("%s waking up", myName.string());
Eric Laurent6dbdc402011-07-22 09:04:31 -07002140 acquireWakeLock_l();
Eric Laurenta553c252009-07-17 12:17:14 -07002141
Eric Laurent71c44962012-01-17 19:20:12 -08002142 mPrevMixerStatus = MIXER_IDLE;
Glenn Kasten67cb3122012-03-01 17:10:56 -08002143
Glenn Kasten2521a012012-02-24 07:21:48 -08002144 checkSilentMode_l();
Eric Laurenta553c252009-07-17 12:17:14 -07002145
Glenn Kasten67cb3122012-03-01 17:10:56 -08002146 standbyTime = systemTime() + standbyDelay;
Eric Laurent059b4be2009-11-09 23:32:22 -08002147 sleepTime = idleSleepTime;
Glenn Kasten0fe52c82012-02-24 14:59:21 -08002148 if (mType == MIXER) {
2149 sleepTimeShift = 0;
2150 }
Glenn Kasten67cb3122012-03-01 17:10:56 -08002151
Eric Laurenta553c252009-07-17 12:17:14 -07002152 continue;
2153 }
2154 }
2155
Glenn Kastena13446a2012-03-08 07:47:15 -08002156 mixer_state newMixerStatus = prepareTracks_l(&tracksToRemove);
2157 // Shift in the new status; this could be a queue if it's
2158 // useful to filter the mixer status over several cycles.
2159 mPrevMixerStatus = mMixerStatus;
2160 mMixerStatus = newMixerStatus;
Eric Laurent65b65452010-06-01 23:49:17 -07002161
2162 // prevent any changes in effect chain list and in each effect chain
2163 // during mixing and effect process as the audio buffers could be deleted
2164 // or modified if an effect is created or deleted
Eric Laurent8ed6ed02010-07-13 04:45:46 -07002165 lockEffectChains_l(effectChains);
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -08002166 }
Eric Laurenta553c252009-07-17 12:17:14 -07002167
Glenn Kastena13446a2012-03-08 07:47:15 -08002168 if (CC_LIKELY(mMixerStatus == MIXER_TRACKS_READY)) {
Glenn Kasten67cb3122012-03-01 17:10:56 -08002169 threadLoop_mix();
2170 } else {
2171 threadLoop_sleepTime();
2172 }
2173
2174 if (mSuspended > 0) {
2175 sleepTime = suspendSleepTimeUs();
2176 }
2177
2178 // only process effects if we're going to write
2179 if (sleepTime == 0) {
Glenn Kasten67cb3122012-03-01 17:10:56 -08002180 for (size_t i = 0; i < effectChains.size(); i ++) {
2181 effectChains[i]->process_l();
2182 }
2183 }
2184
2185 // enable changes in effect chain
2186 unlockEffectChains(effectChains);
2187
2188 // sleepTime == 0 means we must write to audio hardware
2189 if (sleepTime == 0) {
2190
2191 threadLoop_write();
2192
2193if (mType == MIXER) {
2194 // write blocked detection
2195 nsecs_t now = systemTime();
2196 nsecs_t delta = now - mLastWriteTime;
2197 if (!mStandby && delta > maxPeriod) {
2198 mNumDelayedWrites++;
2199 if ((now - lastWarning) > kWarningThrottleNs) {
2200 ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p",
2201 ns2ms(delta), mNumDelayedWrites, this);
2202 lastWarning = now;
2203 }
2204 // FIXME this is broken: longStandbyExit should be handled out of the if() and with
2205 // a different threshold. Or completely removed for what it is worth anyway...
2206 if (mStandby) {
2207 longStandbyExit = true;
2208 }
2209 }
2210}
2211
2212 mStandby = false;
2213 } else {
2214 usleep(sleepTime);
2215 }
2216
2217 // finally let go of removed track(s), without the lock held
2218 // since we can't guarantee the destructors won't acquire that
2219 // same lock.
Glenn Kasten761415b2012-03-06 11:23:32 -08002220 tracksToRemove.clear();
Glenn Kasten67cb3122012-03-01 17:10:56 -08002221
Glenn Kasten08d2c042012-03-06 11:28:04 -08002222 // FIXME I don't understand the need for this here;
2223 // it was in the original code but maybe the
2224 // assignment in saveOutputTracks() makes this unnecessary?
2225 clearOutputTracks();
Glenn Kasten67cb3122012-03-01 17:10:56 -08002226
2227 // Effect chains will be actually deleted here if they were removed from
2228 // mEffectChains list during mixing or effects processing
2229 effectChains.clear();
2230
2231 // FIXME Note that the above .clear() is no longer necessary since effectChains
2232 // is now local to this block, but will keep it for now (at least until merge done).
2233 }
2234
2235if (mType == MIXER || mType == DIRECT) {
2236 // put output stream into standby mode
2237 if (!mStandby) {
2238 mOutput->stream->common.standby(&mOutput->stream->common);
2239 }
2240}
2241if (mType == DUPLICATING) {
2242 // for DuplicatingThread, standby mode is handled by the outputTracks
2243}
2244
2245 releaseWakeLock();
2246
2247 ALOGV("Thread %p type %d exiting", this, mType);
2248 return false;
2249}
2250
2251// shared by MIXER and DIRECT, overridden by DUPLICATING
2252void AudioFlinger::PlaybackThread::threadLoop_write()
2253{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002254 // FIXME rewrite to reduce number of system calls
2255 mLastWriteTime = systemTime();
2256 mInWrite = true;
2257 mBytesWritten += mixBufferSize;
2258 int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize);
2259 if (bytesWritten < 0) mBytesWritten -= mixBufferSize;
2260 mNumWrites++;
2261 mInWrite = false;
Glenn Kasten67cb3122012-03-01 17:10:56 -08002262}
2263
2264// shared by MIXER and DIRECT, overridden by DUPLICATING
2265void AudioFlinger::PlaybackThread::threadLoop_standby()
2266{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002267 ALOGV("Audio hardware entering standby, mixer %p, suspend count %u", this, mSuspended);
2268 mOutput->stream->common.standby(&mOutput->stream->common);
Glenn Kasten67cb3122012-03-01 17:10:56 -08002269}
2270
2271void AudioFlinger::MixerThread::threadLoop_mix()
2272{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002273 // obtain the presentation timestamp of the next output buffer
2274 int64_t pts;
2275 status_t status = INVALID_OPERATION;
John Grossmand8cf2962012-02-08 16:37:41 -08002276
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002277 if (NULL != mOutput->stream->get_next_write_timestamp) {
2278 status = mOutput->stream->get_next_write_timestamp(
2279 mOutput->stream, &pts);
2280 }
John Grossmand8cf2962012-02-08 16:37:41 -08002281
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002282 if (status != NO_ERROR) {
2283 pts = AudioBufferProvider::kInvalidPTS;
2284 }
John Grossmand8cf2962012-02-08 16:37:41 -08002285
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002286 // mix buffers...
2287 mAudioMixer->process(pts);
2288 // increase sleep time progressively when application underrun condition clears.
2289 // Only increase sleep time if the mixer is ready for two consecutive times to avoid
2290 // that a steady state of alternating ready/not ready conditions keeps the sleep time
2291 // such that we would underrun the audio HAL.
2292 if ((sleepTime == 0) && (sleepTimeShift > 0)) {
2293 sleepTimeShift--;
2294 }
2295 sleepTime = 0;
Glenn Kasten0fe52c82012-02-24 14:59:21 -08002296 standbyTime = systemTime() + standbyDelay;
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002297 //TODO: delay standby when effects have a tail
Glenn Kasten67cb3122012-03-01 17:10:56 -08002298}
2299
2300void AudioFlinger::MixerThread::threadLoop_sleepTime()
2301{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002302 // If no tracks are ready, sleep once for the duration of an output
2303 // buffer size, then write 0s to the output
2304 if (sleepTime == 0) {
Glenn Kastena13446a2012-03-08 07:47:15 -08002305 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002306 sleepTime = activeSleepTime >> sleepTimeShift;
2307 if (sleepTime < kMinThreadSleepTimeUs) {
2308 sleepTime = kMinThreadSleepTimeUs;
Eric Laurent96c08a62009-09-07 08:38:38 -07002309 }
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002310 // reduce sleep time in case of consecutive application underruns to avoid
2311 // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer
2312 // duration we would end up writing less data than needed by the audio HAL if
2313 // the condition persists.
2314 if (sleepTimeShift < kMaxThreadSleepTimeShift) {
2315 sleepTimeShift++;
2316 }
2317 } else {
2318 sleepTime = idleSleepTime;
2319 }
2320 } else if (mBytesWritten != 0 ||
Glenn Kastena13446a2012-03-08 07:47:15 -08002321 (mMixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) {
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002322 memset (mMixBuffer, 0, mixBufferSize);
2323 sleepTime = 0;
Glenn Kastena13446a2012-03-08 07:47:15 -08002324 ALOGV_IF((mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start");
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002325 }
2326 // TODO add standby time extension fct of effect tail
Eric Laurenta553c252009-07-17 12:17:14 -07002327}
2328
2329// prepareTracks_l() must be called with ThreadBase::mLock held
Glenn Kasten789fef12012-01-26 13:37:52 -08002330AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l(
Glenn Kastend9cccfa2012-02-28 18:40:35 -08002331 Vector< sp<Track> > *tracksToRemove)
Eric Laurenta553c252009-07-17 12:17:14 -07002332{
2333
Glenn Kasten789fef12012-01-26 13:37:52 -08002334 mixer_state mixerStatus = MIXER_IDLE;
Eric Laurenta553c252009-07-17 12:17:14 -07002335 // find out which tracks need to be processed
Glenn Kastend9cccfa2012-02-28 18:40:35 -08002336 size_t count = mActiveTracks.size();
Eric Laurent65b65452010-06-01 23:49:17 -07002337 size_t mixedTracks = 0;
2338 size_t tracksWithEffect = 0;
Glenn Kasten871c16c2010-03-05 12:18:01 -08002339
2340 float masterVolume = mMasterVolume;
Glenn Kastenf743e1f2012-03-14 12:56:26 -07002341 bool masterMute = mMasterMute;
Glenn Kasten871c16c2010-03-05 12:18:01 -08002342
Eric Laurent8cc93b92010-08-11 05:20:11 -07002343 if (masterMute) {
2344 masterVolume = 0;
2345 }
Eric Laurent65b65452010-06-01 23:49:17 -07002346 // Delegate master volume control to effect in output mix effect chain if needed
Dima Zavin24fc2fb2011-04-19 22:30:36 -07002347 sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent65b65452010-06-01 23:49:17 -07002348 if (chain != 0) {
Eric Laurent8cc93b92010-08-11 05:20:11 -07002349 uint32_t v = (uint32_t)(masterVolume * (1 << 24));
Eric Laurent76c40f72010-07-15 12:50:15 -07002350 chain->setVolume_l(&v, &v);
Eric Laurent65b65452010-06-01 23:49:17 -07002351 masterVolume = (float)((v + (1 << 23)) >> 24);
2352 chain.clear();
2353 }
Glenn Kasten871c16c2010-03-05 12:18:01 -08002354
Eric Laurenta553c252009-07-17 12:17:14 -07002355 for (size_t i=0 ; i<count ; i++) {
Glenn Kastend9cccfa2012-02-28 18:40:35 -08002356 sp<Track> t = mActiveTracks[i].promote();
Eric Laurenta553c252009-07-17 12:17:14 -07002357 if (t == 0) continue;
2358
Glenn Kasten68deb152011-12-19 15:06:39 -08002359 // this const just means the local variable doesn't change
Eric Laurenta553c252009-07-17 12:17:14 -07002360 Track* const track = t.get();
2361 audio_track_cblk_t* cblk = track->cblk();
2362
2363 // The first time a track is added we wait
2364 // for all its buffers to be filled before processing it
Glenn Kasten68deb152011-12-19 15:06:39 -08002365 int name = track->name();
Eric Laurent8a04fe02011-11-08 18:10:16 -08002366 // make sure that we have enough frames to mix one full buffer.
2367 // enforce this condition only once to enable draining the buffer in case the client
2368 // app does not call stop() and relies on underrun to stop:
Eric Laurent71c44962012-01-17 19:20:12 -08002369 // hence the test on (mPrevMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed
Eric Laurent8a04fe02011-11-08 18:10:16 -08002370 // during last round
Eric Laurent19ddf0e2011-11-03 12:16:05 -07002371 uint32_t minFrames = 1;
Eric Laurent8a04fe02011-11-08 18:10:16 -08002372 if (!track->isStopped() && !track->isPausing() &&
Eric Laurent71c44962012-01-17 19:20:12 -08002373 (mPrevMixerStatus == MIXER_TRACKS_READY)) {
Eric Laurent19ddf0e2011-11-03 12:16:05 -07002374 if (t->sampleRate() == (int)mSampleRate) {
2375 minFrames = mFrameCount;
2376 } else {
Eric Laurent72dafb22011-12-22 16:08:41 -08002377 // +1 for rounding and +1 for additional sample needed for interpolation
2378 minFrames = (mFrameCount * t->sampleRate()) / mSampleRate + 1 + 1;
2379 // add frames already consumed but not yet released by the resampler
Glenn Kastenf743e1f2012-03-14 12:56:26 -07002380 // because cblk->framesReady() will include these frames
Eric Laurent72dafb22011-12-22 16:08:41 -08002381 minFrames += mAudioMixer->getUnreleasedFrames(track->name());
2382 // the minimum track buffer size is normally twice the number of frames necessary
2383 // to fill one buffer and the resampler should not leave more than one buffer worth
2384 // of unreleased frames after each pass, but just in case...
Steve Blockec193de2012-01-09 18:35:44 +00002385 ALOG_ASSERT(minFrames <= cblk->frameCount);
Eric Laurent19ddf0e2011-11-03 12:16:05 -07002386 }
2387 }
John Grossmand8cf2962012-02-08 16:37:41 -08002388 if ((track->framesReady() >= minFrames) && track->isReady() &&
Eric Laurent71f37cd2010-03-31 12:21:17 -07002389 !track->isPaused() && !track->isTerminated())
Eric Laurenta553c252009-07-17 12:17:14 -07002390 {
Glenn Kasten68deb152011-12-19 15:06:39 -08002391 //ALOGV("track %d u=%08x, s=%08x [OK] on thread %p", name, cblk->user, cblk->server, this);
Eric Laurenta553c252009-07-17 12:17:14 -07002392
Eric Laurent65b65452010-06-01 23:49:17 -07002393 mixedTracks++;
2394
2395 // track->mainBuffer() != mMixBuffer means there is an effect chain
2396 // connected to the track
2397 chain.clear();
2398 if (track->mainBuffer() != mMixBuffer) {
2399 chain = getEffectChain_l(track->sessionId());
2400 // Delegate volume control to effect in track effect chain if needed
2401 if (chain != 0) {
2402 tracksWithEffect++;
2403 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00002404 ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on session %d",
Glenn Kasten68deb152011-12-19 15:06:39 -08002405 name, track->sessionId());
Eric Laurent65b65452010-06-01 23:49:17 -07002406 }
2407 }
2408
2409
2410 int param = AudioMixer::VOLUME;
2411 if (track->mFillingUpStatus == Track::FS_FILLED) {
2412 // no ramp for the first volume setting
2413 track->mFillingUpStatus = Track::FS_ACTIVE;
2414 if (track->mState == TrackBase::RESUMING) {
2415 track->mState = TrackBase::ACTIVE;
2416 param = AudioMixer::RAMP_VOLUME;
2417 }
Glenn Kasten68deb152011-12-19 15:06:39 -08002418 mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL);
Eric Laurent65b65452010-06-01 23:49:17 -07002419 } else if (cblk->server != 0) {
2420 // If the track is stopped before the first frame was mixed,
2421 // do not apply ramp
2422 param = AudioMixer::RAMP_VOLUME;
2423 }
2424
Eric Laurenta553c252009-07-17 12:17:14 -07002425 // compute volume for this track
Eric Laurent27a2fdf2010-09-10 17:44:44 -07002426 uint32_t vl, vr, va;
Eric Laurent2a6b80b2010-07-29 23:43:43 -07002427 if (track->isMuted() || track->isPausing() ||
Glenn Kastenaae26c82012-02-08 12:35:35 -08002428 mStreamTypes[track->streamType()].mute) {
Eric Laurent27a2fdf2010-09-10 17:44:44 -07002429 vl = vr = va = 0;
Eric Laurenta553c252009-07-17 12:17:14 -07002430 if (track->isPausing()) {
2431 track->setPaused();
2432 }
2433 } else {
Eric Laurent27a2fdf2010-09-10 17:44:44 -07002434
Glenn Kasten871c16c2010-03-05 12:18:01 -08002435 // read original volumes with volume control
Glenn Kastenaae26c82012-02-08 12:35:35 -08002436 float typeVolume = mStreamTypes[track->streamType()].volume;
Glenn Kasten871c16c2010-03-05 12:18:01 -08002437 float v = masterVolume * typeVolume;
Glenn Kastenbc4de882012-01-17 14:39:34 -08002438 uint32_t vlr = cblk->getVolumeLR();
Glenn Kasten0632bad2012-01-17 12:20:54 -08002439 vl = vlr & 0xFFFF;
2440 vr = vlr >> 16;
2441 // track volumes come from shared memory, so can't be trusted and must be clamped
2442 if (vl > MAX_GAIN_INT) {
2443 ALOGV("Track left volume out of range: %04X", vl);
2444 vl = MAX_GAIN_INT;
2445 }
2446 if (vr > MAX_GAIN_INT) {
2447 ALOGV("Track right volume out of range: %04X", vr);
2448 vr = MAX_GAIN_INT;
2449 }
2450 // now apply the master volume and stream type volume
2451 vl = (uint32_t)(v * vl) << 12;
2452 vr = (uint32_t)(v * vr) << 12;
2453 // assuming master volume and stream type volume each go up to 1.0,
2454 // vl and vr are now in 8.24 format
Eric Laurenta553c252009-07-17 12:17:14 -07002455
Glenn Kasten4790bd82012-01-03 14:22:33 -08002456 uint16_t sendLevel = cblk->getSendLevel_U4_12();
2457 // send level comes from shared memory and so may be corrupt
Glenn Kasten4f22c052012-01-27 15:26:23 -08002458 if (sendLevel > MAX_GAIN_INT) {
Glenn Kasten4790bd82012-01-03 14:22:33 -08002459 ALOGV("Track send level out of range: %04X", sendLevel);
Glenn Kasten0632bad2012-01-17 12:20:54 -08002460 sendLevel = MAX_GAIN_INT;
Glenn Kasten4790bd82012-01-03 14:22:33 -08002461 }
2462 va = (uint32_t)(v * sendLevel);
Eric Laurenta553c252009-07-17 12:17:14 -07002463 }
Eric Laurent27a2fdf2010-09-10 17:44:44 -07002464 // Delegate volume control to effect in track effect chain if needed
2465 if (chain != 0 && chain->setVolume_l(&vl, &vr)) {
2466 // Do not ramp volume if volume is controlled by effect
2467 param = AudioMixer::VOLUME;
2468 track->mHasVolumeController = true;
2469 } else {
2470 // force no volume ramp when volume controller was just disabled or removed
2471 // from effect chain to avoid volume spike
2472 if (track->mHasVolumeController) {
2473 param = AudioMixer::VOLUME;
2474 }
2475 track->mHasVolumeController = false;
2476 }
2477
2478 // Convert volumes from 8.24 to 4.12 format
Glenn Kasten0632bad2012-01-17 12:20:54 -08002479 // This additional clamping is needed in case chain->setVolume_l() overshot
Glenn Kasten4f22c052012-01-27 15:26:23 -08002480 vl = (vl + (1 << 11)) >> 12;
2481 if (vl > MAX_GAIN_INT) vl = MAX_GAIN_INT;
2482 vr = (vr + (1 << 11)) >> 12;
2483 if (vr > MAX_GAIN_INT) vr = MAX_GAIN_INT;
Eric Laurent27a2fdf2010-09-10 17:44:44 -07002484
Glenn Kasten4f22c052012-01-27 15:26:23 -08002485 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 -07002486
Eric Laurent65b65452010-06-01 23:49:17 -07002487 // XXX: these things DON'T need to be done each time
Glenn Kasten68deb152011-12-19 15:06:39 -08002488 mAudioMixer->setBufferProvider(name, track);
2489 mAudioMixer->enable(name);
Eric Laurent65b65452010-06-01 23:49:17 -07002490
Glenn Kasten4f22c052012-01-27 15:26:23 -08002491 mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)vl);
2492 mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)vr);
2493 mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)va);
Eric Laurenta553c252009-07-17 12:17:14 -07002494 mAudioMixer->setParameter(
Glenn Kasten68deb152011-12-19 15:06:39 -08002495 name,
Eric Laurenta553c252009-07-17 12:17:14 -07002496 AudioMixer::TRACK,
Eric Laurent65b65452010-06-01 23:49:17 -07002497 AudioMixer::FORMAT, (void *)track->format());
Eric Laurenta553c252009-07-17 12:17:14 -07002498 mAudioMixer->setParameter(
Glenn Kasten68deb152011-12-19 15:06:39 -08002499 name,
Eric Laurenta553c252009-07-17 12:17:14 -07002500 AudioMixer::TRACK,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07002501 AudioMixer::CHANNEL_MASK, (void *)track->channelMask());
Eric Laurenta553c252009-07-17 12:17:14 -07002502 mAudioMixer->setParameter(
Glenn Kasten68deb152011-12-19 15:06:39 -08002503 name,
Eric Laurenta553c252009-07-17 12:17:14 -07002504 AudioMixer::RESAMPLE,
2505 AudioMixer::SAMPLE_RATE,
Eric Laurent65b65452010-06-01 23:49:17 -07002506 (void *)(cblk->sampleRate));
2507 mAudioMixer->setParameter(
Glenn Kasten68deb152011-12-19 15:06:39 -08002508 name,
Eric Laurent65b65452010-06-01 23:49:17 -07002509 AudioMixer::TRACK,
2510 AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer());
2511 mAudioMixer->setParameter(
Glenn Kasten68deb152011-12-19 15:06:39 -08002512 name,
Eric Laurent65b65452010-06-01 23:49:17 -07002513 AudioMixer::TRACK,
2514 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer());
Eric Laurenta553c252009-07-17 12:17:14 -07002515
2516 // reset retry count
2517 track->mRetryCount = kMaxTrackRetries;
Glenn Kastenf743e1f2012-03-14 12:56:26 -07002518
Eric Laurent71c44962012-01-17 19:20:12 -08002519 // If one track is ready, set the mixer ready if:
2520 // - the mixer was not ready during previous round OR
2521 // - no other track is not ready
2522 if (mPrevMixerStatus != MIXER_TRACKS_READY ||
2523 mixerStatus != MIXER_TRACKS_ENABLED) {
2524 mixerStatus = MIXER_TRACKS_READY;
2525 }
Eric Laurenta553c252009-07-17 12:17:14 -07002526 } else {
Glenn Kasten68deb152011-12-19 15:06:39 -08002527 //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 -07002528 if (track->isStopped()) {
2529 track->reset();
2530 }
2531 if (track->isTerminated() || track->isStopped() || track->isPaused()) {
2532 // We have consumed all the buffers of this track.
2533 // Remove it from the list of active tracks.
2534 tracksToRemove->add(track);
Eric Laurenta553c252009-07-17 12:17:14 -07002535 } else {
2536 // No buffers for this track. Give it a few chances to
2537 // fill a buffer, then remove it from active list.
2538 if (--(track->mRetryCount) <= 0) {
Glenn Kasten68deb152011-12-19 15:06:39 -08002539 ALOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this);
Eric Laurenta553c252009-07-17 12:17:14 -07002540 tracksToRemove->add(track);
Eric Laurent4712baa2010-09-30 16:12:31 -07002541 // indicate to client process that the track was disabled because of underrun
Eric Laurentae29b762011-03-28 18:37:07 -07002542 android_atomic_or(CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent71c44962012-01-17 19:20:12 -08002543 // If one track is not ready, mark the mixer also not ready if:
2544 // - the mixer was ready during previous round OR
2545 // - no other track is ready
2546 } else if (mPrevMixerStatus == MIXER_TRACKS_READY ||
2547 mixerStatus != MIXER_TRACKS_READY) {
Eric Laurent059b4be2009-11-09 23:32:22 -08002548 mixerStatus = MIXER_TRACKS_ENABLED;
Eric Laurenta553c252009-07-17 12:17:14 -07002549 }
Eric Laurenta553c252009-07-17 12:17:14 -07002550 }
Glenn Kasten68deb152011-12-19 15:06:39 -08002551 mAudioMixer->disable(name);
Eric Laurenta553c252009-07-17 12:17:14 -07002552 }
2553 }
2554
2555 // remove all the tracks that need to be...
2556 count = tracksToRemove->size();
Glenn Kastene80a4cc2011-12-15 09:51:17 -08002557 if (CC_UNLIKELY(count)) {
Eric Laurenta553c252009-07-17 12:17:14 -07002558 for (size_t i=0 ; i<count ; i++) {
2559 const sp<Track>& track = tracksToRemove->itemAt(i);
2560 mActiveTracks.remove(track);
Eric Laurent65b65452010-06-01 23:49:17 -07002561 if (track->mainBuffer() != mMixBuffer) {
2562 chain = getEffectChain_l(track->sessionId());
2563 if (chain != 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01002564 ALOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId());
Eric Laurent90681d62011-05-09 12:09:06 -07002565 chain->decActiveTrackCnt();
Eric Laurent65b65452010-06-01 23:49:17 -07002566 }
2567 }
Eric Laurenta553c252009-07-17 12:17:14 -07002568 if (track->isTerminated()) {
Eric Laurent90681d62011-05-09 12:09:06 -07002569 removeTrack_l(track);
Eric Laurenta553c252009-07-17 12:17:14 -07002570 }
2571 }
2572 }
2573
Eric Laurent65b65452010-06-01 23:49:17 -07002574 // mix buffer must be cleared if all tracks are connected to an
2575 // effect chain as in this case the mixer will not write to
2576 // mix buffer and track effects will accumulate into it
2577 if (mixedTracks != 0 && mixedTracks == tracksWithEffect) {
2578 memset(mMixBuffer, 0, mFrameCount * mChannelCount * sizeof(int16_t));
2579 }
2580
Eric Laurent059b4be2009-11-09 23:32:22 -08002581 return mixerStatus;
Eric Laurenta553c252009-07-17 12:17:14 -07002582}
2583
Glenn Kasten0fe52c82012-02-24 14:59:21 -08002584/*
2585The derived values that are cached:
2586 - mixBufferSize from frame count * frame size
2587 - activeSleepTime from activeSleepTimeUs()
2588 - idleSleepTime from idleSleepTimeUs()
2589 - standbyDelay from mActiveSleepTimeUs (DIRECT only)
2590 - maxPeriod from frame count and sample rate (MIXER only)
2591
2592The parameters that affect these derived values are:
2593 - frame count
2594 - frame size
2595 - sample rate
2596 - device type: A2DP or not
2597 - device latency
2598 - format: PCM or not
2599 - active sleep time
2600 - idle sleep time
2601*/
2602
2603void AudioFlinger::PlaybackThread::cacheParameters_l()
2604{
2605 mixBufferSize = mFrameCount * mFrameSize;
2606 activeSleepTime = activeSleepTimeUs();
2607 idleSleepTime = idleSleepTimeUs();
2608}
2609
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08002610void AudioFlinger::MixerThread::invalidateTracks(audio_stream_type_t streamType)
Eric Laurenta553c252009-07-17 12:17:14 -07002611{
Steve Block71f2cf12011-10-20 11:56:00 +01002612 ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d",
Eric Laurent8ed6ed02010-07-13 04:45:46 -07002613 this, streamType, mTracks.size());
Eric Laurenta553c252009-07-17 12:17:14 -07002614 Mutex::Autolock _l(mLock);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07002615
Eric Laurenta553c252009-07-17 12:17:14 -07002616 size_t size = mTracks.size();
2617 for (size_t i = 0; i < size; i++) {
2618 sp<Track> t = mTracks[i];
Glenn Kastenaae26c82012-02-08 12:35:35 -08002619 if (t->streamType() == streamType) {
Eric Laurentae29b762011-03-28 18:37:07 -07002620 android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07002621 t->mCblk->cv.signal();
Eric Laurenta553c252009-07-17 12:17:14 -07002622 }
Eric Laurent53334cd2010-06-23 17:38:20 -07002623 }
2624}
Eric Laurenta553c252009-07-17 12:17:14 -07002625
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08002626void AudioFlinger::PlaybackThread::setStreamValid(audio_stream_type_t streamType, bool valid)
Eric Laurent05ce0942011-08-30 10:18:54 -07002627{
Steve Block71f2cf12011-10-20 11:56:00 +01002628 ALOGV ("PlaybackThread::setStreamValid() thread %p, streamType %d, valid %d",
Eric Laurent05ce0942011-08-30 10:18:54 -07002629 this, streamType, valid);
2630 Mutex::Autolock _l(mLock);
2631
2632 mStreamTypes[streamType].valid = valid;
2633}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634
Eric Laurenta553c252009-07-17 12:17:14 -07002635// getTrackName_l() must be called with ThreadBase::mLock held
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002636int AudioFlinger::MixerThread::getTrackName_l()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637{
2638 return mAudioMixer->getTrackName();
2639}
2640
Eric Laurenta553c252009-07-17 12:17:14 -07002641// deleteTrackName_l() must be called with ThreadBase::mLock held
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002642void AudioFlinger::MixerThread::deleteTrackName_l(int name)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643{
Steve Block71f2cf12011-10-20 11:56:00 +01002644 ALOGV("remove track (%d) and delete from mixer", name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 mAudioMixer->deleteTrackName(name);
2646}
2647
Eric Laurenta553c252009-07-17 12:17:14 -07002648// checkForNewParameters_l() must be called with ThreadBase::mLock held
2649bool AudioFlinger::MixerThread::checkForNewParameters_l()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650{
Eric Laurenta553c252009-07-17 12:17:14 -07002651 bool reconfig = false;
2652
Eric Laurent8fce46a2009-08-04 09:45:33 -07002653 while (!mNewParameters.isEmpty()) {
Eric Laurenta553c252009-07-17 12:17:14 -07002654 status_t status = NO_ERROR;
Eric Laurent8fce46a2009-08-04 09:45:33 -07002655 String8 keyValuePair = mNewParameters[0];
2656 AudioParameter param = AudioParameter(keyValuePair);
Eric Laurenta553c252009-07-17 12:17:14 -07002657 int value;
Eric Laurent8fce46a2009-08-04 09:45:33 -07002658
Eric Laurenta553c252009-07-17 12:17:14 -07002659 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
2660 reconfig = true;
2661 }
2662 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
Glenn Kasten0a204ed2012-01-12 12:27:51 -08002663 if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) {
Eric Laurenta553c252009-07-17 12:17:14 -07002664 status = BAD_VALUE;
2665 } else {
2666 reconfig = true;
2667 }
2668 }
2669 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -07002670 if (value != AUDIO_CHANNEL_OUT_STEREO) {
Eric Laurenta553c252009-07-17 12:17:14 -07002671 status = BAD_VALUE;
2672 } else {
2673 reconfig = true;
2674 }
2675 }
2676 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
2677 // do not accept frame count changes if tracks are open as the track buffer
Glenn Kastene5fb2632011-12-14 10:28:06 -08002678 // size depends on frame count and correct behavior would not be guaranteed
Eric Laurenta553c252009-07-17 12:17:14 -07002679 // if frame count is changed after track creation
2680 if (!mTracks.isEmpty()) {
2681 status = INVALID_OPERATION;
2682 } else {
2683 reconfig = true;
2684 }
2685 }
Eric Laurent65b65452010-06-01 23:49:17 -07002686 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07002687#ifdef ADD_BATTERY_DATA
Gloria Wang9b3f1522011-02-24 14:51:45 -08002688 // when changing the audio output device, call addBatteryData to notify
2689 // the change
Eric Laurent90681d62011-05-09 12:09:06 -07002690 if ((int)mDevice != value) {
Gloria Wang9b3f1522011-02-24 14:51:45 -08002691 uint32_t params = 0;
2692 // check whether speaker is on
Dima Zavin24fc2fb2011-04-19 22:30:36 -07002693 if (value & AUDIO_DEVICE_OUT_SPEAKER) {
Gloria Wang9b3f1522011-02-24 14:51:45 -08002694 params |= IMediaPlayerService::kBatteryDataSpeakerOn;
2695 }
2696
2697 int deviceWithoutSpeaker
Dima Zavin24fc2fb2011-04-19 22:30:36 -07002698 = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER;
Gloria Wang9b3f1522011-02-24 14:51:45 -08002699 // check if any other device (except speaker) is on
2700 if (value & deviceWithoutSpeaker ) {
2701 params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
2702 }
2703
2704 if (params != 0) {
2705 addBatteryData(params);
2706 }
2707 }
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07002708#endif
Gloria Wang9b3f1522011-02-24 14:51:45 -08002709
Eric Laurent65b65452010-06-01 23:49:17 -07002710 // forward device change to effects that have requested to be
2711 // aware of attached audio device.
2712 mDevice = (uint32_t)value;
2713 for (size_t i = 0; i < mEffectChains.size(); i++) {
Eric Laurent76c40f72010-07-15 12:50:15 -07002714 mEffectChains[i]->setDevice_l(mDevice);
Eric Laurent65b65452010-06-01 23:49:17 -07002715 }
2716 }
2717
Eric Laurenta553c252009-07-17 12:17:14 -07002718 if (status == NO_ERROR) {
Dima Zavin31f188892011-04-18 16:57:27 -07002719 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07002720 keyValuePair.string());
Eric Laurenta553c252009-07-17 12:17:14 -07002721 if (!mStandby && status == INVALID_OPERATION) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07002722 mOutput->stream->common.standby(&mOutput->stream->common);
2723 mStandby = true;
2724 mBytesWritten = 0;
2725 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07002726 keyValuePair.string());
Eric Laurenta553c252009-07-17 12:17:14 -07002727 }
2728 if (status == NO_ERROR && reconfig) {
2729 delete mAudioMixer;
Glenn Kasten2589cf92012-01-27 18:08:45 -08002730 // for safety in case readOutputParameters() accesses mAudioMixer (it doesn't)
2731 mAudioMixer = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07002732 readOutputParameters();
2733 mAudioMixer = new AudioMixer(mFrameCount, mSampleRate);
2734 for (size_t i = 0; i < mTracks.size() ; i++) {
2735 int name = getTrackName_l();
2736 if (name < 0) break;
2737 mTracks[i]->mName = name;
Eric Laurent6f7e0972009-08-10 08:15:12 -07002738 // limit track sample rate to 2 x new output sample rate
2739 if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) {
2740 mTracks[i]->mCblk->sampleRate = 2 * sampleRate();
2741 }
Eric Laurenta553c252009-07-17 12:17:14 -07002742 }
Eric Laurent8fce46a2009-08-04 09:45:33 -07002743 sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
Eric Laurenta553c252009-07-17 12:17:14 -07002744 }
2745 }
Eric Laurent3fdb1262009-11-07 00:01:32 -08002746
2747 mNewParameters.removeAt(0);
2748
Eric Laurenta553c252009-07-17 12:17:14 -07002749 mParamStatus = status;
Eric Laurenta553c252009-07-17 12:17:14 -07002750 mParamCond.signal();
Eric Laurent5f37be32011-09-13 11:40:21 -07002751 // wait for condition with time out in case the thread calling ThreadBase::setParameters()
2752 // already timed out waiting for the status and will never signal the condition.
Glenn Kastencbfe2e12011-12-13 11:04:14 -08002753 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
Eric Laurenta553c252009-07-17 12:17:14 -07002754 }
2755 return reconfig;
2756}
2757
2758status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args)
2759{
2760 const size_t SIZE = 256;
2761 char buffer[SIZE];
2762 String8 result;
2763
2764 PlaybackThread::dumpInternals(fd, args);
2765
2766 snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames());
2767 result.append(buffer);
2768 write(fd, result.string(), result.size());
2769 return NO_ERROR;
2770}
2771
Eric Laurent059b4be2009-11-09 23:32:22 -08002772uint32_t AudioFlinger::MixerThread::idleSleepTimeUs()
2773{
Eric Laurenta54d7d32010-07-29 06:50:24 -07002774 return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
Eric Laurent62443f52009-10-05 20:29:18 -07002775}
2776
Eric Laurent8448a792010-08-18 18:13:17 -07002777uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs()
2778{
2779 return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
2780}
2781
Glenn Kasten0fe52c82012-02-24 14:59:21 -08002782void AudioFlinger::MixerThread::cacheParameters_l()
2783{
2784 PlaybackThread::cacheParameters_l();
2785
2786 // FIXME: Relaxed timing because of a certain device that can't meet latency
2787 // Should be reduced to 2x after the vendor fixes the driver issue
2788 // increase threshold again due to low power audio mode. The way this warning
2789 // threshold is calculated and its usefulness should be reconsidered anyway.
2790 maxPeriod = seconds(mFrameCount) / mSampleRate * 15;
2791}
2792
Eric Laurenta553c252009-07-17 12:17:14 -07002793// ----------------------------------------------------------------------------
Glenn Kasten39d00cb2012-01-17 11:09:42 -08002794AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger,
2795 AudioStreamOut* output, audio_io_handle_t id, uint32_t device)
Glenn Kastenefd511a2012-01-26 10:38:26 -08002796 : PlaybackThread(audioFlinger, output, id, device, DIRECT)
Glenn Kastendc3ac852012-01-25 15:28:08 -08002797 // mLeftVolFloat, mRightVolFloat
2798 // mLeftVolShort, mRightVolShort
Eric Laurenta553c252009-07-17 12:17:14 -07002799{
Eric Laurenta553c252009-07-17 12:17:14 -07002800}
2801
2802AudioFlinger::DirectOutputThread::~DirectOutputThread()
2803{
2804}
2805
Glenn Kasten761415b2012-03-06 11:23:32 -08002806AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l(
2807 Vector< sp<Track> > *tracksToRemove
Glenn Kasten67cb3122012-03-01 17:10:56 -08002808)
Eric Laurenta553c252009-07-17 12:17:14 -07002809{
Glenn Kasten761415b2012-03-06 11:23:32 -08002810 sp<Track> trackToRemove;
2811
Glenn Kastena13446a2012-03-08 07:47:15 -08002812 mixer_state mixerStatus = MIXER_IDLE;
Eric Laurent65b65452010-06-01 23:49:17 -07002813
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002814 // find out which tracks need to be processed
2815 if (mActiveTracks.size() != 0) {
2816 sp<Track> t = mActiveTracks[0].promote();
Glenn Kastena13446a2012-03-08 07:47:15 -08002817 // The track died recently
2818 if (t == 0) return MIXER_IDLE;
Eric Laurenta553c252009-07-17 12:17:14 -07002819
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002820 Track* const track = t.get();
2821 audio_track_cblk_t* cblk = track->cblk();
Eric Laurenta553c252009-07-17 12:17:14 -07002822
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002823 // The first time a track is added we wait
2824 // for all its buffers to be filled before processing it
2825 if (cblk->framesReady() && track->isReady() &&
2826 !track->isPaused() && !track->isTerminated())
2827 {
2828 //ALOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server);
Eric Laurenta553c252009-07-17 12:17:14 -07002829
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002830 if (track->mFillingUpStatus == Track::FS_FILLED) {
2831 track->mFillingUpStatus = Track::FS_ACTIVE;
2832 mLeftVolFloat = mRightVolFloat = 0;
2833 mLeftVolShort = mRightVolShort = 0;
2834 if (track->mState == TrackBase::RESUMING) {
2835 track->mState = TrackBase::ACTIVE;
2836 rampVolume = true;
2837 }
2838 } else if (cblk->server != 0) {
2839 // If the track is stopped before the first frame was mixed,
2840 // do not apply ramp
2841 rampVolume = true;
2842 }
2843 // compute volume for this track
2844 float left, right;
2845 if (track->isMuted() || mMasterMute || track->isPausing() ||
2846 mStreamTypes[track->streamType()].mute) {
2847 left = right = 0;
2848 if (track->isPausing()) {
2849 track->setPaused();
2850 }
2851 } else {
2852 float typeVolume = mStreamTypes[track->streamType()].volume;
2853 float v = mMasterVolume * typeVolume;
2854 uint32_t vlr = cblk->getVolumeLR();
2855 float v_clamped = v * (vlr & 0xFFFF);
2856 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
2857 left = v_clamped/MAX_GAIN;
2858 v_clamped = v * (vlr >> 16);
2859 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
2860 right = v_clamped/MAX_GAIN;
2861 }
Eric Laurenta553c252009-07-17 12:17:14 -07002862
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002863 if (left != mLeftVolFloat || right != mRightVolFloat) {
2864 mLeftVolFloat = left;
2865 mRightVolFloat = right;
Eric Laurenta553c252009-07-17 12:17:14 -07002866
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002867 // If audio HAL implements volume control,
2868 // force software volume to nominal value
2869 if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) {
2870 left = 1.0f;
2871 right = 1.0f;
2872 }
Eric Laurent65b65452010-06-01 23:49:17 -07002873
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002874 // Convert volumes from float to 8.24
2875 uint32_t vl = (uint32_t)(left * (1 << 24));
2876 uint32_t vr = (uint32_t)(right * (1 << 24));
Eric Laurent65b65452010-06-01 23:49:17 -07002877
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002878 // Delegate volume control to effect in track effect chain if needed
2879 // only one effect chain can be present on DirectOutputThread, so if
2880 // there is one, the track is connected to it
2881 if (!mEffectChains.isEmpty()) {
2882 // Do not ramp volume if volume is controlled by effect
2883 if (mEffectChains[0]->setVolume_l(&vl, &vr)) {
Eric Laurent65b65452010-06-01 23:49:17 -07002884 rampVolume = false;
Eric Laurenta553c252009-07-17 12:17:14 -07002885 }
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002886 }
Eric Laurenta553c252009-07-17 12:17:14 -07002887
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002888 // Convert volumes from 8.24 to 4.12 format
2889 uint32_t v_clamped = (vl + (1 << 11)) >> 12;
2890 if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT;
2891 leftVol = (uint16_t)v_clamped;
2892 v_clamped = (vr + (1 << 11)) >> 12;
2893 if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT;
2894 rightVol = (uint16_t)v_clamped;
2895 } else {
2896 leftVol = mLeftVolShort;
2897 rightVol = mRightVolShort;
2898 rampVolume = false;
2899 }
2900
2901 // reset retry count
2902 track->mRetryCount = kMaxTrackRetriesDirect;
Glenn Kastenb8f96762012-03-07 17:05:59 -08002903 mActiveTrack = t;
Glenn Kastena13446a2012-03-08 07:47:15 -08002904 mixerStatus = MIXER_TRACKS_READY;
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002905 } else {
2906 //ALOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server);
2907 if (track->isStopped()) {
2908 track->reset();
2909 }
2910 if (track->isTerminated() || track->isStopped() || track->isPaused()) {
2911 // We have consumed all the buffers of this track.
2912 // Remove it from the list of active tracks.
2913 trackToRemove = track;
2914 } else {
2915 // No buffers for this track. Give it a few chances to
2916 // fill a buffer, then remove it from active list.
2917 if (--(track->mRetryCount) <= 0) {
2918 ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name());
2919 trackToRemove = track;
Eric Laurenta553c252009-07-17 12:17:14 -07002920 } else {
Glenn Kastena13446a2012-03-08 07:47:15 -08002921 mixerStatus = MIXER_TRACKS_ENABLED;
Eric Laurenta553c252009-07-17 12:17:14 -07002922 }
2923 }
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002924 }
2925 }
Eric Laurenta553c252009-07-17 12:17:14 -07002926
Glenn Kasten761415b2012-03-06 11:23:32 -08002927 // FIXME merge this with similar code for removing multiple tracks
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002928 // remove all the tracks that need to be...
2929 if (CC_UNLIKELY(trackToRemove != 0)) {
Glenn Kasten761415b2012-03-06 11:23:32 -08002930 tracksToRemove->add(trackToRemove);
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002931 mActiveTracks.remove(trackToRemove);
2932 if (!mEffectChains.isEmpty()) {
Glenn Kasten81916df2012-03-08 12:18:35 -08002933 ALOGV("stopping track on chain %p for session Id: %d", mEffectChains[0].get(),
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002934 trackToRemove->sessionId());
2935 mEffectChains[0]->decActiveTrackCnt();
2936 }
2937 if (trackToRemove->isTerminated()) {
2938 removeTrack_l(trackToRemove);
2939 }
2940 }
Eric Laurent65b65452010-06-01 23:49:17 -07002941
Glenn Kastena13446a2012-03-08 07:47:15 -08002942 return mixerStatus;
Glenn Kasten67cb3122012-03-01 17:10:56 -08002943}
Eric Laurenta553c252009-07-17 12:17:14 -07002944
Glenn Kasten67cb3122012-03-01 17:10:56 -08002945void AudioFlinger::DirectOutputThread::threadLoop_mix()
2946{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002947 AudioBufferProvider::Buffer buffer;
2948 size_t frameCount = mFrameCount;
2949 int8_t *curBuf = (int8_t *)mMixBuffer;
2950 // output audio to hardware
2951 while (frameCount) {
2952 buffer.frameCount = frameCount;
Glenn Kastenb8f96762012-03-07 17:05:59 -08002953 mActiveTrack->getNextBuffer(&buffer);
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002954 if (CC_UNLIKELY(buffer.raw == NULL)) {
2955 memset(curBuf, 0, frameCount * mFrameSize);
2956 break;
2957 }
2958 memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
2959 frameCount -= buffer.frameCount;
2960 curBuf += buffer.frameCount * mFrameSize;
Glenn Kastenb8f96762012-03-07 17:05:59 -08002961 mActiveTrack->releaseBuffer(&buffer);
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08002962 }
2963 sleepTime = 0;
2964 standbyTime = systemTime() + standbyDelay;
Glenn Kastenb8f96762012-03-07 17:05:59 -08002965 mActiveTrack.clear();
Glenn Kasten9779e9f2012-03-09 12:08:48 -08002966
2967 // apply volume
2968
2969 // Do not apply volume on compressed audio
2970 if (!audio_is_linear_pcm(mFormat)) {
2971 return;
2972 }
2973
2974 // convert to signed 16 bit before volume calculation
2975 if (mFormat == AUDIO_FORMAT_PCM_8_BIT) {
2976 size_t count = mFrameCount * mChannelCount;
2977 uint8_t *src = (uint8_t *)mMixBuffer + count-1;
2978 int16_t *dst = mMixBuffer + count-1;
Glenn Kasten18db49a2012-03-12 16:29:55 -07002979 while (count--) {
Glenn Kasten9779e9f2012-03-09 12:08:48 -08002980 *dst-- = (int16_t)(*src--^0x80) << 8;
2981 }
2982 }
2983
2984 frameCount = mFrameCount;
2985 int16_t *out = mMixBuffer;
2986 if (rampVolume) {
2987 if (mChannelCount == 1) {
2988 int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16;
2989 int32_t vlInc = d / (int32_t)frameCount;
2990 int32_t vl = ((int32_t)mLeftVolShort << 16);
2991 do {
2992 out[0] = clamp16(mul(out[0], vl >> 16) >> 12);
2993 out++;
2994 vl += vlInc;
2995 } while (--frameCount);
2996
2997 } else {
2998 int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16;
2999 int32_t vlInc = d / (int32_t)frameCount;
3000 d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16;
3001 int32_t vrInc = d / (int32_t)frameCount;
3002 int32_t vl = ((int32_t)mLeftVolShort << 16);
3003 int32_t vr = ((int32_t)mRightVolShort << 16);
3004 do {
3005 out[0] = clamp16(mul(out[0], vl >> 16) >> 12);
3006 out[1] = clamp16(mul(out[1], vr >> 16) >> 12);
3007 out += 2;
3008 vl += vlInc;
3009 vr += vrInc;
3010 } while (--frameCount);
3011 }
3012 } else {
3013 if (mChannelCount == 1) {
3014 do {
3015 out[0] = clamp16(mul(out[0], leftVol) >> 12);
3016 out++;
3017 } while (--frameCount);
3018 } else {
3019 do {
3020 out[0] = clamp16(mul(out[0], leftVol) >> 12);
3021 out[1] = clamp16(mul(out[1], rightVol) >> 12);
3022 out += 2;
3023 } while (--frameCount);
3024 }
3025 }
3026
3027 // convert back to unsigned 8 bit after volume calculation
3028 if (mFormat == AUDIO_FORMAT_PCM_8_BIT) {
3029 size_t count = mFrameCount * mChannelCount;
3030 int16_t *src = mMixBuffer;
3031 uint8_t *dst = (uint8_t *)mMixBuffer;
Glenn Kasten18db49a2012-03-12 16:29:55 -07003032 while (count--) {
Glenn Kasten9779e9f2012-03-09 12:08:48 -08003033 *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80;
3034 }
3035 }
3036
3037 mLeftVolShort = leftVol;
3038 mRightVolShort = rightVol;
Glenn Kasten67cb3122012-03-01 17:10:56 -08003039}
3040
3041void AudioFlinger::DirectOutputThread::threadLoop_sleepTime()
3042{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003043 if (sleepTime == 0) {
Glenn Kastena13446a2012-03-08 07:47:15 -08003044 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003045 sleepTime = activeSleepTime;
3046 } else {
3047 sleepTime = idleSleepTime;
3048 }
3049 } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) {
3050 memset (mMixBuffer, 0, mFrameCount * mFrameSize);
3051 sleepTime = 0;
3052 }
Eric Laurenta553c252009-07-17 12:17:14 -07003053}
3054
3055// getTrackName_l() must be called with ThreadBase::mLock held
3056int AudioFlinger::DirectOutputThread::getTrackName_l()
3057{
3058 return 0;
3059}
3060
3061// deleteTrackName_l() must be called with ThreadBase::mLock held
3062void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name)
3063{
3064}
3065
3066// checkForNewParameters_l() must be called with ThreadBase::mLock held
3067bool AudioFlinger::DirectOutputThread::checkForNewParameters_l()
3068{
3069 bool reconfig = false;
3070
Eric Laurent8fce46a2009-08-04 09:45:33 -07003071 while (!mNewParameters.isEmpty()) {
Eric Laurenta553c252009-07-17 12:17:14 -07003072 status_t status = NO_ERROR;
Eric Laurent8fce46a2009-08-04 09:45:33 -07003073 String8 keyValuePair = mNewParameters[0];
3074 AudioParameter param = AudioParameter(keyValuePair);
Eric Laurenta553c252009-07-17 12:17:14 -07003075 int value;
Eric Laurent8fce46a2009-08-04 09:45:33 -07003076
Eric Laurenta553c252009-07-17 12:17:14 -07003077 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
3078 // do not accept frame count changes if tracks are open as the track buffer
3079 // size depends on frame count and correct behavior would not be garantied
3080 // if frame count is changed after track creation
3081 if (!mTracks.isEmpty()) {
3082 status = INVALID_OPERATION;
3083 } else {
3084 reconfig = true;
3085 }
3086 }
3087 if (status == NO_ERROR) {
Dima Zavin31f188892011-04-18 16:57:27 -07003088 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07003089 keyValuePair.string());
Eric Laurenta553c252009-07-17 12:17:14 -07003090 if (!mStandby && status == INVALID_OPERATION) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07003091 mOutput->stream->common.standby(&mOutput->stream->common);
3092 mStandby = true;
3093 mBytesWritten = 0;
3094 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07003095 keyValuePair.string());
Eric Laurenta553c252009-07-17 12:17:14 -07003096 }
3097 if (status == NO_ERROR && reconfig) {
3098 readOutputParameters();
Eric Laurent8fce46a2009-08-04 09:45:33 -07003099 sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
Eric Laurenta553c252009-07-17 12:17:14 -07003100 }
3101 }
Eric Laurent3fdb1262009-11-07 00:01:32 -08003102
3103 mNewParameters.removeAt(0);
3104
Eric Laurenta553c252009-07-17 12:17:14 -07003105 mParamStatus = status;
Eric Laurenta553c252009-07-17 12:17:14 -07003106 mParamCond.signal();
Eric Laurent5f37be32011-09-13 11:40:21 -07003107 // wait for condition with time out in case the thread calling ThreadBase::setParameters()
3108 // already timed out waiting for the status and will never signal the condition.
Glenn Kastencbfe2e12011-12-13 11:04:14 -08003109 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
Eric Laurenta553c252009-07-17 12:17:14 -07003110 }
3111 return reconfig;
The Android Open Source Projectf1e484a2009-01-22 00:13:42 -08003112}
3113
Eric Laurent059b4be2009-11-09 23:32:22 -08003114uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs()
Eric Laurent62443f52009-10-05 20:29:18 -07003115{
3116 uint32_t time;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07003117 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent44331692011-12-05 09:47:19 -08003118 time = PlaybackThread::activeSleepTimeUs();
Eric Laurent059b4be2009-11-09 23:32:22 -08003119 } else {
3120 time = 10000;
3121 }
3122 return time;
3123}
3124
3125uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs()
3126{
3127 uint32_t time;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07003128 if (audio_is_linear_pcm(mFormat)) {
Eric Laurenta54d7d32010-07-29 06:50:24 -07003129 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
Eric Laurent62443f52009-10-05 20:29:18 -07003130 } else {
3131 time = 10000;
3132 }
3133 return time;
3134}
3135
Eric Laurent8448a792010-08-18 18:13:17 -07003136uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs()
3137{
3138 uint32_t time;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07003139 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent8448a792010-08-18 18:13:17 -07003140 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
3141 } else {
3142 time = 10000;
3143 }
3144 return time;
3145}
3146
Glenn Kasten0fe52c82012-02-24 14:59:21 -08003147void AudioFlinger::DirectOutputThread::cacheParameters_l()
3148{
3149 PlaybackThread::cacheParameters_l();
3150
3151 // use shorter standby delay as on normal output to release
3152 // hardware resources as soon as possible
3153 standbyDelay = microseconds(activeSleepTime*2);
3154}
Eric Laurent8448a792010-08-18 18:13:17 -07003155
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003156// ----------------------------------------------------------------------------
3157
Glenn Kastenefd511a2012-01-26 10:38:26 -08003158AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08003159 AudioFlinger::MixerThread* mainThread, audio_io_handle_t id)
Glenn Kastenefd511a2012-01-26 10:38:26 -08003160 : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device(), DUPLICATING),
3161 mWaitTimeMs(UINT_MAX)
Eric Laurenta553c252009-07-17 12:17:14 -07003162{
Eric Laurenta553c252009-07-17 12:17:14 -07003163 addOutputTrack(mainThread);
3164}
3165
3166AudioFlinger::DuplicatingThread::~DuplicatingThread()
3167{
Eric Laurent0a080292009-12-07 10:53:10 -08003168 for (size_t i = 0; i < mOutputTracks.size(); i++) {
3169 mOutputTracks[i]->destroy();
3170 }
Eric Laurenta553c252009-07-17 12:17:14 -07003171}
3172
Glenn Kasten67cb3122012-03-01 17:10:56 -08003173void AudioFlinger::DuplicatingThread::threadLoop_mix()
Eric Laurenta553c252009-07-17 12:17:14 -07003174{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003175 // mix buffers...
3176 if (outputsReady(outputTracks)) {
3177 mAudioMixer->process(AudioBufferProvider::kInvalidPTS);
3178 } else {
3179 memset(mMixBuffer, 0, mixBufferSize);
3180 }
3181 sleepTime = 0;
3182 writeFrames = mFrameCount;
Glenn Kasten67cb3122012-03-01 17:10:56 -08003183}
3184
3185void AudioFlinger::DuplicatingThread::threadLoop_sleepTime()
3186{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003187 if (sleepTime == 0) {
Glenn Kastena13446a2012-03-08 07:47:15 -08003188 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003189 sleepTime = activeSleepTime;
3190 } else {
3191 sleepTime = idleSleepTime;
3192 }
3193 } else if (mBytesWritten != 0) {
3194 // flush remaining overflow buffers in output tracks
3195 for (size_t i = 0; i < outputTracks.size(); i++) {
3196 if (outputTracks[i]->isActive()) {
3197 sleepTime = 0;
3198 writeFrames = 0;
3199 memset(mMixBuffer, 0, mixBufferSize);
3200 break;
Eric Laurenta553c252009-07-17 12:17:14 -07003201 }
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003202 }
3203 }
Glenn Kasten67cb3122012-03-01 17:10:56 -08003204}
Eric Laurentf69a3f82009-09-22 00:35:48 -07003205
Glenn Kasten67cb3122012-03-01 17:10:56 -08003206void AudioFlinger::DuplicatingThread::threadLoop_write()
3207{
Glenn Kasten0fe52c82012-02-24 14:59:21 -08003208 standbyTime = systemTime() + standbyDelay;
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003209 for (size_t i = 0; i < outputTracks.size(); i++) {
3210 outputTracks[i]->write(mMixBuffer, writeFrames);
3211 }
3212 mBytesWritten += mixBufferSize;
Glenn Kasten67cb3122012-03-01 17:10:56 -08003213}
Glenn Kastenbea6b952012-02-29 07:57:06 -08003214
Glenn Kasten67cb3122012-03-01 17:10:56 -08003215void AudioFlinger::DuplicatingThread::threadLoop_standby()
3216{
Glenn Kasten26bbc5c2012-03-06 11:30:57 -08003217 // DuplicatingThread implements standby by stopping all tracks
3218 for (size_t i = 0; i < outputTracks.size(); i++) {
3219 outputTracks[i]->stop();
3220 }
Eric Laurenta553c252009-07-17 12:17:14 -07003221}
3222
Glenn Kasten08d2c042012-03-06 11:28:04 -08003223void AudioFlinger::DuplicatingThread::saveOutputTracks()
3224{
3225 outputTracks = mOutputTracks;
3226}
3227
3228void AudioFlinger::DuplicatingThread::clearOutputTracks()
3229{
3230 outputTracks.clear();
3231}
3232
Eric Laurenta553c252009-07-17 12:17:14 -07003233void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread)
3234{
Glenn Kasten91540ae2012-02-24 14:12:20 -08003235 Mutex::Autolock _l(mLock);
Glenn Kastenb3db2132012-01-19 08:59:58 -08003236 // FIXME explain this formula
Eric Laurenta553c252009-07-17 12:17:14 -07003237 int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate();
Glenn Kasten685c9ce2012-01-20 13:32:16 -08003238 OutputTrack *outputTrack = new OutputTrack(thread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003239 this,
Eric Laurenta553c252009-07-17 12:17:14 -07003240 mSampleRate,
3241 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003242 mChannelMask,
Eric Laurenta553c252009-07-17 12:17:14 -07003243 frameCount);
Eric Laurent6c30a712009-08-10 23:22:32 -07003244 if (outputTrack->cblk() != NULL) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -07003245 thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f);
Eric Laurent6c30a712009-08-10 23:22:32 -07003246 mOutputTracks.add(outputTrack);
Steve Block71f2cf12011-10-20 11:56:00 +01003247 ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread);
Glenn Kasten2bad67f2012-03-06 11:24:48 -08003248 updateWaitTime_l();
Eric Laurent6c30a712009-08-10 23:22:32 -07003249 }
Eric Laurenta553c252009-07-17 12:17:14 -07003250}
3251
3252void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
3253{
3254 Mutex::Autolock _l(mLock);
3255 for (size_t i = 0; i < mOutputTracks.size(); i++) {
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08003256 if (mOutputTracks[i]->thread() == thread) {
Eric Laurent6c30a712009-08-10 23:22:32 -07003257 mOutputTracks[i]->destroy();
Eric Laurenta553c252009-07-17 12:17:14 -07003258 mOutputTracks.removeAt(i);
Glenn Kasten2bad67f2012-03-06 11:24:48 -08003259 updateWaitTime_l();
Eric Laurenta553c252009-07-17 12:17:14 -07003260 return;
3261 }
3262 }
Steve Block71f2cf12011-10-20 11:56:00 +01003263 ALOGV("removeOutputTrack(): unkonwn thread: %p", thread);
Eric Laurenta553c252009-07-17 12:17:14 -07003264}
3265
Glenn Kasten2bad67f2012-03-06 11:24:48 -08003266// caller must hold mLock
3267void AudioFlinger::DuplicatingThread::updateWaitTime_l()
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003268{
3269 mWaitTimeMs = UINT_MAX;
3270 for (size_t i = 0; i < mOutputTracks.size(); i++) {
3271 sp<ThreadBase> strong = mOutputTracks[i]->thread().promote();
Glenn Kasten706b6182012-01-20 13:44:40 -08003272 if (strong != 0) {
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003273 uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate();
3274 if (waitTimeMs < mWaitTimeMs) {
3275 mWaitTimeMs = waitTimeMs;
3276 }
3277 }
3278 }
3279}
3280
3281
Glenn Kasten0fccd352012-02-24 15:42:17 -08003282bool AudioFlinger::DuplicatingThread::outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks)
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003283{
3284 for (size_t i = 0; i < outputTracks.size(); i++) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07003285 sp<ThreadBase> thread = outputTracks[i]->thread().promote();
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003286 if (thread == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003287 ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get());
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003288 return false;
3289 }
3290 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3291 if (playbackThread->standby() && !playbackThread->isSuspended()) {
Steve Block71f2cf12011-10-20 11:56:00 +01003292 ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get());
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08003293 return false;
3294 }
3295 }
3296 return true;
3297}
3298
3299uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs()
3300{
3301 return (mWaitTimeMs * 1000) / 2;
3302}
3303
Glenn Kasten0fe52c82012-02-24 14:59:21 -08003304void AudioFlinger::DuplicatingThread::cacheParameters_l()
3305{
3306 // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first
3307 updateWaitTime_l();
3308
3309 MixerThread::cacheParameters_l();
3310}
3311
Eric Laurenta553c252009-07-17 12:17:14 -07003312// ----------------------------------------------------------------------------
3313
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003314// TrackBase constructor must be called with AudioFlinger::mLock held
Eric Laurenta553c252009-07-17 12:17:14 -07003315AudioFlinger::ThreadBase::TrackBase::TrackBase(
Glenn Kasten685c9ce2012-01-20 13:32:16 -08003316 ThreadBase *thread,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003317 const sp<Client>& client,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003318 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08003319 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003320 uint32_t channelMask,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003321 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -07003322 const sp<IMemory>& sharedBuffer,
3323 int sessionId)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003324 : RefBase(),
Eric Laurenta553c252009-07-17 12:17:14 -07003325 mThread(thread),
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003326 mClient(client),
Glenn Kastendc3ac852012-01-25 15:28:08 -08003327 mCblk(NULL),
3328 // mBuffer
3329 // mBufferEnd
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003330 mFrameCount(0),
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003331 mState(IDLE),
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003332 mFormat(format),
Glenn Kasten35269822012-02-21 10:35:56 -08003333 mStepServerFailed(false),
Eric Laurent65b65452010-06-01 23:49:17 -07003334 mSessionId(sessionId)
Glenn Kastendc3ac852012-01-25 15:28:08 -08003335 // mChannelCount
3336 // mChannelMask
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003337{
Steve Block71f2cf12011-10-20 11:56:00 +01003338 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003339
Steve Block5baa3a62011-12-20 16:23:08 +00003340 // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize);
Glenn Kasten18db49a2012-03-12 16:29:55 -07003341 size_t size = sizeof(audio_track_cblk_t);
3342 uint8_t channelCount = popcount(channelMask);
3343 size_t bufferSize = frameCount*channelCount*sizeof(int16_t);
3344 if (sharedBuffer == 0) {
3345 size += bufferSize;
3346 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003347
Glenn Kasten18db49a2012-03-12 16:29:55 -07003348 if (client != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 mCblkMemory = client->heap()->allocate(size);
3350 if (mCblkMemory != 0) {
3351 mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer());
Glenn Kasten3694ec12012-01-27 16:47:15 -08003352 if (mCblk != NULL) { // construct the shared structure in-place.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 new(mCblk) audio_track_cblk_t();
3354 // clear all buffers
3355 mCblk->frameCount = frameCount;
Eric Laurent88e209d2009-07-07 07:10:45 -07003356 mCblk->sampleRate = sampleRate;
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003357 mChannelCount = channelCount;
3358 mChannelMask = channelMask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 if (sharedBuffer == 0) {
3360 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
3361 memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
3362 // Force underrun condition to avoid false underrun callback until first data is
Eric Laurent4712baa2010-09-30 16:12:31 -07003363 // written to buffer (other flags are cleared)
Eric Laurenteb8f850d2010-05-14 03:26:45 -07003364 mCblk->flags = CBLK_UNDERRUN_ON;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 } else {
3366 mBuffer = sharedBuffer->pointer();
3367 }
3368 mBufferEnd = (uint8_t *)mBuffer + bufferSize;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 } else {
Steve Block3762c312012-01-06 19:20:56 +00003371 ALOGE("not enough memory for AudioTrack size=%u", size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 client->heap()->dump("AudioTrack");
3373 return;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003374 }
Glenn Kasten18db49a2012-03-12 16:29:55 -07003375 } else {
3376 mCblk = (audio_track_cblk_t *)(new uint8_t[size]);
Glenn Kastenf743e1f2012-03-14 12:56:26 -07003377 // construct the shared structure in-place.
3378 new(mCblk) audio_track_cblk_t();
3379 // clear all buffers
3380 mCblk->frameCount = frameCount;
3381 mCblk->sampleRate = sampleRate;
3382 mChannelCount = channelCount;
3383 mChannelMask = channelMask;
3384 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
3385 memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
3386 // Force underrun condition to avoid false underrun callback until first data is
3387 // written to buffer (other flags are cleared)
3388 mCblk->flags = CBLK_UNDERRUN_ON;
3389 mBufferEnd = (uint8_t *)mBuffer + bufferSize;
Glenn Kasten18db49a2012-03-12 16:29:55 -07003390 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003391}
3392
Eric Laurent2bb6b2a2009-09-16 06:02:45 -07003393AudioFlinger::ThreadBase::TrackBase::~TrackBase()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003394{
Glenn Kasten3694ec12012-01-27 16:47:15 -08003395 if (mCblk != NULL) {
Glenn Kastend9d68dc2012-02-03 10:24:48 -08003396 if (mClient == 0) {
Eric Laurenta553c252009-07-17 12:17:14 -07003397 delete mCblk;
Glenn Kastend9d68dc2012-02-03 10:24:48 -08003398 } else {
3399 mCblk->~audio_track_cblk_t(); // destroy our shared-structure.
Eric Laurenta553c252009-07-17 12:17:14 -07003400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 }
Glenn Kastenf31d0502012-01-25 15:27:15 -08003402 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Glenn Kasten706b6182012-01-20 13:44:40 -08003403 if (mClient != 0) {
Glenn Kastendc3ac852012-01-25 15:28:08 -08003404 // Client destructor must run with AudioFlinger mutex locked
Eric Laurentb9481d82009-09-17 05:12:56 -07003405 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
Glenn Kasten706b6182012-01-20 13:44:40 -08003406 // If the client's reference count drops to zero, the associated destructor
3407 // must run with AudioFlinger lock held. Thus the explicit clear() rather than
3408 // relying on the automatic clear() at end of scope.
Eric Laurentb9481d82009-09-17 05:12:56 -07003409 mClient.clear();
3410 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003411}
3412
Glenn Kastenc2db1192012-02-22 10:47:35 -08003413// AudioBufferProvider interface
3414// getNextBuffer() = 0;
3415// This implementation of releaseBuffer() is used by Track and RecordTrack, but not TimedTrack
Eric Laurent2bb6b2a2009-09-16 06:02:45 -07003416void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003417{
Glenn Kastenc434c902011-12-13 11:53:26 -08003418 buffer->raw = NULL;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003419 mFrameCount = buffer->frameCount;
Glenn Kastenc2db1192012-02-22 10:47:35 -08003420 (void) step(); // ignore return value of step()
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003421 buffer->frameCount = 0;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003422}
3423
Eric Laurent2bb6b2a2009-09-16 06:02:45 -07003424bool AudioFlinger::ThreadBase::TrackBase::step() {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003425 bool result;
3426 audio_track_cblk_t* cblk = this->cblk();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003427
3428 result = cblk->stepServer(mFrameCount);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003429 if (!result) {
Steve Block71f2cf12011-10-20 11:56:00 +01003430 ALOGV("stepServer failed acquiring cblk mutex");
Glenn Kasten35269822012-02-21 10:35:56 -08003431 mStepServerFailed = true;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003432 }
3433 return result;
3434}
3435
Eric Laurent2bb6b2a2009-09-16 06:02:45 -07003436void AudioFlinger::ThreadBase::TrackBase::reset() {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003437 audio_track_cblk_t* cblk = this->cblk();
3438
3439 cblk->user = 0;
3440 cblk->server = 0;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003441 cblk->userBase = 0;
3442 cblk->serverBase = 0;
Glenn Kasten35269822012-02-21 10:35:56 -08003443 mStepServerFailed = false;
Steve Block71f2cf12011-10-20 11:56:00 +01003444 ALOGV("TrackBase::reset");
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003445}
3446
Eric Laurent2bb6b2a2009-09-16 06:02:45 -07003447int AudioFlinger::ThreadBase::TrackBase::sampleRate() const {
The Android Open Source Project10592532009-03-18 17:39:46 -07003448 return (int)mCblk->sampleRate;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003449}
3450
Eric Laurent2bb6b2a2009-09-16 06:02:45 -07003451void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003452 audio_track_cblk_t* cblk = this->cblk();
Glenn Kastenfaf354d2012-01-11 09:48:27 -08003453 size_t frameSize = cblk->frameSize;
3454 int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize;
3455 int8_t *bufferEnd = bufferStart + frames * frameSize;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003456
3457 // Check validity of returned pointer in case the track control block would have been corrupted.
Eric Laurenta553c252009-07-17 12:17:14 -07003458 if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd ||
Glenn Kastenfaf354d2012-01-11 09:48:27 -08003459 ((unsigned long)bufferStart & (unsigned long)(frameSize - 1))) {
Steve Block3762c312012-01-06 19:20:56 +00003460 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 -07003461 server %d, serverBase %d, user %d, userBase %d",
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003462 bufferStart, bufferEnd, mBuffer, mBufferEnd,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003463 cblk->server, cblk->serverBase, cblk->user, cblk->userBase);
Glenn Kasten3694ec12012-01-27 16:47:15 -08003464 return NULL;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003465 }
3466
3467 return bufferStart;
3468}
3469
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003470// ----------------------------------------------------------------------------
3471
Eric Laurenta553c252009-07-17 12:17:14 -07003472// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
3473AudioFlinger::PlaybackThread::Track::Track(
Glenn Kasten685c9ce2012-01-20 13:32:16 -08003474 PlaybackThread *thread,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003475 const sp<Client>& client,
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08003476 audio_stream_type_t streamType,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003477 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08003478 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003479 uint32_t channelMask,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003480 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -07003481 const sp<IMemory>& sharedBuffer,
3482 int sessionId)
Glenn Kasten35269822012-02-21 10:35:56 -08003483 : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer, sessionId),
Glenn Kastene9b2b3b2012-03-19 11:14:37 -07003484 mMute(false),
3485 // mFillingUpStatus ?
3486 // mRetryCount initialized later when needed
3487 mSharedBuffer(sharedBuffer),
3488 mStreamType(streamType),
3489 mName(-1), // see note below
3490 mMainBuffer(thread->mixBuffer()),
3491 mAuxBuffer(NULL),
Eric Laurenta92ebfa2010-08-31 13:50:07 -07003492 mAuxEffectId(0), mHasVolumeController(false)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003493{
Eric Laurent8a77a992009-09-09 05:16:08 -07003494 if (mCblk != NULL) {
Eric Laurent8a77a992009-09-09 05:16:08 -07003495 // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of
3496 // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack
Eric Laurent09508612011-07-21 19:35:01 -07003497 mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t);
Glenn Kastene9b2b3b2012-03-19 11:14:37 -07003498 // to avoid leaking a track name, do not allocate one unless there is an mCblk
3499 mName = thread->getTrackName_l();
3500 if (mName < 0) {
3501 ALOGE("no more track names available");
3502 }
Eric Laurenta553c252009-07-17 12:17:14 -07003503 }
Glenn Kastene9b2b3b2012-03-19 11:14:37 -07003504 ALOGV("Track constructor name %d, calling pid %d", mName, IPCThreadState::self()->getCallingPid());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003505}
3506
Eric Laurenta553c252009-07-17 12:17:14 -07003507AudioFlinger::PlaybackThread::Track::~Track()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003508{
Steve Block71f2cf12011-10-20 11:56:00 +01003509 ALOGV("PlaybackThread::Track destructor");
Eric Laurenta553c252009-07-17 12:17:14 -07003510 sp<ThreadBase> thread = mThread.promote();
3511 if (thread != 0) {
Eric Laurentac196e12009-12-01 02:17:41 -08003512 Mutex::Autolock _l(thread->mLock);
Eric Laurenta553c252009-07-17 12:17:14 -07003513 mState = TERMINATED;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003514 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003515}
3516
Eric Laurenta553c252009-07-17 12:17:14 -07003517void AudioFlinger::PlaybackThread::Track::destroy()
3518{
3519 // NOTE: destroyTrack_l() can remove a strong reference to this Track
3520 // by removing it from mTracks vector, so there is a risk that this Tracks's
Glenn Kastenb3db2132012-01-19 08:59:58 -08003521 // destructor is called. As the destructor needs to lock mLock,
Eric Laurenta553c252009-07-17 12:17:14 -07003522 // we must acquire a strong reference on this Track before locking mLock
3523 // here so that the destructor is called only when exiting this function.
3524 // On the other hand, as long as Track::destroy() is only called by
3525 // TrackHandle destructor, the TrackHandle still holds a strong ref on
3526 // this Track with its member mTrack.
3527 sp<Track> keep(this);
3528 { // scope for mLock
3529 sp<ThreadBase> thread = mThread.promote();
3530 if (thread != 0) {
Eric Laurentac196e12009-12-01 02:17:41 -08003531 if (!isOutputTrack()) {
3532 if (mState == ACTIVE || mState == RESUMING) {
Glenn Kastenaae26c82012-02-08 12:35:35 -08003533 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
Gloria Wang9b3f1522011-02-24 14:51:45 -08003534
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003535#ifdef ADD_BATTERY_DATA
Gloria Wang9b3f1522011-02-24 14:51:45 -08003536 // to track the speaker usage
3537 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003538#endif
Eric Laurentac196e12009-12-01 02:17:41 -08003539 }
3540 AudioSystem::releaseOutput(thread->id());
Eric Laurent49f02be2009-11-19 09:00:56 -08003541 }
Eric Laurenta553c252009-07-17 12:17:14 -07003542 Mutex::Autolock _l(thread->mLock);
3543 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3544 playbackThread->destroyTrack_l(this);
3545 }
3546 }
3547}
3548
3549void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003550{
Glenn Kastenbc4de882012-01-17 14:39:34 -08003551 uint32_t vlr = mCblk->getVolumeLR();
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003552 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 -07003553 mName - AudioMixer::TRACK0,
Glenn Kasten81211142012-02-05 18:09:08 -08003554 (mClient == 0) ? getpid_cached : mClient->pid(),
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003555 mStreamType,
3556 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07003557 mChannelMask,
Eric Laurent65b65452010-06-01 23:49:17 -07003558 mSessionId,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003559 mFrameCount,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003560 mState,
3561 mMute,
3562 mFillingUpStatus,
3563 mCblk->sampleRate,
Glenn Kasten0632bad2012-01-17 12:20:54 -08003564 vlr & 0xFFFF,
3565 vlr >> 16,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003566 mCblk->server,
Eric Laurent65b65452010-06-01 23:49:17 -07003567 mCblk->user,
3568 (int)mMainBuffer,
3569 (int)mAuxBuffer);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003570}
3571
Glenn Kastenc2db1192012-02-22 10:47:35 -08003572// AudioBufferProvider interface
John Grossmand8cf2962012-02-08 16:37:41 -08003573status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(
Glenn Kasten18db49a2012-03-12 16:29:55 -07003574 AudioBufferProvider::Buffer* buffer, int64_t pts)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003575{
Glenn Kasten18db49a2012-03-12 16:29:55 -07003576 audio_track_cblk_t* cblk = this->cblk();
3577 uint32_t framesReady;
3578 uint32_t framesReq = buffer->frameCount;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003579
Glenn Kasten18db49a2012-03-12 16:29:55 -07003580 // Check if last stepServer failed, try to step now
3581 if (mStepServerFailed) {
3582 if (!step()) goto getNextBuffer_exit;
3583 ALOGV("stepServer recovered");
3584 mStepServerFailed = false;
3585 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003586
Glenn Kasten18db49a2012-03-12 16:29:55 -07003587 framesReady = cblk->framesReady();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003588
Glenn Kasten18db49a2012-03-12 16:29:55 -07003589 if (CC_LIKELY(framesReady)) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003590 uint32_t s = cblk->server;
3591 uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
3592
3593 bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd;
3594 if (framesReq > framesReady) {
3595 framesReq = framesReady;
3596 }
3597 if (s + framesReq > bufferEnd) {
3598 framesReq = bufferEnd - s;
3599 }
3600
Glenn Kasten18db49a2012-03-12 16:29:55 -07003601 buffer->raw = getBuffer(s, framesReq);
3602 if (buffer->raw == NULL) goto getNextBuffer_exit;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003603
Glenn Kasten18db49a2012-03-12 16:29:55 -07003604 buffer->frameCount = framesReq;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003605 return NO_ERROR;
Glenn Kasten18db49a2012-03-12 16:29:55 -07003606 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003607
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003608getNextBuffer_exit:
Glenn Kasten18db49a2012-03-12 16:29:55 -07003609 buffer->raw = NULL;
3610 buffer->frameCount = 0;
3611 ALOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get());
3612 return NOT_ENOUGH_DATA;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003613}
3614
Glenn Kasten18db49a2012-03-12 16:29:55 -07003615uint32_t AudioFlinger::PlaybackThread::Track::framesReady() const {
John Grossmand8cf2962012-02-08 16:37:41 -08003616 return mCblk->framesReady();
3617}
3618
Eric Laurenta553c252009-07-17 12:17:14 -07003619bool AudioFlinger::PlaybackThread::Track::isReady() const {
Eric Laurent9a30fc12010-10-05 14:41:42 -07003620 if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003621
John Grossmand8cf2962012-02-08 16:37:41 -08003622 if (framesReady() >= mCblk->frameCount ||
Eric Laurenteb8f850d2010-05-14 03:26:45 -07003623 (mCblk->flags & CBLK_FORCEREADY_MSK)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003624 mFillingUpStatus = FS_FILLED;
Eric Laurentae29b762011-03-28 18:37:07 -07003625 android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003626 return true;
3627 }
3628 return false;
3629}
3630
Glenn Kasten6a20b262012-02-02 10:56:47 -08003631status_t AudioFlinger::PlaybackThread::Track::start(pid_t tid)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003632{
Eric Laurent49f02be2009-11-19 09:00:56 -08003633 status_t status = NO_ERROR;
Glenn Kasten6a20b262012-02-02 10:56:47 -08003634 ALOGV("start(%d), calling pid %d session %d tid %d",
3635 mName, IPCThreadState::self()->getCallingPid(), mSessionId, tid);
Eric Laurenta553c252009-07-17 12:17:14 -07003636 sp<ThreadBase> thread = mThread.promote();
3637 if (thread != 0) {
3638 Mutex::Autolock _l(thread->mLock);
Glenn Kasten56356202012-01-26 13:39:18 -08003639 track_state state = mState;
Eric Laurent49f02be2009-11-19 09:00:56 -08003640 // here the track could be either new, or restarted
3641 // in both cases "unstop" the track
3642 if (mState == PAUSED) {
3643 mState = TrackBase::RESUMING;
Steve Block71f2cf12011-10-20 11:56:00 +01003644 ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this);
Eric Laurent49f02be2009-11-19 09:00:56 -08003645 } else {
3646 mState = TrackBase::ACTIVE;
Steve Block71f2cf12011-10-20 11:56:00 +01003647 ALOGV("? => ACTIVE (%d) on thread %p", mName, this);
Eric Laurent49f02be2009-11-19 09:00:56 -08003648 }
3649
3650 if (!isOutputTrack() && state != ACTIVE && state != RESUMING) {
3651 thread->mLock.unlock();
Glenn Kastenaae26c82012-02-08 12:35:35 -08003652 status = AudioSystem::startOutput(thread->id(), mStreamType, mSessionId);
Eric Laurent49f02be2009-11-19 09:00:56 -08003653 thread->mLock.lock();
Gloria Wang9b3f1522011-02-24 14:51:45 -08003654
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003655#ifdef ADD_BATTERY_DATA
Gloria Wang9b3f1522011-02-24 14:51:45 -08003656 // to track the speaker usage
3657 if (status == NO_ERROR) {
3658 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart);
3659 }
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003660#endif
Eric Laurent49f02be2009-11-19 09:00:56 -08003661 }
3662 if (status == NO_ERROR) {
3663 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3664 playbackThread->addTrack_l(this);
3665 } else {
3666 mState = state;
3667 }
3668 } else {
3669 status = BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -07003670 }
Eric Laurent49f02be2009-11-19 09:00:56 -08003671 return status;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003672}
3673
Eric Laurenta553c252009-07-17 12:17:14 -07003674void AudioFlinger::PlaybackThread::Track::stop()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003675{
Glenn Kasten7ed4f0c2012-02-03 11:10:00 -08003676 ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
Eric Laurenta553c252009-07-17 12:17:14 -07003677 sp<ThreadBase> thread = mThread.promote();
3678 if (thread != 0) {
3679 Mutex::Autolock _l(thread->mLock);
Glenn Kasten56356202012-01-26 13:39:18 -08003680 track_state state = mState;
Eric Laurenta553c252009-07-17 12:17:14 -07003681 if (mState > STOPPED) {
3682 mState = STOPPED;
3683 // If the track is not active (PAUSED and buffers full), flush buffers
3684 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3685 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
3686 reset();
3687 }
Steve Block71f2cf12011-10-20 11:56:00 +01003688 ALOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003689 }
Eric Laurent49f02be2009-11-19 09:00:56 -08003690 if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) {
3691 thread->mLock.unlock();
Glenn Kastenaae26c82012-02-08 12:35:35 -08003692 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
Eric Laurent49f02be2009-11-19 09:00:56 -08003693 thread->mLock.lock();
Gloria Wang9b3f1522011-02-24 14:51:45 -08003694
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003695#ifdef ADD_BATTERY_DATA
Gloria Wang9b3f1522011-02-24 14:51:45 -08003696 // to track the speaker usage
3697 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003698#endif
Eric Laurent49f02be2009-11-19 09:00:56 -08003699 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003700 }
3701}
3702
Eric Laurenta553c252009-07-17 12:17:14 -07003703void AudioFlinger::PlaybackThread::Track::pause()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003704{
Glenn Kasten7ed4f0c2012-02-03 11:10:00 -08003705 ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
Eric Laurenta553c252009-07-17 12:17:14 -07003706 sp<ThreadBase> thread = mThread.promote();
3707 if (thread != 0) {
3708 Mutex::Autolock _l(thread->mLock);
3709 if (mState == ACTIVE || mState == RESUMING) {
3710 mState = PAUSING;
Steve Block71f2cf12011-10-20 11:56:00 +01003711 ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get());
Eric Laurent49f02be2009-11-19 09:00:56 -08003712 if (!isOutputTrack()) {
3713 thread->mLock.unlock();
Glenn Kastenaae26c82012-02-08 12:35:35 -08003714 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
Eric Laurent49f02be2009-11-19 09:00:56 -08003715 thread->mLock.lock();
Gloria Wang9b3f1522011-02-24 14:51:45 -08003716
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003717#ifdef ADD_BATTERY_DATA
Gloria Wang9b3f1522011-02-24 14:51:45 -08003718 // to track the speaker usage
3719 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Glenn Kasten0e9ce2e2012-03-13 17:55:35 -07003720#endif
Eric Laurent49f02be2009-11-19 09:00:56 -08003721 }
Eric Laurenta553c252009-07-17 12:17:14 -07003722 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003723 }
3724}
3725
Eric Laurenta553c252009-07-17 12:17:14 -07003726void AudioFlinger::PlaybackThread::Track::flush()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003727{
Steve Block71f2cf12011-10-20 11:56:00 +01003728 ALOGV("flush(%d)", mName);
Eric Laurenta553c252009-07-17 12:17:14 -07003729 sp<ThreadBase> thread = mThread.promote();
3730 if (thread != 0) {
3731 Mutex::Autolock _l(thread->mLock);
3732 if (mState != STOPPED && mState != PAUSED && mState != PAUSING) {
3733 return;
3734 }
3735 // No point remaining in PAUSED state after a flush => go to
3736 // STOPPED state
3737 mState = STOPPED;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003738
Eric Laurentae29b762011-03-28 18:37:07 -07003739 // do not reset the track if it is still in the process of being stopped or paused.
3740 // this will be done by prepareTracks_l() when the track is stopped.
3741 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3742 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
3743 reset();
3744 }
Eric Laurenta553c252009-07-17 12:17:14 -07003745 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003746}
3747
Eric Laurenta553c252009-07-17 12:17:14 -07003748void AudioFlinger::PlaybackThread::Track::reset()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003749{
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003750 // Do not reset twice to avoid discarding data written just after a flush and before
3751 // the audioflinger thread detects the track is stopped.
3752 if (!mResetDone) {
3753 TrackBase::reset();
3754 // Force underrun condition to avoid false underrun callback until first data is
3755 // written to buffer
Eric Laurentae29b762011-03-28 18:37:07 -07003756 android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags);
3757 android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags);
Eric Laurenta553c252009-07-17 12:17:14 -07003758 mFillingUpStatus = FS_FILLING;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08003759 mResetDone = true;
3760 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003761}
3762
Eric Laurenta553c252009-07-17 12:17:14 -07003763void AudioFlinger::PlaybackThread::Track::mute(bool muted)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003764{
3765 mMute = muted;
3766}
3767
Eric Laurent65b65452010-06-01 23:49:17 -07003768status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId)
3769{
3770 status_t status = DEAD_OBJECT;
3771 sp<ThreadBase> thread = mThread.promote();
3772 if (thread != 0) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07003773 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3774 status = playbackThread->attachAuxEffect(this, EffectId);
Eric Laurent65b65452010-06-01 23:49:17 -07003775 }
3776 return status;
3777}
3778
3779void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer)
3780{
3781 mAuxEffectId = EffectId;
3782 mAuxBuffer = buffer;
3783}
3784
John Grossmand8cf2962012-02-08 16:37:41 -08003785// timed audio tracks
3786
3787sp<AudioFlinger::PlaybackThread::TimedTrack>
3788AudioFlinger::PlaybackThread::TimedTrack::create(
Glenn Kasten685c9ce2012-01-20 13:32:16 -08003789 PlaybackThread *thread,
John Grossmand8cf2962012-02-08 16:37:41 -08003790 const sp<Client>& client,
3791 audio_stream_type_t streamType,
3792 uint32_t sampleRate,
3793 audio_format_t format,
3794 uint32_t channelMask,
3795 int frameCount,
3796 const sp<IMemory>& sharedBuffer,
3797 int sessionId) {
3798 if (!client->reserveTimedTrack())
3799 return NULL;
3800
Glenn Kasten44c71fa2012-03-19 10:38:51 -07003801 return new TimedTrack(
John Grossmand8cf2962012-02-08 16:37:41 -08003802 thread, client, streamType, sampleRate, format, channelMask, frameCount,
3803 sharedBuffer, sessionId);
John Grossmand8cf2962012-02-08 16:37:41 -08003804}
3805
3806AudioFlinger::PlaybackThread::TimedTrack::TimedTrack(
Glenn Kasten685c9ce2012-01-20 13:32:16 -08003807 PlaybackThread *thread,
John Grossmand8cf2962012-02-08 16:37:41 -08003808 const sp<Client>& client,
3809 audio_stream_type_t streamType,
3810 uint32_t sampleRate,
3811 audio_format_t format,
3812 uint32_t channelMask,
3813 int frameCount,
3814 const sp<IMemory>& sharedBuffer,
3815 int sessionId)
3816 : Track(thread, client, streamType, sampleRate, format, channelMask,
3817 frameCount, sharedBuffer, sessionId),
3818 mTimedSilenceBuffer(NULL),
3819 mTimedSilenceBufferSize(0),
3820 mTimedAudioOutputOnTime(false),
3821 mMediaTimeTransformValid(false)
3822{
3823 LocalClock lc;
3824 mLocalTimeFreq = lc.getLocalFreq();
3825
3826 mLocalTimeToSampleTransform.a_zero = 0;
3827 mLocalTimeToSampleTransform.b_zero = 0;
3828 mLocalTimeToSampleTransform.a_to_b_numer = sampleRate;
3829 mLocalTimeToSampleTransform.a_to_b_denom = mLocalTimeFreq;
3830 LinearTransform::reduce(&mLocalTimeToSampleTransform.a_to_b_numer,
3831 &mLocalTimeToSampleTransform.a_to_b_denom);
3832}
3833
3834AudioFlinger::PlaybackThread::TimedTrack::~TimedTrack() {
3835 mClient->releaseTimedTrack();
3836 delete [] mTimedSilenceBuffer;
3837}
3838
3839status_t AudioFlinger::PlaybackThread::TimedTrack::allocateTimedBuffer(
3840 size_t size, sp<IMemory>* buffer) {
3841
3842 Mutex::Autolock _l(mTimedBufferQueueLock);
3843
3844 trimTimedBufferQueue_l();
3845
3846 // lazily initialize the shared memory heap for timed buffers
3847 if (mTimedMemoryDealer == NULL) {
3848 const int kTimedBufferHeapSize = 512 << 10;
3849
3850 mTimedMemoryDealer = new MemoryDealer(kTimedBufferHeapSize,
3851 "AudioFlingerTimed");
3852 if (mTimedMemoryDealer == NULL)
3853 return NO_MEMORY;
3854 }
3855
3856 sp<IMemory> newBuffer = mTimedMemoryDealer->allocate(size);
3857 if (newBuffer == NULL) {
3858 newBuffer = mTimedMemoryDealer->allocate(size);
3859 if (newBuffer == NULL)
3860 return NO_MEMORY;
3861 }
3862
3863 *buffer = newBuffer;
3864 return NO_ERROR;
3865}
3866
3867// caller must hold mTimedBufferQueueLock
3868void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() {
3869 int64_t mediaTimeNow;
3870 {
3871 Mutex::Autolock mttLock(mMediaTimeTransformLock);
3872 if (!mMediaTimeTransformValid)
3873 return;
3874
3875 int64_t targetTimeNow;
3876 status_t res = (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME)
3877 ? mCCHelper.getCommonTime(&targetTimeNow)
3878 : mCCHelper.getLocalTime(&targetTimeNow);
3879
3880 if (OK != res)
3881 return;
3882
3883 if (!mMediaTimeTransform.doReverseTransform(targetTimeNow,
3884 &mediaTimeNow)) {
3885 return;
3886 }
3887 }
3888
3889 size_t trimIndex;
3890 for (trimIndex = 0; trimIndex < mTimedBufferQueue.size(); trimIndex++) {
3891 if (mTimedBufferQueue[trimIndex].pts() > mediaTimeNow)
3892 break;
3893 }
3894
3895 if (trimIndex) {
3896 mTimedBufferQueue.removeItemsAt(0, trimIndex);
3897 }
3898}
3899
3900status_t AudioFlinger::PlaybackThread::TimedTrack::queueTimedBuffer(
3901 const sp<IMemory>& buffer, int64_t pts) {
3902
3903 {
3904 Mutex::Autolock mttLock(mMediaTimeTransformLock);
3905 if (!mMediaTimeTransformValid)
3906 return INVALID_OPERATION;
3907 }
3908
3909 Mutex::Autolock _l(mTimedBufferQueueLock);
3910
3911 mTimedBufferQueue.add(TimedBuffer(buffer, pts));
3912
3913 return NO_ERROR;
3914}
3915
3916status_t AudioFlinger::PlaybackThread::TimedTrack::setMediaTimeTransform(
3917 const LinearTransform& xform, TimedAudioTrack::TargetTimeline target) {
3918
3919 ALOGV("%s az=%lld bz=%lld n=%d d=%u tgt=%d", __PRETTY_FUNCTION__,
3920 xform.a_zero, xform.b_zero, xform.a_to_b_numer, xform.a_to_b_denom,
3921 target);
3922
3923 if (!(target == TimedAudioTrack::LOCAL_TIME ||
3924 target == TimedAudioTrack::COMMON_TIME)) {
3925 return BAD_VALUE;
3926 }
3927
3928 Mutex::Autolock lock(mMediaTimeTransformLock);
3929 mMediaTimeTransform = xform;
3930 mMediaTimeTransformTarget = target;
3931 mMediaTimeTransformValid = true;
3932
3933 return NO_ERROR;
3934}
3935
3936#define min(a, b) ((a) < (b) ? (a) : (b))
3937
3938// implementation of getNextBuffer for tracks whose buffers have timestamps
3939status_t AudioFlinger::PlaybackThread::TimedTrack::getNextBuffer(
3940 AudioBufferProvider::Buffer* buffer, int64_t pts)
3941{
3942 if (pts == AudioBufferProvider::kInvalidPTS) {
3943 buffer->raw = 0;
3944 buffer->frameCount = 0;
3945 return INVALID_OPERATION;
3946 }
3947
John Grossmand8cf2962012-02-08 16:37:41 -08003948 Mutex::Autolock _l(mTimedBufferQueueLock);
3949
3950 while (true) {
3951
3952 // if we have no timed buffers, then fail
3953 if (mTimedBufferQueue.isEmpty()) {
3954 buffer->raw = 0;
3955 buffer->frameCount = 0;
3956 return NOT_ENOUGH_DATA;
3957 }
3958
3959 TimedBuffer& head = mTimedBufferQueue.editItemAt(0);
3960
3961 // calculate the PTS of the head of the timed buffer queue expressed in
3962 // local time
3963 int64_t headLocalPTS;
3964 {
3965 Mutex::Autolock mttLock(mMediaTimeTransformLock);
3966
Glenn Kasten81916df2012-03-08 12:18:35 -08003967 ALOG_ASSERT(mMediaTimeTransformValid, "media time transform invalid");
John Grossmand8cf2962012-02-08 16:37:41 -08003968
3969 if (mMediaTimeTransform.a_to_b_denom == 0) {
3970 // the transform represents a pause, so yield silence
3971 timedYieldSilence(buffer->frameCount, buffer);
3972 return NO_ERROR;
3973 }
3974
3975 int64_t transformedPTS;
3976 if (!mMediaTimeTransform.doForwardTransform(head.pts(),
3977 &transformedPTS)) {
3978 // the transform failed. this shouldn't happen, but if it does
3979 // then just drop this buffer
3980 ALOGW("timedGetNextBuffer transform failed");
3981 buffer->raw = 0;
3982 buffer->frameCount = 0;
3983 mTimedBufferQueue.removeAt(0);
3984 return NO_ERROR;
3985 }
3986
3987 if (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) {
3988 if (OK != mCCHelper.commonTimeToLocalTime(transformedPTS,
3989 &headLocalPTS)) {
3990 buffer->raw = 0;
3991 buffer->frameCount = 0;
3992 return INVALID_OPERATION;
3993 }
3994 } else {
3995 headLocalPTS = transformedPTS;
3996 }
3997 }
3998
3999 // adjust the head buffer's PTS to reflect the portion of the head buffer
4000 // that has already been consumed
4001 int64_t effectivePTS = headLocalPTS +
4002 ((head.position() / mCblk->frameSize) * mLocalTimeFreq / sampleRate());
4003
4004 // Calculate the delta in samples between the head of the input buffer
4005 // queue and the start of the next output buffer that will be written.
4006 // If the transformation fails because of over or underflow, it means
4007 // that the sample's position in the output stream is so far out of
4008 // whack that it should just be dropped.
4009 int64_t sampleDelta;
4010 if (llabs(effectivePTS - pts) >= (static_cast<int64_t>(1) << 31)) {
4011 ALOGV("*** head buffer is too far from PTS: dropped buffer");
4012 mTimedBufferQueue.removeAt(0);
4013 continue;
4014 }
4015 if (!mLocalTimeToSampleTransform.doForwardTransform(
4016 (effectivePTS - pts) << 32, &sampleDelta)) {
4017 ALOGV("*** too late during sample rate transform: dropped buffer");
4018 mTimedBufferQueue.removeAt(0);
4019 continue;
4020 }
4021
4022 ALOGV("*** %s head.pts=%lld head.pos=%d pts=%lld sampleDelta=[%d.%08x]",
4023 __PRETTY_FUNCTION__, head.pts(), head.position(), pts,
4024 static_cast<int32_t>((sampleDelta >= 0 ? 0 : 1) + (sampleDelta >> 32)),
4025 static_cast<uint32_t>(sampleDelta & 0xFFFFFFFF));
4026
4027 // if the delta between the ideal placement for the next input sample and
4028 // the current output position is within this threshold, then we will
4029 // concatenate the next input samples to the previous output
4030 const int64_t kSampleContinuityThreshold =
4031 (static_cast<int64_t>(sampleRate()) << 32) / 10;
4032
4033 // if this is the first buffer of audio that we're emitting from this track
4034 // then it should be almost exactly on time.
4035 const int64_t kSampleStartupThreshold = 1LL << 32;
4036
4037 if ((mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleContinuityThreshold) ||
4038 (!mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleStartupThreshold)) {
4039 // the next input is close enough to being on time, so concatenate it
4040 // with the last output
4041 timedYieldSamples(buffer);
4042
4043 ALOGV("*** on time: head.pos=%d frameCount=%u", head.position(), buffer->frameCount);
4044 return NO_ERROR;
4045 } else if (sampleDelta > 0) {
4046 // the gap between the current output position and the proper start of
4047 // the next input sample is too big, so fill it with silence
4048 uint32_t framesUntilNextInput = (sampleDelta + 0x80000000) >> 32;
4049
4050 timedYieldSilence(framesUntilNextInput, buffer);
4051 ALOGV("*** silence: frameCount=%u", buffer->frameCount);
4052 return NO_ERROR;
4053 } else {
4054 // the next input sample is late
4055 uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32));
4056 size_t onTimeSamplePosition =
4057 head.position() + lateFrames * mCblk->frameSize;
4058
4059 if (onTimeSamplePosition > head.buffer()->size()) {
4060 // all the remaining samples in the head are too late, so
4061 // drop it and move on
4062 ALOGV("*** too late: dropped buffer");
4063 mTimedBufferQueue.removeAt(0);
4064 continue;
4065 } else {
4066 // skip over the late samples
4067 head.setPosition(onTimeSamplePosition);
4068
4069 // yield the available samples
4070 timedYieldSamples(buffer);
4071
4072 ALOGV("*** late: head.pos=%d frameCount=%u", head.position(), buffer->frameCount);
4073 return NO_ERROR;
4074 }
4075 }
4076 }
4077}
4078
4079// Yield samples from the timed buffer queue head up to the given output
4080// buffer's capacity.
4081//
4082// Caller must hold mTimedBufferQueueLock
4083void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSamples(
4084 AudioBufferProvider::Buffer* buffer) {
4085
4086 const TimedBuffer& head = mTimedBufferQueue[0];
4087
4088 buffer->raw = (static_cast<uint8_t*>(head.buffer()->pointer()) +
4089 head.position());
4090
4091 uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) /
4092 mCblk->frameSize);
4093 size_t framesRequested = buffer->frameCount;
4094 buffer->frameCount = min(framesLeftInHead, framesRequested);
4095
4096 mTimedAudioOutputOnTime = true;
4097}
4098
4099// Yield samples of silence up to the given output buffer's capacity
4100//
4101// Caller must hold mTimedBufferQueueLock
4102void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSilence(
4103 uint32_t numFrames, AudioBufferProvider::Buffer* buffer) {
4104
4105 // lazily allocate a buffer filled with silence
4106 if (mTimedSilenceBufferSize < numFrames * mCblk->frameSize) {
4107 delete [] mTimedSilenceBuffer;
4108 mTimedSilenceBufferSize = numFrames * mCblk->frameSize;
4109 mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize];
4110 memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize);
4111 }
4112
4113 buffer->raw = mTimedSilenceBuffer;
4114 size_t framesRequested = buffer->frameCount;
4115 buffer->frameCount = min(numFrames, framesRequested);
4116
4117 mTimedAudioOutputOnTime = false;
4118}
4119
Glenn Kastenc2db1192012-02-22 10:47:35 -08004120// AudioBufferProvider interface
John Grossmand8cf2962012-02-08 16:37:41 -08004121void AudioFlinger::PlaybackThread::TimedTrack::releaseBuffer(
4122 AudioBufferProvider::Buffer* buffer) {
4123
4124 Mutex::Autolock _l(mTimedBufferQueueLock);
4125
John Grossman8c010612012-02-12 17:51:21 -08004126 // If the buffer which was just released is part of the buffer at the head
4127 // of the queue, be sure to update the amt of the buffer which has been
4128 // consumed. If the buffer being returned is not part of the head of the
4129 // queue, its either because the buffer is part of the silence buffer, or
4130 // because the head of the timed queue was trimmed after the mixer called
4131 // getNextBuffer but before the mixer called releaseBuffer.
4132 if ((buffer->raw != mTimedSilenceBuffer) && mTimedBufferQueue.size()) {
John Grossmand8cf2962012-02-08 16:37:41 -08004133 TimedBuffer& head = mTimedBufferQueue.editItemAt(0);
John Grossman8c010612012-02-12 17:51:21 -08004134
4135 void* start = head.buffer()->pointer();
Glenn Kasten175b2be2012-02-17 16:24:10 -08004136 void* end = (char *) head.buffer()->pointer() + head.buffer()->size();
John Grossman8c010612012-02-12 17:51:21 -08004137
4138 if ((buffer->raw >= start) && (buffer->raw <= end)) {
4139 head.setPosition(head.position() +
4140 (buffer->frameCount * mCblk->frameSize));
4141 if (static_cast<size_t>(head.position()) >= head.buffer()->size()) {
4142 mTimedBufferQueue.removeAt(0);
4143 }
John Grossmand8cf2962012-02-08 16:37:41 -08004144 }
4145 }
4146
4147 buffer->raw = 0;
4148 buffer->frameCount = 0;
4149}
4150
4151uint32_t AudioFlinger::PlaybackThread::TimedTrack::framesReady() const {
4152 Mutex::Autolock _l(mTimedBufferQueueLock);
4153
4154 uint32_t frames = 0;
4155 for (size_t i = 0; i < mTimedBufferQueue.size(); i++) {
4156 const TimedBuffer& tb = mTimedBufferQueue[i];
4157 frames += (tb.buffer()->size() - tb.position()) / mCblk->frameSize;
4158 }
4159
4160 return frames;
4161}
4162
4163AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer()
4164 : mPTS(0), mPosition(0) {}
4165
4166AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer(
4167 const sp<IMemory>& buffer, int64_t pts)
4168 : mBuffer(buffer), mPTS(pts), mPosition(0) {}
4169
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004170// ----------------------------------------------------------------------------
4171
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004172// RecordTrack constructor must be called with AudioFlinger::mLock held
Eric Laurenta553c252009-07-17 12:17:14 -07004173AudioFlinger::RecordThread::RecordTrack::RecordTrack(
Glenn Kasten685c9ce2012-01-20 13:32:16 -08004174 RecordThread *thread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08004177 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004178 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 int frameCount,
Eric Laurent65b65452010-06-01 23:49:17 -07004180 int sessionId)
Eric Laurenta553c252009-07-17 12:17:14 -07004181 : TrackBase(thread, client, sampleRate, format,
Glenn Kasten35269822012-02-21 10:35:56 -08004182 channelMask, frameCount, 0 /*sharedBuffer*/, sessionId),
Eric Laurenta553c252009-07-17 12:17:14 -07004183 mOverflow(false)
4184{
Eric Laurent8a77a992009-09-09 05:16:08 -07004185 if (mCblk != NULL) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07004186 ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer);
4187 if (format == AUDIO_FORMAT_PCM_16_BIT) {
4188 mCblk->frameSize = mChannelCount * sizeof(int16_t);
4189 } else if (format == AUDIO_FORMAT_PCM_8_BIT) {
4190 mCblk->frameSize = mChannelCount * sizeof(int8_t);
4191 } else {
4192 mCblk->frameSize = sizeof(int8_t);
4193 }
Eric Laurent8a77a992009-09-09 05:16:08 -07004194 }
Eric Laurenta553c252009-07-17 12:17:14 -07004195}
4196
4197AudioFlinger::RecordThread::RecordTrack::~RecordTrack()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198{
Eric Laurent49f02be2009-11-19 09:00:56 -08004199 sp<ThreadBase> thread = mThread.promote();
4200 if (thread != 0) {
4201 AudioSystem::releaseInput(thread->id());
4202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203}
4204
Glenn Kastenc2db1192012-02-22 10:47:35 -08004205// AudioBufferProvider interface
John Grossmand8cf2962012-02-08 16:37:41 -08004206status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207{
4208 audio_track_cblk_t* cblk = this->cblk();
4209 uint32_t framesAvail;
4210 uint32_t framesReq = buffer->frameCount;
4211
Glenn Kasten18db49a2012-03-12 16:29:55 -07004212 // Check if last stepServer failed, try to step now
Glenn Kasten35269822012-02-21 10:35:56 -08004213 if (mStepServerFailed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 if (!step()) goto getNextBuffer_exit;
Steve Block71f2cf12011-10-20 11:56:00 +01004215 ALOGV("stepServer recovered");
Glenn Kasten35269822012-02-21 10:35:56 -08004216 mStepServerFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 }
4218
4219 framesAvail = cblk->framesAvailable_l();
4220
Glenn Kastene80a4cc2011-12-15 09:51:17 -08004221 if (CC_LIKELY(framesAvail)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 uint32_t s = cblk->server;
4223 uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
4224
4225 if (framesReq > framesAvail) {
4226 framesReq = framesAvail;
4227 }
4228 if (s + framesReq > bufferEnd) {
4229 framesReq = bufferEnd - s;
4230 }
4231
4232 buffer->raw = getBuffer(s, framesReq);
Glenn Kastenc434c902011-12-13 11:53:26 -08004233 if (buffer->raw == NULL) goto getNextBuffer_exit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234
4235 buffer->frameCount = framesReq;
4236 return NO_ERROR;
4237 }
4238
4239getNextBuffer_exit:
Glenn Kastenc434c902011-12-13 11:53:26 -08004240 buffer->raw = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 buffer->frameCount = 0;
4242 return NOT_ENOUGH_DATA;
4243}
4244
Glenn Kasten6a20b262012-02-02 10:56:47 -08004245status_t AudioFlinger::RecordThread::RecordTrack::start(pid_t tid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246{
Eric Laurenta553c252009-07-17 12:17:14 -07004247 sp<ThreadBase> thread = mThread.promote();
4248 if (thread != 0) {
4249 RecordThread *recordThread = (RecordThread *)thread.get();
Glenn Kasten6a20b262012-02-02 10:56:47 -08004250 return recordThread->start(this, tid);
Eric Laurent49f02be2009-11-19 09:00:56 -08004251 } else {
4252 return BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -07004253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254}
4255
Eric Laurenta553c252009-07-17 12:17:14 -07004256void AudioFlinger::RecordThread::RecordTrack::stop()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257{
Eric Laurenta553c252009-07-17 12:17:14 -07004258 sp<ThreadBase> thread = mThread.promote();
4259 if (thread != 0) {
4260 RecordThread *recordThread = (RecordThread *)thread.get();
4261 recordThread->stop(this);
Eric Laurentae29b762011-03-28 18:37:07 -07004262 TrackBase::reset();
Glenn Kasten76b6c0c2012-02-14 08:52:15 -08004263 // Force overrun condition to avoid false overrun callback until first data is
Eric Laurentae29b762011-03-28 18:37:07 -07004264 // read from buffer
4265 android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags);
Eric Laurenta553c252009-07-17 12:17:14 -07004266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267}
4268
Eric Laurent3fdb1262009-11-07 00:01:32 -08004269void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size)
4270{
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004271 snprintf(buffer, size, " %05d %03u 0x%08x %05d %04u %01d %05u %08x %08x\n",
Glenn Kasten81211142012-02-05 18:09:08 -08004272 (mClient == 0) ? getpid_cached : mClient->pid(),
Eric Laurent3fdb1262009-11-07 00:01:32 -08004273 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004274 mChannelMask,
Eric Laurent65b65452010-06-01 23:49:17 -07004275 mSessionId,
Eric Laurent3fdb1262009-11-07 00:01:32 -08004276 mFrameCount,
4277 mState,
4278 mCblk->sampleRate,
4279 mCblk->server,
4280 mCblk->user);
4281}
4282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283
4284// ----------------------------------------------------------------------------
4285
Eric Laurenta553c252009-07-17 12:17:14 -07004286AudioFlinger::PlaybackThread::OutputTrack::OutputTrack(
Glenn Kasten685c9ce2012-01-20 13:32:16 -08004287 PlaybackThread *playbackThread,
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004288 DuplicatingThread *sourceThread,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08004290 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004291 uint32_t channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 int frameCount)
Glenn Kasten685c9ce2012-01-20 13:32:16 -08004293 : Track(playbackThread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0),
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004294 mActive(false), mSourceThread(sourceThread)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295{
Eric Laurenta553c252009-07-17 12:17:14 -07004296
Eric Laurent6c30a712009-08-10 23:22:32 -07004297 if (mCblk != NULL) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -07004298 mCblk->flags |= CBLK_DIRECTION_OUT;
Eric Laurent6c30a712009-08-10 23:22:32 -07004299 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
Eric Laurent6c30a712009-08-10 23:22:32 -07004300 mOutBuffer.frameCount = 0;
Eric Laurent6c30a712009-08-10 23:22:32 -07004301 playbackThread->mTracks.add(this);
Steve Block71f2cf12011-10-20 11:56:00 +01004302 ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004303 "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p",
4304 mCblk, mBuffer, mCblk->buffers,
4305 mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd);
Eric Laurent6c30a712009-08-10 23:22:32 -07004306 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00004307 ALOGW("Error creating output track on thread %p", playbackThread);
Eric Laurent6c30a712009-08-10 23:22:32 -07004308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309}
4310
Eric Laurenta553c252009-07-17 12:17:14 -07004311AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312{
Eric Laurent6c30a712009-08-10 23:22:32 -07004313 clearBufferQueue();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314}
4315
Glenn Kasten6a20b262012-02-02 10:56:47 -08004316status_t AudioFlinger::PlaybackThread::OutputTrack::start(pid_t tid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317{
Glenn Kasten6a20b262012-02-02 10:56:47 -08004318 status_t status = Track::start(tid);
Eric Laurenta553c252009-07-17 12:17:14 -07004319 if (status != NO_ERROR) {
4320 return status;
4321 }
4322
4323 mActive = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 mRetryCount = 127;
4325 return status;
4326}
4327
Eric Laurenta553c252009-07-17 12:17:14 -07004328void AudioFlinger::PlaybackThread::OutputTrack::stop()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329{
4330 Track::stop();
4331 clearBufferQueue();
4332 mOutBuffer.frameCount = 0;
Eric Laurenta553c252009-07-17 12:17:14 -07004333 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334}
4335
Eric Laurenta553c252009-07-17 12:17:14 -07004336bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337{
4338 Buffer *pInBuffer;
4339 Buffer inBuffer;
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004340 uint32_t channelCount = mChannelCount;
Eric Laurenta553c252009-07-17 12:17:14 -07004341 bool outputBufferFull = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 inBuffer.frameCount = frames;
4343 inBuffer.i16 = data;
Eric Laurenta553c252009-07-17 12:17:14 -07004344
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004345 uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs();
Eric Laurenta553c252009-07-17 12:17:14 -07004346
Eric Laurent62443f52009-10-05 20:29:18 -07004347 if (!mActive && frames != 0) {
Glenn Kasten6a20b262012-02-02 10:56:47 -08004348 start(0);
Eric Laurenta553c252009-07-17 12:17:14 -07004349 sp<ThreadBase> thread = mThread.promote();
4350 if (thread != 0) {
4351 MixerThread *mixerThread = (MixerThread *)thread.get();
4352 if (mCblk->frameCount > frames){
4353 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
4354 uint32_t startFrames = (mCblk->frameCount - frames);
4355 pInBuffer = new Buffer;
Eric Laurentb0a01472010-05-14 05:45:46 -07004356 pInBuffer->mBuffer = new int16_t[startFrames * channelCount];
Eric Laurenta553c252009-07-17 12:17:14 -07004357 pInBuffer->frameCount = startFrames;
4358 pInBuffer->i16 = pInBuffer->mBuffer;
Eric Laurentb0a01472010-05-14 05:45:46 -07004359 memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t));
Eric Laurenta553c252009-07-17 12:17:14 -07004360 mBufferQueue.add(pInBuffer);
4361 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00004362 ALOGW ("OutputTrack::write() %p no more buffers in queue", this);
Eric Laurenta553c252009-07-17 12:17:14 -07004363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 }
Eric Laurenta553c252009-07-17 12:17:14 -07004365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 }
4367
Eric Laurenta553c252009-07-17 12:17:14 -07004368 while (waitTimeLeftMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 // First write pending buffers, then new data
4370 if (mBufferQueue.size()) {
4371 pInBuffer = mBufferQueue.itemAt(0);
4372 } else {
4373 pInBuffer = &inBuffer;
4374 }
Eric Laurenta553c252009-07-17 12:17:14 -07004375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 if (pInBuffer->frameCount == 0) {
4377 break;
4378 }
Eric Laurenta553c252009-07-17 12:17:14 -07004379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 if (mOutBuffer.frameCount == 0) {
4381 mOutBuffer.frameCount = pInBuffer->frameCount;
Eric Laurenta553c252009-07-17 12:17:14 -07004382 nsecs_t startTime = systemTime();
Glenn Kasten34f9f8b2012-01-20 17:00:00 -08004383 if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)NO_MORE_BUFFERS) {
Steve Block71f2cf12011-10-20 11:56:00 +01004384 ALOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get());
Eric Laurenta553c252009-07-17 12:17:14 -07004385 outputBufferFull = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 break;
4387 }
Eric Laurenta553c252009-07-17 12:17:14 -07004388 uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime);
Eric Laurenta553c252009-07-17 12:17:14 -07004389 if (waitTimeLeftMs >= waitTimeMs) {
4390 waitTimeLeftMs -= waitTimeMs;
4391 } else {
4392 waitTimeLeftMs = 0;
4393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 }
Eric Laurenta553c252009-07-17 12:17:14 -07004395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount;
Eric Laurentb0a01472010-05-14 05:45:46 -07004397 memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 mCblk->stepUser(outFrames);
4399 pInBuffer->frameCount -= outFrames;
Eric Laurentb0a01472010-05-14 05:45:46 -07004400 pInBuffer->i16 += outFrames * channelCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 mOutBuffer.frameCount -= outFrames;
Eric Laurentb0a01472010-05-14 05:45:46 -07004402 mOutBuffer.i16 += outFrames * channelCount;
Eric Laurenta553c252009-07-17 12:17:14 -07004403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 if (pInBuffer->frameCount == 0) {
4405 if (mBufferQueue.size()) {
4406 mBufferQueue.removeAt(0);
4407 delete [] pInBuffer->mBuffer;
4408 delete pInBuffer;
Steve Block71f2cf12011-10-20 11:56:00 +01004409 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 -08004410 } else {
4411 break;
4412 }
4413 }
4414 }
Eric Laurenta553c252009-07-17 12:17:14 -07004415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 // If we could not write all frames, allocate a buffer and queue it for next time.
4417 if (inBuffer.frameCount) {
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004418 sp<ThreadBase> thread = mThread.promote();
4419 if (thread != 0 && !thread->standby()) {
4420 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
4421 pInBuffer = new Buffer;
Eric Laurentb0a01472010-05-14 05:45:46 -07004422 pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount];
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004423 pInBuffer->frameCount = inBuffer.frameCount;
4424 pInBuffer->i16 = pInBuffer->mBuffer;
Eric Laurentb0a01472010-05-14 05:45:46 -07004425 memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t));
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004426 mBufferQueue.add(pInBuffer);
Steve Block71f2cf12011-10-20 11:56:00 +01004427 ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size());
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004428 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00004429 ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this);
Eric Laurent8ac9f8d2009-12-18 05:47:48 -08004430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 }
4432 }
Eric Laurenta553c252009-07-17 12:17:14 -07004433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 // Calling write() with a 0 length buffer, means that no more data will be written:
Eric Laurenta553c252009-07-17 12:17:14 -07004435 // 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 -08004436 // by output mixer.
Eric Laurenta553c252009-07-17 12:17:14 -07004437 if (frames == 0 && mBufferQueue.size() == 0) {
4438 if (mCblk->user < mCblk->frameCount) {
4439 frames = mCblk->frameCount - mCblk->user;
4440 pInBuffer = new Buffer;
Eric Laurentb0a01472010-05-14 05:45:46 -07004441 pInBuffer->mBuffer = new int16_t[frames * channelCount];
Eric Laurenta553c252009-07-17 12:17:14 -07004442 pInBuffer->frameCount = frames;
4443 pInBuffer->i16 = pInBuffer->mBuffer;
Eric Laurentb0a01472010-05-14 05:45:46 -07004444 memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t));
Eric Laurenta553c252009-07-17 12:17:14 -07004445 mBufferQueue.add(pInBuffer);
Eric Laurent62443f52009-10-05 20:29:18 -07004446 } else if (mActive) {
Eric Laurenta553c252009-07-17 12:17:14 -07004447 stop();
4448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 }
4450
Eric Laurenta553c252009-07-17 12:17:14 -07004451 return outputBufferFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452}
4453
Eric Laurenta553c252009-07-17 12:17:14 -07004454status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455{
4456 int active;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 status_t result;
4458 audio_track_cblk_t* cblk = mCblk;
4459 uint32_t framesReq = buffer->frameCount;
4460
Steve Block71f2cf12011-10-20 11:56:00 +01004461// ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 buffer->frameCount = 0;
Eric Laurenta553c252009-07-17 12:17:14 -07004463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 uint32_t framesAvail = cblk->framesAvailable();
4465
Eric Laurenta553c252009-07-17 12:17:14 -07004466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467 if (framesAvail == 0) {
Eric Laurenta553c252009-07-17 12:17:14 -07004468 Mutex::Autolock _l(cblk->lock);
4469 goto start_loop_here;
4470 while (framesAvail == 0) {
4471 active = mActive;
Glenn Kastene80a4cc2011-12-15 09:51:17 -08004472 if (CC_UNLIKELY(!active)) {
Steve Block71f2cf12011-10-20 11:56:00 +01004473 ALOGV("Not active and NO_MORE_BUFFERS");
Glenn Kasten34f9f8b2012-01-20 17:00:00 -08004474 return NO_MORE_BUFFERS;
Eric Laurenta553c252009-07-17 12:17:14 -07004475 }
4476 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
4477 if (result != NO_ERROR) {
Glenn Kasten34f9f8b2012-01-20 17:00:00 -08004478 return NO_MORE_BUFFERS;
Eric Laurenta553c252009-07-17 12:17:14 -07004479 }
4480 // read the server count again
4481 start_loop_here:
4482 framesAvail = cblk->framesAvailable_l();
4483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004484 }
4485
Eric Laurenta553c252009-07-17 12:17:14 -07004486// if (framesAvail < framesReq) {
Glenn Kasten34f9f8b2012-01-20 17:00:00 -08004487// return NO_MORE_BUFFERS;
Eric Laurenta553c252009-07-17 12:17:14 -07004488// }
4489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004490 if (framesReq > framesAvail) {
4491 framesReq = framesAvail;
4492 }
4493
4494 uint32_t u = cblk->user;
4495 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
4496
4497 if (u + framesReq > bufferEnd) {
4498 framesReq = bufferEnd - u;
4499 }
4500
4501 buffer->frameCount = framesReq;
4502 buffer->raw = (void *)cblk->buffer(u);
4503 return NO_ERROR;
4504}
4505
4506
Eric Laurenta553c252009-07-17 12:17:14 -07004507void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508{
4509 size_t size = mBufferQueue.size();
Eric Laurenta553c252009-07-17 12:17:14 -07004510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004511 for (size_t i = 0; i < size; i++) {
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08004512 Buffer *pBuffer = mBufferQueue.itemAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 delete [] pBuffer->mBuffer;
4514 delete pBuffer;
4515 }
4516 mBufferQueue.clear();
4517}
4518
4519// ----------------------------------------------------------------------------
4520
4521AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
4522 : RefBase(),
4523 mAudioFlinger(audioFlinger),
Glenn Kastenb3db2132012-01-19 08:59:58 -08004524 // 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 -08004525 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossmand8cf2962012-02-08 16:37:41 -08004526 mPid(pid),
4527 mTimedTrackCount(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528{
4529 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
4530}
4531
Eric Laurentb9481d82009-09-17 05:12:56 -07004532// Client destructor must be called with AudioFlinger::mLock held
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004533AudioFlinger::Client::~Client()
4534{
Eric Laurentb9481d82009-09-17 05:12:56 -07004535 mAudioFlinger->removeClient_l(mPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536}
4537
Glenn Kasten1f812f72012-01-30 10:15:48 -08004538sp<MemoryDealer> AudioFlinger::Client::heap() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004539{
4540 return mMemoryDealer;
4541}
4542
John Grossmand8cf2962012-02-08 16:37:41 -08004543// Reserve one of the limited slots for a timed audio track associated
4544// with this client
4545bool AudioFlinger::Client::reserveTimedTrack()
4546{
4547 const int kMaxTimedTracksPerClient = 4;
4548
4549 Mutex::Autolock _l(mTimedTrackLock);
4550
4551 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
4552 ALOGW("can not create timed track - pid %d has exceeded the limit",
4553 mPid);
4554 return false;
4555 }
4556
4557 mTimedTrackCount++;
4558 return true;
4559}
4560
4561// Release a slot for a timed audio track
4562void AudioFlinger::Client::releaseTimedTrack()
4563{
4564 Mutex::Autolock _l(mTimedTrackLock);
4565 mTimedTrackCount--;
4566}
4567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568// ----------------------------------------------------------------------------
4569
Eric Laurent4f0f17d2010-05-12 02:05:53 -07004570AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
4571 const sp<IAudioFlingerClient>& client,
4572 pid_t pid)
Glenn Kastendc3ac852012-01-25 15:28:08 -08004573 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Eric Laurent4f0f17d2010-05-12 02:05:53 -07004574{
4575}
4576
4577AudioFlinger::NotificationClient::~NotificationClient()
4578{
Eric Laurent4f0f17d2010-05-12 02:05:53 -07004579}
4580
4581void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
4582{
4583 sp<NotificationClient> keep(this);
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08004584 mAudioFlinger->removeNotificationClient(mPid);
Eric Laurent4f0f17d2010-05-12 02:05:53 -07004585}
4586
4587// ----------------------------------------------------------------------------
4588
Eric Laurenta553c252009-07-17 12:17:14 -07004589AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004590 : BnAudioTrack(),
4591 mTrack(track)
4592{
4593}
4594
4595AudioFlinger::TrackHandle::~TrackHandle() {
4596 // just stop the track on deletion, associated resources
4597 // will be freed from the main thread once all pending buffers have
4598 // been played. Unless it's not in the active track list, in which
4599 // case we free everything now...
4600 mTrack->destroy();
4601}
4602
Glenn Kasten0ae4d972012-01-26 13:40:12 -08004603sp<IMemory> AudioFlinger::TrackHandle::getCblk() const {
4604 return mTrack->getCblk();
4605}
4606
Glenn Kasten6a20b262012-02-02 10:56:47 -08004607status_t AudioFlinger::TrackHandle::start(pid_t tid) {
4608 return mTrack->start(tid);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004609}
4610
4611void AudioFlinger::TrackHandle::stop() {
4612 mTrack->stop();
4613}
4614
4615void AudioFlinger::TrackHandle::flush() {
4616 mTrack->flush();
4617}
4618
4619void AudioFlinger::TrackHandle::mute(bool e) {
4620 mTrack->mute(e);
4621}
4622
4623void AudioFlinger::TrackHandle::pause() {
4624 mTrack->pause();
4625}
4626
Eric Laurent65b65452010-06-01 23:49:17 -07004627status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId)
4628{
4629 return mTrack->attachAuxEffect(EffectId);
4630}
4631
John Grossmand8cf2962012-02-08 16:37:41 -08004632status_t AudioFlinger::TrackHandle::allocateTimedBuffer(size_t size,
4633 sp<IMemory>* buffer) {
4634 if (!mTrack->isTimedTrack())
4635 return INVALID_OPERATION;
4636
4637 PlaybackThread::TimedTrack* tt =
4638 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
4639 return tt->allocateTimedBuffer(size, buffer);
4640}
4641
4642status_t AudioFlinger::TrackHandle::queueTimedBuffer(const sp<IMemory>& buffer,
4643 int64_t pts) {
4644 if (!mTrack->isTimedTrack())
4645 return INVALID_OPERATION;
4646
4647 PlaybackThread::TimedTrack* tt =
4648 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
4649 return tt->queueTimedBuffer(buffer, pts);
4650}
4651
4652status_t AudioFlinger::TrackHandle::setMediaTimeTransform(
4653 const LinearTransform& xform, int target) {
4654
4655 if (!mTrack->isTimedTrack())
4656 return INVALID_OPERATION;
4657
4658 PlaybackThread::TimedTrack* tt =
4659 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
4660 return tt->setMediaTimeTransform(
4661 xform, static_cast<TimedAudioTrack::TargetTimeline>(target));
4662}
4663
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004664status_t AudioFlinger::TrackHandle::onTransact(
4665 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
4666{
4667 return BnAudioTrack::onTransact(code, data, reply, flags);
4668}
4669
4670// ----------------------------------------------------------------------------
4671
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004672sp<IAudioRecord> AudioFlinger::openRecord(
4673 pid_t pid,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08004674 audio_io_handle_t input,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004675 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08004676 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07004677 uint32_t channelMask,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004678 int frameCount,
Glenn Kasten35269822012-02-21 10:35:56 -08004679 // FIXME dead, remove from IAudioFlinger
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004680 uint32_t flags,
Eric Laurent65b65452010-06-01 23:49:17 -07004681 int *sessionId,
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004682 status_t *status)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004683{
Eric Laurenta553c252009-07-17 12:17:14 -07004684 sp<RecordThread::RecordTrack> recordTrack;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004685 sp<RecordHandle> recordHandle;
4686 sp<Client> client;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004687 status_t lStatus;
Eric Laurenta553c252009-07-17 12:17:14 -07004688 RecordThread *thread;
4689 size_t inFrameCount;
Eric Laurent65b65452010-06-01 23:49:17 -07004690 int lSessionId;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004691
4692 // check calling permissions
4693 if (!recordingAllowed()) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004694 lStatus = PERMISSION_DENIED;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004695 goto Exit;
4696 }
4697
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004698 // add client to list
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004699 { // scope for mLock
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004700 Mutex::Autolock _l(mLock);
Eric Laurenta553c252009-07-17 12:17:14 -07004701 thread = checkRecordThread_l(input);
4702 if (thread == NULL) {
4703 lStatus = BAD_VALUE;
4704 goto Exit;
4705 }
4706
Glenn Kasten803a86a2012-01-25 14:28:29 -08004707 client = registerPid_l(pid);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004708
Eric Laurent65b65452010-06-01 23:49:17 -07004709 // If no audio session id is provided, create one here
Dima Zavin24fc2fb2011-04-19 22:30:36 -07004710 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent65b65452010-06-01 23:49:17 -07004711 lSessionId = *sessionId;
4712 } else {
Eric Laurent464d5b32011-06-17 21:29:58 -07004713 lSessionId = nextUniqueId();
Eric Laurent65b65452010-06-01 23:49:17 -07004714 if (sessionId != NULL) {
4715 *sessionId = lSessionId;
4716 }
4717 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004718 // create new record track. The record track uses one track in mHardwareMixerThread by convention.
Eric Laurent464d5b32011-06-17 21:29:58 -07004719 recordTrack = thread->createRecordTrack_l(client,
4720 sampleRate,
4721 format,
4722 channelMask,
4723 frameCount,
Eric Laurent464d5b32011-06-17 21:29:58 -07004724 lSessionId,
4725 &lStatus);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004726 }
Eric Laurent464d5b32011-06-17 21:29:58 -07004727 if (lStatus != NO_ERROR) {
Eric Laurentb9481d82009-09-17 05:12:56 -07004728 // remove local strong reference to Client before deleting the RecordTrack so that the Client
4729 // destructor is called by the TrackBase destructor with mLock held
4730 client.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004731 recordTrack.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004732 goto Exit;
4733 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004734
4735 // return to handle to client
4736 recordHandle = new RecordHandle(recordTrack);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004737 lStatus = NO_ERROR;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004738
4739Exit:
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004740 if (status) {
4741 *status = lStatus;
4742 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004743 return recordHandle;
4744}
4745
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004746// ----------------------------------------------------------------------------
4747
Eric Laurenta553c252009-07-17 12:17:14 -07004748AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004749 : BnAudioRecord(),
4750 mRecordTrack(recordTrack)
4751{
4752}
4753
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004754AudioFlinger::RecordHandle::~RecordHandle() {
4755 stop();
4756}
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004757
Glenn Kasten0ae4d972012-01-26 13:40:12 -08004758sp<IMemory> AudioFlinger::RecordHandle::getCblk() const {
4759 return mRecordTrack->getCblk();
4760}
4761
Glenn Kasten6a20b262012-02-02 10:56:47 -08004762status_t AudioFlinger::RecordHandle::start(pid_t tid) {
Steve Block71f2cf12011-10-20 11:56:00 +01004763 ALOGV("RecordHandle::start()");
Glenn Kasten6a20b262012-02-02 10:56:47 -08004764 return mRecordTrack->start(tid);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004765}
4766
4767void AudioFlinger::RecordHandle::stop() {
Steve Block71f2cf12011-10-20 11:56:00 +01004768 ALOGV("RecordHandle::stop()");
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004769 mRecordTrack->stop();
4770}
4771
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004772status_t AudioFlinger::RecordHandle::onTransact(
4773 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
4774{
4775 return BnAudioRecord::onTransact(code, data, reply, flags);
4776}
4777
4778// ----------------------------------------------------------------------------
4779
Eric Laurent464d5b32011-06-17 21:29:58 -07004780AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger,
4781 AudioStreamIn *input,
4782 uint32_t sampleRate,
4783 uint32_t channels,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08004784 audio_io_handle_t id,
Eric Laurent464d5b32011-06-17 21:29:58 -07004785 uint32_t device) :
Glenn Kastenefd511a2012-01-26 10:38:26 -08004786 ThreadBase(audioFlinger, id, device, RECORD),
Glenn Kastendc3ac852012-01-25 15:28:08 -08004787 mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL),
4788 // mRsmpInIndex and mInputBytes set by readInputParameters()
4789 mReqChannelCount(popcount(channels)),
4790 mReqSampleRate(sampleRate)
4791 // mBytesRead is only meaningful while active, and so is cleared in start()
4792 // (but might be better to also clear here for dump?)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004793{
Glenn Kasten86e33622012-02-28 12:30:08 -08004794 snprintf(mName, kNameLength, "AudioIn_%X", id);
Eric Laurent6dbdc402011-07-22 09:04:31 -07004795
Eric Laurenta553c252009-07-17 12:17:14 -07004796 readInputParameters();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004797}
4798
Eric Laurenta553c252009-07-17 12:17:14 -07004799
4800AudioFlinger::RecordThread::~RecordThread()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004801{
Eric Laurenta553c252009-07-17 12:17:14 -07004802 delete[] mRsmpInBuffer;
Glenn Kasten2589cf92012-01-27 18:08:45 -08004803 delete mResampler;
4804 delete[] mRsmpOutBuffer;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004805}
4806
Eric Laurenta553c252009-07-17 12:17:14 -07004807void AudioFlinger::RecordThread::onFirstRef()
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004808{
Eric Laurent6dbdc402011-07-22 09:04:31 -07004809 run(mName, PRIORITY_URGENT_AUDIO);
Eric Laurenta553c252009-07-17 12:17:14 -07004810}
Eric Laurent49f02be2009-11-19 09:00:56 -08004811
Eric Laurent828b9772011-08-07 16:32:26 -07004812status_t AudioFlinger::RecordThread::readyToRun()
4813{
4814 status_t status = initCheck();
Steve Block8564c8d2012-01-05 23:22:43 +00004815 ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this);
Eric Laurent828b9772011-08-07 16:32:26 -07004816 return status;
4817}
4818
Eric Laurenta553c252009-07-17 12:17:14 -07004819bool AudioFlinger::RecordThread::threadLoop()
4820{
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004821 AudioBufferProvider::Buffer buffer;
Eric Laurenta553c252009-07-17 12:17:14 -07004822 sp<RecordTrack> activeTrack;
Eric Laurent464d5b32011-06-17 21:29:58 -07004823 Vector< sp<EffectChain> > effectChains;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004824
Eric Laurent4712baa2010-09-30 16:12:31 -07004825 nsecs_t lastWarning = 0;
4826
Eric Laurent6dbdc402011-07-22 09:04:31 -07004827 acquireWakeLock();
4828
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004829 // start recording
4830 while (!exitPending()) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004831
Eric Laurenta553c252009-07-17 12:17:14 -07004832 processConfigEvents();
4833
4834 { // scope for mLock
4835 Mutex::Autolock _l(mLock);
4836 checkForNewParameters_l();
4837 if (mActiveTrack == 0 && mConfigEvents.isEmpty()) {
4838 if (!mStandby) {
Dima Zavin31f188892011-04-18 16:57:27 -07004839 mInput->stream->common.standby(&mInput->stream->common);
Eric Laurenta553c252009-07-17 12:17:14 -07004840 mStandby = true;
4841 }
4842
4843 if (exitPending()) break;
4844
Eric Laurent6dbdc402011-07-22 09:04:31 -07004845 releaseWakeLock_l();
Steve Block71f2cf12011-10-20 11:56:00 +01004846 ALOGV("RecordThread: loop stopping");
Eric Laurenta553c252009-07-17 12:17:14 -07004847 // go to sleep
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004848 mWaitWorkCV.wait(mLock);
Steve Block71f2cf12011-10-20 11:56:00 +01004849 ALOGV("RecordThread: loop starting");
Eric Laurent6dbdc402011-07-22 09:04:31 -07004850 acquireWakeLock_l();
Eric Laurenta553c252009-07-17 12:17:14 -07004851 continue;
4852 }
4853 if (mActiveTrack != 0) {
4854 if (mActiveTrack->mState == TrackBase::PAUSING) {
Eric Laurent9cc489a22009-12-05 05:20:01 -08004855 if (!mStandby) {
Dima Zavin31f188892011-04-18 16:57:27 -07004856 mInput->stream->common.standby(&mInput->stream->common);
Eric Laurent9cc489a22009-12-05 05:20:01 -08004857 mStandby = true;
4858 }
Eric Laurenta553c252009-07-17 12:17:14 -07004859 mActiveTrack.clear();
4860 mStartStopCond.broadcast();
4861 } else if (mActiveTrack->mState == TrackBase::RESUMING) {
Eric Laurenta553c252009-07-17 12:17:14 -07004862 if (mReqChannelCount != mActiveTrack->channelCount()) {
4863 mActiveTrack.clear();
Eric Laurent9cc489a22009-12-05 05:20:01 -08004864 mStartStopCond.broadcast();
4865 } else if (mBytesRead != 0) {
4866 // record start succeeds only if first read from audio input
4867 // succeeds
4868 if (mBytesRead > 0) {
4869 mActiveTrack->mState = TrackBase::ACTIVE;
4870 } else {
4871 mActiveTrack.clear();
4872 }
4873 mStartStopCond.broadcast();
Eric Laurenta553c252009-07-17 12:17:14 -07004874 }
Eric Laurent9cc489a22009-12-05 05:20:01 -08004875 mStandby = false;
Eric Laurenta553c252009-07-17 12:17:14 -07004876 }
Eric Laurenta553c252009-07-17 12:17:14 -07004877 }
Eric Laurent464d5b32011-06-17 21:29:58 -07004878 lockEffectChains_l(effectChains);
Eric Laurenta553c252009-07-17 12:17:14 -07004879 }
4880
4881 if (mActiveTrack != 0) {
Eric Laurent9cc489a22009-12-05 05:20:01 -08004882 if (mActiveTrack->mState != TrackBase::ACTIVE &&
4883 mActiveTrack->mState != TrackBase::RESUMING) {
Eric Laurent464d5b32011-06-17 21:29:58 -07004884 unlockEffectChains(effectChains);
4885 usleep(kRecordThreadSleepUs);
Eric Laurent49f02be2009-11-19 09:00:56 -08004886 continue;
4887 }
Eric Laurent464d5b32011-06-17 21:29:58 -07004888 for (size_t i = 0; i < effectChains.size(); i ++) {
4889 effectChains[i]->process_l();
4890 }
Eric Laurent464d5b32011-06-17 21:29:58 -07004891
Eric Laurenta553c252009-07-17 12:17:14 -07004892 buffer.frameCount = mFrameCount;
Glenn Kastenc2db1192012-02-22 10:47:35 -08004893 if (CC_LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) {
Eric Laurenta553c252009-07-17 12:17:14 -07004894 size_t framesOut = buffer.frameCount;
Glenn Kastenc434c902011-12-13 11:53:26 -08004895 if (mResampler == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -07004896 // no resampling
4897 while (framesOut) {
4898 size_t framesIn = mFrameCount - mRsmpInIndex;
4899 if (framesIn) {
4900 int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize;
4901 int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize;
4902 if (framesIn > framesOut)
4903 framesIn = framesOut;
4904 mRsmpInIndex += framesIn;
4905 framesOut -= framesIn;
Eric Laurentb0a01472010-05-14 05:45:46 -07004906 if ((int)mChannelCount == mReqChannelCount ||
Dima Zavin24fc2fb2011-04-19 22:30:36 -07004907 mFormat != AUDIO_FORMAT_PCM_16_BIT) {
Eric Laurenta553c252009-07-17 12:17:14 -07004908 memcpy(dst, src, framesIn * mFrameSize);
4909 } else {
4910 int16_t *src16 = (int16_t *)src;
4911 int16_t *dst16 = (int16_t *)dst;
4912 if (mChannelCount == 1) {
4913 while (framesIn--) {
4914 *dst16++ = *src16;
4915 *dst16++ = *src16++;
4916 }
4917 } else {
4918 while (framesIn--) {
4919 *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1);
4920 src16 += 2;
4921 }
4922 }
4923 }
4924 }
4925 if (framesOut && mFrameCount == mRsmpInIndex) {
Eric Laurenta553c252009-07-17 12:17:14 -07004926 if (framesOut == mFrameCount &&
Dima Zavin24fc2fb2011-04-19 22:30:36 -07004927 ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) {
Dima Zavin31f188892011-04-18 16:57:27 -07004928 mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes);
Eric Laurenta553c252009-07-17 12:17:14 -07004929 framesOut = 0;
4930 } else {
Dima Zavin31f188892011-04-18 16:57:27 -07004931 mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes);
Eric Laurenta553c252009-07-17 12:17:14 -07004932 mRsmpInIndex = 0;
4933 }
Eric Laurent9cc489a22009-12-05 05:20:01 -08004934 if (mBytesRead < 0) {
Steve Block3762c312012-01-06 19:20:56 +00004935 ALOGE("Error reading audio input");
Eric Laurent9cc489a22009-12-05 05:20:01 -08004936 if (mActiveTrack->mState == TrackBase::ACTIVE) {
Eric Laurentba8811f2010-03-02 18:38:06 -08004937 // Force input into standby so that it tries to
4938 // recover at next read attempt
Dima Zavin31f188892011-04-18 16:57:27 -07004939 mInput->stream->common.standby(&mInput->stream->common);
Eric Laurent464d5b32011-06-17 21:29:58 -07004940 usleep(kRecordThreadSleepUs);
Eric Laurent9cc489a22009-12-05 05:20:01 -08004941 }
Eric Laurenta553c252009-07-17 12:17:14 -07004942 mRsmpInIndex = mFrameCount;
4943 framesOut = 0;
4944 buffer.frameCount = 0;
4945 }
4946 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004947 }
4948 } else {
Eric Laurenta553c252009-07-17 12:17:14 -07004949 // resampling
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004950
Eric Laurenta553c252009-07-17 12:17:14 -07004951 memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t));
4952 // alter output frame count as if we were expecting stereo samples
4953 if (mChannelCount == 1 && mReqChannelCount == 1) {
4954 framesOut >>= 1;
4955 }
4956 mResampler->resample(mRsmpOutBuffer, framesOut, this);
4957 // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer()
4958 // are 32 bit aligned which should be always true.
4959 if (mChannelCount == 2 && mReqChannelCount == 1) {
Glenn Kasten490909d2011-12-15 09:52:39 -08004960 ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut);
Eric Laurenta553c252009-07-17 12:17:14 -07004961 // the resampler always outputs stereo samples: do post stereo to mono conversion
4962 int16_t *src = (int16_t *)mRsmpOutBuffer;
4963 int16_t *dst = buffer.i16;
4964 while (framesOut--) {
4965 *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1);
4966 src += 2;
4967 }
4968 } else {
Glenn Kasten490909d2011-12-15 09:52:39 -08004969 ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut);
Eric Laurenta553c252009-07-17 12:17:14 -07004970 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004971
Eric Laurenta553c252009-07-17 12:17:14 -07004972 }
4973 mActiveTrack->releaseBuffer(&buffer);
4974 mActiveTrack->overflow();
4975 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004976 // client isn't retrieving buffers fast enough
4977 else {
Eric Laurent4712baa2010-09-30 16:12:31 -07004978 if (!mActiveTrack->setOverflow()) {
4979 nsecs_t now = systemTime();
Glenn Kastencbfe2e12011-12-13 11:04:14 -08004980 if ((now - lastWarning) > kWarningThrottleNs) {
Steve Block8564c8d2012-01-05 23:22:43 +00004981 ALOGW("RecordThread: buffer overflow");
Eric Laurent4712baa2010-09-30 16:12:31 -07004982 lastWarning = now;
4983 }
4984 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004985 // Release the processor for a while before asking for a new buffer.
4986 // This will give the application more chance to read from the buffer and
4987 // clear the overflow.
Eric Laurent464d5b32011-06-17 21:29:58 -07004988 usleep(kRecordThreadSleepUs);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004989 }
4990 }
Eric Laurent21b5c472011-07-26 20:54:46 -07004991 // enable changes in effect chain
4992 unlockEffectChains(effectChains);
Eric Laurent464d5b32011-06-17 21:29:58 -07004993 effectChains.clear();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004994 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004995
Eric Laurenta553c252009-07-17 12:17:14 -07004996 if (!mStandby) {
Dima Zavin31f188892011-04-18 16:57:27 -07004997 mInput->stream->common.standby(&mInput->stream->common);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08004998 }
Eric Laurenta553c252009-07-17 12:17:14 -07004999 mActiveTrack.clear();
5000
Eric Laurent49f02be2009-11-19 09:00:56 -08005001 mStartStopCond.broadcast();
5002
Eric Laurent6dbdc402011-07-22 09:04:31 -07005003 releaseWakeLock();
5004
Steve Block71f2cf12011-10-20 11:56:00 +01005005 ALOGV("RecordThread %p exiting", this);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07005006 return false;
5007}
5008
Eric Laurent464d5b32011-06-17 21:29:58 -07005009
5010sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l(
5011 const sp<AudioFlinger::Client>& client,
5012 uint32_t sampleRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005013 audio_format_t format,
Eric Laurent464d5b32011-06-17 21:29:58 -07005014 int channelMask,
5015 int frameCount,
Eric Laurent464d5b32011-06-17 21:29:58 -07005016 int sessionId,
5017 status_t *status)
5018{
5019 sp<RecordTrack> track;
5020 status_t lStatus;
5021
5022 lStatus = initCheck();
5023 if (lStatus != NO_ERROR) {
Steve Block3762c312012-01-06 19:20:56 +00005024 ALOGE("Audio driver not initialized.");
Eric Laurent464d5b32011-06-17 21:29:58 -07005025 goto Exit;
5026 }
5027
5028 { // scope for mLock
5029 Mutex::Autolock _l(mLock);
5030
5031 track = new RecordTrack(this, client, sampleRate,
Glenn Kasten35269822012-02-21 10:35:56 -08005032 format, channelMask, frameCount, sessionId);
Eric Laurent464d5b32011-06-17 21:29:58 -07005033
Glenn Kasten706b6182012-01-20 13:44:40 -08005034 if (track->getCblk() == 0) {
Eric Laurent464d5b32011-06-17 21:29:58 -07005035 lStatus = NO_MEMORY;
5036 goto Exit;
5037 }
5038
5039 mTrack = track.get();
Eric Laurent6639b552011-08-01 09:52:20 -07005040 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
5041 bool suspend = audio_is_bluetooth_sco_device(
Eric Laurent2d95dfb2011-08-29 12:42:48 -07005042 (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff();
Eric Laurent6639b552011-08-01 09:52:20 -07005043 setEffectSuspended_l(FX_IID_AEC, suspend, sessionId);
5044 setEffectSuspended_l(FX_IID_NS, suspend, sessionId);
Eric Laurent464d5b32011-06-17 21:29:58 -07005045 }
5046 lStatus = NO_ERROR;
5047
5048Exit:
5049 if (status) {
5050 *status = lStatus;
5051 }
5052 return track;
5053}
5054
Glenn Kasten6a20b262012-02-02 10:56:47 -08005055status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, pid_t tid)
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07005056{
Glenn Kasten6a20b262012-02-02 10:56:47 -08005057 ALOGV("RecordThread::start tid=%d", tid);
Glenn Kasten18db49a2012-03-12 16:29:55 -07005058 sp<ThreadBase> strongMe = this;
Eric Laurent49f02be2009-11-19 09:00:56 -08005059 status_t status = NO_ERROR;
5060 {
Glenn Kasten325ee1c2012-01-05 15:41:56 -08005061 AutoMutex lock(mLock);
Eric Laurent49f02be2009-11-19 09:00:56 -08005062 if (mActiveTrack != 0) {
5063 if (recordTrack != mActiveTrack.get()) {
5064 status = -EBUSY;
5065 } else if (mActiveTrack->mState == TrackBase::PAUSING) {
Eric Laurent9cc489a22009-12-05 05:20:01 -08005066 mActiveTrack->mState = TrackBase::ACTIVE;
Eric Laurent49f02be2009-11-19 09:00:56 -08005067 }
5068 return status;
5069 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07005070
Eric Laurent49f02be2009-11-19 09:00:56 -08005071 recordTrack->mState = TrackBase::IDLE;
5072 mActiveTrack = recordTrack;
5073 mLock.unlock();
5074 status_t status = AudioSystem::startInput(mId);
5075 mLock.lock();
5076 if (status != NO_ERROR) {
5077 mActiveTrack.clear();
5078 return status;
5079 }
Eric Laurent9cc489a22009-12-05 05:20:01 -08005080 mRsmpInIndex = mFrameCount;
5081 mBytesRead = 0;
Eric Laurent4bb21c42011-02-28 16:52:51 -08005082 if (mResampler != NULL) {
5083 mResampler->reset();
5084 }
5085 mActiveTrack->mState = TrackBase::RESUMING;
Eric Laurent49f02be2009-11-19 09:00:56 -08005086 // signal thread to start
Steve Block71f2cf12011-10-20 11:56:00 +01005087 ALOGV("Signal record thread");
Eric Laurent49f02be2009-11-19 09:00:56 -08005088 mWaitWorkCV.signal();
5089 // do not wait for mStartStopCond if exiting
Glenn Kasten761286f2012-01-06 08:39:38 -08005090 if (exitPending()) {
Eric Laurent49f02be2009-11-19 09:00:56 -08005091 mActiveTrack.clear();
5092 status = INVALID_OPERATION;
5093 goto startError;
5094 }
5095 mStartStopCond.wait(mLock);
5096 if (mActiveTrack == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01005097 ALOGV("Record failed to start");
Eric Laurent49f02be2009-11-19 09:00:56 -08005098 status = BAD_VALUE;
5099 goto startError;
5100 }
Steve Block71f2cf12011-10-20 11:56:00 +01005101 ALOGV("Record started OK");
Eric Laurent49f02be2009-11-19 09:00:56 -08005102 return status;
Eric Laurenta553c252009-07-17 12:17:14 -07005103 }
Eric Laurent49f02be2009-11-19 09:00:56 -08005104startError:
5105 AudioSystem::stopInput(mId);
5106 return status;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07005107}
5108
Eric Laurenta553c252009-07-17 12:17:14 -07005109void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) {
Steve Block71f2cf12011-10-20 11:56:00 +01005110 ALOGV("RecordThread::stop");
Glenn Kasten18db49a2012-03-12 16:29:55 -07005111 sp<ThreadBase> strongMe = this;
Eric Laurent49f02be2009-11-19 09:00:56 -08005112 {
Glenn Kasten325ee1c2012-01-05 15:41:56 -08005113 AutoMutex lock(mLock);
Eric Laurent49f02be2009-11-19 09:00:56 -08005114 if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) {
5115 mActiveTrack->mState = TrackBase::PAUSING;
5116 // do not wait for mStartStopCond if exiting
Glenn Kasten761286f2012-01-06 08:39:38 -08005117 if (exitPending()) {
Eric Laurent49f02be2009-11-19 09:00:56 -08005118 return;
5119 }
5120 mStartStopCond.wait(mLock);
5121 // if we have been restarted, recordTrack == mActiveTrack.get() here
5122 if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) {
5123 mLock.unlock();
5124 AudioSystem::stopInput(mId);
5125 mLock.lock();
Steve Block71f2cf12011-10-20 11:56:00 +01005126 ALOGV("Record stopped OK");
Eric Laurent49f02be2009-11-19 09:00:56 -08005127 }
5128 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07005129 }
5130}
5131
Eric Laurenta553c252009-07-17 12:17:14 -07005132status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005133{
5134 const size_t SIZE = 256;
5135 char buffer[SIZE];
5136 String8 result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005137
Eric Laurent3fdb1262009-11-07 00:01:32 -08005138 snprintf(buffer, SIZE, "\nInput thread %p internals\n", this);
5139 result.append(buffer);
5140
5141 if (mActiveTrack != 0) {
5142 result.append("Active Track:\n");
Jean-Michel Trivi54392232011-05-24 15:53:33 -07005143 result.append(" Clien Fmt Chn mask Session Buf S SRate Serv User\n");
Eric Laurent3fdb1262009-11-07 00:01:32 -08005144 mActiveTrack->dump(buffer, SIZE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 result.append(buffer);
Eric Laurent3fdb1262009-11-07 00:01:32 -08005146
5147 snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex);
5148 result.append(buffer);
5149 snprintf(buffer, SIZE, "In size: %d\n", mInputBytes);
5150 result.append(buffer);
Glenn Kastenc434c902011-12-13 11:53:26 -08005151 snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL));
Eric Laurent3fdb1262009-11-07 00:01:32 -08005152 result.append(buffer);
5153 snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount);
5154 result.append(buffer);
5155 snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate);
5156 result.append(buffer);
5157
5158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005159 } else {
5160 result.append("No record client\n");
5161 }
5162 write(fd, result.string(), result.size());
Eric Laurent3fdb1262009-11-07 00:01:32 -08005163
5164 dumpBase(fd, args);
Eric Laurent1345d332011-07-24 17:49:51 -07005165 dumpEffectChains(fd, args);
Eric Laurent3fdb1262009-11-07 00:01:32 -08005166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 return NO_ERROR;
5168}
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07005169
Glenn Kastenc2db1192012-02-22 10:47:35 -08005170// AudioBufferProvider interface
John Grossmand8cf2962012-02-08 16:37:41 -08005171status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts)
Eric Laurenta553c252009-07-17 12:17:14 -07005172{
5173 size_t framesReq = buffer->frameCount;
5174 size_t framesReady = mFrameCount - mRsmpInIndex;
5175 int channelCount;
5176
5177 if (framesReady == 0) {
Dima Zavin31f188892011-04-18 16:57:27 -07005178 mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes);
Eric Laurent9cc489a22009-12-05 05:20:01 -08005179 if (mBytesRead < 0) {
Steve Block3762c312012-01-06 19:20:56 +00005180 ALOGE("RecordThread::getNextBuffer() Error reading audio input");
Eric Laurent9cc489a22009-12-05 05:20:01 -08005181 if (mActiveTrack->mState == TrackBase::ACTIVE) {
Eric Laurentba8811f2010-03-02 18:38:06 -08005182 // Force input into standby so that it tries to
5183 // recover at next read attempt
Dima Zavin31f188892011-04-18 16:57:27 -07005184 mInput->stream->common.standby(&mInput->stream->common);
Eric Laurent464d5b32011-06-17 21:29:58 -07005185 usleep(kRecordThreadSleepUs);
Eric Laurent9cc489a22009-12-05 05:20:01 -08005186 }
Glenn Kastenc434c902011-12-13 11:53:26 -08005187 buffer->raw = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07005188 buffer->frameCount = 0;
5189 return NOT_ENOUGH_DATA;
5190 }
5191 mRsmpInIndex = 0;
5192 framesReady = mFrameCount;
5193 }
5194
5195 if (framesReq > framesReady) {
5196 framesReq = framesReady;
5197 }
5198
5199 if (mChannelCount == 1 && mReqChannelCount == 2) {
5200 channelCount = 1;
5201 } else {
5202 channelCount = 2;
5203 }
5204 buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount;
5205 buffer->frameCount = framesReq;
5206 return NO_ERROR;
5207}
5208
Glenn Kastenc2db1192012-02-22 10:47:35 -08005209// AudioBufferProvider interface
Eric Laurenta553c252009-07-17 12:17:14 -07005210void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer)
5211{
5212 mRsmpInIndex += buffer->frameCount;
5213 buffer->frameCount = 0;
5214}
5215
5216bool AudioFlinger::RecordThread::checkForNewParameters_l()
5217{
5218 bool reconfig = false;
5219
Eric Laurent8fce46a2009-08-04 09:45:33 -07005220 while (!mNewParameters.isEmpty()) {
Eric Laurenta553c252009-07-17 12:17:14 -07005221 status_t status = NO_ERROR;
Eric Laurent8fce46a2009-08-04 09:45:33 -07005222 String8 keyValuePair = mNewParameters[0];
5223 AudioParameter param = AudioParameter(keyValuePair);
Eric Laurenta553c252009-07-17 12:17:14 -07005224 int value;
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005225 audio_format_t reqFormat = mFormat;
Eric Laurenta553c252009-07-17 12:17:14 -07005226 int reqSamplingRate = mReqSampleRate;
5227 int reqChannelCount = mReqChannelCount;
Eric Laurent8fce46a2009-08-04 09:45:33 -07005228
Eric Laurenta553c252009-07-17 12:17:14 -07005229 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
5230 reqSamplingRate = value;
5231 reconfig = true;
5232 }
5233 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005234 reqFormat = (audio_format_t) value;
Eric Laurenta553c252009-07-17 12:17:14 -07005235 reconfig = true;
5236 }
5237 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005238 reqChannelCount = popcount(value);
Eric Laurenta553c252009-07-17 12:17:14 -07005239 reconfig = true;
5240 }
5241 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
5242 // do not accept frame count changes if tracks are open as the track buffer
Glenn Kastenb3db2132012-01-19 08:59:58 -08005243 // size depends on frame count and correct behavior would not be guaranteed
Eric Laurenta553c252009-07-17 12:17:14 -07005244 // if frame count is changed after track creation
5245 if (mActiveTrack != 0) {
5246 status = INVALID_OPERATION;
5247 } else {
5248 reconfig = true;
5249 }
5250 }
Eric Laurent464d5b32011-06-17 21:29:58 -07005251 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
5252 // forward device change to effects that have requested to be
5253 // aware of attached audio device.
5254 for (size_t i = 0; i < mEffectChains.size(); i++) {
5255 mEffectChains[i]->setDevice_l(value);
5256 }
5257 // store input device and output device but do not forward output device to audio HAL.
5258 // Note that status is ignored by the caller for output device
5259 // (see AudioFlinger::setParameters()
5260 if (value & AUDIO_DEVICE_OUT_ALL) {
5261 mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL);
5262 status = BAD_VALUE;
5263 } else {
5264 mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL);
Eric Laurent6639b552011-08-01 09:52:20 -07005265 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
5266 if (mTrack != NULL) {
5267 bool suspend = audio_is_bluetooth_sco_device(
Eric Laurent2d95dfb2011-08-29 12:42:48 -07005268 (audio_devices_t)value) && mAudioFlinger->btNrecIsOff();
Eric Laurent6639b552011-08-01 09:52:20 -07005269 setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId());
5270 setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId());
5271 }
Eric Laurent464d5b32011-06-17 21:29:58 -07005272 }
5273 mDevice |= (uint32_t)value;
5274 }
Eric Laurenta553c252009-07-17 12:17:14 -07005275 if (status == NO_ERROR) {
Dima Zavin31f188892011-04-18 16:57:27 -07005276 status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string());
Eric Laurenta553c252009-07-17 12:17:14 -07005277 if (status == INVALID_OPERATION) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07005278 mInput->stream->common.standby(&mInput->stream->common);
5279 status = mInput->stream->common.set_parameters(&mInput->stream->common,
5280 keyValuePair.string());
Eric Laurenta553c252009-07-17 12:17:14 -07005281 }
5282 if (reconfig) {
5283 if (status == BAD_VALUE &&
Dima Zavin31f188892011-04-18 16:57:27 -07005284 reqFormat == mInput->stream->common.get_format(&mInput->stream->common) &&
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005285 reqFormat == AUDIO_FORMAT_PCM_16_BIT &&
Dima Zavin31f188892011-04-18 16:57:27 -07005286 ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) &&
Glenn Kastend8f32c22012-03-08 12:32:47 -08005287 popcount(mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_2 &&
5288 (reqChannelCount <= FCC_2)) {
Eric Laurenta553c252009-07-17 12:17:14 -07005289 status = NO_ERROR;
5290 }
5291 if (status == NO_ERROR) {
5292 readInputParameters();
Eric Laurent8fce46a2009-08-04 09:45:33 -07005293 sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED);
Eric Laurenta553c252009-07-17 12:17:14 -07005294 }
5295 }
5296 }
Eric Laurent3fdb1262009-11-07 00:01:32 -08005297
5298 mNewParameters.removeAt(0);
5299
Eric Laurenta553c252009-07-17 12:17:14 -07005300 mParamStatus = status;
5301 mParamCond.signal();
Eric Laurent5f37be32011-09-13 11:40:21 -07005302 // wait for condition with time out in case the thread calling ThreadBase::setParameters()
5303 // already timed out waiting for the status and will never signal the condition.
Glenn Kastencbfe2e12011-12-13 11:04:14 -08005304 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
Eric Laurenta553c252009-07-17 12:17:14 -07005305 }
5306 return reconfig;
5307}
5308
5309String8 AudioFlinger::RecordThread::getParameters(const String8& keys)
5310{
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005311 char *s;
Eric Laurent828b9772011-08-07 16:32:26 -07005312 String8 out_s8 = String8();
5313
5314 Mutex::Autolock _l(mLock);
5315 if (initCheck() != NO_ERROR) {
5316 return out_s8;
5317 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005318
Dima Zavin31f188892011-04-18 16:57:27 -07005319 s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string());
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005320 out_s8 = String8(s);
5321 free(s);
5322 return out_s8;
Eric Laurenta553c252009-07-17 12:17:14 -07005323}
5324
Eric Laurenteb8f850d2010-05-14 03:26:45 -07005325void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) {
Eric Laurenta553c252009-07-17 12:17:14 -07005326 AudioSystem::OutputDescriptor desc;
Glenn Kasten3694ec12012-01-27 16:47:15 -08005327 void *param2 = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07005328
5329 switch (event) {
5330 case AudioSystem::INPUT_OPENED:
5331 case AudioSystem::INPUT_CONFIG_CHANGED:
Jean-Michel Trivi54392232011-05-24 15:53:33 -07005332 desc.channels = mChannelMask;
Eric Laurenta553c252009-07-17 12:17:14 -07005333 desc.samplingRate = mSampleRate;
5334 desc.format = mFormat;
5335 desc.frameCount = mFrameCount;
5336 desc.latency = 0;
5337 param2 = &desc;
5338 break;
5339
5340 case AudioSystem::INPUT_CLOSED:
5341 default:
5342 break;
5343 }
Eric Laurent49f02be2009-11-19 09:00:56 -08005344 mAudioFlinger->audioConfigChanged_l(event, mId, param2);
Eric Laurenta553c252009-07-17 12:17:14 -07005345}
5346
5347void AudioFlinger::RecordThread::readInputParameters()
5348{
Glenn Kasten2589cf92012-01-27 18:08:45 -08005349 delete mRsmpInBuffer;
5350 // mRsmpInBuffer is always assigned a new[] below
5351 delete mRsmpOutBuffer;
5352 mRsmpOutBuffer = NULL;
5353 delete mResampler;
Glenn Kastenc434c902011-12-13 11:53:26 -08005354 mResampler = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07005355
Dima Zavin31f188892011-04-18 16:57:27 -07005356 mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common);
Jean-Michel Trivi54392232011-05-24 15:53:33 -07005357 mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common);
5358 mChannelCount = (uint16_t)popcount(mChannelMask);
Dima Zavin31f188892011-04-18 16:57:27 -07005359 mFormat = mInput->stream->common.get_format(&mInput->stream->common);
Glenn Kastenfaf354d2012-01-11 09:48:27 -08005360 mFrameSize = audio_stream_frame_size(&mInput->stream->common);
Dima Zavin31f188892011-04-18 16:57:27 -07005361 mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common);
Eric Laurenta553c252009-07-17 12:17:14 -07005362 mFrameCount = mInputBytes / mFrameSize;
5363 mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount];
5364
Glenn Kastend8f32c22012-03-08 12:32:47 -08005365 if (mSampleRate != mReqSampleRate && mChannelCount <= FCC_2 && mReqChannelCount <= FCC_2)
Eric Laurenta553c252009-07-17 12:17:14 -07005366 {
5367 int channelCount;
Glenn Kasten18db49a2012-03-12 16:29:55 -07005368 // optimization: if mono to mono, use the resampler in stereo to stereo mode to avoid
5369 // stereo to mono post process as the resampler always outputs stereo.
Eric Laurenta553c252009-07-17 12:17:14 -07005370 if (mChannelCount == 1 && mReqChannelCount == 2) {
5371 channelCount = 1;
5372 } else {
5373 channelCount = 2;
5374 }
5375 mResampler = AudioResampler::create(16, channelCount, mReqSampleRate);
5376 mResampler->setSampleRate(mSampleRate);
5377 mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN);
5378 mRsmpOutBuffer = new int32_t[mFrameCount * 2];
5379
5380 // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples
5381 if (mChannelCount == 1 && mReqChannelCount == 1) {
5382 mFrameCount >>= 1;
5383 }
5384
5385 }
5386 mRsmpInIndex = mFrameCount;
5387}
5388
Eric Laurent47d0a922010-02-26 02:47:27 -08005389unsigned int AudioFlinger::RecordThread::getInputFramesLost()
5390{
Eric Laurent828b9772011-08-07 16:32:26 -07005391 Mutex::Autolock _l(mLock);
5392 if (initCheck() != NO_ERROR) {
5393 return 0;
5394 }
5395
Dima Zavin31f188892011-04-18 16:57:27 -07005396 return mInput->stream->get_input_frames_lost(mInput->stream);
Eric Laurent47d0a922010-02-26 02:47:27 -08005397}
5398
Eric Laurent464d5b32011-06-17 21:29:58 -07005399uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId)
5400{
5401 Mutex::Autolock _l(mLock);
5402 uint32_t result = 0;
5403 if (getEffectChain_l(sessionId) != 0) {
5404 result = EFFECT_SESSION;
5405 }
5406
5407 if (mTrack != NULL && sessionId == mTrack->sessionId()) {
5408 result |= TRACK_SESSION;
5409 }
5410
5411 return result;
5412}
5413
Eric Laurent6639b552011-08-01 09:52:20 -07005414AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track()
5415{
5416 Mutex::Autolock _l(mLock);
5417 return mTrack;
5418}
5419
Glenn Kasten5b0135e2012-01-26 09:46:34 -08005420AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() const
Eric Laurent828b9772011-08-07 16:32:26 -07005421{
5422 Mutex::Autolock _l(mLock);
5423 return mInput;
5424}
5425
5426AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput()
5427{
5428 Mutex::Autolock _l(mLock);
5429 AudioStreamIn *input = mInput;
5430 mInput = NULL;
5431 return input;
5432}
5433
5434// this method must always be called either with ThreadBase mLock held or inside the thread loop
5435audio_stream_t* AudioFlinger::RecordThread::stream()
5436{
5437 if (mInput == NULL) {
5438 return NULL;
5439 }
5440 return &mInput->stream->common;
5441}
5442
5443
Eric Laurenta553c252009-07-17 12:17:14 -07005444// ----------------------------------------------------------------------------
5445
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005446audio_io_handle_t AudioFlinger::openOutput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -07005447 uint32_t *pSamplingRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005448 audio_format_t *pFormat,
Eric Laurenta553c252009-07-17 12:17:14 -07005449 uint32_t *pChannels,
5450 uint32_t *pLatencyMs,
Glenn Kasten28b269f2012-03-07 09:15:37 -08005451 audio_policy_output_flags_t flags)
Eric Laurenta553c252009-07-17 12:17:14 -07005452{
5453 status_t status;
5454 PlaybackThread *thread = NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07005455 uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005456 audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT;
Eric Laurenta553c252009-07-17 12:17:14 -07005457 uint32_t channels = pChannels ? *pChannels : 0;
5458 uint32_t latency = pLatencyMs ? *pLatencyMs : 0;
Dima Zavin31f188892011-04-18 16:57:27 -07005459 audio_stream_out_t *outStream;
5460 audio_hw_device_t *outHwDev;
Eric Laurenta553c252009-07-17 12:17:14 -07005461
Steve Block71f2cf12011-10-20 11:56:00 +01005462 ALOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
Eric Laurenta553c252009-07-17 12:17:14 -07005463 pDevices ? *pDevices : 0,
5464 samplingRate,
5465 format,
5466 channels,
5467 flags);
5468
5469 if (pDevices == NULL || *pDevices == 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -07005470 return 0;
Eric Laurenta553c252009-07-17 12:17:14 -07005471 }
Dima Zavin31f188892011-04-18 16:57:27 -07005472
Eric Laurenta553c252009-07-17 12:17:14 -07005473 Mutex::Autolock _l(mLock);
5474
Dima Zavin31f188892011-04-18 16:57:27 -07005475 outHwDev = findSuitableHwDev_l(*pDevices);
5476 if (outHwDev == NULL)
5477 return 0;
5478
Glenn Kasten23c9c742012-02-02 14:16:03 -08005479 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005480 status = outHwDev->open_output_stream(outHwDev, *pDevices, &format,
Dima Zavin31f188892011-04-18 16:57:27 -07005481 &channels, &samplingRate, &outStream);
Glenn Kasten23c9c742012-02-02 14:16:03 -08005482 mHardwareStatus = AUDIO_HW_IDLE;
Steve Block71f2cf12011-10-20 11:56:00 +01005483 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d",
Dima Zavin31f188892011-04-18 16:57:27 -07005484 outStream,
Eric Laurenta553c252009-07-17 12:17:14 -07005485 samplingRate,
5486 format,
5487 channels,
5488 status);
5489
Dima Zavin31f188892011-04-18 16:57:27 -07005490 if (outStream != NULL) {
5491 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005492 audio_io_handle_t id = nextUniqueId();
Dima Zavin31f188892011-04-18 16:57:27 -07005493
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005494 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) ||
5495 (format != AUDIO_FORMAT_PCM_16_BIT) ||
5496 (channels != AUDIO_CHANNEL_OUT_STEREO)) {
Eric Laurent65b65452010-06-01 23:49:17 -07005497 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block71f2cf12011-10-20 11:56:00 +01005498 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Eric Laurenta553c252009-07-17 12:17:14 -07005499 } else {
Eric Laurent65b65452010-06-01 23:49:17 -07005500 thread = new MixerThread(this, output, id, *pDevices);
Steve Block71f2cf12011-10-20 11:56:00 +01005501 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Eric Laurenta553c252009-07-17 12:17:14 -07005502 }
Eric Laurent65b65452010-06-01 23:49:17 -07005503 mPlaybackThreads.add(id, thread);
Eric Laurenta553c252009-07-17 12:17:14 -07005504
Glenn Kasten3694ec12012-01-27 16:47:15 -08005505 if (pSamplingRate != NULL) *pSamplingRate = samplingRate;
5506 if (pFormat != NULL) *pFormat = format;
5507 if (pChannels != NULL) *pChannels = channels;
5508 if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
Eric Laurent9cc489a22009-12-05 05:20:01 -08005509
Eric Laurenteb8f850d2010-05-14 03:26:45 -07005510 // notify client processes of the new output creation
5511 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurent65b65452010-06-01 23:49:17 -07005512 return id;
Eric Laurenta553c252009-07-17 12:17:14 -07005513 }
5514
Eric Laurent9cc489a22009-12-05 05:20:01 -08005515 return 0;
Eric Laurenta553c252009-07-17 12:17:14 -07005516}
5517
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005518audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
5519 audio_io_handle_t output2)
Eric Laurenta553c252009-07-17 12:17:14 -07005520{
5521 Mutex::Autolock _l(mLock);
Eric Laurentddb78e72009-07-28 08:44:33 -07005522 MixerThread *thread1 = checkMixerThread_l(output1);
5523 MixerThread *thread2 = checkMixerThread_l(output2);
Eric Laurenta553c252009-07-17 12:17:14 -07005524
Eric Laurentddb78e72009-07-28 08:44:33 -07005525 if (thread1 == NULL || thread2 == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00005526 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2);
Eric Laurentddb78e72009-07-28 08:44:33 -07005527 return 0;
Eric Laurenta553c252009-07-17 12:17:14 -07005528 }
5529
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005530 audio_io_handle_t id = nextUniqueId();
Eric Laurent65b65452010-06-01 23:49:17 -07005531 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
Eric Laurentddb78e72009-07-28 08:44:33 -07005532 thread->addOutputTrack(thread2);
Eric Laurent65b65452010-06-01 23:49:17 -07005533 mPlaybackThreads.add(id, thread);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07005534 // notify client processes of the new output creation
5535 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurent65b65452010-06-01 23:49:17 -07005536 return id;
Eric Laurenta553c252009-07-17 12:17:14 -07005537}
5538
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005539status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Eric Laurenta553c252009-07-17 12:17:14 -07005540{
Eric Laurent49018a52009-08-04 08:37:05 -07005541 // keep strong reference on the playback thread so that
5542 // it is not destroyed while exit() is executed
Glenn Kasten18db49a2012-03-12 16:29:55 -07005543 sp<PlaybackThread> thread;
Eric Laurenta553c252009-07-17 12:17:14 -07005544 {
5545 Mutex::Autolock _l(mLock);
5546 thread = checkPlaybackThread_l(output);
5547 if (thread == NULL) {
5548 return BAD_VALUE;
5549 }
5550
Steve Block71f2cf12011-10-20 11:56:00 +01005551 ALOGV("closeOutput() %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -07005552
Eric Laurent464d5b32011-06-17 21:29:58 -07005553 if (thread->type() == ThreadBase::MIXER) {
Eric Laurenta553c252009-07-17 12:17:14 -07005554 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent464d5b32011-06-17 21:29:58 -07005555 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Eric Laurentddb78e72009-07-28 08:44:33 -07005556 DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Eric Laurent49018a52009-08-04 08:37:05 -07005557 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurenta553c252009-07-17 12:17:14 -07005558 }
5559 }
5560 }
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08005561 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Eric Laurentddb78e72009-07-28 08:44:33 -07005562 mPlaybackThreads.removeItem(output);
Eric Laurenta553c252009-07-17 12:17:14 -07005563 }
5564 thread->exit();
Glenn Kasten761286f2012-01-06 08:39:38 -08005565 // The thread entity (active unit of execution) is no longer running here,
5566 // but the ThreadBase container still exists.
Eric Laurenta553c252009-07-17 12:17:14 -07005567
Eric Laurent464d5b32011-06-17 21:29:58 -07005568 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurent828b9772011-08-07 16:32:26 -07005569 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten81916df2012-03-08 12:18:35 -08005570 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurent828b9772011-08-07 16:32:26 -07005571 // from now on thread->mOutput is NULL
Dima Zavin31f188892011-04-18 16:57:27 -07005572 out->hwDev->close_output_stream(out->hwDev, out->stream);
5573 delete out;
Eric Laurent49018a52009-08-04 08:37:05 -07005574 }
Eric Laurenta553c252009-07-17 12:17:14 -07005575 return NO_ERROR;
5576}
5577
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005578status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Eric Laurenta553c252009-07-17 12:17:14 -07005579{
5580 Mutex::Autolock _l(mLock);
5581 PlaybackThread *thread = checkPlaybackThread_l(output);
5582
5583 if (thread == NULL) {
5584 return BAD_VALUE;
5585 }
5586
Steve Block71f2cf12011-10-20 11:56:00 +01005587 ALOGV("suspendOutput() %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -07005588 thread->suspend();
5589
5590 return NO_ERROR;
5591}
5592
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005593status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Eric Laurenta553c252009-07-17 12:17:14 -07005594{
5595 Mutex::Autolock _l(mLock);
5596 PlaybackThread *thread = checkPlaybackThread_l(output);
5597
5598 if (thread == NULL) {
5599 return BAD_VALUE;
5600 }
5601
Steve Block71f2cf12011-10-20 11:56:00 +01005602 ALOGV("restoreOutput() %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -07005603
5604 thread->restore();
5605
5606 return NO_ERROR;
5607}
5608
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005609audio_io_handle_t AudioFlinger::openInput(uint32_t *pDevices,
Eric Laurenta553c252009-07-17 12:17:14 -07005610 uint32_t *pSamplingRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005611 audio_format_t *pFormat,
Eric Laurenta553c252009-07-17 12:17:14 -07005612 uint32_t *pChannels,
Glenn Kasten882c0a22012-01-27 12:32:34 -08005613 audio_in_acoustics_t acoustics)
Eric Laurenta553c252009-07-17 12:17:14 -07005614{
5615 status_t status;
5616 RecordThread *thread = NULL;
5617 uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005618 audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT;
Eric Laurenta553c252009-07-17 12:17:14 -07005619 uint32_t channels = pChannels ? *pChannels : 0;
5620 uint32_t reqSamplingRate = samplingRate;
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005621 audio_format_t reqFormat = format;
Eric Laurenta553c252009-07-17 12:17:14 -07005622 uint32_t reqChannels = channels;
Dima Zavin31f188892011-04-18 16:57:27 -07005623 audio_stream_in_t *inStream;
5624 audio_hw_device_t *inHwDev;
Eric Laurenta553c252009-07-17 12:17:14 -07005625
5626 if (pDevices == NULL || *pDevices == 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -07005627 return 0;
Eric Laurenta553c252009-07-17 12:17:14 -07005628 }
Dima Zavin31f188892011-04-18 16:57:27 -07005629
Eric Laurenta553c252009-07-17 12:17:14 -07005630 Mutex::Autolock _l(mLock);
5631
Dima Zavin31f188892011-04-18 16:57:27 -07005632 inHwDev = findSuitableHwDev_l(*pDevices);
5633 if (inHwDev == NULL)
5634 return 0;
5635
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005636 status = inHwDev->open_input_stream(inHwDev, *pDevices, &format,
Dima Zavin31f188892011-04-18 16:57:27 -07005637 &channels, &samplingRate,
Glenn Kasten882c0a22012-01-27 12:32:34 -08005638 acoustics,
Dima Zavin31f188892011-04-18 16:57:27 -07005639 &inStream);
Steve Block71f2cf12011-10-20 11:56:00 +01005640 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, acoustics %x, status %d",
Dima Zavin31f188892011-04-18 16:57:27 -07005641 inStream,
Eric Laurenta553c252009-07-17 12:17:14 -07005642 samplingRate,
5643 format,
5644 channels,
5645 acoustics,
5646 status);
5647
5648 // If the input could not be opened with the requested parameters and we can handle the conversion internally,
5649 // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo
5650 // or stereo to mono conversions on 16 bit PCM inputs.
Dima Zavin31f188892011-04-18 16:57:27 -07005651 if (inStream == NULL && status == BAD_VALUE &&
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005652 reqFormat == format && format == AUDIO_FORMAT_PCM_16_BIT &&
Eric Laurenta553c252009-07-17 12:17:14 -07005653 (samplingRate <= 2 * reqSamplingRate) &&
Glenn Kastend8f32c22012-03-08 12:32:47 -08005654 (popcount(channels) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Steve Block71f2cf12011-10-20 11:56:00 +01005655 ALOGV("openInput() reopening with proposed sampling rate and channels");
Glenn Kasten0a204ed2012-01-12 12:27:51 -08005656 status = inHwDev->open_input_stream(inHwDev, *pDevices, &format,
Dima Zavin31f188892011-04-18 16:57:27 -07005657 &channels, &samplingRate,
Glenn Kasten882c0a22012-01-27 12:32:34 -08005658 acoustics,
Dima Zavin31f188892011-04-18 16:57:27 -07005659 &inStream);
Eric Laurenta553c252009-07-17 12:17:14 -07005660 }
5661
Dima Zavin31f188892011-04-18 16:57:27 -07005662 if (inStream != NULL) {
5663 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
5664
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005665 audio_io_handle_t id = nextUniqueId();
Eric Laurent464d5b32011-06-17 21:29:58 -07005666 // Start record thread
5667 // RecorThread require both input and output device indication to forward to audio
5668 // pre processing modules
5669 uint32_t device = (*pDevices) | primaryOutputDevice_l();
5670 thread = new RecordThread(this,
5671 input,
5672 reqSamplingRate,
5673 reqChannels,
5674 id,
5675 device);
Eric Laurent65b65452010-06-01 23:49:17 -07005676 mRecordThreads.add(id, thread);
Steve Block71f2cf12011-10-20 11:56:00 +01005677 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten3694ec12012-01-27 16:47:15 -08005678 if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate;
5679 if (pFormat != NULL) *pFormat = format;
5680 if (pChannels != NULL) *pChannels = reqChannels;
Eric Laurenta553c252009-07-17 12:17:14 -07005681
Dima Zavin31f188892011-04-18 16:57:27 -07005682 input->stream->common.standby(&input->stream->common);
Eric Laurent9cc489a22009-12-05 05:20:01 -08005683
Eric Laurenteb8f850d2010-05-14 03:26:45 -07005684 // notify client processes of the new input creation
5685 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
Eric Laurent65b65452010-06-01 23:49:17 -07005686 return id;
Eric Laurenta553c252009-07-17 12:17:14 -07005687 }
5688
Eric Laurent9cc489a22009-12-05 05:20:01 -08005689 return 0;
Eric Laurenta553c252009-07-17 12:17:14 -07005690}
5691
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005692status_t AudioFlinger::closeInput(audio_io_handle_t input)
Eric Laurenta553c252009-07-17 12:17:14 -07005693{
Eric Laurent49018a52009-08-04 08:37:05 -07005694 // keep strong reference on the record thread so that
5695 // it is not destroyed while exit() is executed
Glenn Kasten18db49a2012-03-12 16:29:55 -07005696 sp<RecordThread> thread;
Eric Laurenta553c252009-07-17 12:17:14 -07005697 {
5698 Mutex::Autolock _l(mLock);
5699 thread = checkRecordThread_l(input);
5700 if (thread == NULL) {
5701 return BAD_VALUE;
5702 }
5703
Steve Block71f2cf12011-10-20 11:56:00 +01005704 ALOGV("closeInput() %d", input);
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08005705 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Eric Laurentddb78e72009-07-28 08:44:33 -07005706 mRecordThreads.removeItem(input);
Eric Laurenta553c252009-07-17 12:17:14 -07005707 }
5708 thread->exit();
Glenn Kasten761286f2012-01-06 08:39:38 -08005709 // The thread entity (active unit of execution) is no longer running here,
5710 // but the ThreadBase container still exists.
Eric Laurenta553c252009-07-17 12:17:14 -07005711
Eric Laurent828b9772011-08-07 16:32:26 -07005712 AudioStreamIn *in = thread->clearInput();
Glenn Kasten81916df2012-03-08 12:18:35 -08005713 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurent828b9772011-08-07 16:32:26 -07005714 // from now on thread->mInput is NULL
Dima Zavin31f188892011-04-18 16:57:27 -07005715 in->hwDev->close_input_stream(in->hwDev, in->stream);
5716 delete in;
Eric Laurent49018a52009-08-04 08:37:05 -07005717
Eric Laurenta553c252009-07-17 12:17:14 -07005718 return NO_ERROR;
5719}
5720
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005721status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Eric Laurenta553c252009-07-17 12:17:14 -07005722{
5723 Mutex::Autolock _l(mLock);
5724 MixerThread *dstThread = checkMixerThread_l(output);
5725 if (dstThread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00005726 ALOGW("setStreamOutput() bad output id %d", output);
Eric Laurenta553c252009-07-17 12:17:14 -07005727 return BAD_VALUE;
5728 }
5729
Steve Block71f2cf12011-10-20 11:56:00 +01005730 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07005731 audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream);
Eric Laurenta553c252009-07-17 12:17:14 -07005732
Eric Laurent05ce0942011-08-30 10:18:54 -07005733 dstThread->setStreamValid(stream, true);
5734
Eric Laurenta553c252009-07-17 12:17:14 -07005735 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentddb78e72009-07-28 08:44:33 -07005736 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08005737 if (thread != dstThread && thread->type() != ThreadBase::DIRECT) {
Eric Laurenta553c252009-07-17 12:17:14 -07005738 MixerThread *srcThread = (MixerThread *)thread;
Eric Laurent05ce0942011-08-30 10:18:54 -07005739 srcThread->setStreamValid(stream, false);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07005740 srcThread->invalidateTracks(stream);
Eric Laurenta553c252009-07-17 12:17:14 -07005741 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07005742 }
Eric Laurentd069f322009-09-01 05:56:26 -07005743
Eric Laurenta553c252009-07-17 12:17:14 -07005744 return NO_ERROR;
5745}
5746
Eric Laurent65b65452010-06-01 23:49:17 -07005747
5748int AudioFlinger::newAudioSessionId()
5749{
Eric Laurent464d5b32011-06-17 21:29:58 -07005750 return nextUniqueId();
Eric Laurent65b65452010-06-01 23:49:17 -07005751}
5752
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005753void AudioFlinger::acquireAudioSessionId(int audioSession)
5754{
5755 Mutex::Autolock _l(mLock);
Glenn Kastend153b1f2012-02-03 11:10:26 -08005756 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block71f2cf12011-10-20 11:56:00 +01005757 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten150d2382012-02-08 14:04:28 -08005758 size_t num = mAudioSessionRefs.size();
5759 for (size_t i = 0; i< num; i++) {
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005760 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten9a42ac92012-03-06 11:22:01 -08005761 if (ref->mSessionid == audioSession && ref->mPid == caller) {
5762 ref->mCnt++;
5763 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005764 return;
5765 }
5766 }
Glenn Kastendc3ac852012-01-25 15:28:08 -08005767 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
5768 ALOGV(" added new entry for %d", audioSession);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005769}
5770
5771void AudioFlinger::releaseAudioSessionId(int audioSession)
5772{
5773 Mutex::Autolock _l(mLock);
Glenn Kastend153b1f2012-02-03 11:10:26 -08005774 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block71f2cf12011-10-20 11:56:00 +01005775 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten150d2382012-02-08 14:04:28 -08005776 size_t num = mAudioSessionRefs.size();
5777 for (size_t i = 0; i< num; i++) {
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005778 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten9a42ac92012-03-06 11:22:01 -08005779 if (ref->mSessionid == audioSession && ref->mPid == caller) {
5780 ref->mCnt--;
5781 ALOGV(" decremented refcount to %d", ref->mCnt);
5782 if (ref->mCnt == 0) {
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005783 mAudioSessionRefs.removeAt(i);
5784 delete ref;
5785 purgeStaleEffects_l();
5786 }
5787 return;
5788 }
5789 }
Steve Block8564c8d2012-01-05 23:22:43 +00005790 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005791}
5792
5793void AudioFlinger::purgeStaleEffects_l() {
5794
Steve Block71f2cf12011-10-20 11:56:00 +01005795 ALOGV("purging stale effects");
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005796
5797 Vector< sp<EffectChain> > chains;
5798
5799 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
5800 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
5801 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
5802 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen2255a1e2011-08-12 14:14:39 -07005803 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
5804 chains.push(ec);
5805 }
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005806 }
5807 }
5808 for (size_t i = 0; i < mRecordThreads.size(); i++) {
5809 sp<RecordThread> t = mRecordThreads.valueAt(i);
5810 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
5811 sp<EffectChain> ec = t->mEffectChains[j];
5812 chains.push(ec);
5813 }
5814 }
5815
5816 for (size_t i = 0; i < chains.size(); i++) {
5817 sp<EffectChain> ec = chains[i];
5818 int sessionid = ec->sessionId();
5819 sp<ThreadBase> t = ec->mThread.promote();
5820 if (t == 0) {
5821 continue;
5822 }
5823 size_t numsessionrefs = mAudioSessionRefs.size();
5824 bool found = false;
5825 for (size_t k = 0; k < numsessionrefs; k++) {
5826 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten9a42ac92012-03-06 11:22:01 -08005827 if (ref->mSessionid == sessionid) {
Steve Block71f2cf12011-10-20 11:56:00 +01005828 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kasten18db49a2012-03-12 16:29:55 -07005829 sessionid, ref->mPid, ref->mCnt);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005830 found = true;
5831 break;
5832 }
5833 }
5834 if (!found) {
5835 // remove all effects from the chain
5836 while (ec->mEffects.size()) {
5837 sp<EffectModule> effect = ec->mEffects[0];
5838 effect->unPin();
5839 Mutex::Autolock _l (t->mLock);
5840 t->removeEffect_l(effect);
5841 for (size_t j = 0; j < effect->mHandles.size(); j++) {
5842 sp<EffectHandle> handle = effect->mHandles[j].promote();
5843 if (handle != 0) {
5844 handle->mEffect.clear();
Eric Laurent7fa1cee2011-10-19 11:44:54 -07005845 if (handle->mHasControl && handle->mEnabled) {
5846 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
5847 }
Marco Nelissenc74b93f2011-08-02 13:33:41 -07005848 }
5849 }
5850 AudioSystem::unregisterEffect(effect->id());
5851 }
5852 }
5853 }
5854 return;
5855}
5856
Eric Laurenta553c252009-07-17 12:17:14 -07005857// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005858AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Eric Laurenta553c252009-07-17 12:17:14 -07005859{
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08005860 return mPlaybackThreads.valueFor(output).get();
Eric Laurenta553c252009-07-17 12:17:14 -07005861}
5862
5863// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005864AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Eric Laurenta553c252009-07-17 12:17:14 -07005865{
5866 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08005867 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Eric Laurenta553c252009-07-17 12:17:14 -07005868}
5869
5870// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005871AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Eric Laurenta553c252009-07-17 12:17:14 -07005872{
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08005873 return mRecordThreads.valueFor(input).get();
Eric Laurenta553c252009-07-17 12:17:14 -07005874}
5875
Eric Laurent464d5b32011-06-17 21:29:58 -07005876uint32_t AudioFlinger::nextUniqueId()
Eric Laurent65b65452010-06-01 23:49:17 -07005877{
Eric Laurent464d5b32011-06-17 21:29:58 -07005878 return android_atomic_inc(&mNextUniqueId);
Eric Laurent65b65452010-06-01 23:49:17 -07005879}
5880
Glenn Kasten0fccd352012-02-24 15:42:17 -08005881AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent464d5b32011-06-17 21:29:58 -07005882{
5883 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
5884 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent828b9772011-08-07 16:32:26 -07005885 AudioStreamOut *output = thread->getOutput();
5886 if (output != NULL && output->hwDev == mPrimaryHardwareDev) {
Eric Laurent464d5b32011-06-17 21:29:58 -07005887 return thread;
5888 }
5889 }
5890 return NULL;
5891}
5892
Glenn Kasten0fccd352012-02-24 15:42:17 -08005893uint32_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent464d5b32011-06-17 21:29:58 -07005894{
5895 PlaybackThread *thread = primaryPlaybackThread_l();
5896
5897 if (thread == NULL) {
5898 return 0;
5899 }
5900
5901 return thread->device();
5902}
5903
5904
Eric Laurent65b65452010-06-01 23:49:17 -07005905// ----------------------------------------------------------------------------
5906// Effect management
5907// ----------------------------------------------------------------------------
5908
5909
Glenn Kasten3f6d83a2012-01-26 16:25:10 -08005910status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Eric Laurent65b65452010-06-01 23:49:17 -07005911{
5912 Mutex::Autolock _l(mLock);
5913 return EffectQueryNumberEffects(numEffects);
5914}
5915
Glenn Kasten3f6d83a2012-01-26 16:25:10 -08005916status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Eric Laurent65b65452010-06-01 23:49:17 -07005917{
5918 Mutex::Autolock _l(mLock);
Eric Laurent53334cd2010-06-23 17:38:20 -07005919 return EffectQueryEffect(index, descriptor);
Eric Laurent65b65452010-06-01 23:49:17 -07005920}
5921
Glenn Kasten67313332012-01-30 07:40:52 -08005922status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kasten3f6d83a2012-01-26 16:25:10 -08005923 effect_descriptor_t *descriptor) const
Eric Laurent65b65452010-06-01 23:49:17 -07005924{
5925 Mutex::Autolock _l(mLock);
5926 return EffectGetDescriptor(pUuid, descriptor);
5927}
5928
Eric Laurentdf9b81c2010-07-02 08:12:41 -07005929
Eric Laurent65b65452010-06-01 23:49:17 -07005930sp<IEffect> AudioFlinger::createEffect(pid_t pid,
5931 effect_descriptor_t *pDesc,
5932 const sp<IEffectClient>& effectClient,
5933 int32_t priority,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08005934 audio_io_handle_t io,
Eric Laurent65b65452010-06-01 23:49:17 -07005935 int sessionId,
5936 status_t *status,
5937 int *id,
5938 int *enabled)
5939{
5940 status_t lStatus = NO_ERROR;
5941 sp<EffectHandle> handle;
Eric Laurent65b65452010-06-01 23:49:17 -07005942 effect_descriptor_t desc;
Eric Laurent65b65452010-06-01 23:49:17 -07005943
Glenn Kasten803a86a2012-01-25 14:28:29 -08005944 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent464d5b32011-06-17 21:29:58 -07005945 pid, effectClient.get(), priority, sessionId, io);
Eric Laurent65b65452010-06-01 23:49:17 -07005946
5947 if (pDesc == NULL) {
5948 lStatus = BAD_VALUE;
5949 goto Exit;
5950 }
5951
Eric Laurent98c92592010-09-23 16:10:16 -07005952 // check audio settings permission for global effects
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005953 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent98c92592010-09-23 16:10:16 -07005954 lStatus = PERMISSION_DENIED;
5955 goto Exit;
5956 }
5957
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005958 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent98c92592010-09-23 16:10:16 -07005959 // that can only be created by audio policy manager (running in same process)
Glenn Kasten81211142012-02-05 18:09:08 -08005960 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent98c92592010-09-23 16:10:16 -07005961 lStatus = PERMISSION_DENIED;
5962 goto Exit;
5963 }
5964
Eric Laurent464d5b32011-06-17 21:29:58 -07005965 if (io == 0) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005966 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent98c92592010-09-23 16:10:16 -07005967 // output must be specified by AudioPolicyManager when using session
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005968 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent98c92592010-09-23 16:10:16 -07005969 lStatus = BAD_VALUE;
5970 goto Exit;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07005971 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent98c92592010-09-23 16:10:16 -07005972 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent464d5b32011-06-17 21:29:58 -07005973 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent98c92592010-09-23 16:10:16 -07005974 // and we will exit safely
Eric Laurent464d5b32011-06-17 21:29:58 -07005975 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent98c92592010-09-23 16:10:16 -07005976 }
5977 }
5978
Eric Laurent65b65452010-06-01 23:49:17 -07005979 {
5980 Mutex::Autolock _l(mLock);
5981
Eric Laurentdf9b81c2010-07-02 08:12:41 -07005982
Eric Laurent65b65452010-06-01 23:49:17 -07005983 if (!EffectIsNullUuid(&pDesc->uuid)) {
5984 // if uuid is specified, request effect descriptor
5985 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
5986 if (lStatus < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00005987 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07005988 goto Exit;
5989 }
5990 } else {
5991 // if uuid is not specified, look for an available implementation
5992 // of the required type in effect factory
5993 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block8564c8d2012-01-05 23:22:43 +00005994 ALOGW("createEffect() no effect type");
Eric Laurent65b65452010-06-01 23:49:17 -07005995 lStatus = BAD_VALUE;
5996 goto Exit;
5997 }
5998 uint32_t numEffects = 0;
5999 effect_descriptor_t d;
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006000 d.flags = 0; // prevent compiler warning
Eric Laurent65b65452010-06-01 23:49:17 -07006001 bool found = false;
6002
6003 lStatus = EffectQueryNumberEffects(&numEffects);
6004 if (lStatus < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006005 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006006 goto Exit;
6007 }
Eric Laurent53334cd2010-06-23 17:38:20 -07006008 for (uint32_t i = 0; i < numEffects; i++) {
6009 lStatus = EffectQueryEffect(i, &desc);
Eric Laurent65b65452010-06-01 23:49:17 -07006010 if (lStatus < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006011 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006012 continue;
6013 }
6014 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
6015 // If matching type found save effect descriptor. If the session is
6016 // 0 and the effect is not auxiliary, continue enumeration in case
6017 // an auxiliary version of this effect type is available
6018 found = true;
6019 memcpy(&d, &desc, sizeof(effect_descriptor_t));
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006020 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Eric Laurent65b65452010-06-01 23:49:17 -07006021 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6022 break;
6023 }
6024 }
6025 }
6026 if (!found) {
6027 lStatus = BAD_VALUE;
Steve Block8564c8d2012-01-05 23:22:43 +00006028 ALOGW("createEffect() effect not found");
Eric Laurent65b65452010-06-01 23:49:17 -07006029 goto Exit;
6030 }
6031 // For same effect type, chose auxiliary version over insert version if
6032 // connect to output mix (Compliance to OpenSL ES)
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006033 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Eric Laurent65b65452010-06-01 23:49:17 -07006034 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
6035 memcpy(&desc, &d, sizeof(effect_descriptor_t));
6036 }
6037 }
6038
6039 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006040 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Eric Laurent65b65452010-06-01 23:49:17 -07006041 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6042 lStatus = INVALID_OPERATION;
6043 goto Exit;
6044 }
6045
Eric Laurentf82fccd2011-07-27 19:49:51 -07006046 // check recording permission for visualizer
6047 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
6048 !recordingAllowed()) {
6049 lStatus = PERMISSION_DENIED;
6050 goto Exit;
6051 }
6052
Eric Laurent65b65452010-06-01 23:49:17 -07006053 // return effect descriptor
6054 memcpy(pDesc, &desc, sizeof(effect_descriptor_t));
6055
6056 // If output is not specified try to find a matching audio session ID in one of the
6057 // output threads.
Eric Laurent98c92592010-09-23 16:10:16 -07006058 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
6059 // because of code checking output when entering the function.
Eric Laurent464d5b32011-06-17 21:29:58 -07006060 // Note: io is never 0 when creating an effect on an input
6061 if (io == 0) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07006062 // look for the thread where the specified audio session is present
Eric Laurent98c92592010-09-23 16:10:16 -07006063 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
6064 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent464d5b32011-06-17 21:29:58 -07006065 io = mPlaybackThreads.keyAt(i);
Eric Laurent98c92592010-09-23 16:10:16 -07006066 break;
Eric Laurent493941b2010-07-28 01:32:47 -07006067 }
Eric Laurent65b65452010-06-01 23:49:17 -07006068 }
Eric Laurent464d5b32011-06-17 21:29:58 -07006069 if (io == 0) {
Glenn Kasten18db49a2012-03-12 16:29:55 -07006070 for (size_t i = 0; i < mRecordThreads.size(); i++) {
6071 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
6072 io = mRecordThreads.keyAt(i);
6073 break;
6074 }
6075 }
Eric Laurent464d5b32011-06-17 21:29:58 -07006076 }
Eric Laurent98c92592010-09-23 16:10:16 -07006077 // If no output thread contains the requested session ID, default to
6078 // first output. The effect chain will be moved to the correct output
6079 // thread when a track with the same session ID is created
Eric Laurent464d5b32011-06-17 21:29:58 -07006080 if (io == 0 && mPlaybackThreads.size()) {
6081 io = mPlaybackThreads.keyAt(0);
6082 }
Steve Block71f2cf12011-10-20 11:56:00 +01006083 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent464d5b32011-06-17 21:29:58 -07006084 }
6085 ThreadBase *thread = checkRecordThread_l(io);
6086 if (thread == NULL) {
6087 thread = checkPlaybackThread_l(io);
6088 if (thread == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00006089 ALOGE("createEffect() unknown output thread");
Eric Laurent464d5b32011-06-17 21:29:58 -07006090 lStatus = BAD_VALUE;
6091 goto Exit;
Eric Laurent98c92592010-09-23 16:10:16 -07006092 }
Eric Laurent65b65452010-06-01 23:49:17 -07006093 }
Eric Laurent98c92592010-09-23 16:10:16 -07006094
Glenn Kasten803a86a2012-01-25 14:28:29 -08006095 sp<Client> client = registerPid_l(pid);
Eric Laurent65b65452010-06-01 23:49:17 -07006096
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006097 // create effect on selected output thread
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006098 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
6099 &desc, enabled, &lStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006100 if (handle != 0 && id != NULL) {
6101 *id = handle->id();
6102 }
6103 }
6104
6105Exit:
Glenn Kasten18db49a2012-03-12 16:29:55 -07006106 if (status != NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07006107 *status = lStatus;
6108 }
6109 return handle;
6110}
6111
Glenn Kasten39d00cb2012-01-17 11:09:42 -08006112status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
6113 audio_io_handle_t dstOutput)
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006114{
Steve Block71f2cf12011-10-20 11:56:00 +01006115 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurentf82fccd2011-07-27 19:49:51 -07006116 sessionId, srcOutput, dstOutput);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006117 Mutex::Autolock _l(mLock);
6118 if (srcOutput == dstOutput) {
Steve Block8564c8d2012-01-05 23:22:43 +00006119 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006120 return NO_ERROR;
Eric Laurent53334cd2010-06-23 17:38:20 -07006121 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006122 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
6123 if (srcThread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00006124 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006125 return BAD_VALUE;
Eric Laurent53334cd2010-06-23 17:38:20 -07006126 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006127 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
6128 if (dstThread == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00006129 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006130 return BAD_VALUE;
6131 }
6132
6133 Mutex::Autolock _dl(dstThread->mLock);
6134 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurentf82fccd2011-07-27 19:49:51 -07006135 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006136
Eric Laurent53334cd2010-06-23 17:38:20 -07006137 return NO_ERROR;
6138}
6139
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006140// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurentf82fccd2011-07-27 19:49:51 -07006141status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006142 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent493941b2010-07-28 01:32:47 -07006143 AudioFlinger::PlaybackThread *dstThread,
6144 bool reRegister)
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006145{
Steve Block71f2cf12011-10-20 11:56:00 +01006146 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurentf82fccd2011-07-27 19:49:51 -07006147 sessionId, srcThread, dstThread);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006148
Eric Laurentf82fccd2011-07-27 19:49:51 -07006149 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006150 if (chain == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006151 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurentf82fccd2011-07-27 19:49:51 -07006152 sessionId, srcThread);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006153 return INVALID_OPERATION;
6154 }
6155
Eric Laurent493941b2010-07-28 01:32:47 -07006156 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006157 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurent6fccbd02011-10-05 17:42:25 -07006158 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006159 // removed.
6160 srcThread->removeEffectChain_l(chain);
6161
6162 // transfer all effects one by one so that new effect chain is created on new thread with
6163 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten39d00cb2012-01-17 11:09:42 -08006164 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent493941b2010-07-28 01:32:47 -07006165 sp<EffectChain> dstChain;
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006166 uint32_t strategy = 0; // prevent compiler warning
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006167 sp<EffectModule> effect = chain->getEffectFromId_l(0);
6168 while (effect != 0) {
6169 srcThread->removeEffect_l(effect);
6170 dstThread->addEffect_l(effect);
Eric Laurent6fccbd02011-10-05 17:42:25 -07006171 // removeEffect_l() has stopped the effect if it was active so it must be restarted
6172 if (effect->state() == EffectModule::ACTIVE ||
6173 effect->state() == EffectModule::STOPPING) {
6174 effect->start();
6175 }
Eric Laurent493941b2010-07-28 01:32:47 -07006176 // if the move request is not received from audio policy manager, the effect must be
6177 // re-registered with the new strategy and output
6178 if (dstChain == 0) {
6179 dstChain = effect->chain().promote();
6180 if (dstChain == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006181 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent493941b2010-07-28 01:32:47 -07006182 srcThread->addEffect_l(effect);
6183 return NO_INIT;
6184 }
6185 strategy = dstChain->strategy();
6186 }
6187 if (reRegister) {
6188 AudioSystem::unregisterEffect(effect->id());
6189 AudioSystem::registerEffect(&effect->desc(),
6190 dstOutput,
6191 strategy,
Eric Laurentf82fccd2011-07-27 19:49:51 -07006192 sessionId,
Eric Laurent493941b2010-07-28 01:32:47 -07006193 effect->id());
6194 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006195 effect = chain->getEffectFromId_l(0);
6196 }
6197
6198 return NO_ERROR;
Eric Laurent53334cd2010-06-23 17:38:20 -07006199}
6200
Eric Laurent464d5b32011-06-17 21:29:58 -07006201
Eric Laurent65b65452010-06-01 23:49:17 -07006202// PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held
Eric Laurent464d5b32011-06-17 21:29:58 -07006203sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l(
Eric Laurent65b65452010-06-01 23:49:17 -07006204 const sp<AudioFlinger::Client>& client,
6205 const sp<IEffectClient>& effectClient,
6206 int32_t priority,
6207 int sessionId,
6208 effect_descriptor_t *desc,
6209 int *enabled,
6210 status_t *status
6211 )
6212{
6213 sp<EffectModule> effect;
6214 sp<EffectHandle> handle;
6215 status_t lStatus;
Eric Laurent65b65452010-06-01 23:49:17 -07006216 sp<EffectChain> chain;
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006217 bool chainCreated = false;
Eric Laurent65b65452010-06-01 23:49:17 -07006218 bool effectCreated = false;
Eric Laurent53334cd2010-06-23 17:38:20 -07006219 bool effectRegistered = false;
Eric Laurent65b65452010-06-01 23:49:17 -07006220
Eric Laurent464d5b32011-06-17 21:29:58 -07006221 lStatus = initCheck();
6222 if (lStatus != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +00006223 ALOGW("createEffect_l() Audio driver not initialized.");
Eric Laurent65b65452010-06-01 23:49:17 -07006224 goto Exit;
6225 }
6226
6227 // Do not allow effects with session ID 0 on direct output or duplicating threads
6228 // TODO: add rule for hw accelerated effects on direct outputs with non PCM format
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006229 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) {
Steve Block8564c8d2012-01-05 23:22:43 +00006230 ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d",
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006231 desc->name, sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -07006232 lStatus = BAD_VALUE;
6233 goto Exit;
6234 }
Eric Laurent464d5b32011-06-17 21:29:58 -07006235 // Only Pre processor effects are allowed on input threads and only on input threads
Glenn Kasten7d3be3a2012-01-26 10:53:32 -08006236 if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) {
Steve Block8564c8d2012-01-05 23:22:43 +00006237 ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d",
Eric Laurent464d5b32011-06-17 21:29:58 -07006238 desc->name, desc->flags, mType);
6239 lStatus = BAD_VALUE;
6240 goto Exit;
6241 }
Eric Laurent65b65452010-06-01 23:49:17 -07006242
Steve Block71f2cf12011-10-20 11:56:00 +01006243 ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -07006244
6245 { // scope for mLock
6246 Mutex::Autolock _l(mLock);
6247
6248 // check for existing effect chain with the requested audio session
6249 chain = getEffectChain_l(sessionId);
6250 if (chain == 0) {
6251 // create a new chain for this session
Steve Block71f2cf12011-10-20 11:56:00 +01006252 ALOGV("createEffect_l() new effect chain for session %d", sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -07006253 chain = new EffectChain(this, sessionId);
6254 addEffectChain_l(chain);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006255 chain->setStrategy(getStrategyForSession_l(sessionId));
6256 chainCreated = true;
Eric Laurent65b65452010-06-01 23:49:17 -07006257 } else {
Eric Laurent76c40f72010-07-15 12:50:15 -07006258 effect = chain->getEffectFromDesc_l(desc);
Eric Laurent65b65452010-06-01 23:49:17 -07006259 }
6260
Glenn Kasten70ed6b72012-01-10 10:46:34 -08006261 ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get());
Eric Laurent65b65452010-06-01 23:49:17 -07006262
6263 if (effect == 0) {
Eric Laurent464d5b32011-06-17 21:29:58 -07006264 int id = mAudioFlinger->nextUniqueId();
Eric Laurent53334cd2010-06-23 17:38:20 -07006265 // Check CPU and memory usage
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006266 lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id);
Eric Laurent53334cd2010-06-23 17:38:20 -07006267 if (lStatus != NO_ERROR) {
6268 goto Exit;
6269 }
6270 effectRegistered = true;
Eric Laurent65b65452010-06-01 23:49:17 -07006271 // create a new effect module if none present in the chain
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006272 effect = new EffectModule(this, chain, desc, id, sessionId);
Eric Laurent65b65452010-06-01 23:49:17 -07006273 lStatus = effect->status();
6274 if (lStatus != NO_ERROR) {
6275 goto Exit;
6276 }
Eric Laurent76c40f72010-07-15 12:50:15 -07006277 lStatus = chain->addEffect_l(effect);
Eric Laurent65b65452010-06-01 23:49:17 -07006278 if (lStatus != NO_ERROR) {
6279 goto Exit;
6280 }
Eric Laurent53334cd2010-06-23 17:38:20 -07006281 effectCreated = true;
Eric Laurent65b65452010-06-01 23:49:17 -07006282
6283 effect->setDevice(mDevice);
Eric Laurent53334cd2010-06-23 17:38:20 -07006284 effect->setMode(mAudioFlinger->getMode());
Eric Laurent65b65452010-06-01 23:49:17 -07006285 }
6286 // create effect handle and connect it to effect module
6287 handle = new EffectHandle(effect, client, effectClient, priority);
6288 lStatus = effect->addHandle(handle);
Glenn Kasten3694ec12012-01-27 16:47:15 -08006289 if (enabled != NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07006290 *enabled = (int)effect->isEnabled();
6291 }
6292 }
6293
6294Exit:
6295 if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006296 Mutex::Autolock _l(mLock);
Eric Laurent53334cd2010-06-23 17:38:20 -07006297 if (effectCreated) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006298 chain->removeEffect_l(effect);
Eric Laurent65b65452010-06-01 23:49:17 -07006299 }
Eric Laurent53334cd2010-06-23 17:38:20 -07006300 if (effectRegistered) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006301 AudioSystem::unregisterEffect(effect->id());
6302 }
6303 if (chainCreated) {
6304 removeEffectChain_l(chain);
Eric Laurent53334cd2010-06-23 17:38:20 -07006305 }
Eric Laurent65b65452010-06-01 23:49:17 -07006306 handle.clear();
6307 }
6308
Glenn Kasten18db49a2012-03-12 16:29:55 -07006309 if (status != NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07006310 *status = lStatus;
6311 }
6312 return handle;
6313}
6314
Eric Laurent464d5b32011-06-17 21:29:58 -07006315sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId)
6316{
Eric Laurent464d5b32011-06-17 21:29:58 -07006317 sp<EffectChain> chain = getEffectChain_l(sessionId);
Glenn Kastenc64a6f02012-01-30 13:00:02 -08006318 return chain != 0 ? chain->getEffectFromId_l(effectId) : 0;
Eric Laurent464d5b32011-06-17 21:29:58 -07006319}
6320
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006321// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and
6322// PlaybackThread::mLock held
Eric Laurent464d5b32011-06-17 21:29:58 -07006323status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect)
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006324{
6325 // check for existing effect chain with the requested audio session
6326 int sessionId = effect->sessionId();
6327 sp<EffectChain> chain = getEffectChain_l(sessionId);
6328 bool chainCreated = false;
6329
6330 if (chain == 0) {
6331 // create a new chain for this session
Steve Block71f2cf12011-10-20 11:56:00 +01006332 ALOGV("addEffect_l() new effect chain for session %d", sessionId);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006333 chain = new EffectChain(this, sessionId);
6334 addEffectChain_l(chain);
6335 chain->setStrategy(getStrategyForSession_l(sessionId));
6336 chainCreated = true;
6337 }
Steve Block71f2cf12011-10-20 11:56:00 +01006338 ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get());
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006339
6340 if (chain->getEffectFromId_l(effect->id()) != 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00006341 ALOGW("addEffect_l() %p effect %s already present in chain %p",
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006342 this, effect->desc().name, chain.get());
6343 return BAD_VALUE;
6344 }
6345
6346 status_t status = chain->addEffect_l(effect);
6347 if (status != NO_ERROR) {
6348 if (chainCreated) {
6349 removeEffectChain_l(chain);
6350 }
6351 return status;
6352 }
6353
6354 effect->setDevice(mDevice);
6355 effect->setMode(mAudioFlinger->getMode());
6356 return NO_ERROR;
6357}
6358
Eric Laurent464d5b32011-06-17 21:29:58 -07006359void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006360
Steve Block71f2cf12011-10-20 11:56:00 +01006361 ALOGV("removeEffect_l() %p effect %p", this, effect.get());
Eric Laurent53334cd2010-06-23 17:38:20 -07006362 effect_descriptor_t desc = effect->desc();
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006363 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6364 detachAuxEffect_l(effect->id());
6365 }
6366
6367 sp<EffectChain> chain = effect->chain().promote();
6368 if (chain != 0) {
6369 // remove effect chain if removing last effect
6370 if (chain->removeEffect_l(effect) == 0) {
6371 removeEffectChain_l(chain);
6372 }
6373 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00006374 ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get());
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006375 }
6376}
6377
Eric Laurent464d5b32011-06-17 21:29:58 -07006378void AudioFlinger::ThreadBase::lockEffectChains_l(
Glenn Kasten18db49a2012-03-12 16:29:55 -07006379 Vector< sp<AudioFlinger::EffectChain> >& effectChains)
Eric Laurent464d5b32011-06-17 21:29:58 -07006380{
6381 effectChains = mEffectChains;
6382 for (size_t i = 0; i < mEffectChains.size(); i++) {
6383 mEffectChains[i]->lock();
6384 }
6385}
6386
6387void AudioFlinger::ThreadBase::unlockEffectChains(
Glenn Kasten18db49a2012-03-12 16:29:55 -07006388 const Vector< sp<AudioFlinger::EffectChain> >& effectChains)
Eric Laurent464d5b32011-06-17 21:29:58 -07006389{
6390 for (size_t i = 0; i < effectChains.size(); i++) {
6391 effectChains[i]->unlock();
6392 }
6393}
6394
6395sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId)
6396{
6397 Mutex::Autolock _l(mLock);
6398 return getEffectChain_l(sessionId);
6399}
6400
6401sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId)
6402{
Eric Laurent464d5b32011-06-17 21:29:58 -07006403 size_t size = mEffectChains.size();
6404 for (size_t i = 0; i < size; i++) {
6405 if (mEffectChains[i]->sessionId() == sessionId) {
Glenn Kastenc64a6f02012-01-30 13:00:02 -08006406 return mEffectChains[i];
Eric Laurent464d5b32011-06-17 21:29:58 -07006407 }
6408 }
Glenn Kastenc64a6f02012-01-30 13:00:02 -08006409 return 0;
Eric Laurent464d5b32011-06-17 21:29:58 -07006410}
6411
Glenn Kastenaccb1142012-01-04 11:00:47 -08006412void AudioFlinger::ThreadBase::setMode(audio_mode_t mode)
Eric Laurent464d5b32011-06-17 21:29:58 -07006413{
6414 Mutex::Autolock _l(mLock);
6415 size_t size = mEffectChains.size();
6416 for (size_t i = 0; i < size; i++) {
6417 mEffectChains[i]->setMode_l(mode);
6418 }
6419}
6420
6421void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect,
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006422 const wp<EffectHandle>& handle,
Glenn Kasten29441ff2012-02-03 10:32:24 -08006423 bool unpinIfLast) {
Eric Laurentf82fccd2011-07-27 19:49:51 -07006424
Eric Laurent53334cd2010-06-23 17:38:20 -07006425 Mutex::Autolock _l(mLock);
Steve Block71f2cf12011-10-20 11:56:00 +01006426 ALOGV("disconnectEffect() %p effect %p", this, effect.get());
Eric Laurent53334cd2010-06-23 17:38:20 -07006427 // delete the effect module if removing last handle on it
6428 if (effect->removeHandle(handle) == 0) {
Glenn Kasten29441ff2012-02-03 10:32:24 -08006429 if (!effect->isPinned() || unpinIfLast) {
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006430 removeEffect_l(effect);
6431 AudioSystem::unregisterEffect(effect->id());
6432 }
Eric Laurent53334cd2010-06-23 17:38:20 -07006433 }
6434}
6435
Eric Laurent65b65452010-06-01 23:49:17 -07006436status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain)
6437{
6438 int session = chain->sessionId();
6439 int16_t *buffer = mMixBuffer;
Eric Laurent53334cd2010-06-23 17:38:20 -07006440 bool ownsBuffer = false;
Eric Laurent65b65452010-06-01 23:49:17 -07006441
Steve Block71f2cf12011-10-20 11:56:00 +01006442 ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
Eric Laurent53334cd2010-06-23 17:38:20 -07006443 if (session > 0) {
Eric Laurent65b65452010-06-01 23:49:17 -07006444 // Only one effect chain can be present in direct output thread and it uses
6445 // the mix buffer as input
6446 if (mType != DIRECT) {
6447 size_t numSamples = mFrameCount * mChannelCount;
6448 buffer = new int16_t[numSamples];
6449 memset(buffer, 0, numSamples * sizeof(int16_t));
Steve Block71f2cf12011-10-20 11:56:00 +01006450 ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session);
Eric Laurent65b65452010-06-01 23:49:17 -07006451 ownsBuffer = true;
6452 }
Eric Laurent65b65452010-06-01 23:49:17 -07006453
Eric Laurent53334cd2010-06-23 17:38:20 -07006454 // Attach all tracks with same session ID to this chain.
6455 for (size_t i = 0; i < mTracks.size(); ++i) {
6456 sp<Track> track = mTracks[i];
6457 if (session == track->sessionId()) {
Steve Block71f2cf12011-10-20 11:56:00 +01006458 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer);
Eric Laurent53334cd2010-06-23 17:38:20 -07006459 track->setMainBuffer(buffer);
Eric Laurent90681d62011-05-09 12:09:06 -07006460 chain->incTrackCnt();
Eric Laurent53334cd2010-06-23 17:38:20 -07006461 }
6462 }
6463
6464 // indicate all active tracks in the chain
6465 for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
6466 sp<Track> track = mActiveTracks[i].promote();
6467 if (track == 0) continue;
6468 if (session == track->sessionId()) {
Steve Block71f2cf12011-10-20 11:56:00 +01006469 ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session);
Eric Laurent90681d62011-05-09 12:09:06 -07006470 chain->incActiveTrackCnt();
Eric Laurent53334cd2010-06-23 17:38:20 -07006471 }
Eric Laurent65b65452010-06-01 23:49:17 -07006472 }
6473 }
6474
Eric Laurent53334cd2010-06-23 17:38:20 -07006475 chain->setInBuffer(buffer, ownsBuffer);
6476 chain->setOutBuffer(mMixBuffer);
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006477 // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006478 // chains list in order to be processed last as it contains output stage effects
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006479 // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before
6480 // session AUDIO_SESSION_OUTPUT_STAGE to be processed
Eric Laurent53334cd2010-06-23 17:38:20 -07006481 // after track specific effects and before output stage
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006482 // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and
6483 // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006484 // Effect chain for other sessions are inserted at beginning of effect
6485 // chains list to be processed before output mix effects. Relative order between other
6486 // sessions is not important
Eric Laurent53334cd2010-06-23 17:38:20 -07006487 size_t size = mEffectChains.size();
6488 size_t i = 0;
6489 for (i = 0; i < size; i++) {
6490 if (mEffectChains[i]->sessionId() < session) break;
Eric Laurent65b65452010-06-01 23:49:17 -07006491 }
Eric Laurent53334cd2010-06-23 17:38:20 -07006492 mEffectChains.insertAt(chain, i);
Eric Laurentf82fccd2011-07-27 19:49:51 -07006493 checkSuspendOnAddEffectChain_l(chain);
Eric Laurent65b65452010-06-01 23:49:17 -07006494
6495 return NO_ERROR;
6496}
6497
6498size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain)
6499{
6500 int session = chain->sessionId();
6501
Steve Block71f2cf12011-10-20 11:56:00 +01006502 ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
Eric Laurent65b65452010-06-01 23:49:17 -07006503
6504 for (size_t i = 0; i < mEffectChains.size(); i++) {
6505 if (chain == mEffectChains[i]) {
6506 mEffectChains.removeAt(i);
Eric Laurent90681d62011-05-09 12:09:06 -07006507 // detach all active tracks from the chain
6508 for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
6509 sp<Track> track = mActiveTracks[i].promote();
6510 if (track == 0) continue;
6511 if (session == track->sessionId()) {
Steve Block71f2cf12011-10-20 11:56:00 +01006512 ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d",
Eric Laurent90681d62011-05-09 12:09:06 -07006513 chain.get(), session);
6514 chain->decActiveTrackCnt();
6515 }
6516 }
6517
Eric Laurent65b65452010-06-01 23:49:17 -07006518 // detach all tracks with same session ID from this chain
6519 for (size_t i = 0; i < mTracks.size(); ++i) {
6520 sp<Track> track = mTracks[i];
6521 if (session == track->sessionId()) {
6522 track->setMainBuffer(mMixBuffer);
Eric Laurent90681d62011-05-09 12:09:06 -07006523 chain->decTrackCnt();
Eric Laurent65b65452010-06-01 23:49:17 -07006524 }
6525 }
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006526 break;
Eric Laurent65b65452010-06-01 23:49:17 -07006527 }
6528 }
6529 return mEffectChains.size();
6530}
6531
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006532status_t AudioFlinger::PlaybackThread::attachAuxEffect(
6533 const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
Eric Laurent65b65452010-06-01 23:49:17 -07006534{
6535 Mutex::Autolock _l(mLock);
6536 return attachAuxEffect_l(track, EffectId);
6537}
6538
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006539status_t AudioFlinger::PlaybackThread::attachAuxEffect_l(
6540 const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
Eric Laurent65b65452010-06-01 23:49:17 -07006541{
6542 status_t status = NO_ERROR;
6543
6544 if (EffectId == 0) {
6545 track->setAuxBuffer(0, NULL);
6546 } else {
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006547 // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX
6548 sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
Eric Laurent65b65452010-06-01 23:49:17 -07006549 if (effect != 0) {
6550 if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6551 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer());
6552 } else {
6553 status = INVALID_OPERATION;
6554 }
6555 } else {
6556 status = BAD_VALUE;
6557 }
6558 }
6559 return status;
6560}
6561
6562void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId)
6563{
Glenn Kasten18db49a2012-03-12 16:29:55 -07006564 for (size_t i = 0; i < mTracks.size(); ++i) {
Eric Laurent65b65452010-06-01 23:49:17 -07006565 sp<Track> track = mTracks[i];
6566 if (track->auxEffectId() == effectId) {
6567 attachAuxEffect_l(track, 0);
6568 }
6569 }
6570}
6571
Eric Laurent464d5b32011-06-17 21:29:58 -07006572status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain)
6573{
6574 // only one chain per input thread
6575 if (mEffectChains.size() != 0) {
6576 return INVALID_OPERATION;
6577 }
Steve Block71f2cf12011-10-20 11:56:00 +01006578 ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this);
Eric Laurent464d5b32011-06-17 21:29:58 -07006579
6580 chain->setInBuffer(NULL);
6581 chain->setOutBuffer(NULL);
6582
Eric Laurentf82fccd2011-07-27 19:49:51 -07006583 checkSuspendOnAddEffectChain_l(chain);
6584
Eric Laurent464d5b32011-06-17 21:29:58 -07006585 mEffectChains.add(chain);
6586
6587 return NO_ERROR;
6588}
6589
6590size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain)
6591{
Steve Block71f2cf12011-10-20 11:56:00 +01006592 ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this);
Steve Block8564c8d2012-01-05 23:22:43 +00006593 ALOGW_IF(mEffectChains.size() != 1,
Eric Laurent464d5b32011-06-17 21:29:58 -07006594 "removeEffectChain_l() %p invalid chain size %d on thread %p",
6595 chain.get(), mEffectChains.size(), this);
6596 if (mEffectChains.size() == 1) {
6597 mEffectChains.removeAt(0);
6598 }
6599 return 0;
6600}
6601
Eric Laurent65b65452010-06-01 23:49:17 -07006602// ----------------------------------------------------------------------------
6603// EffectModule implementation
6604// ----------------------------------------------------------------------------
6605
6606#undef LOG_TAG
6607#define LOG_TAG "AudioFlinger::EffectModule"
6608
Glenn Kasten685c9ce2012-01-20 13:32:16 -08006609AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
Eric Laurent65b65452010-06-01 23:49:17 -07006610 const wp<AudioFlinger::EffectChain>& chain,
6611 effect_descriptor_t *desc,
6612 int id,
6613 int sessionId)
Glenn Kasten685c9ce2012-01-20 13:32:16 -08006614 : mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL),
Eric Laurentf82fccd2011-07-27 19:49:51 -07006615 mStatus(NO_INIT), mState(IDLE), mSuspended(false)
Eric Laurent65b65452010-06-01 23:49:17 -07006616{
Steve Block71f2cf12011-10-20 11:56:00 +01006617 ALOGV("Constructor %p", this);
Eric Laurent65b65452010-06-01 23:49:17 -07006618 int lStatus;
Glenn Kasten685c9ce2012-01-20 13:32:16 -08006619 if (thread == NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07006620 return;
6621 }
Eric Laurent65b65452010-06-01 23:49:17 -07006622
6623 memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t));
6624
6625 // create effect engine from effect factory
Eric Laurent464d5b32011-06-17 21:29:58 -07006626 mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface);
Eric Laurent53334cd2010-06-23 17:38:20 -07006627
Eric Laurent65b65452010-06-01 23:49:17 -07006628 if (mStatus != NO_ERROR) {
6629 return;
6630 }
6631 lStatus = init();
6632 if (lStatus < 0) {
6633 mStatus = lStatus;
6634 goto Error;
6635 }
6636
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006637 if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) {
6638 mPinned = true;
6639 }
Steve Block71f2cf12011-10-20 11:56:00 +01006640 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface);
Eric Laurent65b65452010-06-01 23:49:17 -07006641 return;
6642Error:
6643 EffectRelease(mEffectInterface);
6644 mEffectInterface = NULL;
Steve Block71f2cf12011-10-20 11:56:00 +01006645 ALOGV("Constructor Error %d", mStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006646}
6647
6648AudioFlinger::EffectModule::~EffectModule()
6649{
Steve Block71f2cf12011-10-20 11:56:00 +01006650 ALOGV("Destructor %p", this);
Eric Laurent65b65452010-06-01 23:49:17 -07006651 if (mEffectInterface != NULL) {
Eric Laurent464d5b32011-06-17 21:29:58 -07006652 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
6653 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
6654 sp<ThreadBase> thread = mThread.promote();
6655 if (thread != 0) {
Eric Laurent828b9772011-08-07 16:32:26 -07006656 audio_stream_t *stream = thread->stream();
6657 if (stream != NULL) {
6658 stream->remove_audio_effect(stream, mEffectInterface);
6659 }
Eric Laurent464d5b32011-06-17 21:29:58 -07006660 }
6661 }
Eric Laurent65b65452010-06-01 23:49:17 -07006662 // release effect engine
6663 EffectRelease(mEffectInterface);
6664 }
6665}
6666
Glenn Kasten1f812f72012-01-30 10:15:48 -08006667status_t AudioFlinger::EffectModule::addHandle(const sp<EffectHandle>& handle)
Eric Laurent65b65452010-06-01 23:49:17 -07006668{
6669 status_t status;
6670
6671 Mutex::Autolock _l(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -07006672 int priority = handle->priority();
6673 size_t size = mHandles.size();
6674 sp<EffectHandle> h;
6675 size_t i;
6676 for (i = 0; i < size; i++) {
6677 h = mHandles[i].promote();
6678 if (h == 0) continue;
6679 if (h->priority() <= priority) break;
6680 }
6681 // if inserted in first place, move effect control from previous owner to this handle
6682 if (i == 0) {
Eric Laurentf82fccd2011-07-27 19:49:51 -07006683 bool enabled = false;
Eric Laurent65b65452010-06-01 23:49:17 -07006684 if (h != 0) {
Eric Laurentf82fccd2011-07-27 19:49:51 -07006685 enabled = h->enabled();
6686 h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
Eric Laurent65b65452010-06-01 23:49:17 -07006687 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07006688 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
Eric Laurent65b65452010-06-01 23:49:17 -07006689 status = NO_ERROR;
6690 } else {
6691 status = ALREADY_EXISTS;
6692 }
Steve Block71f2cf12011-10-20 11:56:00 +01006693 ALOGV("addHandle() %p added handle %p in position %d", this, handle.get(), i);
Eric Laurent65b65452010-06-01 23:49:17 -07006694 mHandles.insertAt(handle, i);
6695 return status;
6696}
6697
6698size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle)
6699{
6700 Mutex::Autolock _l(mLock);
6701 size_t size = mHandles.size();
6702 size_t i;
6703 for (i = 0; i < size; i++) {
6704 if (mHandles[i] == handle) break;
6705 }
6706 if (i == size) {
6707 return size;
6708 }
Steve Block71f2cf12011-10-20 11:56:00 +01006709 ALOGV("removeHandle() %p removed handle %p in position %d", this, handle.unsafe_get(), i);
Eric Laurentf82fccd2011-07-27 19:49:51 -07006710
6711 bool enabled = false;
6712 EffectHandle *hdl = handle.unsafe_get();
Glenn Kasten3694ec12012-01-27 16:47:15 -08006713 if (hdl != NULL) {
Steve Block71f2cf12011-10-20 11:56:00 +01006714 ALOGV("removeHandle() unsafe_get OK");
Eric Laurentf82fccd2011-07-27 19:49:51 -07006715 enabled = hdl->enabled();
6716 }
Eric Laurent65b65452010-06-01 23:49:17 -07006717 mHandles.removeAt(i);
6718 size = mHandles.size();
6719 // if removed from first place, move effect control from this handle to next in line
6720 if (i == 0 && size != 0) {
6721 sp<EffectHandle> h = mHandles[0].promote();
6722 if (h != 0) {
Eric Laurentf82fccd2011-07-27 19:49:51 -07006723 h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/);
Eric Laurent65b65452010-06-01 23:49:17 -07006724 }
6725 }
6726
Eric Laurent21b5c472011-07-26 20:54:46 -07006727 // Prevent calls to process() and other functions on effect interface from now on.
6728 // The effect engine will be released by the destructor when the last strong reference on
6729 // this object is released which can happen after next process is called.
Marco Nelissenc74b93f2011-08-02 13:33:41 -07006730 if (size == 0 && !mPinned) {
Eric Laurent21b5c472011-07-26 20:54:46 -07006731 mState = DESTROYED;
Eric Laurent4fd3ecc2010-09-28 14:09:57 -07006732 }
6733
Eric Laurent65b65452010-06-01 23:49:17 -07006734 return size;
6735}
6736
Eric Laurentf82fccd2011-07-27 19:49:51 -07006737sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle()
6738{
6739 Mutex::Autolock _l(mLock);
Glenn Kastenc64a6f02012-01-30 13:00:02 -08006740 return mHandles.size() != 0 ? mHandles[0].promote() : 0;
Eric Laurentf82fccd2011-07-27 19:49:51 -07006741}
6742
Glenn Kasten29441ff2012-02-03 10:32:24 -08006743void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpinIfLast)
Eric Laurent65b65452010-06-01 23:49:17 -07006744{
Glenn Kasten0765c442012-01-27 15:24:38 -08006745 ALOGV("disconnect() %p handle %p", this, handle.unsafe_get());
Eric Laurent65b65452010-06-01 23:49:17 -07006746 // keep a strong reference on this EffectModule to avoid calling the
6747 // destructor before we exit
6748 sp<EffectModule> keep(this);
Eric Laurent53334cd2010-06-23 17:38:20 -07006749 {
6750 sp<ThreadBase> thread = mThread.promote();
6751 if (thread != 0) {
Glenn Kasten29441ff2012-02-03 10:32:24 -08006752 thread->disconnectEffect(keep, handle, unpinIfLast);
Eric Laurent65b65452010-06-01 23:49:17 -07006753 }
6754 }
6755}
6756
Eric Laurent7d850f22010-07-09 13:34:17 -07006757void AudioFlinger::EffectModule::updateState() {
6758 Mutex::Autolock _l(mLock);
6759
6760 switch (mState) {
6761 case RESTART:
6762 reset_l();
6763 // FALL THROUGH
6764
6765 case STARTING:
6766 // clear auxiliary effect input buffer for next accumulation
6767 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6768 memset(mConfig.inputCfg.buffer.raw,
6769 0,
6770 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
6771 }
6772 start_l();
6773 mState = ACTIVE;
6774 break;
6775 case STOPPING:
6776 stop_l();
6777 mDisableWaitCnt = mMaxDisableWaitCnt;
6778 mState = STOPPED;
6779 break;
6780 case STOPPED:
6781 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
6782 // turn off sequence.
6783 if (--mDisableWaitCnt == 0) {
6784 reset_l();
6785 mState = IDLE;
6786 }
6787 break;
Eric Laurent21b5c472011-07-26 20:54:46 -07006788 default: //IDLE , ACTIVE, DESTROYED
Eric Laurent7d850f22010-07-09 13:34:17 -07006789 break;
6790 }
6791}
6792
Eric Laurent65b65452010-06-01 23:49:17 -07006793void AudioFlinger::EffectModule::process()
6794{
6795 Mutex::Autolock _l(mLock);
6796
Eric Laurent21b5c472011-07-26 20:54:46 -07006797 if (mState == DESTROYED || mEffectInterface == NULL ||
Eric Laurent7d850f22010-07-09 13:34:17 -07006798 mConfig.inputCfg.buffer.raw == NULL ||
6799 mConfig.outputCfg.buffer.raw == NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07006800 return;
6801 }
6802
Eric Laurenta92ebfa2010-08-31 13:50:07 -07006803 if (isProcessEnabled()) {
Eric Laurent65b65452010-06-01 23:49:17 -07006804 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
6805 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kasten490909d2011-12-15 09:52:39 -08006806 ditherAndClamp(mConfig.inputCfg.buffer.s32,
Eric Laurent65b65452010-06-01 23:49:17 -07006807 mConfig.inputCfg.buffer.s32,
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006808 mConfig.inputCfg.buffer.frameCount/2);
Eric Laurent65b65452010-06-01 23:49:17 -07006809 }
6810
Eric Laurent65b65452010-06-01 23:49:17 -07006811 // do the actual processing in the effect engine
Eric Laurent7d850f22010-07-09 13:34:17 -07006812 int ret = (*mEffectInterface)->process(mEffectInterface,
6813 &mConfig.inputCfg.buffer,
6814 &mConfig.outputCfg.buffer);
6815
6816 // force transition to IDLE state when engine is ready
6817 if (mState == STOPPED && ret == -ENODATA) {
6818 mDisableWaitCnt = 1;
6819 }
Eric Laurent65b65452010-06-01 23:49:17 -07006820
6821 // clear auxiliary effect input buffer for next accumulation
6822 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurent67b5ed32011-01-19 18:36:13 -08006823 memset(mConfig.inputCfg.buffer.raw, 0,
6824 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
Eric Laurent65b65452010-06-01 23:49:17 -07006825 }
6826 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
Eric Laurent67b5ed32011-01-19 18:36:13 -08006827 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
6828 // If an insert effect is idle and input buffer is different from output buffer,
6829 // accumulate input onto output
Eric Laurent65b65452010-06-01 23:49:17 -07006830 sp<EffectChain> chain = mChain.promote();
Eric Laurent90681d62011-05-09 12:09:06 -07006831 if (chain != 0 && chain->activeTrackCnt() != 0) {
Eric Laurent67b5ed32011-01-19 18:36:13 -08006832 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here
6833 int16_t *in = mConfig.inputCfg.buffer.s16;
6834 int16_t *out = mConfig.outputCfg.buffer.s16;
6835 for (size_t i = 0; i < frameCnt; i++) {
6836 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
Eric Laurent65b65452010-06-01 23:49:17 -07006837 }
Eric Laurent65b65452010-06-01 23:49:17 -07006838 }
6839 }
6840}
6841
Eric Laurentdf9b81c2010-07-02 08:12:41 -07006842void AudioFlinger::EffectModule::reset_l()
Eric Laurent65b65452010-06-01 23:49:17 -07006843{
6844 if (mEffectInterface == NULL) {
6845 return;
6846 }
6847 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
6848}
6849
6850status_t AudioFlinger::EffectModule::configure()
6851{
6852 uint32_t channels;
6853 if (mEffectInterface == NULL) {
6854 return NO_INIT;
6855 }
6856
6857 sp<ThreadBase> thread = mThread.promote();
6858 if (thread == 0) {
6859 return DEAD_OBJECT;
6860 }
6861
6862 // TODO: handle configuration of effects replacing track process
6863 if (thread->channelCount() == 1) {
Eric Laurent0fb66c22011-05-17 19:16:02 -07006864 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurent65b65452010-06-01 23:49:17 -07006865 } else {
Eric Laurent0fb66c22011-05-17 19:16:02 -07006866 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurent65b65452010-06-01 23:49:17 -07006867 }
6868
6869 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurent0fb66c22011-05-17 19:16:02 -07006870 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurent65b65452010-06-01 23:49:17 -07006871 } else {
6872 mConfig.inputCfg.channels = channels;
6873 }
6874 mConfig.outputCfg.channels = channels;
Eric Laurent0fb66c22011-05-17 19:16:02 -07006875 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
6876 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurent65b65452010-06-01 23:49:17 -07006877 mConfig.inputCfg.samplingRate = thread->sampleRate();
6878 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
6879 mConfig.inputCfg.bufferProvider.cookie = NULL;
6880 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
6881 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
6882 mConfig.outputCfg.bufferProvider.cookie = NULL;
6883 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
6884 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
6885 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
6886 // Insert effect:
Dima Zavin24fc2fb2011-04-19 22:30:36 -07006887 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006888 // always overwrites output buffer: input buffer == output buffer
Eric Laurent65b65452010-06-01 23:49:17 -07006889 // - in other sessions:
6890 // last effect in the chain accumulates in output buffer: input buffer != output buffer
6891 // other effect: overwrites output buffer: input buffer == output buffer
6892 // Auxiliary effect:
6893 // accumulates in output buffer: input buffer != output buffer
6894 // Therefore: accumulate <=> input buffer != output buffer
6895 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
6896 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
6897 } else {
6898 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
6899 }
6900 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
6901 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
6902 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
6903 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
6904
Steve Block71f2cf12011-10-20 11:56:00 +01006905 ALOGV("configure() %p thread %p buffer %p framecount %d",
Eric Laurent8ed6ed02010-07-13 04:45:46 -07006906 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
6907
Eric Laurent65b65452010-06-01 23:49:17 -07006908 status_t cmdStatus;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07006909 uint32_t size = sizeof(int);
6910 status_t status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurent4abf8822011-12-16 15:30:36 -08006911 EFFECT_CMD_SET_CONFIG,
Eric Laurenta4c72ac2010-07-28 05:40:18 -07006912 sizeof(effect_config_t),
6913 &mConfig,
6914 &size,
6915 &cmdStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006916 if (status == 0) {
6917 status = cmdStatus;
6918 }
Eric Laurent7d850f22010-07-09 13:34:17 -07006919
6920 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
6921 (1000 * mConfig.outputCfg.buffer.frameCount);
6922
Eric Laurent65b65452010-06-01 23:49:17 -07006923 return status;
6924}
6925
6926status_t AudioFlinger::EffectModule::init()
6927{
Eric Laurentdf9b81c2010-07-02 08:12:41 -07006928 Mutex::Autolock _l(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -07006929 if (mEffectInterface == NULL) {
6930 return NO_INIT;
6931 }
6932 status_t cmdStatus;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07006933 uint32_t size = sizeof(status_t);
6934 status_t status = (*mEffectInterface)->command(mEffectInterface,
6935 EFFECT_CMD_INIT,
6936 0,
6937 NULL,
6938 &size,
6939 &cmdStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006940 if (status == 0) {
6941 status = cmdStatus;
6942 }
6943 return status;
6944}
6945
Eric Laurent6fccbd02011-10-05 17:42:25 -07006946status_t AudioFlinger::EffectModule::start()
6947{
6948 Mutex::Autolock _l(mLock);
6949 return start_l();
6950}
6951
Eric Laurentdf9b81c2010-07-02 08:12:41 -07006952status_t AudioFlinger::EffectModule::start_l()
Eric Laurent65b65452010-06-01 23:49:17 -07006953{
6954 if (mEffectInterface == NULL) {
6955 return NO_INIT;
6956 }
6957 status_t cmdStatus;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07006958 uint32_t size = sizeof(status_t);
6959 status_t status = (*mEffectInterface)->command(mEffectInterface,
6960 EFFECT_CMD_ENABLE,
6961 0,
6962 NULL,
6963 &size,
6964 &cmdStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07006965 if (status == 0) {
6966 status = cmdStatus;
6967 }
Eric Laurent464d5b32011-06-17 21:29:58 -07006968 if (status == 0 &&
6969 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
6970 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
6971 sp<ThreadBase> thread = mThread.promote();
6972 if (thread != 0) {
Eric Laurent828b9772011-08-07 16:32:26 -07006973 audio_stream_t *stream = thread->stream();
6974 if (stream != NULL) {
6975 stream->add_audio_effect(stream, mEffectInterface);
6976 }
Eric Laurent464d5b32011-06-17 21:29:58 -07006977 }
6978 }
Eric Laurent65b65452010-06-01 23:49:17 -07006979 return status;
6980}
6981
Eric Laurent21b5c472011-07-26 20:54:46 -07006982status_t AudioFlinger::EffectModule::stop()
6983{
6984 Mutex::Autolock _l(mLock);
6985 return stop_l();
6986}
6987
Eric Laurentdf9b81c2010-07-02 08:12:41 -07006988status_t AudioFlinger::EffectModule::stop_l()
Eric Laurent65b65452010-06-01 23:49:17 -07006989{
6990 if (mEffectInterface == NULL) {
6991 return NO_INIT;
6992 }
6993 status_t cmdStatus;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07006994 uint32_t size = sizeof(status_t);
6995 status_t status = (*mEffectInterface)->command(mEffectInterface,
6996 EFFECT_CMD_DISABLE,
6997 0,
6998 NULL,
6999 &size,
7000 &cmdStatus);
Eric Laurent65b65452010-06-01 23:49:17 -07007001 if (status == 0) {
7002 status = cmdStatus;
7003 }
Eric Laurent464d5b32011-06-17 21:29:58 -07007004 if (status == 0 &&
7005 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
7006 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
7007 sp<ThreadBase> thread = mThread.promote();
7008 if (thread != 0) {
Eric Laurent828b9772011-08-07 16:32:26 -07007009 audio_stream_t *stream = thread->stream();
7010 if (stream != NULL) {
7011 stream->remove_audio_effect(stream, mEffectInterface);
7012 }
Eric Laurent464d5b32011-06-17 21:29:58 -07007013 }
7014 }
Eric Laurent65b65452010-06-01 23:49:17 -07007015 return status;
7016}
7017
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007018status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
7019 uint32_t cmdSize,
7020 void *pCmdData,
7021 uint32_t *replySize,
7022 void *pReplyData)
Eric Laurent65b65452010-06-01 23:49:17 -07007023{
Eric Laurentdf9b81c2010-07-02 08:12:41 -07007024 Mutex::Autolock _l(mLock);
Steve Block71f2cf12011-10-20 11:56:00 +01007025// ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
Eric Laurent65b65452010-06-01 23:49:17 -07007026
Eric Laurent21b5c472011-07-26 20:54:46 -07007027 if (mState == DESTROYED || mEffectInterface == NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07007028 return NO_INIT;
7029 }
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007030 status_t status = (*mEffectInterface)->command(mEffectInterface,
7031 cmdCode,
7032 cmdSize,
7033 pCmdData,
7034 replySize,
7035 pReplyData);
Eric Laurent65b65452010-06-01 23:49:17 -07007036 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007037 uint32_t size = (replySize == NULL) ? 0 : *replySize;
Eric Laurent65b65452010-06-01 23:49:17 -07007038 for (size_t i = 1; i < mHandles.size(); i++) {
7039 sp<EffectHandle> h = mHandles[i].promote();
7040 if (h != 0) {
7041 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
7042 }
7043 }
7044 }
7045 return status;
7046}
7047
7048status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
7049{
Eric Laurent6752ec82011-08-10 10:37:50 -07007050
Eric Laurent65b65452010-06-01 23:49:17 -07007051 Mutex::Autolock _l(mLock);
Steve Block71f2cf12011-10-20 11:56:00 +01007052 ALOGV("setEnabled %p enabled %d", this, enabled);
Eric Laurent65b65452010-06-01 23:49:17 -07007053
7054 if (enabled != isEnabled()) {
Eric Laurent6752ec82011-08-10 10:37:50 -07007055 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
7056 if (enabled && status != NO_ERROR) {
7057 return status;
7058 }
7059
Eric Laurent65b65452010-06-01 23:49:17 -07007060 switch (mState) {
7061 // going from disabled to enabled
7062 case IDLE:
Eric Laurent7d850f22010-07-09 13:34:17 -07007063 mState = STARTING;
7064 break;
7065 case STOPPED:
7066 mState = RESTART;
Eric Laurent65b65452010-06-01 23:49:17 -07007067 break;
7068 case STOPPING:
7069 mState = ACTIVE;
7070 break;
Eric Laurent65b65452010-06-01 23:49:17 -07007071
7072 // going from enabled to disabled
Eric Laurent7d850f22010-07-09 13:34:17 -07007073 case RESTART:
Eric Laurenta92ebfa2010-08-31 13:50:07 -07007074 mState = STOPPED;
7075 break;
Eric Laurent65b65452010-06-01 23:49:17 -07007076 case STARTING:
Eric Laurent7d850f22010-07-09 13:34:17 -07007077 mState = IDLE;
Eric Laurent65b65452010-06-01 23:49:17 -07007078 break;
7079 case ACTIVE:
7080 mState = STOPPING;
7081 break;
Eric Laurent21b5c472011-07-26 20:54:46 -07007082 case DESTROYED:
7083 return NO_ERROR; // simply ignore as we are being destroyed
Eric Laurent65b65452010-06-01 23:49:17 -07007084 }
7085 for (size_t i = 1; i < mHandles.size(); i++) {
7086 sp<EffectHandle> h = mHandles[i].promote();
7087 if (h != 0) {
7088 h->setEnabled(enabled);
7089 }
7090 }
7091 }
7092 return NO_ERROR;
7093}
7094
Glenn Kasteneabd94a2012-02-02 14:06:11 -08007095bool AudioFlinger::EffectModule::isEnabled() const
Eric Laurent65b65452010-06-01 23:49:17 -07007096{
7097 switch (mState) {
Eric Laurent7d850f22010-07-09 13:34:17 -07007098 case RESTART:
Eric Laurent65b65452010-06-01 23:49:17 -07007099 case STARTING:
7100 case ACTIVE:
7101 return true;
7102 case IDLE:
7103 case STOPPING:
7104 case STOPPED:
Eric Laurent21b5c472011-07-26 20:54:46 -07007105 case DESTROYED:
Eric Laurent65b65452010-06-01 23:49:17 -07007106 default:
7107 return false;
7108 }
7109}
7110
Glenn Kasteneabd94a2012-02-02 14:06:11 -08007111bool AudioFlinger::EffectModule::isProcessEnabled() const
Eric Laurenta92ebfa2010-08-31 13:50:07 -07007112{
7113 switch (mState) {
7114 case RESTART:
7115 case ACTIVE:
7116 case STOPPING:
7117 case STOPPED:
7118 return true;
7119 case IDLE:
7120 case STARTING:
Eric Laurent21b5c472011-07-26 20:54:46 -07007121 case DESTROYED:
Eric Laurenta92ebfa2010-08-31 13:50:07 -07007122 default:
7123 return false;
7124 }
7125}
7126
Eric Laurent65b65452010-06-01 23:49:17 -07007127status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
7128{
Eric Laurentdf9b81c2010-07-02 08:12:41 -07007129 Mutex::Autolock _l(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -07007130 status_t status = NO_ERROR;
7131
7132 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
7133 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
Eric Laurenta92ebfa2010-08-31 13:50:07 -07007134 if (isProcessEnabled() &&
Eric Laurent0d7e0482010-07-19 06:24:46 -07007135 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
7136 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurent65b65452010-06-01 23:49:17 -07007137 status_t cmdStatus;
7138 uint32_t volume[2];
7139 uint32_t *pVolume = NULL;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007140 uint32_t size = sizeof(volume);
Eric Laurent65b65452010-06-01 23:49:17 -07007141 volume[0] = *left;
7142 volume[1] = *right;
7143 if (controller) {
7144 pVolume = volume;
7145 }
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007146 status = (*mEffectInterface)->command(mEffectInterface,
7147 EFFECT_CMD_SET_VOLUME,
7148 size,
7149 volume,
7150 &size,
7151 pVolume);
Eric Laurent65b65452010-06-01 23:49:17 -07007152 if (controller && status == NO_ERROR && size == sizeof(volume)) {
7153 *left = volume[0];
7154 *right = volume[1];
7155 }
7156 }
7157 return status;
7158}
7159
7160status_t AudioFlinger::EffectModule::setDevice(uint32_t device)
7161{
Eric Laurentdf9b81c2010-07-02 08:12:41 -07007162 Mutex::Autolock _l(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -07007163 status_t status = NO_ERROR;
Eric Laurent464d5b32011-06-17 21:29:58 -07007164 if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
7165 // audio pre processing modules on RecordThread can receive both output and
7166 // input device indication in the same call
7167 uint32_t dev = device & AUDIO_DEVICE_OUT_ALL;
7168 if (dev) {
7169 status_t cmdStatus;
7170 uint32_t size = sizeof(status_t);
7171
7172 status = (*mEffectInterface)->command(mEffectInterface,
7173 EFFECT_CMD_SET_DEVICE,
7174 sizeof(uint32_t),
7175 &dev,
7176 &size,
7177 &cmdStatus);
7178 if (status == NO_ERROR) {
7179 status = cmdStatus;
7180 }
7181 }
7182 dev = device & AUDIO_DEVICE_IN_ALL;
7183 if (dev) {
7184 status_t cmdStatus;
7185 uint32_t size = sizeof(status_t);
7186
7187 status_t status2 = (*mEffectInterface)->command(mEffectInterface,
7188 EFFECT_CMD_SET_INPUT_DEVICE,
7189 sizeof(uint32_t),
7190 &dev,
7191 &size,
7192 &cmdStatus);
7193 if (status2 == NO_ERROR) {
7194 status2 = cmdStatus;
7195 }
7196 if (status == NO_ERROR) {
7197 status = status2;
7198 }
Eric Laurent65b65452010-06-01 23:49:17 -07007199 }
7200 }
7201 return status;
7202}
7203
Glenn Kastenaccb1142012-01-04 11:00:47 -08007204status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
Eric Laurent53334cd2010-06-23 17:38:20 -07007205{
Eric Laurentdf9b81c2010-07-02 08:12:41 -07007206 Mutex::Autolock _l(mLock);
Eric Laurent53334cd2010-06-23 17:38:20 -07007207 status_t status = NO_ERROR;
7208 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
Eric Laurent53334cd2010-06-23 17:38:20 -07007209 status_t cmdStatus;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007210 uint32_t size = sizeof(status_t);
7211 status = (*mEffectInterface)->command(mEffectInterface,
7212 EFFECT_CMD_SET_AUDIO_MODE,
Glenn Kastenaccb1142012-01-04 11:00:47 -08007213 sizeof(audio_mode_t),
Eric Laurent0fb66c22011-05-17 19:16:02 -07007214 &mode,
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007215 &size,
7216 &cmdStatus);
Eric Laurent53334cd2010-06-23 17:38:20 -07007217 if (status == NO_ERROR) {
7218 status = cmdStatus;
7219 }
7220 }
7221 return status;
7222}
7223
Eric Laurentf82fccd2011-07-27 19:49:51 -07007224void AudioFlinger::EffectModule::setSuspended(bool suspended)
7225{
7226 Mutex::Autolock _l(mLock);
7227 mSuspended = suspended;
7228}
Glenn Kasten1dce8412012-01-04 11:01:11 -08007229
7230bool AudioFlinger::EffectModule::suspended() const
Eric Laurentf82fccd2011-07-27 19:49:51 -07007231{
7232 Mutex::Autolock _l(mLock);
7233 return mSuspended;
7234}
7235
Eric Laurent65b65452010-06-01 23:49:17 -07007236status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
7237{
7238 const size_t SIZE = 256;
7239 char buffer[SIZE];
7240 String8 result;
7241
7242 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
7243 result.append(buffer);
7244
7245 bool locked = tryLock(mLock);
7246 // failed to lock - AudioFlinger is probably deadlocked
7247 if (!locked) {
7248 result.append("\t\tCould not lock Fx mutex:\n");
7249 }
7250
7251 result.append("\t\tSession Status State Engine:\n");
7252 snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n",
7253 mSessionId, mStatus, mState, (uint32_t)mEffectInterface);
7254 result.append(buffer);
7255
7256 result.append("\t\tDescriptor:\n");
7257 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
7258 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
7259 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2],
7260 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
7261 result.append(buffer);
7262 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
7263 mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion,
7264 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2],
7265 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
7266 result.append(buffer);
Eric Laurent0fb66c22011-05-17 19:16:02 -07007267 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n",
Eric Laurent65b65452010-06-01 23:49:17 -07007268 mDescriptor.apiVersion,
7269 mDescriptor.flags);
7270 result.append(buffer);
7271 snprintf(buffer, SIZE, "\t\t- name: %s\n",
7272 mDescriptor.name);
7273 result.append(buffer);
7274 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
7275 mDescriptor.implementor);
7276 result.append(buffer);
7277
7278 result.append("\t\t- Input configuration:\n");
7279 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
7280 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
7281 (uint32_t)mConfig.inputCfg.buffer.raw,
7282 mConfig.inputCfg.buffer.frameCount,
7283 mConfig.inputCfg.samplingRate,
7284 mConfig.inputCfg.channels,
7285 mConfig.inputCfg.format);
7286 result.append(buffer);
7287
7288 result.append("\t\t- Output configuration:\n");
7289 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
7290 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
7291 (uint32_t)mConfig.outputCfg.buffer.raw,
7292 mConfig.outputCfg.buffer.frameCount,
7293 mConfig.outputCfg.samplingRate,
7294 mConfig.outputCfg.channels,
7295 mConfig.outputCfg.format);
7296 result.append(buffer);
7297
7298 snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size());
7299 result.append(buffer);
7300 result.append("\t\t\tPid Priority Ctrl Locked client server\n");
7301 for (size_t i = 0; i < mHandles.size(); ++i) {
7302 sp<EffectHandle> handle = mHandles[i].promote();
7303 if (handle != 0) {
7304 handle->dump(buffer, SIZE);
7305 result.append(buffer);
7306 }
7307 }
7308
7309 result.append("\n");
7310
7311 write(fd, result.string(), result.length());
7312
7313 if (locked) {
7314 mLock.unlock();
7315 }
7316
7317 return NO_ERROR;
7318}
7319
7320// ----------------------------------------------------------------------------
7321// EffectHandle implementation
7322// ----------------------------------------------------------------------------
7323
7324#undef LOG_TAG
7325#define LOG_TAG "AudioFlinger::EffectHandle"
7326
7327AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
7328 const sp<AudioFlinger::Client>& client,
7329 const sp<IEffectClient>& effectClient,
7330 int32_t priority)
7331 : BnEffect(),
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007332 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentf82fccd2011-07-27 19:49:51 -07007333 mPriority(priority), mHasControl(false), mEnabled(false)
Eric Laurent65b65452010-06-01 23:49:17 -07007334{
Steve Block71f2cf12011-10-20 11:56:00 +01007335 ALOGV("constructor %p", this);
Eric Laurent65b65452010-06-01 23:49:17 -07007336
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007337 if (client == 0) {
7338 return;
7339 }
Eric Laurent65b65452010-06-01 23:49:17 -07007340 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
7341 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
7342 if (mCblkMemory != 0) {
7343 mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer());
7344
Glenn Kasten3694ec12012-01-27 16:47:15 -08007345 if (mCblk != NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07007346 new(mCblk) effect_param_cblk_t();
7347 mBuffer = (uint8_t *)mCblk + bufOffset;
Glenn Kasten18db49a2012-03-12 16:29:55 -07007348 }
Eric Laurent65b65452010-06-01 23:49:17 -07007349 } else {
Steve Block3762c312012-01-06 19:20:56 +00007350 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t));
Eric Laurent65b65452010-06-01 23:49:17 -07007351 return;
7352 }
7353}
7354
7355AudioFlinger::EffectHandle::~EffectHandle()
7356{
Steve Block71f2cf12011-10-20 11:56:00 +01007357 ALOGV("Destructor %p", this);
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007358 disconnect(false);
Steve Block71f2cf12011-10-20 11:56:00 +01007359 ALOGV("Destructor DONE %p", this);
Eric Laurent65b65452010-06-01 23:49:17 -07007360}
7361
7362status_t AudioFlinger::EffectHandle::enable()
7363{
Steve Block71f2cf12011-10-20 11:56:00 +01007364 ALOGV("enable %p", this);
Eric Laurent65b65452010-06-01 23:49:17 -07007365 if (!mHasControl) return INVALID_OPERATION;
7366 if (mEffect == 0) return DEAD_OBJECT;
7367
Eric Laurent6752ec82011-08-10 10:37:50 -07007368 if (mEnabled) {
7369 return NO_ERROR;
7370 }
7371
Eric Laurentf82fccd2011-07-27 19:49:51 -07007372 mEnabled = true;
7373
7374 sp<ThreadBase> thread = mEffect->thread().promote();
7375 if (thread != 0) {
7376 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
7377 }
7378
7379 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
7380 if (mEffect->suspended()) {
7381 return NO_ERROR;
7382 }
7383
Eric Laurent6752ec82011-08-10 10:37:50 -07007384 status_t status = mEffect->setEnabled(true);
7385 if (status != NO_ERROR) {
7386 if (thread != 0) {
7387 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
7388 }
7389 mEnabled = false;
7390 }
7391 return status;
Eric Laurent65b65452010-06-01 23:49:17 -07007392}
7393
7394status_t AudioFlinger::EffectHandle::disable()
7395{
Steve Block71f2cf12011-10-20 11:56:00 +01007396 ALOGV("disable %p", this);
Eric Laurent65b65452010-06-01 23:49:17 -07007397 if (!mHasControl) return INVALID_OPERATION;
Eric Laurentf82fccd2011-07-27 19:49:51 -07007398 if (mEffect == 0) return DEAD_OBJECT;
Eric Laurent65b65452010-06-01 23:49:17 -07007399
Eric Laurent6752ec82011-08-10 10:37:50 -07007400 if (!mEnabled) {
7401 return NO_ERROR;
7402 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07007403 mEnabled = false;
7404
7405 if (mEffect->suspended()) {
7406 return NO_ERROR;
7407 }
7408
7409 status_t status = mEffect->setEnabled(false);
7410
7411 sp<ThreadBase> thread = mEffect->thread().promote();
7412 if (thread != 0) {
7413 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
7414 }
7415
7416 return status;
Eric Laurent65b65452010-06-01 23:49:17 -07007417}
7418
7419void AudioFlinger::EffectHandle::disconnect()
7420{
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007421 disconnect(true);
7422}
7423
Glenn Kasten29441ff2012-02-03 10:32:24 -08007424void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007425{
Glenn Kasten29441ff2012-02-03 10:32:24 -08007426 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
Eric Laurent65b65452010-06-01 23:49:17 -07007427 if (mEffect == 0) {
7428 return;
7429 }
Glenn Kasten29441ff2012-02-03 10:32:24 -08007430 mEffect->disconnect(this, unpinIfLast);
Eric Laurentf82fccd2011-07-27 19:49:51 -07007431
Eric Laurent7fa1cee2011-10-19 11:44:54 -07007432 if (mHasControl && mEnabled) {
Eric Laurent6752ec82011-08-10 10:37:50 -07007433 sp<ThreadBase> thread = mEffect->thread().promote();
7434 if (thread != 0) {
7435 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
7436 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07007437 }
7438
Eric Laurent65b65452010-06-01 23:49:17 -07007439 // release sp on module => module destructor can be called now
7440 mEffect.clear();
Eric Laurent65b65452010-06-01 23:49:17 -07007441 if (mClient != 0) {
Glenn Kasten3694ec12012-01-27 16:47:15 -08007442 if (mCblk != NULL) {
Glenn Kastend9d68dc2012-02-03 10:24:48 -08007443 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007444 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
7445 }
Glenn Kastenf31d0502012-01-25 15:27:15 -08007446 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Glenn Kasten803a86a2012-01-25 14:28:29 -08007447 // Client destructor must run with AudioFlinger mutex locked
Eric Laurent65b65452010-06-01 23:49:17 -07007448 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
7449 mClient.clear();
7450 }
7451}
7452
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007453status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
7454 uint32_t cmdSize,
7455 void *pCmdData,
7456 uint32_t *replySize,
7457 void *pReplyData)
Eric Laurent65b65452010-06-01 23:49:17 -07007458{
Steve Block71f2cf12011-10-20 11:56:00 +01007459// ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007460// cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
Eric Laurent65b65452010-06-01 23:49:17 -07007461
7462 // only get parameter command is permitted for applications not controlling the effect
7463 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
7464 return INVALID_OPERATION;
7465 }
7466 if (mEffect == 0) return DEAD_OBJECT;
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007467 if (mClient == 0) return INVALID_OPERATION;
Eric Laurent65b65452010-06-01 23:49:17 -07007468
7469 // handle commands that are not forwarded transparently to effect engine
7470 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
7471 // No need to trylock() here as this function is executed in the binder thread serving a particular client process:
7472 // no risk to block the whole media server process or mixer threads is we are stuck here
7473 Mutex::Autolock _l(mCblk->lock);
7474 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
7475 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
7476 mCblk->serverIndex = 0;
7477 mCblk->clientIndex = 0;
7478 return BAD_VALUE;
7479 }
7480 status_t status = NO_ERROR;
7481 while (mCblk->serverIndex < mCblk->clientIndex) {
7482 int reply;
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007483 uint32_t rsize = sizeof(int);
Eric Laurent65b65452010-06-01 23:49:17 -07007484 int *p = (int *)(mBuffer + mCblk->serverIndex);
7485 int size = *p++;
Eric Laurent53334cd2010-06-23 17:38:20 -07007486 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
Steve Block8564c8d2012-01-05 23:22:43 +00007487 ALOGW("command(): invalid parameter block size");
Eric Laurent53334cd2010-06-23 17:38:20 -07007488 break;
7489 }
Eric Laurent65b65452010-06-01 23:49:17 -07007490 effect_param_t *param = (effect_param_t *)p;
Eric Laurent53334cd2010-06-23 17:38:20 -07007491 if (param->psize == 0 || param->vsize == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00007492 ALOGW("command(): null parameter or value size");
Eric Laurent53334cd2010-06-23 17:38:20 -07007493 mCblk->serverIndex += size;
7494 continue;
7495 }
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007496 uint32_t psize = sizeof(effect_param_t) +
7497 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
7498 param->vsize;
7499 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
7500 psize,
7501 p,
7502 &rsize,
7503 &reply);
Eric Laurente65280c2010-09-02 11:56:55 -07007504 // stop at first error encountered
7505 if (ret != NO_ERROR) {
Eric Laurent65b65452010-06-01 23:49:17 -07007506 status = ret;
Eric Laurente65280c2010-09-02 11:56:55 -07007507 *(int *)pReplyData = reply;
7508 break;
7509 } else if (reply != NO_ERROR) {
7510 *(int *)pReplyData = reply;
7511 break;
Eric Laurent65b65452010-06-01 23:49:17 -07007512 }
7513 mCblk->serverIndex += size;
7514 }
7515 mCblk->serverIndex = 0;
7516 mCblk->clientIndex = 0;
7517 return status;
7518 } else if (cmdCode == EFFECT_CMD_ENABLE) {
Eric Laurente65280c2010-09-02 11:56:55 -07007519 *(int *)pReplyData = NO_ERROR;
Eric Laurent65b65452010-06-01 23:49:17 -07007520 return enable();
7521 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurente65280c2010-09-02 11:56:55 -07007522 *(int *)pReplyData = NO_ERROR;
Eric Laurent65b65452010-06-01 23:49:17 -07007523 return disable();
7524 }
7525
7526 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
7527}
7528
Eric Laurentf82fccd2011-07-27 19:49:51 -07007529void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
Eric Laurent65b65452010-06-01 23:49:17 -07007530{
Steve Block71f2cf12011-10-20 11:56:00 +01007531 ALOGV("setControl %p control %d", this, hasControl);
Eric Laurent65b65452010-06-01 23:49:17 -07007532
7533 mHasControl = hasControl;
Eric Laurentf82fccd2011-07-27 19:49:51 -07007534 mEnabled = enabled;
7535
Eric Laurent65b65452010-06-01 23:49:17 -07007536 if (signal && mEffectClient != 0) {
7537 mEffectClient->controlStatusChanged(hasControl);
7538 }
7539}
7540
Eric Laurenta4c72ac2010-07-28 05:40:18 -07007541void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
7542 uint32_t cmdSize,
7543 void *pCmdData,
7544 uint32_t replySize,
7545 void *pReplyData)
Eric Laurent65b65452010-06-01 23:49:17 -07007546{
7547 if (mEffectClient != 0) {
7548 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
7549 }
7550}
7551
7552
7553
7554void AudioFlinger::EffectHandle::setEnabled(bool enabled)
7555{
7556 if (mEffectClient != 0) {
7557 mEffectClient->enableStatusChanged(enabled);
7558 }
7559}
7560
7561status_t AudioFlinger::EffectHandle::onTransact(
7562 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
7563{
7564 return BnEffect::onTransact(code, data, reply, flags);
7565}
7566
7567
7568void AudioFlinger::EffectHandle::dump(char* buffer, size_t size)
7569{
Glenn Kasten3694ec12012-01-27 16:47:15 -08007570 bool locked = mCblk != NULL && tryLock(mCblk->lock);
Eric Laurent65b65452010-06-01 23:49:17 -07007571
7572 snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n",
Glenn Kasten81211142012-02-05 18:09:08 -08007573 (mClient == 0) ? getpid_cached : mClient->pid(),
Eric Laurent65b65452010-06-01 23:49:17 -07007574 mPriority,
7575 mHasControl,
7576 !locked,
Marco Nelissenc74b93f2011-08-02 13:33:41 -07007577 mCblk ? mCblk->clientIndex : 0,
7578 mCblk ? mCblk->serverIndex : 0
Eric Laurent65b65452010-06-01 23:49:17 -07007579 );
7580
7581 if (locked) {
7582 mCblk->lock.unlock();
7583 }
7584}
7585
7586#undef LOG_TAG
7587#define LOG_TAG "AudioFlinger::EffectChain"
7588
Glenn Kasten685c9ce2012-01-20 13:32:16 -08007589AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Eric Laurent65b65452010-06-01 23:49:17 -07007590 int sessionId)
Glenn Kasten685c9ce2012-01-20 13:32:16 -08007591 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Eric Laurent90681d62011-05-09 12:09:06 -07007592 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
7593 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurent65b65452010-06-01 23:49:17 -07007594{
Dima Zavin24fc2fb2011-04-19 22:30:36 -07007595 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Glenn Kasten685c9ce2012-01-20 13:32:16 -08007596 if (thread == NULL) {
Eric Laurentf9c361d2011-11-11 15:42:52 -08007597 return;
7598 }
7599 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
7600 thread->frameCount();
Eric Laurent65b65452010-06-01 23:49:17 -07007601}
7602
7603AudioFlinger::EffectChain::~EffectChain()
7604{
7605 if (mOwnInBuffer) {
7606 delete mInBuffer;
7607 }
7608
7609}
7610
Eric Laurentf82fccd2011-07-27 19:49:51 -07007611// getEffectFromDesc_l() must be called with ThreadBase::mLock held
Eric Laurent76c40f72010-07-15 12:50:15 -07007612sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor)
Eric Laurent65b65452010-06-01 23:49:17 -07007613{
Eric Laurent65b65452010-06-01 23:49:17 -07007614 size_t size = mEffects.size();
7615
7616 for (size_t i = 0; i < size; i++) {
7617 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kastenc64a6f02012-01-30 13:00:02 -08007618 return mEffects[i];
Eric Laurent65b65452010-06-01 23:49:17 -07007619 }
7620 }
Glenn Kastenc64a6f02012-01-30 13:00:02 -08007621 return 0;
Eric Laurent65b65452010-06-01 23:49:17 -07007622}
7623
Eric Laurentf82fccd2011-07-27 19:49:51 -07007624// getEffectFromId_l() must be called with ThreadBase::mLock held
Eric Laurent76c40f72010-07-15 12:50:15 -07007625sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
Eric Laurent65b65452010-06-01 23:49:17 -07007626{
Eric Laurent65b65452010-06-01 23:49:17 -07007627 size_t size = mEffects.size();
7628
7629 for (size_t i = 0; i < size; i++) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -07007630 // by convention, return first effect if id provided is 0 (0 is never a valid id)
7631 if (id == 0 || mEffects[i]->id() == id) {
Glenn Kastenc64a6f02012-01-30 13:00:02 -08007632 return mEffects[i];
Eric Laurent65b65452010-06-01 23:49:17 -07007633 }
7634 }
Glenn Kastenc64a6f02012-01-30 13:00:02 -08007635 return 0;
Eric Laurent65b65452010-06-01 23:49:17 -07007636}
7637
Eric Laurentf82fccd2011-07-27 19:49:51 -07007638// getEffectFromType_l() must be called with ThreadBase::mLock held
7639sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
7640 const effect_uuid_t *type)
7641{
Eric Laurentf82fccd2011-07-27 19:49:51 -07007642 size_t size = mEffects.size();
7643
7644 for (size_t i = 0; i < size; i++) {
7645 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
Glenn Kastenc64a6f02012-01-30 13:00:02 -08007646 return mEffects[i];
Eric Laurentf82fccd2011-07-27 19:49:51 -07007647 }
7648 }
Glenn Kastenc64a6f02012-01-30 13:00:02 -08007649 return 0;
Eric Laurentf82fccd2011-07-27 19:49:51 -07007650}
7651
Eric Laurent65b65452010-06-01 23:49:17 -07007652// Must be called with EffectChain::mLock locked
7653void AudioFlinger::EffectChain::process_l()
7654{
Eric Laurent4fd3ecc2010-09-28 14:09:57 -07007655 sp<ThreadBase> thread = mThread.promote();
7656 if (thread == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00007657 ALOGW("process_l(): cannot promote mixer thread");
Eric Laurent4fd3ecc2010-09-28 14:09:57 -07007658 return;
7659 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -07007660 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
7661 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Eric Laurentf9c361d2011-11-11 15:42:52 -08007662 // always process effects unless no more tracks are on the session and the effect tail
7663 // has been rendered
7664 bool doProcess = true;
Eric Laurent4fd3ecc2010-09-28 14:09:57 -07007665 if (!isGlobalSession) {
Eric Laurentf9c361d2011-11-11 15:42:52 -08007666 bool tracksOnSession = (trackCnt() != 0);
Eric Laurent90681d62011-05-09 12:09:06 -07007667
Eric Laurentf9c361d2011-11-11 15:42:52 -08007668 if (!tracksOnSession && mTailBufferCount == 0) {
7669 doProcess = false;
7670 }
7671
7672 if (activeTrackCnt() == 0) {
7673 // if no track is active and the effect tail has not been rendered,
7674 // the input buffer must be cleared here as the mixer process will not do it
7675 if (tracksOnSession || mTailBufferCount > 0) {
7676 size_t numSamples = thread->frameCount() * thread->channelCount();
7677 memset(mInBuffer, 0, numSamples * sizeof(int16_t));
7678 if (mTailBufferCount > 0) {
7679 mTailBufferCount--;
7680 }
7681 }
7682 }
Eric Laurent4fd3ecc2010-09-28 14:09:57 -07007683 }
7684
Eric Laurent65b65452010-06-01 23:49:17 -07007685 size_t size = mEffects.size();
Eric Laurentf9c361d2011-11-11 15:42:52 -08007686 if (doProcess) {
Eric Laurent4fd3ecc2010-09-28 14:09:57 -07007687 for (size_t i = 0; i < size; i++) {
7688 mEffects[i]->process();
7689 }
Eric Laurent65b65452010-06-01 23:49:17 -07007690 }
Eric Laurent7d850f22010-07-09 13:34:17 -07007691 for (size_t i = 0; i < size; i++) {
7692 mEffects[i]->updateState();
7693 }
Eric Laurent65b65452010-06-01 23:49:17 -07007694}
7695
Eric Laurent76c40f72010-07-15 12:50:15 -07007696// addEffect_l() must be called with PlaybackThread::mLock held
Eric Laurent8ed6ed02010-07-13 04:45:46 -07007697status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
Eric Laurent65b65452010-06-01 23:49:17 -07007698{
7699 effect_descriptor_t desc = effect->desc();
7700 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
7701
7702 Mutex::Autolock _l(mLock);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07007703 effect->setChain(this);
7704 sp<ThreadBase> thread = mThread.promote();
7705 if (thread == 0) {
7706 return NO_INIT;
7707 }
7708 effect->setThread(thread);
Eric Laurent65b65452010-06-01 23:49:17 -07007709
7710 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
7711 // Auxiliary effects are inserted at the beginning of mEffects vector as
7712 // they are processed first and accumulated in chain input buffer
7713 mEffects.insertAt(effect, 0);
Eric Laurent8ed6ed02010-07-13 04:45:46 -07007714
Eric Laurent65b65452010-06-01 23:49:17 -07007715 // the input buffer for auxiliary effect contains mono samples in
7716 // 32 bit format. This is to avoid saturation in AudoMixer
7717 // accumulation stage. Saturation is done in EffectModule::process() before
7718 // calling the process in effect engine
7719 size_t numSamples = thread->frameCount();
7720 int32_t *buffer = new int32_t[numSamples];
7721 memset(buffer, 0, numSamples * sizeof(int32_t));
7722 effect->setInBuffer((int16_t *)buffer);
7723 // auxiliary effects output samples to chain input buffer for further processing
7724 // by insert effects
7725 effect->setOutBuffer(mInBuffer);
7726 } else {
7727 // Insert effects are inserted at the end of mEffects vector as they are processed
7728 // after track and auxiliary effects.
Eric Laurent53334cd2010-06-23 17:38:20 -07007729 // Insert effect order as a function of indicated preference:
7730 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
7731 // another effect is present
7732 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
7733 // last effect claiming first position
7734 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
7735 // first effect claiming last position
Eric Laurent65b65452010-06-01 23:49:17 -07007736 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
Eric Laurent53334cd2010-06-23 17:38:20 -07007737 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
7738 // already present
Eric Laurent65b65452010-06-01 23:49:17 -07007739
Glenn Kasten150d2382012-02-08 14:04:28 -08007740 size_t size = mEffects.size();
7741 size_t idx_insert = size;
7742 ssize_t idx_insert_first = -1;
7743 ssize_t idx_insert_last = -1;
Eric Laurent65b65452010-06-01 23:49:17 -07007744
Glenn Kasten150d2382012-02-08 14:04:28 -08007745 for (size_t i = 0; i < size; i++) {
Eric Laurent65b65452010-06-01 23:49:17 -07007746 effect_descriptor_t d = mEffects[i]->desc();
7747 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
7748 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
7749 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
7750 // check invalid effect chaining combinations
7751 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
Eric Laurent53334cd2010-06-23 17:38:20 -07007752 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
Steve Block8564c8d2012-01-05 23:22:43 +00007753 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name);
Eric Laurent65b65452010-06-01 23:49:17 -07007754 return INVALID_OPERATION;
7755 }
Eric Laurent53334cd2010-06-23 17:38:20 -07007756 // remember position of first insert effect and by default
7757 // select this as insert position for new effect
Eric Laurent65b65452010-06-01 23:49:17 -07007758 if (idx_insert == size) {
7759 idx_insert = i;
7760 }
Eric Laurent53334cd2010-06-23 17:38:20 -07007761 // remember position of last insert effect claiming
7762 // first position
Eric Laurent65b65452010-06-01 23:49:17 -07007763 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
7764 idx_insert_first = i;
7765 }
Eric Laurent53334cd2010-06-23 17:38:20 -07007766 // remember position of first insert effect claiming
7767 // last position
7768 if (iPref == EFFECT_FLAG_INSERT_LAST &&
7769 idx_insert_last == -1) {
Eric Laurent65b65452010-06-01 23:49:17 -07007770 idx_insert_last = i;
7771 }
7772 }
7773 }
7774
Eric Laurent53334cd2010-06-23 17:38:20 -07007775 // modify idx_insert from first position if needed
7776 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
7777 if (idx_insert_last != -1) {
7778 idx_insert = idx_insert_last;
7779 } else {
7780 idx_insert = size;
7781 }
7782 } else {
7783 if (idx_insert_first != -1) {
7784 idx_insert = idx_insert_first + 1;
7785 }
Eric Laurent65b65452010-06-01 23:49:17 -07007786 }
7787
7788 // always read samples from chain input buffer
7789 effect->setInBuffer(mInBuffer);
7790
7791 // if last effect in the chain, output samples to chain
7792 // output buffer, otherwise to chain input buffer
7793 if (idx_insert == size) {
7794 if (idx_insert != 0) {
7795 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
7796 mEffects[idx_insert-1]->configure();
7797 }
7798 effect->setOutBuffer(mOutBuffer);
7799 } else {
7800 effect->setOutBuffer(mInBuffer);
7801 }
Eric Laurent53334cd2010-06-23 17:38:20 -07007802 mEffects.insertAt(effect, idx_insert);
Eric Laurent65b65452010-06-01 23:49:17 -07007803
Steve Block71f2cf12011-10-20 11:56:00 +01007804 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert);
Eric Laurent65b65452010-06-01 23:49:17 -07007805 }
7806 effect->configure();
7807 return NO_ERROR;
7808}
7809
Eric Laurent76c40f72010-07-15 12:50:15 -07007810// removeEffect_l() must be called with PlaybackThread::mLock held
7811size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
Eric Laurent65b65452010-06-01 23:49:17 -07007812{
7813 Mutex::Autolock _l(mLock);
Glenn Kasten150d2382012-02-08 14:04:28 -08007814 size_t size = mEffects.size();
Eric Laurent65b65452010-06-01 23:49:17 -07007815 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
7816
Glenn Kasten150d2382012-02-08 14:04:28 -08007817 for (size_t i = 0; i < size; i++) {
Eric Laurent65b65452010-06-01 23:49:17 -07007818 if (effect == mEffects[i]) {
Eric Laurent21b5c472011-07-26 20:54:46 -07007819 // calling stop here will remove pre-processing effect from the audio HAL.
7820 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
7821 // the middle of a read from audio HAL
Eric Laurent6fccbd02011-10-05 17:42:25 -07007822 if (mEffects[i]->state() == EffectModule::ACTIVE ||
7823 mEffects[i]->state() == EffectModule::STOPPING) {
7824 mEffects[i]->stop();
7825 }
Eric Laurent65b65452010-06-01 23:49:17 -07007826 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
7827 delete[] effect->inBuffer();
7828 } else {
7829 if (i == size - 1 && i != 0) {
7830 mEffects[i - 1]->setOutBuffer(mOutBuffer);
7831 mEffects[i - 1]->configure();
7832 }
7833 }
7834 mEffects.removeAt(i);
Steve Block71f2cf12011-10-20 11:56:00 +01007835 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i);
Eric Laurent65b65452010-06-01 23:49:17 -07007836 break;
7837 }
7838 }
Eric Laurent65b65452010-06-01 23:49:17 -07007839
7840 return mEffects.size();
7841}
7842
Eric Laurent76c40f72010-07-15 12:50:15 -07007843// setDevice_l() must be called with PlaybackThread::mLock held
7844void AudioFlinger::EffectChain::setDevice_l(uint32_t device)
Eric Laurent65b65452010-06-01 23:49:17 -07007845{
7846 size_t size = mEffects.size();
7847 for (size_t i = 0; i < size; i++) {
7848 mEffects[i]->setDevice(device);
7849 }
7850}
7851
Eric Laurent76c40f72010-07-15 12:50:15 -07007852// setMode_l() must be called with PlaybackThread::mLock held
Glenn Kastenaccb1142012-01-04 11:00:47 -08007853void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
Eric Laurent53334cd2010-06-23 17:38:20 -07007854{
7855 size_t size = mEffects.size();
7856 for (size_t i = 0; i < size; i++) {
7857 mEffects[i]->setMode(mode);
7858 }
7859}
7860
Eric Laurent76c40f72010-07-15 12:50:15 -07007861// setVolume_l() must be called with PlaybackThread::mLock held
7862bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
Eric Laurent65b65452010-06-01 23:49:17 -07007863{
7864 uint32_t newLeft = *left;
7865 uint32_t newRight = *right;
7866 bool hasControl = false;
Eric Laurent76c40f72010-07-15 12:50:15 -07007867 int ctrlIdx = -1;
7868 size_t size = mEffects.size();
Eric Laurent65b65452010-06-01 23:49:17 -07007869
Eric Laurent76c40f72010-07-15 12:50:15 -07007870 // first update volume controller
7871 for (size_t i = size; i > 0; i--) {
Eric Laurenta92ebfa2010-08-31 13:50:07 -07007872 if (mEffects[i - 1]->isProcessEnabled() &&
Eric Laurent76c40f72010-07-15 12:50:15 -07007873 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
7874 ctrlIdx = i - 1;
Eric Laurent0d7e0482010-07-19 06:24:46 -07007875 hasControl = true;
Eric Laurent76c40f72010-07-15 12:50:15 -07007876 break;
7877 }
7878 }
7879
7880 if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) {
Eric Laurent0d7e0482010-07-19 06:24:46 -07007881 if (hasControl) {
7882 *left = mNewLeftVolume;
7883 *right = mNewRightVolume;
7884 }
7885 return hasControl;
Eric Laurent76c40f72010-07-15 12:50:15 -07007886 }
7887
7888 mVolumeCtrlIdx = ctrlIdx;
Eric Laurent0d7e0482010-07-19 06:24:46 -07007889 mLeftVolume = newLeft;
7890 mRightVolume = newRight;
Eric Laurent76c40f72010-07-15 12:50:15 -07007891
7892 // second get volume update from volume controller
7893 if (ctrlIdx >= 0) {
7894 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
Eric Laurent0d7e0482010-07-19 06:24:46 -07007895 mNewLeftVolume = newLeft;
7896 mNewRightVolume = newRight;
Eric Laurent65b65452010-06-01 23:49:17 -07007897 }
7898 // then indicate volume to all other effects in chain.
7899 // Pass altered volume to effects before volume controller
7900 // and requested volume to effects after controller
7901 uint32_t lVol = newLeft;
7902 uint32_t rVol = newRight;
Eric Laurent76c40f72010-07-15 12:50:15 -07007903
Eric Laurent65b65452010-06-01 23:49:17 -07007904 for (size_t i = 0; i < size; i++) {
Eric Laurent76c40f72010-07-15 12:50:15 -07007905 if ((int)i == ctrlIdx) continue;
7906 // this also works for ctrlIdx == -1 when there is no volume controller
7907 if ((int)i > ctrlIdx) {
Eric Laurent65b65452010-06-01 23:49:17 -07007908 lVol = *left;
7909 rVol = *right;
7910 }
7911 mEffects[i]->setVolume(&lVol, &rVol, false);
7912 }
7913 *left = newLeft;
7914 *right = newRight;
7915
7916 return hasControl;
7917}
7918
Eric Laurent65b65452010-06-01 23:49:17 -07007919status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
7920{
7921 const size_t SIZE = 256;
7922 char buffer[SIZE];
7923 String8 result;
7924
7925 snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId);
7926 result.append(buffer);
7927
7928 bool locked = tryLock(mLock);
7929 // failed to lock - AudioFlinger is probably deadlocked
7930 if (!locked) {
7931 result.append("\tCould not lock mutex:\n");
7932 }
7933
Eric Laurent76c40f72010-07-15 12:50:15 -07007934 result.append("\tNum fx In buffer Out buffer Active tracks:\n");
7935 snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n",
Eric Laurent65b65452010-06-01 23:49:17 -07007936 mEffects.size(),
7937 (uint32_t)mInBuffer,
7938 (uint32_t)mOutBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -07007939 mActiveTrackCnt);
7940 result.append(buffer);
7941 write(fd, result.string(), result.size());
7942
7943 for (size_t i = 0; i < mEffects.size(); ++i) {
7944 sp<EffectModule> effect = mEffects[i];
7945 if (effect != 0) {
7946 effect->dump(fd, args);
7947 }
7948 }
7949
7950 if (locked) {
7951 mLock.unlock();
7952 }
7953
7954 return NO_ERROR;
7955}
7956
Eric Laurentf82fccd2011-07-27 19:49:51 -07007957// must be called with ThreadBase::mLock held
7958void AudioFlinger::EffectChain::setEffectSuspended_l(
7959 const effect_uuid_t *type, bool suspend)
7960{
7961 sp<SuspendedEffectDesc> desc;
7962 // use effect type UUID timelow as key as there is no real risk of identical
7963 // timeLow fields among effect type UUIDs.
Glenn Kasten150d2382012-02-08 14:04:28 -08007964 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
Eric Laurentf82fccd2011-07-27 19:49:51 -07007965 if (suspend) {
7966 if (index >= 0) {
7967 desc = mSuspendedEffects.valueAt(index);
7968 } else {
7969 desc = new SuspendedEffectDesc();
7970 memcpy(&desc->mType, type, sizeof(effect_uuid_t));
7971 mSuspendedEffects.add(type->timeLow, desc);
Steve Block71f2cf12011-10-20 11:56:00 +01007972 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
Eric Laurentf82fccd2011-07-27 19:49:51 -07007973 }
7974 if (desc->mRefCount++ == 0) {
7975 sp<EffectModule> effect = getEffectIfEnabled(type);
7976 if (effect != 0) {
7977 desc->mEffect = effect;
7978 effect->setSuspended(true);
7979 effect->setEnabled(false);
7980 }
7981 }
7982 } else {
7983 if (index < 0) {
7984 return;
7985 }
7986 desc = mSuspendedEffects.valueAt(index);
7987 if (desc->mRefCount <= 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00007988 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentf82fccd2011-07-27 19:49:51 -07007989 desc->mRefCount = 1;
7990 }
7991 if (--desc->mRefCount == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01007992 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
Eric Laurentf82fccd2011-07-27 19:49:51 -07007993 if (desc->mEffect != 0) {
7994 sp<EffectModule> effect = desc->mEffect.promote();
7995 if (effect != 0) {
7996 effect->setSuspended(false);
7997 sp<EffectHandle> handle = effect->controlHandle();
7998 if (handle != 0) {
7999 effect->setEnabled(handle->enabled());
8000 }
8001 }
8002 desc->mEffect.clear();
8003 }
8004 mSuspendedEffects.removeItemsAt(index);
8005 }
8006 }
8007}
8008
8009// must be called with ThreadBase::mLock held
8010void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
8011{
8012 sp<SuspendedEffectDesc> desc;
8013
Glenn Kasten150d2382012-02-08 14:04:28 -08008014 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008015 if (suspend) {
8016 if (index >= 0) {
8017 desc = mSuspendedEffects.valueAt(index);
8018 } else {
8019 desc = new SuspendedEffectDesc();
8020 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
Steve Block71f2cf12011-10-20 11:56:00 +01008021 ALOGV("setEffectSuspendedAll_l() add entry for 0");
Eric Laurentf82fccd2011-07-27 19:49:51 -07008022 }
8023 if (desc->mRefCount++ == 0) {
Glenn Kasten97040262012-01-30 12:56:03 -08008024 Vector< sp<EffectModule> > effects;
8025 getSuspendEligibleEffects(effects);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008026 for (size_t i = 0; i < effects.size(); i++) {
8027 setEffectSuspended_l(&effects[i]->desc().type, true);
8028 }
8029 }
8030 } else {
8031 if (index < 0) {
8032 return;
8033 }
8034 desc = mSuspendedEffects.valueAt(index);
8035 if (desc->mRefCount <= 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00008036 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008037 desc->mRefCount = 1;
8038 }
8039 if (--desc->mRefCount == 0) {
8040 Vector<const effect_uuid_t *> types;
8041 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
8042 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
8043 continue;
8044 }
8045 types.add(&mSuspendedEffects.valueAt(i)->mType);
8046 }
8047 for (size_t i = 0; i < types.size(); i++) {
8048 setEffectSuspended_l(types[i], false);
8049 }
Steve Block71f2cf12011-10-20 11:56:00 +01008050 ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
Eric Laurentf82fccd2011-07-27 19:49:51 -07008051 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
8052 }
8053 }
8054}
8055
Eric Laurent5e7acae2011-09-23 08:40:41 -07008056
8057// The volume effect is used for automated tests only
8058#ifndef OPENSL_ES_H_
8059static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
8060 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
8061const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
8062#endif //OPENSL_ES_H_
8063
Eric Laurent6752ec82011-08-10 10:37:50 -07008064bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
8065{
8066 // auxiliary effects and visualizer are never suspended on output mix
8067 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
8068 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
Eric Laurent5e7acae2011-09-23 08:40:41 -07008069 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
8070 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
Eric Laurent6752ec82011-08-10 10:37:50 -07008071 return false;
8072 }
8073 return true;
8074}
8075
Glenn Kasten97040262012-01-30 12:56:03 -08008076void AudioFlinger::EffectChain::getSuspendEligibleEffects(Vector< sp<AudioFlinger::EffectModule> > &effects)
Eric Laurentf82fccd2011-07-27 19:49:51 -07008077{
Glenn Kasten97040262012-01-30 12:56:03 -08008078 effects.clear();
Eric Laurentf82fccd2011-07-27 19:49:51 -07008079 for (size_t i = 0; i < mEffects.size(); i++) {
Glenn Kasten97040262012-01-30 12:56:03 -08008080 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
8081 effects.add(mEffects[i]);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008082 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07008083 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07008084}
8085
8086sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
8087 const effect_uuid_t *type)
8088{
Glenn Kastenc64a6f02012-01-30 13:00:02 -08008089 sp<EffectModule> effect = getEffectFromType_l(type);
8090 return effect != 0 && effect->isEnabled() ? effect : 0;
Eric Laurentf82fccd2011-07-27 19:49:51 -07008091}
8092
8093void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
8094 bool enabled)
8095{
Glenn Kasten150d2382012-02-08 14:04:28 -08008096 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008097 if (enabled) {
8098 if (index < 0) {
8099 // if the effect is not suspend check if all effects are suspended
8100 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
8101 if (index < 0) {
8102 return;
8103 }
Eric Laurent6752ec82011-08-10 10:37:50 -07008104 if (!isEffectEligibleForSuspend(effect->desc())) {
8105 return;
8106 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07008107 setEffectSuspended_l(&effect->desc().type, enabled);
8108 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
Eric Laurent6752ec82011-08-10 10:37:50 -07008109 if (index < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00008110 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
Eric Laurent6752ec82011-08-10 10:37:50 -07008111 return;
8112 }
Eric Laurentf82fccd2011-07-27 19:49:51 -07008113 }
Steve Block71f2cf12011-10-20 11:56:00 +01008114 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
Glenn Kasten18db49a2012-03-12 16:29:55 -07008115 effect->desc().type.timeLow);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008116 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
8117 // if effect is requested to suspended but was not yet enabled, supend it now.
8118 if (desc->mEffect == 0) {
8119 desc->mEffect = effect;
8120 effect->setEnabled(false);
8121 effect->setSuspended(true);
8122 }
8123 } else {
8124 if (index < 0) {
8125 return;
8126 }
Steve Block71f2cf12011-10-20 11:56:00 +01008127 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
Glenn Kasten18db49a2012-03-12 16:29:55 -07008128 effect->desc().type.timeLow);
Eric Laurentf82fccd2011-07-27 19:49:51 -07008129 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
8130 desc->mEffect.clear();
8131 effect->setSuspended(false);
8132 }
8133}
8134
Eric Laurent65b65452010-06-01 23:49:17 -07008135#undef LOG_TAG
8136#define LOG_TAG "AudioFlinger"
8137
Eric Laurenta553c252009-07-17 12:17:14 -07008138// ----------------------------------------------------------------------------
8139
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07008140status_t AudioFlinger::onTransact(
8141 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
8142{
8143 return BnAudioFlinger::onTransact(code, data, reply, flags);
8144}
8145
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07008146}; // namespace android