blob: 7e5f102c2bc7ff6a3d288e669bbf73a6360929e3 [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>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070030#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070031#include <binder/Parcel.h>
32#include <binder/IPCThreadState.h>
33#include <utils/String16.h>
34#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070035#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070036
Dima Zavinfce7a472011-04-19 22:30:36 -070037#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <cutils/properties.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080039#include <cutils/compiler.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070040
Glenn Kastend3cee2f2012-03-13 17:55:35 -070041#undef ADD_BATTERY_DATA
42
43#ifdef ADD_BATTERY_DATA
Gloria Wang9ee159b2011-02-24 14:51:45 -080044#include <media/IMediaPlayerService.h>
Glenn Kasten25b248e2012-01-03 15:28:29 -080045#include <media/IMediaDeathNotifier.h>
Glenn Kastend3cee2f2012-03-13 17:55:35 -070046#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
48#include <private/media/AudioTrackShared.h>
49#include <private/media/AudioEffectShared.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070050
Dima Zavin64760242011-05-11 14:15:23 -070051#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070052#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
54#include "AudioMixer.h"
55#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080056#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070057
Mathias Agopian65ab4712010-07-14 17:59:35 -070058#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070059#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070060#include <audio_effects/effect_ns.h>
61#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070062
Glenn Kasten3b21c502011-12-15 09:52:39 -080063#include <audio_utils/primitives.h>
64
Eric Laurentfeb0db62011-07-22 09:04:31 -070065#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080066
Glenn Kasten4d8d0c32011-07-08 15:26:12 -070067// #define DEBUG_CPU_USAGE 10 // log statistics every n wall clock seconds
Glenn Kasten190a46f2012-03-06 11:27:10 -080068#ifdef DEBUG_CPU_USAGE
69#include <cpustats/CentralTendencyStatistics.h>
70#include <cpustats/ThreadCpuUsage.h>
71#endif
Glenn Kasten4d8d0c32011-07-08 15:26:12 -070072
John Grossman4ff14ba2012-02-08 16:37:41 -080073#include <common_time/cc_helper.h>
74#include <common_time/local_clock.h>
75
Glenn Kasten58912562012-04-03 10:45:00 -070076#include "FastMixer.h"
77
78// NBAIO implementations
79#include "AudioStreamOutSink.h"
80#include "MonoPipe.h"
81#include "MonoPipeReader.h"
Glenn Kastenfbae5da2012-05-21 09:17:20 -070082#include "Pipe.h"
83#include "PipeReader.h"
Glenn Kasten58912562012-04-03 10:45:00 -070084#include "SourceAudioBufferProvider.h"
85
Glenn Kasten1dc28b72012-04-24 10:01:03 -070086#include "SchedulingPolicyService.h"
Glenn Kasten58912562012-04-03 10:45:00 -070087
Mathias Agopian65ab4712010-07-14 17:59:35 -070088// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070089
John Grossman1c345192012-03-27 14:00:17 -070090// Note: the following macro is used for extremely verbose logging message. In
91// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
92// 0; but one side effect of this is to turn all LOGV's as well. Some messages
93// are so verbose that we want to suppress them even when we have ALOG_ASSERT
94// turned on. Do not uncomment the #def below unless you really know what you
95// are doing and want to see all of the extremely verbose messages.
96//#define VERY_VERY_VERBOSE_LOGGING
97#ifdef VERY_VERY_VERBOSE_LOGGING
98#define ALOGVV ALOGV
99#else
100#define ALOGVV(a...) do { } while(0)
101#endif
Eric Laurentde070132010-07-13 04:45:46 -0700102
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103namespace android {
104
Glenn Kastenec1d6b52011-12-12 09:04:45 -0800105static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
106static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -0700107
Mathias Agopian65ab4712010-07-14 17:59:35 -0700108static const float MAX_GAIN = 4096.0f;
Glenn Kastenb1cf75c2012-01-17 12:20:54 -0800109static const uint32_t MAX_GAIN_INT = 0x1000;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700110
111// retry counts for buffer fill timeout
112// 50 * ~20msecs = 1 second
113static const int8_t kMaxTrackRetries = 50;
114static const int8_t kMaxTrackStartupRetries = 50;
115// allow less retry attempts on direct output thread.
116// direct outputs can be a scarce resource in audio hardware and should
117// be released as quickly as possible.
118static const int8_t kMaxTrackRetriesDirect = 2;
119
120static const int kDumpLockRetries = 50;
Glenn Kasten7dede872011-12-13 11:04:14 -0800121static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700122
Glenn Kasten7dede872011-12-13 11:04:14 -0800123// don't warn about blocked writes or record buffer overflows more often than this
124static const nsecs_t kWarningThrottleNs = seconds(5);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700125
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700126// RecordThread loop sleep time upon application overrun or audio HAL read error
127static const int kRecordThreadSleepUs = 5000;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700128
Glenn Kasten7dede872011-12-13 11:04:14 -0800129// maximum time to wait for setParameters to complete
130static const nsecs_t kSetParametersTimeoutNs = seconds(2);
Eric Laurent60cd0a02011-09-13 11:40:21 -0700131
Eric Laurent7cafbb32011-11-22 18:50:29 -0800132// minimum sleep time for the mixer thread loop when tracks are active but in underrun
133static const uint32_t kMinThreadSleepTimeUs = 5000;
134// maximum divider applied to the active sleep time in the mixer thread loop
135static const uint32_t kMaxThreadSleepTimeShift = 2;
136
Glenn Kasten58912562012-04-03 10:45:00 -0700137// minimum normal mix buffer size, expressed in milliseconds rather than frames
138static const uint32_t kMinNormalMixBufferSizeMs = 20;
Glenn Kasten4adcede2012-05-14 12:26:02 -0700139// maximum normal mix buffer size
140static const uint32_t kMaxNormalMixBufferSizeMs = 24;
Glenn Kasten58912562012-04-03 10:45:00 -0700141
John Grossman4ff14ba2012-02-08 16:37:41 -0800142nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -0800143
Glenn Kasten300a2ee2012-04-25 13:47:36 -0700144// Whether to use fast mixer
145static const enum {
146 FastMixer_Never, // never initialize or use: for debugging only
147 FastMixer_Always, // always initialize and use, even if not needed: for debugging only
148 // normal mixer multiplier is 1
149 FastMixer_Static, // initialize if needed, then use all the time if initialized,
Glenn Kasten4adcede2012-05-14 12:26:02 -0700150 // multiplier is calculated based on min & max normal mixer buffer size
Glenn Kasten300a2ee2012-04-25 13:47:36 -0700151 FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load,
Glenn Kasten4adcede2012-05-14 12:26:02 -0700152 // multiplier is calculated based on min & max normal mixer buffer size
Glenn Kasten300a2ee2012-04-25 13:47:36 -0700153 // FIXME for FastMixer_Dynamic:
154 // Supporting this option will require fixing HALs that can't handle large writes.
155 // For example, one HAL implementation returns an error from a large write,
156 // and another HAL implementation corrupts memory, possibly in the sample rate converter.
157 // We could either fix the HAL implementations, or provide a wrapper that breaks
158 // up large writes into smaller ones, and the wrapper would need to deal with scheduler.
159} kUseFastMixer = FastMixer_Static;
160
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700161static uint32_t gScreenState; // incremented by 2 when screen state changes, bit 0 == 1 means "off"
162 // AudioFlinger::setParameters() updates, other threads read w/o lock
163
Glenn Kastenfd4e20c2012-06-04 11:51:12 -0700164// Priorities for requestPriority
165static const int kPriorityAudioApp = 2;
166static const int kPriorityFastMixer = 3;
167
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168// ----------------------------------------------------------------------------
169
Glenn Kastend3cee2f2012-03-13 17:55:35 -0700170#ifdef ADD_BATTERY_DATA
Gloria Wang9ee159b2011-02-24 14:51:45 -0800171// To collect the amplifier usage
172static void addBatteryData(uint32_t params) {
Glenn Kasten25b248e2012-01-03 15:28:29 -0800173 sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService();
174 if (service == NULL) {
175 // it already logged
Gloria Wang9ee159b2011-02-24 14:51:45 -0800176 return;
177 }
178
179 service->addBatteryData(params);
180}
Glenn Kastend3cee2f2012-03-13 17:55:35 -0700181#endif
Gloria Wang9ee159b2011-02-24 14:51:45 -0800182
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700183static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700184{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700185 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700186 int rc;
187
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700188 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
189 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
190 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
191 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700192 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700193 }
194 rc = audio_hw_device_open(mod, dev);
195 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
196 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
197 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700198 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700199 }
200 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
201 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
202 rc = BAD_VALUE;
203 goto out;
204 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700205 return 0;
206
207out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700208 *dev = NULL;
209 return rc;
210}
211
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212// ----------------------------------------------------------------------------
213
214AudioFlinger::AudioFlinger()
215 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800216 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700217 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800218 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800219 mMasterMute(false),
220 mNextUniqueId(1),
221 mMode(AUDIO_MODE_INVALID),
222 mBtNrecIsOff(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700223{
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700224}
225
226void AudioFlinger::onFirstRef()
227{
Dima Zavin799a70e2011-04-18 16:57:27 -0700228 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700229
Eric Laurent93575202011-01-18 18:39:02 -0800230 Mutex::Autolock _l(mLock);
231
Dima Zavin799a70e2011-04-18 16:57:27 -0700232 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800233 char val_str[PROPERTY_VALUE_MAX] = { 0 };
234 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
235 uint32_t int_val;
236 if (1 == sscanf(val_str, "%u", &int_val)) {
237 mStandbyTimeInNsecs = milliseconds(int_val);
238 ALOGI("Using %u mSec as standby time.", int_val);
239 } else {
240 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
241 ALOGI("Using default %u mSec as standby time.",
242 (uint32_t)(mStandbyTimeInNsecs / 1000000));
243 }
244 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700245
Eric Laurenta4c5a552012-03-29 10:12:40 -0700246 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700247}
248
249AudioFlinger::~AudioFlinger()
250{
251 while (!mRecordThreads.isEmpty()) {
252 // closeInput() will remove first entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700253 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254 }
255 while (!mPlaybackThreads.isEmpty()) {
256 // closeOutput() will remove first entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700257 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700258 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700259
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800260 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
261 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700262 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
263 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264 }
265}
266
Eric Laurenta4c5a552012-03-29 10:12:40 -0700267static const char * const audio_interfaces[] = {
268 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
269 AUDIO_HARDWARE_MODULE_ID_A2DP,
270 AUDIO_HARDWARE_MODULE_ID_USB,
271};
272#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
273
John Grossmanee578c02012-07-23 17:05:46 -0700274AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
275 audio_module_handle_t module,
276 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700277{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700278 // if module is 0, the request comes from an old policy manager and we should load
279 // well known modules
280 if (module == 0) {
281 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
282 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
283 loadHwModule_l(audio_interfaces[i]);
284 }
285 } else {
286 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700287 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
288 if (audioHwDevice != NULL) {
289 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700290 }
291 }
292 // then try to find a module supporting the requested device.
Dima Zavin799a70e2011-04-18 16:57:27 -0700293 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
John Grossmanee578c02012-07-23 17:05:46 -0700294 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
295 audio_hw_device_t *dev = audioHwDevice->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700296 if ((dev->get_supported_devices(dev) & devices) == devices)
John Grossmanee578c02012-07-23 17:05:46 -0700297 return audioHwDevice;
Dima Zavin799a70e2011-04-18 16:57:27 -0700298 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700299
Dima Zavin799a70e2011-04-18 16:57:27 -0700300 return NULL;
301}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700303void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304{
305 const size_t SIZE = 256;
306 char buffer[SIZE];
307 String8 result;
308
309 result.append("Clients:\n");
310 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800311 sp<Client> client = mClients.valueAt(i).promote();
312 if (client != 0) {
313 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
314 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315 }
316 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700317
318 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800319 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700320 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
321 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800322 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700323 result.append(buffer);
324 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326}
327
328
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700329void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330{
331 const size_t SIZE = 256;
332 char buffer[SIZE];
333 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800334 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335
John Grossman4ff14ba2012-02-08 16:37:41 -0800336 snprintf(buffer, SIZE, "Hardware status: %d\n"
337 "Standby Time mSec: %u\n",
338 hardwareStatus,
339 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700340 result.append(buffer);
341 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342}
343
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700344void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345{
346 const size_t SIZE = 256;
347 char buffer[SIZE];
348 String8 result;
349 snprintf(buffer, SIZE, "Permission Denial: "
350 "can't dump AudioFlinger from pid=%d, uid=%d\n",
351 IPCThreadState::self()->getCallingPid(),
352 IPCThreadState::self()->getCallingUid());
353 result.append(buffer);
354 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355}
356
357static bool tryLock(Mutex& mutex)
358{
359 bool locked = false;
360 for (int i = 0; i < kDumpLockRetries; ++i) {
361 if (mutex.tryLock() == NO_ERROR) {
362 locked = true;
363 break;
364 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800365 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700366 }
367 return locked;
368}
369
370status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
371{
Glenn Kasten44deb052012-02-05 18:09:08 -0800372 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 dumpPermissionDenial(fd, args);
374 } else {
375 // get state of hardware lock
376 bool hardwareLocked = tryLock(mHardwareLock);
377 if (!hardwareLocked) {
378 String8 result(kHardwareLockedString);
379 write(fd, result.string(), result.size());
380 } else {
381 mHardwareLock.unlock();
382 }
383
384 bool locked = tryLock(mLock);
385
386 // failed to lock - AudioFlinger is probably deadlocked
387 if (!locked) {
388 String8 result(kDeadlockedString);
389 write(fd, result.string(), result.size());
390 }
391
392 dumpClients(fd, args);
393 dumpInternals(fd, args);
394
395 // dump playback threads
396 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
397 mPlaybackThreads.valueAt(i)->dump(fd, args);
398 }
399
400 // dump record threads
401 for (size_t i = 0; i < mRecordThreads.size(); i++) {
402 mRecordThreads.valueAt(i)->dump(fd, args);
403 }
404
Dima Zavin799a70e2011-04-18 16:57:27 -0700405 // dump all hardware devs
406 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700407 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700408 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 }
410 if (locked) mLock.unlock();
411 }
412 return NO_ERROR;
413}
414
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800415sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
416{
417 // If pid is already in the mClients wp<> map, then use that entry
418 // (for which promote() is always != 0), otherwise create a new entry and Client.
419 sp<Client> client = mClients.valueFor(pid).promote();
420 if (client == 0) {
421 client = new Client(this, pid);
422 mClients.add(pid, client);
423 }
424
425 return client;
426}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700427
428// IAudioFlinger interface
429
430
431sp<IAudioTrack> AudioFlinger::createTrack(
432 pid_t pid,
Glenn Kastenfff6d712012-01-12 16:38:12 -0800433 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700434 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800435 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700436 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700437 int frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -0800438 IAudioFlinger::track_flags_t flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800440 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800441 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442 int *sessionId,
443 status_t *status)
444{
445 sp<PlaybackThread::Track> track;
446 sp<TrackHandle> trackHandle;
447 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700448 status_t lStatus;
449 int lSessionId;
450
Glenn Kasten263709e2012-01-06 08:40:01 -0800451 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
452 // but if someone uses binder directly they could bypass that and cause us to crash
453 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000454 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700455 lStatus = BAD_VALUE;
456 goto Exit;
457 }
458
459 {
460 Mutex::Autolock _l(mLock);
461 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700462 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000464 ALOGE("unknown output thread");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700465 lStatus = BAD_VALUE;
466 goto Exit;
467 }
468
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800469 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470
Steve Block3856b092011-10-20 11:56:00 +0100471 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700472 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700473 // check if an effect chain with the same session ID is present on another
474 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700475 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700476 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
477 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700478 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700479 if (sessions & PlaybackThread::EFFECT_SESSION) {
480 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700481 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700482 }
Eric Laurentde070132010-07-13 04:45:46 -0700483 }
484 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700485 lSessionId = *sessionId;
486 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700487 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700488 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700489 if (sessionId != NULL) {
490 *sessionId = lSessionId;
491 }
492 }
Steve Block3856b092011-10-20 11:56:00 +0100493 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700494
495 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800496 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700497
498 // move effect chain to this output thread if an effect on same session was waiting
499 // for a track to be created
500 if (lStatus == NO_ERROR && effectThread != NULL) {
501 Mutex::Autolock _dl(thread->mLock);
502 Mutex::Autolock _sl(effectThread->mLock);
503 moveEffectChain_l(lSessionId, effectThread, thread, true);
504 }
Eric Laurenta011e352012-03-29 15:51:43 -0700505
506 // Look for sync events awaiting for a session to be used.
507 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
508 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
509 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700510 if (lStatus == NO_ERROR) {
511 track->setSyncEvent(mPendingSyncEvents[i]);
512 } else {
513 mPendingSyncEvents[i]->cancel();
514 }
Eric Laurenta011e352012-03-29 15:51:43 -0700515 mPendingSyncEvents.removeAt(i);
516 i--;
517 }
518 }
519 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700520 }
521 if (lStatus == NO_ERROR) {
522 trackHandle = new TrackHandle(track);
523 } else {
524 // remove local strong reference to Client before deleting the Track so that the Client
525 // destructor is called by the TrackBase destructor with mLock held
526 client.clear();
527 track.clear();
528 }
529
530Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700531 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700532 *status = lStatus;
533 }
534 return trackHandle;
535}
536
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800537uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538{
539 Mutex::Autolock _l(mLock);
540 PlaybackThread *thread = checkPlaybackThread_l(output);
541 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000542 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 return 0;
544 }
545 return thread->sampleRate();
546}
547
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800548int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549{
550 Mutex::Autolock _l(mLock);
551 PlaybackThread *thread = checkPlaybackThread_l(output);
552 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000553 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700554 return 0;
555 }
556 return thread->channelCount();
557}
558
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800559audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560{
561 Mutex::Autolock _l(mLock);
562 PlaybackThread *thread = checkPlaybackThread_l(output);
563 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000564 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800565 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700566 }
567 return thread->format();
568}
569
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800570size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571{
572 Mutex::Autolock _l(mLock);
573 PlaybackThread *thread = checkPlaybackThread_l(output);
574 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000575 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700576 return 0;
577 }
Glenn Kasten58912562012-04-03 10:45:00 -0700578 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
579 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580 return thread->frameCount();
581}
582
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800583uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584{
585 Mutex::Autolock _l(mLock);
586 PlaybackThread *thread = checkPlaybackThread_l(output);
587 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000588 ALOGW("latency() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589 return 0;
590 }
591 return thread->latency();
592}
593
594status_t AudioFlinger::setMasterVolume(float value)
595{
Eric Laurenta1884f92011-08-23 08:25:03 -0700596 status_t ret = initCheck();
597 if (ret != NO_ERROR) {
598 return ret;
599 }
600
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601 // check calling permissions
602 if (!settingsAllowed()) {
603 return PERMISSION_DENIED;
604 }
605
Eric Laurenta4c5a552012-03-29 10:12:40 -0700606 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700607 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700608
John Grossmanee578c02012-07-23 17:05:46 -0700609 // Set master volume in the HALs which support it.
610 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
611 AutoMutex lock(mHardwareLock);
612 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800613
John Grossmanee578c02012-07-23 17:05:46 -0700614 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
615 if (dev->canSetMasterVolume()) {
616 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800617 }
John Grossmanee578c02012-07-23 17:05:46 -0700618 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700620
John Grossmanee578c02012-07-23 17:05:46 -0700621 // Now set the master volume in each playback thread. Playback threads
622 // assigned to HALs which do not have master volume support will apply
623 // master volume during the mix operation. Threads with HALs which do
624 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800625 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700626 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700627
628 return NO_ERROR;
629}
630
Glenn Kastenf78aee72012-01-04 11:00:47 -0800631status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632{
Eric Laurenta1884f92011-08-23 08:25:03 -0700633 status_t ret = initCheck();
634 if (ret != NO_ERROR) {
635 return ret;
636 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637
638 // check calling permissions
639 if (!settingsAllowed()) {
640 return PERMISSION_DENIED;
641 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800642 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000643 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644 return BAD_VALUE;
645 }
646
647 { // scope for the lock
648 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700649 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700651 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652 mHardwareStatus = AUDIO_HW_IDLE;
653 }
654
655 if (NO_ERROR == ret) {
656 Mutex::Autolock _l(mLock);
657 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800658 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700659 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660 }
661
662 return ret;
663}
664
665status_t AudioFlinger::setMicMute(bool state)
666{
Eric Laurenta1884f92011-08-23 08:25:03 -0700667 status_t ret = initCheck();
668 if (ret != NO_ERROR) {
669 return ret;
670 }
671
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 // check calling permissions
673 if (!settingsAllowed()) {
674 return PERMISSION_DENIED;
675 }
676
677 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700678 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700680 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 mHardwareStatus = AUDIO_HW_IDLE;
682 return ret;
683}
684
685bool AudioFlinger::getMicMute() const
686{
Eric Laurenta1884f92011-08-23 08:25:03 -0700687 status_t ret = initCheck();
688 if (ret != NO_ERROR) {
689 return false;
690 }
691
Dima Zavinfce7a472011-04-19 22:30:36 -0700692 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800693 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700694 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700696 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697 mHardwareStatus = AUDIO_HW_IDLE;
698 return state;
699}
700
701status_t AudioFlinger::setMasterMute(bool muted)
702{
John Grossmand8f178d2012-07-20 14:51:35 -0700703 status_t ret = initCheck();
704 if (ret != NO_ERROR) {
705 return ret;
706 }
707
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 // check calling permissions
709 if (!settingsAllowed()) {
710 return PERMISSION_DENIED;
711 }
712
John Grossmanee578c02012-07-23 17:05:46 -0700713 Mutex::Autolock _l(mLock);
714 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700715
John Grossmanee578c02012-07-23 17:05:46 -0700716 // Set master mute in the HALs which support it.
717 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
718 AutoMutex lock(mHardwareLock);
719 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700720
John Grossmanee578c02012-07-23 17:05:46 -0700721 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
722 if (dev->canSetMasterMute()) {
723 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700724 }
John Grossmanee578c02012-07-23 17:05:46 -0700725 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700726 }
727
John Grossmanee578c02012-07-23 17:05:46 -0700728 // Now set the master mute in each playback thread. Playback threads
729 // assigned to HALs which do not have master mute support will apply master
730 // mute during the mix operation. Threads with HALs which do support master
731 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800732 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700733 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734
735 return NO_ERROR;
736}
737
738float AudioFlinger::masterVolume() const
739{
Glenn Kasten98067102011-12-13 11:47:54 -0800740 Mutex::Autolock _l(mLock);
741 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742}
743
744bool AudioFlinger::masterMute() const
745{
Glenn Kasten98067102011-12-13 11:47:54 -0800746 Mutex::Autolock _l(mLock);
747 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748}
749
John Grossman4ff14ba2012-02-08 16:37:41 -0800750float AudioFlinger::masterVolume_l() const
751{
John Grossman4ff14ba2012-02-08 16:37:41 -0800752 return mMasterVolume;
753}
754
John Grossmand8f178d2012-07-20 14:51:35 -0700755bool AudioFlinger::masterMute_l() const
756{
John Grossmanee578c02012-07-23 17:05:46 -0700757 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700758}
759
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800760status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
761 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700762{
763 // check calling permissions
764 if (!settingsAllowed()) {
765 return PERMISSION_DENIED;
766 }
767
Glenn Kasten263709e2012-01-06 08:40:01 -0800768 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000769 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700770 return BAD_VALUE;
771 }
772
773 AutoMutex lock(mLock);
774 PlaybackThread *thread = NULL;
775 if (output) {
776 thread = checkPlaybackThread_l(output);
777 if (thread == NULL) {
778 return BAD_VALUE;
779 }
780 }
781
782 mStreamTypes[stream].volume = value;
783
784 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800785 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700786 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700787 }
788 } else {
789 thread->setStreamVolume(stream, value);
790 }
791
792 return NO_ERROR;
793}
794
Glenn Kastenfff6d712012-01-12 16:38:12 -0800795status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796{
797 // check calling permissions
798 if (!settingsAllowed()) {
799 return PERMISSION_DENIED;
800 }
801
Glenn Kasten263709e2012-01-06 08:40:01 -0800802 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700803 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000804 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805 return BAD_VALUE;
806 }
807
Eric Laurent93575202011-01-18 18:39:02 -0800808 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809 mStreamTypes[stream].mute = muted;
810 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700811 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812
813 return NO_ERROR;
814}
815
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800816float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817{
Glenn Kasten263709e2012-01-06 08:40:01 -0800818 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 return 0.0f;
820 }
821
822 AutoMutex lock(mLock);
823 float volume;
824 if (output) {
825 PlaybackThread *thread = checkPlaybackThread_l(output);
826 if (thread == NULL) {
827 return 0.0f;
828 }
829 volume = thread->streamVolume(stream);
830 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800831 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832 }
833
834 return volume;
835}
836
Glenn Kastenfff6d712012-01-12 16:38:12 -0800837bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838{
Glenn Kasten263709e2012-01-06 08:40:01 -0800839 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840 return true;
841 }
842
Glenn Kasten6637baa2012-01-09 09:40:36 -0800843 AutoMutex lock(mLock);
844 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845}
846
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800847status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848{
Glenn Kasten23d82a92012-02-03 11:10:00 -0800849 ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling pid %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850 ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid());
851 // check calling permissions
852 if (!settingsAllowed()) {
853 return PERMISSION_DENIED;
854 }
855
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856 // ioHandle == 0 means the parameters are global to the audio hardware interface
857 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700858 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700859 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800860 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700861 AutoMutex lock(mHardwareLock);
862 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
863 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
864 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
865 status_t result = dev->set_parameters(dev, keyValuePairs.string());
866 final_result = result ?: final_result;
867 }
868 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800869 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700870 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
871 AudioParameter param = AudioParameter(keyValuePairs);
872 String8 value;
873 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700874 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
875 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700876 for (size_t i = 0; i < mRecordThreads.size(); i++) {
877 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Glenn Kasten510a3d62012-07-16 14:24:34 -0700878 audio_devices_t device = thread->device() & AUDIO_DEVICE_IN_ALL;
879 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
880 // collect all of the thread's session IDs
881 KeyedVector<int, bool> ids = thread->sessionIds();
882 // suspend effects associated with those session IDs
883 for (size_t j = 0; j < ids.size(); ++j) {
884 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700885 thread->setEffectSuspended(FX_IID_AEC,
886 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700887 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700888 thread->setEffectSuspended(FX_IID_NS,
889 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700890 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700891 }
892 }
Eric Laurentbee53372011-08-29 12:42:48 -0700893 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700894 }
895 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700896 String8 screenState;
897 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
898 bool isOff = screenState == "off";
899 if (isOff != (gScreenState & 1)) {
900 gScreenState = ((gScreenState & ~1) + 2) | isOff;
901 }
902 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700903 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700904 }
905
906 // hold a strong ref on thread in case closeOutput() or closeInput() is called
907 // and the thread is exited once the lock is released
908 sp<ThreadBase> thread;
909 {
910 Mutex::Autolock _l(mLock);
911 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700912 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800914 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700915 // indicate output device change to all input threads for pre processing
916 AudioParameter param = AudioParameter(keyValuePairs);
917 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700918 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
919 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700920 for (size_t i = 0; i < mRecordThreads.size(); i++) {
921 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
922 }
923 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 }
925 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800926 if (thread != 0) {
927 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 }
929 return BAD_VALUE;
930}
931
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800932String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933{
Glenn Kasten23d82a92012-02-03 11:10:00 -0800934// ALOGV("getParameters() io %d, keys %s, tid %d, calling pid %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935// ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid());
936
Eric Laurenta4c5a552012-03-29 10:12:40 -0700937 Mutex::Autolock _l(mLock);
938
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700940 String8 out_s8;
941
Dima Zavin799a70e2011-04-18 16:57:27 -0700942 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800943 char *s;
944 {
945 AutoMutex lock(mHardwareLock);
946 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700947 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800948 s = dev->get_parameters(dev, keys.string());
949 mHardwareStatus = AUDIO_HW_IDLE;
950 }
John Grossmanef7740b2012-02-09 11:28:36 -0800951 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700952 free(s);
953 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700954 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 }
956
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
958 if (playbackThread != NULL) {
959 return playbackThread->getParameters(keys);
960 }
961 RecordThread *recordThread = checkRecordThread_l(ioHandle);
962 if (recordThread != NULL) {
963 return recordThread->getParameters(keys);
964 }
965 return String8("");
966}
967
Glenn Kastendd8104c2012-07-02 12:42:44 -0700968size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
969 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970{
Eric Laurenta1884f92011-08-23 08:25:03 -0700971 status_t ret = initCheck();
972 if (ret != NO_ERROR) {
973 return 0;
974 }
975
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800976 AutoMutex lock(mHardwareLock);
977 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700978 struct audio_config config = {
979 sample_rate: sampleRate,
Glenn Kastendd8104c2012-07-02 12:42:44 -0700980 channel_mask: channelMask,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700981 format: format,
982 };
John Grossmanee578c02012-07-23 17:05:46 -0700983 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
984 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800985 mHardwareStatus = AUDIO_HW_IDLE;
986 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700987}
988
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800989unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 Mutex::Autolock _l(mLock);
992
993 RecordThread *recordThread = checkRecordThread_l(ioHandle);
994 if (recordThread != NULL) {
995 return recordThread->getInputFramesLost();
996 }
997 return 0;
998}
999
1000status_t AudioFlinger::setVoiceVolume(float value)
1001{
Eric Laurenta1884f92011-08-23 08:25:03 -07001002 status_t ret = initCheck();
1003 if (ret != NO_ERROR) {
1004 return ret;
1005 }
1006
Mathias Agopian65ab4712010-07-14 17:59:35 -07001007 // check calling permissions
1008 if (!settingsAllowed()) {
1009 return PERMISSION_DENIED;
1010 }
1011
1012 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001013 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001014 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001015 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016 mHardwareStatus = AUDIO_HW_IDLE;
1017
1018 return ret;
1019}
1020
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001021status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
1022 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023{
1024 status_t status;
1025
1026 Mutex::Autolock _l(mLock);
1027
1028 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1029 if (playbackThread != NULL) {
1030 return playbackThread->getRenderPosition(halFrames, dspFrames);
1031 }
1032
1033 return BAD_VALUE;
1034}
1035
1036void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1037{
1038
1039 Mutex::Autolock _l(mLock);
1040
Glenn Kastenbb001922012-02-03 11:10:26 -08001041 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042 if (mNotificationClients.indexOfKey(pid) < 0) {
1043 sp<NotificationClient> notificationClient = new NotificationClient(this,
1044 client,
1045 pid);
Steve Block3856b092011-10-20 11:56:00 +01001046 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047
1048 mNotificationClients.add(pid, notificationClient);
1049
1050 sp<IBinder> binder = client->asBinder();
1051 binder->linkToDeath(notificationClient);
1052
1053 // the config change is always sent from playback or record threads to avoid deadlock
1054 // with AudioSystem::gLock
1055 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1056 mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED);
1057 }
1058
1059 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1060 mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED);
1061 }
1062 }
1063}
1064
1065void AudioFlinger::removeNotificationClient(pid_t pid)
1066{
1067 Mutex::Autolock _l(mLock);
1068
Glenn Kastena3b09252012-01-20 09:19:01 -08001069 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001070
Steve Block3856b092011-10-20 11:56:00 +01001071 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001072 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001073 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001074 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001075 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001076 ALOGV(" pid %d @ %d", ref->mPid, i);
1077 if (ref->mPid == pid) {
1078 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001079 mAudioSessionRefs.removeAt(i);
1080 delete ref;
1081 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001082 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001083 } else {
1084 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001085 }
1086 }
1087 if (removed) {
1088 purgeStaleEffects_l();
1089 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090}
1091
1092// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001093void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001094{
1095 size_t size = mNotificationClients.size();
1096 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001097 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1098 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001099 }
1100}
1101
1102// removeClient_l() must be called with AudioFlinger::mLock held
1103void AudioFlinger::removeClient_l(pid_t pid)
1104{
Steve Block3856b092011-10-20 11:56:00 +01001105 ALOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106 mClients.removeItem(pid);
1107}
1108
Eric Laurent717e1282012-06-29 16:36:52 -07001109// getEffectThread_l() must be called with AudioFlinger::mLock held
1110sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1111{
1112 sp<PlaybackThread> thread;
1113
1114 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1115 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1116 ALOG_ASSERT(thread == 0);
1117 thread = mPlaybackThreads.valueAt(i);
1118 }
1119 }
1120
1121 return thread;
1122}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123
1124// ----------------------------------------------------------------------------
1125
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001126AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001127 audio_devices_t device, type_t type)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128 : Thread(false),
Glenn Kasten23bb8be2012-01-26 10:38:26 -08001129 mType(type),
Glenn Kasten58912562012-04-03 10:45:00 -07001130 mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mNormalFrameCount(0),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001131 // mChannelMask
1132 mChannelCount(0),
1133 mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID),
1134 mParamStatus(NO_ERROR),
Glenn Kasten5ad92f62012-07-19 10:02:15 -07001135 mStandby(false), mDevice(device), mId(id),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001136 mDeathRecipient(new PMDeathRecipient(this))
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137{
1138}
1139
1140AudioFlinger::ThreadBase::~ThreadBase()
1141{
1142 mParamCond.broadcast();
Eric Laurentfeb0db62011-07-22 09:04:31 -07001143 // do not lock the mutex in destructor
1144 releaseWakeLock_l();
Eric Laurent9d18ec52011-09-27 12:07:15 -07001145 if (mPowerManager != 0) {
1146 sp<IBinder> binder = mPowerManager->asBinder();
1147 binder->unlinkToDeath(mDeathRecipient);
1148 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149}
1150
1151void AudioFlinger::ThreadBase::exit()
1152{
Steve Block3856b092011-10-20 11:56:00 +01001153 ALOGV("ThreadBase::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154 {
Glenn Kastenb28686f2012-01-06 08:39:38 -08001155 // This lock prevents the following race in thread (uniprocessor for illustration):
1156 // if (!exitPending()) {
1157 // // context switch from here to exit()
1158 // // exit() calls requestExit(), what exitPending() observes
1159 // // exit() calls signal(), which is dropped since no waiters
1160 // // context switch back from exit() to here
1161 // mWaitWorkCV.wait(...);
1162 // // now thread is hung
1163 // }
Glenn Kastena7d8d6f2012-01-05 15:41:56 -08001164 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001165 requestExit();
1166 mWaitWorkCV.signal();
1167 }
Glenn Kastenb28686f2012-01-06 08:39:38 -08001168 // When Thread::requestExitAndWait is made virtual and this method is renamed to
1169 // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();"
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170 requestExitAndWait();
1171}
1172
Mathias Agopian65ab4712010-07-14 17:59:35 -07001173status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs)
1174{
1175 status_t status;
1176
Steve Block3856b092011-10-20 11:56:00 +01001177 ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178 Mutex::Autolock _l(mLock);
1179
1180 mNewParameters.add(keyValuePairs);
1181 mWaitWorkCV.signal();
1182 // wait condition with timeout in case the thread loop has exited
1183 // before the request could be processed
Glenn Kasten7dede872011-12-13 11:04:14 -08001184 if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185 status = mParamStatus;
1186 mWaitWorkCV.signal();
1187 } else {
1188 status = TIMED_OUT;
1189 }
1190 return status;
1191}
1192
1193void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param)
1194{
1195 Mutex::Autolock _l(mLock);
1196 sendConfigEvent_l(event, param);
1197}
1198
1199// sendConfigEvent_l() must be called with ThreadBase::mLock held
1200void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param)
1201{
Glenn Kastenf3990f22011-12-13 11:50:00 -08001202 ConfigEvent configEvent;
1203 configEvent.mEvent = event;
1204 configEvent.mParam = param;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001205 mConfigEvents.add(configEvent);
Steve Block3856b092011-10-20 11:56:00 +01001206 ALOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207 mWaitWorkCV.signal();
1208}
1209
1210void AudioFlinger::ThreadBase::processConfigEvents()
1211{
1212 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001213 while (!mConfigEvents.isEmpty()) {
Steve Block3856b092011-10-20 11:56:00 +01001214 ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size());
Glenn Kastenf3990f22011-12-13 11:50:00 -08001215 ConfigEvent configEvent = mConfigEvents[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001216 mConfigEvents.removeAt(0);
1217 // release mLock before locking AudioFlinger mLock: lock order is always
1218 // AudioFlinger then ThreadBase to avoid cross deadlock
1219 mLock.unlock();
1220 mAudioFlinger->mLock.lock();
Glenn Kastenf3990f22011-12-13 11:50:00 -08001221 audioConfigChanged_l(configEvent.mEvent, configEvent.mParam);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222 mAudioFlinger->mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001223 mLock.lock();
1224 }
1225 mLock.unlock();
1226}
1227
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07001228void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001229{
1230 const size_t SIZE = 256;
1231 char buffer[SIZE];
1232 String8 result;
1233
1234 bool locked = tryLock(mLock);
1235 if (!locked) {
1236 snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this);
1237 write(fd, buffer, strlen(buffer));
1238 }
1239
Eric Laurent612bbb52012-03-14 15:03:26 -07001240 snprintf(buffer, SIZE, "io handle: %d\n", mId);
1241 result.append(buffer);
1242 snprintf(buffer, SIZE, "TID: %d\n", getTid());
1243 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 snprintf(buffer, SIZE, "standby: %d\n", mStandby);
1245 result.append(buffer);
1246 snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate);
1247 result.append(buffer);
Glenn Kasten58912562012-04-03 10:45:00 -07001248 snprintf(buffer, SIZE, "HAL frame count: %d\n", mFrameCount);
1249 result.append(buffer);
1250 snprintf(buffer, SIZE, "Normal frame count: %d\n", mNormalFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251 result.append(buffer);
1252 snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount);
1253 result.append(buffer);
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001254 snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask);
1255 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256 snprintf(buffer, SIZE, "Format: %d\n", mFormat);
1257 result.append(buffer);
Glenn Kastenb9980652012-01-11 09:48:27 -08001258 snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 result.append(buffer);
1260
1261 snprintf(buffer, SIZE, "\nPending setParameters commands: \n");
1262 result.append(buffer);
1263 result.append(" Index Command");
1264 for (size_t i = 0; i < mNewParameters.size(); ++i) {
1265 snprintf(buffer, SIZE, "\n %02d ", i);
1266 result.append(buffer);
1267 result.append(mNewParameters[i]);
1268 }
1269
1270 snprintf(buffer, SIZE, "\n\nPending config events: \n");
1271 result.append(buffer);
1272 snprintf(buffer, SIZE, " Index event param\n");
1273 result.append(buffer);
1274 for (size_t i = 0; i < mConfigEvents.size(); i++) {
Glenn Kastenf3990f22011-12-13 11:50:00 -08001275 snprintf(buffer, SIZE, " %02d %02d %d\n", i, mConfigEvents[i].mEvent, mConfigEvents[i].mParam);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276 result.append(buffer);
1277 }
1278 result.append("\n");
1279
1280 write(fd, result.string(), result.size());
1281
1282 if (locked) {
1283 mLock.unlock();
1284 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001285}
1286
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07001287void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args)
Eric Laurent1d2bff02011-07-24 17:49:51 -07001288{
1289 const size_t SIZE = 256;
1290 char buffer[SIZE];
1291 String8 result;
1292
1293 snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size());
1294 write(fd, buffer, strlen(buffer));
1295
1296 for (size_t i = 0; i < mEffectChains.size(); ++i) {
1297 sp<EffectChain> chain = mEffectChains[i];
1298 if (chain != 0) {
1299 chain->dump(fd, args);
1300 }
1301 }
Eric Laurent1d2bff02011-07-24 17:49:51 -07001302}
1303
Eric Laurentfeb0db62011-07-22 09:04:31 -07001304void AudioFlinger::ThreadBase::acquireWakeLock()
1305{
1306 Mutex::Autolock _l(mLock);
1307 acquireWakeLock_l();
1308}
1309
1310void AudioFlinger::ThreadBase::acquireWakeLock_l()
1311{
1312 if (mPowerManager == 0) {
1313 // use checkService() to avoid blocking if power service is not up yet
1314 sp<IBinder> binder =
1315 defaultServiceManager()->checkService(String16("power"));
1316 if (binder == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001317 ALOGW("Thread %s cannot connect to the power manager service", mName);
Eric Laurentfeb0db62011-07-22 09:04:31 -07001318 } else {
1319 mPowerManager = interface_cast<IPowerManager>(binder);
1320 binder->linkToDeath(mDeathRecipient);
1321 }
1322 }
1323 if (mPowerManager != 0) {
1324 sp<IBinder> binder = new BBinder();
1325 status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
1326 binder,
1327 String16(mName));
1328 if (status == NO_ERROR) {
1329 mWakeLockToken = binder;
1330 }
Steve Block3856b092011-10-20 11:56:00 +01001331 ALOGV("acquireWakeLock_l() %s status %d", mName, status);
Eric Laurentfeb0db62011-07-22 09:04:31 -07001332 }
1333}
1334
1335void AudioFlinger::ThreadBase::releaseWakeLock()
1336{
1337 Mutex::Autolock _l(mLock);
Eric Laurent6dbe8832011-07-28 13:59:02 -07001338 releaseWakeLock_l();
Eric Laurentfeb0db62011-07-22 09:04:31 -07001339}
1340
1341void AudioFlinger::ThreadBase::releaseWakeLock_l()
1342{
1343 if (mWakeLockToken != 0) {
Steve Block3856b092011-10-20 11:56:00 +01001344 ALOGV("releaseWakeLock_l() %s", mName);
Eric Laurentfeb0db62011-07-22 09:04:31 -07001345 if (mPowerManager != 0) {
1346 mPowerManager->releaseWakeLock(mWakeLockToken, 0);
1347 }
1348 mWakeLockToken.clear();
1349 }
1350}
1351
1352void AudioFlinger::ThreadBase::clearPowerManager()
1353{
1354 Mutex::Autolock _l(mLock);
1355 releaseWakeLock_l();
1356 mPowerManager.clear();
1357}
1358
1359void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who)
1360{
1361 sp<ThreadBase> thread = mThread.promote();
1362 if (thread != 0) {
1363 thread->clearPowerManager();
1364 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001365 ALOGW("power manager service died !!!");
Eric Laurentfeb0db62011-07-22 09:04:31 -07001366}
Eric Laurent1d2bff02011-07-24 17:49:51 -07001367
Eric Laurent59255e42011-07-27 19:49:51 -07001368void AudioFlinger::ThreadBase::setEffectSuspended(
1369 const effect_uuid_t *type, bool suspend, int sessionId)
1370{
1371 Mutex::Autolock _l(mLock);
1372 setEffectSuspended_l(type, suspend, sessionId);
1373}
1374
1375void AudioFlinger::ThreadBase::setEffectSuspended_l(
1376 const effect_uuid_t *type, bool suspend, int sessionId)
1377{
Glenn Kasten090f0192012-01-30 13:00:02 -08001378 sp<EffectChain> chain = getEffectChain_l(sessionId);
Eric Laurent59255e42011-07-27 19:49:51 -07001379 if (chain != 0) {
1380 if (type != NULL) {
1381 chain->setEffectSuspended_l(type, suspend);
1382 } else {
1383 chain->setEffectSuspendedAll_l(suspend);
1384 }
1385 }
1386
1387 updateSuspendedSessions_l(type, suspend, sessionId);
1388}
1389
1390void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain)
1391{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001392 ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId());
Eric Laurent59255e42011-07-27 19:49:51 -07001393 if (index < 0) {
1394 return;
1395 }
1396
Glenn Kasten0a7af182012-07-09 16:09:19 -07001397 const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects =
1398 mSuspendedSessions.valueAt(index);
Eric Laurent59255e42011-07-27 19:49:51 -07001399
1400 for (size_t i = 0; i < sessionEffects.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001401 sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i);
Eric Laurent59255e42011-07-27 19:49:51 -07001402 for (int j = 0; j < desc->mRefCount; j++) {
1403 if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) {
1404 chain->setEffectSuspendedAll_l(true);
1405 } else {
Steve Block3856b092011-10-20 11:56:00 +01001406 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001407 desc->mType.timeLow);
Eric Laurent59255e42011-07-27 19:49:51 -07001408 chain->setEffectSuspended_l(&desc->mType, true);
1409 }
1410 }
1411 }
1412}
1413
Eric Laurent59255e42011-07-27 19:49:51 -07001414void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type,
1415 bool suspend,
1416 int sessionId)
1417{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001418 ssize_t index = mSuspendedSessions.indexOfKey(sessionId);
Eric Laurent59255e42011-07-27 19:49:51 -07001419
1420 KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects;
1421
1422 if (suspend) {
1423 if (index >= 0) {
Glenn Kasten0a7af182012-07-09 16:09:19 -07001424 sessionEffects = mSuspendedSessions.valueAt(index);
Eric Laurent59255e42011-07-27 19:49:51 -07001425 } else {
1426 mSuspendedSessions.add(sessionId, sessionEffects);
1427 }
1428 } else {
1429 if (index < 0) {
1430 return;
1431 }
Glenn Kasten0a7af182012-07-09 16:09:19 -07001432 sessionEffects = mSuspendedSessions.valueAt(index);
Eric Laurent59255e42011-07-27 19:49:51 -07001433 }
1434
1435
1436 int key = EffectChain::kKeyForSuspendAll;
1437 if (type != NULL) {
1438 key = type->timeLow;
1439 }
1440 index = sessionEffects.indexOfKey(key);
1441
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001442 sp<SuspendedSessionDesc> desc;
Eric Laurent59255e42011-07-27 19:49:51 -07001443 if (suspend) {
1444 if (index >= 0) {
1445 desc = sessionEffects.valueAt(index);
1446 } else {
1447 desc = new SuspendedSessionDesc();
1448 if (type != NULL) {
Glenn Kastena189a682012-02-20 12:16:30 -08001449 desc->mType = *type;
Eric Laurent59255e42011-07-27 19:49:51 -07001450 }
1451 sessionEffects.add(key, desc);
Steve Block3856b092011-10-20 11:56:00 +01001452 ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key);
Eric Laurent59255e42011-07-27 19:49:51 -07001453 }
1454 desc->mRefCount++;
1455 } else {
1456 if (index < 0) {
1457 return;
1458 }
1459 desc = sessionEffects.valueAt(index);
1460 if (--desc->mRefCount == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001461 ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key);
Eric Laurent59255e42011-07-27 19:49:51 -07001462 sessionEffects.removeItemsAt(index);
1463 if (sessionEffects.isEmpty()) {
Steve Block3856b092011-10-20 11:56:00 +01001464 ALOGV("updateSuspendedSessions_l() restore removing session %d",
Eric Laurent59255e42011-07-27 19:49:51 -07001465 sessionId);
1466 mSuspendedSessions.removeItem(sessionId);
1467 }
1468 }
1469 }
1470 if (!sessionEffects.isEmpty()) {
1471 mSuspendedSessions.replaceValueFor(sessionId, sessionEffects);
1472 }
1473}
1474
1475void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1476 bool enabled,
1477 int sessionId)
1478{
1479 Mutex::Autolock _l(mLock);
Eric Laurenta85a74a2011-10-19 11:44:54 -07001480 checkSuspendOnEffectEnabled_l(effect, enabled, sessionId);
1481}
Eric Laurent59255e42011-07-27 19:49:51 -07001482
Eric Laurenta85a74a2011-10-19 11:44:54 -07001483void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
1484 bool enabled,
1485 int sessionId)
1486{
Eric Laurentdb7c0792011-08-10 10:37:50 -07001487 if (mType != RECORD) {
1488 // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
1489 // another session. This gives the priority to well behaved effect control panels
1490 // and applications not using global effects.
Eric Laurent808e7d12012-05-11 19:44:09 -07001491 // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect
1492 // global effects
1493 if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) {
Eric Laurentdb7c0792011-08-10 10:37:50 -07001494 setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
1495 }
1496 }
Eric Laurent59255e42011-07-27 19:49:51 -07001497
1498 sp<EffectChain> chain = getEffectChain_l(sessionId);
1499 if (chain != 0) {
1500 chain->checkSuspendOnEffectEnabled(effect, enabled);
1501 }
1502}
1503
Mathias Agopian65ab4712010-07-14 17:59:35 -07001504// ----------------------------------------------------------------------------
1505
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001506AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger,
1507 AudioStreamOut* output,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001508 audio_io_handle_t id,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001509 audio_devices_t device,
Glenn Kasten23bb8be2012-01-26 10:38:26 -08001510 type_t type)
1511 : ThreadBase(audioFlinger, id, device, type),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001512 mMixBuffer(NULL), mSuspended(0), mBytesWritten(0),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001513 // mStreamTypes[] initialized in constructor body
1514 mOutput(output),
Glenn Kastenfec279f2012-03-08 07:47:15 -08001515 mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false),
Glenn Kastenaa4397f2012-03-12 18:13:59 -07001516 mMixerStatus(MIXER_IDLE),
Glenn Kasten81028042012-04-30 18:15:12 -07001517 mMixerStatusIgnoringFastTracks(MIXER_IDLE),
Glenn Kasten58912562012-04-03 10:45:00 -07001518 standbyDelay(AudioFlinger::mStandbyTimeInNsecs),
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001519 mScreenState(gScreenState),
Glenn Kasten288ed212012-04-25 17:52:27 -07001520 // index 0 is reserved for normal mixer's submix
1521 mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522{
Glenn Kasten480b4682012-02-28 12:30:08 -08001523 snprintf(mName, kNameLength, "AudioOut_%X", id);
Eric Laurentfeb0db62011-07-22 09:04:31 -07001524
John Grossmanee578c02012-07-23 17:05:46 -07001525 // Assumes constructor is called by AudioFlinger with it's mLock held, but
1526 // it would be safer to explicitly pass initial masterVolume/masterMute as
1527 // parameter.
1528 //
1529 // If the HAL we are using has support for master volume or master mute,
1530 // then do not attenuate or mute during mixing (just leave the volume at 1.0
1531 // and the mute set to false).
1532 mMasterVolume = audioFlinger->masterVolume_l();
1533 mMasterMute = audioFlinger->masterMute_l();
1534 if (mOutput && mOutput->audioHwDev) {
1535 if (mOutput->audioHwDev->canSetMasterVolume()) {
1536 mMasterVolume = 1.0;
1537 }
1538
1539 if (mOutput->audioHwDev->canSetMasterMute()) {
1540 mMasterMute = false;
1541 }
1542 }
1543
Mathias Agopian65ab4712010-07-14 17:59:35 -07001544 readOutputParameters();
1545
Glenn Kasten263709e2012-01-06 08:40:01 -08001546 // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor
Glenn Kastenfff6d712012-01-12 16:38:12 -08001547 // There is no AUDIO_STREAM_MIN, and ++ operator does not compile
1548 for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT;
1549 stream = (audio_stream_type_t) (stream + 1)) {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001550 mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream);
1551 mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001552 }
Glenn Kasten6637baa2012-01-09 09:40:36 -08001553 // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here,
1554 // because mAudioFlinger doesn't have one to copy from
Mathias Agopian65ab4712010-07-14 17:59:35 -07001555}
1556
1557AudioFlinger::PlaybackThread::~PlaybackThread()
1558{
1559 delete [] mMixBuffer;
1560}
1561
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07001562void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563{
1564 dumpInternals(fd, args);
1565 dumpTracks(fd, args);
1566 dumpEffectChains(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001567}
1568
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07001569void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001570{
1571 const size_t SIZE = 256;
1572 char buffer[SIZE];
1573 String8 result;
1574
Glenn Kasten58912562012-04-03 10:45:00 -07001575 result.appendFormat("Output thread %p stream volumes in dB:\n ", this);
1576 for (int i = 0; i < AUDIO_STREAM_CNT; ++i) {
1577 const stream_type_t *st = &mStreamTypes[i];
1578 if (i > 0) {
1579 result.appendFormat(", ");
1580 }
1581 result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume));
1582 if (st->mute) {
1583 result.append("M");
1584 }
1585 }
1586 result.append("\n");
1587 write(fd, result.string(), result.length());
1588 result.clear();
1589
Mathias Agopian65ab4712010-07-14 17:59:35 -07001590 snprintf(buffer, SIZE, "Output thread %p tracks\n", this);
1591 result.append(buffer);
Glenn Kasten288ed212012-04-25 17:52:27 -07001592 Track::appendDumpHeader(result);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001593 for (size_t i = 0; i < mTracks.size(); ++i) {
1594 sp<Track> track = mTracks[i];
1595 if (track != 0) {
1596 track->dump(buffer, SIZE);
1597 result.append(buffer);
1598 }
1599 }
1600
1601 snprintf(buffer, SIZE, "Output thread %p active tracks\n", this);
1602 result.append(buffer);
Glenn Kasten288ed212012-04-25 17:52:27 -07001603 Track::appendDumpHeader(result);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001604 for (size_t i = 0; i < mActiveTracks.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -08001605 sp<Track> track = mActiveTracks[i].promote();
1606 if (track != 0) {
1607 track->dump(buffer, SIZE);
1608 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609 }
1610 }
1611 write(fd, result.string(), result.size());
Glenn Kasten88cbea82012-05-15 07:39:27 -07001612
1613 // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way.
1614 FastTrackUnderruns underruns = getFastTrackUnderruns(0);
1615 fdprintf(fd, "Normal mixer raw underrun counters: partial=%u empty=%u\n",
1616 underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001617}
1618
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07001619void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001620{
1621 const size_t SIZE = 256;
1622 char buffer[SIZE];
1623 String8 result;
1624
1625 snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this);
1626 result.append(buffer);
1627 snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime));
1628 result.append(buffer);
1629 snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites);
1630 result.append(buffer);
1631 snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites);
1632 result.append(buffer);
1633 snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite);
1634 result.append(buffer);
1635 snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended);
1636 result.append(buffer);
1637 snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer);
1638 result.append(buffer);
1639 write(fd, result.string(), result.size());
Glenn Kasten1295bb4d2012-05-31 07:43:43 -07001640 fdprintf(fd, "Fast track availMask=%#x\n", mFastTrackAvailMask);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001641
1642 dumpBase(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001643}
1644
1645// Thread virtuals
1646status_t AudioFlinger::PlaybackThread::readyToRun()
1647{
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001648 status_t status = initCheck();
1649 if (status == NO_ERROR) {
Steve Blockdf64d152012-01-04 20:05:49 +00001650 ALOGI("AudioFlinger's thread %p ready to run", this);
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001651 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001652 ALOGE("No working audio driver found.");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001653 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001654 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001655}
1656
1657void AudioFlinger::PlaybackThread::onFirstRef()
1658{
Eric Laurentfeb0db62011-07-22 09:04:31 -07001659 run(mName, ANDROID_PRIORITY_URGENT_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001660}
1661
1662// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
Glenn Kastenea7939a2012-03-14 12:56:26 -07001663sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
Mathias Agopian65ab4712010-07-14 17:59:35 -07001664 const sp<AudioFlinger::Client>& client,
Glenn Kastenfff6d712012-01-12 16:38:12 -08001665 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001666 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001667 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001668 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001669 int frameCount,
1670 const sp<IMemory>& sharedBuffer,
1671 int sessionId,
Glenn Kasten73d22752012-03-19 13:38:30 -07001672 IAudioFlinger::track_flags_t flags,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001673 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001674 status_t *status)
1675{
1676 sp<Track> track;
1677 status_t lStatus;
1678
Glenn Kasten73d22752012-03-19 13:38:30 -07001679 bool isTimed = (flags & IAudioFlinger::TRACK_TIMED) != 0;
1680
1681 // client expresses a preference for FAST, but we get the final say
Glenn Kastene0fa4672012-04-24 14:35:14 -07001682 if (flags & IAudioFlinger::TRACK_FAST) {
1683 if (
Glenn Kasten73d22752012-03-19 13:38:30 -07001684 // not timed
1685 (!isTimed) &&
1686 // either of these use cases:
1687 (
1688 // use case 1: shared buffer with any frame count
1689 (
1690 (sharedBuffer != 0)
1691 ) ||
Glenn Kastene0fa4672012-04-24 14:35:14 -07001692 // use case 2: callback handler and frame count is default or at least as large as HAL
Glenn Kasten73d22752012-03-19 13:38:30 -07001693 (
Glenn Kasten3acbd052012-02-28 10:39:56 -08001694 (tid != -1) &&
Glenn Kastene0fa4672012-04-24 14:35:14 -07001695 ((frameCount == 0) ||
Glenn Kasten300a2ee2012-04-25 13:47:36 -07001696 (frameCount >= (int) (mFrameCount * 2))) // * 2 is due to SRC jitter, see below
Glenn Kasten73d22752012-03-19 13:38:30 -07001697 )
1698 ) &&
1699 // PCM data
1700 audio_is_linear_pcm(format) &&
1701 // mono or stereo
1702 ( (channelMask == AUDIO_CHANNEL_OUT_MONO) ||
1703 (channelMask == AUDIO_CHANNEL_OUT_STEREO) ) &&
Glenn Kasten58912562012-04-03 10:45:00 -07001704#ifndef FAST_TRACKS_AT_NON_NATIVE_SAMPLE_RATE
Glenn Kasten73d22752012-03-19 13:38:30 -07001705 // hardware sample rate
Glenn Kasten58912562012-04-03 10:45:00 -07001706 (sampleRate == mSampleRate) &&
1707#endif
1708 // normal mixer has an associated fast mixer
1709 hasFastMixer() &&
1710 // there are sufficient fast track slots available
1711 (mFastTrackAvailMask != 0)
Glenn Kasten73d22752012-03-19 13:38:30 -07001712 // FIXME test that MixerThread for this fast track has a capable output HAL
1713 // FIXME add a permission test also?
Glenn Kastene0fa4672012-04-24 14:35:14 -07001714 ) {
Glenn Kastene0fa4672012-04-24 14:35:14 -07001715 // if frameCount not specified, then it defaults to fast mixer (HAL) frame count
1716 if (frameCount == 0) {
Glenn Kasten300a2ee2012-04-25 13:47:36 -07001717 frameCount = mFrameCount * 2; // FIXME * 2 is due to SRC jitter, should be computed
Glenn Kastene0fa4672012-04-24 14:35:14 -07001718 }
Glenn Kasten31dfd1d2012-05-01 11:07:08 -07001719 ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d",
Glenn Kasten300a2ee2012-04-25 13:47:36 -07001720 frameCount, mFrameCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001721 } else {
Glenn Kasten852fca92012-05-24 08:44:00 -07001722 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d "
Glenn Kasten254af182012-07-03 14:59:05 -07001723 "mFrameCount=%d format=%d isLinear=%d channelMask=%#x sampleRate=%d mSampleRate=%d "
Glenn Kasten58912562012-04-03 10:45:00 -07001724 "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x",
1725 isTimed, sharedBuffer.get(), frameCount, mFrameCount, format,
1726 audio_is_linear_pcm(format),
1727 channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask);
Glenn Kasten73d22752012-03-19 13:38:30 -07001728 flags &= ~IAudioFlinger::TRACK_FAST;
Glenn Kastene0fa4672012-04-24 14:35:14 -07001729 // For compatibility with AudioTrack calculation, buffer depth is forced
1730 // to be at least 2 x the normal mixer frame count and cover audio hardware latency.
1731 // This is probably too conservative, but legacy application code may depend on it.
1732 // If you change this calculation, also review the start threshold which is related.
1733 uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream);
1734 uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate);
1735 if (minBufCount < 2) {
1736 minBufCount = 2;
Glenn Kasten58912562012-04-03 10:45:00 -07001737 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001738 int minFrameCount = mNormalFrameCount * minBufCount;
1739 if (frameCount < minFrameCount) {
1740 frameCount = minFrameCount;
1741 }
1742 }
Glenn Kasten73d22752012-03-19 13:38:30 -07001743 }
1744
Mathias Agopian65ab4712010-07-14 17:59:35 -07001745 if (mType == DIRECT) {
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001746 if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) {
1747 if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
Steve Block29357bc2012-01-06 19:20:56 +00001748 ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x \""
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001749 "for output %p with format %d",
1750 sampleRate, format, channelMask, mOutput, mFormat);
1751 lStatus = BAD_VALUE;
1752 goto Exit;
1753 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001754 }
1755 } else {
1756 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
1757 if (sampleRate > mSampleRate*2) {
Steve Block29357bc2012-01-06 19:20:56 +00001758 ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001759 lStatus = BAD_VALUE;
1760 goto Exit;
1761 }
1762 }
1763
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001764 lStatus = initCheck();
1765 if (lStatus != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +00001766 ALOGE("Audio driver not initialized.");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001767 goto Exit;
1768 }
1769
1770 { // scope for mLock
1771 Mutex::Autolock _l(mLock);
Eric Laurentde070132010-07-13 04:45:46 -07001772
1773 // all tracks in same audio session must share the same routing strategy otherwise
1774 // conflicts will happen when tracks are moved from one output to another by audio policy
1775 // manager
Glenn Kasten02bbd202012-02-08 12:35:35 -08001776 uint32_t strategy = AudioSystem::getStrategyForStream(streamType);
Eric Laurentde070132010-07-13 04:45:46 -07001777 for (size_t i = 0; i < mTracks.size(); ++i) {
1778 sp<Track> t = mTracks[i];
Glenn Kasten639dbee2012-03-07 12:26:34 -08001779 if (t != 0 && !t->isOutputTrack()) {
Glenn Kasten02bbd202012-02-08 12:35:35 -08001780 uint32_t actual = AudioSystem::getStrategyForStream(t->streamType());
Glenn Kastend8796012011-10-28 10:31:42 -07001781 if (sessionId == t->sessionId() && strategy != actual) {
Steve Block29357bc2012-01-06 19:20:56 +00001782 ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",
Glenn Kastend8796012011-10-28 10:31:42 -07001783 strategy, actual);
Eric Laurentde070132010-07-13 04:45:46 -07001784 lStatus = BAD_VALUE;
1785 goto Exit;
1786 }
1787 }
1788 }
1789
John Grossman4ff14ba2012-02-08 16:37:41 -08001790 if (!isTimed) {
1791 track = new Track(this, client, streamType, sampleRate, format,
Glenn Kasten73d22752012-03-19 13:38:30 -07001792 channelMask, frameCount, sharedBuffer, sessionId, flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001793 } else {
1794 track = TimedTrack::create(this, client, streamType, sampleRate, format,
1795 channelMask, frameCount, sharedBuffer, sessionId);
1796 }
Glenn Kastend5903ec2012-03-18 10:33:27 -07001797 if (track == 0 || track->getCblk() == NULL || track->name() < 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001798 lStatus = NO_MEMORY;
1799 goto Exit;
1800 }
1801 mTracks.add(track);
1802
1803 sp<EffectChain> chain = getEffectChain_l(sessionId);
1804 if (chain != 0) {
Steve Block3856b092011-10-20 11:56:00 +01001805 ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001806 track->setMainBuffer(chain->inBuffer());
Glenn Kasten02bbd202012-02-08 12:35:35 -08001807 chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType()));
Eric Laurentb469b942011-05-09 12:09:06 -07001808 chain->incTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001809 }
1810 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001811
Glenn Kasten3acbd052012-02-28 10:39:56 -08001812 if ((flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) {
1813 pid_t callingPid = IPCThreadState::self()->getCallingPid();
1814 // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
1815 // so ask activity manager to do this on our behalf
Glenn Kastenfd4e20c2012-06-04 11:51:12 -07001816 int err = requestPriority(callingPid, tid, kPriorityAudioApp);
Glenn Kasten3acbd052012-02-28 10:39:56 -08001817 if (err != 0) {
1818 ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d",
Glenn Kastenfd4e20c2012-06-04 11:51:12 -07001819 kPriorityAudioApp, callingPid, tid, err);
Glenn Kasten3acbd052012-02-28 10:39:56 -08001820 }
1821 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001822
Mathias Agopian65ab4712010-07-14 17:59:35 -07001823 lStatus = NO_ERROR;
1824
1825Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001826 if (status) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001827 *status = lStatus;
1828 }
1829 return track;
1830}
1831
Eric Laurente737cda2012-05-22 18:55:44 -07001832uint32_t AudioFlinger::MixerThread::correctLatency(uint32_t latency) const
1833{
1834 if (mFastMixer != NULL) {
1835 MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
1836 latency += (pipe->getAvgFrames() * 1000) / mSampleRate;
1837 }
1838 return latency;
1839}
1840
1841uint32_t AudioFlinger::PlaybackThread::correctLatency(uint32_t latency) const
1842{
1843 return latency;
1844}
1845
Mathias Agopian65ab4712010-07-14 17:59:35 -07001846uint32_t AudioFlinger::PlaybackThread::latency() const
1847{
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001848 Mutex::Autolock _l(mLock);
Marco Nelissenf06c2ed2012-06-06 09:52:31 -07001849 return latency_l();
1850}
1851uint32_t AudioFlinger::PlaybackThread::latency_l() const
1852{
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001853 if (initCheck() == NO_ERROR) {
Eric Laurente737cda2012-05-22 18:55:44 -07001854 return correctLatency(mOutput->stream->get_latency(mOutput->stream));
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001855 } else {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001856 return 0;
1857 }
1858}
1859
Glenn Kasten6637baa2012-01-09 09:40:36 -08001860void AudioFlinger::PlaybackThread::setMasterVolume(float value)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001861{
Glenn Kasten6637baa2012-01-09 09:40:36 -08001862 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -07001863 // Don't apply master volume in SW if our HAL can do it for us.
1864 if (mOutput && mOutput->audioHwDev &&
1865 mOutput->audioHwDev->canSetMasterVolume()) {
1866 mMasterVolume = 1.0;
1867 } else {
1868 mMasterVolume = value;
1869 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001870}
1871
Glenn Kasten6637baa2012-01-09 09:40:36 -08001872void AudioFlinger::PlaybackThread::setMasterMute(bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001873{
Glenn Kasten6637baa2012-01-09 09:40:36 -08001874 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -07001875 // Don't apply master mute in SW if our HAL can do it for us.
1876 if (mOutput && mOutput->audioHwDev &&
1877 mOutput->audioHwDev->canSetMasterMute()) {
1878 mMasterMute = false;
1879 } else {
1880 mMasterMute = muted;
1881 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001882}
1883
Glenn Kasten6637baa2012-01-09 09:40:36 -08001884void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885{
Glenn Kasten6637baa2012-01-09 09:40:36 -08001886 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001887 mStreamTypes[stream].volume = value;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001888}
1889
Glenn Kasten6637baa2012-01-09 09:40:36 -08001890void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001891{
Glenn Kasten6637baa2012-01-09 09:40:36 -08001892 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001893 mStreamTypes[stream].mute = muted;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001894}
1895
Glenn Kastenfff6d712012-01-12 16:38:12 -08001896float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001897{
Glenn Kasten6637baa2012-01-09 09:40:36 -08001898 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001899 return mStreamTypes[stream].volume;
1900}
1901
Mathias Agopian65ab4712010-07-14 17:59:35 -07001902// addTrack_l() must be called with ThreadBase::mLock held
1903status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track)
1904{
1905 status_t status = ALREADY_EXISTS;
1906
1907 // set retry count for buffer fill
1908 track->mRetryCount = kMaxTrackStartupRetries;
1909 if (mActiveTracks.indexOf(track) < 0) {
1910 // the track is newly added, make sure it fills up all its
1911 // buffers before playing. This is to ensure the client will
1912 // effectively get the latency it requested.
1913 track->mFillingUpStatus = Track::FS_FILLING;
1914 track->mResetDone = false;
Eric Laurent29864602012-05-08 18:57:51 -07001915 track->mPresentationCompleteFrames = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001916 mActiveTracks.add(track);
1917 if (track->mainBuffer() != mMixBuffer) {
1918 sp<EffectChain> chain = getEffectChain_l(track->sessionId());
1919 if (chain != 0) {
Steve Block3856b092011-10-20 11:56:00 +01001920 ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId());
Eric Laurentb469b942011-05-09 12:09:06 -07001921 chain->incActiveTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001922 }
1923 }
1924
1925 status = NO_ERROR;
1926 }
1927
Steve Block3856b092011-10-20 11:56:00 +01001928 ALOGV("mWaitWorkCV.broadcast");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001929 mWaitWorkCV.broadcast();
1930
1931 return status;
1932}
1933
1934// destroyTrack_l() must be called with ThreadBase::mLock held
1935void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track)
1936{
1937 track->mState = TrackBase::TERMINATED;
Glenn Kasten288ed212012-04-25 17:52:27 -07001938 // active tracks are removed by threadLoop()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939 if (mActiveTracks.indexOf(track) < 0) {
Eric Laurentb469b942011-05-09 12:09:06 -07001940 removeTrack_l(track);
1941 }
1942}
1943
1944void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track)
1945{
Eric Laurent29864602012-05-08 18:57:51 -07001946 track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
Eric Laurentb469b942011-05-09 12:09:06 -07001947 mTracks.remove(track);
1948 deleteTrackName_l(track->name());
Glenn Kasten288ed212012-04-25 17:52:27 -07001949 // redundant as track is about to be destroyed, for dumpsys only
1950 track->mName = -1;
1951 if (track->isFastTrack()) {
1952 int index = track->mFastIndex;
Eric Laurent29864602012-05-08 18:57:51 -07001953 ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks);
Glenn Kasten288ed212012-04-25 17:52:27 -07001954 ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index)));
1955 mFastTrackAvailMask |= 1 << index;
1956 // redundant as track is about to be destroyed, for dumpsys only
1957 track->mFastIndex = -1;
1958 }
Eric Laurentb469b942011-05-09 12:09:06 -07001959 sp<EffectChain> chain = getEffectChain_l(track->sessionId());
1960 if (chain != 0) {
1961 chain->decTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001962 }
1963}
1964
1965String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
1966{
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001967 String8 out_s8 = String8("");
Dima Zavinfce7a472011-04-19 22:30:36 -07001968 char *s;
1969
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001970 Mutex::Autolock _l(mLock);
1971 if (initCheck() != NO_ERROR) {
1972 return out_s8;
1973 }
1974
Dima Zavin799a70e2011-04-18 16:57:27 -07001975 s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string());
Dima Zavinfce7a472011-04-19 22:30:36 -07001976 out_s8 = String8(s);
1977 free(s);
1978 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979}
1980
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001981// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001982void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) {
1983 AudioSystem::OutputDescriptor desc;
Glenn Kastena0d68332012-01-27 16:47:15 -08001984 void *param2 = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985
Steve Block3856b092011-10-20 11:56:00 +01001986 ALOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001987
1988 switch (event) {
1989 case AudioSystem::OUTPUT_OPENED:
1990 case AudioSystem::OUTPUT_CONFIG_CHANGED:
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001991 desc.channels = mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001992 desc.samplingRate = mSampleRate;
1993 desc.format = mFormat;
Glenn Kasten58912562012-04-03 10:45:00 -07001994 desc.frameCount = mNormalFrameCount; // FIXME see AudioFlinger::frameCount(audio_io_handle_t)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001995 desc.latency = latency();
1996 param2 = &desc;
1997 break;
1998
1999 case AudioSystem::STREAM_CONFIG_CHANGED:
2000 param2 = &param;
2001 case AudioSystem::OUTPUT_CLOSED:
2002 default:
2003 break;
2004 }
2005 mAudioFlinger->audioConfigChanged_l(event, mId, param2);
2006}
2007
2008void AudioFlinger::PlaybackThread::readOutputParameters()
2009{
Dima Zavin799a70e2011-04-18 16:57:27 -07002010 mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common);
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07002011 mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common);
2012 mChannelCount = (uint16_t)popcount(mChannelMask);
Dima Zavin799a70e2011-04-18 16:57:27 -07002013 mFormat = mOutput->stream->common.get_format(&mOutput->stream->common);
Glenn Kastenb9980652012-01-11 09:48:27 -08002014 mFrameSize = audio_stream_frame_size(&mOutput->stream->common);
Dima Zavin799a70e2011-04-18 16:57:27 -07002015 mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize;
Glenn Kasten58912562012-04-03 10:45:00 -07002016 if (mFrameCount & 15) {
2017 ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames",
2018 mFrameCount);
2019 }
2020
Glenn Kasten300a2ee2012-04-25 13:47:36 -07002021 // Calculate size of normal mix buffer relative to the HAL output buffer size
Glenn Kasten4adcede2012-05-14 12:26:02 -07002022 double multiplier = 1.0;
Glenn Kasten300a2ee2012-04-25 13:47:36 -07002023 if (mType == MIXER && (kUseFastMixer == FastMixer_Static || kUseFastMixer == FastMixer_Dynamic)) {
Glenn Kasten58912562012-04-03 10:45:00 -07002024 size_t minNormalFrameCount = (kMinNormalMixBufferSizeMs * mSampleRate) / 1000;
Glenn Kasten4adcede2012-05-14 12:26:02 -07002025 size_t maxNormalFrameCount = (kMaxNormalMixBufferSizeMs * mSampleRate) / 1000;
2026 // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer
2027 minNormalFrameCount = (minNormalFrameCount + 15) & ~15;
2028 maxNormalFrameCount = maxNormalFrameCount & ~15;
2029 if (maxNormalFrameCount < minNormalFrameCount) {
2030 maxNormalFrameCount = minNormalFrameCount;
2031 }
2032 multiplier = (double) minNormalFrameCount / (double) mFrameCount;
2033 if (multiplier <= 1.0) {
2034 multiplier = 1.0;
2035 } else if (multiplier <= 2.0) {
2036 if (2 * mFrameCount <= maxNormalFrameCount) {
2037 multiplier = 2.0;
2038 } else {
2039 multiplier = (double) maxNormalFrameCount / (double) mFrameCount;
2040 }
2041 } else {
2042 // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL SRC
2043 // (it would be unusual for the normal mix buffer size to not be a multiple of fast
2044 // track, but we sometimes have to do this to satisfy the maximum frame count constraint)
2045 // FIXME this rounding up should not be done if no HAL SRC
2046 uint32_t truncMult = (uint32_t) multiplier;
2047 if ((truncMult & 1)) {
2048 if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) {
2049 ++truncMult;
2050 }
2051 }
2052 multiplier = (double) truncMult;
Glenn Kasten58912562012-04-03 10:45:00 -07002053 }
Glenn Kasten58912562012-04-03 10:45:00 -07002054 }
Glenn Kasten4adcede2012-05-14 12:26:02 -07002055 mNormalFrameCount = multiplier * mFrameCount;
2056 // round up to nearest 16 frames to satisfy AudioMixer
2057 mNormalFrameCount = (mNormalFrameCount + 15) & ~15;
Glenn Kasten58912562012-04-03 10:45:00 -07002058 ALOGI("HAL output buffer size %u frames, normal mix buffer size %u frames", mFrameCount, mNormalFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002059
Glenn Kastene9dd0172012-01-27 18:08:45 -08002060 delete[] mMixBuffer;
Eric Laurent67c0a582012-05-01 19:31:12 -07002061 mMixBuffer = new int16_t[mNormalFrameCount * mChannelCount];
2062 memset(mMixBuffer, 0, mNormalFrameCount * mChannelCount * sizeof(int16_t));
Mathias Agopian65ab4712010-07-14 17:59:35 -07002063
Eric Laurentde070132010-07-13 04:45:46 -07002064 // force reconfiguration of effect chains and engines to take new buffer size and audio
2065 // parameters into account
2066 // Note that mLock is not held when readOutputParameters() is called from the constructor
2067 // but in this case nothing is done below as no audio sessions have effect yet so it doesn't
2068 // matter.
2069 // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains
2070 Vector< sp<EffectChain> > effectChains = mEffectChains;
2071 for (size_t i = 0; i < effectChains.size(); i ++) {
Eric Laurent39e94f82010-07-28 01:32:47 -07002072 mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false);
Eric Laurentde070132010-07-13 04:45:46 -07002073 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002074}
2075
Eric Laurente737cda2012-05-22 18:55:44 -07002076
Mathias Agopian65ab4712010-07-14 17:59:35 -07002077status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames)
2078{
Glenn Kastena0d68332012-01-27 16:47:15 -08002079 if (halFrames == NULL || dspFrames == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002080 return BAD_VALUE;
2081 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002082 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002083 if (initCheck() != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002084 return INVALID_OPERATION;
2085 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002086 *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002087
Dima Zavin799a70e2011-04-18 16:57:27 -07002088 return mOutput->stream->get_render_position(mOutput->stream, dspFrames);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002089}
2090
Eric Laurent39e94f82010-07-28 01:32:47 -07002091uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002092{
2093 Mutex::Autolock _l(mLock);
Eric Laurent39e94f82010-07-28 01:32:47 -07002094 uint32_t result = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002095 if (getEffectChain_l(sessionId) != 0) {
Eric Laurent39e94f82010-07-28 01:32:47 -07002096 result = EFFECT_SESSION;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002097 }
2098
2099 for (size_t i = 0; i < mTracks.size(); ++i) {
2100 sp<Track> track = mTracks[i];
Eric Laurentde070132010-07-13 04:45:46 -07002101 if (sessionId == track->sessionId() &&
2102 !(track->mCblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent39e94f82010-07-28 01:32:47 -07002103 result |= TRACK_SESSION;
2104 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002105 }
2106 }
2107
Eric Laurent39e94f82010-07-28 01:32:47 -07002108 return result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002109}
2110
Eric Laurentde070132010-07-13 04:45:46 -07002111uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId)
2112{
Dima Zavinfce7a472011-04-19 22:30:36 -07002113 // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
Eric Laurentde070132010-07-13 04:45:46 -07002114 // it is moved to correct output by audio policy manager when A2DP is connected or disconnected
Dima Zavinfce7a472011-04-19 22:30:36 -07002115 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2116 return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Eric Laurentde070132010-07-13 04:45:46 -07002117 }
2118 for (size_t i = 0; i < mTracks.size(); i++) {
2119 sp<Track> track = mTracks[i];
2120 if (sessionId == track->sessionId() &&
2121 !(track->mCblk->flags & CBLK_INVALID_MSK)) {
Glenn Kasten02bbd202012-02-08 12:35:35 -08002122 return AudioSystem::getStrategyForStream(track->streamType());
Eric Laurentde070132010-07-13 04:45:46 -07002123 }
2124 }
Dima Zavinfce7a472011-04-19 22:30:36 -07002125 return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Eric Laurentde070132010-07-13 04:45:46 -07002126}
2127
Mathias Agopian65ab4712010-07-14 17:59:35 -07002128
Glenn Kastenaed850d2012-01-26 09:46:34 -08002129AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002130{
2131 Mutex::Autolock _l(mLock);
2132 return mOutput;
2133}
2134
2135AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput()
2136{
2137 Mutex::Autolock _l(mLock);
2138 AudioStreamOut *output = mOutput;
2139 mOutput = NULL;
Glenn Kasten58912562012-04-03 10:45:00 -07002140 // FIXME FastMixer might also have a raw ptr to mOutputSink;
2141 // must push a NULL and wait for ack
2142 mOutputSink.clear();
2143 mPipeSink.clear();
2144 mNormalSink.clear();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002145 return output;
2146}
2147
2148// this method must always be called either with ThreadBase mLock held or inside the thread loop
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08002149audio_stream_t* AudioFlinger::PlaybackThread::stream() const
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002150{
2151 if (mOutput == NULL) {
2152 return NULL;
2153 }
2154 return &mOutput->stream->common;
2155}
2156
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08002157uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const
Eric Laurent162b40b2011-12-05 09:47:19 -08002158{
Eric Laurentab9071b2012-06-04 13:45:29 -07002159 return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000);
Eric Laurent162b40b2011-12-05 09:47:19 -08002160}
2161
Eric Laurenta011e352012-03-29 15:51:43 -07002162status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event)
2163{
2164 if (!isValidSyncEvent(event)) {
2165 return BAD_VALUE;
2166 }
2167
2168 Mutex::Autolock _l(mLock);
2169
2170 for (size_t i = 0; i < mTracks.size(); ++i) {
2171 sp<Track> track = mTracks[i];
2172 if (event->triggerSession() == track->sessionId()) {
2173 track->setSyncEvent(event);
2174 return NO_ERROR;
2175 }
2176 }
2177
2178 return NAME_NOT_FOUND;
2179}
2180
2181bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event)
2182{
2183 switch (event->type()) {
2184 case AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE:
2185 return true;
2186 default:
2187 break;
2188 }
2189 return false;
2190}
2191
Eric Laurent44a957f2012-05-15 15:26:05 -07002192void AudioFlinger::PlaybackThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove)
2193{
2194 size_t count = tracksToRemove.size();
2195 if (CC_UNLIKELY(count)) {
2196 for (size_t i = 0 ; i < count ; i++) {
2197 const sp<Track>& track = tracksToRemove.itemAt(i);
2198 if ((track->sharedBuffer() != 0) &&
2199 (track->mState == TrackBase::ACTIVE || track->mState == TrackBase::RESUMING)) {
2200 AudioSystem::stopOutput(mId, track->streamType(), track->sessionId());
2201 }
2202 }
2203 }
2204
2205}
2206
Mathias Agopian65ab4712010-07-14 17:59:35 -07002207// ----------------------------------------------------------------------------
2208
Glenn Kasten23bb8be2012-01-26 10:38:26 -08002209AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002210 audio_io_handle_t id, audio_devices_t device, type_t type)
Glenn Kasten58912562012-04-03 10:45:00 -07002211 : PlaybackThread(audioFlinger, output, id, device, type),
2212 // mAudioMixer below
Glenn Kasten58912562012-04-03 10:45:00 -07002213 // mFastMixer below
2214 mFastMixerFutex(0)
2215 // mOutputSink below
2216 // mPipeSink below
2217 // mNormalSink below
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218{
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002219 ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type);
Glenn Kasten254af182012-07-03 14:59:05 -07002220 ALOGV("mSampleRate=%d, mChannelMask=%#x, mChannelCount=%d, mFormat=%d, mFrameSize=%d, "
Glenn Kasten58912562012-04-03 10:45:00 -07002221 "mFrameCount=%d, mNormalFrameCount=%d",
2222 mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount,
2223 mNormalFrameCount);
2224 mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
2225
Mathias Agopian65ab4712010-07-14 17:59:35 -07002226 // FIXME - Current mixer implementation only supports stereo output
Glenn Kasten4fe1ec42012-02-27 16:33:15 -08002227 if (mChannelCount != FCC_2) {
2228 ALOGE("Invalid audio hardware channel count %d", mChannelCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002229 }
Glenn Kasten58912562012-04-03 10:45:00 -07002230
2231 // create an NBAIO sink for the HAL output stream, and negotiate
2232 mOutputSink = new AudioStreamOutSink(output->stream);
2233 size_t numCounterOffers = 0;
2234 const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount)};
2235 ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers);
2236 ALOG_ASSERT(index == 0);
2237
Glenn Kasten300a2ee2012-04-25 13:47:36 -07002238 // initialize fast mixer depending on configuration
2239 bool initFastMixer;
2240 switch (kUseFastMixer) {
2241 case FastMixer_Never:
2242 initFastMixer = false;
2243 break;
2244 case FastMixer_Always:
2245 initFastMixer = true;
2246 break;
2247 case FastMixer_Static:
2248 case FastMixer_Dynamic:
2249 initFastMixer = mFrameCount < mNormalFrameCount;
2250 break;
2251 }
2252 if (initFastMixer) {
Glenn Kasten58912562012-04-03 10:45:00 -07002253
2254 // create a MonoPipe to connect our submix to FastMixer
2255 NBAIO_Format format = mOutputSink->format();
Glenn Kasten9017e5e2012-05-15 07:39:52 -07002256 // This pipe depth compensates for scheduling latency of the normal mixer thread.
2257 // When it wakes up after a maximum latency, it runs a few cycles quickly before
2258 // finally blocking. Note the pipe implementation rounds up the request to a power of 2.
2259 MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/);
Glenn Kasten58912562012-04-03 10:45:00 -07002260 const NBAIO_Format offers[1] = {format};
2261 size_t numCounterOffers = 0;
2262 ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers);
2263 ALOG_ASSERT(index == 0);
Glenn Kasten28ed2f92012-06-07 10:17:54 -07002264 monoPipe->setAvgFrames((mScreenState & 1) ?
2265 (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
Glenn Kasten58912562012-04-03 10:45:00 -07002266 mPipeSink = monoPipe;
2267
Glenn Kastenfbae5da2012-05-21 09:17:20 -07002268#ifdef TEE_SINK_FRAMES
2269 // create a Pipe to archive a copy of FastMixer's output for dumpsys
2270 Pipe *teeSink = new Pipe(TEE_SINK_FRAMES, format);
2271 numCounterOffers = 0;
2272 index = teeSink->negotiate(offers, 1, NULL, numCounterOffers);
2273 ALOG_ASSERT(index == 0);
2274 mTeeSink = teeSink;
2275 PipeReader *teeSource = new PipeReader(*teeSink);
2276 numCounterOffers = 0;
2277 index = teeSource->negotiate(offers, 1, NULL, numCounterOffers);
2278 ALOG_ASSERT(index == 0);
2279 mTeeSource = teeSource;
2280#endif
2281
Glenn Kasten58912562012-04-03 10:45:00 -07002282 // create fast mixer and configure it initially with just one fast track for our submix
2283 mFastMixer = new FastMixer();
2284 FastMixerStateQueue *sq = mFastMixer->sq();
Glenn Kasten39993082012-05-31 13:40:27 -07002285#ifdef STATE_QUEUE_DUMP
2286 sq->setObserverDump(&mStateQueueObserverDump);
2287 sq->setMutatorDump(&mStateQueueMutatorDump);
2288#endif
Glenn Kasten58912562012-04-03 10:45:00 -07002289 FastMixerState *state = sq->begin();
2290 FastTrack *fastTrack = &state->mFastTracks[0];
2291 // wrap the source side of the MonoPipe to make it an AudioBufferProvider
2292 fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe));
2293 fastTrack->mVolumeProvider = NULL;
2294 fastTrack->mGeneration++;
2295 state->mFastTracksGen++;
2296 state->mTrackMask = 1;
2297 // fast mixer will use the HAL output sink
2298 state->mOutputSink = mOutputSink.get();
2299 state->mOutputSinkGen++;
2300 state->mFrameCount = mFrameCount;
2301 state->mCommand = FastMixerState::COLD_IDLE;
2302 // already done in constructor initialization list
2303 //mFastMixerFutex = 0;
2304 state->mColdFutexAddr = &mFastMixerFutex;
2305 state->mColdGen++;
2306 state->mDumpState = &mFastMixerDumpState;
Glenn Kastenfbae5da2012-05-21 09:17:20 -07002307 state->mTeeSink = mTeeSink.get();
Glenn Kasten58912562012-04-03 10:45:00 -07002308 sq->end();
2309 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
2310
2311 // start the fast mixer
2312 mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO);
Glenn Kasten58912562012-04-03 10:45:00 -07002313 pid_t tid = mFastMixer->getTid();
Glenn Kastenfd4e20c2012-06-04 11:51:12 -07002314 int err = requestPriority(getpid_cached, tid, kPriorityFastMixer);
Glenn Kasten58912562012-04-03 10:45:00 -07002315 if (err != 0) {
2316 ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d",
Glenn Kastenfd4e20c2012-06-04 11:51:12 -07002317 kPriorityFastMixer, getpid_cached, tid, err);
Glenn Kasten58912562012-04-03 10:45:00 -07002318 }
Glenn Kasten58912562012-04-03 10:45:00 -07002319
Glenn Kastenc15d6652012-05-30 14:52:57 -07002320#ifdef AUDIO_WATCHDOG
2321 // create and start the watchdog
2322 mAudioWatchdog = new AudioWatchdog();
2323 mAudioWatchdog->setDump(&mAudioWatchdogDump);
2324 mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO);
2325 tid = mAudioWatchdog->getTid();
Glenn Kastenfd4e20c2012-06-04 11:51:12 -07002326 err = requestPriority(getpid_cached, tid, kPriorityFastMixer);
Glenn Kastenc15d6652012-05-30 14:52:57 -07002327 if (err != 0) {
2328 ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d",
Glenn Kastenfd4e20c2012-06-04 11:51:12 -07002329 kPriorityFastMixer, getpid_cached, tid, err);
Glenn Kastenc15d6652012-05-30 14:52:57 -07002330 }
2331#endif
2332
Glenn Kasten58912562012-04-03 10:45:00 -07002333 } else {
2334 mFastMixer = NULL;
2335 }
Glenn Kasten300a2ee2012-04-25 13:47:36 -07002336
2337 switch (kUseFastMixer) {
2338 case FastMixer_Never:
2339 case FastMixer_Dynamic:
2340 mNormalSink = mOutputSink;
2341 break;
2342 case FastMixer_Always:
2343 mNormalSink = mPipeSink;
2344 break;
2345 case FastMixer_Static:
2346 mNormalSink = initFastMixer ? mPipeSink : mOutputSink;
2347 break;
2348 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002349}
2350
2351AudioFlinger::MixerThread::~MixerThread()
2352{
Glenn Kasten58912562012-04-03 10:45:00 -07002353 if (mFastMixer != NULL) {
2354 FastMixerStateQueue *sq = mFastMixer->sq();
2355 FastMixerState *state = sq->begin();
2356 if (state->mCommand == FastMixerState::COLD_IDLE) {
2357 int32_t old = android_atomic_inc(&mFastMixerFutex);
2358 if (old == -1) {
2359 __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
2360 }
2361 }
2362 state->mCommand = FastMixerState::EXIT;
2363 sq->end();
2364 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
2365 mFastMixer->join();
2366 // Though the fast mixer thread has exited, it's state queue is still valid.
2367 // We'll use that extract the final state which contains one remaining fast track
2368 // corresponding to our sub-mix.
2369 state = sq->begin();
2370 ALOG_ASSERT(state->mTrackMask == 1);
2371 FastTrack *fastTrack = &state->mFastTracks[0];
2372 ALOG_ASSERT(fastTrack->mBufferProvider != NULL);
2373 delete fastTrack->mBufferProvider;
2374 sq->end(false /*didModify*/);
2375 delete mFastMixer;
Glenn Kastenc15d6652012-05-30 14:52:57 -07002376 if (mAudioWatchdog != 0) {
2377 mAudioWatchdog->requestExit();
2378 mAudioWatchdog->requestExitAndWait();
2379 mAudioWatchdog.clear();
2380 }
Glenn Kasten58912562012-04-03 10:45:00 -07002381 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002382 delete mAudioMixer;
2383}
2384
Glenn Kasten83efdd02012-02-24 07:21:32 -08002385class CpuStats {
2386public:
Glenn Kasten190a46f2012-03-06 11:27:10 -08002387 CpuStats();
2388 void sample(const String8 &title);
Glenn Kasten83efdd02012-02-24 07:21:32 -08002389#ifdef DEBUG_CPU_USAGE
2390private:
Glenn Kasten190a46f2012-03-06 11:27:10 -08002391 ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns
2392 CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns
2393
2394 CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles
2395
2396 int mCpuNum; // thread's current CPU number
2397 int mCpukHz; // frequency of thread's current CPU in kHz
Glenn Kasten83efdd02012-02-24 07:21:32 -08002398#endif
2399};
2400
Glenn Kasten190a46f2012-03-06 11:27:10 -08002401CpuStats::CpuStats()
Glenn Kasten83efdd02012-02-24 07:21:32 -08002402#ifdef DEBUG_CPU_USAGE
Glenn Kasten190a46f2012-03-06 11:27:10 -08002403 : mCpuNum(-1), mCpukHz(-1)
2404#endif
2405{
2406}
2407
2408void CpuStats::sample(const String8 &title) {
2409#ifdef DEBUG_CPU_USAGE
2410 // get current thread's delta CPU time in wall clock ns
2411 double wcNs;
2412 bool valid = mCpuUsage.sampleAndEnable(wcNs);
2413
2414 // record sample for wall clock statistics
2415 if (valid) {
2416 mWcStats.sample(wcNs);
2417 }
2418
2419 // get the current CPU number
2420 int cpuNum = sched_getcpu();
2421
2422 // get the current CPU frequency in kHz
2423 int cpukHz = mCpuUsage.getCpukHz(cpuNum);
2424
2425 // check if either CPU number or frequency changed
2426 if (cpuNum != mCpuNum || cpukHz != mCpukHz) {
2427 mCpuNum = cpuNum;
2428 mCpukHz = cpukHz;
2429 // ignore sample for purposes of cycles
2430 valid = false;
2431 }
2432
2433 // if no change in CPU number or frequency, then record sample for cycle statistics
2434 if (valid && mCpukHz > 0) {
2435 double cycles = wcNs * cpukHz * 0.000001;
2436 mHzStats.sample(cycles);
2437 }
2438
2439 unsigned n = mWcStats.n();
2440 // mCpuUsage.elapsed() is expensive, so don't call it every loop
Glenn Kasten83efdd02012-02-24 07:21:32 -08002441 if ((n & 127) == 1) {
Glenn Kasten190a46f2012-03-06 11:27:10 -08002442 long long elapsed = mCpuUsage.elapsed();
Glenn Kasten83efdd02012-02-24 07:21:32 -08002443 if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) {
2444 double perLoop = elapsed / (double) n;
2445 double perLoop100 = perLoop * 0.01;
Glenn Kasten190a46f2012-03-06 11:27:10 -08002446 double perLoop1k = perLoop * 0.001;
2447 double mean = mWcStats.mean();
2448 double stddev = mWcStats.stddev();
2449 double minimum = mWcStats.minimum();
2450 double maximum = mWcStats.maximum();
2451 double meanCycles = mHzStats.mean();
2452 double stddevCycles = mHzStats.stddev();
2453 double minCycles = mHzStats.minimum();
2454 double maxCycles = mHzStats.maximum();
2455 mCpuUsage.resetElapsed();
2456 mWcStats.reset();
2457 mHzStats.reset();
2458 ALOGD("CPU usage for %s over past %.1f secs\n"
2459 " (%u mixer loops at %.1f mean ms per loop):\n"
2460 " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n"
2461 " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n"
2462 " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f",
2463 title.string(),
Glenn Kasten83efdd02012-02-24 07:21:32 -08002464 elapsed * .000000001, n, perLoop * .000001,
2465 mean * .001,
2466 stddev * .001,
2467 minimum * .001,
2468 maximum * .001,
2469 mean / perLoop100,
2470 stddev / perLoop100,
2471 minimum / perLoop100,
Glenn Kasten190a46f2012-03-06 11:27:10 -08002472 maximum / perLoop100,
2473 meanCycles / perLoop1k,
2474 stddevCycles / perLoop1k,
2475 minCycles / perLoop1k,
2476 maxCycles / perLoop1k);
2477
Glenn Kasten83efdd02012-02-24 07:21:32 -08002478 }
2479 }
2480#endif
2481};
2482
Glenn Kasten37d825e2012-02-24 07:21:48 -08002483void AudioFlinger::PlaybackThread::checkSilentMode_l()
2484{
2485 if (!mMasterMute) {
2486 char value[PROPERTY_VALUE_MAX];
2487 if (property_get("ro.audio.silent", value, "0") > 0) {
2488 char *endptr;
2489 unsigned long ul = strtoul(value, &endptr, 0);
2490 if (*endptr == '\0' && ul != 0) {
2491 ALOGD("Silence is golden");
2492 // The setprop command will not allow a property to be changed after
2493 // the first time it is set, so we don't have to worry about un-muting.
2494 setMasterMute_l(true);
2495 }
2496 }
2497 }
2498}
2499
Glenn Kasten000f0e32012-03-01 17:10:56 -08002500bool AudioFlinger::PlaybackThread::threadLoop()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002501{
2502 Vector< sp<Track> > tracksToRemove;
Glenn Kasten688a6402012-02-29 07:57:06 -08002503
Glenn Kasten000f0e32012-03-01 17:10:56 -08002504 standbyTime = systemTime();
Glenn Kasten000f0e32012-03-01 17:10:56 -08002505
2506 // MIXER
Mathias Agopian65ab4712010-07-14 17:59:35 -07002507 nsecs_t lastWarning = 0;
Glenn Kasten688a6402012-02-29 07:57:06 -08002508
Glenn Kasten000f0e32012-03-01 17:10:56 -08002509 // DUPLICATING
2510 // FIXME could this be made local to while loop?
2511 writeFrames = 0;
Glenn Kasten688a6402012-02-29 07:57:06 -08002512
Glenn Kasten66fcab92012-02-24 14:59:21 -08002513 cacheParameters_l();
Glenn Kasten000f0e32012-03-01 17:10:56 -08002514 sleepTime = idleSleepTime;
2515
Glenn Kasten9f34a362012-03-20 16:46:41 -07002516 if (mType == MIXER) {
2517 sleepTimeShift = 0;
2518 }
Glenn Kasten000f0e32012-03-01 17:10:56 -08002519
Glenn Kasten83efdd02012-02-24 07:21:32 -08002520 CpuStats cpuStats;
Glenn Kasten190a46f2012-03-06 11:27:10 -08002521 const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
Mathias Agopian65ab4712010-07-14 17:59:35 -07002522
Eric Laurentfeb0db62011-07-22 09:04:31 -07002523 acquireWakeLock();
2524
Mathias Agopian65ab4712010-07-14 17:59:35 -07002525 while (!exitPending())
2526 {
Glenn Kasten190a46f2012-03-06 11:27:10 -08002527 cpuStats.sample(myName);
Glenn Kasten688a6402012-02-29 07:57:06 -08002528
Glenn Kasten73ca0f52012-02-29 07:56:15 -08002529 Vector< sp<EffectChain> > effectChains;
2530
Mathias Agopian65ab4712010-07-14 17:59:35 -07002531 processConfigEvents();
2532
Mathias Agopian65ab4712010-07-14 17:59:35 -07002533 { // scope for mLock
2534
2535 Mutex::Autolock _l(mLock);
2536
2537 if (checkForNewParameters_l()) {
Glenn Kasten66fcab92012-02-24 14:59:21 -08002538 cacheParameters_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002539 }
2540
Glenn Kastenfa26a852012-03-06 11:28:04 -08002541 saveOutputTracks();
Glenn Kasten000f0e32012-03-01 17:10:56 -08002542
Mathias Agopian65ab4712010-07-14 17:59:35 -07002543 // put audio hardware into standby after short delay
Glenn Kasten3e074702012-02-28 18:40:35 -08002544 if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) ||
Glenn Kasten1ea6d232012-07-09 14:31:33 -07002545 isSuspended())) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002546 if (!mStandby) {
Glenn Kasten000f0e32012-03-01 17:10:56 -08002547
2548 threadLoop_standby();
2549
Mathias Agopian65ab4712010-07-14 17:59:35 -07002550 mStandby = true;
2551 mBytesWritten = 0;
2552 }
2553
Glenn Kasten3e074702012-02-28 18:40:35 -08002554 if (!mActiveTracks.size() && mConfigEvents.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002555 // we're about to wait, flush the binder command buffer
2556 IPCThreadState::self()->flushCommands();
2557
Glenn Kastenfa26a852012-03-06 11:28:04 -08002558 clearOutputTracks();
Glenn Kasten000f0e32012-03-01 17:10:56 -08002559
Mathias Agopian65ab4712010-07-14 17:59:35 -07002560 if (exitPending()) break;
2561
Eric Laurentfeb0db62011-07-22 09:04:31 -07002562 releaseWakeLock_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002563 // wait until we have something to do...
Glenn Kasten190a46f2012-03-06 11:27:10 -08002564 ALOGV("%s going to sleep", myName.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002565 mWaitWorkCV.wait(mLock);
Glenn Kasten190a46f2012-03-06 11:27:10 -08002566 ALOGV("%s waking up", myName.string());
Eric Laurentfeb0db62011-07-22 09:04:31 -07002567 acquireWakeLock_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002568
Eric Laurentda747442012-04-25 18:53:13 -07002569 mMixerStatus = MIXER_IDLE;
Glenn Kasten81028042012-04-30 18:15:12 -07002570 mMixerStatusIgnoringFastTracks = MIXER_IDLE;
Glenn Kasten000f0e32012-03-01 17:10:56 -08002571
Glenn Kasten37d825e2012-02-24 07:21:48 -08002572 checkSilentMode_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002573
Glenn Kasten000f0e32012-03-01 17:10:56 -08002574 standbyTime = systemTime() + standbyDelay;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002575 sleepTime = idleSleepTime;
Glenn Kasten66fcab92012-02-24 14:59:21 -08002576 if (mType == MIXER) {
2577 sleepTimeShift = 0;
2578 }
Glenn Kasten000f0e32012-03-01 17:10:56 -08002579
Mathias Agopian65ab4712010-07-14 17:59:35 -07002580 continue;
2581 }
2582 }
2583
Glenn Kasten81028042012-04-30 18:15:12 -07002584 // mMixerStatusIgnoringFastTracks is also updated internally
Eric Laurentda747442012-04-25 18:53:13 -07002585 mMixerStatus = prepareTracks_l(&tracksToRemove);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002586
2587 // prevent any changes in effect chain list and in each effect chain
2588 // during mixing and effect process as the audio buffers could be deleted
2589 // or modified if an effect is created or deleted
Eric Laurentde070132010-07-13 04:45:46 -07002590 lockEffectChains_l(effectChains);
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08002591 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002592
Glenn Kastenfec279f2012-03-08 07:47:15 -08002593 if (CC_LIKELY(mMixerStatus == MIXER_TRACKS_READY)) {
Glenn Kasten000f0e32012-03-01 17:10:56 -08002594 threadLoop_mix();
2595 } else {
2596 threadLoop_sleepTime();
2597 }
2598
Glenn Kasten1ea6d232012-07-09 14:31:33 -07002599 if (isSuspended()) {
Glenn Kasten000f0e32012-03-01 17:10:56 -08002600 sleepTime = suspendSleepTimeUs();
2601 }
2602
2603 // only process effects if we're going to write
2604 if (sleepTime == 0) {
Glenn Kasten000f0e32012-03-01 17:10:56 -08002605 for (size_t i = 0; i < effectChains.size(); i ++) {
2606 effectChains[i]->process_l();
2607 }
2608 }
2609
2610 // enable changes in effect chain
2611 unlockEffectChains(effectChains);
2612
2613 // sleepTime == 0 means we must write to audio hardware
2614 if (sleepTime == 0) {
2615
2616 threadLoop_write();
2617
2618if (mType == MIXER) {
2619 // write blocked detection
2620 nsecs_t now = systemTime();
2621 nsecs_t delta = now - mLastWriteTime;
2622 if (!mStandby && delta > maxPeriod) {
2623 mNumDelayedWrites++;
2624 if ((now - lastWarning) > kWarningThrottleNs) {
Glenn Kasten99c99d02012-05-14 16:37:13 -07002625#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER)
Glenn Kastend8e6fd32012-05-07 11:07:57 -07002626 ScopedTrace st(ATRACE_TAG, "underrun");
Glenn Kasten99c99d02012-05-14 16:37:13 -07002627#endif
Glenn Kasten000f0e32012-03-01 17:10:56 -08002628 ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p",
2629 ns2ms(delta), mNumDelayedWrites, this);
2630 lastWarning = now;
2631 }
Glenn Kasten000f0e32012-03-01 17:10:56 -08002632 }
2633}
2634
2635 mStandby = false;
2636 } else {
2637 usleep(sleepTime);
2638 }
2639
Glenn Kasten58912562012-04-03 10:45:00 -07002640 // Finally let go of removed track(s), without the lock held
Glenn Kasten000f0e32012-03-01 17:10:56 -08002641 // since we can't guarantee the destructors won't acquire that
Glenn Kasten58912562012-04-03 10:45:00 -07002642 // same lock. This will also mutate and push a new fast mixer state.
2643 threadLoop_removeTracks(tracksToRemove);
Glenn Kasten1465f0c2012-03-06 11:23:32 -08002644 tracksToRemove.clear();
Glenn Kasten000f0e32012-03-01 17:10:56 -08002645
Glenn Kastenfa26a852012-03-06 11:28:04 -08002646 // FIXME I don't understand the need for this here;
2647 // it was in the original code but maybe the
2648 // assignment in saveOutputTracks() makes this unnecessary?
2649 clearOutputTracks();
Glenn Kasten000f0e32012-03-01 17:10:56 -08002650
2651 // Effect chains will be actually deleted here if they were removed from
2652 // mEffectChains list during mixing or effects processing
2653 effectChains.clear();
2654
2655 // FIXME Note that the above .clear() is no longer necessary since effectChains
2656 // is now local to this block, but will keep it for now (at least until merge done).
2657 }
2658
Glenn Kasten9f34a362012-03-20 16:46:41 -07002659 // for DuplicatingThread, standby mode is handled by the outputTracks, otherwise ...
2660 if (mType == MIXER || mType == DIRECT) {
2661 // put output stream into standby mode
2662 if (!mStandby) {
2663 mOutput->stream->common.standby(&mOutput->stream->common);
2664 }
Glenn Kasten000f0e32012-03-01 17:10:56 -08002665 }
Glenn Kasten000f0e32012-03-01 17:10:56 -08002666
2667 releaseWakeLock();
2668
2669 ALOGV("Thread %p type %d exiting", this, mType);
2670 return false;
2671}
2672
Glenn Kasten58912562012-04-03 10:45:00 -07002673void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove)
2674{
Glenn Kasten58912562012-04-03 10:45:00 -07002675 PlaybackThread::threadLoop_removeTracks(tracksToRemove);
2676}
2677
2678void AudioFlinger::MixerThread::threadLoop_write()
2679{
2680 // FIXME we should only do one push per cycle; confirm this is true
2681 // Start the fast mixer if it's not already running
2682 if (mFastMixer != NULL) {
2683 FastMixerStateQueue *sq = mFastMixer->sq();
2684 FastMixerState *state = sq->begin();
Glenn Kasten300a2ee2012-04-25 13:47:36 -07002685 if (state->mCommand != FastMixerState::MIX_WRITE &&
2686 (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) {
Glenn Kasten58912562012-04-03 10:45:00 -07002687 if (state->mCommand == FastMixerState::COLD_IDLE) {
2688 int32_t old = android_atomic_inc(&mFastMixerFutex);
2689 if (old == -1) {
2690 __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
2691 }
Glenn Kastenc15d6652012-05-30 14:52:57 -07002692 if (mAudioWatchdog != 0) {
2693 mAudioWatchdog->resume();
2694 }
Glenn Kasten58912562012-04-03 10:45:00 -07002695 }
2696 state->mCommand = FastMixerState::MIX_WRITE;
2697 sq->end();
2698 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
Glenn Kasten300a2ee2012-04-25 13:47:36 -07002699 if (kUseFastMixer == FastMixer_Dynamic) {
2700 mNormalSink = mPipeSink;
2701 }
Glenn Kasten58912562012-04-03 10:45:00 -07002702 } else {
2703 sq->end(false /*didModify*/);
2704 }
2705 }
2706 PlaybackThread::threadLoop_write();
2707}
2708
Glenn Kasten000f0e32012-03-01 17:10:56 -08002709// shared by MIXER and DIRECT, overridden by DUPLICATING
2710void AudioFlinger::PlaybackThread::threadLoop_write()
2711{
Glenn Kasten952eeb22012-03-06 11:30:57 -08002712 // FIXME rewrite to reduce number of system calls
2713 mLastWriteTime = systemTime();
2714 mInWrite = true;
Eric Laurent67c0a582012-05-01 19:31:12 -07002715 int bytesWritten;
Glenn Kasten58912562012-04-03 10:45:00 -07002716
Eric Laurent67c0a582012-05-01 19:31:12 -07002717 // If an NBAIO sink is present, use it to write the normal mixer's submix
2718 if (mNormalSink != 0) {
Glenn Kasten58912562012-04-03 10:45:00 -07002719#define mBitShift 2 // FIXME
Eric Laurent67c0a582012-05-01 19:31:12 -07002720 size_t count = mixBufferSize >> mBitShift;
Glenn Kasten99c99d02012-05-14 16:37:13 -07002721#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER)
Eric Laurent67c0a582012-05-01 19:31:12 -07002722 Tracer::traceBegin(ATRACE_TAG, "write");
Glenn Kasten99c99d02012-05-14 16:37:13 -07002723#endif
Glenn Kasten28ed2f92012-06-07 10:17:54 -07002724 // update the setpoint when gScreenState changes
2725 uint32_t screenState = gScreenState;
2726 if (screenState != mScreenState) {
2727 mScreenState = screenState;
2728 MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
2729 if (pipe != NULL) {
2730 pipe->setAvgFrames((mScreenState & 1) ?
2731 (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
2732 }
2733 }
Eric Laurent67c0a582012-05-01 19:31:12 -07002734 ssize_t framesWritten = mNormalSink->write(mMixBuffer, count);
Glenn Kasten99c99d02012-05-14 16:37:13 -07002735#if defined(ATRACE_TAG) && (ATRACE_TAG != ATRACE_TAG_NEVER)
Eric Laurent67c0a582012-05-01 19:31:12 -07002736 Tracer::traceEnd(ATRACE_TAG);
Glenn Kasten99c99d02012-05-14 16:37:13 -07002737#endif
Eric Laurent67c0a582012-05-01 19:31:12 -07002738 if (framesWritten > 0) {
2739 bytesWritten = framesWritten << mBitShift;
2740 } else {
2741 bytesWritten = framesWritten;
2742 }
2743 // otherwise use the HAL / AudioStreamOut directly
2744 } else {
2745 // Direct output thread.
2746 bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize);
Glenn Kasten58912562012-04-03 10:45:00 -07002747 }
2748
Eric Laurent67c0a582012-05-01 19:31:12 -07002749 if (bytesWritten > 0) mBytesWritten += mixBufferSize;
Glenn Kasten952eeb22012-03-06 11:30:57 -08002750 mNumWrites++;
2751 mInWrite = false;
Glenn Kasten000f0e32012-03-01 17:10:56 -08002752}
2753
Glenn Kasten58912562012-04-03 10:45:00 -07002754void AudioFlinger::MixerThread::threadLoop_standby()
2755{
2756 // Idle the fast mixer if it's currently running
2757 if (mFastMixer != NULL) {
2758 FastMixerStateQueue *sq = mFastMixer->sq();
2759 FastMixerState *state = sq->begin();
2760 if (!(state->mCommand & FastMixerState::IDLE)) {
2761 state->mCommand = FastMixerState::COLD_IDLE;
2762 state->mColdFutexAddr = &mFastMixerFutex;
2763 state->mColdGen++;
2764 mFastMixerFutex = 0;
2765 sq->end();
2766 // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
2767 sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED);
Glenn Kasten300a2ee2012-04-25 13:47:36 -07002768 if (kUseFastMixer == FastMixer_Dynamic) {
2769 mNormalSink = mOutputSink;
2770 }
Glenn Kastenc15d6652012-05-30 14:52:57 -07002771 if (mAudioWatchdog != 0) {
2772 mAudioWatchdog->pause();
2773 }
Glenn Kasten58912562012-04-03 10:45:00 -07002774 } else {
2775 sq->end(false /*didModify*/);
2776 }
2777 }
2778 PlaybackThread::threadLoop_standby();
2779}
2780
Glenn Kasten000f0e32012-03-01 17:10:56 -08002781// shared by MIXER and DIRECT, overridden by DUPLICATING
2782void AudioFlinger::PlaybackThread::threadLoop_standby()
2783{
Glenn Kasten1ea6d232012-07-09 14:31:33 -07002784 ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended);
Glenn Kasten952eeb22012-03-06 11:30:57 -08002785 mOutput->stream->common.standby(&mOutput->stream->common);
Glenn Kasten000f0e32012-03-01 17:10:56 -08002786}
2787
2788void AudioFlinger::MixerThread::threadLoop_mix()
2789{
Glenn Kasten952eeb22012-03-06 11:30:57 -08002790 // obtain the presentation timestamp of the next output buffer
2791 int64_t pts;
2792 status_t status = INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08002793
John Grossman2c3b2da2012-08-02 17:08:54 -07002794 if (mNormalSink != 0) {
2795 status = mNormalSink->getNextWriteTimestamp(&pts);
2796 } else {
2797 status = mOutputSink->getNextWriteTimestamp(&pts);
Glenn Kasten952eeb22012-03-06 11:30:57 -08002798 }
John Grossman4ff14ba2012-02-08 16:37:41 -08002799
Glenn Kasten952eeb22012-03-06 11:30:57 -08002800 if (status != NO_ERROR) {
2801 pts = AudioBufferProvider::kInvalidPTS;
2802 }
John Grossman4ff14ba2012-02-08 16:37:41 -08002803
Glenn Kasten952eeb22012-03-06 11:30:57 -08002804 // mix buffers...
2805 mAudioMixer->process(pts);
2806 // increase sleep time progressively when application underrun condition clears.
2807 // Only increase sleep time if the mixer is ready for two consecutive times to avoid
2808 // that a steady state of alternating ready/not ready conditions keeps the sleep time
2809 // such that we would underrun the audio HAL.
2810 if ((sleepTime == 0) && (sleepTimeShift > 0)) {
2811 sleepTimeShift--;
2812 }
2813 sleepTime = 0;
Glenn Kasten66fcab92012-02-24 14:59:21 -08002814 standbyTime = systemTime() + standbyDelay;
Glenn Kasten952eeb22012-03-06 11:30:57 -08002815 //TODO: delay standby when effects have a tail
Glenn Kasten000f0e32012-03-01 17:10:56 -08002816}
2817
2818void AudioFlinger::MixerThread::threadLoop_sleepTime()
2819{
Glenn Kasten952eeb22012-03-06 11:30:57 -08002820 // If no tracks are ready, sleep once for the duration of an output
2821 // buffer size, then write 0s to the output
2822 if (sleepTime == 0) {
Glenn Kastenfec279f2012-03-08 07:47:15 -08002823 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
Glenn Kasten952eeb22012-03-06 11:30:57 -08002824 sleepTime = activeSleepTime >> sleepTimeShift;
2825 if (sleepTime < kMinThreadSleepTimeUs) {
2826 sleepTime = kMinThreadSleepTimeUs;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002827 }
Glenn Kasten952eeb22012-03-06 11:30:57 -08002828 // reduce sleep time in case of consecutive application underruns to avoid
2829 // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer
2830 // duration we would end up writing less data than needed by the audio HAL if
2831 // the condition persists.
2832 if (sleepTimeShift < kMaxThreadSleepTimeShift) {
2833 sleepTimeShift++;
2834 }
2835 } else {
2836 sleepTime = idleSleepTime;
2837 }
Glenn Kastenf1da96d2012-07-02 16:10:16 -07002838 } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) {
Glenn Kasten952eeb22012-03-06 11:30:57 -08002839 memset (mMixBuffer, 0, mixBufferSize);
2840 sleepTime = 0;
Glenn Kastenf1da96d2012-07-02 16:10:16 -07002841 ALOGV_IF((mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED)), "anticipated start");
Glenn Kasten952eeb22012-03-06 11:30:57 -08002842 }
2843 // TODO add standby time extension fct of effect tail
Mathias Agopian65ab4712010-07-14 17:59:35 -07002844}
2845
2846// prepareTracks_l() must be called with ThreadBase::mLock held
Glenn Kasten29c23c32012-01-26 13:37:52 -08002847AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l(
Glenn Kasten3e074702012-02-28 18:40:35 -08002848 Vector< sp<Track> > *tracksToRemove)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002849{
2850
Glenn Kasten29c23c32012-01-26 13:37:52 -08002851 mixer_state mixerStatus = MIXER_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002852 // find out which tracks need to be processed
Glenn Kasten3e074702012-02-28 18:40:35 -08002853 size_t count = mActiveTracks.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002854 size_t mixedTracks = 0;
2855 size_t tracksWithEffect = 0;
Glenn Kasten288ed212012-04-25 17:52:27 -07002856 // counts only _active_ fast tracks
Glenn Kasten58912562012-04-03 10:45:00 -07002857 size_t fastTracks = 0;
Glenn Kasten288ed212012-04-25 17:52:27 -07002858 uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset
Mathias Agopian65ab4712010-07-14 17:59:35 -07002859
2860 float masterVolume = mMasterVolume;
Glenn Kastenea7939a2012-03-14 12:56:26 -07002861 bool masterMute = mMasterMute;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002862
Eric Laurent571d49c2010-08-11 05:20:11 -07002863 if (masterMute) {
2864 masterVolume = 0;
2865 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002866 // Delegate master volume control to effect in output mix effect chain if needed
Dima Zavinfce7a472011-04-19 22:30:36 -07002867 sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002868 if (chain != 0) {
Eric Laurent571d49c2010-08-11 05:20:11 -07002869 uint32_t v = (uint32_t)(masterVolume * (1 << 24));
Eric Laurentcab11242010-07-15 12:50:15 -07002870 chain->setVolume_l(&v, &v);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002871 masterVolume = (float)((v + (1 << 23)) >> 24);
2872 chain.clear();
2873 }
2874
Glenn Kasten288ed212012-04-25 17:52:27 -07002875 // prepare a new state to push
2876 FastMixerStateQueue *sq = NULL;
2877 FastMixerState *state = NULL;
2878 bool didModify = false;
2879 FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED;
2880 if (mFastMixer != NULL) {
2881 sq = mFastMixer->sq();
2882 state = sq->begin();
2883 }
2884
Mathias Agopian65ab4712010-07-14 17:59:35 -07002885 for (size_t i=0 ; i<count ; i++) {
Glenn Kasten3e074702012-02-28 18:40:35 -08002886 sp<Track> t = mActiveTracks[i].promote();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002887 if (t == 0) continue;
2888
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08002889 // this const just means the local variable doesn't change
Mathias Agopian65ab4712010-07-14 17:59:35 -07002890 Track* const track = t.get();
Glenn Kasten58912562012-04-03 10:45:00 -07002891
Glenn Kasten288ed212012-04-25 17:52:27 -07002892 // process fast tracks
Glenn Kasten58912562012-04-03 10:45:00 -07002893 if (track->isFastTrack()) {
Glenn Kasten288ed212012-04-25 17:52:27 -07002894
2895 // It's theoretically possible (though unlikely) for a fast track to be created
2896 // and then removed within the same normal mix cycle. This is not a problem, as
2897 // the track never becomes active so it's fast mixer slot is never touched.
2898 // The converse, of removing an (active) track and then creating a new track
2899 // at the identical fast mixer slot within the same normal mix cycle,
2900 // is impossible because the slot isn't marked available until the end of each cycle.
2901 int j = track->mFastIndex;
Glenn Kasten893a0542012-05-30 10:32:06 -07002902 ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks);
2903 ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j)));
Glenn Kasten288ed212012-04-25 17:52:27 -07002904 FastTrack *fastTrack = &state->mFastTracks[j];
2905
2906 // Determine whether the track is currently in underrun condition,
2907 // and whether it had a recent underrun.
Glenn Kasten1295bb4d2012-05-31 07:43:43 -07002908 FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j];
2909 FastTrackUnderruns underruns = ftDump->mUnderruns;
Glenn Kasten09474df2012-05-10 14:48:07 -07002910 uint32_t recentFull = (underruns.mBitFields.mFull -
2911 track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK;
2912 uint32_t recentPartial = (underruns.mBitFields.mPartial -
2913 track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK;
2914 uint32_t recentEmpty = (underruns.mBitFields.mEmpty -
2915 track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK;
2916 uint32_t recentUnderruns = recentPartial + recentEmpty;
2917 track->mObservedUnderruns = underruns;
Glenn Kasten288ed212012-04-25 17:52:27 -07002918 // don't count underruns that occur while stopping or pausing
Glenn Kastend08f48c2012-05-01 18:14:02 -07002919 // or stopped which can occur when flush() is called while active
2920 if (!(track->isStopping() || track->isPausing() || track->isStopped())) {
Glenn Kasten288ed212012-04-25 17:52:27 -07002921 track->mUnderrunCount += recentUnderruns;
2922 }
Glenn Kasten288ed212012-04-25 17:52:27 -07002923
Glenn Kastend08f48c2012-05-01 18:14:02 -07002924 // This is similar to the state machine for normal tracks,
Glenn Kasten288ed212012-04-25 17:52:27 -07002925 // with a few modifications for fast tracks.
Glenn Kastend08f48c2012-05-01 18:14:02 -07002926 bool isActive = true;
2927 switch (track->mState) {
2928 case TrackBase::STOPPING_1:
2929 // track stays active in STOPPING_1 state until first underrun
2930 if (recentUnderruns > 0) {
2931 track->mState = TrackBase::STOPPING_2;
2932 }
2933 break;
2934 case TrackBase::PAUSING:
Glenn Kasten288ed212012-04-25 17:52:27 -07002935 // ramp down is not yet implemented
Glenn Kasten288ed212012-04-25 17:52:27 -07002936 track->setPaused();
Glenn Kastend08f48c2012-05-01 18:14:02 -07002937 break;
2938 case TrackBase::RESUMING:
Glenn Kasten288ed212012-04-25 17:52:27 -07002939 // ramp up is not yet implemented
Glenn Kasten288ed212012-04-25 17:52:27 -07002940 track->mState = TrackBase::ACTIVE;
Glenn Kastend08f48c2012-05-01 18:14:02 -07002941 break;
2942 case TrackBase::ACTIVE:
Glenn Kasten09474df2012-05-10 14:48:07 -07002943 if (recentFull > 0 || recentPartial > 0) {
2944 // track has provided at least some frames recently: reset retry count
2945 track->mRetryCount = kMaxTrackRetries;
2946 }
2947 if (recentUnderruns == 0) {
2948 // no recent underruns: stay active
2949 break;
2950 }
2951 // there has recently been an underrun of some kind
2952 if (track->sharedBuffer() == 0) {
2953 // were any of the recent underruns "empty" (no frames available)?
2954 if (recentEmpty == 0) {
2955 // no, then ignore the partial underruns as they are allowed indefinitely
2956 break;
2957 }
2958 // there has recently been an "empty" underrun: decrement the retry counter
2959 if (--(track->mRetryCount) > 0) {
2960 break;
2961 }
2962 // indicate to client process that the track was disabled because of underrun;
2963 // it will then automatically call start() when data is available
2964 android_atomic_or(CBLK_DISABLED_ON, &track->mCblk->flags);
2965 // remove from active list, but state remains ACTIVE [confusing but true]
2966 isActive = false;
Glenn Kastend08f48c2012-05-01 18:14:02 -07002967 break;
2968 }
2969 // fall through
2970 case TrackBase::STOPPING_2:
2971 case TrackBase::PAUSED:
2972 case TrackBase::TERMINATED:
Eric Laurent29864602012-05-08 18:57:51 -07002973 case TrackBase::STOPPED:
2974 case TrackBase::FLUSHED: // flush() while active
Glenn Kastend08f48c2012-05-01 18:14:02 -07002975 // Check for presentation complete if track is inactive
2976 // We have consumed all the buffers of this track.
2977 // This would be incomplete if we auto-paused on underrun
2978 {
2979 size_t audioHALFrames =
2980 (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000;
2981 size_t framesWritten =
2982 mBytesWritten / audio_stream_frame_size(&mOutput->stream->common);
2983 if (!track->presentationComplete(framesWritten, audioHALFrames)) {
2984 // track stays in active list until presentation is complete
2985 break;
2986 }
2987 }
2988 if (track->isStopping_2()) {
2989 track->mState = TrackBase::STOPPED;
2990 }
2991 if (track->isStopped()) {
2992 // Can't reset directly, as fast mixer is still polling this track
2993 // track->reset();
2994 // So instead mark this track as needing to be reset after push with ack
2995 resetMask |= 1 << i;
2996 }
2997 isActive = false;
2998 break;
2999 case TrackBase::IDLE:
3000 default:
3001 LOG_FATAL("unexpected track state %d", track->mState);
Glenn Kasten288ed212012-04-25 17:52:27 -07003002 }
3003
3004 if (isActive) {
3005 // was it previously inactive?
3006 if (!(state->mTrackMask & (1 << j))) {
3007 ExtendedAudioBufferProvider *eabp = track;
3008 VolumeProvider *vp = track;
3009 fastTrack->mBufferProvider = eabp;
3010 fastTrack->mVolumeProvider = vp;
3011 fastTrack->mSampleRate = track->mSampleRate;
3012 fastTrack->mChannelMask = track->mChannelMask;
3013 fastTrack->mGeneration++;
3014 state->mTrackMask |= 1 << j;
3015 didModify = true;
3016 // no acknowledgement required for newly active tracks
3017 }
3018 // cache the combined master volume and stream type volume for fast mixer; this
3019 // lacks any synchronization or barrier so VolumeProvider may read a stale value
3020 track->mCachedVolume = track->isMuted() ?
3021 0 : masterVolume * mStreamTypes[track->streamType()].volume;
3022 ++fastTracks;
3023 } else {
3024 // was it previously active?
3025 if (state->mTrackMask & (1 << j)) {
3026 fastTrack->mBufferProvider = NULL;
3027 fastTrack->mGeneration++;
3028 state->mTrackMask &= ~(1 << j);
3029 didModify = true;
3030 // If any fast tracks were removed, we must wait for acknowledgement
3031 // because we're about to decrement the last sp<> on those tracks.
3032 block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
Glenn Kastend08f48c2012-05-01 18:14:02 -07003033 } else {
3034 LOG_FATAL("fast track %d should have been active", j);
Glenn Kasten288ed212012-04-25 17:52:27 -07003035 }
Glenn Kastend08f48c2012-05-01 18:14:02 -07003036 tracksToRemove->add(track);
Glenn Kasten288ed212012-04-25 17:52:27 -07003037 // Avoids a misleading display in dumpsys
Glenn Kasten09474df2012-05-10 14:48:07 -07003038 track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL;
Glenn Kasten58912562012-04-03 10:45:00 -07003039 }
3040 continue;
3041 }
3042
3043 { // local variable scope to avoid goto warning
3044
Mathias Agopian65ab4712010-07-14 17:59:35 -07003045 audio_track_cblk_t* cblk = track->cblk();
3046
3047 // The first time a track is added we wait
3048 // for all its buffers to be filled before processing it
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08003049 int name = track->name();
Eric Laurenta47b69c2011-11-08 18:10:16 -08003050 // make sure that we have enough frames to mix one full buffer.
3051 // enforce this condition only once to enable draining the buffer in case the client
3052 // app does not call stop() and relies on underrun to stop:
Eric Laurentda747442012-04-25 18:53:13 -07003053 // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed
Eric Laurenta47b69c2011-11-08 18:10:16 -08003054 // during last round
Eric Laurent3dbe3202011-11-03 12:16:05 -07003055 uint32_t minFrames = 1;
Eric Laurent83faee02012-04-27 18:24:29 -07003056 if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() &&
Glenn Kasten81028042012-04-30 18:15:12 -07003057 (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) {
Eric Laurent3dbe3202011-11-03 12:16:05 -07003058 if (t->sampleRate() == (int)mSampleRate) {
Glenn Kasten58912562012-04-03 10:45:00 -07003059 minFrames = mNormalFrameCount;
Eric Laurent3dbe3202011-11-03 12:16:05 -07003060 } else {
Eric Laurent071ccd52011-12-22 16:08:41 -08003061 // +1 for rounding and +1 for additional sample needed for interpolation
Glenn Kasten58912562012-04-03 10:45:00 -07003062 minFrames = (mNormalFrameCount * t->sampleRate()) / mSampleRate + 1 + 1;
Eric Laurent071ccd52011-12-22 16:08:41 -08003063 // add frames already consumed but not yet released by the resampler
Glenn Kastenea7939a2012-03-14 12:56:26 -07003064 // because cblk->framesReady() will include these frames
Eric Laurent071ccd52011-12-22 16:08:41 -08003065 minFrames += mAudioMixer->getUnreleasedFrames(track->name());
3066 // the minimum track buffer size is normally twice the number of frames necessary
3067 // to fill one buffer and the resampler should not leave more than one buffer worth
3068 // of unreleased frames after each pass, but just in case...
Steve Blockc1dc1cb2012-01-09 18:35:44 +00003069 ALOG_ASSERT(minFrames <= cblk->frameCount);
Eric Laurent3dbe3202011-11-03 12:16:05 -07003070 }
3071 }
John Grossman4ff14ba2012-02-08 16:37:41 -08003072 if ((track->framesReady() >= minFrames) && track->isReady() &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07003073 !track->isPaused() && !track->isTerminated())
3074 {
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08003075 //ALOGV("track %d u=%08x, s=%08x [OK] on thread %p", name, cblk->user, cblk->server, this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003076
3077 mixedTracks++;
3078
3079 // track->mainBuffer() != mMixBuffer means there is an effect chain
3080 // connected to the track
3081 chain.clear();
3082 if (track->mainBuffer() != mMixBuffer) {
3083 chain = getEffectChain_l(track->sessionId());
3084 // Delegate volume control to effect in track effect chain if needed
3085 if (chain != 0) {
3086 tracksWithEffect++;
3087 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00003088 ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on session %d",
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08003089 name, track->sessionId());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003090 }
3091 }
3092
3093
3094 int param = AudioMixer::VOLUME;
3095 if (track->mFillingUpStatus == Track::FS_FILLED) {
3096 // no ramp for the first volume setting
3097 track->mFillingUpStatus = Track::FS_ACTIVE;
3098 if (track->mState == TrackBase::RESUMING) {
3099 track->mState = TrackBase::ACTIVE;
3100 param = AudioMixer::RAMP_VOLUME;
3101 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08003102 mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003103 } else if (cblk->server != 0) {
3104 // If the track is stopped before the first frame was mixed,
3105 // do not apply ramp
3106 param = AudioMixer::RAMP_VOLUME;
3107 }
3108
3109 // compute volume for this track
Eric Laurente0aed6d2010-09-10 17:44:44 -07003110 uint32_t vl, vr, va;
Eric Laurent8569f0d2010-07-29 23:43:43 -07003111 if (track->isMuted() || track->isPausing() ||
Glenn Kasten02bbd202012-02-08 12:35:35 -08003112 mStreamTypes[track->streamType()].mute) {
Eric Laurente0aed6d2010-09-10 17:44:44 -07003113 vl = vr = va = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003114 if (track->isPausing()) {
3115 track->setPaused();
3116 }
3117 } else {
Eric Laurente0aed6d2010-09-10 17:44:44 -07003118
Mathias Agopian65ab4712010-07-14 17:59:35 -07003119 // read original volumes with volume control
Glenn Kasten02bbd202012-02-08 12:35:35 -08003120 float typeVolume = mStreamTypes[track->streamType()].volume;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003121 float v = masterVolume * typeVolume;
Glenn Kasten83d86532012-01-17 14:39:34 -08003122 uint32_t vlr = cblk->getVolumeLR();
Glenn Kastenb1cf75c2012-01-17 12:20:54 -08003123 vl = vlr & 0xFFFF;
3124 vr = vlr >> 16;
3125 // track volumes come from shared memory, so can't be trusted and must be clamped
3126 if (vl > MAX_GAIN_INT) {
3127 ALOGV("Track left volume out of range: %04X", vl);
3128 vl = MAX_GAIN_INT;
3129 }
3130 if (vr > MAX_GAIN_INT) {
3131 ALOGV("Track right volume out of range: %04X", vr);
3132 vr = MAX_GAIN_INT;
3133 }
3134 // now apply the master volume and stream type volume
3135 vl = (uint32_t)(v * vl) << 12;
3136 vr = (uint32_t)(v * vr) << 12;
3137 // assuming master volume and stream type volume each go up to 1.0,
3138 // vl and vr are now in 8.24 format
Mathias Agopian65ab4712010-07-14 17:59:35 -07003139
Glenn Kasten05632a52012-01-03 14:22:33 -08003140 uint16_t sendLevel = cblk->getSendLevel_U4_12();
3141 // send level comes from shared memory and so may be corrupt
Glenn Kasten3b81aca2012-01-27 15:26:23 -08003142 if (sendLevel > MAX_GAIN_INT) {
Glenn Kasten05632a52012-01-03 14:22:33 -08003143 ALOGV("Track send level out of range: %04X", sendLevel);
Glenn Kastenb1cf75c2012-01-17 12:20:54 -08003144 sendLevel = MAX_GAIN_INT;
Glenn Kasten05632a52012-01-03 14:22:33 -08003145 }
3146 va = (uint32_t)(v * sendLevel);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003147 }
Eric Laurente0aed6d2010-09-10 17:44:44 -07003148 // Delegate volume control to effect in track effect chain if needed
3149 if (chain != 0 && chain->setVolume_l(&vl, &vr)) {
3150 // Do not ramp volume if volume is controlled by effect
3151 param = AudioMixer::VOLUME;
3152 track->mHasVolumeController = true;
3153 } else {
3154 // force no volume ramp when volume controller was just disabled or removed
3155 // from effect chain to avoid volume spike
3156 if (track->mHasVolumeController) {
3157 param = AudioMixer::VOLUME;
3158 }
3159 track->mHasVolumeController = false;
3160 }
3161
3162 // Convert volumes from 8.24 to 4.12 format
Glenn Kastenb1cf75c2012-01-17 12:20:54 -08003163 // This additional clamping is needed in case chain->setVolume_l() overshot
Glenn Kasten3b81aca2012-01-27 15:26:23 -08003164 vl = (vl + (1 << 11)) >> 12;
3165 if (vl > MAX_GAIN_INT) vl = MAX_GAIN_INT;
3166 vr = (vr + (1 << 11)) >> 12;
3167 if (vr > MAX_GAIN_INT) vr = MAX_GAIN_INT;
Eric Laurente0aed6d2010-09-10 17:44:44 -07003168
Glenn Kasten3b81aca2012-01-27 15:26:23 -08003169 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 -07003170
Mathias Agopian65ab4712010-07-14 17:59:35 -07003171 // XXX: these things DON'T need to be done each time
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08003172 mAudioMixer->setBufferProvider(name, track);
3173 mAudioMixer->enable(name);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003174
Glenn Kasten3b81aca2012-01-27 15:26:23 -08003175 mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)vl);
3176 mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)vr);
3177 mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)va);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003178 mAudioMixer->setParameter(
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08003179 name,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003180 AudioMixer::TRACK,
3181 AudioMixer::FORMAT, (void *)track->format());
3182 mAudioMixer->setParameter(
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08003183 name,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003184 AudioMixer::TRACK,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07003185 AudioMixer::CHANNEL_MASK, (void *)track->channelMask());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003186 mAudioMixer->setParameter(
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08003187 name,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003188 AudioMixer::RESAMPLE,
3189 AudioMixer::SAMPLE_RATE,
3190 (void *)(cblk->sampleRate));
3191 mAudioMixer->setParameter(
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08003192 name,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003193 AudioMixer::TRACK,
3194 AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer());
3195 mAudioMixer->setParameter(
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08003196 name,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003197 AudioMixer::TRACK,
3198 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer());
3199
3200 // reset retry count
3201 track->mRetryCount = kMaxTrackRetries;
Glenn Kastenea7939a2012-03-14 12:56:26 -07003202
Eric Laurent27741442012-01-17 19:20:12 -08003203 // If one track is ready, set the mixer ready if:
3204 // - the mixer was not ready during previous round OR
3205 // - no other track is not ready
Glenn Kasten81028042012-04-30 18:15:12 -07003206 if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY ||
Eric Laurent27741442012-01-17 19:20:12 -08003207 mixerStatus != MIXER_TRACKS_ENABLED) {
3208 mixerStatus = MIXER_TRACKS_READY;
3209 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003210 } else {
Eric Laurent91b14c42012-05-30 12:30:29 -07003211 // clear effect chain input buffer if an active track underruns to avoid sending
3212 // previous audio buffer again to effects
3213 chain = getEffectChain_l(track->sessionId());
3214 if (chain != 0) {
3215 chain->clearInputBuffer();
3216 }
3217
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08003218 //ALOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", name, cblk->user, cblk->server, this);
Glenn Kasten44cda3a2012-08-01 09:40:18 -07003219 if ((track->sharedBuffer() != 0) ||
Eric Laurent83faee02012-04-27 18:24:29 -07003220 track->isStopped() || track->isPaused()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003221 // We have consumed all the buffers of this track.
3222 // Remove it from the list of active tracks.
Eric Laurenta011e352012-03-29 15:51:43 -07003223 // TODO: use actual buffer filling status instead of latency when available from
3224 // audio HAL
3225 size_t audioHALFrames =
3226 (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000;
3227 size_t framesWritten =
3228 mBytesWritten / audio_stream_frame_size(&mOutput->stream->common);
3229 if (track->presentationComplete(framesWritten, audioHALFrames)) {
Eric Laurent29864602012-05-08 18:57:51 -07003230 if (track->isStopped()) {
3231 track->reset();
3232 }
Eric Laurenta011e352012-03-29 15:51:43 -07003233 tracksToRemove->add(track);
3234 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003235 } else {
Glenn Kastenbf0d21f2012-05-31 14:59:29 -07003236 track->mUnderrunCount++;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003237 // No buffers for this track. Give it a few chances to
3238 // fill a buffer, then remove it from active list.
Glenn Kasten44cda3a2012-08-01 09:40:18 -07003239 if (--(track->mRetryCount) <= 0 || track->isTerminated()) {
3240 ALOGV_IF(track->mRetryCount <= 0, "BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003241 tracksToRemove->add(track);
Glenn Kasten288ed212012-04-25 17:52:27 -07003242 // indicate to client process that the track was disabled because of underrun;
3243 // it will then automatically call start() when data is available
Eric Laurent38ccae22011-03-28 18:37:07 -07003244 android_atomic_or(CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent27741442012-01-17 19:20:12 -08003245 // If one track is not ready, mark the mixer also not ready if:
3246 // - the mixer was ready during previous round OR
3247 // - no other track is ready
Glenn Kasten81028042012-04-30 18:15:12 -07003248 } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY ||
Eric Laurent27741442012-01-17 19:20:12 -08003249 mixerStatus != MIXER_TRACKS_READY) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003250 mixerStatus = MIXER_TRACKS_ENABLED;
3251 }
3252 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08003253 mAudioMixer->disable(name);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003254 }
Glenn Kasten58912562012-04-03 10:45:00 -07003255
3256 } // local variable scope to avoid goto warning
3257track_is_ready: ;
3258
Mathias Agopian65ab4712010-07-14 17:59:35 -07003259 }
3260
Glenn Kasten288ed212012-04-25 17:52:27 -07003261 // Push the new FastMixer state if necessary
Glenn Kastenc15d6652012-05-30 14:52:57 -07003262 bool pauseAudioWatchdog = false;
Glenn Kasten288ed212012-04-25 17:52:27 -07003263 if (didModify) {
3264 state->mFastTracksGen++;
3265 // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle
3266 if (kUseFastMixer == FastMixer_Dynamic &&
3267 state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) {
3268 state->mCommand = FastMixerState::COLD_IDLE;
3269 state->mColdFutexAddr = &mFastMixerFutex;
3270 state->mColdGen++;
3271 mFastMixerFutex = 0;
3272 if (kUseFastMixer == FastMixer_Dynamic) {
3273 mNormalSink = mOutputSink;
3274 }
3275 // If we go into cold idle, need to wait for acknowledgement
3276 // so that fast mixer stops doing I/O.
3277 block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
Glenn Kastenc15d6652012-05-30 14:52:57 -07003278 pauseAudioWatchdog = true;
Glenn Kasten288ed212012-04-25 17:52:27 -07003279 }
3280 sq->end();
3281 }
3282 if (sq != NULL) {
3283 sq->end(didModify);
3284 sq->push(block);
3285 }
Glenn Kastenc15d6652012-05-30 14:52:57 -07003286 if (pauseAudioWatchdog && mAudioWatchdog != 0) {
3287 mAudioWatchdog->pause();
3288 }
Glenn Kasten288ed212012-04-25 17:52:27 -07003289
3290 // Now perform the deferred reset on fast tracks that have stopped
3291 while (resetMask != 0) {
3292 size_t i = __builtin_ctz(resetMask);
3293 ALOG_ASSERT(i < count);
3294 resetMask &= ~(1 << i);
3295 sp<Track> t = mActiveTracks[i].promote();
3296 if (t == 0) continue;
3297 Track* track = t.get();
3298 ALOG_ASSERT(track->isFastTrack() && track->isStopped());
3299 track->reset();
3300 }
Glenn Kasten58912562012-04-03 10:45:00 -07003301
Mathias Agopian65ab4712010-07-14 17:59:35 -07003302 // remove all the tracks that need to be...
3303 count = tracksToRemove->size();
Glenn Kastenf6b16782011-12-15 09:51:17 -08003304 if (CC_UNLIKELY(count)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003305 for (size_t i=0 ; i<count ; i++) {
3306 const sp<Track>& track = tracksToRemove->itemAt(i);
3307 mActiveTracks.remove(track);
3308 if (track->mainBuffer() != mMixBuffer) {
3309 chain = getEffectChain_l(track->sessionId());
3310 if (chain != 0) {
Steve Block3856b092011-10-20 11:56:00 +01003311 ALOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId());
Eric Laurentb469b942011-05-09 12:09:06 -07003312 chain->decActiveTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07003313 }
3314 }
3315 if (track->isTerminated()) {
Eric Laurentb469b942011-05-09 12:09:06 -07003316 removeTrack_l(track);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003317 }
3318 }
3319 }
3320
3321 // mix buffer must be cleared if all tracks are connected to an
3322 // effect chain as in this case the mixer will not write to
3323 // mix buffer and track effects will accumulate into it
Glenn Kasten58912562012-04-03 10:45:00 -07003324 if ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || (mixedTracks == 0 && fastTracks > 0)) {
3325 // FIXME as a performance optimization, should remember previous zero status
3326 memset(mMixBuffer, 0, mNormalFrameCount * mChannelCount * sizeof(int16_t));
Mathias Agopian65ab4712010-07-14 17:59:35 -07003327 }
3328
Glenn Kasten58912562012-04-03 10:45:00 -07003329 // if any fast tracks, then status is ready
Glenn Kasten81028042012-04-30 18:15:12 -07003330 mMixerStatusIgnoringFastTracks = mixerStatus;
Glenn Kasten58912562012-04-03 10:45:00 -07003331 if (fastTracks > 0) {
3332 mixerStatus = MIXER_TRACKS_READY;
3333 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003334 return mixerStatus;
3335}
3336
Glenn Kasten66fcab92012-02-24 14:59:21 -08003337/*
3338The derived values that are cached:
3339 - mixBufferSize from frame count * frame size
3340 - activeSleepTime from activeSleepTimeUs()
3341 - idleSleepTime from idleSleepTimeUs()
3342 - standbyDelay from mActiveSleepTimeUs (DIRECT only)
3343 - maxPeriod from frame count and sample rate (MIXER only)
3344
3345The parameters that affect these derived values are:
3346 - frame count
3347 - frame size
3348 - sample rate
3349 - device type: A2DP or not
3350 - device latency
3351 - format: PCM or not
3352 - active sleep time
3353 - idle sleep time
3354*/
3355
3356void AudioFlinger::PlaybackThread::cacheParameters_l()
3357{
Glenn Kasten58912562012-04-03 10:45:00 -07003358 mixBufferSize = mNormalFrameCount * mFrameSize;
Glenn Kasten66fcab92012-02-24 14:59:21 -08003359 activeSleepTime = activeSleepTimeUs();
3360 idleSleepTime = idleSleepTimeUs();
3361}
3362
Eric Laurent22167852012-06-20 12:26:32 -07003363void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003364{
Steve Block3856b092011-10-20 11:56:00 +01003365 ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d",
Eric Laurentde070132010-07-13 04:45:46 -07003366 this, streamType, mTracks.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003367 Mutex::Autolock _l(mLock);
Eric Laurentde070132010-07-13 04:45:46 -07003368
Mathias Agopian65ab4712010-07-14 17:59:35 -07003369 size_t size = mTracks.size();
3370 for (size_t i = 0; i < size; i++) {
3371 sp<Track> t = mTracks[i];
Glenn Kasten02bbd202012-02-08 12:35:35 -08003372 if (t->streamType() == streamType) {
Eric Laurent38ccae22011-03-28 18:37:07 -07003373 android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003374 t->mCblk->cv.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -07003375 }
3376 }
3377}
3378
Mathias Agopian65ab4712010-07-14 17:59:35 -07003379// getTrackName_l() must be called with ThreadBase::mLock held
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07003380int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003381{
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -07003382 return mAudioMixer->getTrackName(channelMask);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003383}
3384
3385// deleteTrackName_l() must be called with ThreadBase::mLock held
3386void AudioFlinger::MixerThread::deleteTrackName_l(int name)
3387{
Steve Block3856b092011-10-20 11:56:00 +01003388 ALOGV("remove track (%d) and delete from mixer", name);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003389 mAudioMixer->deleteTrackName(name);
3390}
3391
3392// checkForNewParameters_l() must be called with ThreadBase::mLock held
3393bool AudioFlinger::MixerThread::checkForNewParameters_l()
3394{
Glenn Kasten58912562012-04-03 10:45:00 -07003395 // if !&IDLE, holds the FastMixer state to restore after new parameters processed
3396 FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003397 bool reconfig = false;
3398
3399 while (!mNewParameters.isEmpty()) {
Glenn Kasten58912562012-04-03 10:45:00 -07003400
3401 if (mFastMixer != NULL) {
3402 FastMixerStateQueue *sq = mFastMixer->sq();
3403 FastMixerState *state = sq->begin();
3404 if (!(state->mCommand & FastMixerState::IDLE)) {
3405 previousCommand = state->mCommand;
3406 state->mCommand = FastMixerState::HOT_IDLE;
3407 sq->end();
3408 sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED);
3409 } else {
3410 sq->end(false /*didModify*/);
3411 }
3412 }
3413
Mathias Agopian65ab4712010-07-14 17:59:35 -07003414 status_t status = NO_ERROR;
3415 String8 keyValuePair = mNewParameters[0];
3416 AudioParameter param = AudioParameter(keyValuePair);
3417 int value;
3418
3419 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
3420 reconfig = true;
3421 }
3422 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
Glenn Kasten58f30212012-01-12 12:27:51 -08003423 if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003424 status = BAD_VALUE;
3425 } else {
3426 reconfig = true;
3427 }
3428 }
3429 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
Dima Zavinfce7a472011-04-19 22:30:36 -07003430 if (value != AUDIO_CHANNEL_OUT_STEREO) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07003431 status = BAD_VALUE;
3432 } else {
3433 reconfig = true;
3434 }
3435 }
3436 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
3437 // do not accept frame count changes if tracks are open as the track buffer
Glenn Kasten362c4e62011-12-14 10:28:06 -08003438 // size depends on frame count and correct behavior would not be guaranteed
Mathias Agopian65ab4712010-07-14 17:59:35 -07003439 // if frame count is changed after track creation
3440 if (!mTracks.isEmpty()) {
3441 status = INVALID_OPERATION;
3442 } else {
3443 reconfig = true;
3444 }
3445 }
3446 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
Glenn Kastend3cee2f2012-03-13 17:55:35 -07003447#ifdef ADD_BATTERY_DATA
Gloria Wang9ee159b2011-02-24 14:51:45 -08003448 // when changing the audio output device, call addBatteryData to notify
3449 // the change
Glenn Kasten5ad92f62012-07-19 10:02:15 -07003450 if (mDevice != value) {
Gloria Wang9ee159b2011-02-24 14:51:45 -08003451 uint32_t params = 0;
3452 // check whether speaker is on
Dima Zavinfce7a472011-04-19 22:30:36 -07003453 if (value & AUDIO_DEVICE_OUT_SPEAKER) {
Gloria Wang9ee159b2011-02-24 14:51:45 -08003454 params |= IMediaPlayerService::kBatteryDataSpeakerOn;
3455 }
3456
Glenn Kastenbb4350d2012-07-03 15:56:38 -07003457 audio_devices_t deviceWithoutSpeaker
Dima Zavinfce7a472011-04-19 22:30:36 -07003458 = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER;
Gloria Wang9ee159b2011-02-24 14:51:45 -08003459 // check if any other device (except speaker) is on
3460 if (value & deviceWithoutSpeaker ) {
3461 params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
3462 }
3463
3464 if (params != 0) {
3465 addBatteryData(params);
3466 }
3467 }
Glenn Kastend3cee2f2012-03-13 17:55:35 -07003468#endif
Gloria Wang9ee159b2011-02-24 14:51:45 -08003469
Mathias Agopian65ab4712010-07-14 17:59:35 -07003470 // forward device change to effects that have requested to be
3471 // aware of attached audio device.
Glenn Kasten5ad92f62012-07-19 10:02:15 -07003472 mDevice = value;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003473 for (size_t i = 0; i < mEffectChains.size(); i++) {
Eric Laurentcab11242010-07-15 12:50:15 -07003474 mEffectChains[i]->setDevice_l(mDevice);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003475 }
3476 }
3477
3478 if (status == NO_ERROR) {
Dima Zavin799a70e2011-04-18 16:57:27 -07003479 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavinfce7a472011-04-19 22:30:36 -07003480 keyValuePair.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003481 if (!mStandby && status == INVALID_OPERATION) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07003482 mOutput->stream->common.standby(&mOutput->stream->common);
3483 mStandby = true;
3484 mBytesWritten = 0;
3485 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavinfce7a472011-04-19 22:30:36 -07003486 keyValuePair.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003487 }
3488 if (status == NO_ERROR && reconfig) {
3489 delete mAudioMixer;
Glenn Kastene9dd0172012-01-27 18:08:45 -08003490 // for safety in case readOutputParameters() accesses mAudioMixer (it doesn't)
3491 mAudioMixer = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003492 readOutputParameters();
Glenn Kasten58912562012-04-03 10:45:00 -07003493 mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003494 for (size_t i = 0; i < mTracks.size() ; i++) {
Glenn Kasten254af182012-07-03 14:59:05 -07003495 int name = getTrackName_l(mTracks[i]->mChannelMask);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003496 if (name < 0) break;
3497 mTracks[i]->mName = name;
3498 // limit track sample rate to 2 x new output sample rate
3499 if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) {
3500 mTracks[i]->mCblk->sampleRate = 2 * sampleRate();
3501 }
3502 }
3503 sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
3504 }
3505 }
3506
3507 mNewParameters.removeAt(0);
3508
3509 mParamStatus = status;
3510 mParamCond.signal();
Eric Laurent60cd0a02011-09-13 11:40:21 -07003511 // wait for condition with time out in case the thread calling ThreadBase::setParameters()
3512 // already timed out waiting for the status and will never signal the condition.
Glenn Kasten7dede872011-12-13 11:04:14 -08003513 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003514 }
Glenn Kasten58912562012-04-03 10:45:00 -07003515
3516 if (!(previousCommand & FastMixerState::IDLE)) {
3517 ALOG_ASSERT(mFastMixer != NULL);
3518 FastMixerStateQueue *sq = mFastMixer->sq();
3519 FastMixerState *state = sq->begin();
3520 ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE);
3521 state->mCommand = previousCommand;
3522 sq->end();
3523 sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
3524 }
3525
Mathias Agopian65ab4712010-07-14 17:59:35 -07003526 return reconfig;
3527}
3528
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07003529void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003530{
3531 const size_t SIZE = 256;
3532 char buffer[SIZE];
3533 String8 result;
3534
3535 PlaybackThread::dumpInternals(fd, args);
3536
3537 snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames());
3538 result.append(buffer);
3539 write(fd, result.string(), result.size());
Glenn Kasten58912562012-04-03 10:45:00 -07003540
3541 // Make a non-atomic copy of fast mixer dump state so it won't change underneath us
3542 FastMixerDumpState copy = mFastMixerDumpState;
3543 copy.dump(fd);
3544
Glenn Kasten39993082012-05-31 13:40:27 -07003545#ifdef STATE_QUEUE_DUMP
3546 // Similar for state queue
3547 StateQueueObserverDump observerCopy = mStateQueueObserverDump;
3548 observerCopy.dump(fd);
3549 StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump;
3550 mutatorCopy.dump(fd);
3551#endif
3552
Glenn Kastenfbae5da2012-05-21 09:17:20 -07003553 // Write the tee output to a .wav file
3554 NBAIO_Source *teeSource = mTeeSource.get();
3555 if (teeSource != NULL) {
3556 char teePath[64];
3557 struct timeval tv;
3558 gettimeofday(&tv, NULL);
3559 struct tm tm;
3560 localtime_r(&tv.tv_sec, &tm);
3561 strftime(teePath, sizeof(teePath), "/data/misc/media/%T.wav", &tm);
3562 int teeFd = open(teePath, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
3563 if (teeFd >= 0) {
3564 char wavHeader[44];
3565 memcpy(wavHeader,
3566 "RIFF\0\0\0\0WAVEfmt \20\0\0\0\1\0\2\0\104\254\0\0\0\0\0\0\4\0\20\0data\0\0\0\0",
3567 sizeof(wavHeader));
3568 NBAIO_Format format = teeSource->format();
3569 unsigned channelCount = Format_channelCount(format);
3570 ALOG_ASSERT(channelCount <= FCC_2);
3571 unsigned sampleRate = Format_sampleRate(format);
3572 wavHeader[22] = channelCount; // number of channels
3573 wavHeader[24] = sampleRate; // sample rate
3574 wavHeader[25] = sampleRate >> 8;
3575 wavHeader[32] = channelCount * 2; // block alignment
3576 write(teeFd, wavHeader, sizeof(wavHeader));
3577 size_t total = 0;
3578 bool firstRead = true;
3579 for (;;) {
3580#define TEE_SINK_READ 1024
3581 short buffer[TEE_SINK_READ * FCC_2];
3582 size_t count = TEE_SINK_READ;
John Grossman2c3b2da2012-08-02 17:08:54 -07003583 ssize_t actual = teeSource->read(buffer, count,
3584 AudioBufferProvider::kInvalidPTS);
Glenn Kastenfbae5da2012-05-21 09:17:20 -07003585 bool wasFirstRead = firstRead;
3586 firstRead = false;
3587 if (actual <= 0) {
3588 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3589 continue;
3590 }
3591 break;
3592 }
Eric Laurenta5f44eb2012-06-25 11:38:29 -07003593 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kastenfbae5da2012-05-21 09:17:20 -07003594 write(teeFd, buffer, actual * channelCount * sizeof(short));
3595 total += actual;
3596 }
3597 lseek(teeFd, (off_t) 4, SEEK_SET);
3598 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
3599 write(teeFd, &temp, sizeof(temp));
3600 lseek(teeFd, (off_t) 40, SEEK_SET);
3601 temp = total * channelCount * sizeof(short);
3602 write(teeFd, &temp, sizeof(temp));
3603 close(teeFd);
3604 fdprintf(fd, "FastMixer tee copied to %s\n", teePath);
3605 } else {
3606 fdprintf(fd, "FastMixer unable to create tee %s: \n", strerror(errno));
3607 }
3608 }
3609
Glenn Kastenc15d6652012-05-30 14:52:57 -07003610 if (mAudioWatchdog != 0) {
3611 // Make a non-atomic copy of audio watchdog dump so it won't change underneath us
3612 AudioWatchdogDump wdCopy = mAudioWatchdogDump;
3613 wdCopy.dump(fd);
3614 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003615}
3616
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08003617uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003618{
Glenn Kasten58912562012-04-03 10:45:00 -07003619 return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003620}
3621
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08003622uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const
Eric Laurent25cbe0e2010-08-18 18:13:17 -07003623{
Glenn Kasten58912562012-04-03 10:45:00 -07003624 return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000);
Eric Laurent25cbe0e2010-08-18 18:13:17 -07003625}
3626
Glenn Kasten66fcab92012-02-24 14:59:21 -08003627void AudioFlinger::MixerThread::cacheParameters_l()
3628{
3629 PlaybackThread::cacheParameters_l();
3630
3631 // FIXME: Relaxed timing because of a certain device that can't meet latency
3632 // Should be reduced to 2x after the vendor fixes the driver issue
3633 // increase threshold again due to low power audio mode. The way this warning
3634 // threshold is calculated and its usefulness should be reconsidered anyway.
Glenn Kasten58912562012-04-03 10:45:00 -07003635 maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15;
Glenn Kasten66fcab92012-02-24 14:59:21 -08003636}
3637
Mathias Agopian65ab4712010-07-14 17:59:35 -07003638// ----------------------------------------------------------------------------
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003639AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07003640 AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device)
Glenn Kasten23bb8be2012-01-26 10:38:26 -08003641 : PlaybackThread(audioFlinger, output, id, device, DIRECT)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08003642 // mLeftVolFloat, mRightVolFloat
Mathias Agopian65ab4712010-07-14 17:59:35 -07003643{
Mathias Agopian65ab4712010-07-14 17:59:35 -07003644}
3645
3646AudioFlinger::DirectOutputThread::~DirectOutputThread()
3647{
3648}
3649
Glenn Kasten1465f0c2012-03-06 11:23:32 -08003650AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l(
3651 Vector< sp<Track> > *tracksToRemove
Glenn Kasten000f0e32012-03-01 17:10:56 -08003652)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003653{
Glenn Kasten1465f0c2012-03-06 11:23:32 -08003654 sp<Track> trackToRemove;
3655
Glenn Kastenfec279f2012-03-08 07:47:15 -08003656 mixer_state mixerStatus = MIXER_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003657
Glenn Kasten952eeb22012-03-06 11:30:57 -08003658 // find out which tracks need to be processed
3659 if (mActiveTracks.size() != 0) {
3660 sp<Track> t = mActiveTracks[0].promote();
Glenn Kastenfec279f2012-03-08 07:47:15 -08003661 // The track died recently
3662 if (t == 0) return MIXER_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003663
Glenn Kasten952eeb22012-03-06 11:30:57 -08003664 Track* const track = t.get();
3665 audio_track_cblk_t* cblk = track->cblk();
Mathias Agopian65ab4712010-07-14 17:59:35 -07003666
Glenn Kasten952eeb22012-03-06 11:30:57 -08003667 // The first time a track is added we wait
3668 // for all its buffers to be filled before processing it
Eric Laurent67c0a582012-05-01 19:31:12 -07003669 uint32_t minFrames;
3670 if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing()) {
3671 minFrames = mNormalFrameCount;
3672 } else {
3673 minFrames = 1;
3674 }
3675 if ((track->framesReady() >= minFrames) && track->isReady() &&
Glenn Kasten952eeb22012-03-06 11:30:57 -08003676 !track->isPaused() && !track->isTerminated())
3677 {
3678 //ALOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003679
Glenn Kasten952eeb22012-03-06 11:30:57 -08003680 if (track->mFillingUpStatus == Track::FS_FILLED) {
3681 track->mFillingUpStatus = Track::FS_ACTIVE;
3682 mLeftVolFloat = mRightVolFloat = 0;
Glenn Kasten952eeb22012-03-06 11:30:57 -08003683 if (track->mState == TrackBase::RESUMING) {
3684 track->mState = TrackBase::ACTIVE;
Glenn Kasten952eeb22012-03-06 11:30:57 -08003685 }
Glenn Kasten952eeb22012-03-06 11:30:57 -08003686 }
Eric Laurent67c0a582012-05-01 19:31:12 -07003687
Glenn Kasten952eeb22012-03-06 11:30:57 -08003688 // compute volume for this track
3689 float left, right;
3690 if (track->isMuted() || mMasterMute || track->isPausing() ||
3691 mStreamTypes[track->streamType()].mute) {
3692 left = right = 0;
3693 if (track->isPausing()) {
3694 track->setPaused();
3695 }
3696 } else {
3697 float typeVolume = mStreamTypes[track->streamType()].volume;
3698 float v = mMasterVolume * typeVolume;
3699 uint32_t vlr = cblk->getVolumeLR();
3700 float v_clamped = v * (vlr & 0xFFFF);
3701 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
3702 left = v_clamped/MAX_GAIN;
3703 v_clamped = v * (vlr >> 16);
3704 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
3705 right = v_clamped/MAX_GAIN;
3706 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003707
Glenn Kasten952eeb22012-03-06 11:30:57 -08003708 if (left != mLeftVolFloat || right != mRightVolFloat) {
3709 mLeftVolFloat = left;
3710 mRightVolFloat = right;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003711
Glenn Kasten952eeb22012-03-06 11:30:57 -08003712 // Convert volumes from float to 8.24
3713 uint32_t vl = (uint32_t)(left * (1 << 24));
3714 uint32_t vr = (uint32_t)(right * (1 << 24));
Mathias Agopian65ab4712010-07-14 17:59:35 -07003715
Glenn Kasten952eeb22012-03-06 11:30:57 -08003716 // Delegate volume control to effect in track effect chain if needed
3717 // only one effect chain can be present on DirectOutputThread, so if
3718 // there is one, the track is connected to it
3719 if (!mEffectChains.isEmpty()) {
3720 // Do not ramp volume if volume is controlled by effect
Eric Laurent67c0a582012-05-01 19:31:12 -07003721 mEffectChains[0]->setVolume_l(&vl, &vr);
3722 left = (float)vl / (1 << 24);
3723 right = (float)vr / (1 << 24);
Glenn Kasten952eeb22012-03-06 11:30:57 -08003724 }
Eric Laurent67c0a582012-05-01 19:31:12 -07003725 mOutput->stream->set_volume(mOutput->stream, left, right);
Glenn Kasten952eeb22012-03-06 11:30:57 -08003726 }
3727
3728 // reset retry count
3729 track->mRetryCount = kMaxTrackRetriesDirect;
Glenn Kastenb071e9b2012-03-07 17:05:59 -08003730 mActiveTrack = t;
Glenn Kastenfec279f2012-03-08 07:47:15 -08003731 mixerStatus = MIXER_TRACKS_READY;
Glenn Kasten952eeb22012-03-06 11:30:57 -08003732 } else {
Eric Laurent91b14c42012-05-30 12:30:29 -07003733 // clear effect chain input buffer if an active track underruns to avoid sending
3734 // previous audio buffer again to effects
3735 if (!mEffectChains.isEmpty()) {
3736 mEffectChains[0]->clearInputBuffer();
3737 }
3738
Glenn Kasten952eeb22012-03-06 11:30:57 -08003739 //ALOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server);
Glenn Kasten44cda3a2012-08-01 09:40:18 -07003740 if ((track->sharedBuffer() != 0) ||
Eric Laurent67c0a582012-05-01 19:31:12 -07003741 track->isStopped() || track->isPaused()) {
Glenn Kasten952eeb22012-03-06 11:30:57 -08003742 // We have consumed all the buffers of this track.
3743 // Remove it from the list of active tracks.
Eric Laurenta011e352012-03-29 15:51:43 -07003744 // TODO: implement behavior for compressed audio
3745 size_t audioHALFrames =
3746 (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000;
3747 size_t framesWritten =
3748 mBytesWritten / audio_stream_frame_size(&mOutput->stream->common);
3749 if (track->presentationComplete(framesWritten, audioHALFrames)) {
Eric Laurent29864602012-05-08 18:57:51 -07003750 if (track->isStopped()) {
3751 track->reset();
3752 }
Eric Laurenta011e352012-03-29 15:51:43 -07003753 trackToRemove = track;
3754 }
Glenn Kasten952eeb22012-03-06 11:30:57 -08003755 } else {
3756 // No buffers for this track. Give it a few chances to
3757 // fill a buffer, then remove it from active list.
Glenn Kasten44cda3a2012-08-01 09:40:18 -07003758 if (--(track->mRetryCount) <= 0 || track->isTerminated()) {
3759 ALOGV_IF(track->mRetryCount <= 0, "BUFFER TIMEOUT: remove(%d) from active list", track->name());
Glenn Kasten952eeb22012-03-06 11:30:57 -08003760 trackToRemove = track;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003761 } else {
Glenn Kastenfec279f2012-03-08 07:47:15 -08003762 mixerStatus = MIXER_TRACKS_ENABLED;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003763 }
3764 }
Glenn Kasten952eeb22012-03-06 11:30:57 -08003765 }
3766 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003767
Glenn Kasten1465f0c2012-03-06 11:23:32 -08003768 // FIXME merge this with similar code for removing multiple tracks
Glenn Kasten952eeb22012-03-06 11:30:57 -08003769 // remove all the tracks that need to be...
3770 if (CC_UNLIKELY(trackToRemove != 0)) {
Glenn Kasten1465f0c2012-03-06 11:23:32 -08003771 tracksToRemove->add(trackToRemove);
Glenn Kasten952eeb22012-03-06 11:30:57 -08003772 mActiveTracks.remove(trackToRemove);
3773 if (!mEffectChains.isEmpty()) {
Glenn Kasten5798d4e2012-03-08 12:18:35 -08003774 ALOGV("stopping track on chain %p for session Id: %d", mEffectChains[0].get(),
Glenn Kasten952eeb22012-03-06 11:30:57 -08003775 trackToRemove->sessionId());
3776 mEffectChains[0]->decActiveTrackCnt();
3777 }
3778 if (trackToRemove->isTerminated()) {
3779 removeTrack_l(trackToRemove);
3780 }
3781 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003782
Glenn Kastenfec279f2012-03-08 07:47:15 -08003783 return mixerStatus;
Glenn Kasten000f0e32012-03-01 17:10:56 -08003784}
Mathias Agopian65ab4712010-07-14 17:59:35 -07003785
Glenn Kasten000f0e32012-03-01 17:10:56 -08003786void AudioFlinger::DirectOutputThread::threadLoop_mix()
3787{
Glenn Kasten952eeb22012-03-06 11:30:57 -08003788 AudioBufferProvider::Buffer buffer;
3789 size_t frameCount = mFrameCount;
3790 int8_t *curBuf = (int8_t *)mMixBuffer;
3791 // output audio to hardware
3792 while (frameCount) {
3793 buffer.frameCount = frameCount;
Glenn Kastenb071e9b2012-03-07 17:05:59 -08003794 mActiveTrack->getNextBuffer(&buffer);
Glenn Kasten952eeb22012-03-06 11:30:57 -08003795 if (CC_UNLIKELY(buffer.raw == NULL)) {
3796 memset(curBuf, 0, frameCount * mFrameSize);
3797 break;
3798 }
3799 memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
3800 frameCount -= buffer.frameCount;
3801 curBuf += buffer.frameCount * mFrameSize;
Glenn Kastenb071e9b2012-03-07 17:05:59 -08003802 mActiveTrack->releaseBuffer(&buffer);
Glenn Kasten952eeb22012-03-06 11:30:57 -08003803 }
3804 sleepTime = 0;
3805 standbyTime = systemTime() + standbyDelay;
Glenn Kastenb071e9b2012-03-07 17:05:59 -08003806 mActiveTrack.clear();
Glenn Kasten73f4bc32012-03-09 12:08:48 -08003807
Glenn Kasten000f0e32012-03-01 17:10:56 -08003808}
3809
3810void AudioFlinger::DirectOutputThread::threadLoop_sleepTime()
3811{
Glenn Kasten952eeb22012-03-06 11:30:57 -08003812 if (sleepTime == 0) {
Glenn Kastenfec279f2012-03-08 07:47:15 -08003813 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
Glenn Kasten952eeb22012-03-06 11:30:57 -08003814 sleepTime = activeSleepTime;
3815 } else {
3816 sleepTime = idleSleepTime;
3817 }
3818 } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) {
Glenn Kasten58912562012-04-03 10:45:00 -07003819 memset(mMixBuffer, 0, mFrameCount * mFrameSize);
Glenn Kasten952eeb22012-03-06 11:30:57 -08003820 sleepTime = 0;
3821 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003822}
3823
3824// getTrackName_l() must be called with ThreadBase::mLock held
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07003825int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003826{
3827 return 0;
3828}
3829
3830// deleteTrackName_l() must be called with ThreadBase::mLock held
3831void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name)
3832{
3833}
3834
3835// checkForNewParameters_l() must be called with ThreadBase::mLock held
3836bool AudioFlinger::DirectOutputThread::checkForNewParameters_l()
3837{
3838 bool reconfig = false;
3839
3840 while (!mNewParameters.isEmpty()) {
3841 status_t status = NO_ERROR;
3842 String8 keyValuePair = mNewParameters[0];
3843 AudioParameter param = AudioParameter(keyValuePair);
3844 int value;
3845
3846 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
3847 // do not accept frame count changes if tracks are open as the track buffer
3848 // size depends on frame count and correct behavior would not be garantied
3849 // if frame count is changed after track creation
3850 if (!mTracks.isEmpty()) {
3851 status = INVALID_OPERATION;
3852 } else {
3853 reconfig = true;
3854 }
3855 }
3856 if (status == NO_ERROR) {
Dima Zavin799a70e2011-04-18 16:57:27 -07003857 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavinfce7a472011-04-19 22:30:36 -07003858 keyValuePair.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003859 if (!mStandby && status == INVALID_OPERATION) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07003860 mOutput->stream->common.standby(&mOutput->stream->common);
3861 mStandby = true;
3862 mBytesWritten = 0;
3863 status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
Dima Zavinfce7a472011-04-19 22:30:36 -07003864 keyValuePair.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003865 }
3866 if (status == NO_ERROR && reconfig) {
3867 readOutputParameters();
3868 sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
3869 }
3870 }
3871
3872 mNewParameters.removeAt(0);
3873
3874 mParamStatus = status;
3875 mParamCond.signal();
Eric Laurent60cd0a02011-09-13 11:40:21 -07003876 // wait for condition with time out in case the thread calling ThreadBase::setParameters()
3877 // already timed out waiting for the status and will never signal the condition.
Glenn Kasten7dede872011-12-13 11:04:14 -08003878 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003879 }
3880 return reconfig;
3881}
3882
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08003883uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003884{
3885 uint32_t time;
Dima Zavinfce7a472011-04-19 22:30:36 -07003886 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent162b40b2011-12-05 09:47:19 -08003887 time = PlaybackThread::activeSleepTimeUs();
Mathias Agopian65ab4712010-07-14 17:59:35 -07003888 } else {
3889 time = 10000;
3890 }
3891 return time;
3892}
3893
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08003894uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003895{
3896 uint32_t time;
Dima Zavinfce7a472011-04-19 22:30:36 -07003897 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent60e18242010-07-29 06:50:24 -07003898 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003899 } else {
3900 time = 10000;
3901 }
3902 return time;
3903}
3904
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08003905uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const
Eric Laurent25cbe0e2010-08-18 18:13:17 -07003906{
3907 uint32_t time;
Dima Zavinfce7a472011-04-19 22:30:36 -07003908 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent25cbe0e2010-08-18 18:13:17 -07003909 time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
3910 } else {
3911 time = 10000;
3912 }
3913 return time;
3914}
3915
Glenn Kasten66fcab92012-02-24 14:59:21 -08003916void AudioFlinger::DirectOutputThread::cacheParameters_l()
3917{
3918 PlaybackThread::cacheParameters_l();
3919
3920 // use shorter standby delay as on normal output to release
3921 // hardware resources as soon as possible
3922 standbyDelay = microseconds(activeSleepTime*2);
3923}
Eric Laurent25cbe0e2010-08-18 18:13:17 -07003924
Mathias Agopian65ab4712010-07-14 17:59:35 -07003925// ----------------------------------------------------------------------------
3926
Glenn Kasten23bb8be2012-01-26 10:38:26 -08003927AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003928 AudioFlinger::MixerThread* mainThread, audio_io_handle_t id)
Glenn Kasten23bb8be2012-01-26 10:38:26 -08003929 : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device(), DUPLICATING),
3930 mWaitTimeMs(UINT_MAX)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003931{
Mathias Agopian65ab4712010-07-14 17:59:35 -07003932 addOutputTrack(mainThread);
3933}
3934
3935AudioFlinger::DuplicatingThread::~DuplicatingThread()
3936{
3937 for (size_t i = 0; i < mOutputTracks.size(); i++) {
3938 mOutputTracks[i]->destroy();
3939 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003940}
3941
Glenn Kasten000f0e32012-03-01 17:10:56 -08003942void AudioFlinger::DuplicatingThread::threadLoop_mix()
Mathias Agopian65ab4712010-07-14 17:59:35 -07003943{
Glenn Kasten952eeb22012-03-06 11:30:57 -08003944 // mix buffers...
3945 if (outputsReady(outputTracks)) {
3946 mAudioMixer->process(AudioBufferProvider::kInvalidPTS);
3947 } else {
3948 memset(mMixBuffer, 0, mixBufferSize);
3949 }
3950 sleepTime = 0;
Glenn Kasten58912562012-04-03 10:45:00 -07003951 writeFrames = mNormalFrameCount;
Eric Laurenta4f7e0e2012-06-07 17:16:09 -07003952 standbyTime = systemTime() + standbyDelay;
Glenn Kasten000f0e32012-03-01 17:10:56 -08003953}
3954
3955void AudioFlinger::DuplicatingThread::threadLoop_sleepTime()
3956{
Glenn Kasten952eeb22012-03-06 11:30:57 -08003957 if (sleepTime == 0) {
Glenn Kastenfec279f2012-03-08 07:47:15 -08003958 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
Glenn Kasten952eeb22012-03-06 11:30:57 -08003959 sleepTime = activeSleepTime;
3960 } else {
3961 sleepTime = idleSleepTime;
3962 }
3963 } else if (mBytesWritten != 0) {
Eric Laurenta4f7e0e2012-06-07 17:16:09 -07003964 if (mMixerStatus == MIXER_TRACKS_ENABLED) {
3965 writeFrames = mNormalFrameCount;
3966 memset(mMixBuffer, 0, mixBufferSize);
3967 } else {
3968 // flush remaining overflow buffers in output tracks
3969 writeFrames = 0;
Glenn Kasten952eeb22012-03-06 11:30:57 -08003970 }
Eric Laurenta4f7e0e2012-06-07 17:16:09 -07003971 sleepTime = 0;
Glenn Kasten952eeb22012-03-06 11:30:57 -08003972 }
Glenn Kasten000f0e32012-03-01 17:10:56 -08003973}
Mathias Agopian65ab4712010-07-14 17:59:35 -07003974
Glenn Kasten000f0e32012-03-01 17:10:56 -08003975void AudioFlinger::DuplicatingThread::threadLoop_write()
3976{
Glenn Kasten952eeb22012-03-06 11:30:57 -08003977 for (size_t i = 0; i < outputTracks.size(); i++) {
3978 outputTracks[i]->write(mMixBuffer, writeFrames);
3979 }
3980 mBytesWritten += mixBufferSize;
Glenn Kasten000f0e32012-03-01 17:10:56 -08003981}
Glenn Kasten688a6402012-02-29 07:57:06 -08003982
Glenn Kasten000f0e32012-03-01 17:10:56 -08003983void AudioFlinger::DuplicatingThread::threadLoop_standby()
3984{
Glenn Kasten952eeb22012-03-06 11:30:57 -08003985 // DuplicatingThread implements standby by stopping all tracks
3986 for (size_t i = 0; i < outputTracks.size(); i++) {
3987 outputTracks[i]->stop();
3988 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003989}
3990
Glenn Kastenfa26a852012-03-06 11:28:04 -08003991void AudioFlinger::DuplicatingThread::saveOutputTracks()
3992{
3993 outputTracks = mOutputTracks;
3994}
3995
3996void AudioFlinger::DuplicatingThread::clearOutputTracks()
3997{
3998 outputTracks.clear();
3999}
4000
Mathias Agopian65ab4712010-07-14 17:59:35 -07004001void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread)
4002{
Glenn Kastenb6b74062012-02-24 14:12:20 -08004003 Mutex::Autolock _l(mLock);
Glenn Kasten99e53b82012-01-19 08:59:58 -08004004 // FIXME explain this formula
Glenn Kasten58912562012-04-03 10:45:00 -07004005 int frameCount = (3 * mNormalFrameCount * mSampleRate) / thread->sampleRate();
Glenn Kasten9eaa5572012-01-20 13:32:16 -08004006 OutputTrack *outputTrack = new OutputTrack(thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004007 this,
4008 mSampleRate,
4009 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07004010 mChannelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004011 frameCount);
4012 if (outputTrack->cblk() != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -07004013 thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004014 mOutputTracks.add(outputTrack);
Steve Block3856b092011-10-20 11:56:00 +01004015 ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread);
Glenn Kasten438b0362012-03-06 11:24:48 -08004016 updateWaitTime_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07004017 }
4018}
4019
4020void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
4021{
4022 Mutex::Autolock _l(mLock);
4023 for (size_t i = 0; i < mOutputTracks.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -08004024 if (mOutputTracks[i]->thread() == thread) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004025 mOutputTracks[i]->destroy();
4026 mOutputTracks.removeAt(i);
Glenn Kasten438b0362012-03-06 11:24:48 -08004027 updateWaitTime_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07004028 return;
4029 }
4030 }
Steve Block3856b092011-10-20 11:56:00 +01004031 ALOGV("removeOutputTrack(): unkonwn thread: %p", thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004032}
4033
Glenn Kasten438b0362012-03-06 11:24:48 -08004034// caller must hold mLock
4035void AudioFlinger::DuplicatingThread::updateWaitTime_l()
Mathias Agopian65ab4712010-07-14 17:59:35 -07004036{
4037 mWaitTimeMs = UINT_MAX;
4038 for (size_t i = 0; i < mOutputTracks.size(); i++) {
4039 sp<ThreadBase> strong = mOutputTracks[i]->thread().promote();
Glenn Kasten7378ca52012-01-20 13:44:40 -08004040 if (strong != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004041 uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate();
4042 if (waitTimeMs < mWaitTimeMs) {
4043 mWaitTimeMs = waitTimeMs;
4044 }
4045 }
4046 }
4047}
4048
4049
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08004050bool AudioFlinger::DuplicatingThread::outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks)
Mathias Agopian65ab4712010-07-14 17:59:35 -07004051{
4052 for (size_t i = 0; i < outputTracks.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004053 sp<ThreadBase> thread = outputTracks[i]->thread().promote();
Mathias Agopian65ab4712010-07-14 17:59:35 -07004054 if (thread == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00004055 ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07004056 return false;
4057 }
4058 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
Glenn Kasten01542f22012-07-02 12:46:15 -07004059 // see note at standby() declaration
Mathias Agopian65ab4712010-07-14 17:59:35 -07004060 if (playbackThread->standby() && !playbackThread->isSuspended()) {
Steve Block3856b092011-10-20 11:56:00 +01004061 ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07004062 return false;
4063 }
4064 }
4065 return true;
4066}
4067
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08004068uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07004069{
4070 return (mWaitTimeMs * 1000) / 2;
4071}
4072
Glenn Kasten66fcab92012-02-24 14:59:21 -08004073void AudioFlinger::DuplicatingThread::cacheParameters_l()
4074{
4075 // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first
4076 updateWaitTime_l();
4077
4078 MixerThread::cacheParameters_l();
4079}
4080
Mathias Agopian65ab4712010-07-14 17:59:35 -07004081// ----------------------------------------------------------------------------
4082
4083// TrackBase constructor must be called with AudioFlinger::mLock held
4084AudioFlinger::ThreadBase::TrackBase::TrackBase(
Glenn Kasten9eaa5572012-01-20 13:32:16 -08004085 ThreadBase *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004086 const sp<Client>& client,
4087 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08004088 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07004089 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004090 int frameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004091 const sp<IMemory>& sharedBuffer,
4092 int sessionId)
4093 : RefBase(),
4094 mThread(thread),
4095 mClient(client),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08004096 mCblk(NULL),
4097 // mBuffer
4098 // mBufferEnd
Mathias Agopian65ab4712010-07-14 17:59:35 -07004099 mFrameCount(0),
4100 mState(IDLE),
Glenn Kasten58912562012-04-03 10:45:00 -07004101 mSampleRate(sampleRate),
Mathias Agopian65ab4712010-07-14 17:59:35 -07004102 mFormat(format),
Glenn Kasten5cf034d2012-02-21 10:35:56 -08004103 mStepServerFailed(false),
Mathias Agopian65ab4712010-07-14 17:59:35 -07004104 mSessionId(sessionId)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08004105 // mChannelCount
4106 // mChannelMask
Mathias Agopian65ab4712010-07-14 17:59:35 -07004107{
Steve Block3856b092011-10-20 11:56:00 +01004108 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07004109
Steve Blockb8a80522011-12-20 16:23:08 +00004110 // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004111 size_t size = sizeof(audio_track_cblk_t);
4112 uint8_t channelCount = popcount(channelMask);
4113 size_t bufferSize = frameCount*channelCount*sizeof(int16_t);
4114 if (sharedBuffer == 0) {
4115 size += bufferSize;
4116 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07004117
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004118 if (client != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004119 mCblkMemory = client->heap()->allocate(size);
4120 if (mCblkMemory != 0) {
4121 mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer());
Glenn Kastena0d68332012-01-27 16:47:15 -08004122 if (mCblk != NULL) { // construct the shared structure in-place.
Mathias Agopian65ab4712010-07-14 17:59:35 -07004123 new(mCblk) audio_track_cblk_t();
4124 // clear all buffers
4125 mCblk->frameCount = frameCount;
4126 mCblk->sampleRate = sampleRate;
Marco Nelissena1472d92012-03-30 14:36:54 -07004127// uncomment the following lines to quickly test 32-bit wraparound
4128// mCblk->user = 0xffff0000;
4129// mCblk->server = 0xffff0000;
4130// mCblk->userBase = 0xffff0000;
4131// mCblk->serverBase = 0xffff0000;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07004132 mChannelCount = channelCount;
4133 mChannelMask = channelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004134 if (sharedBuffer == 0) {
4135 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
4136 memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
4137 // Force underrun condition to avoid false underrun callback until first data is
Eric Laurent44d98482010-09-30 16:12:31 -07004138 // written to buffer (other flags are cleared)
Mathias Agopian65ab4712010-07-14 17:59:35 -07004139 mCblk->flags = CBLK_UNDERRUN_ON;
4140 } else {
4141 mBuffer = sharedBuffer->pointer();
4142 }
4143 mBufferEnd = (uint8_t *)mBuffer + bufferSize;
4144 }
4145 } else {
Steve Block29357bc2012-01-06 19:20:56 +00004146 ALOGE("not enough memory for AudioTrack size=%u", size);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004147 client->heap()->dump("AudioTrack");
4148 return;
4149 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004150 } else {
4151 mCblk = (audio_track_cblk_t *)(new uint8_t[size]);
Glenn Kastenea7939a2012-03-14 12:56:26 -07004152 // construct the shared structure in-place.
4153 new(mCblk) audio_track_cblk_t();
4154 // clear all buffers
4155 mCblk->frameCount = frameCount;
4156 mCblk->sampleRate = sampleRate;
Marco Nelissena1472d92012-03-30 14:36:54 -07004157// uncomment the following lines to quickly test 32-bit wraparound
4158// mCblk->user = 0xffff0000;
4159// mCblk->server = 0xffff0000;
4160// mCblk->userBase = 0xffff0000;
4161// mCblk->serverBase = 0xffff0000;
Glenn Kastenea7939a2012-03-14 12:56:26 -07004162 mChannelCount = channelCount;
4163 mChannelMask = channelMask;
4164 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
4165 memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
4166 // Force underrun condition to avoid false underrun callback until first data is
4167 // written to buffer (other flags are cleared)
4168 mCblk->flags = CBLK_UNDERRUN_ON;
4169 mBufferEnd = (uint8_t *)mBuffer + bufferSize;
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004170 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07004171}
4172
4173AudioFlinger::ThreadBase::TrackBase::~TrackBase()
4174{
Glenn Kastena0d68332012-01-27 16:47:15 -08004175 if (mCblk != NULL) {
Glenn Kasten1a0ae5b2012-02-03 10:24:48 -08004176 if (mClient == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004177 delete mCblk;
Glenn Kasten1a0ae5b2012-02-03 10:24:48 -08004178 } else {
4179 mCblk->~audio_track_cblk_t(); // destroy our shared-structure.
Mathias Agopian65ab4712010-07-14 17:59:35 -07004180 }
4181 }
Glenn Kastendbfafaf2012-01-25 15:27:15 -08004182 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Glenn Kasten7378ca52012-01-20 13:44:40 -08004183 if (mClient != 0) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08004184 // Client destructor must run with AudioFlinger mutex locked
Mathias Agopian65ab4712010-07-14 17:59:35 -07004185 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
Glenn Kasten7378ca52012-01-20 13:44:40 -08004186 // If the client's reference count drops to zero, the associated destructor
4187 // must run with AudioFlinger lock held. Thus the explicit clear() rather than
4188 // relying on the automatic clear() at end of scope.
Mathias Agopian65ab4712010-07-14 17:59:35 -07004189 mClient.clear();
4190 }
4191}
4192
Glenn Kasten01c4ebf2012-02-22 10:47:35 -08004193// AudioBufferProvider interface
4194// getNextBuffer() = 0;
4195// This implementation of releaseBuffer() is used by Track and RecordTrack, but not TimedTrack
Mathias Agopian65ab4712010-07-14 17:59:35 -07004196void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer)
4197{
Glenn Kastene0feee32011-12-13 11:53:26 -08004198 buffer->raw = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004199 mFrameCount = buffer->frameCount;
Glenn Kasten288ed212012-04-25 17:52:27 -07004200 // FIXME See note at getNextBuffer()
Glenn Kasten01c4ebf2012-02-22 10:47:35 -08004201 (void) step(); // ignore return value of step()
Mathias Agopian65ab4712010-07-14 17:59:35 -07004202 buffer->frameCount = 0;
4203}
4204
4205bool AudioFlinger::ThreadBase::TrackBase::step() {
4206 bool result;
4207 audio_track_cblk_t* cblk = this->cblk();
4208
4209 result = cblk->stepServer(mFrameCount);
4210 if (!result) {
Steve Block3856b092011-10-20 11:56:00 +01004211 ALOGV("stepServer failed acquiring cblk mutex");
Glenn Kasten5cf034d2012-02-21 10:35:56 -08004212 mStepServerFailed = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004213 }
4214 return result;
4215}
4216
4217void AudioFlinger::ThreadBase::TrackBase::reset() {
4218 audio_track_cblk_t* cblk = this->cblk();
4219
4220 cblk->user = 0;
4221 cblk->server = 0;
4222 cblk->userBase = 0;
4223 cblk->serverBase = 0;
Glenn Kasten5cf034d2012-02-21 10:35:56 -08004224 mStepServerFailed = false;
Steve Block3856b092011-10-20 11:56:00 +01004225 ALOGV("TrackBase::reset");
Mathias Agopian65ab4712010-07-14 17:59:35 -07004226}
4227
Mathias Agopian65ab4712010-07-14 17:59:35 -07004228int AudioFlinger::ThreadBase::TrackBase::sampleRate() const {
4229 return (int)mCblk->sampleRate;
4230}
4231
Mathias Agopian65ab4712010-07-14 17:59:35 -07004232void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const {
4233 audio_track_cblk_t* cblk = this->cblk();
Glenn Kastenb9980652012-01-11 09:48:27 -08004234 size_t frameSize = cblk->frameSize;
4235 int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize;
4236 int8_t *bufferEnd = bufferStart + frames * frameSize;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004237
4238 // Check validity of returned pointer in case the track control block would have been corrupted.
Jean-Michel Triviacb86cc2012-04-16 12:43:57 -07004239 ALOG_ASSERT(!(bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd),
4240 "TrackBase::getBuffer buffer out of range:\n"
4241 " start: %p, end %p , mBuffer %p mBufferEnd %p\n"
4242 " server %u, serverBase %u, user %u, userBase %u, frameSize %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07004243 bufferStart, bufferEnd, mBuffer, mBufferEnd,
Jean-Michel Triviacb86cc2012-04-16 12:43:57 -07004244 cblk->server, cblk->serverBase, cblk->user, cblk->userBase, frameSize);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004245
4246 return bufferStart;
4247}
4248
Eric Laurenta011e352012-03-29 15:51:43 -07004249status_t AudioFlinger::ThreadBase::TrackBase::setSyncEvent(const sp<SyncEvent>& event)
4250{
4251 mSyncEvents.add(event);
4252 return NO_ERROR;
4253}
4254
Mathias Agopian65ab4712010-07-14 17:59:35 -07004255// ----------------------------------------------------------------------------
4256
4257// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
4258AudioFlinger::PlaybackThread::Track::Track(
Glenn Kasten9eaa5572012-01-20 13:32:16 -08004259 PlaybackThread *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004260 const sp<Client>& client,
Glenn Kastenfff6d712012-01-12 16:38:12 -08004261 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004262 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08004263 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07004264 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004265 int frameCount,
4266 const sp<IMemory>& sharedBuffer,
Glenn Kasten73d22752012-03-19 13:38:30 -07004267 int sessionId,
4268 IAudioFlinger::track_flags_t flags)
Glenn Kasten5cf034d2012-02-21 10:35:56 -08004269 : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer, sessionId),
Glenn Kastenf9959012012-03-19 11:14:37 -07004270 mMute(false),
Glenn Kasten58912562012-04-03 10:45:00 -07004271 mFillingUpStatus(FS_INVALID),
Glenn Kastenf9959012012-03-19 11:14:37 -07004272 // mRetryCount initialized later when needed
4273 mSharedBuffer(sharedBuffer),
4274 mStreamType(streamType),
4275 mName(-1), // see note below
4276 mMainBuffer(thread->mixBuffer()),
4277 mAuxBuffer(NULL),
Eric Laurenta011e352012-03-29 15:51:43 -07004278 mAuxEffectId(0), mHasVolumeController(false),
Glenn Kasten73d22752012-03-19 13:38:30 -07004279 mPresentationCompleteFrames(0),
Glenn Kasten58912562012-04-03 10:45:00 -07004280 mFlags(flags),
4281 mFastIndex(-1),
Glenn Kasten288ed212012-04-25 17:52:27 -07004282 mUnderrunCount(0),
Glenn Kasten58912562012-04-03 10:45:00 -07004283 mCachedVolume(1.0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07004284{
4285 if (mCblk != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004286 // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of
4287 // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack
Eric Laurentedc15ad2011-07-21 19:35:01 -07004288 mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t);
Glenn Kasten893a0542012-05-30 10:32:06 -07004289 // to avoid leaking a track name, do not allocate one unless there is an mCblk
Glenn Kasten254af182012-07-03 14:59:05 -07004290 mName = thread->getTrackName_l(channelMask);
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07004291 mCblk->mName = mName;
Glenn Kasten893a0542012-05-30 10:32:06 -07004292 if (mName < 0) {
4293 ALOGE("no more track names available");
4294 return;
4295 }
4296 // only allocate a fast track index if we were able to allocate a normal track name
Glenn Kasten58912562012-04-03 10:45:00 -07004297 if (flags & IAudioFlinger::TRACK_FAST) {
4298 mCblk->flags |= CBLK_FAST; // atomic op not needed yet
4299 ALOG_ASSERT(thread->mFastTrackAvailMask != 0);
4300 int i = __builtin_ctz(thread->mFastTrackAvailMask);
Eric Laurent29864602012-05-08 18:57:51 -07004301 ALOG_ASSERT(0 < i && i < (int)FastMixerState::kMaxFastTracks);
Glenn Kasten288ed212012-04-25 17:52:27 -07004302 // FIXME This is too eager. We allocate a fast track index before the
4303 // fast track becomes active. Since fast tracks are a scarce resource,
4304 // this means we are potentially denying other more important fast tracks from
4305 // being created. It would be better to allocate the index dynamically.
Glenn Kasten58912562012-04-03 10:45:00 -07004306 mFastIndex = i;
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07004307 mCblk->mName = i;
Glenn Kasten288ed212012-04-25 17:52:27 -07004308 // Read the initial underruns because this field is never cleared by the fast mixer
Glenn Kasten09474df2012-05-10 14:48:07 -07004309 mObservedUnderruns = thread->getFastTrackUnderruns(i);
Glenn Kasten58912562012-04-03 10:45:00 -07004310 thread->mFastTrackAvailMask &= ~(1 << i);
Glenn Kasten58912562012-04-03 10:45:00 -07004311 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07004312 }
Glenn Kastenf9959012012-03-19 11:14:37 -07004313 ALOGV("Track constructor name %d, calling pid %d", mName, IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07004314}
4315
4316AudioFlinger::PlaybackThread::Track::~Track()
4317{
Steve Block3856b092011-10-20 11:56:00 +01004318 ALOGV("PlaybackThread::Track destructor");
Mathias Agopian65ab4712010-07-14 17:59:35 -07004319}
4320
4321void AudioFlinger::PlaybackThread::Track::destroy()
4322{
4323 // NOTE: destroyTrack_l() can remove a strong reference to this Track
4324 // by removing it from mTracks vector, so there is a risk that this Tracks's
Glenn Kasten99e53b82012-01-19 08:59:58 -08004325 // destructor is called. As the destructor needs to lock mLock,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004326 // we must acquire a strong reference on this Track before locking mLock
4327 // here so that the destructor is called only when exiting this function.
4328 // On the other hand, as long as Track::destroy() is only called by
4329 // TrackHandle destructor, the TrackHandle still holds a strong ref on
4330 // this Track with its member mTrack.
4331 sp<Track> keep(this);
4332 { // scope for mLock
4333 sp<ThreadBase> thread = mThread.promote();
4334 if (thread != 0) {
4335 if (!isOutputTrack()) {
4336 if (mState == ACTIVE || mState == RESUMING) {
Glenn Kasten02bbd202012-02-08 12:35:35 -08004337 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
Gloria Wang9ee159b2011-02-24 14:51:45 -08004338
Glenn Kastend3cee2f2012-03-13 17:55:35 -07004339#ifdef ADD_BATTERY_DATA
Gloria Wang9ee159b2011-02-24 14:51:45 -08004340 // to track the speaker usage
4341 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Glenn Kastend3cee2f2012-03-13 17:55:35 -07004342#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07004343 }
4344 AudioSystem::releaseOutput(thread->id());
4345 }
4346 Mutex::Autolock _l(thread->mLock);
4347 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
4348 playbackThread->destroyTrack_l(this);
4349 }
4350 }
4351}
4352
Glenn Kasten288ed212012-04-25 17:52:27 -07004353/*static*/ void AudioFlinger::PlaybackThread::Track::appendDumpHeader(String8& result)
4354{
Glenn Kastene213c862012-04-25 13:46:15 -07004355 result.append(" Name Client Type Fmt Chn mask Session mFrCnt fCount S M F SRate L dB R dB "
Glenn Kastenbf0d21f2012-05-31 14:59:29 -07004356 " Server User Main buf Aux Buf Flags Underruns\n");
Glenn Kasten288ed212012-04-25 17:52:27 -07004357}
4358
Mathias Agopian65ab4712010-07-14 17:59:35 -07004359void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size)
4360{
Glenn Kasten83d86532012-01-17 14:39:34 -08004361 uint32_t vlr = mCblk->getVolumeLR();
Glenn Kasten58912562012-04-03 10:45:00 -07004362 if (isFastTrack()) {
Glenn Kasten288ed212012-04-25 17:52:27 -07004363 sprintf(buffer, " F %2d", mFastIndex);
Glenn Kasten58912562012-04-03 10:45:00 -07004364 } else {
4365 sprintf(buffer, " %4d", mName - AudioMixer::TRACK0);
4366 }
Glenn Kasten288ed212012-04-25 17:52:27 -07004367 track_state state = mState;
4368 char stateChar;
4369 switch (state) {
4370 case IDLE:
4371 stateChar = 'I';
4372 break;
4373 case TERMINATED:
4374 stateChar = 'T';
4375 break;
Glenn Kastend08f48c2012-05-01 18:14:02 -07004376 case STOPPING_1:
4377 stateChar = 's';
4378 break;
4379 case STOPPING_2:
4380 stateChar = '5';
4381 break;
Glenn Kasten288ed212012-04-25 17:52:27 -07004382 case STOPPED:
4383 stateChar = 'S';
4384 break;
4385 case RESUMING:
4386 stateChar = 'R';
4387 break;
4388 case ACTIVE:
4389 stateChar = 'A';
4390 break;
4391 case PAUSING:
4392 stateChar = 'p';
4393 break;
4394 case PAUSED:
4395 stateChar = 'P';
4396 break;
Eric Laurent29864602012-05-08 18:57:51 -07004397 case FLUSHED:
4398 stateChar = 'F';
4399 break;
Glenn Kasten288ed212012-04-25 17:52:27 -07004400 default:
4401 stateChar = '?';
4402 break;
4403 }
Glenn Kasten09474df2012-05-10 14:48:07 -07004404 char nowInUnderrun;
4405 switch (mObservedUnderruns.mBitFields.mMostRecent) {
4406 case UNDERRUN_FULL:
4407 nowInUnderrun = ' ';
4408 break;
4409 case UNDERRUN_PARTIAL:
4410 nowInUnderrun = '<';
4411 break;
4412 case UNDERRUN_EMPTY:
4413 nowInUnderrun = '*';
4414 break;
4415 default:
4416 nowInUnderrun = '?';
4417 break;
4418 }
Glenn Kastene213c862012-04-25 13:46:15 -07004419 snprintf(&buffer[7], size-7, " %6d %4u %3u 0x%08x %7u %6u %6u %1c %1d %1d %5u %5.2g %5.2g "
4420 "0x%08x 0x%08x 0x%08x 0x%08x %#5x %9u%c\n",
Glenn Kasten44deb052012-02-05 18:09:08 -08004421 (mClient == 0) ? getpid_cached : mClient->pid(),
Mathias Agopian65ab4712010-07-14 17:59:35 -07004422 mStreamType,
4423 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07004424 mChannelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004425 mSessionId,
4426 mFrameCount,
Glenn Kastene213c862012-04-25 13:46:15 -07004427 mCblk->frameCount,
Glenn Kasten288ed212012-04-25 17:52:27 -07004428 stateChar,
Mathias Agopian65ab4712010-07-14 17:59:35 -07004429 mMute,
4430 mFillingUpStatus,
4431 mCblk->sampleRate,
Glenn Kasten58912562012-04-03 10:45:00 -07004432 20.0 * log10((vlr & 0xFFFF) / 4096.0),
4433 20.0 * log10((vlr >> 16) / 4096.0),
Mathias Agopian65ab4712010-07-14 17:59:35 -07004434 mCblk->server,
4435 mCblk->user,
4436 (int)mMainBuffer,
Glenn Kasten288ed212012-04-25 17:52:27 -07004437 (int)mAuxBuffer,
Glenn Kastene213c862012-04-25 13:46:15 -07004438 mCblk->flags,
Glenn Kasten288ed212012-04-25 17:52:27 -07004439 mUnderrunCount,
Glenn Kasten09474df2012-05-10 14:48:07 -07004440 nowInUnderrun);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004441}
4442
Glenn Kasten01c4ebf2012-02-22 10:47:35 -08004443// AudioBufferProvider interface
John Grossman4ff14ba2012-02-08 16:37:41 -08004444status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004445 AudioBufferProvider::Buffer* buffer, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07004446{
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004447 audio_track_cblk_t* cblk = this->cblk();
4448 uint32_t framesReady;
4449 uint32_t framesReq = buffer->frameCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004450
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004451 // Check if last stepServer failed, try to step now
4452 if (mStepServerFailed) {
Glenn Kasten288ed212012-04-25 17:52:27 -07004453 // FIXME When called by fast mixer, this takes a mutex with tryLock().
4454 // Since the fast mixer is higher priority than client callback thread,
4455 // it does not result in priority inversion for client.
4456 // But a non-blocking solution would be preferable to avoid
4457 // fast mixer being unable to tryLock(), and
4458 // to avoid the extra context switches if the client wakes up,
4459 // discovers the mutex is locked, then has to wait for fast mixer to unlock.
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004460 if (!step()) goto getNextBuffer_exit;
4461 ALOGV("stepServer recovered");
4462 mStepServerFailed = false;
4463 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07004464
Glenn Kasten288ed212012-04-25 17:52:27 -07004465 // FIXME Same as above
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004466 framesReady = cblk->framesReady();
Mathias Agopian65ab4712010-07-14 17:59:35 -07004467
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004468 if (CC_LIKELY(framesReady)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004469 uint32_t s = cblk->server;
4470 uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
4471
4472 bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd;
4473 if (framesReq > framesReady) {
4474 framesReq = framesReady;
4475 }
Marco Nelissena1472d92012-03-30 14:36:54 -07004476 if (framesReq > bufferEnd - s) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004477 framesReq = bufferEnd - s;
4478 }
4479
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004480 buffer->raw = getBuffer(s, framesReq);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004481 buffer->frameCount = framesReq;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004482 return NO_ERROR;
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004483 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07004484
4485getNextBuffer_exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004486 buffer->raw = NULL;
4487 buffer->frameCount = 0;
4488 ALOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get());
4489 return NOT_ENOUGH_DATA;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004490}
4491
Glenn Kasten288ed212012-04-25 17:52:27 -07004492// Note that framesReady() takes a mutex on the control block using tryLock().
4493// This could result in priority inversion if framesReady() is called by the normal mixer,
4494// as the normal mixer thread runs at lower
4495// priority than the client's callback thread: there is a short window within framesReady()
4496// during which the normal mixer could be preempted, and the client callback would block.
4497// Another problem can occur if framesReady() is called by the fast mixer:
4498// the tryLock() could block for up to 1 ms, and a sequence of these could delay fast mixer.
4499// FIXME Replace AudioTrackShared control block implementation by a non-blocking FIFO queue.
4500size_t AudioFlinger::PlaybackThread::Track::framesReady() const {
John Grossman4ff14ba2012-02-08 16:37:41 -08004501 return mCblk->framesReady();
4502}
4503
Glenn Kasten288ed212012-04-25 17:52:27 -07004504// Don't call for fast tracks; the framesReady() could result in priority inversion
Mathias Agopian65ab4712010-07-14 17:59:35 -07004505bool AudioFlinger::PlaybackThread::Track::isReady() const {
Eric Laurentaf59ce22010-10-05 14:41:42 -07004506 if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004507
John Grossman4ff14ba2012-02-08 16:37:41 -08004508 if (framesReady() >= mCblk->frameCount ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07004509 (mCblk->flags & CBLK_FORCEREADY_MSK)) {
4510 mFillingUpStatus = FS_FILLED;
Eric Laurent38ccae22011-03-28 18:37:07 -07004511 android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004512 return true;
4513 }
4514 return false;
4515}
4516
Glenn Kasten3acbd052012-02-28 10:39:56 -08004517status_t AudioFlinger::PlaybackThread::Track::start(AudioSystem::sync_event_t event,
Eric Laurenta011e352012-03-29 15:51:43 -07004518 int triggerSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -07004519{
4520 status_t status = NO_ERROR;
Glenn Kasten58912562012-04-03 10:45:00 -07004521 ALOGV("start(%d), calling pid %d session %d",
4522 mName, IPCThreadState::self()->getCallingPid(), mSessionId);
Glenn Kasten3acbd052012-02-28 10:39:56 -08004523
Mathias Agopian65ab4712010-07-14 17:59:35 -07004524 sp<ThreadBase> thread = mThread.promote();
4525 if (thread != 0) {
4526 Mutex::Autolock _l(thread->mLock);
Glenn Kastenb853e982012-01-26 13:39:18 -08004527 track_state state = mState;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004528 // here the track could be either new, or restarted
4529 // in both cases "unstop" the track
4530 if (mState == PAUSED) {
4531 mState = TrackBase::RESUMING;
Steve Block3856b092011-10-20 11:56:00 +01004532 ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004533 } else {
4534 mState = TrackBase::ACTIVE;
Steve Block3856b092011-10-20 11:56:00 +01004535 ALOGV("? => ACTIVE (%d) on thread %p", mName, this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004536 }
4537
4538 if (!isOutputTrack() && state != ACTIVE && state != RESUMING) {
4539 thread->mLock.unlock();
Glenn Kasten02bbd202012-02-08 12:35:35 -08004540 status = AudioSystem::startOutput(thread->id(), mStreamType, mSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004541 thread->mLock.lock();
Gloria Wang9ee159b2011-02-24 14:51:45 -08004542
Glenn Kastend3cee2f2012-03-13 17:55:35 -07004543#ifdef ADD_BATTERY_DATA
Gloria Wang9ee159b2011-02-24 14:51:45 -08004544 // to track the speaker usage
4545 if (status == NO_ERROR) {
4546 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart);
4547 }
Glenn Kastend3cee2f2012-03-13 17:55:35 -07004548#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07004549 }
4550 if (status == NO_ERROR) {
4551 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
4552 playbackThread->addTrack_l(this);
4553 } else {
4554 mState = state;
Eric Laurent29864602012-05-08 18:57:51 -07004555 triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004556 }
4557 } else {
4558 status = BAD_VALUE;
4559 }
4560 return status;
4561}
4562
4563void AudioFlinger::PlaybackThread::Track::stop()
4564{
Glenn Kasten23d82a92012-02-03 11:10:00 -08004565 ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07004566 sp<ThreadBase> thread = mThread.promote();
4567 if (thread != 0) {
4568 Mutex::Autolock _l(thread->mLock);
Glenn Kastenb853e982012-01-26 13:39:18 -08004569 track_state state = mState;
Glenn Kastend08f48c2012-05-01 18:14:02 -07004570 if (state == RESUMING || state == ACTIVE || state == PAUSING || state == PAUSED) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004571 // If the track is not active (PAUSED and buffers full), flush buffers
4572 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
4573 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
4574 reset();
Glenn Kastend08f48c2012-05-01 18:14:02 -07004575 mState = STOPPED;
4576 } else if (!isFastTrack()) {
4577 mState = STOPPED;
4578 } else {
4579 // prepareTracks_l() will set state to STOPPING_2 after next underrun,
4580 // and then to STOPPED and reset() when presentation is complete
4581 mState = STOPPING_1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07004582 }
Glenn Kastend08f48c2012-05-01 18:14:02 -07004583 ALOGV("not stopping/stopped => stopping/stopped (%d) on thread %p", mName, playbackThread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004584 }
4585 if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) {
4586 thread->mLock.unlock();
Glenn Kasten02bbd202012-02-08 12:35:35 -08004587 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004588 thread->mLock.lock();
Gloria Wang9ee159b2011-02-24 14:51:45 -08004589
Glenn Kastend3cee2f2012-03-13 17:55:35 -07004590#ifdef ADD_BATTERY_DATA
Gloria Wang9ee159b2011-02-24 14:51:45 -08004591 // to track the speaker usage
4592 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Glenn Kastend3cee2f2012-03-13 17:55:35 -07004593#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07004594 }
4595 }
4596}
4597
4598void AudioFlinger::PlaybackThread::Track::pause()
4599{
Glenn Kasten23d82a92012-02-03 11:10:00 -08004600 ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07004601 sp<ThreadBase> thread = mThread.promote();
4602 if (thread != 0) {
4603 Mutex::Autolock _l(thread->mLock);
4604 if (mState == ACTIVE || mState == RESUMING) {
4605 mState = PAUSING;
Steve Block3856b092011-10-20 11:56:00 +01004606 ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07004607 if (!isOutputTrack()) {
4608 thread->mLock.unlock();
Glenn Kasten02bbd202012-02-08 12:35:35 -08004609 AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004610 thread->mLock.lock();
Gloria Wang9ee159b2011-02-24 14:51:45 -08004611
Glenn Kastend3cee2f2012-03-13 17:55:35 -07004612#ifdef ADD_BATTERY_DATA
Gloria Wang9ee159b2011-02-24 14:51:45 -08004613 // to track the speaker usage
4614 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
Glenn Kastend3cee2f2012-03-13 17:55:35 -07004615#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07004616 }
4617 }
4618 }
4619}
4620
4621void AudioFlinger::PlaybackThread::Track::flush()
4622{
Steve Block3856b092011-10-20 11:56:00 +01004623 ALOGV("flush(%d)", mName);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004624 sp<ThreadBase> thread = mThread.promote();
4625 if (thread != 0) {
4626 Mutex::Autolock _l(thread->mLock);
Glenn Kastend08f48c2012-05-01 18:14:02 -07004627 if (mState != STOPPING_1 && mState != STOPPING_2 && mState != STOPPED && mState != PAUSED &&
4628 mState != PAUSING) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07004629 return;
4630 }
4631 // No point remaining in PAUSED state after a flush => go to
Eric Laurent29864602012-05-08 18:57:51 -07004632 // FLUSHED state
4633 mState = FLUSHED;
Eric Laurent38ccae22011-03-28 18:37:07 -07004634 // do not reset the track if it is still in the process of being stopped or paused.
4635 // this will be done by prepareTracks_l() when the track is stopped.
Eric Laurent29864602012-05-08 18:57:51 -07004636 // prepareTracks_l() will see mState == FLUSHED, then
Glenn Kastend08f48c2012-05-01 18:14:02 -07004637 // remove from active track list, reset(), and trigger presentation complete
Eric Laurent38ccae22011-03-28 18:37:07 -07004638 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
4639 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
4640 reset();
4641 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07004642 }
4643}
4644
4645void AudioFlinger::PlaybackThread::Track::reset()
4646{
4647 // Do not reset twice to avoid discarding data written just after a flush and before
4648 // the audioflinger thread detects the track is stopped.
4649 if (!mResetDone) {
4650 TrackBase::reset();
4651 // Force underrun condition to avoid false underrun callback until first data is
4652 // written to buffer
Eric Laurent38ccae22011-03-28 18:37:07 -07004653 android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags);
4654 android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004655 mFillingUpStatus = FS_FILLING;
4656 mResetDone = true;
Eric Laurent29864602012-05-08 18:57:51 -07004657 if (mState == FLUSHED) {
4658 mState = IDLE;
4659 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07004660 }
4661}
4662
4663void AudioFlinger::PlaybackThread::Track::mute(bool muted)
4664{
4665 mMute = muted;
4666}
4667
Mathias Agopian65ab4712010-07-14 17:59:35 -07004668status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId)
4669{
4670 status_t status = DEAD_OBJECT;
4671 sp<ThreadBase> thread = mThread.promote();
4672 if (thread != 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004673 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
Eric Laurent717e1282012-06-29 16:36:52 -07004674 sp<AudioFlinger> af = mClient->audioFlinger();
4675
4676 Mutex::Autolock _l(af->mLock);
4677
4678 sp<PlaybackThread> srcThread = af->getEffectThread_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
Eric Laurent717e1282012-06-29 16:36:52 -07004679
Eric Laurent109347d2012-07-02 12:31:03 -07004680 if (EffectId != 0 && srcThread != 0 && playbackThread != srcThread.get()) {
Eric Laurent717e1282012-06-29 16:36:52 -07004681 Mutex::Autolock _dl(playbackThread->mLock);
4682 Mutex::Autolock _sl(srcThread->mLock);
4683 sp<EffectChain> chain = srcThread->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
4684 if (chain == 0) {
4685 return INVALID_OPERATION;
4686 }
4687
4688 sp<EffectModule> effect = chain->getEffectFromId_l(EffectId);
4689 if (effect == 0) {
4690 return INVALID_OPERATION;
4691 }
4692 srcThread->removeEffect_l(effect);
4693 playbackThread->addEffect_l(effect);
4694 // removeEffect_l() has stopped the effect if it was active so it must be restarted
4695 if (effect->state() == EffectModule::ACTIVE ||
4696 effect->state() == EffectModule::STOPPING) {
4697 effect->start();
4698 }
4699
4700 sp<EffectChain> dstChain = effect->chain().promote();
4701 if (dstChain == 0) {
4702 srcThread->addEffect_l(effect);
4703 return INVALID_OPERATION;
4704 }
4705 AudioSystem::unregisterEffect(effect->id());
4706 AudioSystem::registerEffect(&effect->desc(),
4707 srcThread->id(),
4708 dstChain->strategy(),
4709 AUDIO_SESSION_OUTPUT_MIX,
4710 effect->id());
4711 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -07004712 status = playbackThread->attachAuxEffect(this, EffectId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07004713 }
4714 return status;
4715}
4716
4717void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer)
4718{
4719 mAuxEffectId = EffectId;
4720 mAuxBuffer = buffer;
4721}
4722
Eric Laurenta011e352012-03-29 15:51:43 -07004723bool AudioFlinger::PlaybackThread::Track::presentationComplete(size_t framesWritten,
4724 size_t audioHalFrames)
4725{
4726 // a track is considered presented when the total number of frames written to audio HAL
4727 // corresponds to the number of frames written when presentationComplete() is called for the
4728 // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time.
4729 if (mPresentationCompleteFrames == 0) {
4730 mPresentationCompleteFrames = framesWritten + audioHalFrames;
4731 ALOGV("presentationComplete() reset: mPresentationCompleteFrames %d audioHalFrames %d",
4732 mPresentationCompleteFrames, audioHalFrames);
4733 }
4734 if (framesWritten >= mPresentationCompleteFrames) {
4735 ALOGV("presentationComplete() session %d complete: framesWritten %d",
4736 mSessionId, framesWritten);
4737 triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
Eric Laurenta011e352012-03-29 15:51:43 -07004738 return true;
4739 }
4740 return false;
4741}
4742
4743void AudioFlinger::PlaybackThread::Track::triggerEvents(AudioSystem::sync_event_t type)
4744{
4745 for (int i = 0; i < (int)mSyncEvents.size(); i++) {
4746 if (mSyncEvents[i]->type() == type) {
4747 mSyncEvents[i]->trigger();
4748 mSyncEvents.removeAt(i);
4749 i--;
4750 }
4751 }
4752}
4753
Glenn Kasten58912562012-04-03 10:45:00 -07004754// implement VolumeBufferProvider interface
4755
4756uint32_t AudioFlinger::PlaybackThread::Track::getVolumeLR()
4757{
4758 // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs
4759 ALOG_ASSERT(isFastTrack() && (mCblk != NULL));
4760 uint32_t vlr = mCblk->getVolumeLR();
4761 uint32_t vl = vlr & 0xFFFF;
4762 uint32_t vr = vlr >> 16;
4763 // track volumes come from shared memory, so can't be trusted and must be clamped
4764 if (vl > MAX_GAIN_INT) {
4765 vl = MAX_GAIN_INT;
4766 }
4767 if (vr > MAX_GAIN_INT) {
4768 vr = MAX_GAIN_INT;
4769 }
4770 // now apply the cached master volume and stream type volume;
4771 // this is trusted but lacks any synchronization or barrier so may be stale
4772 float v = mCachedVolume;
4773 vl *= v;
4774 vr *= v;
4775 // re-combine into U4.16
4776 vlr = (vr << 16) | (vl & 0xFFFF);
4777 // FIXME look at mute, pause, and stop flags
4778 return vlr;
4779}
Eric Laurenta011e352012-03-29 15:51:43 -07004780
Eric Laurent29864602012-05-08 18:57:51 -07004781status_t AudioFlinger::PlaybackThread::Track::setSyncEvent(const sp<SyncEvent>& event)
4782{
4783 if (mState == TERMINATED || mState == PAUSED ||
4784 ((framesReady() == 0) && ((mSharedBuffer != 0) ||
4785 (mState == STOPPED)))) {
4786 ALOGW("Track::setSyncEvent() in invalid state %d on session %d %s mode, framesReady %d ",
4787 mState, mSessionId, (mSharedBuffer != 0) ? "static" : "stream", framesReady());
4788 event->cancel();
4789 return INVALID_OPERATION;
4790 }
4791 TrackBase::setSyncEvent(event);
4792 return NO_ERROR;
4793}
4794
John Grossman4ff14ba2012-02-08 16:37:41 -08004795// timed audio tracks
4796
4797sp<AudioFlinger::PlaybackThread::TimedTrack>
4798AudioFlinger::PlaybackThread::TimedTrack::create(
Glenn Kasten9eaa5572012-01-20 13:32:16 -08004799 PlaybackThread *thread,
John Grossman4ff14ba2012-02-08 16:37:41 -08004800 const sp<Client>& client,
4801 audio_stream_type_t streamType,
4802 uint32_t sampleRate,
4803 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07004804 audio_channel_mask_t channelMask,
John Grossman4ff14ba2012-02-08 16:37:41 -08004805 int frameCount,
4806 const sp<IMemory>& sharedBuffer,
4807 int sessionId) {
4808 if (!client->reserveTimedTrack())
Glenn Kastend5903ec2012-03-18 10:33:27 -07004809 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -08004810
Glenn Kastena0356762012-03-19 10:38:51 -07004811 return new TimedTrack(
John Grossman4ff14ba2012-02-08 16:37:41 -08004812 thread, client, streamType, sampleRate, format, channelMask, frameCount,
4813 sharedBuffer, sessionId);
John Grossman4ff14ba2012-02-08 16:37:41 -08004814}
4815
4816AudioFlinger::PlaybackThread::TimedTrack::TimedTrack(
Glenn Kasten9eaa5572012-01-20 13:32:16 -08004817 PlaybackThread *thread,
John Grossman4ff14ba2012-02-08 16:37:41 -08004818 const sp<Client>& client,
4819 audio_stream_type_t streamType,
4820 uint32_t sampleRate,
4821 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07004822 audio_channel_mask_t channelMask,
John Grossman4ff14ba2012-02-08 16:37:41 -08004823 int frameCount,
4824 const sp<IMemory>& sharedBuffer,
4825 int sessionId)
4826 : Track(thread, client, streamType, sampleRate, format, channelMask,
Glenn Kasten73d22752012-03-19 13:38:30 -07004827 frameCount, sharedBuffer, sessionId, IAudioFlinger::TRACK_TIMED),
John Grossman9fbdee12012-03-26 17:51:46 -07004828 mQueueHeadInFlight(false),
4829 mTrimQueueHeadOnRelease(false),
John Grossman1c345192012-03-27 14:00:17 -07004830 mFramesPendingInQueue(0),
John Grossman4ff14ba2012-02-08 16:37:41 -08004831 mTimedSilenceBuffer(NULL),
4832 mTimedSilenceBufferSize(0),
4833 mTimedAudioOutputOnTime(false),
4834 mMediaTimeTransformValid(false)
4835{
4836 LocalClock lc;
4837 mLocalTimeFreq = lc.getLocalFreq();
4838
4839 mLocalTimeToSampleTransform.a_zero = 0;
4840 mLocalTimeToSampleTransform.b_zero = 0;
4841 mLocalTimeToSampleTransform.a_to_b_numer = sampleRate;
4842 mLocalTimeToSampleTransform.a_to_b_denom = mLocalTimeFreq;
4843 LinearTransform::reduce(&mLocalTimeToSampleTransform.a_to_b_numer,
4844 &mLocalTimeToSampleTransform.a_to_b_denom);
John Grossman9fbdee12012-03-26 17:51:46 -07004845
4846 mMediaTimeToSampleTransform.a_zero = 0;
4847 mMediaTimeToSampleTransform.b_zero = 0;
4848 mMediaTimeToSampleTransform.a_to_b_numer = sampleRate;
4849 mMediaTimeToSampleTransform.a_to_b_denom = 1000000;
4850 LinearTransform::reduce(&mMediaTimeToSampleTransform.a_to_b_numer,
4851 &mMediaTimeToSampleTransform.a_to_b_denom);
John Grossman4ff14ba2012-02-08 16:37:41 -08004852}
4853
4854AudioFlinger::PlaybackThread::TimedTrack::~TimedTrack() {
4855 mClient->releaseTimedTrack();
4856 delete [] mTimedSilenceBuffer;
4857}
4858
4859status_t AudioFlinger::PlaybackThread::TimedTrack::allocateTimedBuffer(
4860 size_t size, sp<IMemory>* buffer) {
4861
4862 Mutex::Autolock _l(mTimedBufferQueueLock);
4863
4864 trimTimedBufferQueue_l();
4865
4866 // lazily initialize the shared memory heap for timed buffers
4867 if (mTimedMemoryDealer == NULL) {
4868 const int kTimedBufferHeapSize = 512 << 10;
4869
4870 mTimedMemoryDealer = new MemoryDealer(kTimedBufferHeapSize,
4871 "AudioFlingerTimed");
4872 if (mTimedMemoryDealer == NULL)
4873 return NO_MEMORY;
4874 }
4875
4876 sp<IMemory> newBuffer = mTimedMemoryDealer->allocate(size);
4877 if (newBuffer == NULL) {
4878 newBuffer = mTimedMemoryDealer->allocate(size);
4879 if (newBuffer == NULL)
4880 return NO_MEMORY;
4881 }
4882
4883 *buffer = newBuffer;
4884 return NO_ERROR;
4885}
4886
4887// caller must hold mTimedBufferQueueLock
4888void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() {
4889 int64_t mediaTimeNow;
4890 {
4891 Mutex::Autolock mttLock(mMediaTimeTransformLock);
4892 if (!mMediaTimeTransformValid)
4893 return;
4894
4895 int64_t targetTimeNow;
4896 status_t res = (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME)
4897 ? mCCHelper.getCommonTime(&targetTimeNow)
4898 : mCCHelper.getLocalTime(&targetTimeNow);
4899
4900 if (OK != res)
4901 return;
4902
4903 if (!mMediaTimeTransform.doReverseTransform(targetTimeNow,
4904 &mediaTimeNow)) {
4905 return;
4906 }
4907 }
4908
John Grossman1c345192012-03-27 14:00:17 -07004909 size_t trimEnd;
4910 for (trimEnd = 0; trimEnd < mTimedBufferQueue.size(); trimEnd++) {
John Grossman9fbdee12012-03-26 17:51:46 -07004911 int64_t bufEnd;
4912
John Grossmanc95cfbb2012-04-12 11:53:11 -07004913 if ((trimEnd + 1) < mTimedBufferQueue.size()) {
4914 // We have a next buffer. Just use its PTS as the PTS of the frame
4915 // following the last frame in this buffer. If the stream is sparse
4916 // (ie, there are deliberate gaps left in the stream which should be
4917 // filled with silence by the TimedAudioTrack), then this can result
4918 // in one extra buffer being left un-trimmed when it could have
4919 // been. In general, this is not typical, and we would rather
4920 // optimized away the TS calculation below for the more common case
4921 // where PTSes are contiguous.
4922 bufEnd = mTimedBufferQueue[trimEnd + 1].pts();
4923 } else {
4924 // We have no next buffer. Compute the PTS of the frame following
4925 // the last frame in this buffer by computing the duration of of
4926 // this frame in media time units and adding it to the PTS of the
4927 // buffer.
4928 int64_t frameCount = mTimedBufferQueue[trimEnd].buffer()->size()
4929 / mCblk->frameSize;
4930
4931 if (!mMediaTimeToSampleTransform.doReverseTransform(frameCount,
4932 &bufEnd)) {
4933 ALOGE("Failed to convert frame count of %lld to media time"
4934 " duration" " (scale factor %d/%u) in %s",
4935 frameCount,
4936 mMediaTimeToSampleTransform.a_to_b_numer,
4937 mMediaTimeToSampleTransform.a_to_b_denom,
4938 __PRETTY_FUNCTION__);
4939 break;
4940 }
4941 bufEnd += mTimedBufferQueue[trimEnd].pts();
John Grossman9fbdee12012-03-26 17:51:46 -07004942 }
John Grossman9fbdee12012-03-26 17:51:46 -07004943
4944 if (bufEnd > mediaTimeNow)
4945 break;
4946
4947 // Is the buffer we want to use in the middle of a mix operation right
4948 // now? If so, don't actually trim it. Just wait for the releaseBuffer
4949 // from the mixer which should be coming back shortly.
John Grossman1c345192012-03-27 14:00:17 -07004950 if (!trimEnd && mQueueHeadInFlight) {
John Grossman9fbdee12012-03-26 17:51:46 -07004951 mTrimQueueHeadOnRelease = true;
4952 }
John Grossman4ff14ba2012-02-08 16:37:41 -08004953 }
4954
John Grossman9fbdee12012-03-26 17:51:46 -07004955 size_t trimStart = mTrimQueueHeadOnRelease ? 1 : 0;
John Grossman1c345192012-03-27 14:00:17 -07004956 if (trimStart < trimEnd) {
4957 // Update the bookkeeping for framesReady()
4958 for (size_t i = trimStart; i < trimEnd; ++i) {
4959 updateFramesPendingAfterTrim_l(mTimedBufferQueue[i], "trim");
4960 }
4961
4962 // Now actually remove the buffers from the queue.
4963 mTimedBufferQueue.removeItemsAt(trimStart, trimEnd);
John Grossman4ff14ba2012-02-08 16:37:41 -08004964 }
4965}
4966
John Grossman1c345192012-03-27 14:00:17 -07004967void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueueHead_l(
4968 const char* logTag) {
John Grossmand3030da2012-04-12 11:56:36 -07004969 ALOG_ASSERT(mTimedBufferQueue.size() > 0,
4970 "%s called (reason \"%s\"), but timed buffer queue has no"
4971 " elements to trim.", __FUNCTION__, logTag);
John Grossman1c345192012-03-27 14:00:17 -07004972
4973 updateFramesPendingAfterTrim_l(mTimedBufferQueue[0], logTag);
4974 mTimedBufferQueue.removeAt(0);
4975}
4976
4977void AudioFlinger::PlaybackThread::TimedTrack::updateFramesPendingAfterTrim_l(
4978 const TimedBuffer& buf,
4979 const char* logTag) {
4980 uint32_t bufBytes = buf.buffer()->size();
4981 uint32_t consumedAlready = buf.position();
4982
Eric Laurentb388e532012-04-14 13:32:48 -07004983 ALOG_ASSERT(consumedAlready <= bufBytes,
John Grossmand3030da2012-04-12 11:56:36 -07004984 "Bad bookkeeping while updating frames pending. Timed buffer is"
4985 " only %u bytes long, but claims to have consumed %u"
4986 " bytes. (update reason: \"%s\")",
Eric Laurentb388e532012-04-14 13:32:48 -07004987 bufBytes, consumedAlready, logTag);
John Grossman1c345192012-03-27 14:00:17 -07004988
4989 uint32_t bufFrames = (bufBytes - consumedAlready) / mCblk->frameSize;
John Grossmand3030da2012-04-12 11:56:36 -07004990 ALOG_ASSERT(mFramesPendingInQueue >= bufFrames,
4991 "Bad bookkeeping while updating frames pending. Should have at"
4992 " least %u queued frames, but we think we have only %u. (update"
4993 " reason: \"%s\")",
4994 bufFrames, mFramesPendingInQueue, logTag);
John Grossman1c345192012-03-27 14:00:17 -07004995
4996 mFramesPendingInQueue -= bufFrames;
4997}
4998
John Grossman4ff14ba2012-02-08 16:37:41 -08004999status_t AudioFlinger::PlaybackThread::TimedTrack::queueTimedBuffer(
5000 const sp<IMemory>& buffer, int64_t pts) {
5001
5002 {
5003 Mutex::Autolock mttLock(mMediaTimeTransformLock);
5004 if (!mMediaTimeTransformValid)
5005 return INVALID_OPERATION;
5006 }
5007
5008 Mutex::Autolock _l(mTimedBufferQueueLock);
5009
John Grossman1c345192012-03-27 14:00:17 -07005010 uint32_t bufFrames = buffer->size() / mCblk->frameSize;
5011 mFramesPendingInQueue += bufFrames;
John Grossman4ff14ba2012-02-08 16:37:41 -08005012 mTimedBufferQueue.add(TimedBuffer(buffer, pts));
5013
5014 return NO_ERROR;
5015}
5016
5017status_t AudioFlinger::PlaybackThread::TimedTrack::setMediaTimeTransform(
5018 const LinearTransform& xform, TimedAudioTrack::TargetTimeline target) {
5019
John Grossman1c345192012-03-27 14:00:17 -07005020 ALOGVV("setMediaTimeTransform az=%lld bz=%lld n=%d d=%u tgt=%d",
5021 xform.a_zero, xform.b_zero, xform.a_to_b_numer, xform.a_to_b_denom,
5022 target);
John Grossman4ff14ba2012-02-08 16:37:41 -08005023
5024 if (!(target == TimedAudioTrack::LOCAL_TIME ||
5025 target == TimedAudioTrack::COMMON_TIME)) {
5026 return BAD_VALUE;
5027 }
5028
5029 Mutex::Autolock lock(mMediaTimeTransformLock);
5030 mMediaTimeTransform = xform;
5031 mMediaTimeTransformTarget = target;
5032 mMediaTimeTransformValid = true;
5033
5034 return NO_ERROR;
5035}
5036
5037#define min(a, b) ((a) < (b) ? (a) : (b))
5038
5039// implementation of getNextBuffer for tracks whose buffers have timestamps
5040status_t AudioFlinger::PlaybackThread::TimedTrack::getNextBuffer(
5041 AudioBufferProvider::Buffer* buffer, int64_t pts)
5042{
5043 if (pts == AudioBufferProvider::kInvalidPTS) {
Glenn Kastend5903ec2012-03-18 10:33:27 -07005044 buffer->raw = NULL;
John Grossman4ff14ba2012-02-08 16:37:41 -08005045 buffer->frameCount = 0;
John Grossman8d314b72012-04-19 12:08:17 -07005046 mTimedAudioOutputOnTime = false;
John Grossman4ff14ba2012-02-08 16:37:41 -08005047 return INVALID_OPERATION;
5048 }
5049
John Grossman4ff14ba2012-02-08 16:37:41 -08005050 Mutex::Autolock _l(mTimedBufferQueueLock);
5051
John Grossman9fbdee12012-03-26 17:51:46 -07005052 ALOG_ASSERT(!mQueueHeadInFlight,
5053 "getNextBuffer called without releaseBuffer!");
5054
John Grossman4ff14ba2012-02-08 16:37:41 -08005055 while (true) {
5056
5057 // if we have no timed buffers, then fail
5058 if (mTimedBufferQueue.isEmpty()) {
Glenn Kastend5903ec2012-03-18 10:33:27 -07005059 buffer->raw = NULL;
John Grossman4ff14ba2012-02-08 16:37:41 -08005060 buffer->frameCount = 0;
5061 return NOT_ENOUGH_DATA;
5062 }
5063
5064 TimedBuffer& head = mTimedBufferQueue.editItemAt(0);
5065
5066 // calculate the PTS of the head of the timed buffer queue expressed in
5067 // local time
5068 int64_t headLocalPTS;
5069 {
5070 Mutex::Autolock mttLock(mMediaTimeTransformLock);
5071
Glenn Kasten5798d4e2012-03-08 12:18:35 -08005072 ALOG_ASSERT(mMediaTimeTransformValid, "media time transform invalid");
John Grossman4ff14ba2012-02-08 16:37:41 -08005073
5074 if (mMediaTimeTransform.a_to_b_denom == 0) {
5075 // the transform represents a pause, so yield silence
John Grossman9fbdee12012-03-26 17:51:46 -07005076 timedYieldSilence_l(buffer->frameCount, buffer);
John Grossman4ff14ba2012-02-08 16:37:41 -08005077 return NO_ERROR;
5078 }
5079
5080 int64_t transformedPTS;
5081 if (!mMediaTimeTransform.doForwardTransform(head.pts(),
5082 &transformedPTS)) {
5083 // the transform failed. this shouldn't happen, but if it does
5084 // then just drop this buffer
5085 ALOGW("timedGetNextBuffer transform failed");
Glenn Kastend5903ec2012-03-18 10:33:27 -07005086 buffer->raw = NULL;
John Grossman4ff14ba2012-02-08 16:37:41 -08005087 buffer->frameCount = 0;
John Grossman1c345192012-03-27 14:00:17 -07005088 trimTimedBufferQueueHead_l("getNextBuffer; no transform");
John Grossman4ff14ba2012-02-08 16:37:41 -08005089 return NO_ERROR;
5090 }
5091
5092 if (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) {
5093 if (OK != mCCHelper.commonTimeToLocalTime(transformedPTS,
5094 &headLocalPTS)) {
Glenn Kastend5903ec2012-03-18 10:33:27 -07005095 buffer->raw = NULL;
John Grossman4ff14ba2012-02-08 16:37:41 -08005096 buffer->frameCount = 0;
5097 return INVALID_OPERATION;
5098 }
5099 } else {
5100 headLocalPTS = transformedPTS;
5101 }
5102 }
5103
5104 // adjust the head buffer's PTS to reflect the portion of the head buffer
5105 // that has already been consumed
5106 int64_t effectivePTS = headLocalPTS +
5107 ((head.position() / mCblk->frameSize) * mLocalTimeFreq / sampleRate());
5108
5109 // Calculate the delta in samples between the head of the input buffer
5110 // queue and the start of the next output buffer that will be written.
5111 // If the transformation fails because of over or underflow, it means
5112 // that the sample's position in the output stream is so far out of
5113 // whack that it should just be dropped.
5114 int64_t sampleDelta;
5115 if (llabs(effectivePTS - pts) >= (static_cast<int64_t>(1) << 31)) {
5116 ALOGV("*** head buffer is too far from PTS: dropped buffer");
John Grossman1c345192012-03-27 14:00:17 -07005117 trimTimedBufferQueueHead_l("getNextBuffer, buf pts too far from"
5118 " mix");
John Grossman4ff14ba2012-02-08 16:37:41 -08005119 continue;
5120 }
5121 if (!mLocalTimeToSampleTransform.doForwardTransform(
5122 (effectivePTS - pts) << 32, &sampleDelta)) {
John Grossmand3030da2012-04-12 11:56:36 -07005123 ALOGV("*** too late during sample rate transform: dropped buffer");
John Grossman1c345192012-03-27 14:00:17 -07005124 trimTimedBufferQueueHead_l("getNextBuffer, bad local to sample");
John Grossman4ff14ba2012-02-08 16:37:41 -08005125 continue;
5126 }
5127
John Grossman1c345192012-03-27 14:00:17 -07005128 ALOGVV("*** getNextBuffer head.pts=%lld head.pos=%d pts=%lld"
5129 " sampleDelta=[%d.%08x]",
5130 head.pts(), head.position(), pts,
5131 static_cast<int32_t>((sampleDelta >= 0 ? 0 : 1)
5132 + (sampleDelta >> 32)),
5133 static_cast<uint32_t>(sampleDelta & 0xFFFFFFFF));
John Grossman4ff14ba2012-02-08 16:37:41 -08005134
5135 // if the delta between the ideal placement for the next input sample and
5136 // the current output position is within this threshold, then we will
5137 // concatenate the next input samples to the previous output
5138 const int64_t kSampleContinuityThreshold =
John Grossman8d314b72012-04-19 12:08:17 -07005139 (static_cast<int64_t>(sampleRate()) << 32) / 250;
John Grossman4ff14ba2012-02-08 16:37:41 -08005140
5141 // if this is the first buffer of audio that we're emitting from this track
5142 // then it should be almost exactly on time.
5143 const int64_t kSampleStartupThreshold = 1LL << 32;
5144
5145 if ((mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleContinuityThreshold) ||
John Grossman8d314b72012-04-19 12:08:17 -07005146 (!mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleStartupThreshold)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08005147 // the next input is close enough to being on time, so concatenate it
5148 // with the last output
John Grossman9fbdee12012-03-26 17:51:46 -07005149 timedYieldSamples_l(buffer);
John Grossman4ff14ba2012-02-08 16:37:41 -08005150
John Grossman1c345192012-03-27 14:00:17 -07005151 ALOGVV("*** on time: head.pos=%d frameCount=%u",
5152 head.position(), buffer->frameCount);
John Grossman4ff14ba2012-02-08 16:37:41 -08005153 return NO_ERROR;
John Grossman8d314b72012-04-19 12:08:17 -07005154 }
5155
5156 // Looks like our output is not on time. Reset our on timed status.
5157 // Next time we mix samples from our input queue, then should be within
5158 // the StartupThreshold.
5159 mTimedAudioOutputOnTime = false;
5160 if (sampleDelta > 0) {
John Grossman4ff14ba2012-02-08 16:37:41 -08005161 // the gap between the current output position and the proper start of
5162 // the next input sample is too big, so fill it with silence
5163 uint32_t framesUntilNextInput = (sampleDelta + 0x80000000) >> 32;
5164
John Grossman9fbdee12012-03-26 17:51:46 -07005165 timedYieldSilence_l(framesUntilNextInput, buffer);
John Grossman4ff14ba2012-02-08 16:37:41 -08005166 ALOGV("*** silence: frameCount=%u", buffer->frameCount);
5167 return NO_ERROR;
5168 } else {
5169 // the next input sample is late
5170 uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32));
5171 size_t onTimeSamplePosition =
5172 head.position() + lateFrames * mCblk->frameSize;
5173
5174 if (onTimeSamplePosition > head.buffer()->size()) {
5175 // all the remaining samples in the head are too late, so
5176 // drop it and move on
5177 ALOGV("*** too late: dropped buffer");
John Grossman1c345192012-03-27 14:00:17 -07005178 trimTimedBufferQueueHead_l("getNextBuffer, dropped late buffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08005179 continue;
5180 } else {
5181 // skip over the late samples
5182 head.setPosition(onTimeSamplePosition);
5183
5184 // yield the available samples
John Grossman9fbdee12012-03-26 17:51:46 -07005185 timedYieldSamples_l(buffer);
John Grossman4ff14ba2012-02-08 16:37:41 -08005186
5187 ALOGV("*** late: head.pos=%d frameCount=%u", head.position(), buffer->frameCount);
5188 return NO_ERROR;
5189 }
5190 }
5191 }
5192}
5193
5194// Yield samples from the timed buffer queue head up to the given output
5195// buffer's capacity.
5196//
5197// Caller must hold mTimedBufferQueueLock
John Grossman9fbdee12012-03-26 17:51:46 -07005198void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSamples_l(
John Grossman4ff14ba2012-02-08 16:37:41 -08005199 AudioBufferProvider::Buffer* buffer) {
5200
5201 const TimedBuffer& head = mTimedBufferQueue[0];
5202
5203 buffer->raw = (static_cast<uint8_t*>(head.buffer()->pointer()) +
5204 head.position());
5205
5206 uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) /
5207 mCblk->frameSize);
5208 size_t framesRequested = buffer->frameCount;
5209 buffer->frameCount = min(framesLeftInHead, framesRequested);
5210
John Grossman9fbdee12012-03-26 17:51:46 -07005211 mQueueHeadInFlight = true;
John Grossman4ff14ba2012-02-08 16:37:41 -08005212 mTimedAudioOutputOnTime = true;
5213}
5214
5215// Yield samples of silence up to the given output buffer's capacity
5216//
5217// Caller must hold mTimedBufferQueueLock
John Grossman9fbdee12012-03-26 17:51:46 -07005218void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSilence_l(
John Grossman4ff14ba2012-02-08 16:37:41 -08005219 uint32_t numFrames, AudioBufferProvider::Buffer* buffer) {
5220
5221 // lazily allocate a buffer filled with silence
5222 if (mTimedSilenceBufferSize < numFrames * mCblk->frameSize) {
5223 delete [] mTimedSilenceBuffer;
5224 mTimedSilenceBufferSize = numFrames * mCblk->frameSize;
5225 mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize];
5226 memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize);
5227 }
5228
5229 buffer->raw = mTimedSilenceBuffer;
5230 size_t framesRequested = buffer->frameCount;
5231 buffer->frameCount = min(numFrames, framesRequested);
5232
5233 mTimedAudioOutputOnTime = false;
5234}
5235
Glenn Kasten01c4ebf2012-02-22 10:47:35 -08005236// AudioBufferProvider interface
John Grossman4ff14ba2012-02-08 16:37:41 -08005237void AudioFlinger::PlaybackThread::TimedTrack::releaseBuffer(
5238 AudioBufferProvider::Buffer* buffer) {
5239
5240 Mutex::Autolock _l(mTimedBufferQueueLock);
5241
John Grossmanfe5b3ba2012-02-12 17:51:21 -08005242 // If the buffer which was just released is part of the buffer at the head
5243 // of the queue, be sure to update the amt of the buffer which has been
5244 // consumed. If the buffer being returned is not part of the head of the
5245 // queue, its either because the buffer is part of the silence buffer, or
5246 // because the head of the timed queue was trimmed after the mixer called
5247 // getNextBuffer but before the mixer called releaseBuffer.
John Grossman9fbdee12012-03-26 17:51:46 -07005248 if (buffer->raw == mTimedSilenceBuffer) {
5249 ALOG_ASSERT(!mQueueHeadInFlight,
5250 "Queue head in flight during release of silence buffer!");
5251 goto done;
5252 }
5253
5254 ALOG_ASSERT(mQueueHeadInFlight,
5255 "TimedTrack::releaseBuffer of non-silence buffer, but no queue"
5256 " head in flight.");
5257
5258 if (mTimedBufferQueue.size()) {
John Grossman4ff14ba2012-02-08 16:37:41 -08005259 TimedBuffer& head = mTimedBufferQueue.editItemAt(0);
John Grossmanfe5b3ba2012-02-12 17:51:21 -08005260
5261 void* start = head.buffer()->pointer();
John Grossman9fbdee12012-03-26 17:51:46 -07005262 void* end = reinterpret_cast<void*>(
5263 reinterpret_cast<uint8_t*>(head.buffer()->pointer())
5264 + head.buffer()->size());
John Grossmanfe5b3ba2012-02-12 17:51:21 -08005265
John Grossman9fbdee12012-03-26 17:51:46 -07005266 ALOG_ASSERT((buffer->raw >= start) && (buffer->raw < end),
5267 "released buffer not within the head of the timed buffer"
5268 " queue; qHead = [%p, %p], released buffer = %p",
5269 start, end, buffer->raw);
5270
5271 head.setPosition(head.position() +
5272 (buffer->frameCount * mCblk->frameSize));
5273 mQueueHeadInFlight = false;
5274
John Grossman1c345192012-03-27 14:00:17 -07005275 ALOG_ASSERT(mFramesPendingInQueue >= buffer->frameCount,
5276 "Bad bookkeeping during releaseBuffer! Should have at"
5277 " least %u queued frames, but we think we have only %u",
5278 buffer->frameCount, mFramesPendingInQueue);
5279
5280 mFramesPendingInQueue -= buffer->frameCount;
5281
John Grossman9fbdee12012-03-26 17:51:46 -07005282 if ((static_cast<size_t>(head.position()) >= head.buffer()->size())
5283 || mTrimQueueHeadOnRelease) {
John Grossman1c345192012-03-27 14:00:17 -07005284 trimTimedBufferQueueHead_l("releaseBuffer");
John Grossman9fbdee12012-03-26 17:51:46 -07005285 mTrimQueueHeadOnRelease = false;
John Grossman4ff14ba2012-02-08 16:37:41 -08005286 }
John Grossman9fbdee12012-03-26 17:51:46 -07005287 } else {
5288 LOG_FATAL("TimedTrack::releaseBuffer of non-silence buffer with no"
5289 " buffers in the timed buffer queue");
John Grossman4ff14ba2012-02-08 16:37:41 -08005290 }
5291
John Grossman9fbdee12012-03-26 17:51:46 -07005292done:
John Grossman4ff14ba2012-02-08 16:37:41 -08005293 buffer->raw = 0;
5294 buffer->frameCount = 0;
5295}
5296
Glenn Kasten288ed212012-04-25 17:52:27 -07005297size_t AudioFlinger::PlaybackThread::TimedTrack::framesReady() const {
John Grossman4ff14ba2012-02-08 16:37:41 -08005298 Mutex::Autolock _l(mTimedBufferQueueLock);
John Grossman1c345192012-03-27 14:00:17 -07005299 return mFramesPendingInQueue;
John Grossman4ff14ba2012-02-08 16:37:41 -08005300}
5301
5302AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer()
5303 : mPTS(0), mPosition(0) {}
5304
5305AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer(
5306 const sp<IMemory>& buffer, int64_t pts)
5307 : mBuffer(buffer), mPTS(pts), mPosition(0) {}
5308
Mathias Agopian65ab4712010-07-14 17:59:35 -07005309// ----------------------------------------------------------------------------
5310
5311// RecordTrack constructor must be called with AudioFlinger::mLock held
5312AudioFlinger::RecordThread::RecordTrack::RecordTrack(
Glenn Kasten9eaa5572012-01-20 13:32:16 -08005313 RecordThread *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005314 const sp<Client>& client,
5315 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08005316 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07005317 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005318 int frameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005319 int sessionId)
5320 : TrackBase(thread, client, sampleRate, format,
Glenn Kasten5cf034d2012-02-21 10:35:56 -08005321 channelMask, frameCount, 0 /*sharedBuffer*/, sessionId),
Mathias Agopian65ab4712010-07-14 17:59:35 -07005322 mOverflow(false)
5323{
5324 if (mCblk != NULL) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07005325 ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer);
5326 if (format == AUDIO_FORMAT_PCM_16_BIT) {
5327 mCblk->frameSize = mChannelCount * sizeof(int16_t);
5328 } else if (format == AUDIO_FORMAT_PCM_8_BIT) {
5329 mCblk->frameSize = mChannelCount * sizeof(int8_t);
5330 } else {
5331 mCblk->frameSize = sizeof(int8_t);
5332 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07005333 }
5334}
5335
5336AudioFlinger::RecordThread::RecordTrack::~RecordTrack()
5337{
Glenn Kasten510a3d62012-07-16 14:24:34 -07005338 ALOGV("%s", __func__);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005339}
5340
Glenn Kasten01c4ebf2012-02-22 10:47:35 -08005341// AudioBufferProvider interface
John Grossman4ff14ba2012-02-08 16:37:41 -08005342status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005343{
5344 audio_track_cblk_t* cblk = this->cblk();
5345 uint32_t framesAvail;
5346 uint32_t framesReq = buffer->frameCount;
5347
Glenn Kastene53b9ea2012-03-12 16:29:55 -07005348 // Check if last stepServer failed, try to step now
Glenn Kasten5cf034d2012-02-21 10:35:56 -08005349 if (mStepServerFailed) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005350 if (!step()) goto getNextBuffer_exit;
Steve Block3856b092011-10-20 11:56:00 +01005351 ALOGV("stepServer recovered");
Glenn Kasten5cf034d2012-02-21 10:35:56 -08005352 mStepServerFailed = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005353 }
5354
5355 framesAvail = cblk->framesAvailable_l();
5356
Glenn Kastenf6b16782011-12-15 09:51:17 -08005357 if (CC_LIKELY(framesAvail)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005358 uint32_t s = cblk->server;
5359 uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
5360
5361 if (framesReq > framesAvail) {
5362 framesReq = framesAvail;
5363 }
Marco Nelissena1472d92012-03-30 14:36:54 -07005364 if (framesReq > bufferEnd - s) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005365 framesReq = bufferEnd - s;
5366 }
5367
5368 buffer->raw = getBuffer(s, framesReq);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005369 buffer->frameCount = framesReq;
5370 return NO_ERROR;
5371 }
5372
5373getNextBuffer_exit:
Glenn Kastene0feee32011-12-13 11:53:26 -08005374 buffer->raw = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005375 buffer->frameCount = 0;
5376 return NOT_ENOUGH_DATA;
5377}
5378
Glenn Kasten3acbd052012-02-28 10:39:56 -08005379status_t AudioFlinger::RecordThread::RecordTrack::start(AudioSystem::sync_event_t event,
Eric Laurenta011e352012-03-29 15:51:43 -07005380 int triggerSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005381{
5382 sp<ThreadBase> thread = mThread.promote();
5383 if (thread != 0) {
5384 RecordThread *recordThread = (RecordThread *)thread.get();
Glenn Kasten3acbd052012-02-28 10:39:56 -08005385 return recordThread->start(this, event, triggerSession);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005386 } else {
5387 return BAD_VALUE;
5388 }
5389}
5390
5391void AudioFlinger::RecordThread::RecordTrack::stop()
5392{
5393 sp<ThreadBase> thread = mThread.promote();
5394 if (thread != 0) {
5395 RecordThread *recordThread = (RecordThread *)thread.get();
Glenn Kasten1d491ff2012-07-16 14:28:13 -07005396 recordThread->mLock.lock();
5397 bool doStop = recordThread->stop_l(this);
5398 if (doStop) {
5399 TrackBase::reset();
5400 // Force overrun condition to avoid false overrun callback until first data is
5401 // read from buffer
5402 android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags);
5403 }
5404 recordThread->mLock.unlock();
5405 if (doStop) {
5406 AudioSystem::stopInput(recordThread->id());
5407 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07005408 }
5409}
5410
Glenn Kasten510a3d62012-07-16 14:24:34 -07005411/*static*/ void AudioFlinger::RecordThread::RecordTrack::appendDumpHeader(String8& result)
5412{
5413 result.append(" Clien Fmt Chn mask Session Buf S SRate Serv User\n");
5414}
5415
Mathias Agopian65ab4712010-07-14 17:59:35 -07005416void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size)
5417{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07005418 snprintf(buffer, size, " %05d %03u 0x%08x %05d %04u %01d %05u %08x %08x\n",
Glenn Kasten44deb052012-02-05 18:09:08 -08005419 (mClient == 0) ? getpid_cached : mClient->pid(),
Mathias Agopian65ab4712010-07-14 17:59:35 -07005420 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07005421 mChannelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005422 mSessionId,
5423 mFrameCount,
5424 mState,
5425 mCblk->sampleRate,
5426 mCblk->server,
5427 mCblk->user);
5428}
5429
5430
5431// ----------------------------------------------------------------------------
5432
5433AudioFlinger::PlaybackThread::OutputTrack::OutputTrack(
Glenn Kasten9eaa5572012-01-20 13:32:16 -08005434 PlaybackThread *playbackThread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005435 DuplicatingThread *sourceThread,
5436 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08005437 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07005438 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005439 int frameCount)
Glenn Kasten73d22752012-03-19 13:38:30 -07005440 : Track(playbackThread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount,
5441 NULL, 0, IAudioFlinger::TRACK_DEFAULT),
Mathias Agopian65ab4712010-07-14 17:59:35 -07005442 mActive(false), mSourceThread(sourceThread)
5443{
5444
Mathias Agopian65ab4712010-07-14 17:59:35 -07005445 if (mCblk != NULL) {
5446 mCblk->flags |= CBLK_DIRECTION_OUT;
5447 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005448 mOutBuffer.frameCount = 0;
5449 playbackThread->mTracks.add(this);
Steve Block3856b092011-10-20 11:56:00 +01005450 ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07005451 "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p",
5452 mCblk, mBuffer, mCblk->buffers,
5453 mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005454 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00005455 ALOGW("Error creating output track on thread %p", playbackThread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005456 }
5457}
5458
5459AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack()
5460{
5461 clearBufferQueue();
5462}
5463
Glenn Kasten3acbd052012-02-28 10:39:56 -08005464status_t AudioFlinger::PlaybackThread::OutputTrack::start(AudioSystem::sync_event_t event,
Eric Laurenta011e352012-03-29 15:51:43 -07005465 int triggerSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005466{
Glenn Kasten3acbd052012-02-28 10:39:56 -08005467 status_t status = Track::start(event, triggerSession);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005468 if (status != NO_ERROR) {
5469 return status;
5470 }
5471
5472 mActive = true;
5473 mRetryCount = 127;
5474 return status;
5475}
5476
5477void AudioFlinger::PlaybackThread::OutputTrack::stop()
5478{
5479 Track::stop();
5480 clearBufferQueue();
5481 mOutBuffer.frameCount = 0;
5482 mActive = false;
5483}
5484
5485bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames)
5486{
5487 Buffer *pInBuffer;
5488 Buffer inBuffer;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07005489 uint32_t channelCount = mChannelCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005490 bool outputBufferFull = false;
5491 inBuffer.frameCount = frames;
5492 inBuffer.i16 = data;
5493
5494 uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs();
5495
5496 if (!mActive && frames != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08005497 start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005498 sp<ThreadBase> thread = mThread.promote();
5499 if (thread != 0) {
5500 MixerThread *mixerThread = (MixerThread *)thread.get();
5501 if (mCblk->frameCount > frames){
5502 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
5503 uint32_t startFrames = (mCblk->frameCount - frames);
5504 pInBuffer = new Buffer;
5505 pInBuffer->mBuffer = new int16_t[startFrames * channelCount];
5506 pInBuffer->frameCount = startFrames;
5507 pInBuffer->i16 = pInBuffer->mBuffer;
5508 memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t));
5509 mBufferQueue.add(pInBuffer);
5510 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00005511 ALOGW ("OutputTrack::write() %p no more buffers in queue", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005512 }
5513 }
5514 }
5515 }
5516
5517 while (waitTimeLeftMs) {
5518 // First write pending buffers, then new data
5519 if (mBufferQueue.size()) {
5520 pInBuffer = mBufferQueue.itemAt(0);
5521 } else {
5522 pInBuffer = &inBuffer;
5523 }
5524
5525 if (pInBuffer->frameCount == 0) {
5526 break;
5527 }
5528
5529 if (mOutBuffer.frameCount == 0) {
5530 mOutBuffer.frameCount = pInBuffer->frameCount;
5531 nsecs_t startTime = systemTime();
Glenn Kasten335787f2012-01-20 17:00:00 -08005532 if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)NO_MORE_BUFFERS) {
Steve Block3856b092011-10-20 11:56:00 +01005533 ALOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07005534 outputBufferFull = true;
5535 break;
5536 }
5537 uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime);
5538 if (waitTimeLeftMs >= waitTimeMs) {
5539 waitTimeLeftMs -= waitTimeMs;
5540 } else {
5541 waitTimeLeftMs = 0;
5542 }
5543 }
5544
5545 uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount;
5546 memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t));
5547 mCblk->stepUser(outFrames);
5548 pInBuffer->frameCount -= outFrames;
5549 pInBuffer->i16 += outFrames * channelCount;
5550 mOutBuffer.frameCount -= outFrames;
5551 mOutBuffer.i16 += outFrames * channelCount;
5552
5553 if (pInBuffer->frameCount == 0) {
5554 if (mBufferQueue.size()) {
5555 mBufferQueue.removeAt(0);
5556 delete [] pInBuffer->mBuffer;
5557 delete pInBuffer;
Steve Block3856b092011-10-20 11:56:00 +01005558 ALOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07005559 } else {
5560 break;
5561 }
5562 }
5563 }
5564
5565 // If we could not write all frames, allocate a buffer and queue it for next time.
5566 if (inBuffer.frameCount) {
5567 sp<ThreadBase> thread = mThread.promote();
5568 if (thread != 0 && !thread->standby()) {
5569 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
5570 pInBuffer = new Buffer;
5571 pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount];
5572 pInBuffer->frameCount = inBuffer.frameCount;
5573 pInBuffer->i16 = pInBuffer->mBuffer;
5574 memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t));
5575 mBufferQueue.add(pInBuffer);
Steve Block3856b092011-10-20 11:56:00 +01005576 ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07005577 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00005578 ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005579 }
5580 }
5581 }
5582
5583 // Calling write() with a 0 length buffer, means that no more data will be written:
5584 // If no more buffers are pending, fill output track buffer to make sure it is started
5585 // by output mixer.
5586 if (frames == 0 && mBufferQueue.size() == 0) {
5587 if (mCblk->user < mCblk->frameCount) {
5588 frames = mCblk->frameCount - mCblk->user;
5589 pInBuffer = new Buffer;
5590 pInBuffer->mBuffer = new int16_t[frames * channelCount];
5591 pInBuffer->frameCount = frames;
5592 pInBuffer->i16 = pInBuffer->mBuffer;
5593 memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t));
5594 mBufferQueue.add(pInBuffer);
5595 } else if (mActive) {
5596 stop();
5597 }
5598 }
5599
5600 return outputBufferFull;
5601}
5602
5603status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs)
5604{
5605 int active;
5606 status_t result;
5607 audio_track_cblk_t* cblk = mCblk;
5608 uint32_t framesReq = buffer->frameCount;
5609
Steve Block3856b092011-10-20 11:56:00 +01005610// ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005611 buffer->frameCount = 0;
5612
5613 uint32_t framesAvail = cblk->framesAvailable();
5614
5615
5616 if (framesAvail == 0) {
5617 Mutex::Autolock _l(cblk->lock);
5618 goto start_loop_here;
5619 while (framesAvail == 0) {
5620 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -08005621 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +01005622 ALOGV("Not active and NO_MORE_BUFFERS");
Glenn Kasten335787f2012-01-20 17:00:00 -08005623 return NO_MORE_BUFFERS;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005624 }
5625 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
5626 if (result != NO_ERROR) {
Glenn Kasten335787f2012-01-20 17:00:00 -08005627 return NO_MORE_BUFFERS;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005628 }
5629 // read the server count again
5630 start_loop_here:
5631 framesAvail = cblk->framesAvailable_l();
5632 }
5633 }
5634
5635// if (framesAvail < framesReq) {
Glenn Kasten335787f2012-01-20 17:00:00 -08005636// return NO_MORE_BUFFERS;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005637// }
5638
5639 if (framesReq > framesAvail) {
5640 framesReq = framesAvail;
5641 }
5642
5643 uint32_t u = cblk->user;
5644 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
5645
Marco Nelissena1472d92012-03-30 14:36:54 -07005646 if (framesReq > bufferEnd - u) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005647 framesReq = bufferEnd - u;
5648 }
5649
5650 buffer->frameCount = framesReq;
5651 buffer->raw = (void *)cblk->buffer(u);
5652 return NO_ERROR;
5653}
5654
5655
5656void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue()
5657{
5658 size_t size = mBufferQueue.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005659
5660 for (size_t i = 0; i < size; i++) {
Glenn Kastena1117922012-01-26 10:53:32 -08005661 Buffer *pBuffer = mBufferQueue.itemAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005662 delete [] pBuffer->mBuffer;
5663 delete pBuffer;
5664 }
5665 mBufferQueue.clear();
5666}
5667
5668// ----------------------------------------------------------------------------
5669
5670AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
5671 : RefBase(),
5672 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08005673 // 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 -07005674 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08005675 mPid(pid),
5676 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005677{
5678 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
5679}
5680
5681// Client destructor must be called with AudioFlinger::mLock held
5682AudioFlinger::Client::~Client()
5683{
5684 mAudioFlinger->removeClient_l(mPid);
5685}
5686
Glenn Kasten435dbe62012-01-30 10:15:48 -08005687sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07005688{
5689 return mMemoryDealer;
5690}
5691
John Grossman4ff14ba2012-02-08 16:37:41 -08005692// Reserve one of the limited slots for a timed audio track associated
5693// with this client
5694bool AudioFlinger::Client::reserveTimedTrack()
5695{
5696 const int kMaxTimedTracksPerClient = 4;
5697
5698 Mutex::Autolock _l(mTimedTrackLock);
5699
5700 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
5701 ALOGW("can not create timed track - pid %d has exceeded the limit",
5702 mPid);
5703 return false;
5704 }
5705
5706 mTimedTrackCount++;
5707 return true;
5708}
5709
5710// Release a slot for a timed audio track
5711void AudioFlinger::Client::releaseTimedTrack()
5712{
5713 Mutex::Autolock _l(mTimedTrackLock);
5714 mTimedTrackCount--;
5715}
5716
Mathias Agopian65ab4712010-07-14 17:59:35 -07005717// ----------------------------------------------------------------------------
5718
5719AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
5720 const sp<IAudioFlingerClient>& client,
5721 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08005722 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005723{
5724}
5725
5726AudioFlinger::NotificationClient::~NotificationClient()
5727{
Mathias Agopian65ab4712010-07-14 17:59:35 -07005728}
5729
5730void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
5731{
5732 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08005733 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005734}
5735
5736// ----------------------------------------------------------------------------
5737
5738AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track)
5739 : BnAudioTrack(),
5740 mTrack(track)
5741{
5742}
5743
5744AudioFlinger::TrackHandle::~TrackHandle() {
5745 // just stop the track on deletion, associated resources
5746 // will be freed from the main thread once all pending buffers have
5747 // been played. Unless it's not in the active track list, in which
5748 // case we free everything now...
5749 mTrack->destroy();
5750}
5751
Glenn Kasten90716c52012-01-26 13:40:12 -08005752sp<IMemory> AudioFlinger::TrackHandle::getCblk() const {
5753 return mTrack->getCblk();
5754}
5755
Glenn Kasten3acbd052012-02-28 10:39:56 -08005756status_t AudioFlinger::TrackHandle::start() {
5757 return mTrack->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005758}
5759
5760void AudioFlinger::TrackHandle::stop() {
5761 mTrack->stop();
5762}
5763
5764void AudioFlinger::TrackHandle::flush() {
5765 mTrack->flush();
5766}
5767
5768void AudioFlinger::TrackHandle::mute(bool e) {
5769 mTrack->mute(e);
5770}
5771
5772void AudioFlinger::TrackHandle::pause() {
5773 mTrack->pause();
5774}
5775
Mathias Agopian65ab4712010-07-14 17:59:35 -07005776status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId)
5777{
5778 return mTrack->attachAuxEffect(EffectId);
5779}
5780
John Grossman4ff14ba2012-02-08 16:37:41 -08005781status_t AudioFlinger::TrackHandle::allocateTimedBuffer(size_t size,
5782 sp<IMemory>* buffer) {
5783 if (!mTrack->isTimedTrack())
5784 return INVALID_OPERATION;
5785
5786 PlaybackThread::TimedTrack* tt =
5787 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
5788 return tt->allocateTimedBuffer(size, buffer);
5789}
5790
5791status_t AudioFlinger::TrackHandle::queueTimedBuffer(const sp<IMemory>& buffer,
5792 int64_t pts) {
5793 if (!mTrack->isTimedTrack())
5794 return INVALID_OPERATION;
5795
5796 PlaybackThread::TimedTrack* tt =
5797 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
5798 return tt->queueTimedBuffer(buffer, pts);
5799}
5800
5801status_t AudioFlinger::TrackHandle::setMediaTimeTransform(
5802 const LinearTransform& xform, int target) {
5803
5804 if (!mTrack->isTimedTrack())
5805 return INVALID_OPERATION;
5806
5807 PlaybackThread::TimedTrack* tt =
5808 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
5809 return tt->setMediaTimeTransform(
5810 xform, static_cast<TimedAudioTrack::TargetTimeline>(target));
5811}
5812
Mathias Agopian65ab4712010-07-14 17:59:35 -07005813status_t AudioFlinger::TrackHandle::onTransact(
5814 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
5815{
5816 return BnAudioTrack::onTransact(code, data, reply, flags);
5817}
5818
5819// ----------------------------------------------------------------------------
5820
5821sp<IAudioRecord> AudioFlinger::openRecord(
5822 pid_t pid,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005823 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005824 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08005825 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07005826 audio_channel_mask_t channelMask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005827 int frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08005828 IAudioFlinger::track_flags_t flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07005829 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07005830 int *sessionId,
5831 status_t *status)
5832{
5833 sp<RecordThread::RecordTrack> recordTrack;
5834 sp<RecordHandle> recordHandle;
5835 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005836 status_t lStatus;
5837 RecordThread *thread;
5838 size_t inFrameCount;
5839 int lSessionId;
5840
5841 // check calling permissions
5842 if (!recordingAllowed()) {
5843 lStatus = PERMISSION_DENIED;
5844 goto Exit;
5845 }
5846
5847 // add client to list
5848 { // scope for mLock
5849 Mutex::Autolock _l(mLock);
5850 thread = checkRecordThread_l(input);
5851 if (thread == NULL) {
5852 lStatus = BAD_VALUE;
5853 goto Exit;
5854 }
5855
Glenn Kasten98ec94c2012-01-25 14:28:29 -08005856 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005857
5858 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07005859 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005860 lSessionId = *sessionId;
5861 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005862 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005863 if (sessionId != NULL) {
5864 *sessionId = lSessionId;
5865 }
5866 }
5867 // create new record track. The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07005868 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
5869 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005870 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005871 if (lStatus != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07005872 // remove local strong reference to Client before deleting the RecordTrack so that the Client
5873 // destructor is called by the TrackBase destructor with mLock held
5874 client.clear();
5875 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005876 goto Exit;
5877 }
5878
5879 // return to handle to client
5880 recordHandle = new RecordHandle(recordTrack);
5881 lStatus = NO_ERROR;
5882
5883Exit:
5884 if (status) {
5885 *status = lStatus;
5886 }
5887 return recordHandle;
5888}
5889
5890// ----------------------------------------------------------------------------
5891
5892AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack)
5893 : BnAudioRecord(),
5894 mRecordTrack(recordTrack)
5895{
5896}
5897
5898AudioFlinger::RecordHandle::~RecordHandle() {
Glenn Kastend96c5722012-04-25 13:44:49 -07005899 stop_nonvirtual();
Glenn Kasten510a3d62012-07-16 14:24:34 -07005900 mRecordTrack->destroy();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005901}
5902
Glenn Kasten90716c52012-01-26 13:40:12 -08005903sp<IMemory> AudioFlinger::RecordHandle::getCblk() const {
5904 return mRecordTrack->getCblk();
5905}
5906
Glenn Kasten0ec23ce2012-07-10 12:56:08 -07005907status_t AudioFlinger::RecordHandle::start(int /*AudioSystem::sync_event_t*/ event, int triggerSession) {
Steve Block3856b092011-10-20 11:56:00 +01005908 ALOGV("RecordHandle::start()");
Glenn Kasten3acbd052012-02-28 10:39:56 -08005909 return mRecordTrack->start((AudioSystem::sync_event_t)event, triggerSession);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005910}
5911
5912void AudioFlinger::RecordHandle::stop() {
Glenn Kastend96c5722012-04-25 13:44:49 -07005913 stop_nonvirtual();
5914}
5915
5916void AudioFlinger::RecordHandle::stop_nonvirtual() {
Steve Block3856b092011-10-20 11:56:00 +01005917 ALOGV("RecordHandle::stop()");
Mathias Agopian65ab4712010-07-14 17:59:35 -07005918 mRecordTrack->stop();
5919}
5920
Mathias Agopian65ab4712010-07-14 17:59:35 -07005921status_t AudioFlinger::RecordHandle::onTransact(
5922 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
5923{
5924 return BnAudioRecord::onTransact(code, data, reply, flags);
5925}
5926
5927// ----------------------------------------------------------------------------
5928
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005929AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger,
5930 AudioStreamIn *input,
5931 uint32_t sampleRate,
Glenn Kasten254af182012-07-03 14:59:05 -07005932 audio_channel_mask_t channelMask,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08005933 audio_io_handle_t id,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07005934 audio_devices_t device) :
Glenn Kasten23bb8be2012-01-26 10:38:26 -08005935 ThreadBase(audioFlinger, id, device, RECORD),
Glenn Kasten510a3d62012-07-16 14:24:34 -07005936 mInput(input), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08005937 // mRsmpInIndex and mInputBytes set by readInputParameters()
Glenn Kasten254af182012-07-03 14:59:05 -07005938 mReqChannelCount(popcount(channelMask)),
Glenn Kasten84afa3b2012-01-25 15:28:08 -08005939 mReqSampleRate(sampleRate)
5940 // mBytesRead is only meaningful while active, and so is cleared in start()
5941 // (but might be better to also clear here for dump?)
Mathias Agopian65ab4712010-07-14 17:59:35 -07005942{
Glenn Kasten480b4682012-02-28 12:30:08 -08005943 snprintf(mName, kNameLength, "AudioIn_%X", id);
Eric Laurentfeb0db62011-07-22 09:04:31 -07005944
Mathias Agopian65ab4712010-07-14 17:59:35 -07005945 readInputParameters();
5946}
5947
5948
5949AudioFlinger::RecordThread::~RecordThread()
5950{
5951 delete[] mRsmpInBuffer;
Glenn Kastene9dd0172012-01-27 18:08:45 -08005952 delete mResampler;
5953 delete[] mRsmpOutBuffer;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005954}
5955
5956void AudioFlinger::RecordThread::onFirstRef()
5957{
Eric Laurentfeb0db62011-07-22 09:04:31 -07005958 run(mName, PRIORITY_URGENT_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07005959}
5960
Eric Laurentb8ba0a92011-08-07 16:32:26 -07005961status_t AudioFlinger::RecordThread::readyToRun()
5962{
5963 status_t status = initCheck();
Steve Block5ff1dd52012-01-05 23:22:43 +00005964 ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this);
Eric Laurentb8ba0a92011-08-07 16:32:26 -07005965 return status;
5966}
5967
Mathias Agopian65ab4712010-07-14 17:59:35 -07005968bool AudioFlinger::RecordThread::threadLoop()
5969{
5970 AudioBufferProvider::Buffer buffer;
5971 sp<RecordTrack> activeTrack;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07005972 Vector< sp<EffectChain> > effectChains;
Mathias Agopian65ab4712010-07-14 17:59:35 -07005973
Eric Laurent44d98482010-09-30 16:12:31 -07005974 nsecs_t lastWarning = 0;
5975
Glenn Kastene4e2a372012-07-23 12:55:09 -07005976 inputStandBy();
Eric Laurentfeb0db62011-07-22 09:04:31 -07005977 acquireWakeLock();
5978
Mathias Agopian65ab4712010-07-14 17:59:35 -07005979 // start recording
5980 while (!exitPending()) {
5981
5982 processConfigEvents();
5983
5984 { // scope for mLock
5985 Mutex::Autolock _l(mLock);
5986 checkForNewParameters_l();
5987 if (mActiveTrack == 0 && mConfigEvents.isEmpty()) {
Glenn Kastene4e2a372012-07-23 12:55:09 -07005988 standby();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005989
5990 if (exitPending()) break;
5991
Eric Laurentfeb0db62011-07-22 09:04:31 -07005992 releaseWakeLock_l();
Steve Block3856b092011-10-20 11:56:00 +01005993 ALOGV("RecordThread: loop stopping");
Mathias Agopian65ab4712010-07-14 17:59:35 -07005994 // go to sleep
5995 mWaitWorkCV.wait(mLock);
Steve Block3856b092011-10-20 11:56:00 +01005996 ALOGV("RecordThread: loop starting");
Eric Laurentfeb0db62011-07-22 09:04:31 -07005997 acquireWakeLock_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07005998 continue;
5999 }
6000 if (mActiveTrack != 0) {
6001 if (mActiveTrack->mState == TrackBase::PAUSING) {
Glenn Kastene4e2a372012-07-23 12:55:09 -07006002 standby();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006003 mActiveTrack.clear();
6004 mStartStopCond.broadcast();
6005 } else if (mActiveTrack->mState == TrackBase::RESUMING) {
6006 if (mReqChannelCount != mActiveTrack->channelCount()) {
6007 mActiveTrack.clear();
6008 mStartStopCond.broadcast();
6009 } else if (mBytesRead != 0) {
6010 // record start succeeds only if first read from audio input
6011 // succeeds
6012 if (mBytesRead > 0) {
6013 mActiveTrack->mState = TrackBase::ACTIVE;
6014 } else {
6015 mActiveTrack.clear();
6016 }
6017 mStartStopCond.broadcast();
6018 }
6019 mStandby = false;
Glenn Kasten510a3d62012-07-16 14:24:34 -07006020 } else if (mActiveTrack->mState == TrackBase::TERMINATED) {
6021 removeTrack_l(mActiveTrack);
6022 mActiveTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006023 }
6024 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006025 lockEffectChains_l(effectChains);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006026 }
6027
6028 if (mActiveTrack != 0) {
6029 if (mActiveTrack->mState != TrackBase::ACTIVE &&
6030 mActiveTrack->mState != TrackBase::RESUMING) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006031 unlockEffectChains(effectChains);
6032 usleep(kRecordThreadSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006033 continue;
6034 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006035 for (size_t i = 0; i < effectChains.size(); i ++) {
6036 effectChains[i]->process_l();
6037 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006038
Mathias Agopian65ab4712010-07-14 17:59:35 -07006039 buffer.frameCount = mFrameCount;
Glenn Kasten01c4ebf2012-02-22 10:47:35 -08006040 if (CC_LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006041 size_t framesOut = buffer.frameCount;
Glenn Kastene0feee32011-12-13 11:53:26 -08006042 if (mResampler == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006043 // no resampling
6044 while (framesOut) {
6045 size_t framesIn = mFrameCount - mRsmpInIndex;
6046 if (framesIn) {
6047 int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize;
6048 int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize;
6049 if (framesIn > framesOut)
6050 framesIn = framesOut;
6051 mRsmpInIndex += framesIn;
6052 framesOut -= framesIn;
6053 if ((int)mChannelCount == mReqChannelCount ||
Dima Zavinfce7a472011-04-19 22:30:36 -07006054 mFormat != AUDIO_FORMAT_PCM_16_BIT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006055 memcpy(dst, src, framesIn * mFrameSize);
6056 } else {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006057 if (mChannelCount == 1) {
Glenn Kasten69d79962012-07-19 14:02:22 -07006058 upmix_to_stereo_i16_from_mono_i16((int16_t *)dst,
6059 (int16_t *)src, framesIn);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006060 } else {
Glenn Kasten69d79962012-07-19 14:02:22 -07006061 downmix_to_mono_i16_from_stereo_i16((int16_t *)dst,
6062 (int16_t *)src, framesIn);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006063 }
6064 }
6065 }
6066 if (framesOut && mFrameCount == mRsmpInIndex) {
6067 if (framesOut == mFrameCount &&
Dima Zavinfce7a472011-04-19 22:30:36 -07006068 ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) {
Dima Zavin799a70e2011-04-18 16:57:27 -07006069 mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006070 framesOut = 0;
6071 } else {
Dima Zavin799a70e2011-04-18 16:57:27 -07006072 mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006073 mRsmpInIndex = 0;
6074 }
6075 if (mBytesRead < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00006076 ALOGE("Error reading audio input");
Mathias Agopian65ab4712010-07-14 17:59:35 -07006077 if (mActiveTrack->mState == TrackBase::ACTIVE) {
6078 // Force input into standby so that it tries to
6079 // recover at next read attempt
Glenn Kastene4e2a372012-07-23 12:55:09 -07006080 inputStandBy();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006081 usleep(kRecordThreadSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006082 }
6083 mRsmpInIndex = mFrameCount;
6084 framesOut = 0;
6085 buffer.frameCount = 0;
6086 }
6087 }
6088 }
6089 } else {
6090 // resampling
6091
6092 memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t));
6093 // alter output frame count as if we were expecting stereo samples
6094 if (mChannelCount == 1 && mReqChannelCount == 1) {
6095 framesOut >>= 1;
6096 }
6097 mResampler->resample(mRsmpOutBuffer, framesOut, this);
6098 // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer()
6099 // are 32 bit aligned which should be always true.
6100 if (mChannelCount == 2 && mReqChannelCount == 1) {
Glenn Kasten3b21c502011-12-15 09:52:39 -08006101 ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006102 // the resampler always outputs stereo samples: do post stereo to mono conversion
Glenn Kasten69d79962012-07-19 14:02:22 -07006103 downmix_to_mono_i16_from_stereo_i16(buffer.i16, (int16_t *)mRsmpOutBuffer,
6104 framesOut);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006105 } else {
Glenn Kasten3b21c502011-12-15 09:52:39 -08006106 ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006107 }
6108
6109 }
Eric Laurenta011e352012-03-29 15:51:43 -07006110 if (mFramestoDrop == 0) {
6111 mActiveTrack->releaseBuffer(&buffer);
6112 } else {
6113 if (mFramestoDrop > 0) {
6114 mFramestoDrop -= buffer.frameCount;
Eric Laurent29864602012-05-08 18:57:51 -07006115 if (mFramestoDrop <= 0) {
6116 clearSyncStartEvent();
6117 }
6118 } else {
6119 mFramestoDrop += buffer.frameCount;
6120 if (mFramestoDrop >= 0 || mSyncStartEvent == 0 ||
6121 mSyncStartEvent->isCancelled()) {
6122 ALOGW("Synced record %s, session %d, trigger session %d",
6123 (mFramestoDrop >= 0) ? "timed out" : "cancelled",
6124 mActiveTrack->sessionId(),
6125 (mSyncStartEvent != 0) ? mSyncStartEvent->triggerSession() : 0);
6126 clearSyncStartEvent();
Eric Laurenta011e352012-03-29 15:51:43 -07006127 }
6128 }
6129 }
Glenn Kasten04270da2012-07-10 12:55:49 -07006130 mActiveTrack->clearOverflow();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006131 }
6132 // client isn't retrieving buffers fast enough
6133 else {
Eric Laurent44d98482010-09-30 16:12:31 -07006134 if (!mActiveTrack->setOverflow()) {
6135 nsecs_t now = systemTime();
Glenn Kasten7dede872011-12-13 11:04:14 -08006136 if ((now - lastWarning) > kWarningThrottleNs) {
Steve Block5ff1dd52012-01-05 23:22:43 +00006137 ALOGW("RecordThread: buffer overflow");
Eric Laurent44d98482010-09-30 16:12:31 -07006138 lastWarning = now;
6139 }
6140 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006141 // Release the processor for a while before asking for a new buffer.
6142 // This will give the application more chance to read from the buffer and
6143 // clear the overflow.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006144 usleep(kRecordThreadSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006145 }
6146 }
Eric Laurentec437d82011-07-26 20:54:46 -07006147 // enable changes in effect chain
6148 unlockEffectChains(effectChains);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006149 effectChains.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006150 }
6151
Glenn Kastene4e2a372012-07-23 12:55:09 -07006152 standby();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006153
Glenn Kasten33e6e352012-07-16 15:56:57 -07006154 {
6155 Mutex::Autolock _l(mLock);
6156 mActiveTrack.clear();
6157 mStartStopCond.broadcast();
6158 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006159
Eric Laurentfeb0db62011-07-22 09:04:31 -07006160 releaseWakeLock();
6161
Steve Block3856b092011-10-20 11:56:00 +01006162 ALOGV("RecordThread %p exiting", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006163 return false;
6164}
6165
Glenn Kastene4e2a372012-07-23 12:55:09 -07006166void AudioFlinger::RecordThread::standby()
6167{
6168 if (!mStandby) {
6169 inputStandBy();
6170 mStandby = true;
6171 }
6172}
6173
6174void AudioFlinger::RecordThread::inputStandBy()
6175{
6176 mInput->stream->common.standby(&mInput->stream->common);
6177}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006178
6179sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l(
6180 const sp<AudioFlinger::Client>& client,
6181 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08006182 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07006183 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006184 int frameCount,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006185 int sessionId,
Glenn Kasten1879fff2012-07-11 15:36:59 -07006186 IAudioFlinger::track_flags_t flags,
6187 pid_t tid,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006188 status_t *status)
6189{
6190 sp<RecordTrack> track;
6191 status_t lStatus;
6192
6193 lStatus = initCheck();
6194 if (lStatus != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +00006195 ALOGE("Audio driver not initialized.");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006196 goto Exit;
6197 }
6198
Glenn Kasten1879fff2012-07-11 15:36:59 -07006199 // FIXME use flags and tid similar to createTrack_l()
6200
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006201 { // scope for mLock
6202 Mutex::Autolock _l(mLock);
6203
6204 track = new RecordTrack(this, client, sampleRate,
Glenn Kasten5cf034d2012-02-21 10:35:56 -08006205 format, channelMask, frameCount, sessionId);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006206
Glenn Kasten7378ca52012-01-20 13:44:40 -08006207 if (track->getCblk() == 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006208 lStatus = NO_MEMORY;
6209 goto Exit;
6210 }
Glenn Kasten510a3d62012-07-16 14:24:34 -07006211 mTracks.add(track);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006212
Eric Laurent59bd0da2011-08-01 09:52:20 -07006213 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
Glenn Kastenbb4350d2012-07-03 15:56:38 -07006214 bool suspend = audio_is_bluetooth_sco_device(mDevice & AUDIO_DEVICE_IN_ALL) &&
6215 mAudioFlinger->btNrecIsOff();
Eric Laurent59bd0da2011-08-01 09:52:20 -07006216 setEffectSuspended_l(FX_IID_AEC, suspend, sessionId);
6217 setEffectSuspended_l(FX_IID_NS, suspend, sessionId);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006218 }
6219 lStatus = NO_ERROR;
6220
6221Exit:
6222 if (status) {
6223 *status = lStatus;
6224 }
6225 return track;
6226}
6227
Eric Laurenta011e352012-03-29 15:51:43 -07006228status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack,
Glenn Kasten3acbd052012-02-28 10:39:56 -08006229 AudioSystem::sync_event_t event,
Eric Laurenta011e352012-03-29 15:51:43 -07006230 int triggerSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -07006231{
Glenn Kasten58912562012-04-03 10:45:00 -07006232 ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07006233 sp<ThreadBase> strongMe = this;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006234 status_t status = NO_ERROR;
Eric Laurenta011e352012-03-29 15:51:43 -07006235
6236 if (event == AudioSystem::SYNC_EVENT_NONE) {
Eric Laurent29864602012-05-08 18:57:51 -07006237 clearSyncStartEvent();
Eric Laurenta011e352012-03-29 15:51:43 -07006238 } else if (event != AudioSystem::SYNC_EVENT_SAME) {
6239 mSyncStartEvent = mAudioFlinger->createSyncEvent(event,
6240 triggerSession,
6241 recordTrack->sessionId(),
6242 syncStartEventCallback,
6243 this);
Eric Laurent29864602012-05-08 18:57:51 -07006244 // Sync event can be cancelled by the trigger session if the track is not in a
6245 // compatible state in which case we start record immediately
6246 if (mSyncStartEvent->isCancelled()) {
6247 clearSyncStartEvent();
6248 } else {
6249 // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs
6250 mFramestoDrop = - ((AudioSystem::kSyncRecordStartTimeOutMs * mReqSampleRate) / 1000);
6251 }
Eric Laurenta011e352012-03-29 15:51:43 -07006252 }
6253
Mathias Agopian65ab4712010-07-14 17:59:35 -07006254 {
Glenn Kastena7d8d6f2012-01-05 15:41:56 -08006255 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006256 if (mActiveTrack != 0) {
6257 if (recordTrack != mActiveTrack.get()) {
6258 status = -EBUSY;
6259 } else if (mActiveTrack->mState == TrackBase::PAUSING) {
6260 mActiveTrack->mState = TrackBase::ACTIVE;
6261 }
6262 return status;
6263 }
6264
6265 recordTrack->mState = TrackBase::IDLE;
6266 mActiveTrack = recordTrack;
6267 mLock.unlock();
6268 status_t status = AudioSystem::startInput(mId);
6269 mLock.lock();
6270 if (status != NO_ERROR) {
6271 mActiveTrack.clear();
Eric Laurenta011e352012-03-29 15:51:43 -07006272 clearSyncStartEvent();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006273 return status;
6274 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006275 mRsmpInIndex = mFrameCount;
6276 mBytesRead = 0;
Eric Laurent243f5f92011-02-28 16:52:51 -08006277 if (mResampler != NULL) {
6278 mResampler->reset();
6279 }
6280 mActiveTrack->mState = TrackBase::RESUMING;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006281 // signal thread to start
Steve Block3856b092011-10-20 11:56:00 +01006282 ALOGV("Signal record thread");
Mathias Agopian65ab4712010-07-14 17:59:35 -07006283 mWaitWorkCV.signal();
6284 // do not wait for mStartStopCond if exiting
Glenn Kastenb28686f2012-01-06 08:39:38 -08006285 if (exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006286 mActiveTrack.clear();
6287 status = INVALID_OPERATION;
6288 goto startError;
6289 }
6290 mStartStopCond.wait(mLock);
6291 if (mActiveTrack == 0) {
Steve Block3856b092011-10-20 11:56:00 +01006292 ALOGV("Record failed to start");
Mathias Agopian65ab4712010-07-14 17:59:35 -07006293 status = BAD_VALUE;
6294 goto startError;
6295 }
Steve Block3856b092011-10-20 11:56:00 +01006296 ALOGV("Record started OK");
Mathias Agopian65ab4712010-07-14 17:59:35 -07006297 return status;
6298 }
6299startError:
6300 AudioSystem::stopInput(mId);
Eric Laurenta011e352012-03-29 15:51:43 -07006301 clearSyncStartEvent();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006302 return status;
6303}
6304
Eric Laurenta011e352012-03-29 15:51:43 -07006305void AudioFlinger::RecordThread::clearSyncStartEvent()
6306{
6307 if (mSyncStartEvent != 0) {
6308 mSyncStartEvent->cancel();
6309 }
6310 mSyncStartEvent.clear();
Eric Laurent29864602012-05-08 18:57:51 -07006311 mFramestoDrop = 0;
Eric Laurenta011e352012-03-29 15:51:43 -07006312}
6313
6314void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event)
6315{
6316 sp<SyncEvent> strongEvent = event.promote();
6317
6318 if (strongEvent != 0) {
6319 RecordThread *me = (RecordThread *)strongEvent->cookie();
6320 me->handleSyncStartEvent(strongEvent);
6321 }
6322}
6323
6324void AudioFlinger::RecordThread::handleSyncStartEvent(const sp<SyncEvent>& event)
6325{
Eric Laurent29864602012-05-08 18:57:51 -07006326 if (event == mSyncStartEvent) {
Eric Laurenta011e352012-03-29 15:51:43 -07006327 // TODO: use actual buffer filling status instead of 2 buffers when info is available
6328 // from audio HAL
6329 mFramestoDrop = mFrameCount * 2;
Eric Laurenta011e352012-03-29 15:51:43 -07006330 }
6331}
6332
Glenn Kasten1d491ff2012-07-16 14:28:13 -07006333bool AudioFlinger::RecordThread::stop_l(RecordThread::RecordTrack* recordTrack) {
Steve Block3856b092011-10-20 11:56:00 +01006334 ALOGV("RecordThread::stop");
Glenn Kasten1d491ff2012-07-16 14:28:13 -07006335 if (recordTrack != mActiveTrack.get() || recordTrack->mState == TrackBase::PAUSING) {
6336 return false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006337 }
Glenn Kasten1d491ff2012-07-16 14:28:13 -07006338 recordTrack->mState = TrackBase::PAUSING;
6339 // do not wait for mStartStopCond if exiting
6340 if (exitPending()) {
6341 return true;
6342 }
6343 mStartStopCond.wait(mLock);
6344 // if we have been restarted, recordTrack == mActiveTrack.get() here
6345 if (exitPending() || recordTrack != mActiveTrack.get()) {
6346 ALOGV("Record stopped OK");
6347 return true;
6348 }
6349 return false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006350}
6351
Eric Laurenta011e352012-03-29 15:51:43 -07006352bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event)
6353{
6354 return false;
6355}
6356
6357status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event)
6358{
6359 if (!isValidSyncEvent(event)) {
6360 return BAD_VALUE;
6361 }
6362
Glenn Kasten510a3d62012-07-16 14:24:34 -07006363 int eventSession = event->triggerSession();
6364 status_t ret = NAME_NOT_FOUND;
6365
Eric Laurenta011e352012-03-29 15:51:43 -07006366 Mutex::Autolock _l(mLock);
6367
Glenn Kasten510a3d62012-07-16 14:24:34 -07006368 for (size_t i = 0; i < mTracks.size(); i++) {
6369 sp<RecordTrack> track = mTracks[i];
6370 if (eventSession == track->sessionId()) {
6371 track->setSyncEvent(event);
6372 ret = NO_ERROR;
6373 }
Eric Laurenta011e352012-03-29 15:51:43 -07006374 }
Glenn Kasten510a3d62012-07-16 14:24:34 -07006375 return ret;
6376}
6377
6378void AudioFlinger::RecordThread::RecordTrack::destroy()
6379{
6380 // see comments at AudioFlinger::PlaybackThread::Track::destroy()
6381 sp<RecordTrack> keep(this);
6382 {
6383 sp<ThreadBase> thread = mThread.promote();
6384 if (thread != 0) {
6385 if (mState == ACTIVE || mState == RESUMING) {
6386 AudioSystem::stopInput(thread->id());
6387 }
6388 AudioSystem::releaseInput(thread->id());
6389 Mutex::Autolock _l(thread->mLock);
6390 RecordThread *recordThread = (RecordThread *) thread.get();
6391 recordThread->destroyTrack_l(this);
6392 }
6393 }
6394}
6395
6396// destroyTrack_l() must be called with ThreadBase::mLock held
6397void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track)
6398{
6399 track->mState = TrackBase::TERMINATED;
6400 // active tracks are removed by threadLoop()
6401 if (mActiveTrack != track) {
6402 removeTrack_l(track);
6403 }
6404}
6405
6406void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track)
6407{
6408 mTracks.remove(track);
6409 // need anything related to effects here?
Eric Laurenta011e352012-03-29 15:51:43 -07006410}
6411
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07006412void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -07006413{
Glenn Kasten510a3d62012-07-16 14:24:34 -07006414 dumpInternals(fd, args);
6415 dumpTracks(fd, args);
6416 dumpEffectChains(fd, args);
6417}
6418
6419void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args)
6420{
Mathias Agopian65ab4712010-07-14 17:59:35 -07006421 const size_t SIZE = 256;
6422 char buffer[SIZE];
6423 String8 result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006424
6425 snprintf(buffer, SIZE, "\nInput thread %p internals\n", this);
6426 result.append(buffer);
6427
6428 if (mActiveTrack != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006429 snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex);
6430 result.append(buffer);
6431 snprintf(buffer, SIZE, "In size: %d\n", mInputBytes);
6432 result.append(buffer);
Glenn Kastene0feee32011-12-13 11:53:26 -08006433 snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL));
Mathias Agopian65ab4712010-07-14 17:59:35 -07006434 result.append(buffer);
6435 snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount);
6436 result.append(buffer);
6437 snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate);
6438 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006439 } else {
Glenn Kasten510a3d62012-07-16 14:24:34 -07006440 result.append("No active record client\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07006441 }
Glenn Kasten510a3d62012-07-16 14:24:34 -07006442
Mathias Agopian65ab4712010-07-14 17:59:35 -07006443 write(fd, result.string(), result.size());
6444
6445 dumpBase(fd, args);
Glenn Kasten510a3d62012-07-16 14:24:34 -07006446}
6447
6448void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args)
6449{
6450 const size_t SIZE = 256;
6451 char buffer[SIZE];
6452 String8 result;
6453
6454 snprintf(buffer, SIZE, "Input thread %p tracks\n", this);
6455 result.append(buffer);
6456 RecordTrack::appendDumpHeader(result);
6457 for (size_t i = 0; i < mTracks.size(); ++i) {
6458 sp<RecordTrack> track = mTracks[i];
6459 if (track != 0) {
6460 track->dump(buffer, SIZE);
6461 result.append(buffer);
6462 }
6463 }
6464
6465 if (mActiveTrack != 0) {
6466 snprintf(buffer, SIZE, "\nInput thread %p active tracks\n", this);
6467 result.append(buffer);
6468 RecordTrack::appendDumpHeader(result);
6469 mActiveTrack->dump(buffer, SIZE);
6470 result.append(buffer);
6471
6472 }
6473 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07006474}
6475
Glenn Kasten01c4ebf2012-02-22 10:47:35 -08006476// AudioBufferProvider interface
John Grossman4ff14ba2012-02-08 16:37:41 -08006477status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07006478{
6479 size_t framesReq = buffer->frameCount;
6480 size_t framesReady = mFrameCount - mRsmpInIndex;
6481 int channelCount;
6482
6483 if (framesReady == 0) {
Dima Zavin799a70e2011-04-18 16:57:27 -07006484 mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006485 if (mBytesRead < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00006486 ALOGE("RecordThread::getNextBuffer() Error reading audio input");
Mathias Agopian65ab4712010-07-14 17:59:35 -07006487 if (mActiveTrack->mState == TrackBase::ACTIVE) {
6488 // Force input into standby so that it tries to
6489 // recover at next read attempt
Glenn Kastene4e2a372012-07-23 12:55:09 -07006490 inputStandBy();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006491 usleep(kRecordThreadSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006492 }
Glenn Kastene0feee32011-12-13 11:53:26 -08006493 buffer->raw = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006494 buffer->frameCount = 0;
6495 return NOT_ENOUGH_DATA;
6496 }
6497 mRsmpInIndex = 0;
6498 framesReady = mFrameCount;
6499 }
6500
6501 if (framesReq > framesReady) {
6502 framesReq = framesReady;
6503 }
6504
6505 if (mChannelCount == 1 && mReqChannelCount == 2) {
6506 channelCount = 1;
6507 } else {
6508 channelCount = 2;
6509 }
6510 buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount;
6511 buffer->frameCount = framesReq;
6512 return NO_ERROR;
6513}
6514
Glenn Kasten01c4ebf2012-02-22 10:47:35 -08006515// AudioBufferProvider interface
Mathias Agopian65ab4712010-07-14 17:59:35 -07006516void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer)
6517{
6518 mRsmpInIndex += buffer->frameCount;
6519 buffer->frameCount = 0;
6520}
6521
6522bool AudioFlinger::RecordThread::checkForNewParameters_l()
6523{
6524 bool reconfig = false;
6525
6526 while (!mNewParameters.isEmpty()) {
6527 status_t status = NO_ERROR;
6528 String8 keyValuePair = mNewParameters[0];
6529 AudioParameter param = AudioParameter(keyValuePair);
6530 int value;
Glenn Kasten58f30212012-01-12 12:27:51 -08006531 audio_format_t reqFormat = mFormat;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006532 int reqSamplingRate = mReqSampleRate;
6533 int reqChannelCount = mReqChannelCount;
6534
6535 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
6536 reqSamplingRate = value;
6537 reconfig = true;
6538 }
6539 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
Glenn Kasten58f30212012-01-12 12:27:51 -08006540 reqFormat = (audio_format_t) value;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006541 reconfig = true;
6542 }
6543 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
Dima Zavinfce7a472011-04-19 22:30:36 -07006544 reqChannelCount = popcount(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006545 reconfig = true;
6546 }
6547 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
6548 // do not accept frame count changes if tracks are open as the track buffer
Glenn Kasten99e53b82012-01-19 08:59:58 -08006549 // size depends on frame count and correct behavior would not be guaranteed
Mathias Agopian65ab4712010-07-14 17:59:35 -07006550 // if frame count is changed after track creation
6551 if (mActiveTrack != 0) {
6552 status = INVALID_OPERATION;
6553 } else {
6554 reconfig = true;
6555 }
6556 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006557 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
6558 // forward device change to effects that have requested to be
6559 // aware of attached audio device.
6560 for (size_t i = 0; i < mEffectChains.size(); i++) {
6561 mEffectChains[i]->setDevice_l(value);
6562 }
6563 // store input device and output device but do not forward output device to audio HAL.
6564 // Note that status is ignored by the caller for output device
6565 // (see AudioFlinger::setParameters()
Glenn Kasten5ad92f62012-07-19 10:02:15 -07006566 audio_devices_t newDevice = mDevice;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006567 if (value & AUDIO_DEVICE_OUT_ALL) {
Glenn Kasten5ad92f62012-07-19 10:02:15 -07006568 newDevice &= ~(value & AUDIO_DEVICE_OUT_ALL);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006569 status = BAD_VALUE;
6570 } else {
Glenn Kasten5ad92f62012-07-19 10:02:15 -07006571 newDevice &= ~(value & AUDIO_DEVICE_IN_ALL);
Eric Laurent59bd0da2011-08-01 09:52:20 -07006572 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
Glenn Kasten510a3d62012-07-16 14:24:34 -07006573 if (mTracks.size() > 0) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07006574 bool suspend = audio_is_bluetooth_sco_device(
Eric Laurentbee53372011-08-29 12:42:48 -07006575 (audio_devices_t)value) && mAudioFlinger->btNrecIsOff();
Glenn Kasten510a3d62012-07-16 14:24:34 -07006576 for (size_t i = 0; i < mTracks.size(); i++) {
6577 sp<RecordTrack> track = mTracks[i];
6578 setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId());
6579 setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId());
6580 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07006581 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006582 }
Glenn Kasten01542f22012-07-02 12:46:15 -07006583 newDevice |= value;
Glenn Kasten5ad92f62012-07-19 10:02:15 -07006584 mDevice = newDevice; // since mDevice is read by other threads, only write to it once
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006585 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006586 if (status == NO_ERROR) {
Dima Zavin799a70e2011-04-18 16:57:27 -07006587 status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07006588 if (status == INVALID_OPERATION) {
Glenn Kastene4e2a372012-07-23 12:55:09 -07006589 inputStandBy();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07006590 status = mInput->stream->common.set_parameters(&mInput->stream->common,
6591 keyValuePair.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07006592 }
6593 if (reconfig) {
6594 if (status == BAD_VALUE &&
Dima Zavin799a70e2011-04-18 16:57:27 -07006595 reqFormat == mInput->stream->common.get_format(&mInput->stream->common) &&
Dima Zavinfce7a472011-04-19 22:30:36 -07006596 reqFormat == AUDIO_FORMAT_PCM_16_BIT &&
Dima Zavin799a70e2011-04-18 16:57:27 -07006597 ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) &&
Glenn Kasten53d76db2012-03-08 12:32:47 -08006598 popcount(mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_2 &&
6599 (reqChannelCount <= FCC_2)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006600 status = NO_ERROR;
6601 }
6602 if (status == NO_ERROR) {
6603 readInputParameters();
6604 sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED);
6605 }
6606 }
6607 }
6608
6609 mNewParameters.removeAt(0);
6610
6611 mParamStatus = status;
6612 mParamCond.signal();
Eric Laurent60cd0a02011-09-13 11:40:21 -07006613 // wait for condition with time out in case the thread calling ThreadBase::setParameters()
6614 // already timed out waiting for the status and will never signal the condition.
Glenn Kasten7dede872011-12-13 11:04:14 -08006615 mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006616 }
6617 return reconfig;
6618}
6619
6620String8 AudioFlinger::RecordThread::getParameters(const String8& keys)
6621{
Dima Zavinfce7a472011-04-19 22:30:36 -07006622 char *s;
Eric Laurentb8ba0a92011-08-07 16:32:26 -07006623 String8 out_s8 = String8();
6624
6625 Mutex::Autolock _l(mLock);
6626 if (initCheck() != NO_ERROR) {
6627 return out_s8;
6628 }
Dima Zavinfce7a472011-04-19 22:30:36 -07006629
Dima Zavin799a70e2011-04-18 16:57:27 -07006630 s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string());
Dima Zavinfce7a472011-04-19 22:30:36 -07006631 out_s8 = String8(s);
6632 free(s);
6633 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006634}
6635
6636void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) {
6637 AudioSystem::OutputDescriptor desc;
Glenn Kastena0d68332012-01-27 16:47:15 -08006638 void *param2 = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006639
6640 switch (event) {
6641 case AudioSystem::INPUT_OPENED:
6642 case AudioSystem::INPUT_CONFIG_CHANGED:
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07006643 desc.channels = mChannelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006644 desc.samplingRate = mSampleRate;
6645 desc.format = mFormat;
6646 desc.frameCount = mFrameCount;
6647 desc.latency = 0;
6648 param2 = &desc;
6649 break;
6650
6651 case AudioSystem::INPUT_CLOSED:
6652 default:
6653 break;
6654 }
6655 mAudioFlinger->audioConfigChanged_l(event, mId, param2);
6656}
6657
6658void AudioFlinger::RecordThread::readInputParameters()
6659{
Glenn Kastene9dd0172012-01-27 18:08:45 -08006660 delete mRsmpInBuffer;
6661 // mRsmpInBuffer is always assigned a new[] below
6662 delete mRsmpOutBuffer;
6663 mRsmpOutBuffer = NULL;
6664 delete mResampler;
Glenn Kastene0feee32011-12-13 11:53:26 -08006665 mResampler = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006666
Dima Zavin799a70e2011-04-18 16:57:27 -07006667 mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common);
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07006668 mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common);
6669 mChannelCount = (uint16_t)popcount(mChannelMask);
Dima Zavin799a70e2011-04-18 16:57:27 -07006670 mFormat = mInput->stream->common.get_format(&mInput->stream->common);
Glenn Kastenb9980652012-01-11 09:48:27 -08006671 mFrameSize = audio_stream_frame_size(&mInput->stream->common);
Dima Zavin799a70e2011-04-18 16:57:27 -07006672 mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006673 mFrameCount = mInputBytes / mFrameSize;
Glenn Kasten58912562012-04-03 10:45:00 -07006674 mNormalFrameCount = mFrameCount; // not used by record, but used by input effects
Mathias Agopian65ab4712010-07-14 17:59:35 -07006675 mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount];
6676
Glenn Kasten53d76db2012-03-08 12:32:47 -08006677 if (mSampleRate != mReqSampleRate && mChannelCount <= FCC_2 && mReqChannelCount <= FCC_2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07006678 {
6679 int channelCount;
Glenn Kastene53b9ea2012-03-12 16:29:55 -07006680 // optimization: if mono to mono, use the resampler in stereo to stereo mode to avoid
6681 // stereo to mono post process as the resampler always outputs stereo.
Mathias Agopian65ab4712010-07-14 17:59:35 -07006682 if (mChannelCount == 1 && mReqChannelCount == 2) {
6683 channelCount = 1;
6684 } else {
6685 channelCount = 2;
6686 }
6687 mResampler = AudioResampler::create(16, channelCount, mReqSampleRate);
6688 mResampler->setSampleRate(mSampleRate);
6689 mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN);
6690 mRsmpOutBuffer = new int32_t[mFrameCount * 2];
6691
6692 // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples
6693 if (mChannelCount == 1 && mReqChannelCount == 1) {
6694 mFrameCount >>= 1;
6695 }
6696
6697 }
6698 mRsmpInIndex = mFrameCount;
6699}
6700
6701unsigned int AudioFlinger::RecordThread::getInputFramesLost()
6702{
Eric Laurentb8ba0a92011-08-07 16:32:26 -07006703 Mutex::Autolock _l(mLock);
6704 if (initCheck() != NO_ERROR) {
6705 return 0;
6706 }
6707
Dima Zavin799a70e2011-04-18 16:57:27 -07006708 return mInput->stream->get_input_frames_lost(mInput->stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006709}
6710
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006711uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId)
6712{
6713 Mutex::Autolock _l(mLock);
6714 uint32_t result = 0;
6715 if (getEffectChain_l(sessionId) != 0) {
6716 result = EFFECT_SESSION;
6717 }
6718
Glenn Kasten510a3d62012-07-16 14:24:34 -07006719 for (size_t i = 0; i < mTracks.size(); ++i) {
6720 if (sessionId == mTracks[i]->sessionId()) {
6721 result |= TRACK_SESSION;
6722 break;
6723 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006724 }
6725
6726 return result;
6727}
6728
Glenn Kasten510a3d62012-07-16 14:24:34 -07006729KeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds()
Eric Laurent59bd0da2011-08-01 09:52:20 -07006730{
Glenn Kasten510a3d62012-07-16 14:24:34 -07006731 KeyedVector<int, bool> ids;
Eric Laurent59bd0da2011-08-01 09:52:20 -07006732 Mutex::Autolock _l(mLock);
Glenn Kasten510a3d62012-07-16 14:24:34 -07006733 for (size_t j = 0; j < mTracks.size(); ++j) {
6734 sp<RecordThread::RecordTrack> track = mTracks[j];
6735 int sessionId = track->sessionId();
6736 if (ids.indexOfKey(sessionId) < 0) {
6737 ids.add(sessionId, true);
6738 }
6739 }
6740 return ids;
Eric Laurent59bd0da2011-08-01 09:52:20 -07006741}
6742
Eric Laurentb8ba0a92011-08-07 16:32:26 -07006743AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput()
6744{
6745 Mutex::Autolock _l(mLock);
6746 AudioStreamIn *input = mInput;
6747 mInput = NULL;
6748 return input;
6749}
6750
6751// this method must always be called either with ThreadBase mLock held or inside the thread loop
Glenn Kasten0bf65bd2012-02-28 18:32:53 -08006752audio_stream_t* AudioFlinger::RecordThread::stream() const
Eric Laurentb8ba0a92011-08-07 16:32:26 -07006753{
6754 if (mInput == NULL) {
6755 return NULL;
6756 }
6757 return &mInput->stream->common;
6758}
6759
6760
Mathias Agopian65ab4712010-07-14 17:59:35 -07006761// ----------------------------------------------------------------------------
6762
Eric Laurenta4c5a552012-03-29 10:12:40 -07006763audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
6764{
6765 if (!settingsAllowed()) {
6766 return 0;
6767 }
6768 Mutex::Autolock _l(mLock);
6769 return loadHwModule_l(name);
6770}
6771
6772// loadHwModule_l() must be called with AudioFlinger::mLock held
6773audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
6774{
6775 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
6776 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
6777 ALOGW("loadHwModule() module %s already loaded", name);
6778 return mAudioHwDevs.keyAt(i);
6779 }
6780 }
6781
Eric Laurenta4c5a552012-03-29 10:12:40 -07006782 audio_hw_device_t *dev;
6783
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07006784 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07006785 if (rc) {
6786 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
6787 return 0;
6788 }
6789
6790 mHardwareStatus = AUDIO_HW_INIT;
6791 rc = dev->init_check(dev);
6792 mHardwareStatus = AUDIO_HW_IDLE;
6793 if (rc) {
6794 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
6795 return 0;
6796 }
6797
John Grossmanee578c02012-07-23 17:05:46 -07006798 // Check and cache this HAL's level of support for master mute and master
6799 // volume. If this is the first HAL opened, and it supports the get
6800 // methods, use the initial values provided by the HAL as the current
6801 // master mute and volume settings.
6802
6803 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
6804 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07006805 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07006806
6807 if (0 == mAudioHwDevs.size()) {
6808 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
6809 if (NULL != dev->get_master_volume) {
6810 float mv;
6811 if (OK == dev->get_master_volume(dev, &mv)) {
6812 mMasterVolume = mv;
6813 }
6814 }
6815
6816 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
6817 if (NULL != dev->get_master_mute) {
6818 bool mm;
6819 if (OK == dev->get_master_mute(dev, &mm)) {
6820 mMasterMute = mm;
6821 }
6822 }
6823 }
6824
Eric Laurenta4c5a552012-03-29 10:12:40 -07006825 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07006826 if ((NULL != dev->set_master_volume) &&
6827 (OK == dev->set_master_volume(dev, mMasterVolume))) {
6828 flags = static_cast<AudioHwDevice::Flags>(flags |
6829 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
6830 }
6831
6832 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
6833 if ((NULL != dev->set_master_mute) &&
6834 (OK == dev->set_master_mute(dev, mMasterMute))) {
6835 flags = static_cast<AudioHwDevice::Flags>(flags |
6836 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
6837 }
6838
Eric Laurenta4c5a552012-03-29 10:12:40 -07006839 mHardwareStatus = AUDIO_HW_IDLE;
6840 }
6841
6842 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07006843 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07006844
6845 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07006846 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07006847
6848 return handle;
6849
6850}
6851
6852audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
6853 audio_devices_t *pDevices,
6854 uint32_t *pSamplingRate,
6855 audio_format_t *pFormat,
6856 audio_channel_mask_t *pChannelMask,
6857 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07006858 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07006859{
6860 status_t status;
6861 PlaybackThread *thread = NULL;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07006862 struct audio_config config = {
6863 sample_rate: pSamplingRate ? *pSamplingRate : 0,
6864 channel_mask: pChannelMask ? *pChannelMask : 0,
6865 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT,
6866 };
6867 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07006868 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006869
Eric Laurenta4c5a552012-03-29 10:12:40 -07006870 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
6871 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07006872 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07006873 config.sample_rate,
6874 config.format,
6875 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07006876 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006877
6878 if (pDevices == NULL || *pDevices == 0) {
6879 return 0;
6880 }
Dima Zavin799a70e2011-04-18 16:57:27 -07006881
Mathias Agopian65ab4712010-07-14 17:59:35 -07006882 Mutex::Autolock _l(mLock);
6883
Eric Laurenta4c5a552012-03-29 10:12:40 -07006884 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07006885 if (outHwDev == NULL)
6886 return 0;
6887
John Grossmanee578c02012-07-23 17:05:46 -07006888 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07006889 audio_io_handle_t id = nextUniqueId();
6890
Glenn Kasten8abf44d2012-02-02 14:16:03 -08006891 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07006892
John Grossmanee578c02012-07-23 17:05:46 -07006893 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07006894 id,
6895 *pDevices,
6896 (audio_output_flags_t)flags,
6897 &config,
6898 &outStream);
6899
Glenn Kasten8abf44d2012-02-02 14:16:03 -08006900 mHardwareStatus = AUDIO_HW_IDLE;
Steve Block3856b092011-10-20 11:56:00 +01006901 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07006902 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07006903 config.sample_rate,
6904 config.format,
6905 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07006906 status);
6907
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07006908 if (status == NO_ERROR && outStream != NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07006909 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07006910
Eric Laurent0ca3cf92012-04-18 09:24:29 -07006911 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07006912 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
6913 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006914 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01006915 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006916 } else {
6917 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01006918 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006919 }
6920 mPlaybackThreads.add(id, thread);
6921
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07006922 if (pSamplingRate != NULL) *pSamplingRate = config.sample_rate;
6923 if (pFormat != NULL) *pFormat = config.format;
6924 if (pChannelMask != NULL) *pChannelMask = config.channel_mask;
Glenn Kastena0d68332012-01-27 16:47:15 -08006925 if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006926
6927 // notify client processes of the new output creation
6928 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07006929
6930 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07006931 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07006932 ALOGI("Using module %d has the primary audio interface", module);
6933 mPrimaryHardwareDev = outHwDev;
6934
6935 AutoMutex lock(mHardwareLock);
6936 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07006937 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07006938 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07006939 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07006940 return id;
6941 }
6942
6943 return 0;
6944}
6945
Glenn Kasten72ef00d2012-01-17 11:09:42 -08006946audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
6947 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07006948{
6949 Mutex::Autolock _l(mLock);
6950 MixerThread *thread1 = checkMixerThread_l(output1);
6951 MixerThread *thread2 = checkMixerThread_l(output2);
6952
6953 if (thread1 == NULL || thread2 == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00006954 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006955 return 0;
6956 }
6957
Glenn Kasten72ef00d2012-01-17 11:09:42 -08006958 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07006959 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
6960 thread->addOutputTrack(thread2);
6961 mPlaybackThreads.add(id, thread);
6962 // notify client processes of the new output creation
6963 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
6964 return id;
6965}
6966
Glenn Kasten72ef00d2012-01-17 11:09:42 -08006967status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07006968{
Glenn Kastend96c5722012-04-25 13:44:49 -07006969 return closeOutput_nonvirtual(output);
6970}
6971
6972status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
6973{
Mathias Agopian65ab4712010-07-14 17:59:35 -07006974 // keep strong reference on the playback thread so that
6975 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07006976 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07006977 {
6978 Mutex::Autolock _l(mLock);
6979 thread = checkPlaybackThread_l(output);
6980 if (thread == NULL) {
6981 return BAD_VALUE;
6982 }
6983
Steve Block3856b092011-10-20 11:56:00 +01006984 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006985
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006986 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006987 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07006988 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07006989 DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
6990 dupThread->removeOutputTrack((MixerThread *)thread.get());
6991 }
6992 }
6993 }
Glenn Kastena1117922012-01-26 10:53:32 -08006994 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07006995 mPlaybackThreads.removeItem(output);
6996 }
6997 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08006998 // The thread entity (active unit of execution) is no longer running here,
6999 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07007000
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007001 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07007002 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08007003 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07007004 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07007005 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07007006 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007007 }
7008 return NO_ERROR;
7009}
7010
Glenn Kasten72ef00d2012-01-17 11:09:42 -08007011status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007012{
7013 Mutex::Autolock _l(mLock);
7014 PlaybackThread *thread = checkPlaybackThread_l(output);
7015
7016 if (thread == NULL) {
7017 return BAD_VALUE;
7018 }
7019
Steve Block3856b092011-10-20 11:56:00 +01007020 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007021 thread->suspend();
7022
7023 return NO_ERROR;
7024}
7025
Glenn Kasten72ef00d2012-01-17 11:09:42 -08007026status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007027{
7028 Mutex::Autolock _l(mLock);
7029 PlaybackThread *thread = checkPlaybackThread_l(output);
7030
7031 if (thread == NULL) {
7032 return BAD_VALUE;
7033 }
7034
Steve Block3856b092011-10-20 11:56:00 +01007035 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007036
7037 thread->restore();
7038
7039 return NO_ERROR;
7040}
7041
Eric Laurenta4c5a552012-03-29 10:12:40 -07007042audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
7043 audio_devices_t *pDevices,
7044 uint32_t *pSamplingRate,
7045 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07007046 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007047{
7048 status_t status;
7049 RecordThread *thread = NULL;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07007050 struct audio_config config = {
7051 sample_rate: pSamplingRate ? *pSamplingRate : 0,
7052 channel_mask: pChannelMask ? *pChannelMask : 0,
7053 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT,
7054 };
7055 uint32_t reqSamplingRate = config.sample_rate;
7056 audio_format_t reqFormat = config.format;
7057 audio_channel_mask_t reqChannels = config.channel_mask;
7058 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07007059 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007060
7061 if (pDevices == NULL || *pDevices == 0) {
7062 return 0;
7063 }
Dima Zavin799a70e2011-04-18 16:57:27 -07007064
Mathias Agopian65ab4712010-07-14 17:59:35 -07007065 Mutex::Autolock _l(mLock);
7066
Eric Laurenta4c5a552012-03-29 10:12:40 -07007067 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07007068 if (inHwDev == NULL)
7069 return 0;
7070
John Grossmanee578c02012-07-23 17:05:46 -07007071 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07007072 audio_io_handle_t id = nextUniqueId();
7073
John Grossmanee578c02012-07-23 17:05:46 -07007074 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07007075 &inStream);
Eric Laurenta4c5a552012-03-29 10:12:40 -07007076 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07007077 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07007078 config.sample_rate,
7079 config.format,
7080 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07007081 status);
7082
7083 // If the input could not be opened with the requested parameters and we can handle the conversion internally,
7084 // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo
7085 // or stereo to mono conversions on 16 bit PCM inputs.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07007086 if (status == BAD_VALUE &&
7087 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
7088 (config.sample_rate <= 2 * reqSamplingRate) &&
7089 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07007090 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07007091 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07007092 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007093 }
7094
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07007095 if (status == NO_ERROR && inStream != NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07007096 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
7097
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007098 // Start record thread
7099 // RecorThread require both input and output device indication to forward to audio
7100 // pre processing modules
Glenn Kastenbb4350d2012-07-03 15:56:38 -07007101 audio_devices_t device = (*pDevices) | primaryOutputDevice_l();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007102 thread = new RecordThread(this,
7103 input,
7104 reqSamplingRate,
7105 reqChannels,
7106 id,
7107 device);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007108 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01007109 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kastena0d68332012-01-27 16:47:15 -08007110 if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07007111 if (pFormat != NULL) *pFormat = config.format;
Eric Laurenta4c5a552012-03-29 10:12:40 -07007112 if (pChannelMask != NULL) *pChannelMask = reqChannels;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007113
Mathias Agopian65ab4712010-07-14 17:59:35 -07007114 // notify client processes of the new input creation
7115 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
7116 return id;
7117 }
7118
7119 return 0;
7120}
7121
Glenn Kasten72ef00d2012-01-17 11:09:42 -08007122status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007123{
Glenn Kastend96c5722012-04-25 13:44:49 -07007124 return closeInput_nonvirtual(input);
7125}
7126
7127status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
7128{
Mathias Agopian65ab4712010-07-14 17:59:35 -07007129 // keep strong reference on the record thread so that
7130 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07007131 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007132 {
7133 Mutex::Autolock _l(mLock);
7134 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07007135 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07007136 return BAD_VALUE;
7137 }
7138
Steve Block3856b092011-10-20 11:56:00 +01007139 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08007140 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007141 mRecordThreads.removeItem(input);
7142 }
7143 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08007144 // The thread entity (active unit of execution) is no longer running here,
7145 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07007146
Eric Laurentb8ba0a92011-08-07 16:32:26 -07007147 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08007148 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07007149 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07007150 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07007151 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007152
7153 return NO_ERROR;
7154}
7155
Glenn Kasten72ef00d2012-01-17 11:09:42 -08007156status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007157{
7158 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01007159 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007160
7161 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
7162 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07007163 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07007164 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07007165
7166 return NO_ERROR;
7167}
7168
7169
7170int AudioFlinger::newAudioSessionId()
7171{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007172 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07007173}
7174
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007175void AudioFlinger::acquireAudioSessionId(int audioSession)
7176{
7177 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08007178 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01007179 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08007180 size_t num = mAudioSessionRefs.size();
7181 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007182 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08007183 if (ref->mSessionid == audioSession && ref->mPid == caller) {
7184 ref->mCnt++;
7185 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007186 return;
7187 }
7188 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08007189 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
7190 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007191}
7192
7193void AudioFlinger::releaseAudioSessionId(int audioSession)
7194{
7195 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08007196 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01007197 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08007198 size_t num = mAudioSessionRefs.size();
7199 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007200 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08007201 if (ref->mSessionid == audioSession && ref->mPid == caller) {
7202 ref->mCnt--;
7203 ALOGV(" decremented refcount to %d", ref->mCnt);
7204 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007205 mAudioSessionRefs.removeAt(i);
7206 delete ref;
7207 purgeStaleEffects_l();
7208 }
7209 return;
7210 }
7211 }
Steve Block5ff1dd52012-01-05 23:22:43 +00007212 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007213}
7214
7215void AudioFlinger::purgeStaleEffects_l() {
7216
Steve Block3856b092011-10-20 11:56:00 +01007217 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007218
7219 Vector< sp<EffectChain> > chains;
7220
7221 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
7222 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
7223 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
7224 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07007225 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
7226 chains.push(ec);
7227 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007228 }
7229 }
7230 for (size_t i = 0; i < mRecordThreads.size(); i++) {
7231 sp<RecordThread> t = mRecordThreads.valueAt(i);
7232 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
7233 sp<EffectChain> ec = t->mEffectChains[j];
7234 chains.push(ec);
7235 }
7236 }
7237
7238 for (size_t i = 0; i < chains.size(); i++) {
7239 sp<EffectChain> ec = chains[i];
7240 int sessionid = ec->sessionId();
7241 sp<ThreadBase> t = ec->mThread.promote();
7242 if (t == 0) {
7243 continue;
7244 }
7245 size_t numsessionrefs = mAudioSessionRefs.size();
7246 bool found = false;
7247 for (size_t k = 0; k < numsessionrefs; k++) {
7248 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08007249 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01007250 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07007251 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007252 found = true;
7253 break;
7254 }
7255 }
7256 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07007257 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007258 // remove all effects from the chain
7259 while (ec->mEffects.size()) {
7260 sp<EffectModule> effect = ec->mEffects[0];
7261 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007262 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07007263 if (effect->purgeHandles()) {
7264 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007265 }
7266 AudioSystem::unregisterEffect(effect->id());
7267 }
7268 }
7269 }
7270 return;
7271}
7272
Mathias Agopian65ab4712010-07-14 17:59:35 -07007273// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08007274AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07007275{
Glenn Kastena1117922012-01-26 10:53:32 -08007276 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07007277}
7278
7279// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08007280AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07007281{
7282 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08007283 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007284}
7285
7286// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08007287AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07007288{
Glenn Kastena1117922012-01-26 10:53:32 -08007289 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07007290}
7291
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007292uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07007293{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007294 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007295}
7296
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08007297AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007298{
7299 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
7300 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07007301 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07007302 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007303 return thread;
7304 }
7305 }
7306 return NULL;
7307}
7308
Glenn Kastenbb4350d2012-07-03 15:56:38 -07007309audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007310{
7311 PlaybackThread *thread = primaryPlaybackThread_l();
7312
7313 if (thread == NULL) {
7314 return 0;
7315 }
7316
7317 return thread->device();
7318}
7319
Eric Laurenta011e352012-03-29 15:51:43 -07007320sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
7321 int triggerSession,
7322 int listenerSession,
7323 sync_event_callback_t callBack,
7324 void *cookie)
7325{
7326 Mutex::Autolock _l(mLock);
7327
7328 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
7329 status_t playStatus = NAME_NOT_FOUND;
7330 status_t recStatus = NAME_NOT_FOUND;
7331 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
7332 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
7333 if (playStatus == NO_ERROR) {
7334 return event;
7335 }
7336 }
7337 for (size_t i = 0; i < mRecordThreads.size(); i++) {
7338 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
7339 if (recStatus == NO_ERROR) {
7340 return event;
7341 }
7342 }
7343 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
7344 mPendingSyncEvents.add(event);
7345 } else {
7346 ALOGV("createSyncEvent() invalid event %d", event->type());
7347 event.clear();
7348 }
7349 return event;
7350}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007351
Mathias Agopian65ab4712010-07-14 17:59:35 -07007352// ----------------------------------------------------------------------------
7353// Effect management
7354// ----------------------------------------------------------------------------
7355
7356
Glenn Kastenf587ba52012-01-26 16:25:10 -08007357status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07007358{
7359 Mutex::Autolock _l(mLock);
7360 return EffectQueryNumberEffects(numEffects);
7361}
7362
Glenn Kastenf587ba52012-01-26 16:25:10 -08007363status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07007364{
7365 Mutex::Autolock _l(mLock);
7366 return EffectQueryEffect(index, descriptor);
7367}
7368
Glenn Kasten5e92a782012-01-30 07:40:52 -08007369status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08007370 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07007371{
7372 Mutex::Autolock _l(mLock);
7373 return EffectGetDescriptor(pUuid, descriptor);
7374}
7375
7376
Mathias Agopian65ab4712010-07-14 17:59:35 -07007377sp<IEffect> AudioFlinger::createEffect(pid_t pid,
7378 effect_descriptor_t *pDesc,
7379 const sp<IEffectClient>& effectClient,
7380 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08007381 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07007382 int sessionId,
7383 status_t *status,
7384 int *id,
7385 int *enabled)
7386{
7387 status_t lStatus = NO_ERROR;
7388 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007389 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007390
Glenn Kasten98ec94c2012-01-25 14:28:29 -08007391 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007392 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007393
7394 if (pDesc == NULL) {
7395 lStatus = BAD_VALUE;
7396 goto Exit;
7397 }
7398
Eric Laurent84e9a102010-09-23 16:10:16 -07007399 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07007400 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07007401 lStatus = PERMISSION_DENIED;
7402 goto Exit;
7403 }
7404
Dima Zavinfce7a472011-04-19 22:30:36 -07007405 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07007406 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08007407 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07007408 lStatus = PERMISSION_DENIED;
7409 goto Exit;
7410 }
7411
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007412 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07007413 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07007414 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07007415 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07007416 lStatus = BAD_VALUE;
7417 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07007418 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07007419 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007420 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07007421 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007422 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07007423 }
7424 }
7425
Mathias Agopian65ab4712010-07-14 17:59:35 -07007426 {
7427 Mutex::Autolock _l(mLock);
7428
Mathias Agopian65ab4712010-07-14 17:59:35 -07007429
7430 if (!EffectIsNullUuid(&pDesc->uuid)) {
7431 // if uuid is specified, request effect descriptor
7432 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
7433 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007434 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007435 goto Exit;
7436 }
7437 } else {
7438 // if uuid is not specified, look for an available implementation
7439 // of the required type in effect factory
7440 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007441 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07007442 lStatus = BAD_VALUE;
7443 goto Exit;
7444 }
7445 uint32_t numEffects = 0;
7446 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007447 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07007448 bool found = false;
7449
7450 lStatus = EffectQueryNumberEffects(&numEffects);
7451 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007452 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007453 goto Exit;
7454 }
7455 for (uint32_t i = 0; i < numEffects; i++) {
7456 lStatus = EffectQueryEffect(i, &desc);
7457 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007458 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007459 continue;
7460 }
7461 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
7462 // If matching type found save effect descriptor. If the session is
7463 // 0 and the effect is not auxiliary, continue enumeration in case
7464 // an auxiliary version of this effect type is available
7465 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08007466 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07007467 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07007468 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
7469 break;
7470 }
7471 }
7472 }
7473 if (!found) {
7474 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00007475 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07007476 goto Exit;
7477 }
7478 // For same effect type, chose auxiliary version over insert version if
7479 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07007480 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07007481 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08007482 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007483 }
7484 }
7485
7486 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07007487 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07007488 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
7489 lStatus = INVALID_OPERATION;
7490 goto Exit;
7491 }
7492
Eric Laurent59255e42011-07-27 19:49:51 -07007493 // check recording permission for visualizer
7494 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
7495 !recordingAllowed()) {
7496 lStatus = PERMISSION_DENIED;
7497 goto Exit;
7498 }
7499
Mathias Agopian65ab4712010-07-14 17:59:35 -07007500 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08007501 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007502
7503 // If output is not specified try to find a matching audio session ID in one of the
7504 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07007505 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
7506 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007507 // Note: io is never 0 when creating an effect on an input
7508 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07007509 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07007510 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
7511 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007512 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07007513 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07007514 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07007515 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007516 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07007517 for (size_t i = 0; i < mRecordThreads.size(); i++) {
7518 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
7519 io = mRecordThreads.keyAt(i);
7520 break;
7521 }
7522 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007523 }
Eric Laurent84e9a102010-09-23 16:10:16 -07007524 // If no output thread contains the requested session ID, default to
7525 // first output. The effect chain will be moved to the correct output
7526 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007527 if (io == 0 && mPlaybackThreads.size()) {
7528 io = mPlaybackThreads.keyAt(0);
7529 }
Steve Block3856b092011-10-20 11:56:00 +01007530 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007531 }
7532 ThreadBase *thread = checkRecordThread_l(io);
7533 if (thread == NULL) {
7534 thread = checkPlaybackThread_l(io);
7535 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00007536 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007537 lStatus = BAD_VALUE;
7538 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07007539 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07007540 }
Eric Laurent84e9a102010-09-23 16:10:16 -07007541
Glenn Kasten98ec94c2012-01-25 14:28:29 -08007542 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007543
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007544 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07007545 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
7546 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007547 if (handle != 0 && id != NULL) {
7548 *id = handle->id();
7549 }
7550 }
7551
7552Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07007553 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07007554 *status = lStatus;
7555 }
7556 return handle;
7557}
7558
Glenn Kasten72ef00d2012-01-17 11:09:42 -08007559status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
7560 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07007561{
Steve Block3856b092011-10-20 11:56:00 +01007562 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07007563 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07007564 Mutex::Autolock _l(mLock);
7565 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007566 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07007567 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007568 }
Eric Laurentde070132010-07-13 04:45:46 -07007569 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
7570 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007571 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07007572 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007573 }
Eric Laurentde070132010-07-13 04:45:46 -07007574 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
7575 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007576 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07007577 return BAD_VALUE;
7578 }
7579
7580 Mutex::Autolock _dl(dstThread->mLock);
7581 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07007582 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07007583
Mathias Agopian65ab4712010-07-14 17:59:35 -07007584 return NO_ERROR;
7585}
7586
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007587// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07007588status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07007589 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07007590 AudioFlinger::PlaybackThread *dstThread,
7591 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07007592{
Steve Block3856b092011-10-20 11:56:00 +01007593 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07007594 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07007595
Eric Laurent59255e42011-07-27 19:49:51 -07007596 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07007597 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007598 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07007599 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07007600 return INVALID_OPERATION;
7601 }
7602
Eric Laurent39e94f82010-07-28 01:32:47 -07007603 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07007604 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07007605 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07007606 // removed.
7607 srcThread->removeEffectChain_l(chain);
7608
7609 // transfer all effects one by one so that new effect chain is created on new thread with
7610 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08007611 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07007612 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007613 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07007614 sp<EffectModule> effect = chain->getEffectFromId_l(0);
7615 while (effect != 0) {
7616 srcThread->removeEffect_l(effect);
7617 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07007618 // removeEffect_l() has stopped the effect if it was active so it must be restarted
7619 if (effect->state() == EffectModule::ACTIVE ||
7620 effect->state() == EffectModule::STOPPING) {
7621 effect->start();
7622 }
Eric Laurent39e94f82010-07-28 01:32:47 -07007623 // if the move request is not received from audio policy manager, the effect must be
7624 // re-registered with the new strategy and output
7625 if (dstChain == 0) {
7626 dstChain = effect->chain().promote();
7627 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007628 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07007629 srcThread->addEffect_l(effect);
7630 return NO_INIT;
7631 }
7632 strategy = dstChain->strategy();
7633 }
7634 if (reRegister) {
7635 AudioSystem::unregisterEffect(effect->id());
7636 AudioSystem::registerEffect(&effect->desc(),
7637 dstOutput,
7638 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07007639 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07007640 effect->id());
7641 }
Eric Laurentde070132010-07-13 04:45:46 -07007642 effect = chain->getEffectFromId_l(0);
7643 }
7644
7645 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007646}
7647
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007648
Mathias Agopian65ab4712010-07-14 17:59:35 -07007649// PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007650sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l(
Mathias Agopian65ab4712010-07-14 17:59:35 -07007651 const sp<AudioFlinger::Client>& client,
7652 const sp<IEffectClient>& effectClient,
7653 int32_t priority,
7654 int sessionId,
7655 effect_descriptor_t *desc,
7656 int *enabled,
7657 status_t *status
7658 )
7659{
7660 sp<EffectModule> effect;
7661 sp<EffectHandle> handle;
7662 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007663 sp<EffectChain> chain;
Eric Laurentde070132010-07-13 04:45:46 -07007664 bool chainCreated = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007665 bool effectCreated = false;
7666 bool effectRegistered = false;
7667
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007668 lStatus = initCheck();
7669 if (lStatus != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007670 ALOGW("createEffect_l() Audio driver not initialized.");
Mathias Agopian65ab4712010-07-14 17:59:35 -07007671 goto Exit;
7672 }
7673
7674 // Do not allow effects with session ID 0 on direct output or duplicating threads
7675 // TODO: add rule for hw accelerated effects on direct outputs with non PCM format
Dima Zavinfce7a472011-04-19 22:30:36 -07007676 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007677 ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d",
Eric Laurentde070132010-07-13 04:45:46 -07007678 desc->name, sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007679 lStatus = BAD_VALUE;
7680 goto Exit;
7681 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007682 // Only Pre processor effects are allowed on input threads and only on input threads
Glenn Kastena1117922012-01-26 10:53:32 -08007683 if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007684 ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007685 desc->name, desc->flags, mType);
7686 lStatus = BAD_VALUE;
7687 goto Exit;
7688 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07007689
Steve Block3856b092011-10-20 11:56:00 +01007690 ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007691
7692 { // scope for mLock
7693 Mutex::Autolock _l(mLock);
7694
7695 // check for existing effect chain with the requested audio session
7696 chain = getEffectChain_l(sessionId);
7697 if (chain == 0) {
7698 // create a new chain for this session
Steve Block3856b092011-10-20 11:56:00 +01007699 ALOGV("createEffect_l() new effect chain for session %d", sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007700 chain = new EffectChain(this, sessionId);
7701 addEffectChain_l(chain);
Eric Laurentde070132010-07-13 04:45:46 -07007702 chain->setStrategy(getStrategyForSession_l(sessionId));
7703 chainCreated = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007704 } else {
Eric Laurentcab11242010-07-15 12:50:15 -07007705 effect = chain->getEffectFromDesc_l(desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007706 }
7707
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08007708 ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07007709
7710 if (effect == 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007711 int id = mAudioFlinger->nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07007712 // Check CPU and memory usage
Eric Laurentde070132010-07-13 04:45:46 -07007713 lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007714 if (lStatus != NO_ERROR) {
7715 goto Exit;
7716 }
7717 effectRegistered = true;
7718 // create a new effect module if none present in the chain
Eric Laurentde070132010-07-13 04:45:46 -07007719 effect = new EffectModule(this, chain, desc, id, sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007720 lStatus = effect->status();
7721 if (lStatus != NO_ERROR) {
7722 goto Exit;
7723 }
Eric Laurentcab11242010-07-15 12:50:15 -07007724 lStatus = chain->addEffect_l(effect);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007725 if (lStatus != NO_ERROR) {
7726 goto Exit;
7727 }
7728 effectCreated = true;
7729
7730 effect->setDevice(mDevice);
7731 effect->setMode(mAudioFlinger->getMode());
7732 }
7733 // create effect handle and connect it to effect module
7734 handle = new EffectHandle(effect, client, effectClient, priority);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07007735 lStatus = effect->addHandle(handle.get());
Glenn Kastena0d68332012-01-27 16:47:15 -08007736 if (enabled != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07007737 *enabled = (int)effect->isEnabled();
7738 }
7739 }
7740
7741Exit:
7742 if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
Eric Laurentde070132010-07-13 04:45:46 -07007743 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007744 if (effectCreated) {
Eric Laurentde070132010-07-13 04:45:46 -07007745 chain->removeEffect_l(effect);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007746 }
7747 if (effectRegistered) {
Eric Laurentde070132010-07-13 04:45:46 -07007748 AudioSystem::unregisterEffect(effect->id());
7749 }
7750 if (chainCreated) {
7751 removeEffectChain_l(chain);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007752 }
7753 handle.clear();
7754 }
7755
Glenn Kastene53b9ea2012-03-12 16:29:55 -07007756 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07007757 *status = lStatus;
7758 }
7759 return handle;
7760}
7761
Eric Laurent717e1282012-06-29 16:36:52 -07007762sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId)
7763{
7764 Mutex::Autolock _l(mLock);
7765 return getEffect_l(sessionId, effectId);
7766}
7767
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007768sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId)
7769{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007770 sp<EffectChain> chain = getEffectChain_l(sessionId);
Glenn Kasten090f0192012-01-30 13:00:02 -08007771 return chain != 0 ? chain->getEffectFromId_l(effectId) : 0;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007772}
7773
Eric Laurentde070132010-07-13 04:45:46 -07007774// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and
7775// PlaybackThread::mLock held
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007776status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect)
Eric Laurentde070132010-07-13 04:45:46 -07007777{
7778 // check for existing effect chain with the requested audio session
7779 int sessionId = effect->sessionId();
7780 sp<EffectChain> chain = getEffectChain_l(sessionId);
7781 bool chainCreated = false;
7782
7783 if (chain == 0) {
7784 // create a new chain for this session
Steve Block3856b092011-10-20 11:56:00 +01007785 ALOGV("addEffect_l() new effect chain for session %d", sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07007786 chain = new EffectChain(this, sessionId);
7787 addEffectChain_l(chain);
7788 chain->setStrategy(getStrategyForSession_l(sessionId));
7789 chainCreated = true;
7790 }
Steve Block3856b092011-10-20 11:56:00 +01007791 ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get());
Eric Laurentde070132010-07-13 04:45:46 -07007792
7793 if (chain->getEffectFromId_l(effect->id()) != 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00007794 ALOGW("addEffect_l() %p effect %s already present in chain %p",
Eric Laurentde070132010-07-13 04:45:46 -07007795 this, effect->desc().name, chain.get());
7796 return BAD_VALUE;
7797 }
7798
7799 status_t status = chain->addEffect_l(effect);
7800 if (status != NO_ERROR) {
7801 if (chainCreated) {
7802 removeEffectChain_l(chain);
7803 }
7804 return status;
7805 }
7806
7807 effect->setDevice(mDevice);
7808 effect->setMode(mAudioFlinger->getMode());
7809 return NO_ERROR;
7810}
7811
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007812void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) {
Eric Laurentde070132010-07-13 04:45:46 -07007813
Steve Block3856b092011-10-20 11:56:00 +01007814 ALOGV("removeEffect_l() %p effect %p", this, effect.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07007815 effect_descriptor_t desc = effect->desc();
Eric Laurentde070132010-07-13 04:45:46 -07007816 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
7817 detachAuxEffect_l(effect->id());
7818 }
7819
7820 sp<EffectChain> chain = effect->chain().promote();
7821 if (chain != 0) {
7822 // remove effect chain if removing last effect
7823 if (chain->removeEffect_l(effect) == 0) {
7824 removeEffectChain_l(chain);
7825 }
7826 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00007827 ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get());
Eric Laurentde070132010-07-13 04:45:46 -07007828 }
7829}
7830
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007831void AudioFlinger::ThreadBase::lockEffectChains_l(
Glenn Kastene53b9ea2012-03-12 16:29:55 -07007832 Vector< sp<AudioFlinger::EffectChain> >& effectChains)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007833{
7834 effectChains = mEffectChains;
7835 for (size_t i = 0; i < mEffectChains.size(); i++) {
7836 mEffectChains[i]->lock();
7837 }
7838}
7839
7840void AudioFlinger::ThreadBase::unlockEffectChains(
Glenn Kastene53b9ea2012-03-12 16:29:55 -07007841 const Vector< sp<AudioFlinger::EffectChain> >& effectChains)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007842{
7843 for (size_t i = 0; i < effectChains.size(); i++) {
7844 effectChains[i]->unlock();
7845 }
7846}
7847
7848sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId)
7849{
7850 Mutex::Autolock _l(mLock);
7851 return getEffectChain_l(sessionId);
7852}
7853
7854sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId)
7855{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007856 size_t size = mEffectChains.size();
7857 for (size_t i = 0; i < size; i++) {
7858 if (mEffectChains[i]->sessionId() == sessionId) {
Glenn Kasten090f0192012-01-30 13:00:02 -08007859 return mEffectChains[i];
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007860 }
7861 }
Glenn Kasten090f0192012-01-30 13:00:02 -08007862 return 0;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007863}
7864
Glenn Kastenf78aee72012-01-04 11:00:47 -08007865void AudioFlinger::ThreadBase::setMode(audio_mode_t mode)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07007866{
7867 Mutex::Autolock _l(mLock);
7868 size_t size = mEffectChains.size();
7869 for (size_t i = 0; i < size; i++) {
7870 mEffectChains[i]->setMode_l(mode);
7871 }
7872}
7873
7874void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect,
Eric Laurenta5f44eb2012-06-25 11:38:29 -07007875 EffectHandle *handle,
Glenn Kasten58123c32012-02-03 10:32:24 -08007876 bool unpinIfLast) {
Eric Laurent59255e42011-07-27 19:49:51 -07007877
Mathias Agopian65ab4712010-07-14 17:59:35 -07007878 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01007879 ALOGV("disconnectEffect() %p effect %p", this, effect.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07007880 // delete the effect module if removing last handle on it
7881 if (effect->removeHandle(handle) == 0) {
Glenn Kasten58123c32012-02-03 10:32:24 -08007882 if (!effect->isPinned() || unpinIfLast) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07007883 removeEffect_l(effect);
7884 AudioSystem::unregisterEffect(effect->id());
7885 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07007886 }
7887}
7888
7889status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain)
7890{
7891 int session = chain->sessionId();
7892 int16_t *buffer = mMixBuffer;
7893 bool ownsBuffer = false;
7894
Steve Block3856b092011-10-20 11:56:00 +01007895 ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007896 if (session > 0) {
7897 // Only one effect chain can be present in direct output thread and it uses
7898 // the mix buffer as input
7899 if (mType != DIRECT) {
Glenn Kasten58912562012-04-03 10:45:00 -07007900 size_t numSamples = mNormalFrameCount * mChannelCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007901 buffer = new int16_t[numSamples];
7902 memset(buffer, 0, numSamples * sizeof(int16_t));
Steve Block3856b092011-10-20 11:56:00 +01007903 ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007904 ownsBuffer = true;
7905 }
7906
7907 // Attach all tracks with same session ID to this chain.
7908 for (size_t i = 0; i < mTracks.size(); ++i) {
7909 sp<Track> track = mTracks[i];
7910 if (session == track->sessionId()) {
Steve Block3856b092011-10-20 11:56:00 +01007911 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007912 track->setMainBuffer(buffer);
Eric Laurentb469b942011-05-09 12:09:06 -07007913 chain->incTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07007914 }
7915 }
7916
7917 // indicate all active tracks in the chain
7918 for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
7919 sp<Track> track = mActiveTracks[i].promote();
7920 if (track == 0) continue;
7921 if (session == track->sessionId()) {
Steve Block3856b092011-10-20 11:56:00 +01007922 ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session);
Eric Laurentb469b942011-05-09 12:09:06 -07007923 chain->incActiveTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07007924 }
7925 }
7926 }
7927
7928 chain->setInBuffer(buffer, ownsBuffer);
7929 chain->setOutBuffer(mMixBuffer);
Dima Zavinfce7a472011-04-19 22:30:36 -07007930 // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect
Eric Laurentde070132010-07-13 04:45:46 -07007931 // chains list in order to be processed last as it contains output stage effects
Dima Zavinfce7a472011-04-19 22:30:36 -07007932 // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before
7933 // session AUDIO_SESSION_OUTPUT_STAGE to be processed
Mathias Agopian65ab4712010-07-14 17:59:35 -07007934 // after track specific effects and before output stage
Dima Zavinfce7a472011-04-19 22:30:36 -07007935 // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and
7936 // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX
Eric Laurentde070132010-07-13 04:45:46 -07007937 // Effect chain for other sessions are inserted at beginning of effect
7938 // chains list to be processed before output mix effects. Relative order between other
7939 // sessions is not important
Mathias Agopian65ab4712010-07-14 17:59:35 -07007940 size_t size = mEffectChains.size();
7941 size_t i = 0;
7942 for (i = 0; i < size; i++) {
7943 if (mEffectChains[i]->sessionId() < session) break;
7944 }
7945 mEffectChains.insertAt(chain, i);
Eric Laurent59255e42011-07-27 19:49:51 -07007946 checkSuspendOnAddEffectChain_l(chain);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007947
7948 return NO_ERROR;
7949}
7950
7951size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain)
7952{
7953 int session = chain->sessionId();
7954
Steve Block3856b092011-10-20 11:56:00 +01007955 ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -07007956
7957 for (size_t i = 0; i < mEffectChains.size(); i++) {
7958 if (chain == mEffectChains[i]) {
7959 mEffectChains.removeAt(i);
Eric Laurentb469b942011-05-09 12:09:06 -07007960 // detach all active tracks from the chain
7961 for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
7962 sp<Track> track = mActiveTracks[i].promote();
7963 if (track == 0) continue;
7964 if (session == track->sessionId()) {
Steve Block3856b092011-10-20 11:56:00 +01007965 ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d",
Eric Laurentb469b942011-05-09 12:09:06 -07007966 chain.get(), session);
7967 chain->decActiveTrackCnt();
7968 }
7969 }
7970
Mathias Agopian65ab4712010-07-14 17:59:35 -07007971 // detach all tracks with same session ID from this chain
7972 for (size_t i = 0; i < mTracks.size(); ++i) {
7973 sp<Track> track = mTracks[i];
7974 if (session == track->sessionId()) {
7975 track->setMainBuffer(mMixBuffer);
Eric Laurentb469b942011-05-09 12:09:06 -07007976 chain->decTrackCnt();
Mathias Agopian65ab4712010-07-14 17:59:35 -07007977 }
7978 }
Eric Laurentde070132010-07-13 04:45:46 -07007979 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07007980 }
7981 }
7982 return mEffectChains.size();
7983}
7984
Eric Laurentde070132010-07-13 04:45:46 -07007985status_t AudioFlinger::PlaybackThread::attachAuxEffect(
7986 const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007987{
7988 Mutex::Autolock _l(mLock);
7989 return attachAuxEffect_l(track, EffectId);
7990}
7991
Eric Laurentde070132010-07-13 04:45:46 -07007992status_t AudioFlinger::PlaybackThread::attachAuxEffect_l(
7993 const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07007994{
7995 status_t status = NO_ERROR;
7996
7997 if (EffectId == 0) {
7998 track->setAuxBuffer(0, NULL);
7999 } else {
Dima Zavinfce7a472011-04-19 22:30:36 -07008000 // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX
8001 sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008002 if (effect != 0) {
8003 if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
8004 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer());
8005 } else {
8006 status = INVALID_OPERATION;
8007 }
8008 } else {
8009 status = BAD_VALUE;
8010 }
8011 }
8012 return status;
8013}
8014
8015void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId)
8016{
Glenn Kastene53b9ea2012-03-12 16:29:55 -07008017 for (size_t i = 0; i < mTracks.size(); ++i) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07008018 sp<Track> track = mTracks[i];
8019 if (track->auxEffectId() == effectId) {
8020 attachAuxEffect_l(track, 0);
8021 }
8022 }
8023}
8024
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008025status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain)
8026{
8027 // only one chain per input thread
8028 if (mEffectChains.size() != 0) {
8029 return INVALID_OPERATION;
8030 }
Steve Block3856b092011-10-20 11:56:00 +01008031 ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008032
8033 chain->setInBuffer(NULL);
8034 chain->setOutBuffer(NULL);
8035
Eric Laurent59255e42011-07-27 19:49:51 -07008036 checkSuspendOnAddEffectChain_l(chain);
8037
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008038 mEffectChains.add(chain);
8039
8040 return NO_ERROR;
8041}
8042
8043size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain)
8044{
Steve Block3856b092011-10-20 11:56:00 +01008045 ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this);
Steve Block5ff1dd52012-01-05 23:22:43 +00008046 ALOGW_IF(mEffectChains.size() != 1,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008047 "removeEffectChain_l() %p invalid chain size %d on thread %p",
8048 chain.get(), mEffectChains.size(), this);
8049 if (mEffectChains.size() == 1) {
8050 mEffectChains.removeAt(0);
8051 }
8052 return 0;
8053}
8054
Mathias Agopian65ab4712010-07-14 17:59:35 -07008055// ----------------------------------------------------------------------------
8056// EffectModule implementation
8057// ----------------------------------------------------------------------------
8058
8059#undef LOG_TAG
8060#define LOG_TAG "AudioFlinger::EffectModule"
8061
Glenn Kasten9eaa5572012-01-20 13:32:16 -08008062AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07008063 const wp<AudioFlinger::EffectChain>& chain,
8064 effect_descriptor_t *desc,
8065 int id,
8066 int sessionId)
Glenn Kasten415fa752012-07-02 16:11:18 -07008067 : mPinned(sessionId > AUDIO_SESSION_OUTPUT_MIX),
8068 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
Glenn Kastencd2d6102012-07-18 16:49:32 -07008069 mDescriptor(*desc),
Glenn Kasten415fa752012-07-02 16:11:18 -07008070 // mConfig is set by configure() and not used before then
8071 mEffectInterface(NULL),
8072 mStatus(NO_INIT), mState(IDLE),
8073 // mMaxDisableWaitCnt is set by configure() and not used before then
8074 // mDisableWaitCnt is set by process() and updateState() and not used before then
8075 mSuspended(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -07008076{
Steve Block3856b092011-10-20 11:56:00 +01008077 ALOGV("Constructor %p", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008078 int lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07008079
8080 // create effect engine from effect factory
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008081 mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008082
8083 if (mStatus != NO_ERROR) {
8084 return;
8085 }
8086 lStatus = init();
8087 if (lStatus < 0) {
8088 mStatus = lStatus;
8089 goto Error;
8090 }
8091
Steve Block3856b092011-10-20 11:56:00 +01008092 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008093 return;
8094Error:
8095 EffectRelease(mEffectInterface);
8096 mEffectInterface = NULL;
Steve Block3856b092011-10-20 11:56:00 +01008097 ALOGV("Constructor Error %d", mStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008098}
8099
8100AudioFlinger::EffectModule::~EffectModule()
8101{
Steve Block3856b092011-10-20 11:56:00 +01008102 ALOGV("Destructor %p", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008103 if (mEffectInterface != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008104 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
8105 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
8106 sp<ThreadBase> thread = mThread.promote();
8107 if (thread != 0) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07008108 audio_stream_t *stream = thread->stream();
8109 if (stream != NULL) {
8110 stream->remove_audio_effect(stream, mEffectInterface);
8111 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008112 }
8113 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07008114 // release effect engine
8115 EffectRelease(mEffectInterface);
8116 }
8117}
8118
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008119status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
Mathias Agopian65ab4712010-07-14 17:59:35 -07008120{
8121 status_t status;
8122
8123 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008124 int priority = handle->priority();
8125 size_t size = mHandles.size();
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008126 EffectHandle *controlHandle = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07008127 size_t i;
8128 for (i = 0; i < size; i++) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008129 EffectHandle *h = mHandles[i];
8130 if (h == NULL || h->destroyed_l()) continue;
8131 // first non destroyed handle is considered in control
8132 if (controlHandle == NULL)
8133 controlHandle = h;
Mathias Agopian65ab4712010-07-14 17:59:35 -07008134 if (h->priority() <= priority) break;
8135 }
8136 // if inserted in first place, move effect control from previous owner to this handle
8137 if (i == 0) {
Eric Laurent59255e42011-07-27 19:49:51 -07008138 bool enabled = false;
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008139 if (controlHandle != NULL) {
8140 enabled = controlHandle->enabled();
8141 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008142 }
Eric Laurent59255e42011-07-27 19:49:51 -07008143 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008144 status = NO_ERROR;
8145 } else {
8146 status = ALREADY_EXISTS;
8147 }
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008148 ALOGV("addHandle() %p added handle %p in position %d", this, handle, i);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008149 mHandles.insertAt(handle, i);
8150 return status;
8151}
8152
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008153size_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Mathias Agopian65ab4712010-07-14 17:59:35 -07008154{
8155 Mutex::Autolock _l(mLock);
8156 size_t size = mHandles.size();
8157 size_t i;
8158 for (i = 0; i < size; i++) {
8159 if (mHandles[i] == handle) break;
8160 }
8161 if (i == size) {
8162 return size;
8163 }
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008164 ALOGV("removeHandle() %p removed handle %p in position %d", this, handle, i);
Eric Laurent59255e42011-07-27 19:49:51 -07008165
Mathias Agopian65ab4712010-07-14 17:59:35 -07008166 mHandles.removeAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008167 // if removed from first place, move effect control from this handle to next in line
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008168 if (i == 0) {
8169 EffectHandle *h = controlHandle_l();
8170 if (h != NULL) {
8171 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008172 }
8173 }
8174
Eric Laurentec437d82011-07-26 20:54:46 -07008175 // Prevent calls to process() and other functions on effect interface from now on.
8176 // The effect engine will be released by the destructor when the last strong reference on
8177 // this object is released which can happen after next process is called.
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008178 if (mHandles.size() == 0 && !mPinned) {
Eric Laurentec437d82011-07-26 20:54:46 -07008179 mState = DESTROYED;
Eric Laurentdac69112010-09-28 14:09:57 -07008180 }
8181
Eric Laurente65c8912012-07-20 15:57:23 -07008182 return mHandles.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07008183}
8184
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008185// must be called with EffectModule::mLock held
8186AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
Eric Laurent59255e42011-07-27 19:49:51 -07008187{
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008188 // the first valid handle in the list has control over the module
8189 for (size_t i = 0; i < mHandles.size(); i++) {
8190 EffectHandle *h = mHandles[i];
8191 if (h != NULL && !h->destroyed_l()) {
8192 return h;
8193 }
8194 }
8195
8196 return NULL;
Eric Laurent59255e42011-07-27 19:49:51 -07008197}
8198
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008199size_t AudioFlinger::EffectModule::disconnect(EffectHandle *handle, bool unpinIfLast)
Mathias Agopian65ab4712010-07-14 17:59:35 -07008200{
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008201 ALOGV("disconnect() %p handle %p", this, handle);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008202 // keep a strong reference on this EffectModule to avoid calling the
8203 // destructor before we exit
8204 sp<EffectModule> keep(this);
8205 {
8206 sp<ThreadBase> thread = mThread.promote();
8207 if (thread != 0) {
Glenn Kasten58123c32012-02-03 10:32:24 -08008208 thread->disconnectEffect(keep, handle, unpinIfLast);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008209 }
8210 }
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008211 return mHandles.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07008212}
8213
8214void AudioFlinger::EffectModule::updateState() {
8215 Mutex::Autolock _l(mLock);
8216
8217 switch (mState) {
8218 case RESTART:
8219 reset_l();
8220 // FALL THROUGH
8221
8222 case STARTING:
8223 // clear auxiliary effect input buffer for next accumulation
8224 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
8225 memset(mConfig.inputCfg.buffer.raw,
8226 0,
8227 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
8228 }
8229 start_l();
8230 mState = ACTIVE;
8231 break;
8232 case STOPPING:
8233 stop_l();
8234 mDisableWaitCnt = mMaxDisableWaitCnt;
8235 mState = STOPPED;
8236 break;
8237 case STOPPED:
8238 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
8239 // turn off sequence.
8240 if (--mDisableWaitCnt == 0) {
8241 reset_l();
8242 mState = IDLE;
8243 }
8244 break;
Eric Laurentec437d82011-07-26 20:54:46 -07008245 default: //IDLE , ACTIVE, DESTROYED
Mathias Agopian65ab4712010-07-14 17:59:35 -07008246 break;
8247 }
8248}
8249
8250void AudioFlinger::EffectModule::process()
8251{
8252 Mutex::Autolock _l(mLock);
8253
Eric Laurentec437d82011-07-26 20:54:46 -07008254 if (mState == DESTROYED || mEffectInterface == NULL ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07008255 mConfig.inputCfg.buffer.raw == NULL ||
8256 mConfig.outputCfg.buffer.raw == NULL) {
8257 return;
8258 }
8259
Eric Laurent8f45bd72010-08-31 13:50:07 -07008260 if (isProcessEnabled()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07008261 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
8262 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kasten3b21c502011-12-15 09:52:39 -08008263 ditherAndClamp(mConfig.inputCfg.buffer.s32,
Mathias Agopian65ab4712010-07-14 17:59:35 -07008264 mConfig.inputCfg.buffer.s32,
Eric Laurentde070132010-07-13 04:45:46 -07008265 mConfig.inputCfg.buffer.frameCount/2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008266 }
8267
8268 // do the actual processing in the effect engine
8269 int ret = (*mEffectInterface)->process(mEffectInterface,
8270 &mConfig.inputCfg.buffer,
8271 &mConfig.outputCfg.buffer);
8272
8273 // force transition to IDLE state when engine is ready
8274 if (mState == STOPPED && ret == -ENODATA) {
8275 mDisableWaitCnt = 1;
8276 }
8277
8278 // clear auxiliary effect input buffer for next accumulation
8279 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurent73337482011-01-19 18:36:13 -08008280 memset(mConfig.inputCfg.buffer.raw, 0,
8281 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
Mathias Agopian65ab4712010-07-14 17:59:35 -07008282 }
8283 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
Eric Laurent73337482011-01-19 18:36:13 -08008284 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
8285 // If an insert effect is idle and input buffer is different from output buffer,
8286 // accumulate input onto output
Mathias Agopian65ab4712010-07-14 17:59:35 -07008287 sp<EffectChain> chain = mChain.promote();
Eric Laurentb469b942011-05-09 12:09:06 -07008288 if (chain != 0 && chain->activeTrackCnt() != 0) {
Eric Laurent73337482011-01-19 18:36:13 -08008289 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here
8290 int16_t *in = mConfig.inputCfg.buffer.s16;
8291 int16_t *out = mConfig.outputCfg.buffer.s16;
8292 for (size_t i = 0; i < frameCnt; i++) {
8293 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008294 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07008295 }
8296 }
8297}
8298
8299void AudioFlinger::EffectModule::reset_l()
8300{
8301 if (mEffectInterface == NULL) {
8302 return;
8303 }
8304 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
8305}
8306
8307status_t AudioFlinger::EffectModule::configure()
8308{
Mathias Agopian65ab4712010-07-14 17:59:35 -07008309 if (mEffectInterface == NULL) {
8310 return NO_INIT;
8311 }
8312
8313 sp<ThreadBase> thread = mThread.promote();
8314 if (thread == 0) {
8315 return DEAD_OBJECT;
8316 }
8317
8318 // TODO: handle configuration of effects replacing track process
Glenn Kasten254af182012-07-03 14:59:05 -07008319 audio_channel_mask_t channelMask = thread->channelMask();
Mathias Agopian65ab4712010-07-14 17:59:35 -07008320
8321 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurente1315cf2011-05-17 19:16:02 -07008322 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Mathias Agopian65ab4712010-07-14 17:59:35 -07008323 } else {
Glenn Kasten254af182012-07-03 14:59:05 -07008324 mConfig.inputCfg.channels = channelMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07008325 }
Glenn Kasten254af182012-07-03 14:59:05 -07008326 mConfig.outputCfg.channels = channelMask;
Eric Laurente1315cf2011-05-17 19:16:02 -07008327 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
8328 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07008329 mConfig.inputCfg.samplingRate = thread->sampleRate();
8330 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
8331 mConfig.inputCfg.bufferProvider.cookie = NULL;
8332 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
8333 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
8334 mConfig.outputCfg.bufferProvider.cookie = NULL;
8335 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
8336 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
8337 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
8338 // Insert effect:
Dima Zavinfce7a472011-04-19 22:30:36 -07008339 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
Eric Laurentde070132010-07-13 04:45:46 -07008340 // always overwrites output buffer: input buffer == output buffer
Mathias Agopian65ab4712010-07-14 17:59:35 -07008341 // - in other sessions:
8342 // last effect in the chain accumulates in output buffer: input buffer != output buffer
8343 // other effect: overwrites output buffer: input buffer == output buffer
8344 // Auxiliary effect:
8345 // accumulates in output buffer: input buffer != output buffer
8346 // Therefore: accumulate <=> input buffer != output buffer
8347 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
8348 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
8349 } else {
8350 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
8351 }
8352 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
8353 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
8354 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
8355 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
8356
Steve Block3856b092011-10-20 11:56:00 +01008357 ALOGV("configure() %p thread %p buffer %p framecount %d",
Eric Laurentde070132010-07-13 04:45:46 -07008358 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
8359
Mathias Agopian65ab4712010-07-14 17:59:35 -07008360 status_t cmdStatus;
Eric Laurent25f43952010-07-28 05:40:18 -07008361 uint32_t size = sizeof(int);
8362 status_t status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurent3d5188b2011-12-16 15:30:36 -08008363 EFFECT_CMD_SET_CONFIG,
Eric Laurent25f43952010-07-28 05:40:18 -07008364 sizeof(effect_config_t),
8365 &mConfig,
8366 &size,
8367 &cmdStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008368 if (status == 0) {
8369 status = cmdStatus;
8370 }
8371
Marco Nelissenf06c2ed2012-06-06 09:52:31 -07008372 if (status == 0 &&
8373 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
8374 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
8375 effect_param_t *p = (effect_param_t *)buf32;
8376
8377 p->psize = sizeof(uint32_t);
8378 p->vsize = sizeof(uint32_t);
8379 size = sizeof(int);
8380 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
8381
8382 uint32_t latency = 0;
8383 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
8384 if (pbt != NULL) {
8385 latency = pbt->latency_l();
8386 }
8387
8388 *((int32_t *)p->data + 1)= latency;
8389 (*mEffectInterface)->command(mEffectInterface,
8390 EFFECT_CMD_SET_PARAM,
8391 sizeof(effect_param_t) + 8,
8392 &buf32,
8393 &size,
8394 &cmdStatus);
8395 }
8396
Mathias Agopian65ab4712010-07-14 17:59:35 -07008397 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
8398 (1000 * mConfig.outputCfg.buffer.frameCount);
8399
8400 return status;
8401}
8402
8403status_t AudioFlinger::EffectModule::init()
8404{
8405 Mutex::Autolock _l(mLock);
8406 if (mEffectInterface == NULL) {
8407 return NO_INIT;
8408 }
8409 status_t cmdStatus;
Eric Laurent25f43952010-07-28 05:40:18 -07008410 uint32_t size = sizeof(status_t);
8411 status_t status = (*mEffectInterface)->command(mEffectInterface,
8412 EFFECT_CMD_INIT,
8413 0,
8414 NULL,
8415 &size,
8416 &cmdStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008417 if (status == 0) {
8418 status = cmdStatus;
8419 }
8420 return status;
8421}
8422
Eric Laurentec35a142011-10-05 17:42:25 -07008423status_t AudioFlinger::EffectModule::start()
8424{
8425 Mutex::Autolock _l(mLock);
8426 return start_l();
8427}
8428
Mathias Agopian65ab4712010-07-14 17:59:35 -07008429status_t AudioFlinger::EffectModule::start_l()
8430{
8431 if (mEffectInterface == NULL) {
8432 return NO_INIT;
8433 }
8434 status_t cmdStatus;
Eric Laurent25f43952010-07-28 05:40:18 -07008435 uint32_t size = sizeof(status_t);
8436 status_t status = (*mEffectInterface)->command(mEffectInterface,
8437 EFFECT_CMD_ENABLE,
8438 0,
8439 NULL,
8440 &size,
8441 &cmdStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008442 if (status == 0) {
8443 status = cmdStatus;
8444 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008445 if (status == 0 &&
8446 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
8447 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
8448 sp<ThreadBase> thread = mThread.promote();
8449 if (thread != 0) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07008450 audio_stream_t *stream = thread->stream();
8451 if (stream != NULL) {
8452 stream->add_audio_effect(stream, mEffectInterface);
8453 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008454 }
8455 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07008456 return status;
8457}
8458
Eric Laurentec437d82011-07-26 20:54:46 -07008459status_t AudioFlinger::EffectModule::stop()
8460{
8461 Mutex::Autolock _l(mLock);
8462 return stop_l();
8463}
8464
Mathias Agopian65ab4712010-07-14 17:59:35 -07008465status_t AudioFlinger::EffectModule::stop_l()
8466{
8467 if (mEffectInterface == NULL) {
8468 return NO_INIT;
8469 }
8470 status_t cmdStatus;
Eric Laurent25f43952010-07-28 05:40:18 -07008471 uint32_t size = sizeof(status_t);
8472 status_t status = (*mEffectInterface)->command(mEffectInterface,
8473 EFFECT_CMD_DISABLE,
8474 0,
8475 NULL,
8476 &size,
8477 &cmdStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008478 if (status == 0) {
8479 status = cmdStatus;
8480 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008481 if (status == 0 &&
8482 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
8483 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
8484 sp<ThreadBase> thread = mThread.promote();
8485 if (thread != 0) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07008486 audio_stream_t *stream = thread->stream();
8487 if (stream != NULL) {
8488 stream->remove_audio_effect(stream, mEffectInterface);
8489 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008490 }
8491 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07008492 return status;
8493}
8494
Eric Laurent25f43952010-07-28 05:40:18 -07008495status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
8496 uint32_t cmdSize,
8497 void *pCmdData,
8498 uint32_t *replySize,
8499 void *pReplyData)
Mathias Agopian65ab4712010-07-14 17:59:35 -07008500{
8501 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01008502// ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008503
Eric Laurentec437d82011-07-26 20:54:46 -07008504 if (mState == DESTROYED || mEffectInterface == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07008505 return NO_INIT;
8506 }
Eric Laurent25f43952010-07-28 05:40:18 -07008507 status_t status = (*mEffectInterface)->command(mEffectInterface,
8508 cmdCode,
8509 cmdSize,
8510 pCmdData,
8511 replySize,
8512 pReplyData);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008513 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurent25f43952010-07-28 05:40:18 -07008514 uint32_t size = (replySize == NULL) ? 0 : *replySize;
Mathias Agopian65ab4712010-07-14 17:59:35 -07008515 for (size_t i = 1; i < mHandles.size(); i++) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008516 EffectHandle *h = mHandles[i];
8517 if (h != NULL && !h->destroyed_l()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07008518 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
8519 }
8520 }
8521 }
8522 return status;
8523}
8524
8525status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
8526{
8527 Mutex::Autolock _l(mLock);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008528 return setEnabled_l(enabled);
8529}
8530
8531// must be called with EffectModule::mLock held
8532status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
8533{
8534
Steve Block3856b092011-10-20 11:56:00 +01008535 ALOGV("setEnabled %p enabled %d", this, enabled);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008536
8537 if (enabled != isEnabled()) {
Eric Laurentdb7c0792011-08-10 10:37:50 -07008538 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
8539 if (enabled && status != NO_ERROR) {
8540 return status;
8541 }
8542
Mathias Agopian65ab4712010-07-14 17:59:35 -07008543 switch (mState) {
8544 // going from disabled to enabled
8545 case IDLE:
8546 mState = STARTING;
8547 break;
8548 case STOPPED:
8549 mState = RESTART;
8550 break;
8551 case STOPPING:
8552 mState = ACTIVE;
8553 break;
8554
8555 // going from enabled to disabled
8556 case RESTART:
Eric Laurent8f45bd72010-08-31 13:50:07 -07008557 mState = STOPPED;
8558 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07008559 case STARTING:
8560 mState = IDLE;
8561 break;
8562 case ACTIVE:
8563 mState = STOPPING;
8564 break;
Eric Laurentec437d82011-07-26 20:54:46 -07008565 case DESTROYED:
8566 return NO_ERROR; // simply ignore as we are being destroyed
Mathias Agopian65ab4712010-07-14 17:59:35 -07008567 }
8568 for (size_t i = 1; i < mHandles.size(); i++) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008569 EffectHandle *h = mHandles[i];
8570 if (h != NULL && !h->destroyed_l()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07008571 h->setEnabled(enabled);
8572 }
8573 }
8574 }
8575 return NO_ERROR;
8576}
8577
Glenn Kastenc59c0042012-02-02 14:06:11 -08008578bool AudioFlinger::EffectModule::isEnabled() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07008579{
8580 switch (mState) {
8581 case RESTART:
8582 case STARTING:
8583 case ACTIVE:
8584 return true;
8585 case IDLE:
8586 case STOPPING:
8587 case STOPPED:
Eric Laurentec437d82011-07-26 20:54:46 -07008588 case DESTROYED:
Mathias Agopian65ab4712010-07-14 17:59:35 -07008589 default:
8590 return false;
8591 }
8592}
8593
Glenn Kastenc59c0042012-02-02 14:06:11 -08008594bool AudioFlinger::EffectModule::isProcessEnabled() const
Eric Laurent8f45bd72010-08-31 13:50:07 -07008595{
8596 switch (mState) {
8597 case RESTART:
8598 case ACTIVE:
8599 case STOPPING:
8600 case STOPPED:
8601 return true;
8602 case IDLE:
8603 case STARTING:
Eric Laurentec437d82011-07-26 20:54:46 -07008604 case DESTROYED:
Eric Laurent8f45bd72010-08-31 13:50:07 -07008605 default:
8606 return false;
8607 }
8608}
8609
Mathias Agopian65ab4712010-07-14 17:59:35 -07008610status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
8611{
8612 Mutex::Autolock _l(mLock);
8613 status_t status = NO_ERROR;
8614
8615 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
8616 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
Eric Laurent8f45bd72010-08-31 13:50:07 -07008617 if (isProcessEnabled() &&
Eric Laurentf997cab2010-07-19 06:24:46 -07008618 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
8619 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07008620 status_t cmdStatus;
8621 uint32_t volume[2];
8622 uint32_t *pVolume = NULL;
Eric Laurent25f43952010-07-28 05:40:18 -07008623 uint32_t size = sizeof(volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008624 volume[0] = *left;
8625 volume[1] = *right;
8626 if (controller) {
8627 pVolume = volume;
8628 }
Eric Laurent25f43952010-07-28 05:40:18 -07008629 status = (*mEffectInterface)->command(mEffectInterface,
8630 EFFECT_CMD_SET_VOLUME,
8631 size,
8632 volume,
8633 &size,
8634 pVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008635 if (controller && status == NO_ERROR && size == sizeof(volume)) {
8636 *left = volume[0];
8637 *right = volume[1];
8638 }
8639 }
8640 return status;
8641}
8642
Glenn Kastenbb4350d2012-07-03 15:56:38 -07008643status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -07008644{
8645 Mutex::Autolock _l(mLock);
8646 status_t status = NO_ERROR;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008647 if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
8648 // audio pre processing modules on RecordThread can receive both output and
8649 // input device indication in the same call
Glenn Kastenbb4350d2012-07-03 15:56:38 -07008650 audio_devices_t dev = device & AUDIO_DEVICE_OUT_ALL;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07008651 if (dev) {
8652 status_t cmdStatus;
8653 uint32_t size = sizeof(status_t);
8654
8655 status = (*mEffectInterface)->command(mEffectInterface,
8656 EFFECT_CMD_SET_DEVICE,
8657 sizeof(uint32_t),
8658 &dev,
8659 &size,
8660 &cmdStatus);
8661 if (status == NO_ERROR) {
8662 status = cmdStatus;
8663 }
8664 }
8665 dev = device & AUDIO_DEVICE_IN_ALL;
8666 if (dev) {
8667 status_t cmdStatus;
8668 uint32_t size = sizeof(status_t);
8669
8670 status_t status2 = (*mEffectInterface)->command(mEffectInterface,
8671 EFFECT_CMD_SET_INPUT_DEVICE,
8672 sizeof(uint32_t),
8673 &dev,
8674 &size,
8675 &cmdStatus);
8676 if (status2 == NO_ERROR) {
8677 status2 = cmdStatus;
8678 }
8679 if (status == NO_ERROR) {
8680 status = status2;
8681 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07008682 }
8683 }
8684 return status;
8685}
8686
Glenn Kastenf78aee72012-01-04 11:00:47 -08008687status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -07008688{
8689 Mutex::Autolock _l(mLock);
8690 status_t status = NO_ERROR;
8691 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07008692 status_t cmdStatus;
Eric Laurent25f43952010-07-28 05:40:18 -07008693 uint32_t size = sizeof(status_t);
8694 status = (*mEffectInterface)->command(mEffectInterface,
8695 EFFECT_CMD_SET_AUDIO_MODE,
Glenn Kastenf78aee72012-01-04 11:00:47 -08008696 sizeof(audio_mode_t),
Eric Laurente1315cf2011-05-17 19:16:02 -07008697 &mode,
Eric Laurent25f43952010-07-28 05:40:18 -07008698 &size,
8699 &cmdStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008700 if (status == NO_ERROR) {
8701 status = cmdStatus;
8702 }
8703 }
8704 return status;
8705}
8706
Eric Laurent59255e42011-07-27 19:49:51 -07008707void AudioFlinger::EffectModule::setSuspended(bool suspended)
8708{
8709 Mutex::Autolock _l(mLock);
8710 mSuspended = suspended;
8711}
Glenn Kastena3a85482012-01-04 11:01:11 -08008712
8713bool AudioFlinger::EffectModule::suspended() const
Eric Laurent59255e42011-07-27 19:49:51 -07008714{
8715 Mutex::Autolock _l(mLock);
8716 return mSuspended;
8717}
8718
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008719bool AudioFlinger::EffectModule::purgeHandles()
8720{
8721 bool enabled = false;
8722 Mutex::Autolock _l(mLock);
8723 for (size_t i = 0; i < mHandles.size(); i++) {
8724 EffectHandle *handle = mHandles[i];
8725 if (handle != NULL && !handle->destroyed_l()) {
8726 handle->effect().clear();
8727 if (handle->hasControl()) {
8728 enabled = handle->enabled();
8729 }
8730 }
8731 }
8732 return enabled;
8733}
8734
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07008735void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -07008736{
8737 const size_t SIZE = 256;
8738 char buffer[SIZE];
8739 String8 result;
8740
8741 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
8742 result.append(buffer);
8743
8744 bool locked = tryLock(mLock);
8745 // failed to lock - AudioFlinger is probably deadlocked
8746 if (!locked) {
8747 result.append("\t\tCould not lock Fx mutex:\n");
8748 }
8749
8750 result.append("\t\tSession Status State Engine:\n");
8751 snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n",
8752 mSessionId, mStatus, mState, (uint32_t)mEffectInterface);
8753 result.append(buffer);
8754
8755 result.append("\t\tDescriptor:\n");
8756 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
8757 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
8758 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2],
8759 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
8760 result.append(buffer);
8761 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
8762 mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion,
8763 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2],
8764 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
8765 result.append(buffer);
Eric Laurente1315cf2011-05-17 19:16:02 -07008766 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n",
Mathias Agopian65ab4712010-07-14 17:59:35 -07008767 mDescriptor.apiVersion,
8768 mDescriptor.flags);
8769 result.append(buffer);
8770 snprintf(buffer, SIZE, "\t\t- name: %s\n",
8771 mDescriptor.name);
8772 result.append(buffer);
8773 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
8774 mDescriptor.implementor);
8775 result.append(buffer);
8776
8777 result.append("\t\t- Input configuration:\n");
8778 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
8779 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
8780 (uint32_t)mConfig.inputCfg.buffer.raw,
8781 mConfig.inputCfg.buffer.frameCount,
8782 mConfig.inputCfg.samplingRate,
8783 mConfig.inputCfg.channels,
8784 mConfig.inputCfg.format);
8785 result.append(buffer);
8786
8787 result.append("\t\t- Output configuration:\n");
8788 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
8789 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
8790 (uint32_t)mConfig.outputCfg.buffer.raw,
8791 mConfig.outputCfg.buffer.frameCount,
8792 mConfig.outputCfg.samplingRate,
8793 mConfig.outputCfg.channels,
8794 mConfig.outputCfg.format);
8795 result.append(buffer);
8796
8797 snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size());
8798 result.append(buffer);
8799 result.append("\t\t\tPid Priority Ctrl Locked client server\n");
8800 for (size_t i = 0; i < mHandles.size(); ++i) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008801 EffectHandle *handle = mHandles[i];
8802 if (handle != NULL && !handle->destroyed_l()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07008803 handle->dump(buffer, SIZE);
8804 result.append(buffer);
8805 }
8806 }
8807
8808 result.append("\n");
8809
8810 write(fd, result.string(), result.length());
8811
8812 if (locked) {
8813 mLock.unlock();
8814 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07008815}
8816
8817// ----------------------------------------------------------------------------
8818// EffectHandle implementation
8819// ----------------------------------------------------------------------------
8820
8821#undef LOG_TAG
8822#define LOG_TAG "AudioFlinger::EffectHandle"
8823
8824AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
8825 const sp<AudioFlinger::Client>& client,
8826 const sp<IEffectClient>& effectClient,
8827 int32_t priority)
8828 : BnEffect(),
Marco Nelissen3a34bef2011-08-02 13:33:41 -07008829 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008830 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -07008831{
Steve Block3856b092011-10-20 11:56:00 +01008832 ALOGV("constructor %p", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008833
Marco Nelissen3a34bef2011-08-02 13:33:41 -07008834 if (client == 0) {
8835 return;
8836 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07008837 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
8838 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
8839 if (mCblkMemory != 0) {
8840 mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer());
8841
Glenn Kastena0d68332012-01-27 16:47:15 -08008842 if (mCblk != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07008843 new(mCblk) effect_param_cblk_t();
8844 mBuffer = (uint8_t *)mCblk + bufOffset;
Glenn Kastene53b9ea2012-03-12 16:29:55 -07008845 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07008846 } else {
Steve Block29357bc2012-01-06 19:20:56 +00008847 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t));
Mathias Agopian65ab4712010-07-14 17:59:35 -07008848 return;
8849 }
8850}
8851
8852AudioFlinger::EffectHandle::~EffectHandle()
8853{
Steve Block3856b092011-10-20 11:56:00 +01008854 ALOGV("Destructor %p", this);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008855
8856 if (mEffect == 0) {
8857 mDestroyed = true;
8858 return;
8859 }
8860 mEffect->lock();
8861 mDestroyed = true;
8862 mEffect->unlock();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07008863 disconnect(false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008864}
8865
8866status_t AudioFlinger::EffectHandle::enable()
8867{
Steve Block3856b092011-10-20 11:56:00 +01008868 ALOGV("enable %p", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008869 if (!mHasControl) return INVALID_OPERATION;
8870 if (mEffect == 0) return DEAD_OBJECT;
8871
Eric Laurentdb7c0792011-08-10 10:37:50 -07008872 if (mEnabled) {
8873 return NO_ERROR;
8874 }
8875
Eric Laurent59255e42011-07-27 19:49:51 -07008876 mEnabled = true;
8877
8878 sp<ThreadBase> thread = mEffect->thread().promote();
8879 if (thread != 0) {
8880 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
8881 }
8882
8883 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
8884 if (mEffect->suspended()) {
8885 return NO_ERROR;
8886 }
8887
Eric Laurentdb7c0792011-08-10 10:37:50 -07008888 status_t status = mEffect->setEnabled(true);
8889 if (status != NO_ERROR) {
8890 if (thread != 0) {
8891 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
8892 }
8893 mEnabled = false;
8894 }
8895 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07008896}
8897
8898status_t AudioFlinger::EffectHandle::disable()
8899{
Steve Block3856b092011-10-20 11:56:00 +01008900 ALOGV("disable %p", this);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008901 if (!mHasControl) return INVALID_OPERATION;
Eric Laurent59255e42011-07-27 19:49:51 -07008902 if (mEffect == 0) return DEAD_OBJECT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07008903
Eric Laurentdb7c0792011-08-10 10:37:50 -07008904 if (!mEnabled) {
8905 return NO_ERROR;
8906 }
Eric Laurent59255e42011-07-27 19:49:51 -07008907 mEnabled = false;
8908
8909 if (mEffect->suspended()) {
8910 return NO_ERROR;
8911 }
8912
8913 status_t status = mEffect->setEnabled(false);
8914
8915 sp<ThreadBase> thread = mEffect->thread().promote();
8916 if (thread != 0) {
8917 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
8918 }
8919
8920 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07008921}
8922
8923void AudioFlinger::EffectHandle::disconnect()
8924{
Marco Nelissen3a34bef2011-08-02 13:33:41 -07008925 disconnect(true);
8926}
8927
Glenn Kasten58123c32012-02-03 10:32:24 -08008928void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07008929{
Glenn Kasten58123c32012-02-03 10:32:24 -08008930 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
Mathias Agopian65ab4712010-07-14 17:59:35 -07008931 if (mEffect == 0) {
8932 return;
8933 }
Eric Laurenta5f44eb2012-06-25 11:38:29 -07008934 // restore suspended effects if the disconnected handle was enabled and the last one.
8935 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
Eric Laurentdb7c0792011-08-10 10:37:50 -07008936 sp<ThreadBase> thread = mEffect->thread().promote();
8937 if (thread != 0) {
8938 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
8939 }
Eric Laurent59255e42011-07-27 19:49:51 -07008940 }
8941
Mathias Agopian65ab4712010-07-14 17:59:35 -07008942 // release sp on module => module destructor can be called now
8943 mEffect.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07008944 if (mClient != 0) {
Glenn Kastena0d68332012-01-27 16:47:15 -08008945 if (mCblk != NULL) {
Glenn Kasten1a0ae5b2012-02-03 10:24:48 -08008946 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
Marco Nelissen3a34bef2011-08-02 13:33:41 -07008947 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
8948 }
Glenn Kastendbfafaf2012-01-25 15:27:15 -08008949 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Glenn Kasten98ec94c2012-01-25 14:28:29 -08008950 // Client destructor must run with AudioFlinger mutex locked
Mathias Agopian65ab4712010-07-14 17:59:35 -07008951 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
8952 mClient.clear();
8953 }
8954}
8955
Eric Laurent25f43952010-07-28 05:40:18 -07008956status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
8957 uint32_t cmdSize,
8958 void *pCmdData,
8959 uint32_t *replySize,
8960 void *pReplyData)
Mathias Agopian65ab4712010-07-14 17:59:35 -07008961{
Steve Block3856b092011-10-20 11:56:00 +01008962// ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent25f43952010-07-28 05:40:18 -07008963// cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07008964
8965 // only get parameter command is permitted for applications not controlling the effect
8966 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
8967 return INVALID_OPERATION;
8968 }
8969 if (mEffect == 0) return DEAD_OBJECT;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07008970 if (mClient == 0) return INVALID_OPERATION;
Mathias Agopian65ab4712010-07-14 17:59:35 -07008971
8972 // handle commands that are not forwarded transparently to effect engine
8973 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
8974 // No need to trylock() here as this function is executed in the binder thread serving a particular client process:
8975 // no risk to block the whole media server process or mixer threads is we are stuck here
8976 Mutex::Autolock _l(mCblk->lock);
8977 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
8978 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
8979 mCblk->serverIndex = 0;
8980 mCblk->clientIndex = 0;
8981 return BAD_VALUE;
8982 }
8983 status_t status = NO_ERROR;
8984 while (mCblk->serverIndex < mCblk->clientIndex) {
8985 int reply;
Eric Laurent25f43952010-07-28 05:40:18 -07008986 uint32_t rsize = sizeof(int);
Mathias Agopian65ab4712010-07-14 17:59:35 -07008987 int *p = (int *)(mBuffer + mCblk->serverIndex);
8988 int size = *p++;
8989 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
Steve Block5ff1dd52012-01-05 23:22:43 +00008990 ALOGW("command(): invalid parameter block size");
Mathias Agopian65ab4712010-07-14 17:59:35 -07008991 break;
8992 }
8993 effect_param_t *param = (effect_param_t *)p;
8994 if (param->psize == 0 || param->vsize == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00008995 ALOGW("command(): null parameter or value size");
Mathias Agopian65ab4712010-07-14 17:59:35 -07008996 mCblk->serverIndex += size;
8997 continue;
8998 }
Eric Laurent25f43952010-07-28 05:40:18 -07008999 uint32_t psize = sizeof(effect_param_t) +
9000 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
9001 param->vsize;
9002 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
9003 psize,
9004 p,
9005 &rsize,
9006 &reply);
Eric Laurentaeae3de2010-09-02 11:56:55 -07009007 // stop at first error encountered
9008 if (ret != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07009009 status = ret;
Eric Laurentaeae3de2010-09-02 11:56:55 -07009010 *(int *)pReplyData = reply;
9011 break;
9012 } else if (reply != NO_ERROR) {
9013 *(int *)pReplyData = reply;
9014 break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07009015 }
9016 mCblk->serverIndex += size;
9017 }
9018 mCblk->serverIndex = 0;
9019 mCblk->clientIndex = 0;
9020 return status;
9021 } else if (cmdCode == EFFECT_CMD_ENABLE) {
Eric Laurentaeae3de2010-09-02 11:56:55 -07009022 *(int *)pReplyData = NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07009023 return enable();
9024 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentaeae3de2010-09-02 11:56:55 -07009025 *(int *)pReplyData = NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07009026 return disable();
9027 }
9028
9029 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
9030}
9031
Eric Laurent59255e42011-07-27 19:49:51 -07009032void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
Mathias Agopian65ab4712010-07-14 17:59:35 -07009033{
Steve Block3856b092011-10-20 11:56:00 +01009034 ALOGV("setControl %p control %d", this, hasControl);
Mathias Agopian65ab4712010-07-14 17:59:35 -07009035
9036 mHasControl = hasControl;
Eric Laurent59255e42011-07-27 19:49:51 -07009037 mEnabled = enabled;
9038
Mathias Agopian65ab4712010-07-14 17:59:35 -07009039 if (signal && mEffectClient != 0) {
9040 mEffectClient->controlStatusChanged(hasControl);
9041 }
9042}
9043
Eric Laurent25f43952010-07-28 05:40:18 -07009044void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
9045 uint32_t cmdSize,
9046 void *pCmdData,
9047 uint32_t replySize,
9048 void *pReplyData)
Mathias Agopian65ab4712010-07-14 17:59:35 -07009049{
9050 if (mEffectClient != 0) {
9051 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
9052 }
9053}
9054
9055
9056
9057void AudioFlinger::EffectHandle::setEnabled(bool enabled)
9058{
9059 if (mEffectClient != 0) {
9060 mEffectClient->enableStatusChanged(enabled);
9061 }
9062}
9063
9064status_t AudioFlinger::EffectHandle::onTransact(
9065 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
9066{
9067 return BnEffect::onTransact(code, data, reply, flags);
9068}
9069
9070
9071void AudioFlinger::EffectHandle::dump(char* buffer, size_t size)
9072{
Glenn Kastena0d68332012-01-27 16:47:15 -08009073 bool locked = mCblk != NULL && tryLock(mCblk->lock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07009074
9075 snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n",
Glenn Kasten44deb052012-02-05 18:09:08 -08009076 (mClient == 0) ? getpid_cached : mClient->pid(),
Mathias Agopian65ab4712010-07-14 17:59:35 -07009077 mPriority,
9078 mHasControl,
9079 !locked,
Marco Nelissen3a34bef2011-08-02 13:33:41 -07009080 mCblk ? mCblk->clientIndex : 0,
9081 mCblk ? mCblk->serverIndex : 0
Mathias Agopian65ab4712010-07-14 17:59:35 -07009082 );
9083
9084 if (locked) {
9085 mCblk->lock.unlock();
9086 }
9087}
9088
9089#undef LOG_TAG
9090#define LOG_TAG "AudioFlinger::EffectChain"
9091
Glenn Kasten9eaa5572012-01-20 13:32:16 -08009092AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Mathias Agopian65ab4712010-07-14 17:59:35 -07009093 int sessionId)
Glenn Kasten9eaa5572012-01-20 13:32:16 -08009094 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Eric Laurentb469b942011-05-09 12:09:06 -07009095 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
9096 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Mathias Agopian65ab4712010-07-14 17:59:35 -07009097{
Dima Zavinfce7a472011-04-19 22:30:36 -07009098 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Glenn Kasten9eaa5572012-01-20 13:32:16 -08009099 if (thread == NULL) {
Eric Laurent544fe9b2011-11-11 15:42:52 -08009100 return;
9101 }
9102 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
9103 thread->frameCount();
Mathias Agopian65ab4712010-07-14 17:59:35 -07009104}
9105
9106AudioFlinger::EffectChain::~EffectChain()
9107{
9108 if (mOwnInBuffer) {
9109 delete mInBuffer;
9110 }
9111
9112}
9113
Eric Laurent59255e42011-07-27 19:49:51 -07009114// getEffectFromDesc_l() must be called with ThreadBase::mLock held
Eric Laurentcab11242010-07-15 12:50:15 -07009115sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor)
Mathias Agopian65ab4712010-07-14 17:59:35 -07009116{
Mathias Agopian65ab4712010-07-14 17:59:35 -07009117 size_t size = mEffects.size();
9118
9119 for (size_t i = 0; i < size; i++) {
9120 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
Glenn Kasten090f0192012-01-30 13:00:02 -08009121 return mEffects[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07009122 }
9123 }
Glenn Kasten090f0192012-01-30 13:00:02 -08009124 return 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07009125}
9126
Eric Laurent59255e42011-07-27 19:49:51 -07009127// getEffectFromId_l() must be called with ThreadBase::mLock held
Eric Laurentcab11242010-07-15 12:50:15 -07009128sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07009129{
Mathias Agopian65ab4712010-07-14 17:59:35 -07009130 size_t size = mEffects.size();
9131
9132 for (size_t i = 0; i < size; i++) {
Eric Laurentde070132010-07-13 04:45:46 -07009133 // by convention, return first effect if id provided is 0 (0 is never a valid id)
9134 if (id == 0 || mEffects[i]->id() == id) {
Glenn Kasten090f0192012-01-30 13:00:02 -08009135 return mEffects[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07009136 }
9137 }
Glenn Kasten090f0192012-01-30 13:00:02 -08009138 return 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07009139}
9140
Eric Laurent59255e42011-07-27 19:49:51 -07009141// getEffectFromType_l() must be called with ThreadBase::mLock held
9142sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
9143 const effect_uuid_t *type)
9144{
Eric Laurent59255e42011-07-27 19:49:51 -07009145 size_t size = mEffects.size();
9146
9147 for (size_t i = 0; i < size; i++) {
9148 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
Glenn Kasten090f0192012-01-30 13:00:02 -08009149 return mEffects[i];
Eric Laurent59255e42011-07-27 19:49:51 -07009150 }
9151 }
Glenn Kasten090f0192012-01-30 13:00:02 -08009152 return 0;
Eric Laurent59255e42011-07-27 19:49:51 -07009153}
9154
Eric Laurent91b14c42012-05-30 12:30:29 -07009155void AudioFlinger::EffectChain::clearInputBuffer()
9156{
9157 Mutex::Autolock _l(mLock);
9158 sp<ThreadBase> thread = mThread.promote();
9159 if (thread == 0) {
9160 ALOGW("clearInputBuffer(): cannot promote mixer thread");
9161 return;
9162 }
9163 clearInputBuffer_l(thread);
9164}
9165
9166// Must be called with EffectChain::mLock locked
9167void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
9168{
9169 size_t numSamples = thread->frameCount() * thread->channelCount();
9170 memset(mInBuffer, 0, numSamples * sizeof(int16_t));
9171
9172}
9173
Mathias Agopian65ab4712010-07-14 17:59:35 -07009174// Must be called with EffectChain::mLock locked
9175void AudioFlinger::EffectChain::process_l()
9176{
Eric Laurentdac69112010-09-28 14:09:57 -07009177 sp<ThreadBase> thread = mThread.promote();
9178 if (thread == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00009179 ALOGW("process_l(): cannot promote mixer thread");
Eric Laurentdac69112010-09-28 14:09:57 -07009180 return;
9181 }
Dima Zavinfce7a472011-04-19 22:30:36 -07009182 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
9183 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Eric Laurent544fe9b2011-11-11 15:42:52 -08009184 // always process effects unless no more tracks are on the session and the effect tail
9185 // has been rendered
9186 bool doProcess = true;
Eric Laurentdac69112010-09-28 14:09:57 -07009187 if (!isGlobalSession) {
Eric Laurent544fe9b2011-11-11 15:42:52 -08009188 bool tracksOnSession = (trackCnt() != 0);
Eric Laurentb469b942011-05-09 12:09:06 -07009189
Eric Laurent544fe9b2011-11-11 15:42:52 -08009190 if (!tracksOnSession && mTailBufferCount == 0) {
9191 doProcess = false;
9192 }
9193
9194 if (activeTrackCnt() == 0) {
9195 // if no track is active and the effect tail has not been rendered,
9196 // the input buffer must be cleared here as the mixer process will not do it
9197 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent91b14c42012-05-30 12:30:29 -07009198 clearInputBuffer_l(thread);
Eric Laurent544fe9b2011-11-11 15:42:52 -08009199 if (mTailBufferCount > 0) {
9200 mTailBufferCount--;
9201 }
9202 }
9203 }
Eric Laurentdac69112010-09-28 14:09:57 -07009204 }
9205
Mathias Agopian65ab4712010-07-14 17:59:35 -07009206 size_t size = mEffects.size();
Eric Laurent544fe9b2011-11-11 15:42:52 -08009207 if (doProcess) {
Eric Laurentdac69112010-09-28 14:09:57 -07009208 for (size_t i = 0; i < size; i++) {
9209 mEffects[i]->process();
9210 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07009211 }
9212 for (size_t i = 0; i < size; i++) {
9213 mEffects[i]->updateState();
9214 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07009215}
9216
Eric Laurentcab11242010-07-15 12:50:15 -07009217// addEffect_l() must be called with PlaybackThread::mLock held
Eric Laurentde070132010-07-13 04:45:46 -07009218status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
Mathias Agopian65ab4712010-07-14 17:59:35 -07009219{
9220 effect_descriptor_t desc = effect->desc();
9221 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
9222
9223 Mutex::Autolock _l(mLock);
Eric Laurentde070132010-07-13 04:45:46 -07009224 effect->setChain(this);
9225 sp<ThreadBase> thread = mThread.promote();
9226 if (thread == 0) {
9227 return NO_INIT;
9228 }
9229 effect->setThread(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07009230
9231 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
9232 // Auxiliary effects are inserted at the beginning of mEffects vector as
9233 // they are processed first and accumulated in chain input buffer
9234 mEffects.insertAt(effect, 0);
Eric Laurentde070132010-07-13 04:45:46 -07009235
Mathias Agopian65ab4712010-07-14 17:59:35 -07009236 // the input buffer for auxiliary effect contains mono samples in
9237 // 32 bit format. This is to avoid saturation in AudoMixer
9238 // accumulation stage. Saturation is done in EffectModule::process() before
9239 // calling the process in effect engine
9240 size_t numSamples = thread->frameCount();
9241 int32_t *buffer = new int32_t[numSamples];
9242 memset(buffer, 0, numSamples * sizeof(int32_t));
9243 effect->setInBuffer((int16_t *)buffer);
9244 // auxiliary effects output samples to chain input buffer for further processing
9245 // by insert effects
9246 effect->setOutBuffer(mInBuffer);
9247 } else {
9248 // Insert effects are inserted at the end of mEffects vector as they are processed
9249 // after track and auxiliary effects.
9250 // Insert effect order as a function of indicated preference:
9251 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
9252 // another effect is present
9253 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
9254 // last effect claiming first position
9255 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
9256 // first effect claiming last position
9257 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
9258 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
9259 // already present
9260
Glenn Kasten8d6a2442012-02-08 14:04:28 -08009261 size_t size = mEffects.size();
9262 size_t idx_insert = size;
9263 ssize_t idx_insert_first = -1;
9264 ssize_t idx_insert_last = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07009265
Glenn Kasten8d6a2442012-02-08 14:04:28 -08009266 for (size_t i = 0; i < size; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07009267 effect_descriptor_t d = mEffects[i]->desc();
9268 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
9269 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
9270 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
9271 // check invalid effect chaining combinations
9272 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
9273 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
Steve Block5ff1dd52012-01-05 23:22:43 +00009274 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name);
Mathias Agopian65ab4712010-07-14 17:59:35 -07009275 return INVALID_OPERATION;
9276 }
9277 // remember position of first insert effect and by default
9278 // select this as insert position for new effect
9279 if (idx_insert == size) {
9280 idx_insert = i;
9281 }
9282 // remember position of last insert effect claiming
9283 // first position
9284 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
9285 idx_insert_first = i;
9286 }
9287 // remember position of first insert effect claiming
9288 // last position
9289 if (iPref == EFFECT_FLAG_INSERT_LAST &&
9290 idx_insert_last == -1) {
9291 idx_insert_last = i;
9292 }
9293 }
9294 }
9295
9296 // modify idx_insert from first position if needed
9297 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
9298 if (idx_insert_last != -1) {
9299 idx_insert = idx_insert_last;
9300 } else {
9301 idx_insert = size;
9302 }
9303 } else {
9304 if (idx_insert_first != -1) {
9305 idx_insert = idx_insert_first + 1;
9306 }
9307 }
9308
9309 // always read samples from chain input buffer
9310 effect->setInBuffer(mInBuffer);
9311
9312 // if last effect in the chain, output samples to chain
9313 // output buffer, otherwise to chain input buffer
9314 if (idx_insert == size) {
9315 if (idx_insert != 0) {
9316 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
9317 mEffects[idx_insert-1]->configure();
9318 }
9319 effect->setOutBuffer(mOutBuffer);
9320 } else {
9321 effect->setOutBuffer(mInBuffer);
9322 }
9323 mEffects.insertAt(effect, idx_insert);
Mathias Agopian65ab4712010-07-14 17:59:35 -07009324
Steve Block3856b092011-10-20 11:56:00 +01009325 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert);
Mathias Agopian65ab4712010-07-14 17:59:35 -07009326 }
9327 effect->configure();
9328 return NO_ERROR;
9329}
9330
Eric Laurentcab11242010-07-15 12:50:15 -07009331// removeEffect_l() must be called with PlaybackThread::mLock held
9332size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
Mathias Agopian65ab4712010-07-14 17:59:35 -07009333{
9334 Mutex::Autolock _l(mLock);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08009335 size_t size = mEffects.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07009336 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
9337
Glenn Kasten8d6a2442012-02-08 14:04:28 -08009338 for (size_t i = 0; i < size; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07009339 if (effect == mEffects[i]) {
Eric Laurentec437d82011-07-26 20:54:46 -07009340 // calling stop here will remove pre-processing effect from the audio HAL.
9341 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
9342 // the middle of a read from audio HAL
Eric Laurentec35a142011-10-05 17:42:25 -07009343 if (mEffects[i]->state() == EffectModule::ACTIVE ||
9344 mEffects[i]->state() == EffectModule::STOPPING) {
9345 mEffects[i]->stop();
9346 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07009347 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
9348 delete[] effect->inBuffer();
9349 } else {
9350 if (i == size - 1 && i != 0) {
9351 mEffects[i - 1]->setOutBuffer(mOutBuffer);
9352 mEffects[i - 1]->configure();
9353 }
9354 }
9355 mEffects.removeAt(i);
Steve Block3856b092011-10-20 11:56:00 +01009356 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i);
Mathias Agopian65ab4712010-07-14 17:59:35 -07009357 break;
9358 }
9359 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07009360
9361 return mEffects.size();
9362}
9363
Eric Laurentcab11242010-07-15 12:50:15 -07009364// setDevice_l() must be called with PlaybackThread::mLock held
Glenn Kastenbb4350d2012-07-03 15:56:38 -07009365void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -07009366{
9367 size_t size = mEffects.size();
9368 for (size_t i = 0; i < size; i++) {
9369 mEffects[i]->setDevice(device);
9370 }
9371}
9372
Eric Laurentcab11242010-07-15 12:50:15 -07009373// setMode_l() must be called with PlaybackThread::mLock held
Glenn Kastenf78aee72012-01-04 11:00:47 -08009374void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -07009375{
9376 size_t size = mEffects.size();
9377 for (size_t i = 0; i < size; i++) {
9378 mEffects[i]->setMode(mode);
9379 }
9380}
9381
Eric Laurentcab11242010-07-15 12:50:15 -07009382// setVolume_l() must be called with PlaybackThread::mLock held
9383bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
Mathias Agopian65ab4712010-07-14 17:59:35 -07009384{
9385 uint32_t newLeft = *left;
9386 uint32_t newRight = *right;
9387 bool hasControl = false;
Eric Laurentcab11242010-07-15 12:50:15 -07009388 int ctrlIdx = -1;
9389 size_t size = mEffects.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07009390
Eric Laurentcab11242010-07-15 12:50:15 -07009391 // first update volume controller
9392 for (size_t i = size; i > 0; i--) {
Eric Laurent8f45bd72010-08-31 13:50:07 -07009393 if (mEffects[i - 1]->isProcessEnabled() &&
Eric Laurentcab11242010-07-15 12:50:15 -07009394 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
9395 ctrlIdx = i - 1;
Eric Laurentf997cab2010-07-19 06:24:46 -07009396 hasControl = true;
Eric Laurentcab11242010-07-15 12:50:15 -07009397 break;
9398 }
9399 }
9400
9401 if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentf997cab2010-07-19 06:24:46 -07009402 if (hasControl) {
9403 *left = mNewLeftVolume;
9404 *right = mNewRightVolume;
9405 }
9406 return hasControl;
Eric Laurentcab11242010-07-15 12:50:15 -07009407 }
9408
9409 mVolumeCtrlIdx = ctrlIdx;
Eric Laurentf997cab2010-07-19 06:24:46 -07009410 mLeftVolume = newLeft;
9411 mRightVolume = newRight;
Eric Laurentcab11242010-07-15 12:50:15 -07009412
9413 // second get volume update from volume controller
9414 if (ctrlIdx >= 0) {
9415 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
Eric Laurentf997cab2010-07-19 06:24:46 -07009416 mNewLeftVolume = newLeft;
9417 mNewRightVolume = newRight;
Mathias Agopian65ab4712010-07-14 17:59:35 -07009418 }
9419 // then indicate volume to all other effects in chain.
9420 // Pass altered volume to effects before volume controller
9421 // and requested volume to effects after controller
9422 uint32_t lVol = newLeft;
9423 uint32_t rVol = newRight;
Eric Laurentcab11242010-07-15 12:50:15 -07009424
Mathias Agopian65ab4712010-07-14 17:59:35 -07009425 for (size_t i = 0; i < size; i++) {
Eric Laurentcab11242010-07-15 12:50:15 -07009426 if ((int)i == ctrlIdx) continue;
9427 // this also works for ctrlIdx == -1 when there is no volume controller
9428 if ((int)i > ctrlIdx) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07009429 lVol = *left;
9430 rVol = *right;
9431 }
9432 mEffects[i]->setVolume(&lVol, &rVol, false);
9433 }
9434 *left = newLeft;
9435 *right = newRight;
9436
9437 return hasControl;
9438}
9439
Glenn Kastenbe5f05e2012-07-18 15:24:02 -07009440void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -07009441{
9442 const size_t SIZE = 256;
9443 char buffer[SIZE];
9444 String8 result;
9445
9446 snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId);
9447 result.append(buffer);
9448
9449 bool locked = tryLock(mLock);
9450 // failed to lock - AudioFlinger is probably deadlocked
9451 if (!locked) {
9452 result.append("\tCould not lock mutex:\n");
9453 }
9454
Eric Laurentcab11242010-07-15 12:50:15 -07009455 result.append("\tNum fx In buffer Out buffer Active tracks:\n");
9456 snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n",
Mathias Agopian65ab4712010-07-14 17:59:35 -07009457 mEffects.size(),
9458 (uint32_t)mInBuffer,
9459 (uint32_t)mOutBuffer,
Mathias Agopian65ab4712010-07-14 17:59:35 -07009460 mActiveTrackCnt);
9461 result.append(buffer);
9462 write(fd, result.string(), result.size());
9463
9464 for (size_t i = 0; i < mEffects.size(); ++i) {
9465 sp<EffectModule> effect = mEffects[i];
9466 if (effect != 0) {
9467 effect->dump(fd, args);
9468 }
9469 }
9470
9471 if (locked) {
9472 mLock.unlock();
9473 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07009474}
9475
Eric Laurent59255e42011-07-27 19:49:51 -07009476// must be called with ThreadBase::mLock held
9477void AudioFlinger::EffectChain::setEffectSuspended_l(
9478 const effect_uuid_t *type, bool suspend)
9479{
9480 sp<SuspendedEffectDesc> desc;
9481 // use effect type UUID timelow as key as there is no real risk of identical
9482 // timeLow fields among effect type UUIDs.
Glenn Kasten8d6a2442012-02-08 14:04:28 -08009483 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
Eric Laurent59255e42011-07-27 19:49:51 -07009484 if (suspend) {
9485 if (index >= 0) {
9486 desc = mSuspendedEffects.valueAt(index);
9487 } else {
9488 desc = new SuspendedEffectDesc();
Glenn Kastena189a682012-02-20 12:16:30 -08009489 desc->mType = *type;
Eric Laurent59255e42011-07-27 19:49:51 -07009490 mSuspendedEffects.add(type->timeLow, desc);
Steve Block3856b092011-10-20 11:56:00 +01009491 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
Eric Laurent59255e42011-07-27 19:49:51 -07009492 }
9493 if (desc->mRefCount++ == 0) {
9494 sp<EffectModule> effect = getEffectIfEnabled(type);
9495 if (effect != 0) {
9496 desc->mEffect = effect;
9497 effect->setSuspended(true);
9498 effect->setEnabled(false);
9499 }
9500 }
9501 } else {
9502 if (index < 0) {
9503 return;
9504 }
9505 desc = mSuspendedEffects.valueAt(index);
9506 if (desc->mRefCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00009507 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurent59255e42011-07-27 19:49:51 -07009508 desc->mRefCount = 1;
9509 }
9510 if (--desc->mRefCount == 0) {
Steve Block3856b092011-10-20 11:56:00 +01009511 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
Eric Laurent59255e42011-07-27 19:49:51 -07009512 if (desc->mEffect != 0) {
9513 sp<EffectModule> effect = desc->mEffect.promote();
9514 if (effect != 0) {
9515 effect->setSuspended(false);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07009516 effect->lock();
9517 EffectHandle *handle = effect->controlHandle_l();
9518 if (handle != NULL && !handle->destroyed_l()) {
9519 effect->setEnabled_l(handle->enabled());
Eric Laurent59255e42011-07-27 19:49:51 -07009520 }
Eric Laurenta5f44eb2012-06-25 11:38:29 -07009521 effect->unlock();
Eric Laurent59255e42011-07-27 19:49:51 -07009522 }
9523 desc->mEffect.clear();
9524 }
9525 mSuspendedEffects.removeItemsAt(index);
9526 }
9527 }
9528}
9529
9530// must be called with ThreadBase::mLock held
9531void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
9532{
9533 sp<SuspendedEffectDesc> desc;
9534
Glenn Kasten8d6a2442012-02-08 14:04:28 -08009535 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
Eric Laurent59255e42011-07-27 19:49:51 -07009536 if (suspend) {
9537 if (index >= 0) {
9538 desc = mSuspendedEffects.valueAt(index);
9539 } else {
9540 desc = new SuspendedEffectDesc();
9541 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
Steve Block3856b092011-10-20 11:56:00 +01009542 ALOGV("setEffectSuspendedAll_l() add entry for 0");
Eric Laurent59255e42011-07-27 19:49:51 -07009543 }
9544 if (desc->mRefCount++ == 0) {
Glenn Kastend0539712012-01-30 12:56:03 -08009545 Vector< sp<EffectModule> > effects;
9546 getSuspendEligibleEffects(effects);
Eric Laurent59255e42011-07-27 19:49:51 -07009547 for (size_t i = 0; i < effects.size(); i++) {
9548 setEffectSuspended_l(&effects[i]->desc().type, true);
9549 }
9550 }
9551 } else {
9552 if (index < 0) {
9553 return;
9554 }
9555 desc = mSuspendedEffects.valueAt(index);
9556 if (desc->mRefCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00009557 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurent59255e42011-07-27 19:49:51 -07009558 desc->mRefCount = 1;
9559 }
9560 if (--desc->mRefCount == 0) {
9561 Vector<const effect_uuid_t *> types;
9562 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
9563 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
9564 continue;
9565 }
9566 types.add(&mSuspendedEffects.valueAt(i)->mType);
9567 }
9568 for (size_t i = 0; i < types.size(); i++) {
9569 setEffectSuspended_l(types[i], false);
9570 }
Steve Block3856b092011-10-20 11:56:00 +01009571 ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
Eric Laurent59255e42011-07-27 19:49:51 -07009572 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
9573 }
9574 }
9575}
9576
Eric Laurent6bffdb82011-09-23 08:40:41 -07009577
9578// The volume effect is used for automated tests only
9579#ifndef OPENSL_ES_H_
9580static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
9581 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
9582const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
9583#endif //OPENSL_ES_H_
9584
Eric Laurentdb7c0792011-08-10 10:37:50 -07009585bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
9586{
9587 // auxiliary effects and visualizer are never suspended on output mix
9588 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
9589 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
Eric Laurent6bffdb82011-09-23 08:40:41 -07009590 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
9591 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentdb7c0792011-08-10 10:37:50 -07009592 return false;
9593 }
9594 return true;
9595}
9596
Glenn Kastend0539712012-01-30 12:56:03 -08009597void AudioFlinger::EffectChain::getSuspendEligibleEffects(Vector< sp<AudioFlinger::EffectModule> > &effects)
Eric Laurent59255e42011-07-27 19:49:51 -07009598{
Glenn Kastend0539712012-01-30 12:56:03 -08009599 effects.clear();
Eric Laurent59255e42011-07-27 19:49:51 -07009600 for (size_t i = 0; i < mEffects.size(); i++) {
Glenn Kastend0539712012-01-30 12:56:03 -08009601 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
9602 effects.add(mEffects[i]);
Eric Laurent59255e42011-07-27 19:49:51 -07009603 }
Eric Laurent59255e42011-07-27 19:49:51 -07009604 }
Eric Laurent59255e42011-07-27 19:49:51 -07009605}
9606
9607sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
9608 const effect_uuid_t *type)
9609{
Glenn Kasten090f0192012-01-30 13:00:02 -08009610 sp<EffectModule> effect = getEffectFromType_l(type);
9611 return effect != 0 && effect->isEnabled() ? effect : 0;
Eric Laurent59255e42011-07-27 19:49:51 -07009612}
9613
9614void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
9615 bool enabled)
9616{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08009617 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
Eric Laurent59255e42011-07-27 19:49:51 -07009618 if (enabled) {
9619 if (index < 0) {
9620 // if the effect is not suspend check if all effects are suspended
9621 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
9622 if (index < 0) {
9623 return;
9624 }
Eric Laurentdb7c0792011-08-10 10:37:50 -07009625 if (!isEffectEligibleForSuspend(effect->desc())) {
9626 return;
9627 }
Eric Laurent59255e42011-07-27 19:49:51 -07009628 setEffectSuspended_l(&effect->desc().type, enabled);
9629 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
Eric Laurentdb7c0792011-08-10 10:37:50 -07009630 if (index < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00009631 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
Eric Laurentdb7c0792011-08-10 10:37:50 -07009632 return;
9633 }
Eric Laurent59255e42011-07-27 19:49:51 -07009634 }
Steve Block3856b092011-10-20 11:56:00 +01009635 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07009636 effect->desc().type.timeLow);
Eric Laurent59255e42011-07-27 19:49:51 -07009637 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
9638 // if effect is requested to suspended but was not yet enabled, supend it now.
9639 if (desc->mEffect == 0) {
9640 desc->mEffect = effect;
9641 effect->setEnabled(false);
9642 effect->setSuspended(true);
9643 }
9644 } else {
9645 if (index < 0) {
9646 return;
9647 }
Steve Block3856b092011-10-20 11:56:00 +01009648 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07009649 effect->desc().type.timeLow);
Eric Laurent59255e42011-07-27 19:49:51 -07009650 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
9651 desc->mEffect.clear();
9652 effect->setSuspended(false);
9653 }
9654}
9655
Mathias Agopian65ab4712010-07-14 17:59:35 -07009656#undef LOG_TAG
9657#define LOG_TAG "AudioFlinger"
9658
9659// ----------------------------------------------------------------------------
9660
9661status_t AudioFlinger::onTransact(
9662 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
9663{
9664 return BnAudioFlinger::onTransact(code, data, reply, flags);
9665}
9666
Mathias Agopian65ab4712010-07-14 17:59:35 -07009667}; // namespace android