blob: 2c3329e3701c0fd0f814efc58533d0926950e257 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -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"
20//#define LOG_NDEBUG 0
21
22#include <math.h>
23#include <signal.h>
24#include <sys/time.h>
25#include <sys/resource.h>
26
Gloria Wang9ee159b2011-02-24 14:51:45 -080027#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028#include <binder/IServiceManager.h>
29#include <utils/Log.h>
30#include <binder/Parcel.h>
31#include <binder/IPCThreadState.h>
32#include <utils/String16.h>
33#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070034#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035
Dima Zavinfce7a472011-04-19 22:30:36 -070036#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <cutils/properties.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080038#include <cutils/compiler.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039
Gloria Wang9ee159b2011-02-24 14:51:45 -080040#include <media/IMediaPlayerService.h>
Glenn Kasten25b248e2012-01-03 15:28:29 -080041#include <media/IMediaDeathNotifier.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
43#include <private/media/AudioTrackShared.h>
44#include <private/media/AudioEffectShared.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Dima Zavin64760242011-05-11 14:15:23 -070046#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070047#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
49#include "AudioMixer.h"
50#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080051#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Mathias Agopian65ab4712010-07-14 17:59:35 -070053#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070054#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070055#include <audio_effects/effect_ns.h>
56#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070057
Glenn Kasten3b21c502011-12-15 09:52:39 -080058#include <audio_utils/primitives.h>
59
Glenn Kasten4d8d0c32011-07-08 15:26:12 -070060#include <cpustats/ThreadCpuUsage.h>
Eric Laurentfeb0db62011-07-22 09:04:31 -070061#include <powermanager/PowerManager.h>
Glenn Kasten4d8d0c32011-07-08 15:26:12 -070062// #define DEBUG_CPU_USAGE 10 // log statistics every n wall clock seconds
63
John Grossman4ff14ba2012-02-08 16:37:41 -080064#include <common_time/cc_helper.h>
65#include <common_time/local_clock.h>
66
Mathias Agopian65ab4712010-07-14 17:59:35 -070067// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070068
Eric Laurentde070132010-07-13 04:45:46 -070069
Mathias Agopian65ab4712010-07-14 17:59:35 -070070namespace android {
71
Glenn Kastenec1d6b52011-12-12 09:04:45 -080072static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
73static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070074
Mathias Agopian65ab4712010-07-14 17:59:35 -070075static const float MAX_GAIN = 4096.0f;
Glenn Kastenb1cf75c2012-01-17 12:20:54 -080076static const uint32_t MAX_GAIN_INT = 0x1000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070077
78// retry counts for buffer fill timeout
79// 50 * ~20msecs = 1 second
80static const int8_t kMaxTrackRetries = 50;
81static const int8_t kMaxTrackStartupRetries = 50;
82// allow less retry attempts on direct output thread.
83// direct outputs can be a scarce resource in audio hardware and should
84// be released as quickly as possible.
85static const int8_t kMaxTrackRetriesDirect = 2;
86
87static const int kDumpLockRetries = 50;
Glenn Kasten7dede872011-12-13 11:04:14 -080088static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070089
Glenn Kasten7dede872011-12-13 11:04:14 -080090// don't warn about blocked writes or record buffer overflows more often than this
91static const nsecs_t kWarningThrottleNs = seconds(5);
Mathias Agopian65ab4712010-07-14 17:59:35 -070092
Eric Laurent7c7f10b2011-06-17 21:29:58 -070093// RecordThread loop sleep time upon application overrun or audio HAL read error
94static const int kRecordThreadSleepUs = 5000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070095
Glenn Kasten7dede872011-12-13 11:04:14 -080096// maximum time to wait for setParameters to complete
97static const nsecs_t kSetParametersTimeoutNs = seconds(2);
Eric Laurent60cd0a02011-09-13 11:40:21 -070098
Eric Laurent7cafbb32011-11-22 18:50:29 -080099// minimum sleep time for the mixer thread loop when tracks are active but in underrun
100static const uint32_t kMinThreadSleepTimeUs = 5000;
101// maximum divider applied to the active sleep time in the mixer thread loop
102static const uint32_t kMaxThreadSleepTimeShift = 2;
103
John Grossman4ff14ba2012-02-08 16:37:41 -0800104nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -0800105
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106// ----------------------------------------------------------------------------
107
Gloria Wang9ee159b2011-02-24 14:51:45 -0800108// To collect the amplifier usage
109static void addBatteryData(uint32_t params) {
Glenn Kasten25b248e2012-01-03 15:28:29 -0800110 sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService();
111 if (service == NULL) {
112 // it already logged
Gloria Wang9ee159b2011-02-24 14:51:45 -0800113 return;
114 }
115
116 service->addBatteryData(params);
117}
118
Dima Zavin799a70e2011-04-18 16:57:27 -0700119static int load_audio_interface(const char *if_name, const hw_module_t **mod,
120 audio_hw_device_t **dev)
121{
122 int rc;
123
124 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, mod);
125 if (rc)
126 goto out;
127
128 rc = audio_hw_device_open(*mod, dev);
Steve Block29357bc2012-01-06 19:20:56 +0000129 ALOGE_IF(rc, "couldn't open audio hw device in %s.%s (%s)",
Dima Zavin799a70e2011-04-18 16:57:27 -0700130 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
131 if (rc)
132 goto out;
133
134 return 0;
135
136out:
137 *mod = NULL;
138 *dev = NULL;
139 return rc;
140}
141
Glenn Kastenec1d6b52011-12-12 09:04:45 -0800142static const char * const audio_interfaces[] = {
Dima Zavin799a70e2011-04-18 16:57:27 -0700143 "primary",
144 "a2dp",
145 "usb",
146};
147#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
148
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149// ----------------------------------------------------------------------------
150
151AudioFlinger::AudioFlinger()
152 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800153 mPrimaryHardwareDev(NULL),
154 mHardwareStatus(AUDIO_HW_IDLE), // see also onFirstRef()
155 mMasterVolume(1.0f),
156 mMasterVolumeSupportLvl(MVS_NONE),
157 mMasterMute(false),
158 mNextUniqueId(1),
159 mMode(AUDIO_MODE_INVALID),
160 mBtNrecIsOff(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700161{
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700162}
163
164void AudioFlinger::onFirstRef()
165{
Dima Zavin799a70e2011-04-18 16:57:27 -0700166 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700167
Eric Laurent93575202011-01-18 18:39:02 -0800168 Mutex::Autolock _l(mLock);
169
Dima Zavin799a70e2011-04-18 16:57:27 -0700170 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800171 char val_str[PROPERTY_VALUE_MAX] = { 0 };
172 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
173 uint32_t int_val;
174 if (1 == sscanf(val_str, "%u", &int_val)) {
175 mStandbyTimeInNsecs = milliseconds(int_val);
176 ALOGI("Using %u mSec as standby time.", int_val);
177 } else {
178 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
179 ALOGI("Using default %u mSec as standby time.",
180 (uint32_t)(mStandbyTimeInNsecs / 1000000));
181 }
182 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700183
Dima Zavin799a70e2011-04-18 16:57:27 -0700184 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
185 const hw_module_t *mod;
186 audio_hw_device_t *dev;
Dima Zavinfce7a472011-04-19 22:30:36 -0700187
Dima Zavin799a70e2011-04-18 16:57:27 -0700188 rc = load_audio_interface(audio_interfaces[i], &mod, &dev);
189 if (rc)
190 continue;
191
Steve Blockdf64d152012-01-04 20:05:49 +0000192 ALOGI("Loaded %s audio interface from %s (%s)", audio_interfaces[i],
Dima Zavin799a70e2011-04-18 16:57:27 -0700193 mod->name, mod->id);
194 mAudioHwDevs.push(dev);
195
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800196 if (mPrimaryHardwareDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700197 mPrimaryHardwareDev = dev;
Steve Blockdf64d152012-01-04 20:05:49 +0000198 ALOGI("Using '%s' (%s.%s) as the primary audio interface",
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700199 mod->name, mod->id, audio_interfaces[i]);
Dima Zavin799a70e2011-04-18 16:57:27 -0700200 }
201 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800203 if (mPrimaryHardwareDev == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000204 ALOGE("Primary audio interface not found");
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800205 // proceed, all later accesses to mPrimaryHardwareDev verify it's safe with initCheck()
Dima Zavin799a70e2011-04-18 16:57:27 -0700206 }
207
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800208 // Currently (mPrimaryHardwareDev == NULL) == (mAudioHwDevs.size() == 0), but the way the
209 // primary HW dev is selected can change so these conditions might not always be equivalent.
210 // When that happens, re-visit all the code that assumes this.
211
212 AutoMutex lock(mHardwareLock);
213
John Grossman4ff14ba2012-02-08 16:37:41 -0800214 // Determine the level of master volume support the primary audio HAL has,
215 // and set the initial master volume at the same time.
216 float initialVolume = 1.0;
217 mMasterVolumeSupportLvl = MVS_NONE;
218 if (0 == mPrimaryHardwareDev->init_check(mPrimaryHardwareDev)) {
219 audio_hw_device_t *dev = mPrimaryHardwareDev;
220
221 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
222 if ((NULL != dev->get_master_volume) &&
223 (NO_ERROR == dev->get_master_volume(dev, &initialVolume))) {
224 mMasterVolumeSupportLvl = MVS_FULL;
225 } else {
226 mMasterVolumeSupportLvl = MVS_SETONLY;
227 initialVolume = 1.0;
228 }
229
230 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
231 if ((NULL == dev->set_master_volume) ||
232 (NO_ERROR != dev->set_master_volume(dev, initialVolume))) {
233 mMasterVolumeSupportLvl = MVS_NONE;
234 }
235 mHardwareStatus = AUDIO_HW_INIT;
236 }
237
238 // Set the mode for each audio HAL, and try to set the initial volume (if
239 // supported) for all of the non-primary audio HALs.
Dima Zavin799a70e2011-04-18 16:57:27 -0700240 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
241 audio_hw_device_t *dev = mAudioHwDevs[i];
242
243 mHardwareStatus = AUDIO_HW_INIT;
244 rc = dev->init_check(dev);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800245 mHardwareStatus = AUDIO_HW_IDLE;
Dima Zavin799a70e2011-04-18 16:57:27 -0700246 if (rc == 0) {
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800247 mMode = AUDIO_MODE_NORMAL; // assigned multiple times with same value
Dima Zavin799a70e2011-04-18 16:57:27 -0700248 mHardwareStatus = AUDIO_HW_SET_MODE;
249 dev->set_mode(dev, mMode);
John Grossman4ff14ba2012-02-08 16:37:41 -0800250
251 if ((dev != mPrimaryHardwareDev) &&
252 (NULL != dev->set_master_volume)) {
253 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
254 dev->set_master_volume(dev, initialVolume);
255 }
256
257 mHardwareStatus = AUDIO_HW_INIT;
Dima Zavin799a70e2011-04-18 16:57:27 -0700258 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800260
261 mMasterVolumeSW = (MVS_NONE == mMasterVolumeSupportLvl)
262 ? initialVolume
263 : 1.0;
264 mMasterVolume = initialVolume;
265 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266}
267
268AudioFlinger::~AudioFlinger()
269{
Dima Zavin799a70e2011-04-18 16:57:27 -0700270
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271 while (!mRecordThreads.isEmpty()) {
272 // closeInput() will remove first entry from mRecordThreads
273 closeInput(mRecordThreads.keyAt(0));
274 }
275 while (!mPlaybackThreads.isEmpty()) {
276 // closeOutput() will remove first entry from mPlaybackThreads
277 closeOutput(mPlaybackThreads.keyAt(0));
278 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700279
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800280 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
281 // no mHardwareLock needed, as there are no other references to this
282 audio_hw_device_close(mAudioHwDevs[i]);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700283 }
284}
285
Dima Zavin799a70e2011-04-18 16:57:27 -0700286audio_hw_device_t* AudioFlinger::findSuitableHwDev_l(uint32_t devices)
287{
288 /* first matching HW device is returned */
289 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
290 audio_hw_device_t *dev = mAudioHwDevs[i];
291 if ((dev->get_supported_devices(dev) & devices) == devices)
292 return dev;
293 }
294 return NULL;
295}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700296
297status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
298{
299 const size_t SIZE = 256;
300 char buffer[SIZE];
301 String8 result;
302
303 result.append("Clients:\n");
304 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800305 sp<Client> client = mClients.valueAt(i).promote();
306 if (client != 0) {
307 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
308 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309 }
310 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700311
312 result.append("Global session refs:\n");
313 result.append(" session pid cnt\n");
314 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
315 AudioSessionRef *r = mAudioSessionRefs[i];
316 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->sessionid, r->pid, r->cnt);
317 result.append(buffer);
318 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 write(fd, result.string(), result.size());
320 return NO_ERROR;
321}
322
323
324status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
325{
326 const size_t SIZE = 256;
327 char buffer[SIZE];
328 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800329 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330
John Grossman4ff14ba2012-02-08 16:37:41 -0800331 snprintf(buffer, SIZE, "Hardware status: %d\n"
332 "Standby Time mSec: %u\n",
333 hardwareStatus,
334 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 result.append(buffer);
336 write(fd, result.string(), result.size());
337 return NO_ERROR;
338}
339
340status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
341{
342 const size_t SIZE = 256;
343 char buffer[SIZE];
344 String8 result;
345 snprintf(buffer, SIZE, "Permission Denial: "
346 "can't dump AudioFlinger from pid=%d, uid=%d\n",
347 IPCThreadState::self()->getCallingPid(),
348 IPCThreadState::self()->getCallingUid());
349 result.append(buffer);
350 write(fd, result.string(), result.size());
351 return NO_ERROR;
352}
353
354static bool tryLock(Mutex& mutex)
355{
356 bool locked = false;
357 for (int i = 0; i < kDumpLockRetries; ++i) {
358 if (mutex.tryLock() == NO_ERROR) {
359 locked = true;
360 break;
361 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800362 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363 }
364 return locked;
365}
366
367status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
368{
Glenn Kasten44deb052012-02-05 18:09:08 -0800369 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 dumpPermissionDenial(fd, args);
371 } else {
372 // get state of hardware lock
373 bool hardwareLocked = tryLock(mHardwareLock);
374 if (!hardwareLocked) {
375 String8 result(kHardwareLockedString);
376 write(fd, result.string(), result.size());
377 } else {
378 mHardwareLock.unlock();
379 }
380
381 bool locked = tryLock(mLock);
382
383 // failed to lock - AudioFlinger is probably deadlocked
384 if (!locked) {
385 String8 result(kDeadlockedString);
386 write(fd, result.string(), result.size());
387 }
388
389 dumpClients(fd, args);
390 dumpInternals(fd, args);
391
392 // dump playback threads
393 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
394 mPlaybackThreads.valueAt(i)->dump(fd, args);
395 }
396
397 // dump record threads
398 for (size_t i = 0; i < mRecordThreads.size(); i++) {
399 mRecordThreads.valueAt(i)->dump(fd, args);
400 }
401
Dima Zavin799a70e2011-04-18 16:57:27 -0700402 // dump all hardware devs
403 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
404 audio_hw_device_t *dev = mAudioHwDevs[i];
405 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700406 }
407 if (locked) mLock.unlock();
408 }
409 return NO_ERROR;
410}
411
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800412sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
413{
414 // If pid is already in the mClients wp<> map, then use that entry
415 // (for which promote() is always != 0), otherwise create a new entry and Client.
416 sp<Client> client = mClients.valueFor(pid).promote();
417 if (client == 0) {
418 client = new Client(this, pid);
419 mClients.add(pid, client);
420 }
421
422 return client;
423}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424
425// IAudioFlinger interface
426
427
428sp<IAudioTrack> AudioFlinger::createTrack(
429 pid_t pid,
Glenn Kastenfff6d712012-01-12 16:38:12 -0800430 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800432 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700433 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700434 int frameCount,
Glenn Kasten5cf034d2012-02-21 10:35:56 -0800435 // FIXME dead, remove from IAudioFlinger
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436 uint32_t flags,
437 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800438 audio_io_handle_t output,
John Grossman4ff14ba2012-02-08 16:37:41 -0800439 bool isTimed,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440 int *sessionId,
441 status_t *status)
442{
443 sp<PlaybackThread::Track> track;
444 sp<TrackHandle> trackHandle;
445 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700446 status_t lStatus;
447 int lSessionId;
448
Glenn Kasten263709e2012-01-06 08:40:01 -0800449 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
450 // but if someone uses binder directly they could bypass that and cause us to crash
451 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000452 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700453 lStatus = BAD_VALUE;
454 goto Exit;
455 }
456
457 {
458 Mutex::Autolock _l(mLock);
459 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700460 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000462 ALOGE("unknown output thread");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463 lStatus = BAD_VALUE;
464 goto Exit;
465 }
466
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800467 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468
Steve Block3856b092011-10-20 11:56:00 +0100469 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700470 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentde070132010-07-13 04:45:46 -0700471 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700472 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
473 if (mPlaybackThreads.keyAt(i) != output) {
474 // prevent same audio session on different output threads
475 uint32_t sessions = t->hasAudioSession(*sessionId);
476 if (sessions & PlaybackThread::TRACK_SESSION) {
Steve Block29357bc2012-01-06 19:20:56 +0000477 ALOGE("createTrack() session ID %d already in use", *sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700478 lStatus = BAD_VALUE;
479 goto Exit;
480 }
481 // check if an effect with same session ID is waiting for a track to be created
482 if (sessions & PlaybackThread::EFFECT_SESSION) {
483 effectThread = t.get();
484 }
Eric Laurentde070132010-07-13 04:45:46 -0700485 }
486 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700487 lSessionId = *sessionId;
488 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700489 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700490 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491 if (sessionId != NULL) {
492 *sessionId = lSessionId;
493 }
494 }
Steve Block3856b092011-10-20 11:56:00 +0100495 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700496
497 track = thread->createTrack_l(client, streamType, sampleRate, format,
John Grossman4ff14ba2012-02-08 16:37:41 -0800498 channelMask, frameCount, sharedBuffer, lSessionId, isTimed, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700499
500 // move effect chain to this output thread if an effect on same session was waiting
501 // for a track to be created
502 if (lStatus == NO_ERROR && effectThread != NULL) {
503 Mutex::Autolock _dl(thread->mLock);
504 Mutex::Autolock _sl(effectThread->mLock);
505 moveEffectChain_l(lSessionId, effectThread, thread, true);
506 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700507 }
508 if (lStatus == NO_ERROR) {
509 trackHandle = new TrackHandle(track);
510 } else {
511 // remove local strong reference to Client before deleting the Track so that the Client
512 // destructor is called by the TrackBase destructor with mLock held
513 client.clear();
514 track.clear();
515 }
516
517Exit:
518 if(status) {
519 *status = lStatus;
520 }
521 return trackHandle;
522}
523
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800524uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700525{
526 Mutex::Autolock _l(mLock);
527 PlaybackThread *thread = checkPlaybackThread_l(output);
528 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000529 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700530 return 0;
531 }
532 return thread->sampleRate();
533}
534
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800535int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700536{
537 Mutex::Autolock _l(mLock);
538 PlaybackThread *thread = checkPlaybackThread_l(output);
539 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000540 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 return 0;
542 }
543 return thread->channelCount();
544}
545
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800546audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547{
548 Mutex::Autolock _l(mLock);
549 PlaybackThread *thread = checkPlaybackThread_l(output);
550 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000551 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800552 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 }
554 return thread->format();
555}
556
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800557size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558{
559 Mutex::Autolock _l(mLock);
560 PlaybackThread *thread = checkPlaybackThread_l(output);
561 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000562 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563 return 0;
564 }
565 return thread->frameCount();
566}
567
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800568uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569{
570 Mutex::Autolock _l(mLock);
571 PlaybackThread *thread = checkPlaybackThread_l(output);
572 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000573 ALOGW("latency() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574 return 0;
575 }
576 return thread->latency();
577}
578
579status_t AudioFlinger::setMasterVolume(float value)
580{
Eric Laurenta1884f92011-08-23 08:25:03 -0700581 status_t ret = initCheck();
582 if (ret != NO_ERROR) {
583 return ret;
584 }
585
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586 // check calling permissions
587 if (!settingsAllowed()) {
588 return PERMISSION_DENIED;
589 }
590
John Grossman4ff14ba2012-02-08 16:37:41 -0800591 float swmv = value;
592
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593 // when hw supports master volume, don't scale in sw mixer
John Grossman4ff14ba2012-02-08 16:37:41 -0800594 if (MVS_NONE != mMasterVolumeSupportLvl) {
595 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
596 AutoMutex lock(mHardwareLock);
597 audio_hw_device_t *dev = mAudioHwDevs[i];
598
599 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
600 if (NULL != dev->set_master_volume) {
601 dev->set_master_volume(dev, value);
602 }
603 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent93575202011-01-18 18:39:02 -0800604 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800605
606 swmv = 1.0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700608
Eric Laurent93575202011-01-18 18:39:02 -0800609 Mutex::Autolock _l(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -0800610 mMasterVolume = value;
611 mMasterVolumeSW = swmv;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800612 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossman4ff14ba2012-02-08 16:37:41 -0800613 mPlaybackThreads.valueAt(i)->setMasterVolume(swmv);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700614
615 return NO_ERROR;
616}
617
Glenn Kastenf78aee72012-01-04 11:00:47 -0800618status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619{
Eric Laurenta1884f92011-08-23 08:25:03 -0700620 status_t ret = initCheck();
621 if (ret != NO_ERROR) {
622 return ret;
623 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700624
625 // check calling permissions
626 if (!settingsAllowed()) {
627 return PERMISSION_DENIED;
628 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800629 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000630 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631 return BAD_VALUE;
632 }
633
634 { // scope for the lock
635 AutoMutex lock(mHardwareLock);
636 mHardwareStatus = AUDIO_HW_SET_MODE;
Dima Zavin799a70e2011-04-18 16:57:27 -0700637 ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638 mHardwareStatus = AUDIO_HW_IDLE;
639 }
640
641 if (NO_ERROR == ret) {
642 Mutex::Autolock _l(mLock);
643 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800644 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700645 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 }
647
648 return ret;
649}
650
651status_t AudioFlinger::setMicMute(bool state)
652{
Eric Laurenta1884f92011-08-23 08:25:03 -0700653 status_t ret = initCheck();
654 if (ret != NO_ERROR) {
655 return ret;
656 }
657
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658 // check calling permissions
659 if (!settingsAllowed()) {
660 return PERMISSION_DENIED;
661 }
662
663 AutoMutex lock(mHardwareLock);
664 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurenta1884f92011-08-23 08:25:03 -0700665 ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 mHardwareStatus = AUDIO_HW_IDLE;
667 return ret;
668}
669
670bool AudioFlinger::getMicMute() const
671{
Eric Laurenta1884f92011-08-23 08:25:03 -0700672 status_t ret = initCheck();
673 if (ret != NO_ERROR) {
674 return false;
675 }
676
Dima Zavinfce7a472011-04-19 22:30:36 -0700677 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800678 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Dima Zavin799a70e2011-04-18 16:57:27 -0700680 mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 mHardwareStatus = AUDIO_HW_IDLE;
682 return state;
683}
684
685status_t AudioFlinger::setMasterMute(bool muted)
686{
687 // check calling permissions
688 if (!settingsAllowed()) {
689 return PERMISSION_DENIED;
690 }
691
Eric Laurent93575202011-01-18 18:39:02 -0800692 Mutex::Autolock _l(mLock);
Glenn Kasten99e53b82012-01-19 08:59:58 -0800693 // This is an optimization, so PlaybackThread doesn't have to look at the one from AudioFlinger
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 mMasterMute = muted;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800695 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
697
698 return NO_ERROR;
699}
700
701float AudioFlinger::masterVolume() const
702{
Glenn Kasten98067102011-12-13 11:47:54 -0800703 Mutex::Autolock _l(mLock);
704 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705}
706
John Grossman4ff14ba2012-02-08 16:37:41 -0800707float AudioFlinger::masterVolumeSW() const
708{
709 Mutex::Autolock _l(mLock);
710 return masterVolumeSW_l();
711}
712
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713bool AudioFlinger::masterMute() const
714{
Glenn Kasten98067102011-12-13 11:47:54 -0800715 Mutex::Autolock _l(mLock);
716 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717}
718
John Grossman4ff14ba2012-02-08 16:37:41 -0800719float AudioFlinger::masterVolume_l() const
720{
721 if (MVS_FULL == mMasterVolumeSupportLvl) {
722 float ret_val;
723 AutoMutex lock(mHardwareLock);
724
725 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
726 assert(NULL != mPrimaryHardwareDev);
727 assert(NULL != mPrimaryHardwareDev->get_master_volume);
728
729 mPrimaryHardwareDev->get_master_volume(mPrimaryHardwareDev, &ret_val);
730 mHardwareStatus = AUDIO_HW_IDLE;
731 return ret_val;
732 }
733
734 return mMasterVolume;
735}
736
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800737status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
738 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700739{
740 // check calling permissions
741 if (!settingsAllowed()) {
742 return PERMISSION_DENIED;
743 }
744
Glenn Kasten263709e2012-01-06 08:40:01 -0800745 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000746 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747 return BAD_VALUE;
748 }
749
750 AutoMutex lock(mLock);
751 PlaybackThread *thread = NULL;
752 if (output) {
753 thread = checkPlaybackThread_l(output);
754 if (thread == NULL) {
755 return BAD_VALUE;
756 }
757 }
758
759 mStreamTypes[stream].volume = value;
760
761 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800762 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
764 }
765 } else {
766 thread->setStreamVolume(stream, value);
767 }
768
769 return NO_ERROR;
770}
771
Glenn Kastenfff6d712012-01-12 16:38:12 -0800772status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773{
774 // check calling permissions
775 if (!settingsAllowed()) {
776 return PERMISSION_DENIED;
777 }
778
Glenn Kasten263709e2012-01-06 08:40:01 -0800779 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700780 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000781 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782 return BAD_VALUE;
783 }
784
Eric Laurent93575202011-01-18 18:39:02 -0800785 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700786 mStreamTypes[stream].mute = muted;
787 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
788 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
789
790 return NO_ERROR;
791}
792
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800793float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794{
Glenn Kasten263709e2012-01-06 08:40:01 -0800795 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 return 0.0f;
797 }
798
799 AutoMutex lock(mLock);
800 float volume;
801 if (output) {
802 PlaybackThread *thread = checkPlaybackThread_l(output);
803 if (thread == NULL) {
804 return 0.0f;
805 }
806 volume = thread->streamVolume(stream);
807 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800808 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809 }
810
811 return volume;
812}
813
Glenn Kastenfff6d712012-01-12 16:38:12 -0800814bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815{
Glenn Kasten263709e2012-01-06 08:40:01 -0800816 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817 return true;
818 }
819
Glenn Kasten6637baa2012-01-09 09:40:36 -0800820 AutoMutex lock(mLock);
821 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700822}
823
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800824status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825{
826 status_t result;
827
Glenn Kasten23d82a92012-02-03 11:10:00 -0800828 ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling pid %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700829 ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid());
830 // check calling permissions
831 if (!settingsAllowed()) {
832 return PERMISSION_DENIED;
833 }
834
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835 // ioHandle == 0 means the parameters are global to the audio hardware interface
836 if (ioHandle == 0) {
837 AutoMutex lock(mHardwareLock);
838 mHardwareStatus = AUDIO_SET_PARAMETER;
Dima Zavin799a70e2011-04-18 16:57:27 -0700839 status_t final_result = NO_ERROR;
840 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
841 audio_hw_device_t *dev = mAudioHwDevs[i];
842 result = dev->set_parameters(dev, keyValuePairs.string());
843 final_result = result ?: final_result;
844 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700846 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
847 AudioParameter param = AudioParameter(keyValuePairs);
848 String8 value;
849 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
850 Mutex::Autolock _l(mLock);
Eric Laurentbee53372011-08-29 12:42:48 -0700851 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
852 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700853 for (size_t i = 0; i < mRecordThreads.size(); i++) {
854 sp<RecordThread> thread = mRecordThreads.valueAt(i);
855 RecordThread::RecordTrack *track = thread->track();
856 if (track != NULL) {
857 audio_devices_t device = (audio_devices_t)(
858 thread->device() & AUDIO_DEVICE_IN_ALL);
Eric Laurentbee53372011-08-29 12:42:48 -0700859 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700860 thread->setEffectSuspended(FX_IID_AEC,
861 suspend,
862 track->sessionId());
863 thread->setEffectSuspended(FX_IID_NS,
864 suspend,
865 track->sessionId());
866 }
867 }
Eric Laurentbee53372011-08-29 12:42:48 -0700868 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700869 }
870 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700871 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 }
873
874 // hold a strong ref on thread in case closeOutput() or closeInput() is called
875 // and the thread is exited once the lock is released
876 sp<ThreadBase> thread;
877 {
878 Mutex::Autolock _l(mLock);
879 thread = checkPlaybackThread_l(ioHandle);
880 if (thread == NULL) {
881 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800882 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700883 // indicate output device change to all input threads for pre processing
884 AudioParameter param = AudioParameter(keyValuePairs);
885 int value;
886 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
887 for (size_t i = 0; i < mRecordThreads.size(); i++) {
888 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
889 }
890 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891 }
892 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800893 if (thread != 0) {
894 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895 }
896 return BAD_VALUE;
897}
898
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800899String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900{
Glenn Kasten23d82a92012-02-03 11:10:00 -0800901// ALOGV("getParameters() io %d, keys %s, tid %d, calling pid %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902// ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid());
903
904 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700905 String8 out_s8;
906
Dima Zavin799a70e2011-04-18 16:57:27 -0700907 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
908 audio_hw_device_t *dev = mAudioHwDevs[i];
909 char *s = dev->get_parameters(dev, keys.string());
John Grossmanef7740b2012-02-09 11:28:36 -0800910 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700911 free(s);
912 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700913 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914 }
915
916 Mutex::Autolock _l(mLock);
917
918 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
919 if (playbackThread != NULL) {
920 return playbackThread->getParameters(keys);
921 }
922 RecordThread *recordThread = checkRecordThread_l(ioHandle);
923 if (recordThread != NULL) {
924 return recordThread->getParameters(keys);
925 }
926 return String8("");
927}
928
Glenn Kastenf587ba52012-01-26 16:25:10 -0800929size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, int channelCount) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930{
Eric Laurenta1884f92011-08-23 08:25:03 -0700931 status_t ret = initCheck();
932 if (ret != NO_ERROR) {
933 return 0;
934 }
935
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800936 AutoMutex lock(mHardwareLock);
937 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
938 size_t size = mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, sampleRate, format, channelCount);
939 mHardwareStatus = AUDIO_HW_IDLE;
940 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941}
942
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800943unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944{
945 if (ioHandle == 0) {
946 return 0;
947 }
948
949 Mutex::Autolock _l(mLock);
950
951 RecordThread *recordThread = checkRecordThread_l(ioHandle);
952 if (recordThread != NULL) {
953 return recordThread->getInputFramesLost();
954 }
955 return 0;
956}
957
958status_t AudioFlinger::setVoiceVolume(float value)
959{
Eric Laurenta1884f92011-08-23 08:25:03 -0700960 status_t ret = initCheck();
961 if (ret != NO_ERROR) {
962 return ret;
963 }
964
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965 // check calling permissions
966 if (!settingsAllowed()) {
967 return PERMISSION_DENIED;
968 }
969
970 AutoMutex lock(mHardwareLock);
971 mHardwareStatus = AUDIO_SET_VOICE_VOLUME;
Eric Laurenta1884f92011-08-23 08:25:03 -0700972 ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973 mHardwareStatus = AUDIO_HW_IDLE;
974
975 return ret;
976}
977
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800978status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
979 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700980{
981 status_t status;
982
983 Mutex::Autolock _l(mLock);
984
985 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
986 if (playbackThread != NULL) {
987 return playbackThread->getRenderPosition(halFrames, dspFrames);
988 }
989
990 return BAD_VALUE;
991}
992
993void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
994{
995
996 Mutex::Autolock _l(mLock);
997
Glenn Kastenbb001922012-02-03 11:10:26 -0800998 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999 if (mNotificationClients.indexOfKey(pid) < 0) {
1000 sp<NotificationClient> notificationClient = new NotificationClient(this,
1001 client,
1002 pid);
Steve Block3856b092011-10-20 11:56:00 +01001003 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004
1005 mNotificationClients.add(pid, notificationClient);
1006
1007 sp<IBinder> binder = client->asBinder();
1008 binder->linkToDeath(notificationClient);
1009
1010 // the config change is always sent from playback or record threads to avoid deadlock
1011 // with AudioSystem::gLock
1012 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1013 mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED);
1014 }
1015
1016 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1017 mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED);
1018 }
1019 }
1020}
1021
1022void AudioFlinger::removeNotificationClient(pid_t pid)
1023{
1024 Mutex::Autolock _l(mLock);
1025
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001026 ssize_t index = mNotificationClients.indexOfKey(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001027 if (index >= 0) {
1028 sp <NotificationClient> client = mNotificationClients.valueFor(pid);
Steve Block3856b092011-10-20 11:56:00 +01001029 ALOGV("removeNotificationClient() %p, pid %d", client.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001030 mNotificationClients.removeItem(pid);
1031 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001032
Steve Block3856b092011-10-20 11:56:00 +01001033 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001034 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001035 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001036 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001037 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Steve Block3856b092011-10-20 11:56:00 +01001038 ALOGV(" pid %d @ %d", ref->pid, i);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001039 if (ref->pid == pid) {
Steve Block3856b092011-10-20 11:56:00 +01001040 ALOGV(" removing entry for pid %d session %d", pid, ref->sessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001041 mAudioSessionRefs.removeAt(i);
1042 delete ref;
1043 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001044 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001045 } else {
1046 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001047 }
1048 }
1049 if (removed) {
1050 purgeStaleEffects_l();
1051 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052}
1053
1054// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001055void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001056{
1057 size_t size = mNotificationClients.size();
1058 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001059 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1060 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001061 }
1062}
1063
1064// removeClient_l() must be called with AudioFlinger::mLock held
1065void AudioFlinger::removeClient_l(pid_t pid)
1066{
Steve Block3856b092011-10-20 11:56:00 +01001067 ALOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068 mClients.removeItem(pid);
1069}
1070
1071
1072// ----------------------------------------------------------------------------
1073
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001074AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
1075 uint32_t device, type_t type)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001076 : Thread(false),
Glenn Kasten23bb8be2012-01-26 10:38:26 -08001077 mType(type),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001078 mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0),
1079 // mChannelMask
1080 mChannelCount(0),
1081 mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID),
1082 mParamStatus(NO_ERROR),
Glenn Kastenb28686f2012-01-06 08:39:38 -08001083 mStandby(false), mId(id),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001084 mDevice(device),
1085 mDeathRecipient(new PMDeathRecipient(this))
Mathias Agopian65ab4712010-07-14 17:59:35 -07001086{
1087}
1088
1089AudioFlinger::ThreadBase::~ThreadBase()
1090{
1091 mParamCond.broadcast();
Eric Laurentfeb0db62011-07-22 09:04:31 -07001092 // do not lock the mutex in destructor
1093 releaseWakeLock_l();
Eric Laurent9d18ec52011-09-27 12:07:15 -07001094 if (mPowerManager != 0) {
1095 sp<IBinder> binder = mPowerManager->asBinder();
1096 binder->unlinkToDeath(mDeathRecipient);
1097 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001098}
1099
1100void AudioFlinger::ThreadBase::exit()
1101{
Steve Block3856b092011-10-20 11:56:00 +01001102 ALOGV("ThreadBase::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001103 {
Glenn Kastenb28686f2012-01-06 08:39:38 -08001104 // This lock prevents the following race in thread (uniprocessor for illustration):
1105 // if (!exitPending()) {
1106 // // context switch from here to exit()
1107 // // exit() calls requestExit(), what exitPending() observes
1108 // // exit() calls signal(), which is dropped since no waiters
1109 // // context switch back from exit() to here
1110 // mWaitWorkCV.wait(...);
1111 // // now thread is hung
1112 // }
Glenn Kastena7d8d6f2012-01-05 15:41:56 -08001113 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001114 requestExit();
1115 mWaitWorkCV.signal();
1116 }
Glenn Kastenb28686f2012-01-06 08:39:38 -08001117 // When Thread::requestExitAndWait is made virtual and this method is renamed to
1118 // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();"
Mathias Agopian65ab4712010-07-14 17:59:35 -07001119 requestExitAndWait();
1120}
1121
Mathias Agopian65ab4712010-07-14 17:59:35 -07001122status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs)
1123{
1124 status_t status;
1125
Steve Block3856b092011-10-20 11:56:00 +01001126 ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001127 Mutex::Autolock _l(mLock);
1128
1129 mNewParameters.add(keyValuePairs);
1130 mWaitWorkCV.signal();
1131 // wait condition with timeout in case the thread loop has exited
1132 // before the request could be processed
Glenn Kasten7dede872011-12-13 11:04:14 -08001133 if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134 status = mParamStatus;
1135 mWaitWorkCV.signal();
1136 } else {
1137 status = TIMED_OUT;
1138 }
1139 return status;
1140}
1141
1142void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param)
1143{
1144 Mutex::Autolock _l(mLock);
1145 sendConfigEvent_l(event, param);
1146}
1147
1148// sendConfigEvent_l() must be called with ThreadBase::mLock held
1149void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param)
1150{
Glenn Kastenf3990f22011-12-13 11:50:00 -08001151 ConfigEvent configEvent;
1152 configEvent.mEvent = event;
1153 configEvent.mParam = param;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154 mConfigEvents.add(configEvent);
Steve Block3856b092011-10-20 11:56:00 +01001155 ALOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001156 mWaitWorkCV.signal();
1157}
1158
1159void AudioFlinger::ThreadBase::processConfigEvents()
1160{
1161 mLock.lock();
1162 while(!mConfigEvents.isEmpty()) {
Steve Block3856b092011-10-20 11:56:00 +01001163 ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size());
Glenn Kastenf3990f22011-12-13 11:50:00 -08001164 ConfigEvent configEvent = mConfigEvents[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001165 mConfigEvents.removeAt(0);
1166 // release mLock before locking AudioFlinger mLock: lock order is always
1167 // AudioFlinger then ThreadBase to avoid cross deadlock
1168 mLock.unlock();
1169 mAudioFlinger->mLock.lock();
Glenn Kastenf3990f22011-12-13 11:50:00 -08001170 audioConfigChanged_l(configEvent.mEvent, configEvent.mParam);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001171 mAudioFlinger->mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001172 mLock.lock();
1173 }
1174 mLock.unlock();
1175}
1176
1177status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args)
1178{
1179 const size_t SIZE = 256;
1180 char buffer[SIZE];
1181 String8 result;
1182
1183 bool locked = tryLock(mLock);
1184 if (!locked) {
1185 snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this);
1186 write(fd, buffer, strlen(buffer));
1187 }
1188
1189 snprintf(buffer, SIZE, "standby: %d\n", mStandby);
1190 result.append(buffer);
1191 snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate);
1192 result.append(buffer);
1193 snprintf(buffer, SIZE, "Frame count: %d\n", mFrameCount);
1194 result.append(buffer);
1195 snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount);
1196 result.append(buffer);
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001197 snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask);
1198 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199 snprintf(buffer, SIZE, "Format: %d\n", mFormat);
1200 result.append(buffer);
Glenn Kastenb9980652012-01-11 09:48:27 -08001201 snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001202 result.append(buffer);
1203
1204 snprintf(buffer, SIZE, "\nPending setParameters commands: \n");
1205 result.append(buffer);
1206 result.append(" Index Command");
1207 for (size_t i = 0; i < mNewParameters.size(); ++i) {
1208 snprintf(buffer, SIZE, "\n %02d ", i);
1209 result.append(buffer);
1210 result.append(mNewParameters[i]);
1211 }
1212
1213 snprintf(buffer, SIZE, "\n\nPending config events: \n");
1214 result.append(buffer);
1215 snprintf(buffer, SIZE, " Index event param\n");
1216 result.append(buffer);
1217 for (size_t i = 0; i < mConfigEvents.size(); i++) {
Glenn Kastenf3990f22011-12-13 11:50:00 -08001218 snprintf(buffer, SIZE, " %02d %02d %d\n", i, mConfigEvents[i].mEvent, mConfigEvents[i].mParam);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219 result.append(buffer);
1220 }
1221 result.append("\n");
1222
1223 write(fd, result.string(), result.size());
1224
1225 if (locked) {
1226 mLock.unlock();
1227 }
1228 return NO_ERROR;
1229}
1230
Eric Laurent1d2bff02011-07-24 17:49:51 -07001231status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args)
1232{
1233 const size_t SIZE = 256;
1234 char buffer[SIZE];
1235 String8 result;
1236
1237 snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size());
1238 write(fd, buffer, strlen(buffer));
1239
1240 for (size_t i = 0; i < mEffectChains.size(); ++i) {
1241 sp<EffectChain> chain = mEffectChains[i];
1242 if (chain != 0) {
1243 chain->dump(fd, args);
1244 }
1245 }
1246 return NO_ERROR;
1247}
1248
Eric Laurentfeb0db62011-07-22 09:04:31 -07001249void AudioFlinger::ThreadBase::acquireWakeLock()
1250{
1251 Mutex::Autolock _l(mLock);
1252 acquireWakeLock_l();
1253}
1254
1255void AudioFlinger::ThreadBase::acquireWakeLock_l()
1256{
1257 if (mPowerManager == 0) {
1258 // use checkService() to avoid blocking if power service is not up yet
1259 sp<IBinder> binder =
1260 defaultServiceManager()->checkService(String16("power"));
1261 if (binder == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001262 ALOGW("Thread %s cannot connect to the power manager service", mName);
Eric Laurentfeb0db62011-07-22 09:04:31 -07001263 } else {
1264 mPowerManager = interface_cast<IPowerManager>(binder);
1265 binder->linkToDeath(mDeathRecipient);
1266 }
1267 }
1268 if (mPowerManager != 0) {
1269 sp<IBinder> binder = new BBinder();
1270 status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
1271 binder,
1272 String16(mName));
1273 if (status == NO_ERROR) {
1274 mWakeLockToken = binder;
1275 }
Steve Block3856b092011-10-20 11:56:00 +01001276 ALOGV("acquireWakeLock_l() %s status %d", mName, status);
Eric Laurentfeb0db62011-07-22 09:04:31 -07001277 }
1278}
1279
1280void AudioFlinger::ThreadBase::releaseWakeLock()
1281{
1282 Mutex::Autolock _l(mLock);
Eric Laurent6dbe8832011-07-28 13:59:02 -07001283 releaseWakeLock_l();
Eric Laurentfeb0db62011-07-22 09:04:31 -07001284}
1285
1286void AudioFlinger::ThreadBase::releaseWakeLock_l()
1287{
1288 if (mWakeLockToken != 0) {
Steve Block3856b092011-10-20 11:56:00 +01001289 ALOGV("releaseWakeLock_l() %s", mName);
Eric Laurentfeb0db62011-07-22 09:04:31 -07001290 if (mPowerManager != 0) {
1291 mPowerManager->releaseWakeLock(mWakeLockToken, 0);
1292 }
1293 mWakeLockToken.clear();
1294 }
1295}
1296
1297void AudioFlinger::ThreadBase::clearPowerManager()
1298{
1299 Mutex::Autolock _l(mLock);
1300 releaseWakeLock_l();
1301 mPowerManager.clear();
1302}
1303
1304void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who)
1305{
1306 sp<ThreadBase> thread = mThread.promote();
1307 if (thread != 0) {
1308 thread->clearPowerManager();
1309 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001310 ALOGW("power manager service died !!!");
Eric Laurentfeb0db62011-07-22 09:04:31 -07001311}
Eric Laurent1d2bff02011-07-24 17:49:51 -07001312
Eric Laurent59255e42011-07-27 19:49:51 -07001313void AudioFlinger::ThreadBase::setEffectSuspended(
1314 const effect_uuid_t *type, bool suspend, int sessionId)
1315{
1316 Mutex::Autolock _l(mLock);
1317 setEffectSuspended_l(type, suspend, sessionId);
1318}
1319
1320void AudioFlinger::ThreadBase::setEffectSuspended_l(
1321 const effect_uuid_t *type, bool suspend, int sessionId)
1322{
Glenn Kasten090f0192012-01-30 13:00:02 -08001323 sp<EffectChain> chain = getEffectChain_l(sessionId);
Eric Laurent59255e42011-07-27 19:49:51 -07001324 if (chain != 0) {
1325 if (type != NULL) {
1326 chain->setEffectSuspended_l(type, suspend);
1327 } else {
1328 chain->setEffectSuspendedAll_l(suspend);
1329 }
1330 }
1331
1332 updateSuspendedSessions_l(type, suspend, sessionId);
1333}
1334
1335void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain)
1336{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001337 ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId());
Eric Laurent59255e42011-07-27 19:49:51 -07001338 if (index < 0) {
1339 return;
1340 }
1341
1342 KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects =
1343 mSuspendedSessions.editValueAt(index);
1344
1345 for (size_t i = 0; i < sessionEffects.size(); i++) {
1346 sp <SuspendedSessionDesc> desc = sessionEffects.valueAt(i);
1347 for (int j = 0; j < desc->mRefCount; j++) {
1348 if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) {
1349 chain->setEffectSuspendedAll_l(true);
1350 } else {
Steve Block3856b092011-10-20 11:56:00 +01001351 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x",
Eric Laurent59255e42011-07-27 19:49:51 -07001352 desc->mType.timeLow);
1353 chain->setEffectSuspended_l(&desc->mType, true);
1354 }
1355 }
1356 }
1357}
1358
Eric Laurent59255e42011-07-27 19:49:51 -07001359void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type,
1360 bool suspend,
1361 int sessionId)
1362{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001363 ssize_t index = mSuspendedSessions.indexOfKey(sessionId);
Eric Laurent59255e42011-07-27 19:49:51 -07001364
1365 KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects;
1366
1367 if (suspend) {
1368 if (index >= 0) {
1369 sessionEffects = mSuspendedSessions.editValueAt(index);
1370 } else {
1371 mSuspendedSessions.add(sessionId, sessionEffects);
1372 }
1373 } else {
1374 if (index < 0) {
1375 return;
1376 }
1377 sessionEffects = mSuspendedSessions.editValueAt(index);
1378 }
1379
1380
1381 int key = EffectChain::kKeyForSuspendAll;
1382 if (type != NULL) {
1383 key = type->timeLow;
1384 }
1385 index = sessionEffects.indexOfKey(key);
1386
1387 sp <SuspendedSessionDesc> desc;
1388 if (suspend) {
1389 if (index >= 0) {
1390 desc = sessionEffects.valueAt(index);
1391 } else {
1392 desc = new SuspendedSessionDesc();
1393 if (type != NULL) {
1394 memcpy(&desc->mType, type, sizeof(effect_uuid_t));
1395 }
1396 sessionEffects.add(key, desc);
Steve Block3856b092011-10-20 11:56:00 +01001397 ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key);
Eric Laurent59255e42011-07-27 19:49:51 -07001398 }
1399 desc->mRefCount++;
1400 } else {
1401 if (index < 0) {
1402 return;
1403 }
1404 desc = sessionEffects.valueAt(index);
1405 if (--desc->mRefCount == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001406 ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key);
Eric Laurent59255e42011-07-27 19:49:51 -07001407 sessionEffects.removeItemsAt(index);
1408 if (sessionEffects.isEmpty()) {
Steve Block3856b092011-10-20 11:56:00 +01001409 ALOGV("updateSuspendedSessions_l() restore removing session %d",
Eric Laurent59255e42011-07-27 19:49:51 -07001410 sessionId);
1411 mSuspendedSessions.removeItem(sessionId);
1412 }
1413 }
1414 }
1415 if (!sessionEffects.isEmpty()) {
1416 mSuspendedSessions.replaceValueFor(sessionId, sessionEffects);
1417 }
1418}
1419
1420void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1421 bool enabled,
1422 int sessionId)
1423{
1424 Mutex::Autolock _l(mLock);
Eric Laurenta85a74a2011-10-19 11:44:54 -07001425 checkSuspendOnEffectEnabled_l(effect, enabled, sessionId);
1426}
Eric Laurent59255e42011-07-27 19:49:51 -07001427
Eric Laurenta85a74a2011-10-19 11:44:54 -07001428void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
1429 bool enabled,
1430 int sessionId)
1431{
Eric Laurentdb7c0792011-08-10 10:37:50 -07001432 if (mType != RECORD) {
1433 // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
1434 // another session. This gives the priority to well behaved effect control panels
1435 // and applications not using global effects.
1436 if (sessionId != AUDIO_SESSION_OUTPUT_MIX) {
1437 setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
1438 }
1439 }
Eric Laurent59255e42011-07-27 19:49:51 -07001440
1441 sp<EffectChain> chain = getEffectChain_l(sessionId);
1442 if (chain != 0) {
1443 chain->checkSuspendOnEffectEnabled(effect, enabled);
1444 }
1445}
1446
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447// ----------------------------------------------------------------------------
1448
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001449AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger,
1450 AudioStreamOut* output,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001451 audio_io_handle_t id,
Glenn Kasten23bb8be2012-01-26 10:38:26 -08001452 uint32_t device,
1453 type_t type)
1454 : ThreadBase(audioFlinger, id, device, type),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001455 mMixBuffer(NULL), mSuspended(0), mBytesWritten(0),
1456 // Assumes constructor is called by AudioFlinger with it's mLock held,
1457 // but it would be safer to explicitly pass initial masterMute as parameter
1458 mMasterMute(audioFlinger->masterMute_l()),
1459 // mStreamTypes[] initialized in constructor body
1460 mOutput(output),
1461 // Assumes constructor is called by AudioFlinger with it's mLock held,
1462 // but it would be safer to explicitly pass initial masterVolume as parameter
John Grossman4ff14ba2012-02-08 16:37:41 -08001463 mMasterVolume(audioFlinger->masterVolumeSW_l()),
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001464 mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001465{
Eric Laurentfeb0db62011-07-22 09:04:31 -07001466 snprintf(mName, kNameLength, "AudioOut_%d", id);
1467
Mathias Agopian65ab4712010-07-14 17:59:35 -07001468 readOutputParameters();
1469
Glenn Kasten263709e2012-01-06 08:40:01 -08001470 // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor
Glenn Kastenfff6d712012-01-12 16:38:12 -08001471 // There is no AUDIO_STREAM_MIN, and ++ operator does not compile
1472 for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT;
1473 stream = (audio_stream_type_t) (stream + 1)) {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001474 mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream);
1475 mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream);
Glenn Kasten263709e2012-01-06 08:40:01 -08001476 // initialized by stream_type_t default constructor
1477 // mStreamTypes[stream].valid = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001478 }
Glenn Kasten6637baa2012-01-09 09:40:36 -08001479 // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here,
1480 // because mAudioFlinger doesn't have one to copy from
Mathias Agopian65ab4712010-07-14 17:59:35 -07001481}
1482
1483AudioFlinger::PlaybackThread::~PlaybackThread()
1484{
1485 delete [] mMixBuffer;
1486}
1487
1488status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args)
1489{
1490 dumpInternals(fd, args);
1491 dumpTracks(fd, args);
1492 dumpEffectChains(fd, args);
1493 return NO_ERROR;
1494}
1495
1496status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args)
1497{
1498 const size_t SIZE = 256;
1499 char buffer[SIZE];
1500 String8 result;
1501
1502 snprintf(buffer, SIZE, "Output thread %p tracks\n", this);
1503 result.append(buffer);
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001504 result.append(" Name Clien Typ Fmt Chn mask Session Buf S M F SRate LeftV RighV Serv User Main buf Aux Buf\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001505 for (size_t i = 0; i < mTracks.size(); ++i) {
1506 sp<Track> track = mTracks[i];
1507 if (track != 0) {
1508 track->dump(buffer, SIZE);
1509 result.append(buffer);
1510 }
1511 }
1512
1513 snprintf(buffer, SIZE, "Output thread %p active tracks\n", this);
1514 result.append(buffer);
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001515 result.append(" Name Clien Typ Fmt Chn mask Session Buf S M F SRate LeftV RighV Serv User Main buf Aux Buf\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516 for (size_t i = 0; i < mActiveTracks.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -08001517 sp<Track> track = mActiveTracks[i].promote();
1518 if (track != 0) {
1519 track->dump(buffer, SIZE);
1520 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521 }
1522 }
1523 write(fd, result.string(), result.size());
1524 return NO_ERROR;
1525}
1526
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args)
1528{
1529 const size_t SIZE = 256;
1530 char buffer[SIZE];
1531 String8 result;
1532
1533 snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this);
1534 result.append(buffer);
1535 snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime));
1536 result.append(buffer);
1537 snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites);
1538 result.append(buffer);
1539 snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites);
1540 result.append(buffer);
1541 snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite);
1542 result.append(buffer);
1543 snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended);
1544 result.append(buffer);
1545 snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer);
1546 result.append(buffer);
1547 write(fd, result.string(), result.size());
1548
1549 dumpBase(fd, args);
1550
1551 return NO_ERROR;
1552}
1553
1554// Thread virtuals
1555status_t AudioFlinger::PlaybackThread::readyToRun()
1556{
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001557 status_t status = initCheck();
1558 if (status == NO_ERROR) {
Steve Blockdf64d152012-01-04 20:05:49 +00001559 ALOGI("AudioFlinger's thread %p ready to run", this);
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001560 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001561 ALOGE("No working audio driver found.");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001563 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001564}
1565
1566void AudioFlinger::PlaybackThread::onFirstRef()
1567{
Eric Laurentfeb0db62011-07-22 09:04:31 -07001568 run(mName, ANDROID_PRIORITY_URGENT_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569}
1570
1571// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
1572sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
1573 const sp<AudioFlinger::Client>& client,
Glenn Kastenfff6d712012-01-12 16:38:12 -08001574 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001575 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001576 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001577 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001578 int frameCount,
1579 const sp<IMemory>& sharedBuffer,
1580 int sessionId,
John Grossman4ff14ba2012-02-08 16:37:41 -08001581 bool isTimed,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001582 status_t *status)
1583{
1584 sp<Track> track;
1585 status_t lStatus;
1586
1587 if (mType == DIRECT) {
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001588 if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) {
1589 if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
Steve Block29357bc2012-01-06 19:20:56 +00001590 ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x \""
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001591 "for output %p with format %d",
1592 sampleRate, format, channelMask, mOutput, mFormat);
1593 lStatus = BAD_VALUE;
1594 goto Exit;
1595 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001596 }
1597 } else {
1598 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
1599 if (sampleRate > mSampleRate*2) {
Steve Block29357bc2012-01-06 19:20:56 +00001600 ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001601 lStatus = BAD_VALUE;
1602 goto Exit;
1603 }
1604 }
1605
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001606 lStatus = initCheck();
1607 if (lStatus != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +00001608 ALOGE("Audio driver not initialized.");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609 goto Exit;
1610 }
1611
1612 { // scope for mLock
1613 Mutex::Autolock _l(mLock);
Eric Laurentde070132010-07-13 04:45:46 -07001614
1615 // all tracks in same audio session must share the same routing strategy otherwise
1616 // conflicts will happen when tracks are moved from one output to another by audio policy
1617 // manager
Glenn Kasten02bbd202012-02-08 12:35:35 -08001618 uint32_t strategy = AudioSystem::getStrategyForStream(streamType);
Eric Laurentde070132010-07-13 04:45:46 -07001619 for (size_t i = 0; i < mTracks.size(); ++i) {
1620 sp<Track> t = mTracks[i];
1621 if (t != 0) {
Glenn Kasten02bbd202012-02-08 12:35:35 -08001622 uint32_t actual = AudioSystem::getStrategyForStream(t->streamType());
Glenn Kastend8796012011-10-28 10:31:42 -07001623 if (sessionId == t->sessionId() && strategy != actual) {
Steve Block29357bc2012-01-06 19:20:56 +00001624 ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",
Glenn Kastend8796012011-10-28 10:31:42 -07001625 strategy, actual);
Eric Laurentde070132010-07-13 04:45:46 -07001626 lStatus = BAD_VALUE;
1627 goto Exit;
1628 }
1629 }
1630 }
1631
John Grossman4ff14ba2012-02-08 16:37:41 -08001632 if (!isTimed) {
1633 track = new Track(this, client, streamType, sampleRate, format,
1634 channelMask, frameCount, sharedBuffer, sessionId);
1635 } else {
1636 track = TimedTrack::create(this, client, streamType, sampleRate, format,
1637 channelMask, frameCount, sharedBuffer, sessionId);
1638 }
1639 if (track == NULL || track->getCblk() == NULL || track->name() < 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001640 lStatus = NO_MEMORY;
1641 goto Exit;
1642 }
1643 mTracks.add(track);
1644
1645 sp<EffectChain> chain = getEffectChain_l(sessionId);
1646 if (chain != 0) {
Steve Block3856b092011-10-20 11:56:00 +01001647 ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648 track->setMainBuffer(chain->inBuffer());
Glenn Kasten02bbd202012-02-08 12:35:35 -08001649 chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType()));
Eric Laurentb469b942011-05-09 12:09:06 -07001650 chain->incTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001651 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001652
1653 // invalidate track immediately if the stream type was moved to another thread since
1654 // createTrack() was called by the client process.
1655 if (!mStreamTypes[streamType].valid) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001656 ALOGW("createTrack_l() on thread %p: invalidating track on stream %d",
Eric Laurent9f6530f2011-08-30 10:18:54 -07001657 this, streamType);
1658 android_atomic_or(CBLK_INVALID_ON, &track->mCblk->flags);
1659 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001660 }
1661 lStatus = NO_ERROR;
1662
1663Exit:
1664 if(status) {
1665 *status = lStatus;
1666 }
1667 return track;
1668}
1669
1670uint32_t AudioFlinger::PlaybackThread::latency() const
1671{
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001672 Mutex::Autolock _l(mLock);
1673 if (initCheck() == NO_ERROR) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001674 return mOutput->stream->get_latency(mOutput->stream);
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001675 } else {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001676 return 0;
1677 }
1678}
1679
Glenn Kasten6637baa2012-01-09 09:40:36 -08001680void AudioFlinger::PlaybackThread::setMasterVolume(float value)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001681{
Glenn Kasten6637baa2012-01-09 09:40:36 -08001682 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001683 mMasterVolume = value;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001684}
1685
Glenn Kasten6637baa2012-01-09 09:40:36 -08001686void AudioFlinger::PlaybackThread::setMasterMute(bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001687{
Glenn Kasten6637baa2012-01-09 09:40:36 -08001688 Mutex::Autolock _l(mLock);
1689 setMasterMute_l(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001690}
1691
Glenn Kasten6637baa2012-01-09 09:40:36 -08001692void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001693{
Glenn Kasten6637baa2012-01-09 09:40:36 -08001694 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001695 mStreamTypes[stream].volume = value;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001696}
1697
Glenn Kasten6637baa2012-01-09 09:40:36 -08001698void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001699{
Glenn Kasten6637baa2012-01-09 09:40:36 -08001700 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001701 mStreamTypes[stream].mute = muted;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001702}
1703
Glenn Kastenfff6d712012-01-12 16:38:12 -08001704float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001705{
Glenn Kasten6637baa2012-01-09 09:40:36 -08001706 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001707 return mStreamTypes[stream].volume;
1708}
1709
Mathias Agopian65ab4712010-07-14 17:59:35 -07001710// addTrack_l() must be called with ThreadBase::mLock held
1711status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track)
1712{
1713 status_t status = ALREADY_EXISTS;
1714
1715 // set retry count for buffer fill
1716 track->mRetryCount = kMaxTrackStartupRetries;
1717 if (mActiveTracks.indexOf(track) < 0) {
1718 // the track is newly added, make sure it fills up all its
1719 // buffers before playing. This is to ensure the client will
1720 // effectively get the latency it requested.
1721 track->mFillingUpStatus = Track::FS_FILLING;
1722 track->mResetDone = false;
1723 mActiveTracks.add(track);
1724 if (track->mainBuffer() != mMixBuffer) {
1725 sp<EffectChain> chain = getEffectChain_l(track->sessionId());
1726 if (chain != 0) {
Steve Block3856b092011-10-20 11:56:00 +01001727 ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId());
Eric Laurentb469b942011-05-09 12:09:06 -07001728 chain->incActiveTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001729 }
1730 }
1731
1732 status = NO_ERROR;
1733 }
1734
Steve Block3856b092011-10-20 11:56:00 +01001735 ALOGV("mWaitWorkCV.broadcast");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001736 mWaitWorkCV.broadcast();
1737
1738 return status;
1739}
1740
1741// destroyTrack_l() must be called with ThreadBase::mLock held
1742void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track)
1743{
1744 track->mState = TrackBase::TERMINATED;
1745 if (mActiveTracks.indexOf(track) < 0) {
Eric Laurentb469b942011-05-09 12:09:06 -07001746 removeTrack_l(track);
1747 }
1748}
1749
1750void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track)
1751{
1752 mTracks.remove(track);
1753 deleteTrackName_l(track->name());
1754 sp<EffectChain> chain = getEffectChain_l(track->sessionId());
1755 if (chain != 0) {
1756 chain->decTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001757 }
1758}
1759
1760String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
1761{
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001762 String8 out_s8 = String8("");
Dima Zavinfce7a472011-04-19 22:30:36 -07001763 char *s;
1764
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001765 Mutex::Autolock _l(mLock);
1766 if (initCheck() != NO_ERROR) {
1767 return out_s8;
1768 }
1769
Dima Zavin799a70e2011-04-18 16:57:27 -07001770 s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string());
Dima Zavinfce7a472011-04-19 22:30:36 -07001771 out_s8 = String8(s);
1772 free(s);
1773 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001774}
1775
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001776// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001777void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) {
1778 AudioSystem::OutputDescriptor desc;
Glenn Kastena0d68332012-01-27 16:47:15 -08001779 void *param2 = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001780
Steve Block3856b092011-10-20 11:56:00 +01001781 ALOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001782
1783 switch (event) {
1784 case AudioSystem::OUTPUT_OPENED:
1785 case AudioSystem::OUTPUT_CONFIG_CHANGED:
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001786 desc.channels = mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001787 desc.samplingRate = mSampleRate;
1788 desc.format = mFormat;
1789 desc.frameCount = mFrameCount;
1790 desc.latency = latency();
1791 param2 = &desc;
1792 break;
1793
1794 case AudioSystem::STREAM_CONFIG_CHANGED:
1795 param2 = &param;
1796 case AudioSystem::OUTPUT_CLOSED:
1797 default:
1798 break;
1799 }
1800 mAudioFlinger->audioConfigChanged_l(event, mId, param2);
1801}
1802
1803void AudioFlinger::PlaybackThread::readOutputParameters()
1804{
Dima Zavin799a70e2011-04-18 16:57:27 -07001805 mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common);
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001806 mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common);
1807 mChannelCount = (uint16_t)popcount(mChannelMask);
Dima Zavin799a70e2011-04-18 16:57:27 -07001808 mFormat = mOutput->stream->common.get_format(&mOutput->stream->common);
Glenn Kastenb9980652012-01-11 09:48:27 -08001809 mFrameSize = audio_stream_frame_size(&mOutput->stream->common);
Dima Zavin799a70e2011-04-18 16:57:27 -07001810 mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001811
1812 // FIXME - Current mixer implementation only supports stereo output: Always
1813 // Allocate a stereo buffer even if HW output is mono.
Glenn Kastene9dd0172012-01-27 18:08:45 -08001814 delete[] mMixBuffer;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001815 mMixBuffer = new int16_t[mFrameCount * 2];
1816 memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t));
1817
Eric Laurentde070132010-07-13 04:45:46 -07001818 // force reconfiguration of effect chains and engines to take new buffer size and audio
1819 // parameters into account
1820 // Note that mLock is not held when readOutputParameters() is called from the constructor
1821 // but in this case nothing is done below as no audio sessions have effect yet so it doesn't
1822 // matter.
1823 // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains
1824 Vector< sp<EffectChain> > effectChains = mEffectChains;
1825 for (size_t i = 0; i < effectChains.size(); i ++) {
Eric Laurent39e94f82010-07-28 01:32:47 -07001826 mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false);
Eric Laurentde070132010-07-13 04:45:46 -07001827 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001828}
1829
1830status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames)
1831{
Glenn Kastena0d68332012-01-27 16:47:15 -08001832 if (halFrames == NULL || dspFrames == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001833 return BAD_VALUE;
1834 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001835 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001836 if (initCheck() != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001837 return INVALID_OPERATION;
1838 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001839 *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001840
Dima Zavin799a70e2011-04-18 16:57:27 -07001841 return mOutput->stream->get_render_position(mOutput->stream, dspFrames);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001842}
1843
Eric Laurent39e94f82010-07-28 01:32:47 -07001844uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001845{
1846 Mutex::Autolock _l(mLock);
Eric Laurent39e94f82010-07-28 01:32:47 -07001847 uint32_t result = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001848 if (getEffectChain_l(sessionId) != 0) {
Eric Laurent39e94f82010-07-28 01:32:47 -07001849 result = EFFECT_SESSION;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001850 }
1851
1852 for (size_t i = 0; i < mTracks.size(); ++i) {
1853 sp<Track> track = mTracks[i];
Eric Laurentde070132010-07-13 04:45:46 -07001854 if (sessionId == track->sessionId() &&
1855 !(track->mCblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent39e94f82010-07-28 01:32:47 -07001856 result |= TRACK_SESSION;
1857 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001858 }
1859 }
1860
Eric Laurent39e94f82010-07-28 01:32:47 -07001861 return result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001862}
1863
Eric Laurentde070132010-07-13 04:45:46 -07001864uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId)
1865{
Dima Zavinfce7a472011-04-19 22:30:36 -07001866 // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
Eric Laurentde070132010-07-13 04:45:46 -07001867 // it is moved to correct output by audio policy manager when A2DP is connected or disconnected
Dima Zavinfce7a472011-04-19 22:30:36 -07001868 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
1869 return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Eric Laurentde070132010-07-13 04:45:46 -07001870 }
1871 for (size_t i = 0; i < mTracks.size(); i++) {
1872 sp<Track> track = mTracks[i];
1873 if (sessionId == track->sessionId() &&
1874 !(track->mCblk->flags & CBLK_INVALID_MSK)) {
Glenn Kasten02bbd202012-02-08 12:35:35 -08001875 return AudioSystem::getStrategyForStream(track->streamType());
Eric Laurentde070132010-07-13 04:45:46 -07001876 }
1877 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001878 return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Eric Laurentde070132010-07-13 04:45:46 -07001879}
1880
Mathias Agopian65ab4712010-07-14 17:59:35 -07001881
Glenn Kastenaed850d2012-01-26 09:46:34 -08001882AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001883{
1884 Mutex::Autolock _l(mLock);
1885 return mOutput;
1886}
1887
1888AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput()
1889{
1890 Mutex::Autolock _l(mLock);
1891 AudioStreamOut *output = mOutput;
1892 mOutput = NULL;
1893 return output;
1894}
1895
1896// this method must always be called either with ThreadBase mLock held or inside the thread loop
1897audio_stream_t* AudioFlinger::PlaybackThread::stream()
1898{
1899 if (mOutput == NULL) {
1900 return NULL;
1901 }
1902 return &mOutput->stream->common;
1903}
1904
Eric Laurent162b40b2011-12-05 09:47:19 -08001905uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs()
1906{
1907 // A2DP output latency is not due only to buffering capacity. It also reflects encoding,
1908 // decoding and transfer time. So sleeping for half of the latency would likely cause
1909 // underruns
1910 if (audio_is_a2dp_device((audio_devices_t)mDevice)) {
1911 return (uint32_t)((uint32_t)((mFrameCount * 1000) / mSampleRate) * 1000);
1912 } else {
1913 return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2;
1914 }
1915}
1916
Mathias Agopian65ab4712010-07-14 17:59:35 -07001917// ----------------------------------------------------------------------------
1918
Glenn Kasten23bb8be2012-01-26 10:38:26 -08001919AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001920 audio_io_handle_t id, uint32_t device, type_t type)
Glenn Kasten23bb8be2012-01-26 10:38:26 -08001921 : PlaybackThread(audioFlinger, output, id, device, type),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001922 mAudioMixer(new AudioMixer(mFrameCount, mSampleRate)),
1923 mPrevMixerStatus(MIXER_IDLE)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001924{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001925 // FIXME - Current mixer implementation only supports stereo output
1926 if (mChannelCount == 1) {
Steve Block29357bc2012-01-06 19:20:56 +00001927 ALOGE("Invalid audio hardware channel count");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928 }
1929}
1930
1931AudioFlinger::MixerThread::~MixerThread()
1932{
1933 delete mAudioMixer;
1934}
1935
Glenn Kasten83efdd02012-02-24 07:21:32 -08001936class CpuStats {
1937public:
1938 void sample();
1939#ifdef DEBUG_CPU_USAGE
1940private:
1941 ThreadCpuUsage mCpu;
1942#endif
1943};
1944
1945void CpuStats::sample() {
1946#ifdef DEBUG_CPU_USAGE
1947 const CentralTendencyStatistics& stats = mCpu.statistics();
1948 mCpu.sampleAndEnable();
1949 unsigned n = stats.n();
1950 // mCpu.elapsed() is expensive, so don't call it every loop
1951 if ((n & 127) == 1) {
1952 long long elapsed = mCpu.elapsed();
1953 if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) {
1954 double perLoop = elapsed / (double) n;
1955 double perLoop100 = perLoop * 0.01;
1956 double mean = stats.mean();
1957 double stddev = stats.stddev();
1958 double minimum = stats.minimum();
1959 double maximum = stats.maximum();
1960 mCpu.resetStatistics();
1961 ALOGI("CPU usage over past %.1f secs (%u mixer loops at %.1f mean ms per loop):\n us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f",
1962 elapsed * .000000001, n, perLoop * .000001,
1963 mean * .001,
1964 stddev * .001,
1965 minimum * .001,
1966 maximum * .001,
1967 mean / perLoop100,
1968 stddev / perLoop100,
1969 minimum / perLoop100,
1970 maximum / perLoop100);
1971 }
1972 }
1973#endif
1974};
1975
Mathias Agopian65ab4712010-07-14 17:59:35 -07001976bool AudioFlinger::MixerThread::threadLoop()
1977{
1978 Vector< sp<Track> > tracksToRemove;
Glenn Kasten29c23c32012-01-26 13:37:52 -08001979 mixer_state mixerStatus = MIXER_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001980 nsecs_t standbyTime = systemTime();
1981 size_t mixBufferSize = mFrameCount * mFrameSize;
1982 // FIXME: Relaxed timing because of a certain device that can't meet latency
1983 // Should be reduced to 2x after the vendor fixes the driver issue
Eric Laurent5c4e8182011-10-18 15:42:27 -07001984 // increase threshold again due to low power audio mode. The way this warning threshold is
1985 // calculated and its usefulness should be reconsidered anyway.
1986 nsecs_t maxPeriod = seconds(mFrameCount) / mSampleRate * 15;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001987 nsecs_t lastWarning = 0;
1988 bool longStandbyExit = false;
1989 uint32_t activeSleepTime = activeSleepTimeUs();
1990 uint32_t idleSleepTime = idleSleepTimeUs();
1991 uint32_t sleepTime = idleSleepTime;
Eric Laurent7cafbb32011-11-22 18:50:29 -08001992 uint32_t sleepTimeShift = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001993 Vector< sp<EffectChain> > effectChains;
Glenn Kasten83efdd02012-02-24 07:21:32 -08001994 CpuStats cpuStats;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001995
Eric Laurentfeb0db62011-07-22 09:04:31 -07001996 acquireWakeLock();
1997
Mathias Agopian65ab4712010-07-14 17:59:35 -07001998 while (!exitPending())
1999 {
Glenn Kasten83efdd02012-02-24 07:21:32 -08002000 cpuStats.sample();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002001 processConfigEvents();
2002
2003 mixerStatus = MIXER_IDLE;
2004 { // scope for mLock
2005
2006 Mutex::Autolock _l(mLock);
2007
2008 if (checkForNewParameters_l()) {
2009 mixBufferSize = mFrameCount * mFrameSize;
2010 // FIXME: Relaxed timing because of a certain device that can't meet latency
2011 // Should be reduced to 2x after the vendor fixes the driver issue
Eric Laurent5c4e8182011-10-18 15:42:27 -07002012 // increase threshold again due to low power audio mode. The way this warning
2013 // threshold is calculated and its usefulness should be reconsidered anyway.
2014 maxPeriod = seconds(mFrameCount) / mSampleRate * 15;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002015 activeSleepTime = activeSleepTimeUs();
2016 idleSleepTime = idleSleepTimeUs();
2017 }
2018
2019 const SortedVector< wp<Track> >& activeTracks = mActiveTracks;
2020
2021 // put audio hardware into standby after short delay
Glenn Kastenf6b16782011-12-15 09:51:17 -08002022 if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) ||
2023 mSuspended)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024 if (!mStandby) {
Glenn Kasten90bebef2012-01-27 15:24:38 -08002025 ALOGV("Audio hardware entering standby, mixer %p, mSuspended %d", this, mSuspended);
Dima Zavin799a70e2011-04-18 16:57:27 -07002026 mOutput->stream->common.standby(&mOutput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002027 mStandby = true;
2028 mBytesWritten = 0;
2029 }
2030
2031 if (!activeTracks.size() && mConfigEvents.isEmpty()) {
2032 // we're about to wait, flush the binder command buffer
2033 IPCThreadState::self()->flushCommands();
2034
2035 if (exitPending()) break;
2036
Eric Laurentfeb0db62011-07-22 09:04:31 -07002037 releaseWakeLock_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002038 // wait until we have something to do...
Glenn Kasten90bebef2012-01-27 15:24:38 -08002039 ALOGV("MixerThread %p TID %d going to sleep", this, gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002040 mWaitWorkCV.wait(mLock);
Glenn Kasten90bebef2012-01-27 15:24:38 -08002041 ALOGV("MixerThread %p TID %d waking up", this, gettid());
Eric Laurentfeb0db62011-07-22 09:04:31 -07002042 acquireWakeLock_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002043
Eric Laurent27741442012-01-17 19:20:12 -08002044 mPrevMixerStatus = MIXER_IDLE;
Glenn Kastenf1d45922012-01-13 15:54:24 -08002045 if (!mMasterMute) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002046 char value[PROPERTY_VALUE_MAX];
2047 property_get("ro.audio.silent", value, "0");
2048 if (atoi(value)) {
Steve Blockb8a80522011-12-20 16:23:08 +00002049 ALOGD("Silence is golden");
Glenn Kasten6637baa2012-01-09 09:40:36 -08002050 setMasterMute_l(true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002051 }
2052 }
2053
John Grossman4ff14ba2012-02-08 16:37:41 -08002054 standbyTime = systemTime() + mStandbyTimeInNsecs;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002055 sleepTime = idleSleepTime;
Eric Laurent7cafbb32011-11-22 18:50:29 -08002056 sleepTimeShift = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002057 continue;
2058 }
2059 }
2060
2061 mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove);
2062
2063 // prevent any changes in effect chain list and in each effect chain
2064 // during mixing and effect process as the audio buffers could be deleted
2065 // or modified if an effect is created or deleted
Eric Laurentde070132010-07-13 04:45:46 -07002066 lockEffectChains_l(effectChains);
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08002067 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002068
Glenn Kastenf6b16782011-12-15 09:51:17 -08002069 if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08002070 // obtain the presentation timestamp of the next output buffer
2071 int64_t pts;
2072 status_t status = INVALID_OPERATION;
2073
2074 if (NULL != mOutput->stream->get_next_write_timestamp) {
2075 status = mOutput->stream->get_next_write_timestamp(
2076 mOutput->stream, &pts);
2077 }
2078
2079 if (status != NO_ERROR) {
2080 pts = AudioBufferProvider::kInvalidPTS;
2081 }
2082
Mathias Agopian65ab4712010-07-14 17:59:35 -07002083 // mix buffers...
John Grossman4ff14ba2012-02-08 16:37:41 -08002084 mAudioMixer->process(pts);
Eric Laurent21e4b6e2012-01-23 18:56:59 -08002085 // increase sleep time progressively when application underrun condition clears.
2086 // Only increase sleep time if the mixer is ready for two consecutive times to avoid
2087 // that a steady state of alternating ready/not ready conditions keeps the sleep time
2088 // such that we would underrun the audio HAL.
2089 if ((sleepTime == 0) && (sleepTimeShift > 0)) {
Eric Laurent7cafbb32011-11-22 18:50:29 -08002090 sleepTimeShift--;
2091 }
Eric Laurent21e4b6e2012-01-23 18:56:59 -08002092 sleepTime = 0;
John Grossman4ff14ba2012-02-08 16:37:41 -08002093 standbyTime = systemTime() + mStandbyTimeInNsecs;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002094 //TODO: delay standby when effects have a tail
2095 } else {
2096 // If no tracks are ready, sleep once for the duration of an output
2097 // buffer size, then write 0s to the output
2098 if (sleepTime == 0) {
2099 if (mixerStatus == MIXER_TRACKS_ENABLED) {
Eric Laurent7cafbb32011-11-22 18:50:29 -08002100 sleepTime = activeSleepTime >> sleepTimeShift;
2101 if (sleepTime < kMinThreadSleepTimeUs) {
2102 sleepTime = kMinThreadSleepTimeUs;
2103 }
2104 // reduce sleep time in case of consecutive application underruns to avoid
2105 // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer
2106 // duration we would end up writing less data than needed by the audio HAL if
2107 // the condition persists.
2108 if (sleepTimeShift < kMaxThreadSleepTimeShift) {
2109 sleepTimeShift++;
2110 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002111 } else {
2112 sleepTime = idleSleepTime;
2113 }
2114 } else if (mBytesWritten != 0 ||
2115 (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) {
2116 memset (mMixBuffer, 0, mixBufferSize);
2117 sleepTime = 0;
Steve Block3856b092011-10-20 11:56:00 +01002118 ALOGV_IF((mBytesWritten == 0 && (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002119 }
2120 // TODO add standby time extension fct of effect tail
2121 }
2122
2123 if (mSuspended) {
Eric Laurent25cbe0e2010-08-18 18:13:17 -07002124 sleepTime = suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002125 }
2126 // sleepTime == 0 means we must write to audio hardware
2127 if (sleepTime == 0) {
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08002128 for (size_t i = 0; i < effectChains.size(); i ++) {
2129 effectChains[i]->process_l();
2130 }
2131 // enable changes in effect chain
2132 unlockEffectChains(effectChains);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002133 mLastWriteTime = systemTime();
2134 mInWrite = true;
2135 mBytesWritten += mixBufferSize;
2136
Dima Zavin799a70e2011-04-18 16:57:27 -07002137 int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002138 if (bytesWritten < 0) mBytesWritten -= mixBufferSize;
2139 mNumWrites++;
2140 mInWrite = false;
2141 nsecs_t now = systemTime();
2142 nsecs_t delta = now - mLastWriteTime;
Eric Laurent5c4e8182011-10-18 15:42:27 -07002143 if (!mStandby && delta > maxPeriod) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002144 mNumDelayedWrites++;
Glenn Kasten7dede872011-12-13 11:04:14 -08002145 if ((now - lastWarning) > kWarningThrottleNs) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002146 ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p",
Mathias Agopian65ab4712010-07-14 17:59:35 -07002147 ns2ms(delta), mNumDelayedWrites, this);
2148 lastWarning = now;
2149 }
2150 if (mStandby) {
2151 longStandbyExit = true;
2152 }
2153 }
2154 mStandby = false;
2155 } else {
2156 // enable changes in effect chain
Eric Laurentde070132010-07-13 04:45:46 -07002157 unlockEffectChains(effectChains);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002158 usleep(sleepTime);
2159 }
2160
2161 // finally let go of all our tracks, without the lock held
2162 // since we can't guarantee the destructors won't acquire that
2163 // same lock.
2164 tracksToRemove.clear();
2165
2166 // Effect chains will be actually deleted here if they were removed from
2167 // mEffectChains list during mixing or effects processing
2168 effectChains.clear();
2169 }
2170
2171 if (!mStandby) {
Dima Zavin799a70e2011-04-18 16:57:27 -07002172 mOutput->stream->common.standby(&mOutput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002173 }
2174
Eric Laurentfeb0db62011-07-22 09:04:31 -07002175 releaseWakeLock();
2176
Steve Block3856b092011-10-20 11:56:00 +01002177 ALOGV("MixerThread %p exiting", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002178 return false;
2179}
2180
2181// prepareTracks_l() must be called with ThreadBase::mLock held
Glenn Kasten29c23c32012-01-26 13:37:52 -08002182AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l(
2183 const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184{
2185
Glenn Kasten29c23c32012-01-26 13:37:52 -08002186 mixer_state mixerStatus = MIXER_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002187 // find out which tracks need to be processed
2188 size_t count = activeTracks.size();
2189 size_t mixedTracks = 0;
2190 size_t tracksWithEffect = 0;
2191
2192 float masterVolume = mMasterVolume;
2193 bool masterMute = mMasterMute;
2194
Eric Laurent571d49c2010-08-11 05:20:11 -07002195 if (masterMute) {
2196 masterVolume = 0;
2197 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002198 // Delegate master volume control to effect in output mix effect chain if needed
Dima Zavinfce7a472011-04-19 22:30:36 -07002199 sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002200 if (chain != 0) {
Eric Laurent571d49c2010-08-11 05:20:11 -07002201 uint32_t v = (uint32_t)(masterVolume * (1 << 24));
Eric Laurentcab11242010-07-15 12:50:15 -07002202 chain->setVolume_l(&v, &v);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002203 masterVolume = (float)((v + (1 << 23)) >> 24);
2204 chain.clear();
2205 }
2206
2207 for (size_t i=0 ; i<count ; i++) {
2208 sp<Track> t = activeTracks[i].promote();
2209 if (t == 0) continue;
2210
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002211 // this const just means the local variable doesn't change
Mathias Agopian65ab4712010-07-14 17:59:35 -07002212 Track* const track = t.get();
2213 audio_track_cblk_t* cblk = track->cblk();
2214
2215 // The first time a track is added we wait
2216 // for all its buffers to be filled before processing it
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002217 int name = track->name();
Eric Laurenta47b69c2011-11-08 18:10:16 -08002218 // make sure that we have enough frames to mix one full buffer.
2219 // enforce this condition only once to enable draining the buffer in case the client
2220 // app does not call stop() and relies on underrun to stop:
Eric Laurent27741442012-01-17 19:20:12 -08002221 // hence the test on (mPrevMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed
Eric Laurenta47b69c2011-11-08 18:10:16 -08002222 // during last round
Eric Laurent3dbe3202011-11-03 12:16:05 -07002223 uint32_t minFrames = 1;
Eric Laurenta47b69c2011-11-08 18:10:16 -08002224 if (!track->isStopped() && !track->isPausing() &&
Eric Laurent27741442012-01-17 19:20:12 -08002225 (mPrevMixerStatus == MIXER_TRACKS_READY)) {
Eric Laurent3dbe3202011-11-03 12:16:05 -07002226 if (t->sampleRate() == (int)mSampleRate) {
2227 minFrames = mFrameCount;
2228 } else {
Eric Laurent071ccd52011-12-22 16:08:41 -08002229 // +1 for rounding and +1 for additional sample needed for interpolation
2230 minFrames = (mFrameCount * t->sampleRate()) / mSampleRate + 1 + 1;
2231 // add frames already consumed but not yet released by the resampler
2232 // because cblk->framesReady() will include these frames
2233 minFrames += mAudioMixer->getUnreleasedFrames(track->name());
2234 // the minimum track buffer size is normally twice the number of frames necessary
2235 // to fill one buffer and the resampler should not leave more than one buffer worth
2236 // of unreleased frames after each pass, but just in case...
Steve Blockc1dc1cb2012-01-09 18:35:44 +00002237 ALOG_ASSERT(minFrames <= cblk->frameCount);
Eric Laurent3dbe3202011-11-03 12:16:05 -07002238 }
2239 }
John Grossman4ff14ba2012-02-08 16:37:41 -08002240 if ((track->framesReady() >= minFrames) && track->isReady() &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002241 !track->isPaused() && !track->isTerminated())
2242 {
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002243 //ALOGV("track %d u=%08x, s=%08x [OK] on thread %p", name, cblk->user, cblk->server, this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002244
2245 mixedTracks++;
2246
2247 // track->mainBuffer() != mMixBuffer means there is an effect chain
2248 // connected to the track
2249 chain.clear();
2250 if (track->mainBuffer() != mMixBuffer) {
2251 chain = getEffectChain_l(track->sessionId());
2252 // Delegate volume control to effect in track effect chain if needed
2253 if (chain != 0) {
2254 tracksWithEffect++;
2255 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00002256 ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on session %d",
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002257 name, track->sessionId());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002258 }
2259 }
2260
2261
2262 int param = AudioMixer::VOLUME;
2263 if (track->mFillingUpStatus == Track::FS_FILLED) {
2264 // no ramp for the first volume setting
2265 track->mFillingUpStatus = Track::FS_ACTIVE;
2266 if (track->mState == TrackBase::RESUMING) {
2267 track->mState = TrackBase::ACTIVE;
2268 param = AudioMixer::RAMP_VOLUME;
2269 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002270 mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002271 } else if (cblk->server != 0) {
2272 // If the track is stopped before the first frame was mixed,
2273 // do not apply ramp
2274 param = AudioMixer::RAMP_VOLUME;
2275 }
2276
2277 // compute volume for this track
Eric Laurente0aed6d2010-09-10 17:44:44 -07002278 uint32_t vl, vr, va;
Eric Laurent8569f0d2010-07-29 23:43:43 -07002279 if (track->isMuted() || track->isPausing() ||
Glenn Kasten02bbd202012-02-08 12:35:35 -08002280 mStreamTypes[track->streamType()].mute) {
Eric Laurente0aed6d2010-09-10 17:44:44 -07002281 vl = vr = va = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002282 if (track->isPausing()) {
2283 track->setPaused();
2284 }
2285 } else {
Eric Laurente0aed6d2010-09-10 17:44:44 -07002286
Mathias Agopian65ab4712010-07-14 17:59:35 -07002287 // read original volumes with volume control
Glenn Kasten02bbd202012-02-08 12:35:35 -08002288 float typeVolume = mStreamTypes[track->streamType()].volume;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002289 float v = masterVolume * typeVolume;
Glenn Kasten83d86532012-01-17 14:39:34 -08002290 uint32_t vlr = cblk->getVolumeLR();
Glenn Kastenb1cf75c2012-01-17 12:20:54 -08002291 vl = vlr & 0xFFFF;
2292 vr = vlr >> 16;
2293 // track volumes come from shared memory, so can't be trusted and must be clamped
2294 if (vl > MAX_GAIN_INT) {
2295 ALOGV("Track left volume out of range: %04X", vl);
2296 vl = MAX_GAIN_INT;
2297 }
2298 if (vr > MAX_GAIN_INT) {
2299 ALOGV("Track right volume out of range: %04X", vr);
2300 vr = MAX_GAIN_INT;
2301 }
2302 // now apply the master volume and stream type volume
2303 vl = (uint32_t)(v * vl) << 12;
2304 vr = (uint32_t)(v * vr) << 12;
2305 // assuming master volume and stream type volume each go up to 1.0,
2306 // vl and vr are now in 8.24 format
Mathias Agopian65ab4712010-07-14 17:59:35 -07002307
Glenn Kasten05632a52012-01-03 14:22:33 -08002308 uint16_t sendLevel = cblk->getSendLevel_U4_12();
2309 // send level comes from shared memory and so may be corrupt
Glenn Kasten3b81aca2012-01-27 15:26:23 -08002310 if (sendLevel > MAX_GAIN_INT) {
Glenn Kasten05632a52012-01-03 14:22:33 -08002311 ALOGV("Track send level out of range: %04X", sendLevel);
Glenn Kastenb1cf75c2012-01-17 12:20:54 -08002312 sendLevel = MAX_GAIN_INT;
Glenn Kasten05632a52012-01-03 14:22:33 -08002313 }
2314 va = (uint32_t)(v * sendLevel);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002315 }
Eric Laurente0aed6d2010-09-10 17:44:44 -07002316 // Delegate volume control to effect in track effect chain if needed
2317 if (chain != 0 && chain->setVolume_l(&vl, &vr)) {
2318 // Do not ramp volume if volume is controlled by effect
2319 param = AudioMixer::VOLUME;
2320 track->mHasVolumeController = true;
2321 } else {
2322 // force no volume ramp when volume controller was just disabled or removed
2323 // from effect chain to avoid volume spike
2324 if (track->mHasVolumeController) {
2325 param = AudioMixer::VOLUME;
2326 }
2327 track->mHasVolumeController = false;
2328 }
2329
2330 // Convert volumes from 8.24 to 4.12 format
Glenn Kastenb1cf75c2012-01-17 12:20:54 -08002331 // This additional clamping is needed in case chain->setVolume_l() overshot
Glenn Kasten3b81aca2012-01-27 15:26:23 -08002332 vl = (vl + (1 << 11)) >> 12;
2333 if (vl > MAX_GAIN_INT) vl = MAX_GAIN_INT;
2334 vr = (vr + (1 << 11)) >> 12;
2335 if (vr > MAX_GAIN_INT) vr = MAX_GAIN_INT;
Eric Laurente0aed6d2010-09-10 17:44:44 -07002336
Glenn Kasten3b81aca2012-01-27 15:26:23 -08002337 if (va > MAX_GAIN_INT) va = MAX_GAIN_INT; // va is uint32_t, so no need to check for -
Mathias Agopian65ab4712010-07-14 17:59:35 -07002338
Mathias Agopian65ab4712010-07-14 17:59:35 -07002339 // XXX: these things DON'T need to be done each time
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002340 mAudioMixer->setBufferProvider(name, track);
2341 mAudioMixer->enable(name);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002342
Glenn Kasten3b81aca2012-01-27 15:26:23 -08002343 mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)vl);
2344 mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)vr);
2345 mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)va);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002346 mAudioMixer->setParameter(
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002347 name,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002348 AudioMixer::TRACK,
2349 AudioMixer::FORMAT, (void *)track->format());
2350 mAudioMixer->setParameter(
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002351 name,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002352 AudioMixer::TRACK,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07002353 AudioMixer::CHANNEL_MASK, (void *)track->channelMask());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002354 mAudioMixer->setParameter(
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002355 name,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002356 AudioMixer::RESAMPLE,
2357 AudioMixer::SAMPLE_RATE,
2358 (void *)(cblk->sampleRate));
2359 mAudioMixer->setParameter(
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002360 name,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002361 AudioMixer::TRACK,
2362 AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer());
2363 mAudioMixer->setParameter(
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002364 name,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002365 AudioMixer::TRACK,
2366 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer());
2367
2368 // reset retry count
2369 track->mRetryCount = kMaxTrackRetries;
Eric Laurent27741442012-01-17 19:20:12 -08002370 // If one track is ready, set the mixer ready if:
2371 // - the mixer was not ready during previous round OR
2372 // - no other track is not ready
2373 if (mPrevMixerStatus != MIXER_TRACKS_READY ||
2374 mixerStatus != MIXER_TRACKS_ENABLED) {
2375 mixerStatus = MIXER_TRACKS_READY;
2376 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002377 } else {
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002378 //ALOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", name, cblk->user, cblk->server, this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002379 if (track->isStopped()) {
2380 track->reset();
2381 }
2382 if (track->isTerminated() || track->isStopped() || track->isPaused()) {
2383 // We have consumed all the buffers of this track.
2384 // Remove it from the list of active tracks.
2385 tracksToRemove->add(track);
2386 } else {
2387 // No buffers for this track. Give it a few chances to
2388 // fill a buffer, then remove it from active list.
2389 if (--(track->mRetryCount) <= 0) {
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002390 ALOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002391 tracksToRemove->add(track);
Eric Laurent44d98482010-09-30 16:12:31 -07002392 // indicate to client process that the track was disabled because of underrun
Eric Laurent38ccae22011-03-28 18:37:07 -07002393 android_atomic_or(CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent27741442012-01-17 19:20:12 -08002394 // If one track is not ready, mark the mixer also not ready if:
2395 // - the mixer was ready during previous round OR
2396 // - no other track is ready
2397 } else if (mPrevMixerStatus == MIXER_TRACKS_READY ||
2398 mixerStatus != MIXER_TRACKS_READY) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002399 mixerStatus = MIXER_TRACKS_ENABLED;
2400 }
2401 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002402 mAudioMixer->disable(name);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002403 }
2404 }
2405
2406 // remove all the tracks that need to be...
2407 count = tracksToRemove->size();
Glenn Kastenf6b16782011-12-15 09:51:17 -08002408 if (CC_UNLIKELY(count)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002409 for (size_t i=0 ; i<count ; i++) {
2410 const sp<Track>& track = tracksToRemove->itemAt(i);
2411 mActiveTracks.remove(track);
2412 if (track->mainBuffer() != mMixBuffer) {
2413 chain = getEffectChain_l(track->sessionId());
2414 if (chain != 0) {
Steve Block3856b092011-10-20 11:56:00 +01002415 ALOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId());
Eric Laurentb469b942011-05-09 12:09:06 -07002416 chain->decActiveTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002417 }
2418 }
2419 if (track->isTerminated()) {
Eric Laurentb469b942011-05-09 12:09:06 -07002420 removeTrack_l(track);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002421 }
2422 }
2423 }
2424
2425 // mix buffer must be cleared if all tracks are connected to an
2426 // effect chain as in this case the mixer will not write to
2427 // mix buffer and track effects will accumulate into it
2428 if (mixedTracks != 0 && mixedTracks == tracksWithEffect) {
2429 memset(mMixBuffer, 0, mFrameCount * mChannelCount * sizeof(int16_t));
2430 }
2431
Eric Laurent27741442012-01-17 19:20:12 -08002432 mPrevMixerStatus = mixerStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002433 return mixerStatus;
2434}
2435
Glenn Kastenfff6d712012-01-12 16:38:12 -08002436void AudioFlinger::MixerThread::invalidateTracks(audio_stream_type_t streamType)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002437{
Steve Block3856b092011-10-20 11:56:00 +01002438 ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d",
Eric Laurentde070132010-07-13 04:45:46 -07002439 this, streamType, mTracks.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002440 Mutex::Autolock _l(mLock);
Eric Laurentde070132010-07-13 04:45:46 -07002441
Mathias Agopian65ab4712010-07-14 17:59:35 -07002442 size_t size = mTracks.size();
2443 for (size_t i = 0; i < size; i++) {
2444 sp<Track> t = mTracks[i];
Glenn Kasten02bbd202012-02-08 12:35:35 -08002445 if (t->streamType() == streamType) {
Eric Laurent38ccae22011-03-28 18:37:07 -07002446 android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002447 t->mCblk->cv.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002448 }
2449 }
2450}
2451
Glenn Kastenfff6d712012-01-12 16:38:12 -08002452void AudioFlinger::PlaybackThread::setStreamValid(audio_stream_type_t streamType, bool valid)
Eric Laurent9f6530f2011-08-30 10:18:54 -07002453{
Steve Block3856b092011-10-20 11:56:00 +01002454 ALOGV ("PlaybackThread::setStreamValid() thread %p, streamType %d, valid %d",
Eric Laurent9f6530f2011-08-30 10:18:54 -07002455 this, streamType, valid);
2456 Mutex::Autolock _l(mLock);
2457
2458 mStreamTypes[streamType].valid = valid;
2459}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002460
2461// getTrackName_l() must be called with ThreadBase::mLock held
2462int AudioFlinger::MixerThread::getTrackName_l()
2463{
2464 return mAudioMixer->getTrackName();
2465}
2466
2467// deleteTrackName_l() must be called with ThreadBase::mLock held
2468void AudioFlinger::MixerThread::deleteTrackName_l(int name)
2469{
Steve Block3856b092011-10-20 11:56:00 +01002470 ALOGV("remove track (%d) and delete from mixer", name);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002471 mAudioMixer->deleteTrackName(name);
2472}
2473
2474// checkForNewParameters_l() must be called with ThreadBase::mLock held
2475bool AudioFlinger::MixerThread::checkForNewParameters_l()
2476{
2477 bool reconfig = false;
2478
2479 while (!mNewParameters.isEmpty()) {
2480 status_t status = NO_ERROR;
2481 String8 keyValuePair = mNewParameters[0];
2482 AudioParameter param = AudioParameter(keyValuePair);
2483 int value;
2484
2485 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
2486 reconfig = true;
2487 }
2488 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
Glenn Kasten58f30212012-01-12 12:27:51 -08002489 if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002490 status = BAD_VALUE;
2491 } else {
2492 reconfig = true;
2493 }
2494 }
2495 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002496 if (value != AUDIO_CHANNEL_OUT_STEREO) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002497 status = BAD_VALUE;
2498 } else {
2499 reconfig = true;
2500 }
2501 }
2502 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
2503 // do not accept frame count changes if tracks are open as the track buffer
Glenn Kasten362c4e62011-12-14 10:28:06 -08002504 // size depends on frame count and correct behavior would not be guaranteed
Mathias Agopian65ab4712010-07-14 17:59:35 -07002505 // if frame count is changed after track creation
2506 if (!mTracks.isEmpty()) {
2507 status = INVALID_OPERATION;
2508 } else {
2509 reconfig = true;
2510 }
2511 }
2512 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
Gloria Wang9ee159b2011-02-24 14:51:45 -08002513 // when changing the audio output device, call addBatteryData to notify
2514 // the change
Eric Laurentb469b942011-05-09 12:09:06 -07002515 if ((int)mDevice != value) {
Gloria Wang9ee159b2011-02-24 14:51:45 -08002516 uint32_t params = 0;
2517 // check whether speaker is on
Dima Zavinfce7a472011-04-19 22:30:36 -07002518 if (value & AUDIO_DEVICE_OUT_SPEAKER) {
Gloria Wang9ee159b2011-02-24 14:51:45 -08002519 params |= IMediaPlayerService::kBatteryDataSpeakerOn;
2520 }
2521
2522 int deviceWithoutSpeaker
Dima Zavinfce7a472011-04-19 22:30:36 -07002523 = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER;
Gloria Wang9ee159b2011-02-24 14:51:45 -08002524 // check if any other device (except speaker) is on
2525 if (value & deviceWithoutSpeaker ) {
2526 params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
2527 }
2528
2529 if (params != 0) {
2530 addBatteryData(params);
2531 }
2532 }
2533
Mathias Agopian65ab4712010-07-14 17:59:35 -07002534 // forward device change to effects that have requested to be
2535 // aware of attached audio device.
2536 mDevice = (uint32_t)value;
2537 for (size_t i = 0; i < mEffectChains.size(); i++) {
Eric Laurentcab11242010-07-15 12:50:15 -07002538 mEffectChains[i]->setDevice_l(mDevice);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002539 }
2540 }
2541
2542 if (status == NO_ERROR) {
Dima Zavin799a70e2011-04-18 16:57:27 -07002543 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavinfce7a472011-04-19 22:30:36 -07002544 keyValuePair.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002545 if (!mStandby && status == INVALID_OPERATION) {
Dima Zavin799a70e2011-04-18 16:57:27 -07002546 mOutput->stream->common.standby(&mOutput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002547 mStandby = true;
2548 mBytesWritten = 0;
Dima Zavin799a70e2011-04-18 16:57:27 -07002549 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavinfce7a472011-04-19 22:30:36 -07002550 keyValuePair.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002551 }
2552 if (status == NO_ERROR && reconfig) {
2553 delete mAudioMixer;
Glenn Kastene9dd0172012-01-27 18:08:45 -08002554 // for safety in case readOutputParameters() accesses mAudioMixer (it doesn't)
2555 mAudioMixer = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002556 readOutputParameters();
2557 mAudioMixer = new AudioMixer(mFrameCount, mSampleRate);
2558 for (size_t i = 0; i < mTracks.size() ; i++) {
2559 int name = getTrackName_l();
2560 if (name < 0) break;
2561 mTracks[i]->mName = name;
2562 // limit track sample rate to 2 x new output sample rate
2563 if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) {
2564 mTracks[i]->mCblk->sampleRate = 2 * sampleRate();
2565 }
2566 }
2567 sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
2568 }
2569 }
2570
2571 mNewParameters.removeAt(0);
2572
2573 mParamStatus = status;
2574 mParamCond.signal();
Eric Laurent60cd0a02011-09-13 11:40:21 -07002575 // wait for condition with time out in case the thread calling ThreadBase::setParameters()
2576 // already timed out waiting for the status and will never signal the condition.
Glenn Kasten7dede872011-12-13 11:04:14 -08002577 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002578 }
2579 return reconfig;
2580}
2581
2582status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args)
2583{
2584 const size_t SIZE = 256;
2585 char buffer[SIZE];
2586 String8 result;
2587
2588 PlaybackThread::dumpInternals(fd, args);
2589
2590 snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames());
2591 result.append(buffer);
2592 write(fd, result.string(), result.size());
2593 return NO_ERROR;
2594}
2595
Mathias Agopian65ab4712010-07-14 17:59:35 -07002596uint32_t AudioFlinger::MixerThread::idleSleepTimeUs()
2597{
Eric Laurent60e18242010-07-29 06:50:24 -07002598 return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002599}
2600
Eric Laurent25cbe0e2010-08-18 18:13:17 -07002601uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs()
2602{
2603 return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
2604}
2605
Mathias Agopian65ab4712010-07-14 17:59:35 -07002606// ----------------------------------------------------------------------------
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002607AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger,
2608 AudioStreamOut* output, audio_io_handle_t id, uint32_t device)
Glenn Kasten23bb8be2012-01-26 10:38:26 -08002609 : PlaybackThread(audioFlinger, output, id, device, DIRECT)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002610 // mLeftVolFloat, mRightVolFloat
2611 // mLeftVolShort, mRightVolShort
Mathias Agopian65ab4712010-07-14 17:59:35 -07002612{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002613}
2614
2615AudioFlinger::DirectOutputThread::~DirectOutputThread()
2616{
2617}
2618
Mathias Agopian65ab4712010-07-14 17:59:35 -07002619void AudioFlinger::DirectOutputThread::applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp)
2620{
2621 // Do not apply volume on compressed audio
Dima Zavinfce7a472011-04-19 22:30:36 -07002622 if (!audio_is_linear_pcm(mFormat)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002623 return;
2624 }
2625
2626 // convert to signed 16 bit before volume calculation
Dima Zavinfce7a472011-04-19 22:30:36 -07002627 if (mFormat == AUDIO_FORMAT_PCM_8_BIT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002628 size_t count = mFrameCount * mChannelCount;
2629 uint8_t *src = (uint8_t *)mMixBuffer + count-1;
2630 int16_t *dst = mMixBuffer + count-1;
2631 while(count--) {
2632 *dst-- = (int16_t)(*src--^0x80) << 8;
2633 }
2634 }
2635
2636 size_t frameCount = mFrameCount;
2637 int16_t *out = mMixBuffer;
2638 if (ramp) {
2639 if (mChannelCount == 1) {
2640 int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16;
2641 int32_t vlInc = d / (int32_t)frameCount;
2642 int32_t vl = ((int32_t)mLeftVolShort << 16);
2643 do {
2644 out[0] = clamp16(mul(out[0], vl >> 16) >> 12);
2645 out++;
2646 vl += vlInc;
2647 } while (--frameCount);
2648
2649 } else {
2650 int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16;
2651 int32_t vlInc = d / (int32_t)frameCount;
2652 d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16;
2653 int32_t vrInc = d / (int32_t)frameCount;
2654 int32_t vl = ((int32_t)mLeftVolShort << 16);
2655 int32_t vr = ((int32_t)mRightVolShort << 16);
2656 do {
2657 out[0] = clamp16(mul(out[0], vl >> 16) >> 12);
2658 out[1] = clamp16(mul(out[1], vr >> 16) >> 12);
2659 out += 2;
2660 vl += vlInc;
2661 vr += vrInc;
2662 } while (--frameCount);
2663 }
2664 } else {
2665 if (mChannelCount == 1) {
2666 do {
2667 out[0] = clamp16(mul(out[0], leftVol) >> 12);
2668 out++;
2669 } while (--frameCount);
2670 } else {
2671 do {
2672 out[0] = clamp16(mul(out[0], leftVol) >> 12);
2673 out[1] = clamp16(mul(out[1], rightVol) >> 12);
2674 out += 2;
2675 } while (--frameCount);
2676 }
2677 }
2678
2679 // convert back to unsigned 8 bit after volume calculation
Dima Zavinfce7a472011-04-19 22:30:36 -07002680 if (mFormat == AUDIO_FORMAT_PCM_8_BIT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002681 size_t count = mFrameCount * mChannelCount;
2682 int16_t *src = mMixBuffer;
2683 uint8_t *dst = (uint8_t *)mMixBuffer;
2684 while(count--) {
2685 *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80;
2686 }
2687 }
2688
2689 mLeftVolShort = leftVol;
2690 mRightVolShort = rightVol;
2691}
2692
2693bool AudioFlinger::DirectOutputThread::threadLoop()
2694{
Glenn Kasten29c23c32012-01-26 13:37:52 -08002695 mixer_state mixerStatus = MIXER_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002696 sp<Track> trackToRemove;
2697 sp<Track> activeTrack;
2698 nsecs_t standbyTime = systemTime();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002699 size_t mixBufferSize = mFrameCount*mFrameSize;
2700 uint32_t activeSleepTime = activeSleepTimeUs();
2701 uint32_t idleSleepTime = idleSleepTimeUs();
2702 uint32_t sleepTime = idleSleepTime;
2703 // use shorter standby delay as on normal output to release
2704 // hardware resources as soon as possible
2705 nsecs_t standbyDelay = microseconds(activeSleepTime*2);
2706
Eric Laurentfeb0db62011-07-22 09:04:31 -07002707 acquireWakeLock();
2708
Mathias Agopian65ab4712010-07-14 17:59:35 -07002709 while (!exitPending())
2710 {
2711 bool rampVolume;
2712 uint16_t leftVol;
2713 uint16_t rightVol;
2714 Vector< sp<EffectChain> > effectChains;
2715
2716 processConfigEvents();
2717
2718 mixerStatus = MIXER_IDLE;
2719
2720 { // scope for the mLock
2721
2722 Mutex::Autolock _l(mLock);
2723
2724 if (checkForNewParameters_l()) {
2725 mixBufferSize = mFrameCount*mFrameSize;
2726 activeSleepTime = activeSleepTimeUs();
2727 idleSleepTime = idleSleepTimeUs();
2728 standbyDelay = microseconds(activeSleepTime*2);
2729 }
2730
2731 // put audio hardware into standby after short delay
Glenn Kastenf6b16782011-12-15 09:51:17 -08002732 if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) ||
2733 mSuspended)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002734 // wait until we have something to do...
2735 if (!mStandby) {
Glenn Kasten90bebef2012-01-27 15:24:38 -08002736 ALOGV("Audio hardware entering standby, mixer %p", this);
Dima Zavin799a70e2011-04-18 16:57:27 -07002737 mOutput->stream->common.standby(&mOutput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002738 mStandby = true;
2739 mBytesWritten = 0;
2740 }
2741
2742 if (!mActiveTracks.size() && mConfigEvents.isEmpty()) {
2743 // we're about to wait, flush the binder command buffer
2744 IPCThreadState::self()->flushCommands();
2745
2746 if (exitPending()) break;
2747
Eric Laurentfeb0db62011-07-22 09:04:31 -07002748 releaseWakeLock_l();
Glenn Kasten90bebef2012-01-27 15:24:38 -08002749 ALOGV("DirectOutputThread %p TID %d going to sleep", this, gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002750 mWaitWorkCV.wait(mLock);
Glenn Kasten90bebef2012-01-27 15:24:38 -08002751 ALOGV("DirectOutputThread %p TID %d waking up in active mode", this, gettid());
Eric Laurentfeb0db62011-07-22 09:04:31 -07002752 acquireWakeLock_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002753
Glenn Kastenf1d45922012-01-13 15:54:24 -08002754 if (!mMasterMute) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002755 char value[PROPERTY_VALUE_MAX];
2756 property_get("ro.audio.silent", value, "0");
2757 if (atoi(value)) {
Steve Blockb8a80522011-12-20 16:23:08 +00002758 ALOGD("Silence is golden");
Glenn Kasten6637baa2012-01-09 09:40:36 -08002759 setMasterMute_l(true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002760 }
2761 }
2762
2763 standbyTime = systemTime() + standbyDelay;
2764 sleepTime = idleSleepTime;
2765 continue;
2766 }
2767 }
2768
2769 effectChains = mEffectChains;
2770
2771 // find out which tracks need to be processed
2772 if (mActiveTracks.size() != 0) {
2773 sp<Track> t = mActiveTracks[0].promote();
2774 if (t == 0) continue;
2775
2776 Track* const track = t.get();
2777 audio_track_cblk_t* cblk = track->cblk();
2778
2779 // The first time a track is added we wait
2780 // for all its buffers to be filled before processing it
Eric Laurentaf59ce22010-10-05 14:41:42 -07002781 if (cblk->framesReady() && track->isReady() &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002782 !track->isPaused() && !track->isTerminated())
2783 {
Steve Block3856b092011-10-20 11:56:00 +01002784 //ALOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002785
2786 if (track->mFillingUpStatus == Track::FS_FILLED) {
2787 track->mFillingUpStatus = Track::FS_ACTIVE;
2788 mLeftVolFloat = mRightVolFloat = 0;
2789 mLeftVolShort = mRightVolShort = 0;
2790 if (track->mState == TrackBase::RESUMING) {
2791 track->mState = TrackBase::ACTIVE;
2792 rampVolume = true;
2793 }
2794 } else if (cblk->server != 0) {
2795 // If the track is stopped before the first frame was mixed,
2796 // do not apply ramp
2797 rampVolume = true;
2798 }
2799 // compute volume for this track
2800 float left, right;
2801 if (track->isMuted() || mMasterMute || track->isPausing() ||
Glenn Kasten02bbd202012-02-08 12:35:35 -08002802 mStreamTypes[track->streamType()].mute) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002803 left = right = 0;
2804 if (track->isPausing()) {
2805 track->setPaused();
2806 }
2807 } else {
Glenn Kasten02bbd202012-02-08 12:35:35 -08002808 float typeVolume = mStreamTypes[track->streamType()].volume;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002809 float v = mMasterVolume * typeVolume;
Glenn Kasten83d86532012-01-17 14:39:34 -08002810 uint32_t vlr = cblk->getVolumeLR();
Glenn Kastenb1cf75c2012-01-17 12:20:54 -08002811 float v_clamped = v * (vlr & 0xFFFF);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002812 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
2813 left = v_clamped/MAX_GAIN;
Glenn Kastenb1cf75c2012-01-17 12:20:54 -08002814 v_clamped = v * (vlr >> 16);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002815 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
2816 right = v_clamped/MAX_GAIN;
2817 }
2818
2819 if (left != mLeftVolFloat || right != mRightVolFloat) {
2820 mLeftVolFloat = left;
2821 mRightVolFloat = right;
2822
2823 // If audio HAL implements volume control,
2824 // force software volume to nominal value
Dima Zavin799a70e2011-04-18 16:57:27 -07002825 if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002826 left = 1.0f;
2827 right = 1.0f;
2828 }
2829
2830 // Convert volumes from float to 8.24
2831 uint32_t vl = (uint32_t)(left * (1 << 24));
2832 uint32_t vr = (uint32_t)(right * (1 << 24));
2833
2834 // Delegate volume control to effect in track effect chain if needed
2835 // only one effect chain can be present on DirectOutputThread, so if
2836 // there is one, the track is connected to it
2837 if (!effectChains.isEmpty()) {
Eric Laurente0aed6d2010-09-10 17:44:44 -07002838 // Do not ramp volume if volume is controlled by effect
Eric Laurentcab11242010-07-15 12:50:15 -07002839 if(effectChains[0]->setVolume_l(&vl, &vr)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002840 rampVolume = false;
2841 }
2842 }
2843
2844 // Convert volumes from 8.24 to 4.12 format
2845 uint32_t v_clamped = (vl + (1 << 11)) >> 12;
2846 if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT;
2847 leftVol = (uint16_t)v_clamped;
2848 v_clamped = (vr + (1 << 11)) >> 12;
2849 if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT;
2850 rightVol = (uint16_t)v_clamped;
2851 } else {
2852 leftVol = mLeftVolShort;
2853 rightVol = mRightVolShort;
2854 rampVolume = false;
2855 }
2856
2857 // reset retry count
2858 track->mRetryCount = kMaxTrackRetriesDirect;
2859 activeTrack = t;
2860 mixerStatus = MIXER_TRACKS_READY;
2861 } else {
Steve Block3856b092011-10-20 11:56:00 +01002862 //ALOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002863 if (track->isStopped()) {
2864 track->reset();
2865 }
2866 if (track->isTerminated() || track->isStopped() || track->isPaused()) {
2867 // We have consumed all the buffers of this track.
2868 // Remove it from the list of active tracks.
2869 trackToRemove = track;
2870 } else {
2871 // No buffers for this track. Give it a few chances to
2872 // fill a buffer, then remove it from active list.
2873 if (--(track->mRetryCount) <= 0) {
Steve Block3856b092011-10-20 11:56:00 +01002874 ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002875 trackToRemove = track;
2876 } else {
2877 mixerStatus = MIXER_TRACKS_ENABLED;
2878 }
2879 }
2880 }
2881 }
2882
2883 // remove all the tracks that need to be...
Glenn Kastenf6b16782011-12-15 09:51:17 -08002884 if (CC_UNLIKELY(trackToRemove != 0)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002885 mActiveTracks.remove(trackToRemove);
2886 if (!effectChains.isEmpty()) {
Steve Block3856b092011-10-20 11:56:00 +01002887 ALOGV("stopping track on chain %p for session Id: %d", effectChains[0].get(),
Eric Laurentde070132010-07-13 04:45:46 -07002888 trackToRemove->sessionId());
Eric Laurentb469b942011-05-09 12:09:06 -07002889 effectChains[0]->decActiveTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002890 }
2891 if (trackToRemove->isTerminated()) {
Eric Laurentb469b942011-05-09 12:09:06 -07002892 removeTrack_l(trackToRemove);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002893 }
2894 }
2895
Eric Laurentde070132010-07-13 04:45:46 -07002896 lockEffectChains_l(effectChains);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002897 }
2898
Glenn Kastenf6b16782011-12-15 09:51:17 -08002899 if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002900 AudioBufferProvider::Buffer buffer;
2901 size_t frameCount = mFrameCount;
Glenn Kastena1117922012-01-26 10:53:32 -08002902 int8_t *curBuf = (int8_t *)mMixBuffer;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002903 // output audio to hardware
2904 while (frameCount) {
2905 buffer.frameCount = frameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -08002906 activeTrack->getNextBuffer(&buffer,
2907 AudioBufferProvider::kInvalidPTS);
Glenn Kastenf6b16782011-12-15 09:51:17 -08002908 if (CC_UNLIKELY(buffer.raw == NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002909 memset(curBuf, 0, frameCount * mFrameSize);
2910 break;
2911 }
2912 memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
2913 frameCount -= buffer.frameCount;
2914 curBuf += buffer.frameCount * mFrameSize;
2915 activeTrack->releaseBuffer(&buffer);
2916 }
2917 sleepTime = 0;
2918 standbyTime = systemTime() + standbyDelay;
2919 } else {
2920 if (sleepTime == 0) {
2921 if (mixerStatus == MIXER_TRACKS_ENABLED) {
2922 sleepTime = activeSleepTime;
2923 } else {
2924 sleepTime = idleSleepTime;
2925 }
Dima Zavinfce7a472011-04-19 22:30:36 -07002926 } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002927 memset (mMixBuffer, 0, mFrameCount * mFrameSize);
2928 sleepTime = 0;
2929 }
2930 }
2931
2932 if (mSuspended) {
Eric Laurent25cbe0e2010-08-18 18:13:17 -07002933 sleepTime = suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002934 }
2935 // sleepTime == 0 means we must write to audio hardware
2936 if (sleepTime == 0) {
2937 if (mixerStatus == MIXER_TRACKS_READY) {
2938 applyVolume(leftVol, rightVol, rampVolume);
2939 }
2940 for (size_t i = 0; i < effectChains.size(); i ++) {
2941 effectChains[i]->process_l();
2942 }
Eric Laurentde070132010-07-13 04:45:46 -07002943 unlockEffectChains(effectChains);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002944
2945 mLastWriteTime = systemTime();
2946 mInWrite = true;
2947 mBytesWritten += mixBufferSize;
Dima Zavin799a70e2011-04-18 16:57:27 -07002948 int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002949 if (bytesWritten < 0) mBytesWritten -= mixBufferSize;
2950 mNumWrites++;
2951 mInWrite = false;
2952 mStandby = false;
2953 } else {
Eric Laurentde070132010-07-13 04:45:46 -07002954 unlockEffectChains(effectChains);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002955 usleep(sleepTime);
2956 }
2957
2958 // finally let go of removed track, without the lock held
2959 // since we can't guarantee the destructors won't acquire that
2960 // same lock.
2961 trackToRemove.clear();
2962 activeTrack.clear();
2963
2964 // Effect chains will be actually deleted here if they were removed from
2965 // mEffectChains list during mixing or effects processing
2966 effectChains.clear();
2967 }
2968
2969 if (!mStandby) {
Dima Zavin799a70e2011-04-18 16:57:27 -07002970 mOutput->stream->common.standby(&mOutput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002971 }
2972
Eric Laurentfeb0db62011-07-22 09:04:31 -07002973 releaseWakeLock();
2974
Steve Block3856b092011-10-20 11:56:00 +01002975 ALOGV("DirectOutputThread %p exiting", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002976 return false;
2977}
2978
2979// getTrackName_l() must be called with ThreadBase::mLock held
2980int AudioFlinger::DirectOutputThread::getTrackName_l()
2981{
2982 return 0;
2983}
2984
2985// deleteTrackName_l() must be called with ThreadBase::mLock held
2986void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name)
2987{
2988}
2989
2990// checkForNewParameters_l() must be called with ThreadBase::mLock held
2991bool AudioFlinger::DirectOutputThread::checkForNewParameters_l()
2992{
2993 bool reconfig = false;
2994
2995 while (!mNewParameters.isEmpty()) {
2996 status_t status = NO_ERROR;
2997 String8 keyValuePair = mNewParameters[0];
2998 AudioParameter param = AudioParameter(keyValuePair);
2999 int value;
3000
3001 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
3002 // do not accept frame count changes if tracks are open as the track buffer
3003 // size depends on frame count and correct behavior would not be garantied
3004 // if frame count is changed after track creation
3005 if (!mTracks.isEmpty()) {
3006 status = INVALID_OPERATION;
3007 } else {
3008 reconfig = true;
3009 }
3010 }
3011 if (status == NO_ERROR) {
Dima Zavin799a70e2011-04-18 16:57:27 -07003012 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavinfce7a472011-04-19 22:30:36 -07003013 keyValuePair.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003014 if (!mStandby && status == INVALID_OPERATION) {
Dima Zavin799a70e2011-04-18 16:57:27 -07003015 mOutput->stream->common.standby(&mOutput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003016 mStandby = true;
3017 mBytesWritten = 0;
Dima Zavin799a70e2011-04-18 16:57:27 -07003018 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavinfce7a472011-04-19 22:30:36 -07003019 keyValuePair.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003020 }
3021 if (status == NO_ERROR && reconfig) {
3022 readOutputParameters();
3023 sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
3024 }
3025 }
3026
3027 mNewParameters.removeAt(0);
3028
3029 mParamStatus = status;
3030 mParamCond.signal();
Eric Laurent60cd0a02011-09-13 11:40:21 -07003031 // wait for condition with time out in case the thread calling ThreadBase::setParameters()
3032 // already timed out waiting for the status and will never signal the condition.
Glenn Kasten7dede872011-12-13 11:04:14 -08003033 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003034 }
3035 return reconfig;
3036}
3037
3038uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs()
3039{
3040 uint32_t time;
Dima Zavinfce7a472011-04-19 22:30:36 -07003041 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent162b40b2011-12-05 09:47:19 -08003042 time = PlaybackThread::activeSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -07003043 } else {
3044 time = 10000;
3045 }
3046 return time;
3047}
3048
3049uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs()
3050{
3051 uint32_t time;
Dima Zavinfce7a472011-04-19 22:30:36 -07003052 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent60e18242010-07-29 06:50:24 -07003053 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003054 } else {
3055 time = 10000;
3056 }
3057 return time;
3058}
3059
Eric Laurent25cbe0e2010-08-18 18:13:17 -07003060uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs()
3061{
3062 uint32_t time;
Dima Zavinfce7a472011-04-19 22:30:36 -07003063 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent25cbe0e2010-08-18 18:13:17 -07003064 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
3065 } else {
3066 time = 10000;
3067 }
3068 return time;
3069}
3070
3071
Mathias Agopian65ab4712010-07-14 17:59:35 -07003072// ----------------------------------------------------------------------------
3073
Glenn Kasten23bb8be2012-01-26 10:38:26 -08003074AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003075 AudioFlinger::MixerThread* mainThread, audio_io_handle_t id)
Glenn Kasten23bb8be2012-01-26 10:38:26 -08003076 : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device(), DUPLICATING),
3077 mWaitTimeMs(UINT_MAX)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003078{
Mathias Agopian65ab4712010-07-14 17:59:35 -07003079 addOutputTrack(mainThread);
3080}
3081
3082AudioFlinger::DuplicatingThread::~DuplicatingThread()
3083{
3084 for (size_t i = 0; i < mOutputTracks.size(); i++) {
3085 mOutputTracks[i]->destroy();
3086 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003087}
3088
3089bool AudioFlinger::DuplicatingThread::threadLoop()
3090{
3091 Vector< sp<Track> > tracksToRemove;
Glenn Kasten29c23c32012-01-26 13:37:52 -08003092 mixer_state mixerStatus = MIXER_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003093 nsecs_t standbyTime = systemTime();
3094 size_t mixBufferSize = mFrameCount*mFrameSize;
3095 SortedVector< sp<OutputTrack> > outputTracks;
3096 uint32_t writeFrames = 0;
3097 uint32_t activeSleepTime = activeSleepTimeUs();
3098 uint32_t idleSleepTime = idleSleepTimeUs();
3099 uint32_t sleepTime = idleSleepTime;
3100 Vector< sp<EffectChain> > effectChains;
3101
Eric Laurentfeb0db62011-07-22 09:04:31 -07003102 acquireWakeLock();
3103
Mathias Agopian65ab4712010-07-14 17:59:35 -07003104 while (!exitPending())
3105 {
3106 processConfigEvents();
3107
3108 mixerStatus = MIXER_IDLE;
3109 { // scope for the mLock
3110
3111 Mutex::Autolock _l(mLock);
3112
3113 if (checkForNewParameters_l()) {
3114 mixBufferSize = mFrameCount*mFrameSize;
3115 updateWaitTime();
3116 activeSleepTime = activeSleepTimeUs();
3117 idleSleepTime = idleSleepTimeUs();
3118 }
3119
3120 const SortedVector< wp<Track> >& activeTracks = mActiveTracks;
3121
3122 for (size_t i = 0; i < mOutputTracks.size(); i++) {
3123 outputTracks.add(mOutputTracks[i]);
3124 }
3125
3126 // put audio hardware into standby after short delay
Glenn Kastenf6b16782011-12-15 09:51:17 -08003127 if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) ||
3128 mSuspended)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003129 if (!mStandby) {
3130 for (size_t i = 0; i < outputTracks.size(); i++) {
3131 outputTracks[i]->stop();
3132 }
3133 mStandby = true;
3134 mBytesWritten = 0;
3135 }
3136
3137 if (!activeTracks.size() && mConfigEvents.isEmpty()) {
3138 // we're about to wait, flush the binder command buffer
3139 IPCThreadState::self()->flushCommands();
3140 outputTracks.clear();
3141
3142 if (exitPending()) break;
3143
Eric Laurentfeb0db62011-07-22 09:04:31 -07003144 releaseWakeLock_l();
Glenn Kasten90bebef2012-01-27 15:24:38 -08003145 ALOGV("DuplicatingThread %p TID %d going to sleep", this, gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003146 mWaitWorkCV.wait(mLock);
Glenn Kasten90bebef2012-01-27 15:24:38 -08003147 ALOGV("DuplicatingThread %p TID %d waking up", this, gettid());
Eric Laurentfeb0db62011-07-22 09:04:31 -07003148 acquireWakeLock_l();
3149
Eric Laurent27741442012-01-17 19:20:12 -08003150 mPrevMixerStatus = MIXER_IDLE;
Glenn Kastenf1d45922012-01-13 15:54:24 -08003151 if (!mMasterMute) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003152 char value[PROPERTY_VALUE_MAX];
3153 property_get("ro.audio.silent", value, "0");
3154 if (atoi(value)) {
Steve Blockb8a80522011-12-20 16:23:08 +00003155 ALOGD("Silence is golden");
Glenn Kasten6637baa2012-01-09 09:40:36 -08003156 setMasterMute_l(true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003157 }
3158 }
3159
John Grossman4ff14ba2012-02-08 16:37:41 -08003160 standbyTime = systemTime() + mStandbyTimeInNsecs;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003161 sleepTime = idleSleepTime;
3162 continue;
3163 }
3164 }
3165
3166 mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove);
3167
3168 // prevent any changes in effect chain list and in each effect chain
3169 // during mixing and effect process as the audio buffers could be deleted
3170 // or modified if an effect is created or deleted
Eric Laurentde070132010-07-13 04:45:46 -07003171 lockEffectChains_l(effectChains);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003172 }
3173
Glenn Kastenf6b16782011-12-15 09:51:17 -08003174 if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003175 // mix buffers...
3176 if (outputsReady(outputTracks)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08003177 mAudioMixer->process(AudioBufferProvider::kInvalidPTS);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003178 } else {
3179 memset(mMixBuffer, 0, mixBufferSize);
3180 }
3181 sleepTime = 0;
3182 writeFrames = mFrameCount;
3183 } else {
3184 if (sleepTime == 0) {
3185 if (mixerStatus == MIXER_TRACKS_ENABLED) {
3186 sleepTime = activeSleepTime;
3187 } else {
3188 sleepTime = idleSleepTime;
3189 }
3190 } else if (mBytesWritten != 0) {
3191 // flush remaining overflow buffers in output tracks
3192 for (size_t i = 0; i < outputTracks.size(); i++) {
3193 if (outputTracks[i]->isActive()) {
3194 sleepTime = 0;
3195 writeFrames = 0;
3196 memset(mMixBuffer, 0, mixBufferSize);
3197 break;
3198 }
3199 }
3200 }
3201 }
3202
3203 if (mSuspended) {
Eric Laurent25cbe0e2010-08-18 18:13:17 -07003204 sleepTime = suspendSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -07003205 }
3206 // sleepTime == 0 means we must write to audio hardware
3207 if (sleepTime == 0) {
3208 for (size_t i = 0; i < effectChains.size(); i ++) {
3209 effectChains[i]->process_l();
3210 }
3211 // enable changes in effect chain
Eric Laurentde070132010-07-13 04:45:46 -07003212 unlockEffectChains(effectChains);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003213
John Grossman4ff14ba2012-02-08 16:37:41 -08003214 standbyTime = systemTime() + mStandbyTimeInNsecs;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003215 for (size_t i = 0; i < outputTracks.size(); i++) {
3216 outputTracks[i]->write(mMixBuffer, writeFrames);
3217 }
3218 mStandby = false;
3219 mBytesWritten += mixBufferSize;
3220 } else {
3221 // enable changes in effect chain
Eric Laurentde070132010-07-13 04:45:46 -07003222 unlockEffectChains(effectChains);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003223 usleep(sleepTime);
3224 }
3225
3226 // finally let go of all our tracks, without the lock held
3227 // since we can't guarantee the destructors won't acquire that
3228 // same lock.
3229 tracksToRemove.clear();
3230 outputTracks.clear();
3231
3232 // Effect chains will be actually deleted here if they were removed from
3233 // mEffectChains list during mixing or effects processing
3234 effectChains.clear();
3235 }
3236
Eric Laurentfeb0db62011-07-22 09:04:31 -07003237 releaseWakeLock();
3238
Mathias Agopian65ab4712010-07-14 17:59:35 -07003239 return false;
3240}
3241
3242void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread)
3243{
Glenn Kasten99e53b82012-01-19 08:59:58 -08003244 // FIXME explain this formula
Mathias Agopian65ab4712010-07-14 17:59:35 -07003245 int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate();
Glenn Kasten9eaa5572012-01-20 13:32:16 -08003246 OutputTrack *outputTrack = new OutputTrack(thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003247 this,
3248 mSampleRate,
3249 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07003250 mChannelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003251 frameCount);
3252 if (outputTrack->cblk() != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -07003253 thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003254 mOutputTracks.add(outputTrack);
Steve Block3856b092011-10-20 11:56:00 +01003255 ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003256 updateWaitTime();
3257 }
3258}
3259
3260void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
3261{
3262 Mutex::Autolock _l(mLock);
3263 for (size_t i = 0; i < mOutputTracks.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -08003264 if (mOutputTracks[i]->thread() == thread) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003265 mOutputTracks[i]->destroy();
3266 mOutputTracks.removeAt(i);
3267 updateWaitTime();
3268 return;
3269 }
3270 }
Steve Block3856b092011-10-20 11:56:00 +01003271 ALOGV("removeOutputTrack(): unkonwn thread: %p", thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003272}
3273
3274void AudioFlinger::DuplicatingThread::updateWaitTime()
3275{
3276 mWaitTimeMs = UINT_MAX;
3277 for (size_t i = 0; i < mOutputTracks.size(); i++) {
3278 sp<ThreadBase> strong = mOutputTracks[i]->thread().promote();
Glenn Kasten7378ca52012-01-20 13:44:40 -08003279 if (strong != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003280 uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate();
3281 if (waitTimeMs < mWaitTimeMs) {
3282 mWaitTimeMs = waitTimeMs;
3283 }
3284 }
3285 }
3286}
3287
3288
3289bool AudioFlinger::DuplicatingThread::outputsReady(SortedVector< sp<OutputTrack> > &outputTracks)
3290{
3291 for (size_t i = 0; i < outputTracks.size(); i++) {
3292 sp <ThreadBase> thread = outputTracks[i]->thread().promote();
3293 if (thread == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003294 ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003295 return false;
3296 }
3297 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3298 if (playbackThread->standby() && !playbackThread->isSuspended()) {
Steve Block3856b092011-10-20 11:56:00 +01003299 ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003300 return false;
3301 }
3302 }
3303 return true;
3304}
3305
3306uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs()
3307{
3308 return (mWaitTimeMs * 1000) / 2;
3309}
3310
3311// ----------------------------------------------------------------------------
3312
3313// TrackBase constructor must be called with AudioFlinger::mLock held
3314AudioFlinger::ThreadBase::TrackBase::TrackBase(
Glenn Kasten9eaa5572012-01-20 13:32:16 -08003315 ThreadBase *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003316 const sp<Client>& client,
3317 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08003318 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07003319 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003320 int frameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003321 const sp<IMemory>& sharedBuffer,
3322 int sessionId)
3323 : RefBase(),
3324 mThread(thread),
3325 mClient(client),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08003326 mCblk(NULL),
3327 // mBuffer
3328 // mBufferEnd
Mathias Agopian65ab4712010-07-14 17:59:35 -07003329 mFrameCount(0),
3330 mState(IDLE),
Mathias Agopian65ab4712010-07-14 17:59:35 -07003331 mFormat(format),
Glenn Kasten5cf034d2012-02-21 10:35:56 -08003332 mStepServerFailed(false),
Mathias Agopian65ab4712010-07-14 17:59:35 -07003333 mSessionId(sessionId)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08003334 // mChannelCount
3335 // mChannelMask
Mathias Agopian65ab4712010-07-14 17:59:35 -07003336{
Steve Block3856b092011-10-20 11:56:00 +01003337 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003338
Steve Blockb8a80522011-12-20 16:23:08 +00003339 // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003340 size_t size = sizeof(audio_track_cblk_t);
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07003341 uint8_t channelCount = popcount(channelMask);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003342 size_t bufferSize = frameCount*channelCount*sizeof(int16_t);
3343 if (sharedBuffer == 0) {
3344 size += bufferSize;
3345 }
3346
3347 if (client != NULL) {
3348 mCblkMemory = client->heap()->allocate(size);
3349 if (mCblkMemory != 0) {
3350 mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer());
Glenn Kastena0d68332012-01-27 16:47:15 -08003351 if (mCblk != NULL) { // construct the shared structure in-place.
Mathias Agopian65ab4712010-07-14 17:59:35 -07003352 new(mCblk) audio_track_cblk_t();
3353 // clear all buffers
3354 mCblk->frameCount = frameCount;
3355 mCblk->sampleRate = sampleRate;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07003356 mChannelCount = channelCount;
3357 mChannelMask = channelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003358 if (sharedBuffer == 0) {
3359 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
3360 memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
3361 // Force underrun condition to avoid false underrun callback until first data is
Eric Laurent44d98482010-09-30 16:12:31 -07003362 // written to buffer (other flags are cleared)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003363 mCblk->flags = CBLK_UNDERRUN_ON;
3364 } else {
3365 mBuffer = sharedBuffer->pointer();
3366 }
3367 mBufferEnd = (uint8_t *)mBuffer + bufferSize;
3368 }
3369 } else {
Steve Block29357bc2012-01-06 19:20:56 +00003370 ALOGE("not enough memory for AudioTrack size=%u", size);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003371 client->heap()->dump("AudioTrack");
3372 return;
3373 }
3374 } else {
3375 mCblk = (audio_track_cblk_t *)(new uint8_t[size]);
Glenn Kasten4a6f0282012-01-06 15:03:11 -08003376 // construct the shared structure in-place.
Mathias Agopian65ab4712010-07-14 17:59:35 -07003377 new(mCblk) audio_track_cblk_t();
3378 // clear all buffers
3379 mCblk->frameCount = frameCount;
3380 mCblk->sampleRate = sampleRate;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07003381 mChannelCount = channelCount;
3382 mChannelMask = channelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003383 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
3384 memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
3385 // Force underrun condition to avoid false underrun callback until first data is
Eric Laurent44d98482010-09-30 16:12:31 -07003386 // written to buffer (other flags are cleared)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003387 mCblk->flags = CBLK_UNDERRUN_ON;
3388 mBufferEnd = (uint8_t *)mBuffer + bufferSize;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003389 }
3390}
3391
3392AudioFlinger::ThreadBase::TrackBase::~TrackBase()
3393{
Glenn Kastena0d68332012-01-27 16:47:15 -08003394 if (mCblk != NULL) {
Glenn Kasten1a0ae5b2012-02-03 10:24:48 -08003395 if (mClient == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003396 delete mCblk;
Glenn Kasten1a0ae5b2012-02-03 10:24:48 -08003397 } else {
3398 mCblk->~audio_track_cblk_t(); // destroy our shared-structure.
Mathias Agopian65ab4712010-07-14 17:59:35 -07003399 }
3400 }
Glenn Kastendbfafaf2012-01-25 15:27:15 -08003401 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Glenn Kasten7378ca52012-01-20 13:44:40 -08003402 if (mClient != 0) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08003403 // Client destructor must run with AudioFlinger mutex locked
Mathias Agopian65ab4712010-07-14 17:59:35 -07003404 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
Glenn Kasten7378ca52012-01-20 13:44:40 -08003405 // If the client's reference count drops to zero, the associated destructor
3406 // must run with AudioFlinger lock held. Thus the explicit clear() rather than
3407 // relying on the automatic clear() at end of scope.
Mathias Agopian65ab4712010-07-14 17:59:35 -07003408 mClient.clear();
3409 }
3410}
3411
3412void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer)
3413{
Glenn Kastene0feee32011-12-13 11:53:26 -08003414 buffer->raw = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003415 mFrameCount = buffer->frameCount;
3416 step();
3417 buffer->frameCount = 0;
3418}
3419
3420bool AudioFlinger::ThreadBase::TrackBase::step() {
3421 bool result;
3422 audio_track_cblk_t* cblk = this->cblk();
3423
3424 result = cblk->stepServer(mFrameCount);
3425 if (!result) {
Steve Block3856b092011-10-20 11:56:00 +01003426 ALOGV("stepServer failed acquiring cblk mutex");
Glenn Kasten5cf034d2012-02-21 10:35:56 -08003427 mStepServerFailed = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003428 }
3429 return result;
3430}
3431
3432void AudioFlinger::ThreadBase::TrackBase::reset() {
3433 audio_track_cblk_t* cblk = this->cblk();
3434
3435 cblk->user = 0;
3436 cblk->server = 0;
3437 cblk->userBase = 0;
3438 cblk->serverBase = 0;
Glenn Kasten5cf034d2012-02-21 10:35:56 -08003439 mStepServerFailed = false;
Steve Block3856b092011-10-20 11:56:00 +01003440 ALOGV("TrackBase::reset");
Mathias Agopian65ab4712010-07-14 17:59:35 -07003441}
3442
Mathias Agopian65ab4712010-07-14 17:59:35 -07003443int AudioFlinger::ThreadBase::TrackBase::sampleRate() const {
3444 return (int)mCblk->sampleRate;
3445}
3446
Mathias Agopian65ab4712010-07-14 17:59:35 -07003447void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const {
3448 audio_track_cblk_t* cblk = this->cblk();
Glenn Kastenb9980652012-01-11 09:48:27 -08003449 size_t frameSize = cblk->frameSize;
3450 int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize;
3451 int8_t *bufferEnd = bufferStart + frames * frameSize;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003452
3453 // Check validity of returned pointer in case the track control block would have been corrupted.
3454 if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd ||
Glenn Kastenb9980652012-01-11 09:48:27 -08003455 ((unsigned long)bufferStart & (unsigned long)(frameSize - 1))) {
Steve Block29357bc2012-01-06 19:20:56 +00003456 ALOGE("TrackBase::getBuffer buffer out of range:\n start: %p, end %p , mBuffer %p mBufferEnd %p\n \
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07003457 server %d, serverBase %d, user %d, userBase %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07003458 bufferStart, bufferEnd, mBuffer, mBufferEnd,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07003459 cblk->server, cblk->serverBase, cblk->user, cblk->userBase);
Glenn Kastena0d68332012-01-27 16:47:15 -08003460 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003461 }
3462
3463 return bufferStart;
3464}
3465
3466// ----------------------------------------------------------------------------
3467
3468// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
3469AudioFlinger::PlaybackThread::Track::Track(
Glenn Kasten9eaa5572012-01-20 13:32:16 -08003470 PlaybackThread *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003471 const sp<Client>& client,
Glenn Kastenfff6d712012-01-12 16:38:12 -08003472 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003473 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08003474 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07003475 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003476 int frameCount,
3477 const sp<IMemory>& sharedBuffer,
3478 int sessionId)
Glenn Kasten5cf034d2012-02-21 10:35:56 -08003479 : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer, sessionId),
Eric Laurent8f45bd72010-08-31 13:50:07 -07003480 mMute(false), mSharedBuffer(sharedBuffer), mName(-1), mMainBuffer(NULL), mAuxBuffer(NULL),
3481 mAuxEffectId(0), mHasVolumeController(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003482{
3483 if (mCblk != NULL) {
Glenn Kasten9eaa5572012-01-20 13:32:16 -08003484 if (thread != NULL) {
3485 mName = thread->getTrackName_l();
3486 mMainBuffer = thread->mixBuffer();
Mathias Agopian65ab4712010-07-14 17:59:35 -07003487 }
Glenn Kasten23d82a92012-02-03 11:10:00 -08003488 ALOGV("Track constructor name %d, calling pid %d", mName, IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003489 if (mName < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00003490 ALOGE("no more track names available");
Mathias Agopian65ab4712010-07-14 17:59:35 -07003491 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003492 mStreamType = streamType;
3493 // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of
3494 // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack
Eric Laurentedc15ad2011-07-21 19:35:01 -07003495 mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003496 }
3497}
3498
3499AudioFlinger::PlaybackThread::Track::~Track()
3500{
Steve Block3856b092011-10-20 11:56:00 +01003501 ALOGV("PlaybackThread::Track destructor");
Mathias Agopian65ab4712010-07-14 17:59:35 -07003502 sp<ThreadBase> thread = mThread.promote();
3503 if (thread != 0) {
3504 Mutex::Autolock _l(thread->mLock);
3505 mState = TERMINATED;
3506 }
3507}
3508
3509void AudioFlinger::PlaybackThread::Track::destroy()
3510{
3511 // NOTE: destroyTrack_l() can remove a strong reference to this Track
3512 // by removing it from mTracks vector, so there is a risk that this Tracks's
Glenn Kasten99e53b82012-01-19 08:59:58 -08003513 // destructor is called. As the destructor needs to lock mLock,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003514 // we must acquire a strong reference on this Track before locking mLock
3515 // here so that the destructor is called only when exiting this function.
3516 // On the other hand, as long as Track::destroy() is only called by
3517 // TrackHandle destructor, the TrackHandle still holds a strong ref on
3518 // this Track with its member mTrack.
3519 sp<Track> keep(this);
3520 { // scope for mLock
3521 sp<ThreadBase> thread = mThread.promote();
3522 if (thread != 0) {
3523 if (!isOutputTrack()) {
3524 if (mState == ACTIVE || mState == RESUMING) {
Glenn Kasten02bbd202012-02-08 12:35:35 -08003525 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
Gloria Wang9ee159b2011-02-24 14:51:45 -08003526
3527 // to track the speaker usage
3528 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003529 }
3530 AudioSystem::releaseOutput(thread->id());
3531 }
3532 Mutex::Autolock _l(thread->mLock);
3533 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3534 playbackThread->destroyTrack_l(this);
3535 }
3536 }
3537}
3538
3539void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size)
3540{
Glenn Kasten83d86532012-01-17 14:39:34 -08003541 uint32_t vlr = mCblk->getVolumeLR();
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07003542 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",
Mathias Agopian65ab4712010-07-14 17:59:35 -07003543 mName - AudioMixer::TRACK0,
Glenn Kasten44deb052012-02-05 18:09:08 -08003544 (mClient == 0) ? getpid_cached : mClient->pid(),
Mathias Agopian65ab4712010-07-14 17:59:35 -07003545 mStreamType,
3546 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07003547 mChannelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003548 mSessionId,
3549 mFrameCount,
3550 mState,
3551 mMute,
3552 mFillingUpStatus,
3553 mCblk->sampleRate,
Glenn Kastenb1cf75c2012-01-17 12:20:54 -08003554 vlr & 0xFFFF,
3555 vlr >> 16,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003556 mCblk->server,
3557 mCblk->user,
3558 (int)mMainBuffer,
3559 (int)mAuxBuffer);
3560}
3561
John Grossman4ff14ba2012-02-08 16:37:41 -08003562status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(
3563 AudioBufferProvider::Buffer* buffer, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003564{
3565 audio_track_cblk_t* cblk = this->cblk();
3566 uint32_t framesReady;
3567 uint32_t framesReq = buffer->frameCount;
3568
3569 // Check if last stepServer failed, try to step now
Glenn Kasten5cf034d2012-02-21 10:35:56 -08003570 if (mStepServerFailed) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003571 if (!step()) goto getNextBuffer_exit;
Steve Block3856b092011-10-20 11:56:00 +01003572 ALOGV("stepServer recovered");
Glenn Kasten5cf034d2012-02-21 10:35:56 -08003573 mStepServerFailed = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003574 }
3575
3576 framesReady = cblk->framesReady();
3577
Glenn Kastenf6b16782011-12-15 09:51:17 -08003578 if (CC_LIKELY(framesReady)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003579 uint32_t s = cblk->server;
3580 uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
3581
3582 bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd;
3583 if (framesReq > framesReady) {
3584 framesReq = framesReady;
3585 }
3586 if (s + framesReq > bufferEnd) {
3587 framesReq = bufferEnd - s;
3588 }
3589
3590 buffer->raw = getBuffer(s, framesReq);
Glenn Kastene0feee32011-12-13 11:53:26 -08003591 if (buffer->raw == NULL) goto getNextBuffer_exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003592
3593 buffer->frameCount = framesReq;
3594 return NO_ERROR;
3595 }
3596
3597getNextBuffer_exit:
Glenn Kastene0feee32011-12-13 11:53:26 -08003598 buffer->raw = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003599 buffer->frameCount = 0;
Steve Block3856b092011-10-20 11:56:00 +01003600 ALOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003601 return NOT_ENOUGH_DATA;
3602}
3603
John Grossman4ff14ba2012-02-08 16:37:41 -08003604uint32_t AudioFlinger::PlaybackThread::Track::framesReady() const{
3605 return mCblk->framesReady();
3606}
3607
Mathias Agopian65ab4712010-07-14 17:59:35 -07003608bool AudioFlinger::PlaybackThread::Track::isReady() const {
Eric Laurentaf59ce22010-10-05 14:41:42 -07003609 if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003610
John Grossman4ff14ba2012-02-08 16:37:41 -08003611 if (framesReady() >= mCblk->frameCount ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07003612 (mCblk->flags & CBLK_FORCEREADY_MSK)) {
3613 mFillingUpStatus = FS_FILLED;
Eric Laurent38ccae22011-03-28 18:37:07 -07003614 android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003615 return true;
3616 }
3617 return false;
3618}
3619
Glenn Kasten6dbc1352012-02-02 10:56:47 -08003620status_t AudioFlinger::PlaybackThread::Track::start(pid_t tid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003621{
3622 status_t status = NO_ERROR;
Glenn Kasten6dbc1352012-02-02 10:56:47 -08003623 ALOGV("start(%d), calling pid %d session %d tid %d",
3624 mName, IPCThreadState::self()->getCallingPid(), mSessionId, tid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003625 sp<ThreadBase> thread = mThread.promote();
3626 if (thread != 0) {
3627 Mutex::Autolock _l(thread->mLock);
Glenn Kastenb853e982012-01-26 13:39:18 -08003628 track_state state = mState;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003629 // here the track could be either new, or restarted
3630 // in both cases "unstop" the track
3631 if (mState == PAUSED) {
3632 mState = TrackBase::RESUMING;
Steve Block3856b092011-10-20 11:56:00 +01003633 ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003634 } else {
3635 mState = TrackBase::ACTIVE;
Steve Block3856b092011-10-20 11:56:00 +01003636 ALOGV("? => ACTIVE (%d) on thread %p", mName, this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003637 }
3638
3639 if (!isOutputTrack() && state != ACTIVE && state != RESUMING) {
3640 thread->mLock.unlock();
Glenn Kasten02bbd202012-02-08 12:35:35 -08003641 status = AudioSystem::startOutput(thread->id(), mStreamType, mSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003642 thread->mLock.lock();
Gloria Wang9ee159b2011-02-24 14:51:45 -08003643
3644 // to track the speaker usage
3645 if (status == NO_ERROR) {
3646 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart);
3647 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003648 }
3649 if (status == NO_ERROR) {
3650 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3651 playbackThread->addTrack_l(this);
3652 } else {
3653 mState = state;
3654 }
3655 } else {
3656 status = BAD_VALUE;
3657 }
3658 return status;
3659}
3660
3661void AudioFlinger::PlaybackThread::Track::stop()
3662{
Glenn Kasten23d82a92012-02-03 11:10:00 -08003663 ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003664 sp<ThreadBase> thread = mThread.promote();
3665 if (thread != 0) {
3666 Mutex::Autolock _l(thread->mLock);
Glenn Kastenb853e982012-01-26 13:39:18 -08003667 track_state state = mState;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003668 if (mState > STOPPED) {
3669 mState = STOPPED;
3670 // If the track is not active (PAUSED and buffers full), flush buffers
3671 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3672 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
3673 reset();
3674 }
Steve Block3856b092011-10-20 11:56:00 +01003675 ALOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003676 }
3677 if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) {
3678 thread->mLock.unlock();
Glenn Kasten02bbd202012-02-08 12:35:35 -08003679 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003680 thread->mLock.lock();
Gloria Wang9ee159b2011-02-24 14:51:45 -08003681
3682 // to track the speaker usage
3683 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003684 }
3685 }
3686}
3687
3688void AudioFlinger::PlaybackThread::Track::pause()
3689{
Glenn Kasten23d82a92012-02-03 11:10:00 -08003690 ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003691 sp<ThreadBase> thread = mThread.promote();
3692 if (thread != 0) {
3693 Mutex::Autolock _l(thread->mLock);
3694 if (mState == ACTIVE || mState == RESUMING) {
3695 mState = PAUSING;
Steve Block3856b092011-10-20 11:56:00 +01003696 ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003697 if (!isOutputTrack()) {
3698 thread->mLock.unlock();
Glenn Kasten02bbd202012-02-08 12:35:35 -08003699 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003700 thread->mLock.lock();
Gloria Wang9ee159b2011-02-24 14:51:45 -08003701
3702 // to track the speaker usage
3703 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003704 }
3705 }
3706 }
3707}
3708
3709void AudioFlinger::PlaybackThread::Track::flush()
3710{
Steve Block3856b092011-10-20 11:56:00 +01003711 ALOGV("flush(%d)", mName);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003712 sp<ThreadBase> thread = mThread.promote();
3713 if (thread != 0) {
3714 Mutex::Autolock _l(thread->mLock);
3715 if (mState != STOPPED && mState != PAUSED && mState != PAUSING) {
3716 return;
3717 }
3718 // No point remaining in PAUSED state after a flush => go to
3719 // STOPPED state
3720 mState = STOPPED;
3721
Eric Laurent38ccae22011-03-28 18:37:07 -07003722 // do not reset the track if it is still in the process of being stopped or paused.
3723 // this will be done by prepareTracks_l() when the track is stopped.
3724 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3725 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
3726 reset();
3727 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003728 }
3729}
3730
3731void AudioFlinger::PlaybackThread::Track::reset()
3732{
3733 // Do not reset twice to avoid discarding data written just after a flush and before
3734 // the audioflinger thread detects the track is stopped.
3735 if (!mResetDone) {
3736 TrackBase::reset();
3737 // Force underrun condition to avoid false underrun callback until first data is
3738 // written to buffer
Eric Laurent38ccae22011-03-28 18:37:07 -07003739 android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags);
3740 android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003741 mFillingUpStatus = FS_FILLING;
3742 mResetDone = true;
3743 }
3744}
3745
3746void AudioFlinger::PlaybackThread::Track::mute(bool muted)
3747{
3748 mMute = muted;
3749}
3750
Mathias Agopian65ab4712010-07-14 17:59:35 -07003751status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId)
3752{
3753 status_t status = DEAD_OBJECT;
3754 sp<ThreadBase> thread = mThread.promote();
3755 if (thread != 0) {
3756 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3757 status = playbackThread->attachAuxEffect(this, EffectId);
3758 }
3759 return status;
3760}
3761
3762void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer)
3763{
3764 mAuxEffectId = EffectId;
3765 mAuxBuffer = buffer;
3766}
3767
John Grossman4ff14ba2012-02-08 16:37:41 -08003768// timed audio tracks
3769
3770sp<AudioFlinger::PlaybackThread::TimedTrack>
3771AudioFlinger::PlaybackThread::TimedTrack::create(
Glenn Kasten9eaa5572012-01-20 13:32:16 -08003772 PlaybackThread *thread,
John Grossman4ff14ba2012-02-08 16:37:41 -08003773 const sp<Client>& client,
3774 audio_stream_type_t streamType,
3775 uint32_t sampleRate,
3776 audio_format_t format,
3777 uint32_t channelMask,
3778 int frameCount,
3779 const sp<IMemory>& sharedBuffer,
3780 int sessionId) {
3781 if (!client->reserveTimedTrack())
3782 return NULL;
3783
3784 sp<TimedTrack> track = new TimedTrack(
3785 thread, client, streamType, sampleRate, format, channelMask, frameCount,
3786 sharedBuffer, sessionId);
3787
3788 if (track == NULL) {
3789 client->releaseTimedTrack();
3790 return NULL;
3791 }
3792
3793 return track;
3794}
3795
3796AudioFlinger::PlaybackThread::TimedTrack::TimedTrack(
Glenn Kasten9eaa5572012-01-20 13:32:16 -08003797 PlaybackThread *thread,
John Grossman4ff14ba2012-02-08 16:37:41 -08003798 const sp<Client>& client,
3799 audio_stream_type_t streamType,
3800 uint32_t sampleRate,
3801 audio_format_t format,
3802 uint32_t channelMask,
3803 int frameCount,
3804 const sp<IMemory>& sharedBuffer,
3805 int sessionId)
3806 : Track(thread, client, streamType, sampleRate, format, channelMask,
3807 frameCount, sharedBuffer, sessionId),
3808 mTimedSilenceBuffer(NULL),
3809 mTimedSilenceBufferSize(0),
3810 mTimedAudioOutputOnTime(false),
3811 mMediaTimeTransformValid(false)
3812{
3813 LocalClock lc;
3814 mLocalTimeFreq = lc.getLocalFreq();
3815
3816 mLocalTimeToSampleTransform.a_zero = 0;
3817 mLocalTimeToSampleTransform.b_zero = 0;
3818 mLocalTimeToSampleTransform.a_to_b_numer = sampleRate;
3819 mLocalTimeToSampleTransform.a_to_b_denom = mLocalTimeFreq;
3820 LinearTransform::reduce(&mLocalTimeToSampleTransform.a_to_b_numer,
3821 &mLocalTimeToSampleTransform.a_to_b_denom);
3822}
3823
3824AudioFlinger::PlaybackThread::TimedTrack::~TimedTrack() {
3825 mClient->releaseTimedTrack();
3826 delete [] mTimedSilenceBuffer;
3827}
3828
3829status_t AudioFlinger::PlaybackThread::TimedTrack::allocateTimedBuffer(
3830 size_t size, sp<IMemory>* buffer) {
3831
3832 Mutex::Autolock _l(mTimedBufferQueueLock);
3833
3834 trimTimedBufferQueue_l();
3835
3836 // lazily initialize the shared memory heap for timed buffers
3837 if (mTimedMemoryDealer == NULL) {
3838 const int kTimedBufferHeapSize = 512 << 10;
3839
3840 mTimedMemoryDealer = new MemoryDealer(kTimedBufferHeapSize,
3841 "AudioFlingerTimed");
3842 if (mTimedMemoryDealer == NULL)
3843 return NO_MEMORY;
3844 }
3845
3846 sp<IMemory> newBuffer = mTimedMemoryDealer->allocate(size);
3847 if (newBuffer == NULL) {
3848 newBuffer = mTimedMemoryDealer->allocate(size);
3849 if (newBuffer == NULL)
3850 return NO_MEMORY;
3851 }
3852
3853 *buffer = newBuffer;
3854 return NO_ERROR;
3855}
3856
3857// caller must hold mTimedBufferQueueLock
3858void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() {
3859 int64_t mediaTimeNow;
3860 {
3861 Mutex::Autolock mttLock(mMediaTimeTransformLock);
3862 if (!mMediaTimeTransformValid)
3863 return;
3864
3865 int64_t targetTimeNow;
3866 status_t res = (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME)
3867 ? mCCHelper.getCommonTime(&targetTimeNow)
3868 : mCCHelper.getLocalTime(&targetTimeNow);
3869
3870 if (OK != res)
3871 return;
3872
3873 if (!mMediaTimeTransform.doReverseTransform(targetTimeNow,
3874 &mediaTimeNow)) {
3875 return;
3876 }
3877 }
3878
3879 size_t trimIndex;
3880 for (trimIndex = 0; trimIndex < mTimedBufferQueue.size(); trimIndex++) {
3881 if (mTimedBufferQueue[trimIndex].pts() > mediaTimeNow)
3882 break;
3883 }
3884
3885 if (trimIndex) {
3886 mTimedBufferQueue.removeItemsAt(0, trimIndex);
3887 }
3888}
3889
3890status_t AudioFlinger::PlaybackThread::TimedTrack::queueTimedBuffer(
3891 const sp<IMemory>& buffer, int64_t pts) {
3892
3893 {
3894 Mutex::Autolock mttLock(mMediaTimeTransformLock);
3895 if (!mMediaTimeTransformValid)
3896 return INVALID_OPERATION;
3897 }
3898
3899 Mutex::Autolock _l(mTimedBufferQueueLock);
3900
3901 mTimedBufferQueue.add(TimedBuffer(buffer, pts));
3902
3903 return NO_ERROR;
3904}
3905
3906status_t AudioFlinger::PlaybackThread::TimedTrack::setMediaTimeTransform(
3907 const LinearTransform& xform, TimedAudioTrack::TargetTimeline target) {
3908
3909 ALOGV("%s az=%lld bz=%lld n=%d d=%u tgt=%d", __PRETTY_FUNCTION__,
3910 xform.a_zero, xform.b_zero, xform.a_to_b_numer, xform.a_to_b_denom,
3911 target);
3912
3913 if (!(target == TimedAudioTrack::LOCAL_TIME ||
3914 target == TimedAudioTrack::COMMON_TIME)) {
3915 return BAD_VALUE;
3916 }
3917
3918 Mutex::Autolock lock(mMediaTimeTransformLock);
3919 mMediaTimeTransform = xform;
3920 mMediaTimeTransformTarget = target;
3921 mMediaTimeTransformValid = true;
3922
3923 return NO_ERROR;
3924}
3925
3926#define min(a, b) ((a) < (b) ? (a) : (b))
3927
3928// implementation of getNextBuffer for tracks whose buffers have timestamps
3929status_t AudioFlinger::PlaybackThread::TimedTrack::getNextBuffer(
3930 AudioBufferProvider::Buffer* buffer, int64_t pts)
3931{
3932 if (pts == AudioBufferProvider::kInvalidPTS) {
3933 buffer->raw = 0;
3934 buffer->frameCount = 0;
3935 return INVALID_OPERATION;
3936 }
3937
John Grossman4ff14ba2012-02-08 16:37:41 -08003938 Mutex::Autolock _l(mTimedBufferQueueLock);
3939
3940 while (true) {
3941
3942 // if we have no timed buffers, then fail
3943 if (mTimedBufferQueue.isEmpty()) {
3944 buffer->raw = 0;
3945 buffer->frameCount = 0;
3946 return NOT_ENOUGH_DATA;
3947 }
3948
3949 TimedBuffer& head = mTimedBufferQueue.editItemAt(0);
3950
3951 // calculate the PTS of the head of the timed buffer queue expressed in
3952 // local time
3953 int64_t headLocalPTS;
3954 {
3955 Mutex::Autolock mttLock(mMediaTimeTransformLock);
3956
3957 assert(mMediaTimeTransformValid);
3958
3959 if (mMediaTimeTransform.a_to_b_denom == 0) {
3960 // the transform represents a pause, so yield silence
3961 timedYieldSilence(buffer->frameCount, buffer);
3962 return NO_ERROR;
3963 }
3964
3965 int64_t transformedPTS;
3966 if (!mMediaTimeTransform.doForwardTransform(head.pts(),
3967 &transformedPTS)) {
3968 // the transform failed. this shouldn't happen, but if it does
3969 // then just drop this buffer
3970 ALOGW("timedGetNextBuffer transform failed");
3971 buffer->raw = 0;
3972 buffer->frameCount = 0;
3973 mTimedBufferQueue.removeAt(0);
3974 return NO_ERROR;
3975 }
3976
3977 if (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) {
3978 if (OK != mCCHelper.commonTimeToLocalTime(transformedPTS,
3979 &headLocalPTS)) {
3980 buffer->raw = 0;
3981 buffer->frameCount = 0;
3982 return INVALID_OPERATION;
3983 }
3984 } else {
3985 headLocalPTS = transformedPTS;
3986 }
3987 }
3988
3989 // adjust the head buffer's PTS to reflect the portion of the head buffer
3990 // that has already been consumed
3991 int64_t effectivePTS = headLocalPTS +
3992 ((head.position() / mCblk->frameSize) * mLocalTimeFreq / sampleRate());
3993
3994 // Calculate the delta in samples between the head of the input buffer
3995 // queue and the start of the next output buffer that will be written.
3996 // If the transformation fails because of over or underflow, it means
3997 // that the sample's position in the output stream is so far out of
3998 // whack that it should just be dropped.
3999 int64_t sampleDelta;
4000 if (llabs(effectivePTS - pts) >= (static_cast<int64_t>(1) << 31)) {
4001 ALOGV("*** head buffer is too far from PTS: dropped buffer");
4002 mTimedBufferQueue.removeAt(0);
4003 continue;
4004 }
4005 if (!mLocalTimeToSampleTransform.doForwardTransform(
4006 (effectivePTS - pts) << 32, &sampleDelta)) {
4007 ALOGV("*** too late during sample rate transform: dropped buffer");
4008 mTimedBufferQueue.removeAt(0);
4009 continue;
4010 }
4011
4012 ALOGV("*** %s head.pts=%lld head.pos=%d pts=%lld sampleDelta=[%d.%08x]",
4013 __PRETTY_FUNCTION__, head.pts(), head.position(), pts,
4014 static_cast<int32_t>((sampleDelta >= 0 ? 0 : 1) + (sampleDelta >> 32)),
4015 static_cast<uint32_t>(sampleDelta & 0xFFFFFFFF));
4016
4017 // if the delta between the ideal placement for the next input sample and
4018 // the current output position is within this threshold, then we will
4019 // concatenate the next input samples to the previous output
4020 const int64_t kSampleContinuityThreshold =
4021 (static_cast<int64_t>(sampleRate()) << 32) / 10;
4022
4023 // if this is the first buffer of audio that we're emitting from this track
4024 // then it should be almost exactly on time.
4025 const int64_t kSampleStartupThreshold = 1LL << 32;
4026
4027 if ((mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleContinuityThreshold) ||
4028 (!mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleStartupThreshold)) {
4029 // the next input is close enough to being on time, so concatenate it
4030 // with the last output
4031 timedYieldSamples(buffer);
4032
4033 ALOGV("*** on time: head.pos=%d frameCount=%u", head.position(), buffer->frameCount);
4034 return NO_ERROR;
4035 } else if (sampleDelta > 0) {
4036 // the gap between the current output position and the proper start of
4037 // the next input sample is too big, so fill it with silence
4038 uint32_t framesUntilNextInput = (sampleDelta + 0x80000000) >> 32;
4039
4040 timedYieldSilence(framesUntilNextInput, buffer);
4041 ALOGV("*** silence: frameCount=%u", buffer->frameCount);
4042 return NO_ERROR;
4043 } else {
4044 // the next input sample is late
4045 uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32));
4046 size_t onTimeSamplePosition =
4047 head.position() + lateFrames * mCblk->frameSize;
4048
4049 if (onTimeSamplePosition > head.buffer()->size()) {
4050 // all the remaining samples in the head are too late, so
4051 // drop it and move on
4052 ALOGV("*** too late: dropped buffer");
4053 mTimedBufferQueue.removeAt(0);
4054 continue;
4055 } else {
4056 // skip over the late samples
4057 head.setPosition(onTimeSamplePosition);
4058
4059 // yield the available samples
4060 timedYieldSamples(buffer);
4061
4062 ALOGV("*** late: head.pos=%d frameCount=%u", head.position(), buffer->frameCount);
4063 return NO_ERROR;
4064 }
4065 }
4066 }
4067}
4068
4069// Yield samples from the timed buffer queue head up to the given output
4070// buffer's capacity.
4071//
4072// Caller must hold mTimedBufferQueueLock
4073void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSamples(
4074 AudioBufferProvider::Buffer* buffer) {
4075
4076 const TimedBuffer& head = mTimedBufferQueue[0];
4077
4078 buffer->raw = (static_cast<uint8_t*>(head.buffer()->pointer()) +
4079 head.position());
4080
4081 uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) /
4082 mCblk->frameSize);
4083 size_t framesRequested = buffer->frameCount;
4084 buffer->frameCount = min(framesLeftInHead, framesRequested);
4085
4086 mTimedAudioOutputOnTime = true;
4087}
4088
4089// Yield samples of silence up to the given output buffer's capacity
4090//
4091// Caller must hold mTimedBufferQueueLock
4092void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSilence(
4093 uint32_t numFrames, AudioBufferProvider::Buffer* buffer) {
4094
4095 // lazily allocate a buffer filled with silence
4096 if (mTimedSilenceBufferSize < numFrames * mCblk->frameSize) {
4097 delete [] mTimedSilenceBuffer;
4098 mTimedSilenceBufferSize = numFrames * mCblk->frameSize;
4099 mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize];
4100 memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize);
4101 }
4102
4103 buffer->raw = mTimedSilenceBuffer;
4104 size_t framesRequested = buffer->frameCount;
4105 buffer->frameCount = min(numFrames, framesRequested);
4106
4107 mTimedAudioOutputOnTime = false;
4108}
4109
4110void AudioFlinger::PlaybackThread::TimedTrack::releaseBuffer(
4111 AudioBufferProvider::Buffer* buffer) {
4112
4113 Mutex::Autolock _l(mTimedBufferQueueLock);
4114
John Grossmanfe5b3ba2012-02-12 17:51:21 -08004115 // If the buffer which was just released is part of the buffer at the head
4116 // of the queue, be sure to update the amt of the buffer which has been
4117 // consumed. If the buffer being returned is not part of the head of the
4118 // queue, its either because the buffer is part of the silence buffer, or
4119 // because the head of the timed queue was trimmed after the mixer called
4120 // getNextBuffer but before the mixer called releaseBuffer.
4121 if ((buffer->raw != mTimedSilenceBuffer) && mTimedBufferQueue.size()) {
John Grossman4ff14ba2012-02-08 16:37:41 -08004122 TimedBuffer& head = mTimedBufferQueue.editItemAt(0);
John Grossmanfe5b3ba2012-02-12 17:51:21 -08004123
4124 void* start = head.buffer()->pointer();
Glenn Kastenf063b492012-02-17 16:24:10 -08004125 void* end = (char *) head.buffer()->pointer() + head.buffer()->size();
John Grossmanfe5b3ba2012-02-12 17:51:21 -08004126
4127 if ((buffer->raw >= start) && (buffer->raw <= end)) {
4128 head.setPosition(head.position() +
4129 (buffer->frameCount * mCblk->frameSize));
4130 if (static_cast<size_t>(head.position()) >= head.buffer()->size()) {
4131 mTimedBufferQueue.removeAt(0);
4132 }
John Grossman4ff14ba2012-02-08 16:37:41 -08004133 }
4134 }
4135
4136 buffer->raw = 0;
4137 buffer->frameCount = 0;
4138}
4139
4140uint32_t AudioFlinger::PlaybackThread::TimedTrack::framesReady() const {
4141 Mutex::Autolock _l(mTimedBufferQueueLock);
4142
4143 uint32_t frames = 0;
4144 for (size_t i = 0; i < mTimedBufferQueue.size(); i++) {
4145 const TimedBuffer& tb = mTimedBufferQueue[i];
4146 frames += (tb.buffer()->size() - tb.position()) / mCblk->frameSize;
4147 }
4148
4149 return frames;
4150}
4151
4152AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer()
4153 : mPTS(0), mPosition(0) {}
4154
4155AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer(
4156 const sp<IMemory>& buffer, int64_t pts)
4157 : mBuffer(buffer), mPTS(pts), mPosition(0) {}
4158
Mathias Agopian65ab4712010-07-14 17:59:35 -07004159// ----------------------------------------------------------------------------
4160
4161// RecordTrack constructor must be called with AudioFlinger::mLock held
4162AudioFlinger::RecordThread::RecordTrack::RecordTrack(
Glenn Kasten9eaa5572012-01-20 13:32:16 -08004163 RecordThread *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004164 const sp<Client>& client,
4165 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08004166 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07004167 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004168 int frameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004169 int sessionId)
4170 : TrackBase(thread, client, sampleRate, format,
Glenn Kasten5cf034d2012-02-21 10:35:56 -08004171 channelMask, frameCount, 0 /*sharedBuffer*/, sessionId),
Mathias Agopian65ab4712010-07-14 17:59:35 -07004172 mOverflow(false)
4173{
4174 if (mCblk != NULL) {
Steve Block3856b092011-10-20 11:56:00 +01004175 ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer);
Dima Zavinfce7a472011-04-19 22:30:36 -07004176 if (format == AUDIO_FORMAT_PCM_16_BIT) {
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07004177 mCblk->frameSize = mChannelCount * sizeof(int16_t);
Dima Zavinfce7a472011-04-19 22:30:36 -07004178 } else if (format == AUDIO_FORMAT_PCM_8_BIT) {
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07004179 mCblk->frameSize = mChannelCount * sizeof(int8_t);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004180 } else {
4181 mCblk->frameSize = sizeof(int8_t);
4182 }
4183 }
4184}
4185
4186AudioFlinger::RecordThread::RecordTrack::~RecordTrack()
4187{
4188 sp<ThreadBase> thread = mThread.promote();
4189 if (thread != 0) {
4190 AudioSystem::releaseInput(thread->id());
4191 }
4192}
4193
John Grossman4ff14ba2012-02-08 16:37:41 -08004194status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07004195{
4196 audio_track_cblk_t* cblk = this->cblk();
4197 uint32_t framesAvail;
4198 uint32_t framesReq = buffer->frameCount;
4199
4200 // Check if last stepServer failed, try to step now
Glenn Kasten5cf034d2012-02-21 10:35:56 -08004201 if (mStepServerFailed) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004202 if (!step()) goto getNextBuffer_exit;
Steve Block3856b092011-10-20 11:56:00 +01004203 ALOGV("stepServer recovered");
Glenn Kasten5cf034d2012-02-21 10:35:56 -08004204 mStepServerFailed = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004205 }
4206
4207 framesAvail = cblk->framesAvailable_l();
4208
Glenn Kastenf6b16782011-12-15 09:51:17 -08004209 if (CC_LIKELY(framesAvail)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004210 uint32_t s = cblk->server;
4211 uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
4212
4213 if (framesReq > framesAvail) {
4214 framesReq = framesAvail;
4215 }
4216 if (s + framesReq > bufferEnd) {
4217 framesReq = bufferEnd - s;
4218 }
4219
4220 buffer->raw = getBuffer(s, framesReq);
Glenn Kastene0feee32011-12-13 11:53:26 -08004221 if (buffer->raw == NULL) goto getNextBuffer_exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004222
4223 buffer->frameCount = framesReq;
4224 return NO_ERROR;
4225 }
4226
4227getNextBuffer_exit:
Glenn Kastene0feee32011-12-13 11:53:26 -08004228 buffer->raw = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004229 buffer->frameCount = 0;
4230 return NOT_ENOUGH_DATA;
4231}
4232
Glenn Kasten6dbc1352012-02-02 10:56:47 -08004233status_t AudioFlinger::RecordThread::RecordTrack::start(pid_t tid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07004234{
4235 sp<ThreadBase> thread = mThread.promote();
4236 if (thread != 0) {
4237 RecordThread *recordThread = (RecordThread *)thread.get();
Glenn Kasten6dbc1352012-02-02 10:56:47 -08004238 return recordThread->start(this, tid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004239 } else {
4240 return BAD_VALUE;
4241 }
4242}
4243
4244void AudioFlinger::RecordThread::RecordTrack::stop()
4245{
4246 sp<ThreadBase> thread = mThread.promote();
4247 if (thread != 0) {
4248 RecordThread *recordThread = (RecordThread *)thread.get();
4249 recordThread->stop(this);
Eric Laurent38ccae22011-03-28 18:37:07 -07004250 TrackBase::reset();
4251 // Force overerrun condition to avoid false overrun callback until first data is
4252 // read from buffer
4253 android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004254 }
4255}
4256
4257void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size)
4258{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07004259 snprintf(buffer, size, " %05d %03u 0x%08x %05d %04u %01d %05u %08x %08x\n",
Glenn Kasten44deb052012-02-05 18:09:08 -08004260 (mClient == 0) ? getpid_cached : mClient->pid(),
Mathias Agopian65ab4712010-07-14 17:59:35 -07004261 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07004262 mChannelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004263 mSessionId,
4264 mFrameCount,
4265 mState,
4266 mCblk->sampleRate,
4267 mCblk->server,
4268 mCblk->user);
4269}
4270
4271
4272// ----------------------------------------------------------------------------
4273
4274AudioFlinger::PlaybackThread::OutputTrack::OutputTrack(
Glenn Kasten9eaa5572012-01-20 13:32:16 -08004275 PlaybackThread *playbackThread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004276 DuplicatingThread *sourceThread,
4277 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08004278 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07004279 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004280 int frameCount)
Glenn Kasten9eaa5572012-01-20 13:32:16 -08004281 : Track(playbackThread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0),
Mathias Agopian65ab4712010-07-14 17:59:35 -07004282 mActive(false), mSourceThread(sourceThread)
4283{
4284
Mathias Agopian65ab4712010-07-14 17:59:35 -07004285 if (mCblk != NULL) {
4286 mCblk->flags |= CBLK_DIRECTION_OUT;
4287 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004288 mOutBuffer.frameCount = 0;
4289 playbackThread->mTracks.add(this);
Steve Block3856b092011-10-20 11:56:00 +01004290 ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07004291 "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p",
4292 mCblk, mBuffer, mCblk->buffers,
4293 mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004294 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00004295 ALOGW("Error creating output track on thread %p", playbackThread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004296 }
4297}
4298
4299AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack()
4300{
4301 clearBufferQueue();
4302}
4303
Glenn Kasten6dbc1352012-02-02 10:56:47 -08004304status_t AudioFlinger::PlaybackThread::OutputTrack::start(pid_t tid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07004305{
Glenn Kasten6dbc1352012-02-02 10:56:47 -08004306 status_t status = Track::start(tid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004307 if (status != NO_ERROR) {
4308 return status;
4309 }
4310
4311 mActive = true;
4312 mRetryCount = 127;
4313 return status;
4314}
4315
4316void AudioFlinger::PlaybackThread::OutputTrack::stop()
4317{
4318 Track::stop();
4319 clearBufferQueue();
4320 mOutBuffer.frameCount = 0;
4321 mActive = false;
4322}
4323
4324bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames)
4325{
4326 Buffer *pInBuffer;
4327 Buffer inBuffer;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07004328 uint32_t channelCount = mChannelCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004329 bool outputBufferFull = false;
4330 inBuffer.frameCount = frames;
4331 inBuffer.i16 = data;
4332
4333 uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs();
4334
4335 if (!mActive && frames != 0) {
Glenn Kasten6dbc1352012-02-02 10:56:47 -08004336 start(0);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004337 sp<ThreadBase> thread = mThread.promote();
4338 if (thread != 0) {
4339 MixerThread *mixerThread = (MixerThread *)thread.get();
4340 if (mCblk->frameCount > frames){
4341 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
4342 uint32_t startFrames = (mCblk->frameCount - frames);
4343 pInBuffer = new Buffer;
4344 pInBuffer->mBuffer = new int16_t[startFrames * channelCount];
4345 pInBuffer->frameCount = startFrames;
4346 pInBuffer->i16 = pInBuffer->mBuffer;
4347 memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t));
4348 mBufferQueue.add(pInBuffer);
4349 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00004350 ALOGW ("OutputTrack::write() %p no more buffers in queue", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004351 }
4352 }
4353 }
4354 }
4355
4356 while (waitTimeLeftMs) {
4357 // First write pending buffers, then new data
4358 if (mBufferQueue.size()) {
4359 pInBuffer = mBufferQueue.itemAt(0);
4360 } else {
4361 pInBuffer = &inBuffer;
4362 }
4363
4364 if (pInBuffer->frameCount == 0) {
4365 break;
4366 }
4367
4368 if (mOutBuffer.frameCount == 0) {
4369 mOutBuffer.frameCount = pInBuffer->frameCount;
4370 nsecs_t startTime = systemTime();
Glenn Kasten335787f2012-01-20 17:00:00 -08004371 if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)NO_MORE_BUFFERS) {
Steve Block3856b092011-10-20 11:56:00 +01004372 ALOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07004373 outputBufferFull = true;
4374 break;
4375 }
4376 uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime);
4377 if (waitTimeLeftMs >= waitTimeMs) {
4378 waitTimeLeftMs -= waitTimeMs;
4379 } else {
4380 waitTimeLeftMs = 0;
4381 }
4382 }
4383
4384 uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount;
4385 memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t));
4386 mCblk->stepUser(outFrames);
4387 pInBuffer->frameCount -= outFrames;
4388 pInBuffer->i16 += outFrames * channelCount;
4389 mOutBuffer.frameCount -= outFrames;
4390 mOutBuffer.i16 += outFrames * channelCount;
4391
4392 if (pInBuffer->frameCount == 0) {
4393 if (mBufferQueue.size()) {
4394 mBufferQueue.removeAt(0);
4395 delete [] pInBuffer->mBuffer;
4396 delete pInBuffer;
Steve Block3856b092011-10-20 11:56:00 +01004397 ALOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07004398 } else {
4399 break;
4400 }
4401 }
4402 }
4403
4404 // If we could not write all frames, allocate a buffer and queue it for next time.
4405 if (inBuffer.frameCount) {
4406 sp<ThreadBase> thread = mThread.promote();
4407 if (thread != 0 && !thread->standby()) {
4408 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
4409 pInBuffer = new Buffer;
4410 pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount];
4411 pInBuffer->frameCount = inBuffer.frameCount;
4412 pInBuffer->i16 = pInBuffer->mBuffer;
4413 memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t));
4414 mBufferQueue.add(pInBuffer);
Steve Block3856b092011-10-20 11:56:00 +01004415 ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07004416 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00004417 ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004418 }
4419 }
4420 }
4421
4422 // Calling write() with a 0 length buffer, means that no more data will be written:
4423 // If no more buffers are pending, fill output track buffer to make sure it is started
4424 // by output mixer.
4425 if (frames == 0 && mBufferQueue.size() == 0) {
4426 if (mCblk->user < mCblk->frameCount) {
4427 frames = mCblk->frameCount - mCblk->user;
4428 pInBuffer = new Buffer;
4429 pInBuffer->mBuffer = new int16_t[frames * channelCount];
4430 pInBuffer->frameCount = frames;
4431 pInBuffer->i16 = pInBuffer->mBuffer;
4432 memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t));
4433 mBufferQueue.add(pInBuffer);
4434 } else if (mActive) {
4435 stop();
4436 }
4437 }
4438
4439 return outputBufferFull;
4440}
4441
4442status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs)
4443{
4444 int active;
4445 status_t result;
4446 audio_track_cblk_t* cblk = mCblk;
4447 uint32_t framesReq = buffer->frameCount;
4448
Steve Block3856b092011-10-20 11:56:00 +01004449// ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004450 buffer->frameCount = 0;
4451
4452 uint32_t framesAvail = cblk->framesAvailable();
4453
4454
4455 if (framesAvail == 0) {
4456 Mutex::Autolock _l(cblk->lock);
4457 goto start_loop_here;
4458 while (framesAvail == 0) {
4459 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -08004460 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +01004461 ALOGV("Not active and NO_MORE_BUFFERS");
Glenn Kasten335787f2012-01-20 17:00:00 -08004462 return NO_MORE_BUFFERS;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004463 }
4464 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
4465 if (result != NO_ERROR) {
Glenn Kasten335787f2012-01-20 17:00:00 -08004466 return NO_MORE_BUFFERS;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004467 }
4468 // read the server count again
4469 start_loop_here:
4470 framesAvail = cblk->framesAvailable_l();
4471 }
4472 }
4473
4474// if (framesAvail < framesReq) {
Glenn Kasten335787f2012-01-20 17:00:00 -08004475// return NO_MORE_BUFFERS;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004476// }
4477
4478 if (framesReq > framesAvail) {
4479 framesReq = framesAvail;
4480 }
4481
4482 uint32_t u = cblk->user;
4483 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
4484
4485 if (u + framesReq > bufferEnd) {
4486 framesReq = bufferEnd - u;
4487 }
4488
4489 buffer->frameCount = framesReq;
4490 buffer->raw = (void *)cblk->buffer(u);
4491 return NO_ERROR;
4492}
4493
4494
4495void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue()
4496{
4497 size_t size = mBufferQueue.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07004498
4499 for (size_t i = 0; i < size; i++) {
Glenn Kastena1117922012-01-26 10:53:32 -08004500 Buffer *pBuffer = mBufferQueue.itemAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004501 delete [] pBuffer->mBuffer;
4502 delete pBuffer;
4503 }
4504 mBufferQueue.clear();
4505}
4506
4507// ----------------------------------------------------------------------------
4508
4509AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
4510 : RefBase(),
4511 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08004512 // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below
Mathias Agopian65ab4712010-07-14 17:59:35 -07004513 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08004514 mPid(pid),
4515 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07004516{
4517 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
4518}
4519
4520// Client destructor must be called with AudioFlinger::mLock held
4521AudioFlinger::Client::~Client()
4522{
4523 mAudioFlinger->removeClient_l(mPid);
4524}
4525
Glenn Kasten435dbe62012-01-30 10:15:48 -08004526sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07004527{
4528 return mMemoryDealer;
4529}
4530
John Grossman4ff14ba2012-02-08 16:37:41 -08004531// Reserve one of the limited slots for a timed audio track associated
4532// with this client
4533bool AudioFlinger::Client::reserveTimedTrack()
4534{
4535 const int kMaxTimedTracksPerClient = 4;
4536
4537 Mutex::Autolock _l(mTimedTrackLock);
4538
4539 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
4540 ALOGW("can not create timed track - pid %d has exceeded the limit",
4541 mPid);
4542 return false;
4543 }
4544
4545 mTimedTrackCount++;
4546 return true;
4547}
4548
4549// Release a slot for a timed audio track
4550void AudioFlinger::Client::releaseTimedTrack()
4551{
4552 Mutex::Autolock _l(mTimedTrackLock);
4553 mTimedTrackCount--;
4554}
4555
Mathias Agopian65ab4712010-07-14 17:59:35 -07004556// ----------------------------------------------------------------------------
4557
4558AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
4559 const sp<IAudioFlingerClient>& client,
4560 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08004561 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07004562{
4563}
4564
4565AudioFlinger::NotificationClient::~NotificationClient()
4566{
Mathias Agopian65ab4712010-07-14 17:59:35 -07004567}
4568
4569void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
4570{
4571 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08004572 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004573}
4574
4575// ----------------------------------------------------------------------------
4576
4577AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track)
4578 : BnAudioTrack(),
4579 mTrack(track)
4580{
4581}
4582
4583AudioFlinger::TrackHandle::~TrackHandle() {
4584 // just stop the track on deletion, associated resources
4585 // will be freed from the main thread once all pending buffers have
4586 // been played. Unless it's not in the active track list, in which
4587 // case we free everything now...
4588 mTrack->destroy();
4589}
4590
Glenn Kasten90716c52012-01-26 13:40:12 -08004591sp<IMemory> AudioFlinger::TrackHandle::getCblk() const {
4592 return mTrack->getCblk();
4593}
4594
Glenn Kasten6dbc1352012-02-02 10:56:47 -08004595status_t AudioFlinger::TrackHandle::start(pid_t tid) {
4596 return mTrack->start(tid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004597}
4598
4599void AudioFlinger::TrackHandle::stop() {
4600 mTrack->stop();
4601}
4602
4603void AudioFlinger::TrackHandle::flush() {
4604 mTrack->flush();
4605}
4606
4607void AudioFlinger::TrackHandle::mute(bool e) {
4608 mTrack->mute(e);
4609}
4610
4611void AudioFlinger::TrackHandle::pause() {
4612 mTrack->pause();
4613}
4614
Mathias Agopian65ab4712010-07-14 17:59:35 -07004615status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId)
4616{
4617 return mTrack->attachAuxEffect(EffectId);
4618}
4619
John Grossman4ff14ba2012-02-08 16:37:41 -08004620status_t AudioFlinger::TrackHandle::allocateTimedBuffer(size_t size,
4621 sp<IMemory>* buffer) {
4622 if (!mTrack->isTimedTrack())
4623 return INVALID_OPERATION;
4624
4625 PlaybackThread::TimedTrack* tt =
4626 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
4627 return tt->allocateTimedBuffer(size, buffer);
4628}
4629
4630status_t AudioFlinger::TrackHandle::queueTimedBuffer(const sp<IMemory>& buffer,
4631 int64_t pts) {
4632 if (!mTrack->isTimedTrack())
4633 return INVALID_OPERATION;
4634
4635 PlaybackThread::TimedTrack* tt =
4636 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
4637 return tt->queueTimedBuffer(buffer, pts);
4638}
4639
4640status_t AudioFlinger::TrackHandle::setMediaTimeTransform(
4641 const LinearTransform& xform, int target) {
4642
4643 if (!mTrack->isTimedTrack())
4644 return INVALID_OPERATION;
4645
4646 PlaybackThread::TimedTrack* tt =
4647 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
4648 return tt->setMediaTimeTransform(
4649 xform, static_cast<TimedAudioTrack::TargetTimeline>(target));
4650}
4651
Mathias Agopian65ab4712010-07-14 17:59:35 -07004652status_t AudioFlinger::TrackHandle::onTransact(
4653 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
4654{
4655 return BnAudioTrack::onTransact(code, data, reply, flags);
4656}
4657
4658// ----------------------------------------------------------------------------
4659
4660sp<IAudioRecord> AudioFlinger::openRecord(
4661 pid_t pid,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08004662 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004663 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08004664 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07004665 uint32_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004666 int frameCount,
Glenn Kasten5cf034d2012-02-21 10:35:56 -08004667 // FIXME dead, remove from IAudioFlinger
Mathias Agopian65ab4712010-07-14 17:59:35 -07004668 uint32_t flags,
4669 int *sessionId,
4670 status_t *status)
4671{
4672 sp<RecordThread::RecordTrack> recordTrack;
4673 sp<RecordHandle> recordHandle;
4674 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004675 status_t lStatus;
4676 RecordThread *thread;
4677 size_t inFrameCount;
4678 int lSessionId;
4679
4680 // check calling permissions
4681 if (!recordingAllowed()) {
4682 lStatus = PERMISSION_DENIED;
4683 goto Exit;
4684 }
4685
4686 // add client to list
4687 { // scope for mLock
4688 Mutex::Autolock _l(mLock);
4689 thread = checkRecordThread_l(input);
4690 if (thread == NULL) {
4691 lStatus = BAD_VALUE;
4692 goto Exit;
4693 }
4694
Glenn Kasten98ec94c2012-01-25 14:28:29 -08004695 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004696
4697 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07004698 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004699 lSessionId = *sessionId;
4700 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004701 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07004702 if (sessionId != NULL) {
4703 *sessionId = lSessionId;
4704 }
4705 }
4706 // create new record track. The record track uses one track in mHardwareMixerThread by convention.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004707 recordTrack = thread->createRecordTrack_l(client,
4708 sampleRate,
4709 format,
4710 channelMask,
4711 frameCount,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004712 lSessionId,
4713 &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004714 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004715 if (lStatus != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004716 // remove local strong reference to Client before deleting the RecordTrack so that the Client
4717 // destructor is called by the TrackBase destructor with mLock held
4718 client.clear();
4719 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07004720 goto Exit;
4721 }
4722
4723 // return to handle to client
4724 recordHandle = new RecordHandle(recordTrack);
4725 lStatus = NO_ERROR;
4726
4727Exit:
4728 if (status) {
4729 *status = lStatus;
4730 }
4731 return recordHandle;
4732}
4733
4734// ----------------------------------------------------------------------------
4735
4736AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack)
4737 : BnAudioRecord(),
4738 mRecordTrack(recordTrack)
4739{
4740}
4741
4742AudioFlinger::RecordHandle::~RecordHandle() {
4743 stop();
4744}
4745
Glenn Kasten90716c52012-01-26 13:40:12 -08004746sp<IMemory> AudioFlinger::RecordHandle::getCblk() const {
4747 return mRecordTrack->getCblk();
4748}
4749
Glenn Kasten6dbc1352012-02-02 10:56:47 -08004750status_t AudioFlinger::RecordHandle::start(pid_t tid) {
Steve Block3856b092011-10-20 11:56:00 +01004751 ALOGV("RecordHandle::start()");
Glenn Kasten6dbc1352012-02-02 10:56:47 -08004752 return mRecordTrack->start(tid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004753}
4754
4755void AudioFlinger::RecordHandle::stop() {
Steve Block3856b092011-10-20 11:56:00 +01004756 ALOGV("RecordHandle::stop()");
Mathias Agopian65ab4712010-07-14 17:59:35 -07004757 mRecordTrack->stop();
4758}
4759
Mathias Agopian65ab4712010-07-14 17:59:35 -07004760status_t AudioFlinger::RecordHandle::onTransact(
4761 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
4762{
4763 return BnAudioRecord::onTransact(code, data, reply, flags);
4764}
4765
4766// ----------------------------------------------------------------------------
4767
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004768AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger,
4769 AudioStreamIn *input,
4770 uint32_t sampleRate,
4771 uint32_t channels,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08004772 audio_io_handle_t id,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004773 uint32_t device) :
Glenn Kasten23bb8be2012-01-26 10:38:26 -08004774 ThreadBase(audioFlinger, id, device, RECORD),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08004775 mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL),
4776 // mRsmpInIndex and mInputBytes set by readInputParameters()
4777 mReqChannelCount(popcount(channels)),
4778 mReqSampleRate(sampleRate)
4779 // mBytesRead is only meaningful while active, and so is cleared in start()
4780 // (but might be better to also clear here for dump?)
Mathias Agopian65ab4712010-07-14 17:59:35 -07004781{
Eric Laurentfeb0db62011-07-22 09:04:31 -07004782 snprintf(mName, kNameLength, "AudioIn_%d", id);
4783
Mathias Agopian65ab4712010-07-14 17:59:35 -07004784 readInputParameters();
4785}
4786
4787
4788AudioFlinger::RecordThread::~RecordThread()
4789{
4790 delete[] mRsmpInBuffer;
Glenn Kastene9dd0172012-01-27 18:08:45 -08004791 delete mResampler;
4792 delete[] mRsmpOutBuffer;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004793}
4794
4795void AudioFlinger::RecordThread::onFirstRef()
4796{
Eric Laurentfeb0db62011-07-22 09:04:31 -07004797 run(mName, PRIORITY_URGENT_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004798}
4799
Eric Laurentb8ba0a92011-08-07 16:32:26 -07004800status_t AudioFlinger::RecordThread::readyToRun()
4801{
4802 status_t status = initCheck();
Steve Block5ff1dd52012-01-05 23:22:43 +00004803 ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this);
Eric Laurentb8ba0a92011-08-07 16:32:26 -07004804 return status;
4805}
4806
Mathias Agopian65ab4712010-07-14 17:59:35 -07004807bool AudioFlinger::RecordThread::threadLoop()
4808{
4809 AudioBufferProvider::Buffer buffer;
4810 sp<RecordTrack> activeTrack;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004811 Vector< sp<EffectChain> > effectChains;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004812
Eric Laurent44d98482010-09-30 16:12:31 -07004813 nsecs_t lastWarning = 0;
4814
Eric Laurentfeb0db62011-07-22 09:04:31 -07004815 acquireWakeLock();
4816
Mathias Agopian65ab4712010-07-14 17:59:35 -07004817 // start recording
4818 while (!exitPending()) {
4819
4820 processConfigEvents();
4821
4822 { // scope for mLock
4823 Mutex::Autolock _l(mLock);
4824 checkForNewParameters_l();
4825 if (mActiveTrack == 0 && mConfigEvents.isEmpty()) {
4826 if (!mStandby) {
Dima Zavin799a70e2011-04-18 16:57:27 -07004827 mInput->stream->common.standby(&mInput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004828 mStandby = true;
4829 }
4830
4831 if (exitPending()) break;
4832
Eric Laurentfeb0db62011-07-22 09:04:31 -07004833 releaseWakeLock_l();
Steve Block3856b092011-10-20 11:56:00 +01004834 ALOGV("RecordThread: loop stopping");
Mathias Agopian65ab4712010-07-14 17:59:35 -07004835 // go to sleep
4836 mWaitWorkCV.wait(mLock);
Steve Block3856b092011-10-20 11:56:00 +01004837 ALOGV("RecordThread: loop starting");
Eric Laurentfeb0db62011-07-22 09:04:31 -07004838 acquireWakeLock_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07004839 continue;
4840 }
4841 if (mActiveTrack != 0) {
4842 if (mActiveTrack->mState == TrackBase::PAUSING) {
4843 if (!mStandby) {
Dima Zavin799a70e2011-04-18 16:57:27 -07004844 mInput->stream->common.standby(&mInput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004845 mStandby = true;
4846 }
4847 mActiveTrack.clear();
4848 mStartStopCond.broadcast();
4849 } else if (mActiveTrack->mState == TrackBase::RESUMING) {
4850 if (mReqChannelCount != mActiveTrack->channelCount()) {
4851 mActiveTrack.clear();
4852 mStartStopCond.broadcast();
4853 } else if (mBytesRead != 0) {
4854 // record start succeeds only if first read from audio input
4855 // succeeds
4856 if (mBytesRead > 0) {
4857 mActiveTrack->mState = TrackBase::ACTIVE;
4858 } else {
4859 mActiveTrack.clear();
4860 }
4861 mStartStopCond.broadcast();
4862 }
4863 mStandby = false;
4864 }
4865 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004866 lockEffectChains_l(effectChains);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004867 }
4868
4869 if (mActiveTrack != 0) {
4870 if (mActiveTrack->mState != TrackBase::ACTIVE &&
4871 mActiveTrack->mState != TrackBase::RESUMING) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004872 unlockEffectChains(effectChains);
4873 usleep(kRecordThreadSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004874 continue;
4875 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004876 for (size_t i = 0; i < effectChains.size(); i ++) {
4877 effectChains[i]->process_l();
4878 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004879
Mathias Agopian65ab4712010-07-14 17:59:35 -07004880 buffer.frameCount = mFrameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -08004881 if (CC_LIKELY(mActiveTrack->getNextBuffer(
4882 &buffer, AudioBufferProvider::kInvalidPTS) == NO_ERROR)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004883 size_t framesOut = buffer.frameCount;
Glenn Kastene0feee32011-12-13 11:53:26 -08004884 if (mResampler == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004885 // no resampling
4886 while (framesOut) {
4887 size_t framesIn = mFrameCount - mRsmpInIndex;
4888 if (framesIn) {
4889 int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize;
4890 int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize;
4891 if (framesIn > framesOut)
4892 framesIn = framesOut;
4893 mRsmpInIndex += framesIn;
4894 framesOut -= framesIn;
4895 if ((int)mChannelCount == mReqChannelCount ||
Dima Zavinfce7a472011-04-19 22:30:36 -07004896 mFormat != AUDIO_FORMAT_PCM_16_BIT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004897 memcpy(dst, src, framesIn * mFrameSize);
4898 } else {
4899 int16_t *src16 = (int16_t *)src;
4900 int16_t *dst16 = (int16_t *)dst;
4901 if (mChannelCount == 1) {
4902 while (framesIn--) {
4903 *dst16++ = *src16;
4904 *dst16++ = *src16++;
4905 }
4906 } else {
4907 while (framesIn--) {
4908 *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1);
4909 src16 += 2;
4910 }
4911 }
4912 }
4913 }
4914 if (framesOut && mFrameCount == mRsmpInIndex) {
4915 if (framesOut == mFrameCount &&
Dima Zavinfce7a472011-04-19 22:30:36 -07004916 ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) {
Dima Zavin799a70e2011-04-18 16:57:27 -07004917 mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004918 framesOut = 0;
4919 } else {
Dima Zavin799a70e2011-04-18 16:57:27 -07004920 mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004921 mRsmpInIndex = 0;
4922 }
4923 if (mBytesRead < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00004924 ALOGE("Error reading audio input");
Mathias Agopian65ab4712010-07-14 17:59:35 -07004925 if (mActiveTrack->mState == TrackBase::ACTIVE) {
4926 // Force input into standby so that it tries to
4927 // recover at next read attempt
Dima Zavin799a70e2011-04-18 16:57:27 -07004928 mInput->stream->common.standby(&mInput->stream->common);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004929 usleep(kRecordThreadSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004930 }
4931 mRsmpInIndex = mFrameCount;
4932 framesOut = 0;
4933 buffer.frameCount = 0;
4934 }
4935 }
4936 }
4937 } else {
4938 // resampling
4939
4940 memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t));
4941 // alter output frame count as if we were expecting stereo samples
4942 if (mChannelCount == 1 && mReqChannelCount == 1) {
4943 framesOut >>= 1;
4944 }
4945 mResampler->resample(mRsmpOutBuffer, framesOut, this);
4946 // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer()
4947 // are 32 bit aligned which should be always true.
4948 if (mChannelCount == 2 && mReqChannelCount == 1) {
Glenn Kasten3b21c502011-12-15 09:52:39 -08004949 ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004950 // the resampler always outputs stereo samples: do post stereo to mono conversion
4951 int16_t *src = (int16_t *)mRsmpOutBuffer;
4952 int16_t *dst = buffer.i16;
4953 while (framesOut--) {
4954 *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1);
4955 src += 2;
4956 }
4957 } else {
Glenn Kasten3b21c502011-12-15 09:52:39 -08004958 ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004959 }
4960
4961 }
4962 mActiveTrack->releaseBuffer(&buffer);
4963 mActiveTrack->overflow();
4964 }
4965 // client isn't retrieving buffers fast enough
4966 else {
Eric Laurent44d98482010-09-30 16:12:31 -07004967 if (!mActiveTrack->setOverflow()) {
4968 nsecs_t now = systemTime();
Glenn Kasten7dede872011-12-13 11:04:14 -08004969 if ((now - lastWarning) > kWarningThrottleNs) {
Steve Block5ff1dd52012-01-05 23:22:43 +00004970 ALOGW("RecordThread: buffer overflow");
Eric Laurent44d98482010-09-30 16:12:31 -07004971 lastWarning = now;
4972 }
4973 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07004974 // Release the processor for a while before asking for a new buffer.
4975 // This will give the application more chance to read from the buffer and
4976 // clear the overflow.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004977 usleep(kRecordThreadSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004978 }
4979 }
Eric Laurentec437d82011-07-26 20:54:46 -07004980 // enable changes in effect chain
4981 unlockEffectChains(effectChains);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004982 effectChains.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07004983 }
4984
4985 if (!mStandby) {
Dima Zavin799a70e2011-04-18 16:57:27 -07004986 mInput->stream->common.standby(&mInput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004987 }
4988 mActiveTrack.clear();
4989
4990 mStartStopCond.broadcast();
4991
Eric Laurentfeb0db62011-07-22 09:04:31 -07004992 releaseWakeLock();
4993
Steve Block3856b092011-10-20 11:56:00 +01004994 ALOGV("RecordThread %p exiting", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004995 return false;
4996}
4997
Eric Laurent7c7f10b2011-06-17 21:29:58 -07004998
4999sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l(
5000 const sp<AudioFlinger::Client>& client,
5001 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08005002 audio_format_t format,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005003 int channelMask,
5004 int frameCount,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005005 int sessionId,
5006 status_t *status)
5007{
5008 sp<RecordTrack> track;
5009 status_t lStatus;
5010
5011 lStatus = initCheck();
5012 if (lStatus != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +00005013 ALOGE("Audio driver not initialized.");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005014 goto Exit;
5015 }
5016
5017 { // scope for mLock
5018 Mutex::Autolock _l(mLock);
5019
5020 track = new RecordTrack(this, client, sampleRate,
Glenn Kasten5cf034d2012-02-21 10:35:56 -08005021 format, channelMask, frameCount, sessionId);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005022
Glenn Kasten7378ca52012-01-20 13:44:40 -08005023 if (track->getCblk() == 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005024 lStatus = NO_MEMORY;
5025 goto Exit;
5026 }
5027
5028 mTrack = track.get();
Eric Laurent59bd0da2011-08-01 09:52:20 -07005029 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
5030 bool suspend = audio_is_bluetooth_sco_device(
Eric Laurentbee53372011-08-29 12:42:48 -07005031 (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff();
Eric Laurent59bd0da2011-08-01 09:52:20 -07005032 setEffectSuspended_l(FX_IID_AEC, suspend, sessionId);
5033 setEffectSuspended_l(FX_IID_NS, suspend, sessionId);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005034 }
5035 lStatus = NO_ERROR;
5036
5037Exit:
5038 if (status) {
5039 *status = lStatus;
5040 }
5041 return track;
5042}
5043
Glenn Kasten6dbc1352012-02-02 10:56:47 -08005044status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, pid_t tid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005045{
Glenn Kasten6dbc1352012-02-02 10:56:47 -08005046 ALOGV("RecordThread::start tid=%d", tid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005047 sp <ThreadBase> strongMe = this;
5048 status_t status = NO_ERROR;
5049 {
Glenn Kastena7d8d6f2012-01-05 15:41:56 -08005050 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005051 if (mActiveTrack != 0) {
5052 if (recordTrack != mActiveTrack.get()) {
5053 status = -EBUSY;
5054 } else if (mActiveTrack->mState == TrackBase::PAUSING) {
5055 mActiveTrack->mState = TrackBase::ACTIVE;
5056 }
5057 return status;
5058 }
5059
5060 recordTrack->mState = TrackBase::IDLE;
5061 mActiveTrack = recordTrack;
5062 mLock.unlock();
5063 status_t status = AudioSystem::startInput(mId);
5064 mLock.lock();
5065 if (status != NO_ERROR) {
5066 mActiveTrack.clear();
5067 return status;
5068 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07005069 mRsmpInIndex = mFrameCount;
5070 mBytesRead = 0;
Eric Laurent243f5f92011-02-28 16:52:51 -08005071 if (mResampler != NULL) {
5072 mResampler->reset();
5073 }
5074 mActiveTrack->mState = TrackBase::RESUMING;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005075 // signal thread to start
Steve Block3856b092011-10-20 11:56:00 +01005076 ALOGV("Signal record thread");
Mathias Agopian65ab4712010-07-14 17:59:35 -07005077 mWaitWorkCV.signal();
5078 // do not wait for mStartStopCond if exiting
Glenn Kastenb28686f2012-01-06 08:39:38 -08005079 if (exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005080 mActiveTrack.clear();
5081 status = INVALID_OPERATION;
5082 goto startError;
5083 }
5084 mStartStopCond.wait(mLock);
5085 if (mActiveTrack == 0) {
Steve Block3856b092011-10-20 11:56:00 +01005086 ALOGV("Record failed to start");
Mathias Agopian65ab4712010-07-14 17:59:35 -07005087 status = BAD_VALUE;
5088 goto startError;
5089 }
Steve Block3856b092011-10-20 11:56:00 +01005090 ALOGV("Record started OK");
Mathias Agopian65ab4712010-07-14 17:59:35 -07005091 return status;
5092 }
5093startError:
5094 AudioSystem::stopInput(mId);
5095 return status;
5096}
5097
5098void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) {
Steve Block3856b092011-10-20 11:56:00 +01005099 ALOGV("RecordThread::stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -07005100 sp <ThreadBase> strongMe = this;
5101 {
Glenn Kastena7d8d6f2012-01-05 15:41:56 -08005102 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005103 if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) {
5104 mActiveTrack->mState = TrackBase::PAUSING;
5105 // do not wait for mStartStopCond if exiting
Glenn Kastenb28686f2012-01-06 08:39:38 -08005106 if (exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005107 return;
5108 }
5109 mStartStopCond.wait(mLock);
5110 // if we have been restarted, recordTrack == mActiveTrack.get() here
5111 if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) {
5112 mLock.unlock();
5113 AudioSystem::stopInput(mId);
5114 mLock.lock();
Steve Block3856b092011-10-20 11:56:00 +01005115 ALOGV("Record stopped OK");
Mathias Agopian65ab4712010-07-14 17:59:35 -07005116 }
5117 }
5118 }
5119}
5120
5121status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args)
5122{
5123 const size_t SIZE = 256;
5124 char buffer[SIZE];
5125 String8 result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005126
5127 snprintf(buffer, SIZE, "\nInput thread %p internals\n", this);
5128 result.append(buffer);
5129
5130 if (mActiveTrack != 0) {
5131 result.append("Active Track:\n");
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07005132 result.append(" Clien Fmt Chn mask Session Buf S SRate Serv User\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07005133 mActiveTrack->dump(buffer, SIZE);
5134 result.append(buffer);
5135
5136 snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex);
5137 result.append(buffer);
5138 snprintf(buffer, SIZE, "In size: %d\n", mInputBytes);
5139 result.append(buffer);
Glenn Kastene0feee32011-12-13 11:53:26 -08005140 snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL));
Mathias Agopian65ab4712010-07-14 17:59:35 -07005141 result.append(buffer);
5142 snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount);
5143 result.append(buffer);
5144 snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate);
5145 result.append(buffer);
5146
5147
5148 } else {
5149 result.append("No record client\n");
5150 }
5151 write(fd, result.string(), result.size());
5152
5153 dumpBase(fd, args);
Eric Laurent1d2bff02011-07-24 17:49:51 -07005154 dumpEffectChains(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005155
5156 return NO_ERROR;
5157}
5158
John Grossman4ff14ba2012-02-08 16:37:41 -08005159status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005160{
5161 size_t framesReq = buffer->frameCount;
5162 size_t framesReady = mFrameCount - mRsmpInIndex;
5163 int channelCount;
5164
5165 if (framesReady == 0) {
Dima Zavin799a70e2011-04-18 16:57:27 -07005166 mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005167 if (mBytesRead < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00005168 ALOGE("RecordThread::getNextBuffer() Error reading audio input");
Mathias Agopian65ab4712010-07-14 17:59:35 -07005169 if (mActiveTrack->mState == TrackBase::ACTIVE) {
5170 // Force input into standby so that it tries to
5171 // recover at next read attempt
Dima Zavin799a70e2011-04-18 16:57:27 -07005172 mInput->stream->common.standby(&mInput->stream->common);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005173 usleep(kRecordThreadSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005174 }
Glenn Kastene0feee32011-12-13 11:53:26 -08005175 buffer->raw = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005176 buffer->frameCount = 0;
5177 return NOT_ENOUGH_DATA;
5178 }
5179 mRsmpInIndex = 0;
5180 framesReady = mFrameCount;
5181 }
5182
5183 if (framesReq > framesReady) {
5184 framesReq = framesReady;
5185 }
5186
5187 if (mChannelCount == 1 && mReqChannelCount == 2) {
5188 channelCount = 1;
5189 } else {
5190 channelCount = 2;
5191 }
5192 buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount;
5193 buffer->frameCount = framesReq;
5194 return NO_ERROR;
5195}
5196
5197void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer)
5198{
5199 mRsmpInIndex += buffer->frameCount;
5200 buffer->frameCount = 0;
5201}
5202
5203bool AudioFlinger::RecordThread::checkForNewParameters_l()
5204{
5205 bool reconfig = false;
5206
5207 while (!mNewParameters.isEmpty()) {
5208 status_t status = NO_ERROR;
5209 String8 keyValuePair = mNewParameters[0];
5210 AudioParameter param = AudioParameter(keyValuePair);
5211 int value;
Glenn Kasten58f30212012-01-12 12:27:51 -08005212 audio_format_t reqFormat = mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005213 int reqSamplingRate = mReqSampleRate;
5214 int reqChannelCount = mReqChannelCount;
5215
5216 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
5217 reqSamplingRate = value;
5218 reconfig = true;
5219 }
5220 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
Glenn Kasten58f30212012-01-12 12:27:51 -08005221 reqFormat = (audio_format_t) value;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005222 reconfig = true;
5223 }
5224 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
Dima Zavinfce7a472011-04-19 22:30:36 -07005225 reqChannelCount = popcount(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005226 reconfig = true;
5227 }
5228 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
5229 // do not accept frame count changes if tracks are open as the track buffer
Glenn Kasten99e53b82012-01-19 08:59:58 -08005230 // size depends on frame count and correct behavior would not be guaranteed
Mathias Agopian65ab4712010-07-14 17:59:35 -07005231 // if frame count is changed after track creation
5232 if (mActiveTrack != 0) {
5233 status = INVALID_OPERATION;
5234 } else {
5235 reconfig = true;
5236 }
5237 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005238 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
5239 // forward device change to effects that have requested to be
5240 // aware of attached audio device.
5241 for (size_t i = 0; i < mEffectChains.size(); i++) {
5242 mEffectChains[i]->setDevice_l(value);
5243 }
5244 // store input device and output device but do not forward output device to audio HAL.
5245 // Note that status is ignored by the caller for output device
5246 // (see AudioFlinger::setParameters()
5247 if (value & AUDIO_DEVICE_OUT_ALL) {
5248 mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL);
5249 status = BAD_VALUE;
5250 } else {
5251 mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL);
Eric Laurent59bd0da2011-08-01 09:52:20 -07005252 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
5253 if (mTrack != NULL) {
5254 bool suspend = audio_is_bluetooth_sco_device(
Eric Laurentbee53372011-08-29 12:42:48 -07005255 (audio_devices_t)value) && mAudioFlinger->btNrecIsOff();
Eric Laurent59bd0da2011-08-01 09:52:20 -07005256 setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId());
5257 setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId());
5258 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005259 }
5260 mDevice |= (uint32_t)value;
5261 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07005262 if (status == NO_ERROR) {
Dima Zavin799a70e2011-04-18 16:57:27 -07005263 status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07005264 if (status == INVALID_OPERATION) {
Dima Zavin799a70e2011-04-18 16:57:27 -07005265 mInput->stream->common.standby(&mInput->stream->common);
5266 status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07005267 }
5268 if (reconfig) {
5269 if (status == BAD_VALUE &&
Dima Zavin799a70e2011-04-18 16:57:27 -07005270 reqFormat == mInput->stream->common.get_format(&mInput->stream->common) &&
Dima Zavinfce7a472011-04-19 22:30:36 -07005271 reqFormat == AUDIO_FORMAT_PCM_16_BIT &&
Dima Zavin799a70e2011-04-18 16:57:27 -07005272 ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) &&
5273 (popcount(mInput->stream->common.get_channels(&mInput->stream->common)) < 3) &&
Dima Zavinfce7a472011-04-19 22:30:36 -07005274 (reqChannelCount < 3)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005275 status = NO_ERROR;
5276 }
5277 if (status == NO_ERROR) {
5278 readInputParameters();
5279 sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED);
5280 }
5281 }
5282 }
5283
5284 mNewParameters.removeAt(0);
5285
5286 mParamStatus = status;
5287 mParamCond.signal();
Eric Laurent60cd0a02011-09-13 11:40:21 -07005288 // wait for condition with time out in case the thread calling ThreadBase::setParameters()
5289 // already timed out waiting for the status and will never signal the condition.
Glenn Kasten7dede872011-12-13 11:04:14 -08005290 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005291 }
5292 return reconfig;
5293}
5294
5295String8 AudioFlinger::RecordThread::getParameters(const String8& keys)
5296{
Dima Zavinfce7a472011-04-19 22:30:36 -07005297 char *s;
Eric Laurentb8ba0a92011-08-07 16:32:26 -07005298 String8 out_s8 = String8();
5299
5300 Mutex::Autolock _l(mLock);
5301 if (initCheck() != NO_ERROR) {
5302 return out_s8;
5303 }
Dima Zavinfce7a472011-04-19 22:30:36 -07005304
Dima Zavin799a70e2011-04-18 16:57:27 -07005305 s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string());
Dima Zavinfce7a472011-04-19 22:30:36 -07005306 out_s8 = String8(s);
5307 free(s);
5308 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005309}
5310
5311void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) {
5312 AudioSystem::OutputDescriptor desc;
Glenn Kastena0d68332012-01-27 16:47:15 -08005313 void *param2 = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005314
5315 switch (event) {
5316 case AudioSystem::INPUT_OPENED:
5317 case AudioSystem::INPUT_CONFIG_CHANGED:
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07005318 desc.channels = mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005319 desc.samplingRate = mSampleRate;
5320 desc.format = mFormat;
5321 desc.frameCount = mFrameCount;
5322 desc.latency = 0;
5323 param2 = &desc;
5324 break;
5325
5326 case AudioSystem::INPUT_CLOSED:
5327 default:
5328 break;
5329 }
5330 mAudioFlinger->audioConfigChanged_l(event, mId, param2);
5331}
5332
5333void AudioFlinger::RecordThread::readInputParameters()
5334{
Glenn Kastene9dd0172012-01-27 18:08:45 -08005335 delete mRsmpInBuffer;
5336 // mRsmpInBuffer is always assigned a new[] below
5337 delete mRsmpOutBuffer;
5338 mRsmpOutBuffer = NULL;
5339 delete mResampler;
Glenn Kastene0feee32011-12-13 11:53:26 -08005340 mResampler = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005341
Dima Zavin799a70e2011-04-18 16:57:27 -07005342 mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common);
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07005343 mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common);
5344 mChannelCount = (uint16_t)popcount(mChannelMask);
Dima Zavin799a70e2011-04-18 16:57:27 -07005345 mFormat = mInput->stream->common.get_format(&mInput->stream->common);
Glenn Kastenb9980652012-01-11 09:48:27 -08005346 mFrameSize = audio_stream_frame_size(&mInput->stream->common);
Dima Zavin799a70e2011-04-18 16:57:27 -07005347 mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005348 mFrameCount = mInputBytes / mFrameSize;
5349 mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount];
5350
5351 if (mSampleRate != mReqSampleRate && mChannelCount < 3 && mReqChannelCount < 3)
5352 {
5353 int channelCount;
5354 // optmization: if mono to mono, use the resampler in stereo to stereo mode to avoid
5355 // stereo to mono post process as the resampler always outputs stereo.
5356 if (mChannelCount == 1 && mReqChannelCount == 2) {
5357 channelCount = 1;
5358 } else {
5359 channelCount = 2;
5360 }
5361 mResampler = AudioResampler::create(16, channelCount, mReqSampleRate);
5362 mResampler->setSampleRate(mSampleRate);
5363 mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN);
5364 mRsmpOutBuffer = new int32_t[mFrameCount * 2];
5365
5366 // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples
5367 if (mChannelCount == 1 && mReqChannelCount == 1) {
5368 mFrameCount >>= 1;
5369 }
5370
5371 }
5372 mRsmpInIndex = mFrameCount;
5373}
5374
5375unsigned int AudioFlinger::RecordThread::getInputFramesLost()
5376{
Eric Laurentb8ba0a92011-08-07 16:32:26 -07005377 Mutex::Autolock _l(mLock);
5378 if (initCheck() != NO_ERROR) {
5379 return 0;
5380 }
5381
Dima Zavin799a70e2011-04-18 16:57:27 -07005382 return mInput->stream->get_input_frames_lost(mInput->stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005383}
5384
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005385uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId)
5386{
5387 Mutex::Autolock _l(mLock);
5388 uint32_t result = 0;
5389 if (getEffectChain_l(sessionId) != 0) {
5390 result = EFFECT_SESSION;
5391 }
5392
5393 if (mTrack != NULL && sessionId == mTrack->sessionId()) {
5394 result |= TRACK_SESSION;
5395 }
5396
5397 return result;
5398}
5399
Eric Laurent59bd0da2011-08-01 09:52:20 -07005400AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track()
5401{
5402 Mutex::Autolock _l(mLock);
5403 return mTrack;
5404}
5405
Glenn Kastenaed850d2012-01-26 09:46:34 -08005406AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() const
Eric Laurentb8ba0a92011-08-07 16:32:26 -07005407{
5408 Mutex::Autolock _l(mLock);
5409 return mInput;
5410}
5411
5412AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput()
5413{
5414 Mutex::Autolock _l(mLock);
5415 AudioStreamIn *input = mInput;
5416 mInput = NULL;
5417 return input;
5418}
5419
5420// this method must always be called either with ThreadBase mLock held or inside the thread loop
5421audio_stream_t* AudioFlinger::RecordThread::stream()
5422{
5423 if (mInput == NULL) {
5424 return NULL;
5425 }
5426 return &mInput->stream->common;
5427}
5428
5429
Mathias Agopian65ab4712010-07-14 17:59:35 -07005430// ----------------------------------------------------------------------------
5431
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005432audio_io_handle_t AudioFlinger::openOutput(uint32_t *pDevices,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005433 uint32_t *pSamplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08005434 audio_format_t *pFormat,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005435 uint32_t *pChannels,
5436 uint32_t *pLatencyMs,
5437 uint32_t flags)
5438{
5439 status_t status;
5440 PlaybackThread *thread = NULL;
5441 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
5442 uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
Glenn Kasten58f30212012-01-12 12:27:51 -08005443 audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005444 uint32_t channels = pChannels ? *pChannels : 0;
5445 uint32_t latency = pLatencyMs ? *pLatencyMs : 0;
Dima Zavin799a70e2011-04-18 16:57:27 -07005446 audio_stream_out_t *outStream;
5447 audio_hw_device_t *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005448
Steve Block3856b092011-10-20 11:56:00 +01005449 ALOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
Mathias Agopian65ab4712010-07-14 17:59:35 -07005450 pDevices ? *pDevices : 0,
5451 samplingRate,
5452 format,
5453 channels,
5454 flags);
5455
5456 if (pDevices == NULL || *pDevices == 0) {
5457 return 0;
5458 }
Dima Zavin799a70e2011-04-18 16:57:27 -07005459
Mathias Agopian65ab4712010-07-14 17:59:35 -07005460 Mutex::Autolock _l(mLock);
5461
Dima Zavin799a70e2011-04-18 16:57:27 -07005462 outHwDev = findSuitableHwDev_l(*pDevices);
5463 if (outHwDev == NULL)
5464 return 0;
5465
Glenn Kasten58f30212012-01-12 12:27:51 -08005466 status = outHwDev->open_output_stream(outHwDev, *pDevices, &format,
Dima Zavin799a70e2011-04-18 16:57:27 -07005467 &channels, &samplingRate, &outStream);
Steve Block3856b092011-10-20 11:56:00 +01005468 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07005469 outStream,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005470 samplingRate,
5471 format,
5472 channels,
5473 status);
5474
5475 mHardwareStatus = AUDIO_HW_IDLE;
Dima Zavin799a70e2011-04-18 16:57:27 -07005476 if (outStream != NULL) {
5477 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005478 audio_io_handle_t id = nextUniqueId();
Dima Zavin799a70e2011-04-18 16:57:27 -07005479
Dima Zavinfce7a472011-04-19 22:30:36 -07005480 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) ||
5481 (format != AUDIO_FORMAT_PCM_16_BIT) ||
5482 (channels != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005483 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01005484 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005485 } else {
5486 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01005487 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005488 }
5489 mPlaybackThreads.add(id, thread);
5490
Glenn Kastena0d68332012-01-27 16:47:15 -08005491 if (pSamplingRate != NULL) *pSamplingRate = samplingRate;
5492 if (pFormat != NULL) *pFormat = format;
5493 if (pChannels != NULL) *pChannels = channels;
5494 if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005495
5496 // notify client processes of the new output creation
5497 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
5498 return id;
5499 }
5500
5501 return 0;
5502}
5503
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005504audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
5505 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005506{
5507 Mutex::Autolock _l(mLock);
5508 MixerThread *thread1 = checkMixerThread_l(output1);
5509 MixerThread *thread2 = checkMixerThread_l(output2);
5510
5511 if (thread1 == NULL || thread2 == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00005512 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005513 return 0;
5514 }
5515
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005516 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005517 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
5518 thread->addOutputTrack(thread2);
5519 mPlaybackThreads.add(id, thread);
5520 // notify client processes of the new output creation
5521 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
5522 return id;
5523}
5524
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005525status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005526{
5527 // keep strong reference on the playback thread so that
5528 // it is not destroyed while exit() is executed
5529 sp <PlaybackThread> thread;
5530 {
5531 Mutex::Autolock _l(mLock);
5532 thread = checkPlaybackThread_l(output);
5533 if (thread == NULL) {
5534 return BAD_VALUE;
5535 }
5536
Steve Block3856b092011-10-20 11:56:00 +01005537 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005538
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005539 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005540 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005541 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005542 DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
5543 dupThread->removeOutputTrack((MixerThread *)thread.get());
5544 }
5545 }
5546 }
Glenn Kastena1117922012-01-26 10:53:32 -08005547 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005548 mPlaybackThreads.removeItem(output);
5549 }
5550 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08005551 // The thread entity (active unit of execution) is no longer running here,
5552 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07005553
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005554 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07005555 AudioStreamOut *out = thread->clearOutput();
Glenn Kastenaed850d2012-01-26 09:46:34 -08005556 assert(out != NULL);
Eric Laurentb8ba0a92011-08-07 16:32:26 -07005557 // from now on thread->mOutput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07005558 out->hwDev->close_output_stream(out->hwDev, out->stream);
5559 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005560 }
5561 return NO_ERROR;
5562}
5563
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005564status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005565{
5566 Mutex::Autolock _l(mLock);
5567 PlaybackThread *thread = checkPlaybackThread_l(output);
5568
5569 if (thread == NULL) {
5570 return BAD_VALUE;
5571 }
5572
Steve Block3856b092011-10-20 11:56:00 +01005573 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005574 thread->suspend();
5575
5576 return NO_ERROR;
5577}
5578
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005579status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005580{
5581 Mutex::Autolock _l(mLock);
5582 PlaybackThread *thread = checkPlaybackThread_l(output);
5583
5584 if (thread == NULL) {
5585 return BAD_VALUE;
5586 }
5587
Steve Block3856b092011-10-20 11:56:00 +01005588 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005589
5590 thread->restore();
5591
5592 return NO_ERROR;
5593}
5594
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005595audio_io_handle_t AudioFlinger::openInput(uint32_t *pDevices,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005596 uint32_t *pSamplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08005597 audio_format_t *pFormat,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005598 uint32_t *pChannels,
Glenn Kastende9719b2012-01-27 12:32:34 -08005599 audio_in_acoustics_t acoustics)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005600{
5601 status_t status;
5602 RecordThread *thread = NULL;
5603 uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
Glenn Kasten58f30212012-01-12 12:27:51 -08005604 audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005605 uint32_t channels = pChannels ? *pChannels : 0;
5606 uint32_t reqSamplingRate = samplingRate;
Glenn Kasten58f30212012-01-12 12:27:51 -08005607 audio_format_t reqFormat = format;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005608 uint32_t reqChannels = channels;
Dima Zavin799a70e2011-04-18 16:57:27 -07005609 audio_stream_in_t *inStream;
5610 audio_hw_device_t *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005611
5612 if (pDevices == NULL || *pDevices == 0) {
5613 return 0;
5614 }
Dima Zavin799a70e2011-04-18 16:57:27 -07005615
Mathias Agopian65ab4712010-07-14 17:59:35 -07005616 Mutex::Autolock _l(mLock);
5617
Dima Zavin799a70e2011-04-18 16:57:27 -07005618 inHwDev = findSuitableHwDev_l(*pDevices);
5619 if (inHwDev == NULL)
5620 return 0;
5621
Glenn Kasten58f30212012-01-12 12:27:51 -08005622 status = inHwDev->open_input_stream(inHwDev, *pDevices, &format,
Dima Zavin799a70e2011-04-18 16:57:27 -07005623 &channels, &samplingRate,
Glenn Kastende9719b2012-01-27 12:32:34 -08005624 acoustics,
Dima Zavin799a70e2011-04-18 16:57:27 -07005625 &inStream);
Steve Block3856b092011-10-20 11:56:00 +01005626 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, acoustics %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07005627 inStream,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005628 samplingRate,
5629 format,
5630 channels,
5631 acoustics,
5632 status);
5633
5634 // If the input could not be opened with the requested parameters and we can handle the conversion internally,
5635 // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo
5636 // or stereo to mono conversions on 16 bit PCM inputs.
Dima Zavin799a70e2011-04-18 16:57:27 -07005637 if (inStream == NULL && status == BAD_VALUE &&
Dima Zavinfce7a472011-04-19 22:30:36 -07005638 reqFormat == format && format == AUDIO_FORMAT_PCM_16_BIT &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07005639 (samplingRate <= 2 * reqSamplingRate) &&
Dima Zavinfce7a472011-04-19 22:30:36 -07005640 (popcount(channels) < 3) && (popcount(reqChannels) < 3)) {
Steve Block3856b092011-10-20 11:56:00 +01005641 ALOGV("openInput() reopening with proposed sampling rate and channels");
Glenn Kasten58f30212012-01-12 12:27:51 -08005642 status = inHwDev->open_input_stream(inHwDev, *pDevices, &format,
Dima Zavin799a70e2011-04-18 16:57:27 -07005643 &channels, &samplingRate,
Glenn Kastende9719b2012-01-27 12:32:34 -08005644 acoustics,
Dima Zavin799a70e2011-04-18 16:57:27 -07005645 &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005646 }
5647
Dima Zavin799a70e2011-04-18 16:57:27 -07005648 if (inStream != NULL) {
5649 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
5650
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005651 audio_io_handle_t id = nextUniqueId();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005652 // Start record thread
5653 // RecorThread require both input and output device indication to forward to audio
5654 // pre processing modules
5655 uint32_t device = (*pDevices) | primaryOutputDevice_l();
5656 thread = new RecordThread(this,
5657 input,
5658 reqSamplingRate,
5659 reqChannels,
5660 id,
5661 device);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005662 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01005663 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kastena0d68332012-01-27 16:47:15 -08005664 if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate;
5665 if (pFormat != NULL) *pFormat = format;
5666 if (pChannels != NULL) *pChannels = reqChannels;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005667
Dima Zavin799a70e2011-04-18 16:57:27 -07005668 input->stream->common.standby(&input->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005669
5670 // notify client processes of the new input creation
5671 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
5672 return id;
5673 }
5674
5675 return 0;
5676}
5677
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005678status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005679{
5680 // keep strong reference on the record thread so that
5681 // it is not destroyed while exit() is executed
5682 sp <RecordThread> thread;
5683 {
5684 Mutex::Autolock _l(mLock);
5685 thread = checkRecordThread_l(input);
5686 if (thread == NULL) {
5687 return BAD_VALUE;
5688 }
5689
Steve Block3856b092011-10-20 11:56:00 +01005690 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08005691 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005692 mRecordThreads.removeItem(input);
5693 }
5694 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08005695 // The thread entity (active unit of execution) is no longer running here,
5696 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07005697
Eric Laurentb8ba0a92011-08-07 16:32:26 -07005698 AudioStreamIn *in = thread->clearInput();
Glenn Kastenaed850d2012-01-26 09:46:34 -08005699 assert(in != NULL);
Eric Laurentb8ba0a92011-08-07 16:32:26 -07005700 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07005701 in->hwDev->close_input_stream(in->hwDev, in->stream);
5702 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005703
5704 return NO_ERROR;
5705}
5706
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005707status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005708{
5709 Mutex::Autolock _l(mLock);
5710 MixerThread *dstThread = checkMixerThread_l(output);
5711 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00005712 ALOGW("setStreamOutput() bad output id %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005713 return BAD_VALUE;
5714 }
5715
Steve Block3856b092011-10-20 11:56:00 +01005716 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005717 audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream);
5718
Eric Laurent9f6530f2011-08-30 10:18:54 -07005719 dstThread->setStreamValid(stream, true);
5720
Mathias Agopian65ab4712010-07-14 17:59:35 -07005721 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
5722 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Glenn Kastena1117922012-01-26 10:53:32 -08005723 if (thread != dstThread && thread->type() != ThreadBase::DIRECT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005724 MixerThread *srcThread = (MixerThread *)thread;
Eric Laurent9f6530f2011-08-30 10:18:54 -07005725 srcThread->setStreamValid(stream, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005726 srcThread->invalidateTracks(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005727 }
Eric Laurentde070132010-07-13 04:45:46 -07005728 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07005729
5730 return NO_ERROR;
5731}
5732
5733
5734int AudioFlinger::newAudioSessionId()
5735{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005736 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005737}
5738
Marco Nelissen3a34bef2011-08-02 13:33:41 -07005739void AudioFlinger::acquireAudioSessionId(int audioSession)
5740{
5741 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08005742 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01005743 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08005744 size_t num = mAudioSessionRefs.size();
5745 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07005746 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
5747 if (ref->sessionid == audioSession && ref->pid == caller) {
5748 ref->cnt++;
Steve Block3856b092011-10-20 11:56:00 +01005749 ALOGV(" incremented refcount to %d", ref->cnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07005750 return;
5751 }
5752 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08005753 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
5754 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07005755}
5756
5757void AudioFlinger::releaseAudioSessionId(int audioSession)
5758{
5759 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08005760 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01005761 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08005762 size_t num = mAudioSessionRefs.size();
5763 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07005764 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
5765 if (ref->sessionid == audioSession && ref->pid == caller) {
5766 ref->cnt--;
Steve Block3856b092011-10-20 11:56:00 +01005767 ALOGV(" decremented refcount to %d", ref->cnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07005768 if (ref->cnt == 0) {
5769 mAudioSessionRefs.removeAt(i);
5770 delete ref;
5771 purgeStaleEffects_l();
5772 }
5773 return;
5774 }
5775 }
Steve Block5ff1dd52012-01-05 23:22:43 +00005776 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07005777}
5778
5779void AudioFlinger::purgeStaleEffects_l() {
5780
Steve Block3856b092011-10-20 11:56:00 +01005781 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07005782
5783 Vector< sp<EffectChain> > chains;
5784
5785 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
5786 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
5787 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
5788 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07005789 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
5790 chains.push(ec);
5791 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07005792 }
5793 }
5794 for (size_t i = 0; i < mRecordThreads.size(); i++) {
5795 sp<RecordThread> t = mRecordThreads.valueAt(i);
5796 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
5797 sp<EffectChain> ec = t->mEffectChains[j];
5798 chains.push(ec);
5799 }
5800 }
5801
5802 for (size_t i = 0; i < chains.size(); i++) {
5803 sp<EffectChain> ec = chains[i];
5804 int sessionid = ec->sessionId();
5805 sp<ThreadBase> t = ec->mThread.promote();
5806 if (t == 0) {
5807 continue;
5808 }
5809 size_t numsessionrefs = mAudioSessionRefs.size();
5810 bool found = false;
5811 for (size_t k = 0; k < numsessionrefs; k++) {
5812 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
5813 if (ref->sessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01005814 ALOGV(" session %d still exists for %d with %d refs",
Marco Nelissen3a34bef2011-08-02 13:33:41 -07005815 sessionid, ref->pid, ref->cnt);
5816 found = true;
5817 break;
5818 }
5819 }
5820 if (!found) {
5821 // remove all effects from the chain
5822 while (ec->mEffects.size()) {
5823 sp<EffectModule> effect = ec->mEffects[0];
5824 effect->unPin();
5825 Mutex::Autolock _l (t->mLock);
5826 t->removeEffect_l(effect);
5827 for (size_t j = 0; j < effect->mHandles.size(); j++) {
5828 sp<EffectHandle> handle = effect->mHandles[j].promote();
5829 if (handle != 0) {
5830 handle->mEffect.clear();
Eric Laurenta85a74a2011-10-19 11:44:54 -07005831 if (handle->mHasControl && handle->mEnabled) {
5832 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
5833 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07005834 }
5835 }
5836 AudioSystem::unregisterEffect(effect->id());
5837 }
5838 }
5839 }
5840 return;
5841}
5842
Mathias Agopian65ab4712010-07-14 17:59:35 -07005843// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005844AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07005845{
Glenn Kastena1117922012-01-26 10:53:32 -08005846 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005847}
5848
5849// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005850AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07005851{
5852 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08005853 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005854}
5855
5856// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005857AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07005858{
Glenn Kastena1117922012-01-26 10:53:32 -08005859 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005860}
5861
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005862uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07005863{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005864 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005865}
5866
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005867AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l()
5868{
5869 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
5870 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07005871 AudioStreamOut *output = thread->getOutput();
5872 if (output != NULL && output->hwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005873 return thread;
5874 }
5875 }
5876 return NULL;
5877}
5878
5879uint32_t AudioFlinger::primaryOutputDevice_l()
5880{
5881 PlaybackThread *thread = primaryPlaybackThread_l();
5882
5883 if (thread == NULL) {
5884 return 0;
5885 }
5886
5887 return thread->device();
5888}
5889
5890
Mathias Agopian65ab4712010-07-14 17:59:35 -07005891// ----------------------------------------------------------------------------
5892// Effect management
5893// ----------------------------------------------------------------------------
5894
5895
Glenn Kastenf587ba52012-01-26 16:25:10 -08005896status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07005897{
5898 Mutex::Autolock _l(mLock);
5899 return EffectQueryNumberEffects(numEffects);
5900}
5901
Glenn Kastenf587ba52012-01-26 16:25:10 -08005902status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07005903{
5904 Mutex::Autolock _l(mLock);
5905 return EffectQueryEffect(index, descriptor);
5906}
5907
Glenn Kasten5e92a782012-01-30 07:40:52 -08005908status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08005909 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07005910{
5911 Mutex::Autolock _l(mLock);
5912 return EffectGetDescriptor(pUuid, descriptor);
5913}
5914
5915
Mathias Agopian65ab4712010-07-14 17:59:35 -07005916sp<IEffect> AudioFlinger::createEffect(pid_t pid,
5917 effect_descriptor_t *pDesc,
5918 const sp<IEffectClient>& effectClient,
5919 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005920 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005921 int sessionId,
5922 status_t *status,
5923 int *id,
5924 int *enabled)
5925{
5926 status_t lStatus = NO_ERROR;
5927 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005928 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005929
Glenn Kasten98ec94c2012-01-25 14:28:29 -08005930 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005931 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005932
5933 if (pDesc == NULL) {
5934 lStatus = BAD_VALUE;
5935 goto Exit;
5936 }
5937
Eric Laurent84e9a102010-09-23 16:10:16 -07005938 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07005939 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07005940 lStatus = PERMISSION_DENIED;
5941 goto Exit;
5942 }
5943
Dima Zavinfce7a472011-04-19 22:30:36 -07005944 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07005945 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08005946 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07005947 lStatus = PERMISSION_DENIED;
5948 goto Exit;
5949 }
5950
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005951 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07005952 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07005953 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07005954 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07005955 lStatus = BAD_VALUE;
5956 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07005957 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07005958 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005959 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07005960 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005961 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07005962 }
5963 }
5964
Mathias Agopian65ab4712010-07-14 17:59:35 -07005965 {
5966 Mutex::Autolock _l(mLock);
5967
Mathias Agopian65ab4712010-07-14 17:59:35 -07005968
5969 if (!EffectIsNullUuid(&pDesc->uuid)) {
5970 // if uuid is specified, request effect descriptor
5971 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
5972 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00005973 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005974 goto Exit;
5975 }
5976 } else {
5977 // if uuid is not specified, look for an available implementation
5978 // of the required type in effect factory
5979 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00005980 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07005981 lStatus = BAD_VALUE;
5982 goto Exit;
5983 }
5984 uint32_t numEffects = 0;
5985 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07005986 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07005987 bool found = false;
5988
5989 lStatus = EffectQueryNumberEffects(&numEffects);
5990 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00005991 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005992 goto Exit;
5993 }
5994 for (uint32_t i = 0; i < numEffects; i++) {
5995 lStatus = EffectQueryEffect(i, &desc);
5996 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00005997 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005998 continue;
5999 }
6000 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
6001 // If matching type found save effect descriptor. If the session is
6002 // 0 and the effect is not auxiliary, continue enumeration in case
6003 // an auxiliary version of this effect type is available
6004 found = true;
6005 memcpy(&d, &desc, sizeof(effect_descriptor_t));
Dima Zavinfce7a472011-04-19 22:30:36 -07006006 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07006007 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6008 break;
6009 }
6010 }
6011 }
6012 if (!found) {
6013 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00006014 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07006015 goto Exit;
6016 }
6017 // For same effect type, chose auxiliary version over insert version if
6018 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07006019 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07006020 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
6021 memcpy(&desc, &d, sizeof(effect_descriptor_t));
6022 }
6023 }
6024
6025 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07006026 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07006027 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6028 lStatus = INVALID_OPERATION;
6029 goto Exit;
6030 }
6031
Eric Laurent59255e42011-07-27 19:49:51 -07006032 // check recording permission for visualizer
6033 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
6034 !recordingAllowed()) {
6035 lStatus = PERMISSION_DENIED;
6036 goto Exit;
6037 }
6038
Mathias Agopian65ab4712010-07-14 17:59:35 -07006039 // return effect descriptor
6040 memcpy(pDesc, &desc, sizeof(effect_descriptor_t));
6041
6042 // If output is not specified try to find a matching audio session ID in one of the
6043 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07006044 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
6045 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006046 // Note: io is never 0 when creating an effect on an input
6047 if (io == 0) {
Eric Laurent84e9a102010-09-23 16:10:16 -07006048 // look for the thread where the specified audio session is present
6049 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
6050 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006051 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07006052 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07006053 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006054 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006055 if (io == 0) {
6056 for (size_t i = 0; i < mRecordThreads.size(); i++) {
6057 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
6058 io = mRecordThreads.keyAt(i);
6059 break;
6060 }
6061 }
6062 }
Eric Laurent84e9a102010-09-23 16:10:16 -07006063 // If no output thread contains the requested session ID, default to
6064 // first output. The effect chain will be moved to the correct output
6065 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006066 if (io == 0 && mPlaybackThreads.size()) {
6067 io = mPlaybackThreads.keyAt(0);
6068 }
Steve Block3856b092011-10-20 11:56:00 +01006069 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006070 }
6071 ThreadBase *thread = checkRecordThread_l(io);
6072 if (thread == NULL) {
6073 thread = checkPlaybackThread_l(io);
6074 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00006075 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006076 lStatus = BAD_VALUE;
6077 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07006078 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006079 }
Eric Laurent84e9a102010-09-23 16:10:16 -07006080
Glenn Kasten98ec94c2012-01-25 14:28:29 -08006081 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006082
Marco Nelissen3a34bef2011-08-02 13:33:41 -07006083 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07006084 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
6085 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006086 if (handle != 0 && id != NULL) {
6087 *id = handle->id();
6088 }
6089 }
6090
6091Exit:
6092 if(status) {
6093 *status = lStatus;
6094 }
6095 return handle;
6096}
6097
Glenn Kasten72ef00d2012-01-17 11:09:42 -08006098status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
6099 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07006100{
Steve Block3856b092011-10-20 11:56:00 +01006101 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07006102 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07006103 Mutex::Autolock _l(mLock);
6104 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00006105 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07006106 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006107 }
Eric Laurentde070132010-07-13 04:45:46 -07006108 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
6109 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00006110 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07006111 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006112 }
Eric Laurentde070132010-07-13 04:45:46 -07006113 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
6114 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00006115 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07006116 return BAD_VALUE;
6117 }
6118
6119 Mutex::Autolock _dl(dstThread->mLock);
6120 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07006121 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07006122
Mathias Agopian65ab4712010-07-14 17:59:35 -07006123 return NO_ERROR;
6124}
6125
Marco Nelissen3a34bef2011-08-02 13:33:41 -07006126// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07006127status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07006128 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07006129 AudioFlinger::PlaybackThread *dstThread,
6130 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07006131{
Steve Block3856b092011-10-20 11:56:00 +01006132 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07006133 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07006134
Eric Laurent59255e42011-07-27 19:49:51 -07006135 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07006136 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00006137 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07006138 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07006139 return INVALID_OPERATION;
6140 }
6141
Eric Laurent39e94f82010-07-28 01:32:47 -07006142 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07006143 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07006144 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07006145 // removed.
6146 srcThread->removeEffectChain_l(chain);
6147
6148 // transfer all effects one by one so that new effect chain is created on new thread with
6149 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08006150 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07006151 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07006152 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07006153 sp<EffectModule> effect = chain->getEffectFromId_l(0);
6154 while (effect != 0) {
6155 srcThread->removeEffect_l(effect);
6156 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07006157 // removeEffect_l() has stopped the effect if it was active so it must be restarted
6158 if (effect->state() == EffectModule::ACTIVE ||
6159 effect->state() == EffectModule::STOPPING) {
6160 effect->start();
6161 }
Eric Laurent39e94f82010-07-28 01:32:47 -07006162 // if the move request is not received from audio policy manager, the effect must be
6163 // re-registered with the new strategy and output
6164 if (dstChain == 0) {
6165 dstChain = effect->chain().promote();
6166 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00006167 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07006168 srcThread->addEffect_l(effect);
6169 return NO_INIT;
6170 }
6171 strategy = dstChain->strategy();
6172 }
6173 if (reRegister) {
6174 AudioSystem::unregisterEffect(effect->id());
6175 AudioSystem::registerEffect(&effect->desc(),
6176 dstOutput,
6177 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07006178 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07006179 effect->id());
6180 }
Eric Laurentde070132010-07-13 04:45:46 -07006181 effect = chain->getEffectFromId_l(0);
6182 }
6183
6184 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006185}
6186
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006187
Mathias Agopian65ab4712010-07-14 17:59:35 -07006188// PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006189sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l(
Mathias Agopian65ab4712010-07-14 17:59:35 -07006190 const sp<AudioFlinger::Client>& client,
6191 const sp<IEffectClient>& effectClient,
6192 int32_t priority,
6193 int sessionId,
6194 effect_descriptor_t *desc,
6195 int *enabled,
6196 status_t *status
6197 )
6198{
6199 sp<EffectModule> effect;
6200 sp<EffectHandle> handle;
6201 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006202 sp<EffectChain> chain;
Eric Laurentde070132010-07-13 04:45:46 -07006203 bool chainCreated = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006204 bool effectCreated = false;
6205 bool effectRegistered = false;
6206
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006207 lStatus = initCheck();
6208 if (lStatus != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00006209 ALOGW("createEffect_l() Audio driver not initialized.");
Mathias Agopian65ab4712010-07-14 17:59:35 -07006210 goto Exit;
6211 }
6212
6213 // Do not allow effects with session ID 0 on direct output or duplicating threads
6214 // TODO: add rule for hw accelerated effects on direct outputs with non PCM format
Dima Zavinfce7a472011-04-19 22:30:36 -07006215 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) {
Steve Block5ff1dd52012-01-05 23:22:43 +00006216 ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d",
Eric Laurentde070132010-07-13 04:45:46 -07006217 desc->name, sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006218 lStatus = BAD_VALUE;
6219 goto Exit;
6220 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006221 // Only Pre processor effects are allowed on input threads and only on input threads
Glenn Kastena1117922012-01-26 10:53:32 -08006222 if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00006223 ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006224 desc->name, desc->flags, mType);
6225 lStatus = BAD_VALUE;
6226 goto Exit;
6227 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006228
Steve Block3856b092011-10-20 11:56:00 +01006229 ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006230
6231 { // scope for mLock
6232 Mutex::Autolock _l(mLock);
6233
6234 // check for existing effect chain with the requested audio session
6235 chain = getEffectChain_l(sessionId);
6236 if (chain == 0) {
6237 // create a new chain for this session
Steve Block3856b092011-10-20 11:56:00 +01006238 ALOGV("createEffect_l() new effect chain for session %d", sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006239 chain = new EffectChain(this, sessionId);
6240 addEffectChain_l(chain);
Eric Laurentde070132010-07-13 04:45:46 -07006241 chain->setStrategy(getStrategyForSession_l(sessionId));
6242 chainCreated = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006243 } else {
Eric Laurentcab11242010-07-15 12:50:15 -07006244 effect = chain->getEffectFromDesc_l(desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006245 }
6246
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08006247 ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07006248
6249 if (effect == 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006250 int id = mAudioFlinger->nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006251 // Check CPU and memory usage
Eric Laurentde070132010-07-13 04:45:46 -07006252 lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006253 if (lStatus != NO_ERROR) {
6254 goto Exit;
6255 }
6256 effectRegistered = true;
6257 // create a new effect module if none present in the chain
Eric Laurentde070132010-07-13 04:45:46 -07006258 effect = new EffectModule(this, chain, desc, id, sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006259 lStatus = effect->status();
6260 if (lStatus != NO_ERROR) {
6261 goto Exit;
6262 }
Eric Laurentcab11242010-07-15 12:50:15 -07006263 lStatus = chain->addEffect_l(effect);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006264 if (lStatus != NO_ERROR) {
6265 goto Exit;
6266 }
6267 effectCreated = true;
6268
6269 effect->setDevice(mDevice);
6270 effect->setMode(mAudioFlinger->getMode());
6271 }
6272 // create effect handle and connect it to effect module
6273 handle = new EffectHandle(effect, client, effectClient, priority);
6274 lStatus = effect->addHandle(handle);
Glenn Kastena0d68332012-01-27 16:47:15 -08006275 if (enabled != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006276 *enabled = (int)effect->isEnabled();
6277 }
6278 }
6279
6280Exit:
6281 if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
Eric Laurentde070132010-07-13 04:45:46 -07006282 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006283 if (effectCreated) {
Eric Laurentde070132010-07-13 04:45:46 -07006284 chain->removeEffect_l(effect);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006285 }
6286 if (effectRegistered) {
Eric Laurentde070132010-07-13 04:45:46 -07006287 AudioSystem::unregisterEffect(effect->id());
6288 }
6289 if (chainCreated) {
6290 removeEffectChain_l(chain);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006291 }
6292 handle.clear();
6293 }
6294
6295 if(status) {
6296 *status = lStatus;
6297 }
6298 return handle;
6299}
6300
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006301sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId)
6302{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006303 sp<EffectChain> chain = getEffectChain_l(sessionId);
Glenn Kasten090f0192012-01-30 13:00:02 -08006304 return chain != 0 ? chain->getEffectFromId_l(effectId) : 0;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006305}
6306
Eric Laurentde070132010-07-13 04:45:46 -07006307// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and
6308// PlaybackThread::mLock held
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006309status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect)
Eric Laurentde070132010-07-13 04:45:46 -07006310{
6311 // check for existing effect chain with the requested audio session
6312 int sessionId = effect->sessionId();
6313 sp<EffectChain> chain = getEffectChain_l(sessionId);
6314 bool chainCreated = false;
6315
6316 if (chain == 0) {
6317 // create a new chain for this session
Steve Block3856b092011-10-20 11:56:00 +01006318 ALOGV("addEffect_l() new effect chain for session %d", sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07006319 chain = new EffectChain(this, sessionId);
6320 addEffectChain_l(chain);
6321 chain->setStrategy(getStrategyForSession_l(sessionId));
6322 chainCreated = true;
6323 }
Steve Block3856b092011-10-20 11:56:00 +01006324 ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get());
Eric Laurentde070132010-07-13 04:45:46 -07006325
6326 if (chain->getEffectFromId_l(effect->id()) != 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00006327 ALOGW("addEffect_l() %p effect %s already present in chain %p",
Eric Laurentde070132010-07-13 04:45:46 -07006328 this, effect->desc().name, chain.get());
6329 return BAD_VALUE;
6330 }
6331
6332 status_t status = chain->addEffect_l(effect);
6333 if (status != NO_ERROR) {
6334 if (chainCreated) {
6335 removeEffectChain_l(chain);
6336 }
6337 return status;
6338 }
6339
6340 effect->setDevice(mDevice);
6341 effect->setMode(mAudioFlinger->getMode());
6342 return NO_ERROR;
6343}
6344
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006345void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) {
Eric Laurentde070132010-07-13 04:45:46 -07006346
Steve Block3856b092011-10-20 11:56:00 +01006347 ALOGV("removeEffect_l() %p effect %p", this, effect.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07006348 effect_descriptor_t desc = effect->desc();
Eric Laurentde070132010-07-13 04:45:46 -07006349 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6350 detachAuxEffect_l(effect->id());
6351 }
6352
6353 sp<EffectChain> chain = effect->chain().promote();
6354 if (chain != 0) {
6355 // remove effect chain if removing last effect
6356 if (chain->removeEffect_l(effect) == 0) {
6357 removeEffectChain_l(chain);
6358 }
6359 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00006360 ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get());
Eric Laurentde070132010-07-13 04:45:46 -07006361 }
6362}
6363
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006364void AudioFlinger::ThreadBase::lockEffectChains_l(
6365 Vector<sp <AudioFlinger::EffectChain> >& effectChains)
6366{
6367 effectChains = mEffectChains;
6368 for (size_t i = 0; i < mEffectChains.size(); i++) {
6369 mEffectChains[i]->lock();
6370 }
6371}
6372
6373void AudioFlinger::ThreadBase::unlockEffectChains(
6374 Vector<sp <AudioFlinger::EffectChain> >& effectChains)
6375{
6376 for (size_t i = 0; i < effectChains.size(); i++) {
6377 effectChains[i]->unlock();
6378 }
6379}
6380
6381sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId)
6382{
6383 Mutex::Autolock _l(mLock);
6384 return getEffectChain_l(sessionId);
6385}
6386
6387sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId)
6388{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006389 size_t size = mEffectChains.size();
6390 for (size_t i = 0; i < size; i++) {
6391 if (mEffectChains[i]->sessionId() == sessionId) {
Glenn Kasten090f0192012-01-30 13:00:02 -08006392 return mEffectChains[i];
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006393 }
6394 }
Glenn Kasten090f0192012-01-30 13:00:02 -08006395 return 0;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006396}
6397
Glenn Kastenf78aee72012-01-04 11:00:47 -08006398void AudioFlinger::ThreadBase::setMode(audio_mode_t mode)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006399{
6400 Mutex::Autolock _l(mLock);
6401 size_t size = mEffectChains.size();
6402 for (size_t i = 0; i < size; i++) {
6403 mEffectChains[i]->setMode_l(mode);
6404 }
6405}
6406
6407void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect,
Marco Nelissen3a34bef2011-08-02 13:33:41 -07006408 const wp<EffectHandle>& handle,
Glenn Kasten58123c32012-02-03 10:32:24 -08006409 bool unpinIfLast) {
Eric Laurent59255e42011-07-27 19:49:51 -07006410
Mathias Agopian65ab4712010-07-14 17:59:35 -07006411 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01006412 ALOGV("disconnectEffect() %p effect %p", this, effect.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07006413 // delete the effect module if removing last handle on it
6414 if (effect->removeHandle(handle) == 0) {
Glenn Kasten58123c32012-02-03 10:32:24 -08006415 if (!effect->isPinned() || unpinIfLast) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07006416 removeEffect_l(effect);
6417 AudioSystem::unregisterEffect(effect->id());
6418 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006419 }
6420}
6421
6422status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain)
6423{
6424 int session = chain->sessionId();
6425 int16_t *buffer = mMixBuffer;
6426 bool ownsBuffer = false;
6427
Steve Block3856b092011-10-20 11:56:00 +01006428 ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006429 if (session > 0) {
6430 // Only one effect chain can be present in direct output thread and it uses
6431 // the mix buffer as input
6432 if (mType != DIRECT) {
6433 size_t numSamples = mFrameCount * mChannelCount;
6434 buffer = new int16_t[numSamples];
6435 memset(buffer, 0, numSamples * sizeof(int16_t));
Steve Block3856b092011-10-20 11:56:00 +01006436 ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006437 ownsBuffer = true;
6438 }
6439
6440 // Attach all tracks with same session ID to this chain.
6441 for (size_t i = 0; i < mTracks.size(); ++i) {
6442 sp<Track> track = mTracks[i];
6443 if (session == track->sessionId()) {
Steve Block3856b092011-10-20 11:56:00 +01006444 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006445 track->setMainBuffer(buffer);
Eric Laurentb469b942011-05-09 12:09:06 -07006446 chain->incTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006447 }
6448 }
6449
6450 // indicate all active tracks in the chain
6451 for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
6452 sp<Track> track = mActiveTracks[i].promote();
6453 if (track == 0) continue;
6454 if (session == track->sessionId()) {
Steve Block3856b092011-10-20 11:56:00 +01006455 ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session);
Eric Laurentb469b942011-05-09 12:09:06 -07006456 chain->incActiveTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006457 }
6458 }
6459 }
6460
6461 chain->setInBuffer(buffer, ownsBuffer);
6462 chain->setOutBuffer(mMixBuffer);
Dima Zavinfce7a472011-04-19 22:30:36 -07006463 // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect
Eric Laurentde070132010-07-13 04:45:46 -07006464 // chains list in order to be processed last as it contains output stage effects
Dima Zavinfce7a472011-04-19 22:30:36 -07006465 // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before
6466 // session AUDIO_SESSION_OUTPUT_STAGE to be processed
Mathias Agopian65ab4712010-07-14 17:59:35 -07006467 // after track specific effects and before output stage
Dima Zavinfce7a472011-04-19 22:30:36 -07006468 // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and
6469 // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX
Eric Laurentde070132010-07-13 04:45:46 -07006470 // Effect chain for other sessions are inserted at beginning of effect
6471 // chains list to be processed before output mix effects. Relative order between other
6472 // sessions is not important
Mathias Agopian65ab4712010-07-14 17:59:35 -07006473 size_t size = mEffectChains.size();
6474 size_t i = 0;
6475 for (i = 0; i < size; i++) {
6476 if (mEffectChains[i]->sessionId() < session) break;
6477 }
6478 mEffectChains.insertAt(chain, i);
Eric Laurent59255e42011-07-27 19:49:51 -07006479 checkSuspendOnAddEffectChain_l(chain);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006480
6481 return NO_ERROR;
6482}
6483
6484size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain)
6485{
6486 int session = chain->sessionId();
6487
Steve Block3856b092011-10-20 11:56:00 +01006488 ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006489
6490 for (size_t i = 0; i < mEffectChains.size(); i++) {
6491 if (chain == mEffectChains[i]) {
6492 mEffectChains.removeAt(i);
Eric Laurentb469b942011-05-09 12:09:06 -07006493 // detach all active tracks from the chain
6494 for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
6495 sp<Track> track = mActiveTracks[i].promote();
6496 if (track == 0) continue;
6497 if (session == track->sessionId()) {
Steve Block3856b092011-10-20 11:56:00 +01006498 ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d",
Eric Laurentb469b942011-05-09 12:09:06 -07006499 chain.get(), session);
6500 chain->decActiveTrackCnt();
6501 }
6502 }
6503
Mathias Agopian65ab4712010-07-14 17:59:35 -07006504 // detach all tracks with same session ID from this chain
6505 for (size_t i = 0; i < mTracks.size(); ++i) {
6506 sp<Track> track = mTracks[i];
6507 if (session == track->sessionId()) {
6508 track->setMainBuffer(mMixBuffer);
Eric Laurentb469b942011-05-09 12:09:06 -07006509 chain->decTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006510 }
6511 }
Eric Laurentde070132010-07-13 04:45:46 -07006512 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006513 }
6514 }
6515 return mEffectChains.size();
6516}
6517
Eric Laurentde070132010-07-13 04:45:46 -07006518status_t AudioFlinger::PlaybackThread::attachAuxEffect(
6519 const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07006520{
6521 Mutex::Autolock _l(mLock);
6522 return attachAuxEffect_l(track, EffectId);
6523}
6524
Eric Laurentde070132010-07-13 04:45:46 -07006525status_t AudioFlinger::PlaybackThread::attachAuxEffect_l(
6526 const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07006527{
6528 status_t status = NO_ERROR;
6529
6530 if (EffectId == 0) {
6531 track->setAuxBuffer(0, NULL);
6532 } else {
Dima Zavinfce7a472011-04-19 22:30:36 -07006533 // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX
6534 sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006535 if (effect != 0) {
6536 if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6537 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer());
6538 } else {
6539 status = INVALID_OPERATION;
6540 }
6541 } else {
6542 status = BAD_VALUE;
6543 }
6544 }
6545 return status;
6546}
6547
6548void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId)
6549{
6550 for (size_t i = 0; i < mTracks.size(); ++i) {
6551 sp<Track> track = mTracks[i];
6552 if (track->auxEffectId() == effectId) {
6553 attachAuxEffect_l(track, 0);
6554 }
6555 }
6556}
6557
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006558status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain)
6559{
6560 // only one chain per input thread
6561 if (mEffectChains.size() != 0) {
6562 return INVALID_OPERATION;
6563 }
Steve Block3856b092011-10-20 11:56:00 +01006564 ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006565
6566 chain->setInBuffer(NULL);
6567 chain->setOutBuffer(NULL);
6568
Eric Laurent59255e42011-07-27 19:49:51 -07006569 checkSuspendOnAddEffectChain_l(chain);
6570
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006571 mEffectChains.add(chain);
6572
6573 return NO_ERROR;
6574}
6575
6576size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain)
6577{
Steve Block3856b092011-10-20 11:56:00 +01006578 ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this);
Steve Block5ff1dd52012-01-05 23:22:43 +00006579 ALOGW_IF(mEffectChains.size() != 1,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006580 "removeEffectChain_l() %p invalid chain size %d on thread %p",
6581 chain.get(), mEffectChains.size(), this);
6582 if (mEffectChains.size() == 1) {
6583 mEffectChains.removeAt(0);
6584 }
6585 return 0;
6586}
6587
Mathias Agopian65ab4712010-07-14 17:59:35 -07006588// ----------------------------------------------------------------------------
6589// EffectModule implementation
6590// ----------------------------------------------------------------------------
6591
6592#undef LOG_TAG
6593#define LOG_TAG "AudioFlinger::EffectModule"
6594
Glenn Kasten9eaa5572012-01-20 13:32:16 -08006595AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07006596 const wp<AudioFlinger::EffectChain>& chain,
6597 effect_descriptor_t *desc,
6598 int id,
6599 int sessionId)
Glenn Kasten9eaa5572012-01-20 13:32:16 -08006600 : mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL),
Eric Laurent59255e42011-07-27 19:49:51 -07006601 mStatus(NO_INIT), mState(IDLE), mSuspended(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -07006602{
Steve Block3856b092011-10-20 11:56:00 +01006603 ALOGV("Constructor %p", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006604 int lStatus;
Glenn Kasten9eaa5572012-01-20 13:32:16 -08006605 if (thread == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006606 return;
6607 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006608
6609 memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t));
6610
6611 // create effect engine from effect factory
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006612 mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006613
6614 if (mStatus != NO_ERROR) {
6615 return;
6616 }
6617 lStatus = init();
6618 if (lStatus < 0) {
6619 mStatus = lStatus;
6620 goto Error;
6621 }
6622
Marco Nelissen3a34bef2011-08-02 13:33:41 -07006623 if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) {
6624 mPinned = true;
6625 }
Steve Block3856b092011-10-20 11:56:00 +01006626 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006627 return;
6628Error:
6629 EffectRelease(mEffectInterface);
6630 mEffectInterface = NULL;
Steve Block3856b092011-10-20 11:56:00 +01006631 ALOGV("Constructor Error %d", mStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006632}
6633
6634AudioFlinger::EffectModule::~EffectModule()
6635{
Steve Block3856b092011-10-20 11:56:00 +01006636 ALOGV("Destructor %p", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006637 if (mEffectInterface != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006638 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
6639 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
6640 sp<ThreadBase> thread = mThread.promote();
6641 if (thread != 0) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07006642 audio_stream_t *stream = thread->stream();
6643 if (stream != NULL) {
6644 stream->remove_audio_effect(stream, mEffectInterface);
6645 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006646 }
6647 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006648 // release effect engine
6649 EffectRelease(mEffectInterface);
6650 }
6651}
6652
Glenn Kasten435dbe62012-01-30 10:15:48 -08006653status_t AudioFlinger::EffectModule::addHandle(const sp<EffectHandle>& handle)
Mathias Agopian65ab4712010-07-14 17:59:35 -07006654{
6655 status_t status;
6656
6657 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006658 int priority = handle->priority();
6659 size_t size = mHandles.size();
6660 sp<EffectHandle> h;
6661 size_t i;
6662 for (i = 0; i < size; i++) {
6663 h = mHandles[i].promote();
6664 if (h == 0) continue;
6665 if (h->priority() <= priority) break;
6666 }
6667 // if inserted in first place, move effect control from previous owner to this handle
6668 if (i == 0) {
Eric Laurent59255e42011-07-27 19:49:51 -07006669 bool enabled = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006670 if (h != 0) {
Eric Laurent59255e42011-07-27 19:49:51 -07006671 enabled = h->enabled();
6672 h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006673 }
Eric Laurent59255e42011-07-27 19:49:51 -07006674 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006675 status = NO_ERROR;
6676 } else {
6677 status = ALREADY_EXISTS;
6678 }
Steve Block3856b092011-10-20 11:56:00 +01006679 ALOGV("addHandle() %p added handle %p in position %d", this, handle.get(), i);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006680 mHandles.insertAt(handle, i);
6681 return status;
6682}
6683
6684size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle)
6685{
6686 Mutex::Autolock _l(mLock);
6687 size_t size = mHandles.size();
6688 size_t i;
6689 for (i = 0; i < size; i++) {
6690 if (mHandles[i] == handle) break;
6691 }
6692 if (i == size) {
6693 return size;
6694 }
Steve Block3856b092011-10-20 11:56:00 +01006695 ALOGV("removeHandle() %p removed handle %p in position %d", this, handle.unsafe_get(), i);
Eric Laurent59255e42011-07-27 19:49:51 -07006696
6697 bool enabled = false;
6698 EffectHandle *hdl = handle.unsafe_get();
Glenn Kastena0d68332012-01-27 16:47:15 -08006699 if (hdl != NULL) {
Steve Block3856b092011-10-20 11:56:00 +01006700 ALOGV("removeHandle() unsafe_get OK");
Eric Laurent59255e42011-07-27 19:49:51 -07006701 enabled = hdl->enabled();
6702 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006703 mHandles.removeAt(i);
6704 size = mHandles.size();
6705 // if removed from first place, move effect control from this handle to next in line
6706 if (i == 0 && size != 0) {
6707 sp<EffectHandle> h = mHandles[0].promote();
6708 if (h != 0) {
Eric Laurent59255e42011-07-27 19:49:51 -07006709 h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006710 }
6711 }
6712
Eric Laurentec437d82011-07-26 20:54:46 -07006713 // Prevent calls to process() and other functions on effect interface from now on.
6714 // The effect engine will be released by the destructor when the last strong reference on
6715 // this object is released which can happen after next process is called.
Marco Nelissen3a34bef2011-08-02 13:33:41 -07006716 if (size == 0 && !mPinned) {
Eric Laurentec437d82011-07-26 20:54:46 -07006717 mState = DESTROYED;
Eric Laurentdac69112010-09-28 14:09:57 -07006718 }
6719
Mathias Agopian65ab4712010-07-14 17:59:35 -07006720 return size;
6721}
6722
Eric Laurent59255e42011-07-27 19:49:51 -07006723sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle()
6724{
6725 Mutex::Autolock _l(mLock);
Glenn Kasten090f0192012-01-30 13:00:02 -08006726 return mHandles.size() != 0 ? mHandles[0].promote() : 0;
Eric Laurent59255e42011-07-27 19:49:51 -07006727}
6728
Glenn Kasten58123c32012-02-03 10:32:24 -08006729void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpinIfLast)
Mathias Agopian65ab4712010-07-14 17:59:35 -07006730{
Glenn Kasten90bebef2012-01-27 15:24:38 -08006731 ALOGV("disconnect() %p handle %p", this, handle.unsafe_get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07006732 // keep a strong reference on this EffectModule to avoid calling the
6733 // destructor before we exit
6734 sp<EffectModule> keep(this);
6735 {
6736 sp<ThreadBase> thread = mThread.promote();
6737 if (thread != 0) {
Glenn Kasten58123c32012-02-03 10:32:24 -08006738 thread->disconnectEffect(keep, handle, unpinIfLast);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006739 }
6740 }
6741}
6742
6743void AudioFlinger::EffectModule::updateState() {
6744 Mutex::Autolock _l(mLock);
6745
6746 switch (mState) {
6747 case RESTART:
6748 reset_l();
6749 // FALL THROUGH
6750
6751 case STARTING:
6752 // clear auxiliary effect input buffer for next accumulation
6753 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6754 memset(mConfig.inputCfg.buffer.raw,
6755 0,
6756 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
6757 }
6758 start_l();
6759 mState = ACTIVE;
6760 break;
6761 case STOPPING:
6762 stop_l();
6763 mDisableWaitCnt = mMaxDisableWaitCnt;
6764 mState = STOPPED;
6765 break;
6766 case STOPPED:
6767 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
6768 // turn off sequence.
6769 if (--mDisableWaitCnt == 0) {
6770 reset_l();
6771 mState = IDLE;
6772 }
6773 break;
Eric Laurentec437d82011-07-26 20:54:46 -07006774 default: //IDLE , ACTIVE, DESTROYED
Mathias Agopian65ab4712010-07-14 17:59:35 -07006775 break;
6776 }
6777}
6778
6779void AudioFlinger::EffectModule::process()
6780{
6781 Mutex::Autolock _l(mLock);
6782
Eric Laurentec437d82011-07-26 20:54:46 -07006783 if (mState == DESTROYED || mEffectInterface == NULL ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07006784 mConfig.inputCfg.buffer.raw == NULL ||
6785 mConfig.outputCfg.buffer.raw == NULL) {
6786 return;
6787 }
6788
Eric Laurent8f45bd72010-08-31 13:50:07 -07006789 if (isProcessEnabled()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006790 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
6791 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kasten3b21c502011-12-15 09:52:39 -08006792 ditherAndClamp(mConfig.inputCfg.buffer.s32,
Mathias Agopian65ab4712010-07-14 17:59:35 -07006793 mConfig.inputCfg.buffer.s32,
Eric Laurentde070132010-07-13 04:45:46 -07006794 mConfig.inputCfg.buffer.frameCount/2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006795 }
6796
6797 // do the actual processing in the effect engine
6798 int ret = (*mEffectInterface)->process(mEffectInterface,
6799 &mConfig.inputCfg.buffer,
6800 &mConfig.outputCfg.buffer);
6801
6802 // force transition to IDLE state when engine is ready
6803 if (mState == STOPPED && ret == -ENODATA) {
6804 mDisableWaitCnt = 1;
6805 }
6806
6807 // clear auxiliary effect input buffer for next accumulation
6808 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurent73337482011-01-19 18:36:13 -08006809 memset(mConfig.inputCfg.buffer.raw, 0,
6810 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
Mathias Agopian65ab4712010-07-14 17:59:35 -07006811 }
6812 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
Eric Laurent73337482011-01-19 18:36:13 -08006813 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
6814 // If an insert effect is idle and input buffer is different from output buffer,
6815 // accumulate input onto output
Mathias Agopian65ab4712010-07-14 17:59:35 -07006816 sp<EffectChain> chain = mChain.promote();
Eric Laurentb469b942011-05-09 12:09:06 -07006817 if (chain != 0 && chain->activeTrackCnt() != 0) {
Eric Laurent73337482011-01-19 18:36:13 -08006818 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here
6819 int16_t *in = mConfig.inputCfg.buffer.s16;
6820 int16_t *out = mConfig.outputCfg.buffer.s16;
6821 for (size_t i = 0; i < frameCnt; i++) {
6822 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006823 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006824 }
6825 }
6826}
6827
6828void AudioFlinger::EffectModule::reset_l()
6829{
6830 if (mEffectInterface == NULL) {
6831 return;
6832 }
6833 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
6834}
6835
6836status_t AudioFlinger::EffectModule::configure()
6837{
6838 uint32_t channels;
6839 if (mEffectInterface == NULL) {
6840 return NO_INIT;
6841 }
6842
6843 sp<ThreadBase> thread = mThread.promote();
6844 if (thread == 0) {
6845 return DEAD_OBJECT;
6846 }
6847
6848 // TODO: handle configuration of effects replacing track process
6849 if (thread->channelCount() == 1) {
Eric Laurente1315cf2011-05-17 19:16:02 -07006850 channels = AUDIO_CHANNEL_OUT_MONO;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006851 } else {
Eric Laurente1315cf2011-05-17 19:16:02 -07006852 channels = AUDIO_CHANNEL_OUT_STEREO;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006853 }
6854
6855 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurente1315cf2011-05-17 19:16:02 -07006856 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006857 } else {
6858 mConfig.inputCfg.channels = channels;
6859 }
6860 mConfig.outputCfg.channels = channels;
Eric Laurente1315cf2011-05-17 19:16:02 -07006861 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
6862 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006863 mConfig.inputCfg.samplingRate = thread->sampleRate();
6864 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
6865 mConfig.inputCfg.bufferProvider.cookie = NULL;
6866 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
6867 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
6868 mConfig.outputCfg.bufferProvider.cookie = NULL;
6869 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
6870 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
6871 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
6872 // Insert effect:
Dima Zavinfce7a472011-04-19 22:30:36 -07006873 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
Eric Laurentde070132010-07-13 04:45:46 -07006874 // always overwrites output buffer: input buffer == output buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -07006875 // - in other sessions:
6876 // last effect in the chain accumulates in output buffer: input buffer != output buffer
6877 // other effect: overwrites output buffer: input buffer == output buffer
6878 // Auxiliary effect:
6879 // accumulates in output buffer: input buffer != output buffer
6880 // Therefore: accumulate <=> input buffer != output buffer
6881 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
6882 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
6883 } else {
6884 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
6885 }
6886 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
6887 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
6888 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
6889 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
6890
Steve Block3856b092011-10-20 11:56:00 +01006891 ALOGV("configure() %p thread %p buffer %p framecount %d",
Eric Laurentde070132010-07-13 04:45:46 -07006892 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
6893
Mathias Agopian65ab4712010-07-14 17:59:35 -07006894 status_t cmdStatus;
Eric Laurent25f43952010-07-28 05:40:18 -07006895 uint32_t size = sizeof(int);
6896 status_t status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurent3d5188b2011-12-16 15:30:36 -08006897 EFFECT_CMD_SET_CONFIG,
Eric Laurent25f43952010-07-28 05:40:18 -07006898 sizeof(effect_config_t),
6899 &mConfig,
6900 &size,
6901 &cmdStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006902 if (status == 0) {
6903 status = cmdStatus;
6904 }
6905
6906 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
6907 (1000 * mConfig.outputCfg.buffer.frameCount);
6908
6909 return status;
6910}
6911
6912status_t AudioFlinger::EffectModule::init()
6913{
6914 Mutex::Autolock _l(mLock);
6915 if (mEffectInterface == NULL) {
6916 return NO_INIT;
6917 }
6918 status_t cmdStatus;
Eric Laurent25f43952010-07-28 05:40:18 -07006919 uint32_t size = sizeof(status_t);
6920 status_t status = (*mEffectInterface)->command(mEffectInterface,
6921 EFFECT_CMD_INIT,
6922 0,
6923 NULL,
6924 &size,
6925 &cmdStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006926 if (status == 0) {
6927 status = cmdStatus;
6928 }
6929 return status;
6930}
6931
Eric Laurentec35a142011-10-05 17:42:25 -07006932status_t AudioFlinger::EffectModule::start()
6933{
6934 Mutex::Autolock _l(mLock);
6935 return start_l();
6936}
6937
Mathias Agopian65ab4712010-07-14 17:59:35 -07006938status_t AudioFlinger::EffectModule::start_l()
6939{
6940 if (mEffectInterface == NULL) {
6941 return NO_INIT;
6942 }
6943 status_t cmdStatus;
Eric Laurent25f43952010-07-28 05:40:18 -07006944 uint32_t size = sizeof(status_t);
6945 status_t status = (*mEffectInterface)->command(mEffectInterface,
6946 EFFECT_CMD_ENABLE,
6947 0,
6948 NULL,
6949 &size,
6950 &cmdStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006951 if (status == 0) {
6952 status = cmdStatus;
6953 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006954 if (status == 0 &&
6955 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
6956 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
6957 sp<ThreadBase> thread = mThread.promote();
6958 if (thread != 0) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07006959 audio_stream_t *stream = thread->stream();
6960 if (stream != NULL) {
6961 stream->add_audio_effect(stream, mEffectInterface);
6962 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006963 }
6964 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006965 return status;
6966}
6967
Eric Laurentec437d82011-07-26 20:54:46 -07006968status_t AudioFlinger::EffectModule::stop()
6969{
6970 Mutex::Autolock _l(mLock);
6971 return stop_l();
6972}
6973
Mathias Agopian65ab4712010-07-14 17:59:35 -07006974status_t AudioFlinger::EffectModule::stop_l()
6975{
6976 if (mEffectInterface == NULL) {
6977 return NO_INIT;
6978 }
6979 status_t cmdStatus;
Eric Laurent25f43952010-07-28 05:40:18 -07006980 uint32_t size = sizeof(status_t);
6981 status_t status = (*mEffectInterface)->command(mEffectInterface,
6982 EFFECT_CMD_DISABLE,
6983 0,
6984 NULL,
6985 &size,
6986 &cmdStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006987 if (status == 0) {
6988 status = cmdStatus;
6989 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006990 if (status == 0 &&
6991 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
6992 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
6993 sp<ThreadBase> thread = mThread.promote();
6994 if (thread != 0) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07006995 audio_stream_t *stream = thread->stream();
6996 if (stream != NULL) {
6997 stream->remove_audio_effect(stream, mEffectInterface);
6998 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006999 }
7000 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07007001 return status;
7002}
7003
Eric Laurent25f43952010-07-28 05:40:18 -07007004status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
7005 uint32_t cmdSize,
7006 void *pCmdData,
7007 uint32_t *replySize,
7008 void *pReplyData)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007009{
7010 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01007011// ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007012
Eric Laurentec437d82011-07-26 20:54:46 -07007013 if (mState == DESTROYED || mEffectInterface == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07007014 return NO_INIT;
7015 }
Eric Laurent25f43952010-07-28 05:40:18 -07007016 status_t status = (*mEffectInterface)->command(mEffectInterface,
7017 cmdCode,
7018 cmdSize,
7019 pCmdData,
7020 replySize,
7021 pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007022 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurent25f43952010-07-28 05:40:18 -07007023 uint32_t size = (replySize == NULL) ? 0 : *replySize;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007024 for (size_t i = 1; i < mHandles.size(); i++) {
7025 sp<EffectHandle> h = mHandles[i].promote();
7026 if (h != 0) {
7027 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
7028 }
7029 }
7030 }
7031 return status;
7032}
7033
7034status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
7035{
Eric Laurentdb7c0792011-08-10 10:37:50 -07007036
Mathias Agopian65ab4712010-07-14 17:59:35 -07007037 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01007038 ALOGV("setEnabled %p enabled %d", this, enabled);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007039
7040 if (enabled != isEnabled()) {
Eric Laurentdb7c0792011-08-10 10:37:50 -07007041 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
7042 if (enabled && status != NO_ERROR) {
7043 return status;
7044 }
7045
Mathias Agopian65ab4712010-07-14 17:59:35 -07007046 switch (mState) {
7047 // going from disabled to enabled
7048 case IDLE:
7049 mState = STARTING;
7050 break;
7051 case STOPPED:
7052 mState = RESTART;
7053 break;
7054 case STOPPING:
7055 mState = ACTIVE;
7056 break;
7057
7058 // going from enabled to disabled
7059 case RESTART:
Eric Laurent8f45bd72010-08-31 13:50:07 -07007060 mState = STOPPED;
7061 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007062 case STARTING:
7063 mState = IDLE;
7064 break;
7065 case ACTIVE:
7066 mState = STOPPING;
7067 break;
Eric Laurentec437d82011-07-26 20:54:46 -07007068 case DESTROYED:
7069 return NO_ERROR; // simply ignore as we are being destroyed
Mathias Agopian65ab4712010-07-14 17:59:35 -07007070 }
7071 for (size_t i = 1; i < mHandles.size(); i++) {
7072 sp<EffectHandle> h = mHandles[i].promote();
7073 if (h != 0) {
7074 h->setEnabled(enabled);
7075 }
7076 }
7077 }
7078 return NO_ERROR;
7079}
7080
Glenn Kastenc59c0042012-02-02 14:06:11 -08007081bool AudioFlinger::EffectModule::isEnabled() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07007082{
7083 switch (mState) {
7084 case RESTART:
7085 case STARTING:
7086 case ACTIVE:
7087 return true;
7088 case IDLE:
7089 case STOPPING:
7090 case STOPPED:
Eric Laurentec437d82011-07-26 20:54:46 -07007091 case DESTROYED:
Mathias Agopian65ab4712010-07-14 17:59:35 -07007092 default:
7093 return false;
7094 }
7095}
7096
Glenn Kastenc59c0042012-02-02 14:06:11 -08007097bool AudioFlinger::EffectModule::isProcessEnabled() const
Eric Laurent8f45bd72010-08-31 13:50:07 -07007098{
7099 switch (mState) {
7100 case RESTART:
7101 case ACTIVE:
7102 case STOPPING:
7103 case STOPPED:
7104 return true;
7105 case IDLE:
7106 case STARTING:
Eric Laurentec437d82011-07-26 20:54:46 -07007107 case DESTROYED:
Eric Laurent8f45bd72010-08-31 13:50:07 -07007108 default:
7109 return false;
7110 }
7111}
7112
Mathias Agopian65ab4712010-07-14 17:59:35 -07007113status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
7114{
7115 Mutex::Autolock _l(mLock);
7116 status_t status = NO_ERROR;
7117
7118 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
7119 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
Eric Laurent8f45bd72010-08-31 13:50:07 -07007120 if (isProcessEnabled() &&
Eric Laurentf997cab2010-07-19 06:24:46 -07007121 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
7122 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07007123 status_t cmdStatus;
7124 uint32_t volume[2];
7125 uint32_t *pVolume = NULL;
Eric Laurent25f43952010-07-28 05:40:18 -07007126 uint32_t size = sizeof(volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007127 volume[0] = *left;
7128 volume[1] = *right;
7129 if (controller) {
7130 pVolume = volume;
7131 }
Eric Laurent25f43952010-07-28 05:40:18 -07007132 status = (*mEffectInterface)->command(mEffectInterface,
7133 EFFECT_CMD_SET_VOLUME,
7134 size,
7135 volume,
7136 &size,
7137 pVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007138 if (controller && status == NO_ERROR && size == sizeof(volume)) {
7139 *left = volume[0];
7140 *right = volume[1];
7141 }
7142 }
7143 return status;
7144}
7145
7146status_t AudioFlinger::EffectModule::setDevice(uint32_t device)
7147{
7148 Mutex::Autolock _l(mLock);
7149 status_t status = NO_ERROR;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007150 if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
7151 // audio pre processing modules on RecordThread can receive both output and
7152 // input device indication in the same call
7153 uint32_t dev = device & AUDIO_DEVICE_OUT_ALL;
7154 if (dev) {
7155 status_t cmdStatus;
7156 uint32_t size = sizeof(status_t);
7157
7158 status = (*mEffectInterface)->command(mEffectInterface,
7159 EFFECT_CMD_SET_DEVICE,
7160 sizeof(uint32_t),
7161 &dev,
7162 &size,
7163 &cmdStatus);
7164 if (status == NO_ERROR) {
7165 status = cmdStatus;
7166 }
7167 }
7168 dev = device & AUDIO_DEVICE_IN_ALL;
7169 if (dev) {
7170 status_t cmdStatus;
7171 uint32_t size = sizeof(status_t);
7172
7173 status_t status2 = (*mEffectInterface)->command(mEffectInterface,
7174 EFFECT_CMD_SET_INPUT_DEVICE,
7175 sizeof(uint32_t),
7176 &dev,
7177 &size,
7178 &cmdStatus);
7179 if (status2 == NO_ERROR) {
7180 status2 = cmdStatus;
7181 }
7182 if (status == NO_ERROR) {
7183 status = status2;
7184 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07007185 }
7186 }
7187 return status;
7188}
7189
Glenn Kastenf78aee72012-01-04 11:00:47 -08007190status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007191{
7192 Mutex::Autolock _l(mLock);
7193 status_t status = NO_ERROR;
7194 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07007195 status_t cmdStatus;
Eric Laurent25f43952010-07-28 05:40:18 -07007196 uint32_t size = sizeof(status_t);
7197 status = (*mEffectInterface)->command(mEffectInterface,
7198 EFFECT_CMD_SET_AUDIO_MODE,
Glenn Kastenf78aee72012-01-04 11:00:47 -08007199 sizeof(audio_mode_t),
Eric Laurente1315cf2011-05-17 19:16:02 -07007200 &mode,
Eric Laurent25f43952010-07-28 05:40:18 -07007201 &size,
7202 &cmdStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007203 if (status == NO_ERROR) {
7204 status = cmdStatus;
7205 }
7206 }
7207 return status;
7208}
7209
Eric Laurent59255e42011-07-27 19:49:51 -07007210void AudioFlinger::EffectModule::setSuspended(bool suspended)
7211{
7212 Mutex::Autolock _l(mLock);
7213 mSuspended = suspended;
7214}
Glenn Kastena3a85482012-01-04 11:01:11 -08007215
7216bool AudioFlinger::EffectModule::suspended() const
Eric Laurent59255e42011-07-27 19:49:51 -07007217{
7218 Mutex::Autolock _l(mLock);
7219 return mSuspended;
7220}
7221
Mathias Agopian65ab4712010-07-14 17:59:35 -07007222status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
7223{
7224 const size_t SIZE = 256;
7225 char buffer[SIZE];
7226 String8 result;
7227
7228 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
7229 result.append(buffer);
7230
7231 bool locked = tryLock(mLock);
7232 // failed to lock - AudioFlinger is probably deadlocked
7233 if (!locked) {
7234 result.append("\t\tCould not lock Fx mutex:\n");
7235 }
7236
7237 result.append("\t\tSession Status State Engine:\n");
7238 snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n",
7239 mSessionId, mStatus, mState, (uint32_t)mEffectInterface);
7240 result.append(buffer);
7241
7242 result.append("\t\tDescriptor:\n");
7243 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
7244 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
7245 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2],
7246 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
7247 result.append(buffer);
7248 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
7249 mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion,
7250 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2],
7251 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
7252 result.append(buffer);
Eric Laurente1315cf2011-05-17 19:16:02 -07007253 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n",
Mathias Agopian65ab4712010-07-14 17:59:35 -07007254 mDescriptor.apiVersion,
7255 mDescriptor.flags);
7256 result.append(buffer);
7257 snprintf(buffer, SIZE, "\t\t- name: %s\n",
7258 mDescriptor.name);
7259 result.append(buffer);
7260 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
7261 mDescriptor.implementor);
7262 result.append(buffer);
7263
7264 result.append("\t\t- Input configuration:\n");
7265 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
7266 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
7267 (uint32_t)mConfig.inputCfg.buffer.raw,
7268 mConfig.inputCfg.buffer.frameCount,
7269 mConfig.inputCfg.samplingRate,
7270 mConfig.inputCfg.channels,
7271 mConfig.inputCfg.format);
7272 result.append(buffer);
7273
7274 result.append("\t\t- Output configuration:\n");
7275 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
7276 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
7277 (uint32_t)mConfig.outputCfg.buffer.raw,
7278 mConfig.outputCfg.buffer.frameCount,
7279 mConfig.outputCfg.samplingRate,
7280 mConfig.outputCfg.channels,
7281 mConfig.outputCfg.format);
7282 result.append(buffer);
7283
7284 snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size());
7285 result.append(buffer);
7286 result.append("\t\t\tPid Priority Ctrl Locked client server\n");
7287 for (size_t i = 0; i < mHandles.size(); ++i) {
7288 sp<EffectHandle> handle = mHandles[i].promote();
7289 if (handle != 0) {
7290 handle->dump(buffer, SIZE);
7291 result.append(buffer);
7292 }
7293 }
7294
7295 result.append("\n");
7296
7297 write(fd, result.string(), result.length());
7298
7299 if (locked) {
7300 mLock.unlock();
7301 }
7302
7303 return NO_ERROR;
7304}
7305
7306// ----------------------------------------------------------------------------
7307// EffectHandle implementation
7308// ----------------------------------------------------------------------------
7309
7310#undef LOG_TAG
7311#define LOG_TAG "AudioFlinger::EffectHandle"
7312
7313AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
7314 const sp<AudioFlinger::Client>& client,
7315 const sp<IEffectClient>& effectClient,
7316 int32_t priority)
7317 : BnEffect(),
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007318 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent59255e42011-07-27 19:49:51 -07007319 mPriority(priority), mHasControl(false), mEnabled(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007320{
Steve Block3856b092011-10-20 11:56:00 +01007321 ALOGV("constructor %p", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007322
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007323 if (client == 0) {
7324 return;
7325 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07007326 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
7327 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
7328 if (mCblkMemory != 0) {
7329 mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer());
7330
Glenn Kastena0d68332012-01-27 16:47:15 -08007331 if (mCblk != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07007332 new(mCblk) effect_param_cblk_t();
7333 mBuffer = (uint8_t *)mCblk + bufOffset;
7334 }
7335 } else {
Steve Block29357bc2012-01-06 19:20:56 +00007336 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t));
Mathias Agopian65ab4712010-07-14 17:59:35 -07007337 return;
7338 }
7339}
7340
7341AudioFlinger::EffectHandle::~EffectHandle()
7342{
Steve Block3856b092011-10-20 11:56:00 +01007343 ALOGV("Destructor %p", this);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007344 disconnect(false);
Steve Block3856b092011-10-20 11:56:00 +01007345 ALOGV("Destructor DONE %p", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007346}
7347
7348status_t AudioFlinger::EffectHandle::enable()
7349{
Steve Block3856b092011-10-20 11:56:00 +01007350 ALOGV("enable %p", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007351 if (!mHasControl) return INVALID_OPERATION;
7352 if (mEffect == 0) return DEAD_OBJECT;
7353
Eric Laurentdb7c0792011-08-10 10:37:50 -07007354 if (mEnabled) {
7355 return NO_ERROR;
7356 }
7357
Eric Laurent59255e42011-07-27 19:49:51 -07007358 mEnabled = true;
7359
7360 sp<ThreadBase> thread = mEffect->thread().promote();
7361 if (thread != 0) {
7362 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
7363 }
7364
7365 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
7366 if (mEffect->suspended()) {
7367 return NO_ERROR;
7368 }
7369
Eric Laurentdb7c0792011-08-10 10:37:50 -07007370 status_t status = mEffect->setEnabled(true);
7371 if (status != NO_ERROR) {
7372 if (thread != 0) {
7373 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
7374 }
7375 mEnabled = false;
7376 }
7377 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007378}
7379
7380status_t AudioFlinger::EffectHandle::disable()
7381{
Steve Block3856b092011-10-20 11:56:00 +01007382 ALOGV("disable %p", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007383 if (!mHasControl) return INVALID_OPERATION;
Eric Laurent59255e42011-07-27 19:49:51 -07007384 if (mEffect == 0) return DEAD_OBJECT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007385
Eric Laurentdb7c0792011-08-10 10:37:50 -07007386 if (!mEnabled) {
7387 return NO_ERROR;
7388 }
Eric Laurent59255e42011-07-27 19:49:51 -07007389 mEnabled = false;
7390
7391 if (mEffect->suspended()) {
7392 return NO_ERROR;
7393 }
7394
7395 status_t status = mEffect->setEnabled(false);
7396
7397 sp<ThreadBase> thread = mEffect->thread().promote();
7398 if (thread != 0) {
7399 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
7400 }
7401
7402 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007403}
7404
7405void AudioFlinger::EffectHandle::disconnect()
7406{
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007407 disconnect(true);
7408}
7409
Glenn Kasten58123c32012-02-03 10:32:24 -08007410void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007411{
Glenn Kasten58123c32012-02-03 10:32:24 -08007412 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
Mathias Agopian65ab4712010-07-14 17:59:35 -07007413 if (mEffect == 0) {
7414 return;
7415 }
Glenn Kasten58123c32012-02-03 10:32:24 -08007416 mEffect->disconnect(this, unpinIfLast);
Eric Laurent59255e42011-07-27 19:49:51 -07007417
Eric Laurenta85a74a2011-10-19 11:44:54 -07007418 if (mHasControl && mEnabled) {
Eric Laurentdb7c0792011-08-10 10:37:50 -07007419 sp<ThreadBase> thread = mEffect->thread().promote();
7420 if (thread != 0) {
7421 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
7422 }
Eric Laurent59255e42011-07-27 19:49:51 -07007423 }
7424
Mathias Agopian65ab4712010-07-14 17:59:35 -07007425 // release sp on module => module destructor can be called now
7426 mEffect.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07007427 if (mClient != 0) {
Glenn Kastena0d68332012-01-27 16:47:15 -08007428 if (mCblk != NULL) {
Glenn Kasten1a0ae5b2012-02-03 10:24:48 -08007429 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007430 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
7431 }
Glenn Kastendbfafaf2012-01-25 15:27:15 -08007432 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Glenn Kasten98ec94c2012-01-25 14:28:29 -08007433 // Client destructor must run with AudioFlinger mutex locked
Mathias Agopian65ab4712010-07-14 17:59:35 -07007434 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
7435 mClient.clear();
7436 }
7437}
7438
Eric Laurent25f43952010-07-28 05:40:18 -07007439status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
7440 uint32_t cmdSize,
7441 void *pCmdData,
7442 uint32_t *replySize,
7443 void *pReplyData)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007444{
Steve Block3856b092011-10-20 11:56:00 +01007445// ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent25f43952010-07-28 05:40:18 -07007446// cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07007447
7448 // only get parameter command is permitted for applications not controlling the effect
7449 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
7450 return INVALID_OPERATION;
7451 }
7452 if (mEffect == 0) return DEAD_OBJECT;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007453 if (mClient == 0) return INVALID_OPERATION;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007454
7455 // handle commands that are not forwarded transparently to effect engine
7456 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
7457 // No need to trylock() here as this function is executed in the binder thread serving a particular client process:
7458 // no risk to block the whole media server process or mixer threads is we are stuck here
7459 Mutex::Autolock _l(mCblk->lock);
7460 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
7461 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
7462 mCblk->serverIndex = 0;
7463 mCblk->clientIndex = 0;
7464 return BAD_VALUE;
7465 }
7466 status_t status = NO_ERROR;
7467 while (mCblk->serverIndex < mCblk->clientIndex) {
7468 int reply;
Eric Laurent25f43952010-07-28 05:40:18 -07007469 uint32_t rsize = sizeof(int);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007470 int *p = (int *)(mBuffer + mCblk->serverIndex);
7471 int size = *p++;
7472 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007473 ALOGW("command(): invalid parameter block size");
Mathias Agopian65ab4712010-07-14 17:59:35 -07007474 break;
7475 }
7476 effect_param_t *param = (effect_param_t *)p;
7477 if (param->psize == 0 || param->vsize == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007478 ALOGW("command(): null parameter or value size");
Mathias Agopian65ab4712010-07-14 17:59:35 -07007479 mCblk->serverIndex += size;
7480 continue;
7481 }
Eric Laurent25f43952010-07-28 05:40:18 -07007482 uint32_t psize = sizeof(effect_param_t) +
7483 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
7484 param->vsize;
7485 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
7486 psize,
7487 p,
7488 &rsize,
7489 &reply);
Eric Laurentaeae3de2010-09-02 11:56:55 -07007490 // stop at first error encountered
7491 if (ret != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07007492 status = ret;
Eric Laurentaeae3de2010-09-02 11:56:55 -07007493 *(int *)pReplyData = reply;
7494 break;
7495 } else if (reply != NO_ERROR) {
7496 *(int *)pReplyData = reply;
7497 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007498 }
7499 mCblk->serverIndex += size;
7500 }
7501 mCblk->serverIndex = 0;
7502 mCblk->clientIndex = 0;
7503 return status;
7504 } else if (cmdCode == EFFECT_CMD_ENABLE) {
Eric Laurentaeae3de2010-09-02 11:56:55 -07007505 *(int *)pReplyData = NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007506 return enable();
7507 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentaeae3de2010-09-02 11:56:55 -07007508 *(int *)pReplyData = NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007509 return disable();
7510 }
7511
7512 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
7513}
7514
Eric Laurent59255e42011-07-27 19:49:51 -07007515void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007516{
Steve Block3856b092011-10-20 11:56:00 +01007517 ALOGV("setControl %p control %d", this, hasControl);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007518
7519 mHasControl = hasControl;
Eric Laurent59255e42011-07-27 19:49:51 -07007520 mEnabled = enabled;
7521
Mathias Agopian65ab4712010-07-14 17:59:35 -07007522 if (signal && mEffectClient != 0) {
7523 mEffectClient->controlStatusChanged(hasControl);
7524 }
7525}
7526
Eric Laurent25f43952010-07-28 05:40:18 -07007527void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
7528 uint32_t cmdSize,
7529 void *pCmdData,
7530 uint32_t replySize,
7531 void *pReplyData)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007532{
7533 if (mEffectClient != 0) {
7534 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
7535 }
7536}
7537
7538
7539
7540void AudioFlinger::EffectHandle::setEnabled(bool enabled)
7541{
7542 if (mEffectClient != 0) {
7543 mEffectClient->enableStatusChanged(enabled);
7544 }
7545}
7546
7547status_t AudioFlinger::EffectHandle::onTransact(
7548 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
7549{
7550 return BnEffect::onTransact(code, data, reply, flags);
7551}
7552
7553
7554void AudioFlinger::EffectHandle::dump(char* buffer, size_t size)
7555{
Glenn Kastena0d68332012-01-27 16:47:15 -08007556 bool locked = mCblk != NULL && tryLock(mCblk->lock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007557
7558 snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n",
Glenn Kasten44deb052012-02-05 18:09:08 -08007559 (mClient == 0) ? getpid_cached : mClient->pid(),
Mathias Agopian65ab4712010-07-14 17:59:35 -07007560 mPriority,
7561 mHasControl,
7562 !locked,
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007563 mCblk ? mCblk->clientIndex : 0,
7564 mCblk ? mCblk->serverIndex : 0
Mathias Agopian65ab4712010-07-14 17:59:35 -07007565 );
7566
7567 if (locked) {
7568 mCblk->lock.unlock();
7569 }
7570}
7571
7572#undef LOG_TAG
7573#define LOG_TAG "AudioFlinger::EffectChain"
7574
Glenn Kasten9eaa5572012-01-20 13:32:16 -08007575AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07007576 int sessionId)
Glenn Kasten9eaa5572012-01-20 13:32:16 -08007577 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Eric Laurentb469b942011-05-09 12:09:06 -07007578 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
7579 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007580{
Dima Zavinfce7a472011-04-19 22:30:36 -07007581 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Glenn Kasten9eaa5572012-01-20 13:32:16 -08007582 if (thread == NULL) {
Eric Laurent544fe9b2011-11-11 15:42:52 -08007583 return;
7584 }
7585 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
7586 thread->frameCount();
Mathias Agopian65ab4712010-07-14 17:59:35 -07007587}
7588
7589AudioFlinger::EffectChain::~EffectChain()
7590{
7591 if (mOwnInBuffer) {
7592 delete mInBuffer;
7593 }
7594
7595}
7596
Eric Laurent59255e42011-07-27 19:49:51 -07007597// getEffectFromDesc_l() must be called with ThreadBase::mLock held
Eric Laurentcab11242010-07-15 12:50:15 -07007598sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007599{
Mathias Agopian65ab4712010-07-14 17:59:35 -07007600 size_t size = mEffects.size();
7601
7602 for (size_t i = 0; i < size; i++) {
7603 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kasten090f0192012-01-30 13:00:02 -08007604 return mEffects[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07007605 }
7606 }
Glenn Kasten090f0192012-01-30 13:00:02 -08007607 return 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007608}
7609
Eric Laurent59255e42011-07-27 19:49:51 -07007610// getEffectFromId_l() must be called with ThreadBase::mLock held
Eric Laurentcab11242010-07-15 12:50:15 -07007611sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007612{
Mathias Agopian65ab4712010-07-14 17:59:35 -07007613 size_t size = mEffects.size();
7614
7615 for (size_t i = 0; i < size; i++) {
Eric Laurentde070132010-07-13 04:45:46 -07007616 // by convention, return first effect if id provided is 0 (0 is never a valid id)
7617 if (id == 0 || mEffects[i]->id() == id) {
Glenn Kasten090f0192012-01-30 13:00:02 -08007618 return mEffects[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07007619 }
7620 }
Glenn Kasten090f0192012-01-30 13:00:02 -08007621 return 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007622}
7623
Eric Laurent59255e42011-07-27 19:49:51 -07007624// getEffectFromType_l() must be called with ThreadBase::mLock held
7625sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
7626 const effect_uuid_t *type)
7627{
Eric Laurent59255e42011-07-27 19:49:51 -07007628 size_t size = mEffects.size();
7629
7630 for (size_t i = 0; i < size; i++) {
7631 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
Glenn Kasten090f0192012-01-30 13:00:02 -08007632 return mEffects[i];
Eric Laurent59255e42011-07-27 19:49:51 -07007633 }
7634 }
Glenn Kasten090f0192012-01-30 13:00:02 -08007635 return 0;
Eric Laurent59255e42011-07-27 19:49:51 -07007636}
7637
Mathias Agopian65ab4712010-07-14 17:59:35 -07007638// Must be called with EffectChain::mLock locked
7639void AudioFlinger::EffectChain::process_l()
7640{
Eric Laurentdac69112010-09-28 14:09:57 -07007641 sp<ThreadBase> thread = mThread.promote();
7642 if (thread == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007643 ALOGW("process_l(): cannot promote mixer thread");
Eric Laurentdac69112010-09-28 14:09:57 -07007644 return;
7645 }
Dima Zavinfce7a472011-04-19 22:30:36 -07007646 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
7647 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Eric Laurent544fe9b2011-11-11 15:42:52 -08007648 // always process effects unless no more tracks are on the session and the effect tail
7649 // has been rendered
7650 bool doProcess = true;
Eric Laurentdac69112010-09-28 14:09:57 -07007651 if (!isGlobalSession) {
Eric Laurent544fe9b2011-11-11 15:42:52 -08007652 bool tracksOnSession = (trackCnt() != 0);
Eric Laurentb469b942011-05-09 12:09:06 -07007653
Eric Laurent544fe9b2011-11-11 15:42:52 -08007654 if (!tracksOnSession && mTailBufferCount == 0) {
7655 doProcess = false;
7656 }
7657
7658 if (activeTrackCnt() == 0) {
7659 // if no track is active and the effect tail has not been rendered,
7660 // the input buffer must be cleared here as the mixer process will not do it
7661 if (tracksOnSession || mTailBufferCount > 0) {
7662 size_t numSamples = thread->frameCount() * thread->channelCount();
7663 memset(mInBuffer, 0, numSamples * sizeof(int16_t));
7664 if (mTailBufferCount > 0) {
7665 mTailBufferCount--;
7666 }
7667 }
7668 }
Eric Laurentdac69112010-09-28 14:09:57 -07007669 }
7670
Mathias Agopian65ab4712010-07-14 17:59:35 -07007671 size_t size = mEffects.size();
Eric Laurent544fe9b2011-11-11 15:42:52 -08007672 if (doProcess) {
Eric Laurentdac69112010-09-28 14:09:57 -07007673 for (size_t i = 0; i < size; i++) {
7674 mEffects[i]->process();
7675 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07007676 }
7677 for (size_t i = 0; i < size; i++) {
7678 mEffects[i]->updateState();
7679 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07007680}
7681
Eric Laurentcab11242010-07-15 12:50:15 -07007682// addEffect_l() must be called with PlaybackThread::mLock held
Eric Laurentde070132010-07-13 04:45:46 -07007683status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007684{
7685 effect_descriptor_t desc = effect->desc();
7686 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
7687
7688 Mutex::Autolock _l(mLock);
Eric Laurentde070132010-07-13 04:45:46 -07007689 effect->setChain(this);
7690 sp<ThreadBase> thread = mThread.promote();
7691 if (thread == 0) {
7692 return NO_INIT;
7693 }
7694 effect->setThread(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007695
7696 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
7697 // Auxiliary effects are inserted at the beginning of mEffects vector as
7698 // they are processed first and accumulated in chain input buffer
7699 mEffects.insertAt(effect, 0);
Eric Laurentde070132010-07-13 04:45:46 -07007700
Mathias Agopian65ab4712010-07-14 17:59:35 -07007701 // the input buffer for auxiliary effect contains mono samples in
7702 // 32 bit format. This is to avoid saturation in AudoMixer
7703 // accumulation stage. Saturation is done in EffectModule::process() before
7704 // calling the process in effect engine
7705 size_t numSamples = thread->frameCount();
7706 int32_t *buffer = new int32_t[numSamples];
7707 memset(buffer, 0, numSamples * sizeof(int32_t));
7708 effect->setInBuffer((int16_t *)buffer);
7709 // auxiliary effects output samples to chain input buffer for further processing
7710 // by insert effects
7711 effect->setOutBuffer(mInBuffer);
7712 } else {
7713 // Insert effects are inserted at the end of mEffects vector as they are processed
7714 // after track and auxiliary effects.
7715 // Insert effect order as a function of indicated preference:
7716 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
7717 // another effect is present
7718 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
7719 // last effect claiming first position
7720 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
7721 // first effect claiming last position
7722 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
7723 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
7724 // already present
7725
Glenn Kasten8d6a2442012-02-08 14:04:28 -08007726 size_t size = mEffects.size();
7727 size_t idx_insert = size;
7728 ssize_t idx_insert_first = -1;
7729 ssize_t idx_insert_last = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007730
Glenn Kasten8d6a2442012-02-08 14:04:28 -08007731 for (size_t i = 0; i < size; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07007732 effect_descriptor_t d = mEffects[i]->desc();
7733 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
7734 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
7735 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
7736 // check invalid effect chaining combinations
7737 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
7738 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007739 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007740 return INVALID_OPERATION;
7741 }
7742 // remember position of first insert effect and by default
7743 // select this as insert position for new effect
7744 if (idx_insert == size) {
7745 idx_insert = i;
7746 }
7747 // remember position of last insert effect claiming
7748 // first position
7749 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
7750 idx_insert_first = i;
7751 }
7752 // remember position of first insert effect claiming
7753 // last position
7754 if (iPref == EFFECT_FLAG_INSERT_LAST &&
7755 idx_insert_last == -1) {
7756 idx_insert_last = i;
7757 }
7758 }
7759 }
7760
7761 // modify idx_insert from first position if needed
7762 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
7763 if (idx_insert_last != -1) {
7764 idx_insert = idx_insert_last;
7765 } else {
7766 idx_insert = size;
7767 }
7768 } else {
7769 if (idx_insert_first != -1) {
7770 idx_insert = idx_insert_first + 1;
7771 }
7772 }
7773
7774 // always read samples from chain input buffer
7775 effect->setInBuffer(mInBuffer);
7776
7777 // if last effect in the chain, output samples to chain
7778 // output buffer, otherwise to chain input buffer
7779 if (idx_insert == size) {
7780 if (idx_insert != 0) {
7781 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
7782 mEffects[idx_insert-1]->configure();
7783 }
7784 effect->setOutBuffer(mOutBuffer);
7785 } else {
7786 effect->setOutBuffer(mInBuffer);
7787 }
7788 mEffects.insertAt(effect, idx_insert);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007789
Steve Block3856b092011-10-20 11:56:00 +01007790 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007791 }
7792 effect->configure();
7793 return NO_ERROR;
7794}
7795
Eric Laurentcab11242010-07-15 12:50:15 -07007796// removeEffect_l() must be called with PlaybackThread::mLock held
7797size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007798{
7799 Mutex::Autolock _l(mLock);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08007800 size_t size = mEffects.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07007801 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
7802
Glenn Kasten8d6a2442012-02-08 14:04:28 -08007803 for (size_t i = 0; i < size; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07007804 if (effect == mEffects[i]) {
Eric Laurentec437d82011-07-26 20:54:46 -07007805 // calling stop here will remove pre-processing effect from the audio HAL.
7806 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
7807 // the middle of a read from audio HAL
Eric Laurentec35a142011-10-05 17:42:25 -07007808 if (mEffects[i]->state() == EffectModule::ACTIVE ||
7809 mEffects[i]->state() == EffectModule::STOPPING) {
7810 mEffects[i]->stop();
7811 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07007812 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
7813 delete[] effect->inBuffer();
7814 } else {
7815 if (i == size - 1 && i != 0) {
7816 mEffects[i - 1]->setOutBuffer(mOutBuffer);
7817 mEffects[i - 1]->configure();
7818 }
7819 }
7820 mEffects.removeAt(i);
Steve Block3856b092011-10-20 11:56:00 +01007821 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007822 break;
7823 }
7824 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07007825
7826 return mEffects.size();
7827}
7828
Eric Laurentcab11242010-07-15 12:50:15 -07007829// setDevice_l() must be called with PlaybackThread::mLock held
7830void AudioFlinger::EffectChain::setDevice_l(uint32_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007831{
7832 size_t size = mEffects.size();
7833 for (size_t i = 0; i < size; i++) {
7834 mEffects[i]->setDevice(device);
7835 }
7836}
7837
Eric Laurentcab11242010-07-15 12:50:15 -07007838// setMode_l() must be called with PlaybackThread::mLock held
Glenn Kastenf78aee72012-01-04 11:00:47 -08007839void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007840{
7841 size_t size = mEffects.size();
7842 for (size_t i = 0; i < size; i++) {
7843 mEffects[i]->setMode(mode);
7844 }
7845}
7846
Eric Laurentcab11242010-07-15 12:50:15 -07007847// setVolume_l() must be called with PlaybackThread::mLock held
7848bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007849{
7850 uint32_t newLeft = *left;
7851 uint32_t newRight = *right;
7852 bool hasControl = false;
Eric Laurentcab11242010-07-15 12:50:15 -07007853 int ctrlIdx = -1;
7854 size_t size = mEffects.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07007855
Eric Laurentcab11242010-07-15 12:50:15 -07007856 // first update volume controller
7857 for (size_t i = size; i > 0; i--) {
Eric Laurent8f45bd72010-08-31 13:50:07 -07007858 if (mEffects[i - 1]->isProcessEnabled() &&
Eric Laurentcab11242010-07-15 12:50:15 -07007859 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
7860 ctrlIdx = i - 1;
Eric Laurentf997cab2010-07-19 06:24:46 -07007861 hasControl = true;
Eric Laurentcab11242010-07-15 12:50:15 -07007862 break;
7863 }
7864 }
7865
7866 if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentf997cab2010-07-19 06:24:46 -07007867 if (hasControl) {
7868 *left = mNewLeftVolume;
7869 *right = mNewRightVolume;
7870 }
7871 return hasControl;
Eric Laurentcab11242010-07-15 12:50:15 -07007872 }
7873
7874 mVolumeCtrlIdx = ctrlIdx;
Eric Laurentf997cab2010-07-19 06:24:46 -07007875 mLeftVolume = newLeft;
7876 mRightVolume = newRight;
Eric Laurentcab11242010-07-15 12:50:15 -07007877
7878 // second get volume update from volume controller
7879 if (ctrlIdx >= 0) {
7880 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
Eric Laurentf997cab2010-07-19 06:24:46 -07007881 mNewLeftVolume = newLeft;
7882 mNewRightVolume = newRight;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007883 }
7884 // then indicate volume to all other effects in chain.
7885 // Pass altered volume to effects before volume controller
7886 // and requested volume to effects after controller
7887 uint32_t lVol = newLeft;
7888 uint32_t rVol = newRight;
Eric Laurentcab11242010-07-15 12:50:15 -07007889
Mathias Agopian65ab4712010-07-14 17:59:35 -07007890 for (size_t i = 0; i < size; i++) {
Eric Laurentcab11242010-07-15 12:50:15 -07007891 if ((int)i == ctrlIdx) continue;
7892 // this also works for ctrlIdx == -1 when there is no volume controller
7893 if ((int)i > ctrlIdx) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07007894 lVol = *left;
7895 rVol = *right;
7896 }
7897 mEffects[i]->setVolume(&lVol, &rVol, false);
7898 }
7899 *left = newLeft;
7900 *right = newRight;
7901
7902 return hasControl;
7903}
7904
Mathias Agopian65ab4712010-07-14 17:59:35 -07007905status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
7906{
7907 const size_t SIZE = 256;
7908 char buffer[SIZE];
7909 String8 result;
7910
7911 snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId);
7912 result.append(buffer);
7913
7914 bool locked = tryLock(mLock);
7915 // failed to lock - AudioFlinger is probably deadlocked
7916 if (!locked) {
7917 result.append("\tCould not lock mutex:\n");
7918 }
7919
Eric Laurentcab11242010-07-15 12:50:15 -07007920 result.append("\tNum fx In buffer Out buffer Active tracks:\n");
7921 snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n",
Mathias Agopian65ab4712010-07-14 17:59:35 -07007922 mEffects.size(),
7923 (uint32_t)mInBuffer,
7924 (uint32_t)mOutBuffer,
Mathias Agopian65ab4712010-07-14 17:59:35 -07007925 mActiveTrackCnt);
7926 result.append(buffer);
7927 write(fd, result.string(), result.size());
7928
7929 for (size_t i = 0; i < mEffects.size(); ++i) {
7930 sp<EffectModule> effect = mEffects[i];
7931 if (effect != 0) {
7932 effect->dump(fd, args);
7933 }
7934 }
7935
7936 if (locked) {
7937 mLock.unlock();
7938 }
7939
7940 return NO_ERROR;
7941}
7942
Eric Laurent59255e42011-07-27 19:49:51 -07007943// must be called with ThreadBase::mLock held
7944void AudioFlinger::EffectChain::setEffectSuspended_l(
7945 const effect_uuid_t *type, bool suspend)
7946{
7947 sp<SuspendedEffectDesc> desc;
7948 // use effect type UUID timelow as key as there is no real risk of identical
7949 // timeLow fields among effect type UUIDs.
Glenn Kasten8d6a2442012-02-08 14:04:28 -08007950 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
Eric Laurent59255e42011-07-27 19:49:51 -07007951 if (suspend) {
7952 if (index >= 0) {
7953 desc = mSuspendedEffects.valueAt(index);
7954 } else {
7955 desc = new SuspendedEffectDesc();
7956 memcpy(&desc->mType, type, sizeof(effect_uuid_t));
7957 mSuspendedEffects.add(type->timeLow, desc);
Steve Block3856b092011-10-20 11:56:00 +01007958 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
Eric Laurent59255e42011-07-27 19:49:51 -07007959 }
7960 if (desc->mRefCount++ == 0) {
7961 sp<EffectModule> effect = getEffectIfEnabled(type);
7962 if (effect != 0) {
7963 desc->mEffect = effect;
7964 effect->setSuspended(true);
7965 effect->setEnabled(false);
7966 }
7967 }
7968 } else {
7969 if (index < 0) {
7970 return;
7971 }
7972 desc = mSuspendedEffects.valueAt(index);
7973 if (desc->mRefCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007974 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurent59255e42011-07-27 19:49:51 -07007975 desc->mRefCount = 1;
7976 }
7977 if (--desc->mRefCount == 0) {
Steve Block3856b092011-10-20 11:56:00 +01007978 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
Eric Laurent59255e42011-07-27 19:49:51 -07007979 if (desc->mEffect != 0) {
7980 sp<EffectModule> effect = desc->mEffect.promote();
7981 if (effect != 0) {
7982 effect->setSuspended(false);
7983 sp<EffectHandle> handle = effect->controlHandle();
7984 if (handle != 0) {
7985 effect->setEnabled(handle->enabled());
7986 }
7987 }
7988 desc->mEffect.clear();
7989 }
7990 mSuspendedEffects.removeItemsAt(index);
7991 }
7992 }
7993}
7994
7995// must be called with ThreadBase::mLock held
7996void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
7997{
7998 sp<SuspendedEffectDesc> desc;
7999
Glenn Kasten8d6a2442012-02-08 14:04:28 -08008000 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
Eric Laurent59255e42011-07-27 19:49:51 -07008001 if (suspend) {
8002 if (index >= 0) {
8003 desc = mSuspendedEffects.valueAt(index);
8004 } else {
8005 desc = new SuspendedEffectDesc();
8006 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
Steve Block3856b092011-10-20 11:56:00 +01008007 ALOGV("setEffectSuspendedAll_l() add entry for 0");
Eric Laurent59255e42011-07-27 19:49:51 -07008008 }
8009 if (desc->mRefCount++ == 0) {
Glenn Kastend0539712012-01-30 12:56:03 -08008010 Vector< sp<EffectModule> > effects;
8011 getSuspendEligibleEffects(effects);
Eric Laurent59255e42011-07-27 19:49:51 -07008012 for (size_t i = 0; i < effects.size(); i++) {
8013 setEffectSuspended_l(&effects[i]->desc().type, true);
8014 }
8015 }
8016 } else {
8017 if (index < 0) {
8018 return;
8019 }
8020 desc = mSuspendedEffects.valueAt(index);
8021 if (desc->mRefCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00008022 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurent59255e42011-07-27 19:49:51 -07008023 desc->mRefCount = 1;
8024 }
8025 if (--desc->mRefCount == 0) {
8026 Vector<const effect_uuid_t *> types;
8027 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
8028 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
8029 continue;
8030 }
8031 types.add(&mSuspendedEffects.valueAt(i)->mType);
8032 }
8033 for (size_t i = 0; i < types.size(); i++) {
8034 setEffectSuspended_l(types[i], false);
8035 }
Steve Block3856b092011-10-20 11:56:00 +01008036 ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
Eric Laurent59255e42011-07-27 19:49:51 -07008037 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
8038 }
8039 }
8040}
8041
Eric Laurent6bffdb82011-09-23 08:40:41 -07008042
8043// The volume effect is used for automated tests only
8044#ifndef OPENSL_ES_H_
8045static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
8046 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
8047const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
8048#endif //OPENSL_ES_H_
8049
Eric Laurentdb7c0792011-08-10 10:37:50 -07008050bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
8051{
8052 // auxiliary effects and visualizer are never suspended on output mix
8053 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
8054 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
Eric Laurent6bffdb82011-09-23 08:40:41 -07008055 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
8056 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentdb7c0792011-08-10 10:37:50 -07008057 return false;
8058 }
8059 return true;
8060}
8061
Glenn Kastend0539712012-01-30 12:56:03 -08008062void AudioFlinger::EffectChain::getSuspendEligibleEffects(Vector< sp<AudioFlinger::EffectModule> > &effects)
Eric Laurent59255e42011-07-27 19:49:51 -07008063{
Glenn Kastend0539712012-01-30 12:56:03 -08008064 effects.clear();
Eric Laurent59255e42011-07-27 19:49:51 -07008065 for (size_t i = 0; i < mEffects.size(); i++) {
Glenn Kastend0539712012-01-30 12:56:03 -08008066 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
8067 effects.add(mEffects[i]);
Eric Laurent59255e42011-07-27 19:49:51 -07008068 }
Eric Laurent59255e42011-07-27 19:49:51 -07008069 }
Eric Laurent59255e42011-07-27 19:49:51 -07008070}
8071
8072sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
8073 const effect_uuid_t *type)
8074{
Glenn Kasten090f0192012-01-30 13:00:02 -08008075 sp<EffectModule> effect = getEffectFromType_l(type);
8076 return effect != 0 && effect->isEnabled() ? effect : 0;
Eric Laurent59255e42011-07-27 19:49:51 -07008077}
8078
8079void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
8080 bool enabled)
8081{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08008082 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
Eric Laurent59255e42011-07-27 19:49:51 -07008083 if (enabled) {
8084 if (index < 0) {
8085 // if the effect is not suspend check if all effects are suspended
8086 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
8087 if (index < 0) {
8088 return;
8089 }
Eric Laurentdb7c0792011-08-10 10:37:50 -07008090 if (!isEffectEligibleForSuspend(effect->desc())) {
8091 return;
8092 }
Eric Laurent59255e42011-07-27 19:49:51 -07008093 setEffectSuspended_l(&effect->desc().type, enabled);
8094 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
Eric Laurentdb7c0792011-08-10 10:37:50 -07008095 if (index < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00008096 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
Eric Laurentdb7c0792011-08-10 10:37:50 -07008097 return;
8098 }
Eric Laurent59255e42011-07-27 19:49:51 -07008099 }
Steve Block3856b092011-10-20 11:56:00 +01008100 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
Eric Laurent59255e42011-07-27 19:49:51 -07008101 effect->desc().type.timeLow);
8102 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
8103 // if effect is requested to suspended but was not yet enabled, supend it now.
8104 if (desc->mEffect == 0) {
8105 desc->mEffect = effect;
8106 effect->setEnabled(false);
8107 effect->setSuspended(true);
8108 }
8109 } else {
8110 if (index < 0) {
8111 return;
8112 }
Steve Block3856b092011-10-20 11:56:00 +01008113 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
Eric Laurent59255e42011-07-27 19:49:51 -07008114 effect->desc().type.timeLow);
8115 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
8116 desc->mEffect.clear();
8117 effect->setSuspended(false);
8118 }
8119}
8120
Mathias Agopian65ab4712010-07-14 17:59:35 -07008121#undef LOG_TAG
8122#define LOG_TAG "AudioFlinger"
8123
8124// ----------------------------------------------------------------------------
8125
8126status_t AudioFlinger::onTransact(
8127 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
8128{
8129 return BnAudioFlinger::onTransact(code, data, reply, flags);
8130}
8131
Mathias Agopian65ab4712010-07-14 17:59:35 -07008132}; // namespace android