blob: 626b5c298a6afec34ad3a6f15257ca343018a1e6 [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
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080023#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <math.h>
25#include <signal.h>
26#include <sys/time.h>
27#include <sys/resource.h>
28
Gloria Wang9ee159b2011-02-24 14:51:45 -080029#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <binder/IServiceManager.h>
31#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070032#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070036#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
Dima Zavinfce7a472011-04-19 22:30:36 -070038#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039#include <cutils/properties.h>
40
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
44#include "AudioMixer.h"
45#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080046#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070049#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070050#include <audio_effects/effect_ns.h>
51#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten3b21c502011-12-15 09:52:39 -080053#include <audio_utils/primitives.h>
54
Eric Laurentfeb0db62011-07-22 09:04:31 -070055#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080056
John Grossman4ff14ba2012-02-08 16:37:41 -080057#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070058
Glenn Kasten9e58b552013-01-18 15:09:48 -080059#include <media/IMediaLogService.h>
60
Glenn Kastenda6ef132013-01-10 12:31:01 -080061#include <media/nbaio/Pipe.h>
62#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070063#include <media/AudioParameter.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070064#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080065
Mathias Agopian65ab4712010-07-14 17:59:35 -070066// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
John Grossman1c345192012-03-27 14:00:17 -070068// Note: the following macro is used for extremely verbose logging message. In
69// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
70// 0; but one side effect of this is to turn all LOGV's as well. Some messages
71// are so verbose that we want to suppress them even when we have ALOG_ASSERT
72// turned on. Do not uncomment the #def below unless you really know what you
73// are doing and want to see all of the extremely verbose messages.
74//#define VERY_VERY_VERBOSE_LOGGING
75#ifdef VERY_VERY_VERBOSE_LOGGING
76#define ALOGVV ALOGV
77#else
78#define ALOGVV(a...) do { } while(0)
79#endif
Eric Laurentde070132010-07-13 04:45:46 -070080
Mathias Agopian65ab4712010-07-14 17:59:35 -070081namespace android {
82
Glenn Kastenec1d6b52011-12-12 09:04:45 -080083static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
84static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070085
Glenn Kasten58912562012-04-03 10:45:00 -070086
John Grossman4ff14ba2012-02-08 16:37:41 -080087nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080088
Eric Laurent81784c32012-11-19 14:55:58 -080089uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070090
Glenn Kasten46909e72013-02-26 09:20:22 -080091#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080092bool AudioFlinger::mTeeSinkInputEnabled = false;
93bool AudioFlinger::mTeeSinkOutputEnabled = false;
94bool AudioFlinger::mTeeSinkTrackEnabled = false;
95
96size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
97size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
98size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -080099#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800100
Eric Laurent813e2a72013-08-31 12:59:48 -0700101//TODO: remove when effect offload is implemented
102// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
103// we define a minimum time during which a global effect is considered enabled.
104static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
105
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106// ----------------------------------------------------------------------------
107
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700108static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700109{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700110 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700111 int rc;
112
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700113 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
114 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
115 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
116 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700117 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700118 }
119 rc = audio_hw_device_open(mod, dev);
120 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
121 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
122 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700123 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700124 }
125 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
126 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
127 rc = BAD_VALUE;
128 goto out;
129 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700130 return 0;
131
132out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700133 *dev = NULL;
134 return rc;
135}
136
Mathias Agopian65ab4712010-07-14 17:59:35 -0700137// ----------------------------------------------------------------------------
138
139AudioFlinger::AudioFlinger()
140 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800141 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700142 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800143 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800144 mMasterMute(false),
145 mNextUniqueId(1),
146 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700147 mBtNrecIsOff(false),
148 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700149 mIsDeviceTypeKnown(false),
150 mGlobalEffectEnableTime(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151{
Glenn Kasten949a9262013-04-16 12:35:20 -0700152 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800153 char value[PROPERTY_VALUE_MAX];
154 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
155 if (doLog) {
156 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
157 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800158#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800159 (void) property_get("ro.debuggable", value, "0");
160 int debuggable = atoi(value);
161 int teeEnabled = 0;
162 if (debuggable) {
163 (void) property_get("af.tee", value, "0");
164 teeEnabled = atoi(value);
165 }
166 if (teeEnabled & 1)
167 mTeeSinkInputEnabled = true;
168 if (teeEnabled & 2)
169 mTeeSinkOutputEnabled = true;
170 if (teeEnabled & 4)
171 mTeeSinkTrackEnabled = true;
Glenn Kasten46909e72013-02-26 09:20:22 -0800172#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700173}
174
175void AudioFlinger::onFirstRef()
176{
Dima Zavin799a70e2011-04-18 16:57:27 -0700177 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700178
Eric Laurent93575202011-01-18 18:39:02 -0800179 Mutex::Autolock _l(mLock);
180
Dima Zavin799a70e2011-04-18 16:57:27 -0700181 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800182 char val_str[PROPERTY_VALUE_MAX] = { 0 };
183 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
184 uint32_t int_val;
185 if (1 == sscanf(val_str, "%u", &int_val)) {
186 mStandbyTimeInNsecs = milliseconds(int_val);
187 ALOGI("Using %u mSec as standby time.", int_val);
188 } else {
189 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
190 ALOGI("Using default %u mSec as standby time.",
191 (uint32_t)(mStandbyTimeInNsecs / 1000000));
192 }
193 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194
Eric Laurenta4c5a552012-03-29 10:12:40 -0700195 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196}
197
198AudioFlinger::~AudioFlinger()
199{
200 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700201 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700202 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700203 }
204 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700205 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700206 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700208
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800209 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
210 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700211 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
212 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213 }
214}
215
Eric Laurenta4c5a552012-03-29 10:12:40 -0700216static const char * const audio_interfaces[] = {
217 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
218 AUDIO_HARDWARE_MODULE_ID_A2DP,
219 AUDIO_HARDWARE_MODULE_ID_USB,
220};
221#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
222
John Grossmanee578c02012-07-23 17:05:46 -0700223AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
224 audio_module_handle_t module,
225 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700226{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700227 // if module is 0, the request comes from an old policy manager and we should load
228 // well known modules
229 if (module == 0) {
230 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
231 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
232 loadHwModule_l(audio_interfaces[i]);
233 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700234 // then try to find a module supporting the requested device.
235 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
236 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
237 audio_hw_device_t *dev = audioHwDevice->hwDevice();
238 if ((dev->get_supported_devices != NULL) &&
239 (dev->get_supported_devices(dev) & devices) == devices)
240 return audioHwDevice;
241 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700242 } else {
243 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700244 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
245 if (audioHwDevice != NULL) {
246 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700247 }
248 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700249
Dima Zavin799a70e2011-04-18 16:57:27 -0700250 return NULL;
251}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700253void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254{
255 const size_t SIZE = 256;
256 char buffer[SIZE];
257 String8 result;
258
259 result.append("Clients:\n");
260 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800261 sp<Client> client = mClients.valueAt(i).promote();
262 if (client != 0) {
263 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
264 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265 }
266 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700267
268 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800269 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700270 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
271 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800272 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700273 result.append(buffer);
274 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276}
277
278
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700279void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280{
281 const size_t SIZE = 256;
282 char buffer[SIZE];
283 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800284 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285
John Grossman4ff14ba2012-02-08 16:37:41 -0800286 snprintf(buffer, SIZE, "Hardware status: %d\n"
287 "Standby Time mSec: %u\n",
288 hardwareStatus,
289 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700290 result.append(buffer);
291 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292}
293
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700294void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295{
296 const size_t SIZE = 256;
297 char buffer[SIZE];
298 String8 result;
299 snprintf(buffer, SIZE, "Permission Denial: "
300 "can't dump AudioFlinger from pid=%d, uid=%d\n",
301 IPCThreadState::self()->getCallingPid(),
302 IPCThreadState::self()->getCallingUid());
303 result.append(buffer);
304 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305}
306
Eric Laurent81784c32012-11-19 14:55:58 -0800307bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700308{
309 bool locked = false;
310 for (int i = 0; i < kDumpLockRetries; ++i) {
311 if (mutex.tryLock() == NO_ERROR) {
312 locked = true;
313 break;
314 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800315 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 }
317 return locked;
318}
319
320status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
321{
Glenn Kasten44deb052012-02-05 18:09:08 -0800322 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 dumpPermissionDenial(fd, args);
324 } else {
325 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800326 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327 if (!hardwareLocked) {
328 String8 result(kHardwareLockedString);
329 write(fd, result.string(), result.size());
330 } else {
331 mHardwareLock.unlock();
332 }
333
Eric Laurent81784c32012-11-19 14:55:58 -0800334 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335
336 // failed to lock - AudioFlinger is probably deadlocked
337 if (!locked) {
338 String8 result(kDeadlockedString);
339 write(fd, result.string(), result.size());
340 }
341
342 dumpClients(fd, args);
343 dumpInternals(fd, args);
344
345 // dump playback threads
346 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
347 mPlaybackThreads.valueAt(i)->dump(fd, args);
348 }
349
350 // dump record threads
351 for (size_t i = 0; i < mRecordThreads.size(); i++) {
352 mRecordThreads.valueAt(i)->dump(fd, args);
353 }
354
Dima Zavin799a70e2011-04-18 16:57:27 -0700355 // dump all hardware devs
356 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700357 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700358 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700360
Glenn Kasten46909e72013-02-26 09:20:22 -0800361#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700362 // dump the serially shared record tee sink
363 if (mRecordTeeSource != 0) {
364 dumpTee(fd, mRecordTeeSource);
365 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800366#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700367
Glenn Kastend65d73c2012-06-22 17:21:07 -0700368 if (locked) {
369 mLock.unlock();
370 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800371
372 // append a copy of media.log here by forwarding fd to it, but don't attempt
373 // to lookup the service if it's not running, as it will block for a second
374 if (mLogMemoryDealer != 0) {
375 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
376 if (binder != 0) {
377 fdprintf(fd, "\nmedia.log:\n");
378 Vector<String16> args;
379 binder->dump(fd, args);
380 }
381 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382 }
383 return NO_ERROR;
384}
385
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800386sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
387{
388 // If pid is already in the mClients wp<> map, then use that entry
389 // (for which promote() is always != 0), otherwise create a new entry and Client.
390 sp<Client> client = mClients.valueFor(pid).promote();
391 if (client == 0) {
392 client = new Client(this, pid);
393 mClients.add(pid, client);
394 }
395
396 return client;
397}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700398
Glenn Kasten9e58b552013-01-18 15:09:48 -0800399sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
400{
401 if (mLogMemoryDealer == 0) {
402 return new NBLog::Writer();
403 }
404 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
405 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
406 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
407 if (binder != 0) {
408 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
409 }
410 return writer;
411}
412
413void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
414{
Glenn Kasten685ef092013-02-04 08:15:34 -0800415 if (writer == 0) {
416 return;
417 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800418 sp<IMemory> iMemory(writer->getIMemory());
419 if (iMemory == 0) {
420 return;
421 }
422 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
423 if (binder != 0) {
424 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
425 // Now the media.log remote reference to IMemory is gone.
426 // When our last local reference to IMemory also drops to zero,
427 // the IMemory destructor will deallocate the region from mMemoryDealer.
428 }
429}
430
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431// IAudioFlinger interface
432
433
434sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800435 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800437 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700438 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800439 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800440 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800442 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800443 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700444 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700445 String8& name,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700446 status_t *status)
447{
448 sp<PlaybackThread::Track> track;
449 sp<TrackHandle> trackHandle;
450 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700451 status_t lStatus;
452 int lSessionId;
453
Glenn Kasten263709e2012-01-06 08:40:01 -0800454 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
455 // but if someone uses binder directly they could bypass that and cause us to crash
456 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000457 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700458 lStatus = BAD_VALUE;
459 goto Exit;
460 }
461
Glenn Kasten60a83922012-06-21 12:56:37 -0700462 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
463 // and we don't yet support 8.24 or 32-bit PCM
464 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
465 ALOGE("createTrack() invalid format %d", format);
466 lStatus = BAD_VALUE;
467 goto Exit;
468 }
469
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470 {
471 Mutex::Autolock _l(mLock);
472 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700473 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700474 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800475 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476 lStatus = BAD_VALUE;
477 goto Exit;
478 }
479
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800480 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800481 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700482
Steve Block3856b092011-10-20 11:56:00 +0100483 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700484 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700485 // check if an effect chain with the same session ID is present on another
486 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700487 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700488 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
489 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700490 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700491 if (sessions & PlaybackThread::EFFECT_SESSION) {
492 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700493 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700494 }
Eric Laurentde070132010-07-13 04:45:46 -0700495 }
496 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700497 lSessionId = *sessionId;
498 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700499 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700500 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700501 if (sessionId != NULL) {
502 *sessionId = lSessionId;
503 }
504 }
Steve Block3856b092011-10-20 11:56:00 +0100505 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700506
507 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800508 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700509
510 // move effect chain to this output thread if an effect on same session was waiting
511 // for a track to be created
512 if (lStatus == NO_ERROR && effectThread != NULL) {
513 Mutex::Autolock _dl(thread->mLock);
514 Mutex::Autolock _sl(effectThread->mLock);
515 moveEffectChain_l(lSessionId, effectThread, thread, true);
516 }
Eric Laurenta011e352012-03-29 15:51:43 -0700517
518 // Look for sync events awaiting for a session to be used.
519 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
520 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
521 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700522 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700523 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700524 } else {
525 mPendingSyncEvents[i]->cancel();
526 }
Eric Laurenta011e352012-03-29 15:51:43 -0700527 mPendingSyncEvents.removeAt(i);
528 i--;
529 }
530 }
531 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700532 }
533 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700534 // s for server's pid, n for normal mixer name, f for fast index
535 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
536 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537 trackHandle = new TrackHandle(track);
538 } else {
539 // remove local strong reference to Client before deleting the Track so that the Client
540 // destructor is called by the TrackBase destructor with mLock held
541 client.clear();
542 track.clear();
543 }
544
545Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700546 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547 *status = lStatus;
548 }
549 return trackHandle;
550}
551
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800552uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553{
554 Mutex::Autolock _l(mLock);
555 PlaybackThread *thread = checkPlaybackThread_l(output);
556 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000557 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558 return 0;
559 }
560 return thread->sampleRate();
561}
562
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800563int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700564{
565 Mutex::Autolock _l(mLock);
566 PlaybackThread *thread = checkPlaybackThread_l(output);
567 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000568 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569 return 0;
570 }
571 return thread->channelCount();
572}
573
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800574audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575{
576 Mutex::Autolock _l(mLock);
577 PlaybackThread *thread = checkPlaybackThread_l(output);
578 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000579 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800580 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581 }
582 return thread->format();
583}
584
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800585size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586{
587 Mutex::Autolock _l(mLock);
588 PlaybackThread *thread = checkPlaybackThread_l(output);
589 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000590 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 return 0;
592 }
Glenn Kasten58912562012-04-03 10:45:00 -0700593 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
594 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 return thread->frameCount();
596}
597
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800598uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599{
600 Mutex::Autolock _l(mLock);
601 PlaybackThread *thread = checkPlaybackThread_l(output);
602 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800603 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 return 0;
605 }
606 return thread->latency();
607}
608
609status_t AudioFlinger::setMasterVolume(float value)
610{
Eric Laurenta1884f92011-08-23 08:25:03 -0700611 status_t ret = initCheck();
612 if (ret != NO_ERROR) {
613 return ret;
614 }
615
Mathias Agopian65ab4712010-07-14 17:59:35 -0700616 // check calling permissions
617 if (!settingsAllowed()) {
618 return PERMISSION_DENIED;
619 }
620
Eric Laurenta4c5a552012-03-29 10:12:40 -0700621 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700622 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700623
John Grossmanee578c02012-07-23 17:05:46 -0700624 // Set master volume in the HALs which support it.
625 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
626 AutoMutex lock(mHardwareLock);
627 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800628
John Grossmanee578c02012-07-23 17:05:46 -0700629 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
630 if (dev->canSetMasterVolume()) {
631 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800632 }
John Grossmanee578c02012-07-23 17:05:46 -0700633 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635
John Grossmanee578c02012-07-23 17:05:46 -0700636 // Now set the master volume in each playback thread. Playback threads
637 // assigned to HALs which do not have master volume support will apply
638 // master volume during the mix operation. Threads with HALs which do
639 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800640 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700641 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642
643 return NO_ERROR;
644}
645
Glenn Kastenf78aee72012-01-04 11:00:47 -0800646status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647{
Eric Laurenta1884f92011-08-23 08:25:03 -0700648 status_t ret = initCheck();
649 if (ret != NO_ERROR) {
650 return ret;
651 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652
653 // check calling permissions
654 if (!settingsAllowed()) {
655 return PERMISSION_DENIED;
656 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800657 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000658 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700659 return BAD_VALUE;
660 }
661
662 { // scope for the lock
663 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700664 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700666 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667 mHardwareStatus = AUDIO_HW_IDLE;
668 }
669
670 if (NO_ERROR == ret) {
671 Mutex::Autolock _l(mLock);
672 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800673 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700674 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675 }
676
677 return ret;
678}
679
680status_t AudioFlinger::setMicMute(bool state)
681{
Eric Laurenta1884f92011-08-23 08:25:03 -0700682 status_t ret = initCheck();
683 if (ret != NO_ERROR) {
684 return ret;
685 }
686
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687 // check calling permissions
688 if (!settingsAllowed()) {
689 return PERMISSION_DENIED;
690 }
691
692 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700693 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700695 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696 mHardwareStatus = AUDIO_HW_IDLE;
697 return ret;
698}
699
700bool AudioFlinger::getMicMute() const
701{
Eric Laurenta1884f92011-08-23 08:25:03 -0700702 status_t ret = initCheck();
703 if (ret != NO_ERROR) {
704 return false;
705 }
706
Dima Zavinfce7a472011-04-19 22:30:36 -0700707 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800708 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700709 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700711 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 mHardwareStatus = AUDIO_HW_IDLE;
713 return state;
714}
715
716status_t AudioFlinger::setMasterMute(bool muted)
717{
John Grossmand8f178d2012-07-20 14:51:35 -0700718 status_t ret = initCheck();
719 if (ret != NO_ERROR) {
720 return ret;
721 }
722
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723 // check calling permissions
724 if (!settingsAllowed()) {
725 return PERMISSION_DENIED;
726 }
727
John Grossmanee578c02012-07-23 17:05:46 -0700728 Mutex::Autolock _l(mLock);
729 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700730
John Grossmanee578c02012-07-23 17:05:46 -0700731 // Set master mute in the HALs which support it.
732 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
733 AutoMutex lock(mHardwareLock);
734 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700735
John Grossmanee578c02012-07-23 17:05:46 -0700736 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
737 if (dev->canSetMasterMute()) {
738 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700739 }
John Grossmanee578c02012-07-23 17:05:46 -0700740 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700741 }
742
John Grossmanee578c02012-07-23 17:05:46 -0700743 // Now set the master mute in each playback thread. Playback threads
744 // assigned to HALs which do not have master mute support will apply master
745 // mute during the mix operation. Threads with HALs which do support master
746 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800747 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700748 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749
750 return NO_ERROR;
751}
752
753float AudioFlinger::masterVolume() const
754{
Glenn Kasten98067102011-12-13 11:47:54 -0800755 Mutex::Autolock _l(mLock);
756 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757}
758
759bool AudioFlinger::masterMute() const
760{
Glenn Kasten98067102011-12-13 11:47:54 -0800761 Mutex::Autolock _l(mLock);
762 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763}
764
John Grossman4ff14ba2012-02-08 16:37:41 -0800765float AudioFlinger::masterVolume_l() const
766{
John Grossman4ff14ba2012-02-08 16:37:41 -0800767 return mMasterVolume;
768}
769
John Grossmand8f178d2012-07-20 14:51:35 -0700770bool AudioFlinger::masterMute_l() const
771{
John Grossmanee578c02012-07-23 17:05:46 -0700772 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700773}
774
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800775status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
776 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777{
778 // check calling permissions
779 if (!settingsAllowed()) {
780 return PERMISSION_DENIED;
781 }
782
Glenn Kasten263709e2012-01-06 08:40:01 -0800783 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000784 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700785 return BAD_VALUE;
786 }
787
788 AutoMutex lock(mLock);
789 PlaybackThread *thread = NULL;
790 if (output) {
791 thread = checkPlaybackThread_l(output);
792 if (thread == NULL) {
793 return BAD_VALUE;
794 }
795 }
796
797 mStreamTypes[stream].volume = value;
798
799 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800800 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700801 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802 }
803 } else {
804 thread->setStreamVolume(stream, value);
805 }
806
807 return NO_ERROR;
808}
809
Glenn Kastenfff6d712012-01-12 16:38:12 -0800810status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811{
812 // check calling permissions
813 if (!settingsAllowed()) {
814 return PERMISSION_DENIED;
815 }
816
Glenn Kasten263709e2012-01-06 08:40:01 -0800817 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700818 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000819 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 return BAD_VALUE;
821 }
822
Eric Laurent93575202011-01-18 18:39:02 -0800823 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824 mStreamTypes[stream].mute = muted;
825 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700826 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700827
828 return NO_ERROR;
829}
830
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800831float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832{
Glenn Kasten263709e2012-01-06 08:40:01 -0800833 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 return 0.0f;
835 }
836
837 AutoMutex lock(mLock);
838 float volume;
839 if (output) {
840 PlaybackThread *thread = checkPlaybackThread_l(output);
841 if (thread == NULL) {
842 return 0.0f;
843 }
844 volume = thread->streamVolume(stream);
845 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800846 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847 }
848
849 return volume;
850}
851
Glenn Kastenfff6d712012-01-12 16:38:12 -0800852bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853{
Glenn Kasten263709e2012-01-06 08:40:01 -0800854 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 return true;
856 }
857
Glenn Kasten6637baa2012-01-09 09:40:36 -0800858 AutoMutex lock(mLock);
859 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860}
861
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800862status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700864 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
865 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800866
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867 // check calling permissions
868 if (!settingsAllowed()) {
869 return PERMISSION_DENIED;
870 }
871
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 // ioHandle == 0 means the parameters are global to the audio hardware interface
873 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700874 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700875 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800876 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700877 AutoMutex lock(mHardwareLock);
878 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
879 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
880 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
881 status_t result = dev->set_parameters(dev, keyValuePairs.string());
882 final_result = result ?: final_result;
883 }
884 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800885 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700886 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
887 AudioParameter param = AudioParameter(keyValuePairs);
888 String8 value;
889 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700890 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
891 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700892 for (size_t i = 0; i < mRecordThreads.size(); i++) {
893 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700894 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700895 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
896 // collect all of the thread's session IDs
897 KeyedVector<int, bool> ids = thread->sessionIds();
898 // suspend effects associated with those session IDs
899 for (size_t j = 0; j < ids.size(); ++j) {
900 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700901 thread->setEffectSuspended(FX_IID_AEC,
902 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700903 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700904 thread->setEffectSuspended(FX_IID_NS,
905 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700906 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700907 }
908 }
Eric Laurentbee53372011-08-29 12:42:48 -0700909 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700910 }
911 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700912 String8 screenState;
913 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
914 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800915 if (isOff != (AudioFlinger::mScreenState & 1)) {
916 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700917 }
918 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700919 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920 }
921
922 // hold a strong ref on thread in case closeOutput() or closeInput() is called
923 // and the thread is exited once the lock is released
924 sp<ThreadBase> thread;
925 {
926 Mutex::Autolock _l(mLock);
927 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700928 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700929 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800930 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700931 // indicate output device change to all input threads for pre processing
932 AudioParameter param = AudioParameter(keyValuePairs);
933 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700934 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
935 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700936 for (size_t i = 0; i < mRecordThreads.size(); i++) {
937 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
938 }
939 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 }
941 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800942 if (thread != 0) {
943 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 }
945 return BAD_VALUE;
946}
947
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800948String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700950 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
951 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952
Eric Laurenta4c5a552012-03-29 10:12:40 -0700953 Mutex::Autolock _l(mLock);
954
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700956 String8 out_s8;
957
Dima Zavin799a70e2011-04-18 16:57:27 -0700958 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800959 char *s;
960 {
961 AutoMutex lock(mHardwareLock);
962 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700963 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800964 s = dev->get_parameters(dev, keys.string());
965 mHardwareStatus = AUDIO_HW_IDLE;
966 }
John Grossmanef7740b2012-02-09 11:28:36 -0800967 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700968 free(s);
969 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700970 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 }
972
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
974 if (playbackThread != NULL) {
975 return playbackThread->getParameters(keys);
976 }
977 RecordThread *recordThread = checkRecordThread_l(ioHandle);
978 if (recordThread != NULL) {
979 return recordThread->getParameters(keys);
980 }
981 return String8("");
982}
983
Glenn Kastendd8104c2012-07-02 12:42:44 -0700984size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
985 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700986{
Eric Laurenta1884f92011-08-23 08:25:03 -0700987 status_t ret = initCheck();
988 if (ret != NO_ERROR) {
989 return 0;
990 }
991
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800992 AutoMutex lock(mHardwareLock);
993 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000994 struct audio_config config;
995 memset(&config, 0, sizeof(config));
996 config.sample_rate = sampleRate;
997 config.channel_mask = channelMask;
998 config.format = format;
999
John Grossmanee578c02012-07-23 17:05:46 -07001000 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1001 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001002 mHardwareStatus = AUDIO_HW_IDLE;
1003 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004}
1005
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001006unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001007{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008 Mutex::Autolock _l(mLock);
1009
1010 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1011 if (recordThread != NULL) {
1012 return recordThread->getInputFramesLost();
1013 }
1014 return 0;
1015}
1016
1017status_t AudioFlinger::setVoiceVolume(float value)
1018{
Eric Laurenta1884f92011-08-23 08:25:03 -07001019 status_t ret = initCheck();
1020 if (ret != NO_ERROR) {
1021 return ret;
1022 }
1023
Mathias Agopian65ab4712010-07-14 17:59:35 -07001024 // check calling permissions
1025 if (!settingsAllowed()) {
1026 return PERMISSION_DENIED;
1027 }
1028
1029 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001030 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001031 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001032 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001033 mHardwareStatus = AUDIO_HW_IDLE;
1034
1035 return ret;
1036}
1037
Glenn Kasten26c77552012-11-16 12:01:44 -08001038status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001039 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040{
1041 status_t status;
1042
1043 Mutex::Autolock _l(mLock);
1044
1045 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1046 if (playbackThread != NULL) {
1047 return playbackThread->getRenderPosition(halFrames, dspFrames);
1048 }
1049
1050 return BAD_VALUE;
1051}
1052
1053void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1054{
1055
1056 Mutex::Autolock _l(mLock);
1057
Glenn Kastenbb001922012-02-03 11:10:26 -08001058 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001059 if (mNotificationClients.indexOfKey(pid) < 0) {
1060 sp<NotificationClient> notificationClient = new NotificationClient(this,
1061 client,
1062 pid);
Steve Block3856b092011-10-20 11:56:00 +01001063 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064
1065 mNotificationClients.add(pid, notificationClient);
1066
1067 sp<IBinder> binder = client->asBinder();
1068 binder->linkToDeath(notificationClient);
1069
1070 // the config change is always sent from playback or record threads to avoid deadlock
1071 // with AudioSystem::gLock
1072 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001073 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074 }
1075
1076 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001077 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001078 }
1079 }
1080}
1081
1082void AudioFlinger::removeNotificationClient(pid_t pid)
1083{
1084 Mutex::Autolock _l(mLock);
1085
Glenn Kastena3b09252012-01-20 09:19:01 -08001086 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001087
Steve Block3856b092011-10-20 11:56:00 +01001088 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001089 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001090 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001091 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001092 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001093 ALOGV(" pid %d @ %d", ref->mPid, i);
1094 if (ref->mPid == pid) {
1095 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001096 mAudioSessionRefs.removeAt(i);
1097 delete ref;
1098 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001099 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001100 } else {
1101 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001102 }
1103 }
1104 if (removed) {
1105 purgeStaleEffects_l();
1106 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001107}
1108
1109// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001110void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001111{
1112 size_t size = mNotificationClients.size();
1113 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001114 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1115 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001116 }
1117}
1118
1119// removeClient_l() must be called with AudioFlinger::mLock held
1120void AudioFlinger::removeClient_l(pid_t pid)
1121{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001122 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001123 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001124 mClients.removeItem(pid);
1125}
1126
Eric Laurent717e1282012-06-29 16:36:52 -07001127// getEffectThread_l() must be called with AudioFlinger::mLock held
1128sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1129{
1130 sp<PlaybackThread> thread;
1131
1132 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1133 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1134 ALOG_ASSERT(thread == 0);
1135 thread = mPlaybackThreads.valueAt(i);
1136 }
1137 }
1138
1139 return thread;
1140}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143
1144// ----------------------------------------------------------------------------
1145
1146AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1147 : RefBase(),
1148 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001149 // 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 -07001150 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001151 mPid(pid),
1152 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001153{
1154 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1155}
1156
1157// Client destructor must be called with AudioFlinger::mLock held
1158AudioFlinger::Client::~Client()
1159{
1160 mAudioFlinger->removeClient_l(mPid);
1161}
1162
Glenn Kasten435dbe62012-01-30 10:15:48 -08001163sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001164{
1165 return mMemoryDealer;
1166}
1167
John Grossman4ff14ba2012-02-08 16:37:41 -08001168// Reserve one of the limited slots for a timed audio track associated
1169// with this client
1170bool AudioFlinger::Client::reserveTimedTrack()
1171{
1172 const int kMaxTimedTracksPerClient = 4;
1173
1174 Mutex::Autolock _l(mTimedTrackLock);
1175
1176 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1177 ALOGW("can not create timed track - pid %d has exceeded the limit",
1178 mPid);
1179 return false;
1180 }
1181
1182 mTimedTrackCount++;
1183 return true;
1184}
1185
1186// Release a slot for a timed audio track
1187void AudioFlinger::Client::releaseTimedTrack()
1188{
1189 Mutex::Autolock _l(mTimedTrackLock);
1190 mTimedTrackCount--;
1191}
1192
Mathias Agopian65ab4712010-07-14 17:59:35 -07001193// ----------------------------------------------------------------------------
1194
1195AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1196 const sp<IAudioFlingerClient>& client,
1197 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001198 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199{
1200}
1201
1202AudioFlinger::NotificationClient::~NotificationClient()
1203{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204}
1205
1206void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1207{
1208 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001209 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001210}
1211
Mathias Agopian65ab4712010-07-14 17:59:35 -07001212
1213// ----------------------------------------------------------------------------
1214
Jeff Brown893a5642013-08-16 20:19:26 -07001215static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1216 return audio_is_remote_submix_device(inDevice);
1217}
1218
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001220 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001221 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001222 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001223 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001224 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001225 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001226 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001227 int *sessionId,
1228 status_t *status)
1229{
1230 sp<RecordThread::RecordTrack> recordTrack;
1231 sp<RecordHandle> recordHandle;
1232 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001233 status_t lStatus;
1234 RecordThread *thread;
1235 size_t inFrameCount;
1236 int lSessionId;
1237
1238 // check calling permissions
1239 if (!recordingAllowed()) {
1240 lStatus = PERMISSION_DENIED;
1241 goto Exit;
1242 }
1243
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001244 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1245 ALOGE("openRecord() invalid format %d", format);
1246 lStatus = BAD_VALUE;
1247 goto Exit;
1248 }
1249
Mathias Agopian65ab4712010-07-14 17:59:35 -07001250 // add client to list
1251 { // scope for mLock
1252 Mutex::Autolock _l(mLock);
1253 thread = checkRecordThread_l(input);
1254 if (thread == NULL) {
1255 lStatus = BAD_VALUE;
1256 goto Exit;
1257 }
1258
Jeff Brown893a5642013-08-16 20:19:26 -07001259 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1260 && !captureAudioOutputAllowed()) {
1261 lStatus = PERMISSION_DENIED;
1262 goto Exit;
1263 }
1264
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001265 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001266 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001267
1268 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001269 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001270 lSessionId = *sessionId;
1271 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001272 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001273 if (sessionId != NULL) {
1274 *sessionId = lSessionId;
1275 }
1276 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001277 // create new record track.
1278 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001279 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kastenddb0ccf2013-07-31 16:14:50 -07001280 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001281 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001282 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001283 // remove local strong reference to Client before deleting the RecordTrack so that the
1284 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001285 client.clear();
1286 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001287 goto Exit;
1288 }
1289
1290 // return to handle to client
1291 recordHandle = new RecordHandle(recordTrack);
1292 lStatus = NO_ERROR;
1293
1294Exit:
1295 if (status) {
1296 *status = lStatus;
1297 }
1298 return recordHandle;
1299}
1300
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001301
1302
Mathias Agopian65ab4712010-07-14 17:59:35 -07001303// ----------------------------------------------------------------------------
1304
Eric Laurenta4c5a552012-03-29 10:12:40 -07001305audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1306{
1307 if (!settingsAllowed()) {
1308 return 0;
1309 }
1310 Mutex::Autolock _l(mLock);
1311 return loadHwModule_l(name);
1312}
1313
1314// loadHwModule_l() must be called with AudioFlinger::mLock held
1315audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1316{
1317 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1318 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1319 ALOGW("loadHwModule() module %s already loaded", name);
1320 return mAudioHwDevs.keyAt(i);
1321 }
1322 }
1323
Eric Laurenta4c5a552012-03-29 10:12:40 -07001324 audio_hw_device_t *dev;
1325
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001326 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001327 if (rc) {
1328 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1329 return 0;
1330 }
1331
1332 mHardwareStatus = AUDIO_HW_INIT;
1333 rc = dev->init_check(dev);
1334 mHardwareStatus = AUDIO_HW_IDLE;
1335 if (rc) {
1336 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1337 return 0;
1338 }
1339
John Grossmanee578c02012-07-23 17:05:46 -07001340 // Check and cache this HAL's level of support for master mute and master
1341 // volume. If this is the first HAL opened, and it supports the get
1342 // methods, use the initial values provided by the HAL as the current
1343 // master mute and volume settings.
1344
1345 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1346 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001347 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001348
1349 if (0 == mAudioHwDevs.size()) {
1350 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1351 if (NULL != dev->get_master_volume) {
1352 float mv;
1353 if (OK == dev->get_master_volume(dev, &mv)) {
1354 mMasterVolume = mv;
1355 }
1356 }
1357
1358 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1359 if (NULL != dev->get_master_mute) {
1360 bool mm;
1361 if (OK == dev->get_master_mute(dev, &mm)) {
1362 mMasterMute = mm;
1363 }
1364 }
1365 }
1366
Eric Laurenta4c5a552012-03-29 10:12:40 -07001367 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001368 if ((NULL != dev->set_master_volume) &&
1369 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1370 flags = static_cast<AudioHwDevice::Flags>(flags |
1371 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1372 }
1373
1374 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1375 if ((NULL != dev->set_master_mute) &&
1376 (OK == dev->set_master_mute(dev, mMasterMute))) {
1377 flags = static_cast<AudioHwDevice::Flags>(flags |
1378 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1379 }
1380
Eric Laurenta4c5a552012-03-29 10:12:40 -07001381 mHardwareStatus = AUDIO_HW_IDLE;
1382 }
1383
1384 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001385 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001386
1387 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001388 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001389
1390 return handle;
1391
1392}
1393
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001394// ----------------------------------------------------------------------------
1395
Glenn Kasten3b16c762012-11-14 08:44:39 -08001396uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001397{
1398 Mutex::Autolock _l(mLock);
1399 PlaybackThread *thread = primaryPlaybackThread_l();
1400 return thread != NULL ? thread->sampleRate() : 0;
1401}
1402
Glenn Kastene33054e2012-11-14 12:54:39 -08001403size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001404{
1405 Mutex::Autolock _l(mLock);
1406 PlaybackThread *thread = primaryPlaybackThread_l();
1407 return thread != NULL ? thread->frameCountHAL() : 0;
1408}
1409
1410// ----------------------------------------------------------------------------
1411
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001412status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1413{
1414 uid_t uid = IPCThreadState::self()->getCallingUid();
1415 if (uid != AID_SYSTEM) {
1416 return PERMISSION_DENIED;
1417 }
1418 Mutex::Autolock _l(mLock);
1419 if (mIsDeviceTypeKnown) {
1420 return INVALID_OPERATION;
1421 }
1422 mIsLowRamDevice = isLowRamDevice;
1423 mIsDeviceTypeKnown = true;
1424 return NO_ERROR;
1425}
1426
1427// ----------------------------------------------------------------------------
1428
Eric Laurenta4c5a552012-03-29 10:12:40 -07001429audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1430 audio_devices_t *pDevices,
1431 uint32_t *pSamplingRate,
1432 audio_format_t *pFormat,
1433 audio_channel_mask_t *pChannelMask,
1434 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001435 audio_output_flags_t flags,
1436 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001438 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001439 struct audio_config config;
1440 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1441 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1442 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1443 if (offloadInfo) {
1444 config.offload_info = *offloadInfo;
1445 }
1446
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001447 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001448 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449
Eric Laurentbfb1b832013-01-07 09:53:42 -08001450 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001451 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001452 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001453 config.sample_rate,
1454 config.format,
1455 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001456 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001457 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1458 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001459
1460 if (pDevices == NULL || *pDevices == 0) {
1461 return 0;
1462 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001463
Mathias Agopian65ab4712010-07-14 17:59:35 -07001464 Mutex::Autolock _l(mLock);
1465
Eric Laurenta4c5a552012-03-29 10:12:40 -07001466 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001467 if (outHwDev == NULL)
1468 return 0;
1469
John Grossmanee578c02012-07-23 17:05:46 -07001470 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001471 audio_io_handle_t id = nextUniqueId();
1472
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001473 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001474
Glenn Kasten34542ac2013-06-26 11:29:02 -07001475 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001476 id,
1477 *pDevices,
1478 (audio_output_flags_t)flags,
1479 &config,
1480 &outStream);
1481
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001482 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001483 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001484 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001485 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001486 config.sample_rate,
1487 config.format,
1488 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001489 status);
1490
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001491 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001492 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001493
Eric Laurentbfb1b832013-01-07 09:53:42 -08001494 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1495 thread = new OffloadThread(this, output, id, *pDevices);
1496 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1497 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001498 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1499 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001500 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001501 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001502 } else {
1503 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001504 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001505 }
1506 mPlaybackThreads.add(id, thread);
1507
Glenn Kasten7c027242012-12-26 14:43:16 -08001508 if (pSamplingRate != NULL) {
1509 *pSamplingRate = config.sample_rate;
1510 }
1511 if (pFormat != NULL) {
1512 *pFormat = config.format;
1513 }
1514 if (pChannelMask != NULL) {
1515 *pChannelMask = config.channel_mask;
1516 }
1517 if (pLatencyMs != NULL) {
1518 *pLatencyMs = thread->latency();
1519 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520
1521 // notify client processes of the new output creation
1522 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001523
1524 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001525 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001526 ALOGI("Using module %d has the primary audio interface", module);
1527 mPrimaryHardwareDev = outHwDev;
1528
1529 AutoMutex lock(mHardwareLock);
1530 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001531 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001532 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001533 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001534 return id;
1535 }
1536
1537 return 0;
1538}
1539
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001540audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1541 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542{
1543 Mutex::Autolock _l(mLock);
1544 MixerThread *thread1 = checkMixerThread_l(output1);
1545 MixerThread *thread2 = checkMixerThread_l(output2);
1546
1547 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001548 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1549 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001550 return 0;
1551 }
1552
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001553 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001554 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1555 thread->addOutputTrack(thread2);
1556 mPlaybackThreads.add(id, thread);
1557 // notify client processes of the new output creation
1558 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1559 return id;
1560}
1561
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001562status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563{
Glenn Kastend96c5722012-04-25 13:44:49 -07001564 return closeOutput_nonvirtual(output);
1565}
1566
1567status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1568{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569 // keep strong reference on the playback thread so that
1570 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001571 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001572 {
1573 Mutex::Autolock _l(mLock);
1574 thread = checkPlaybackThread_l(output);
1575 if (thread == NULL) {
1576 return BAD_VALUE;
1577 }
1578
Steve Block3856b092011-10-20 11:56:00 +01001579 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001580
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001581 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001582 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001583 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001584 DuplicatingThread *dupThread =
1585 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001586 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001587
1588 }
1589 }
1590 }
1591
1592
1593 mPlaybackThreads.removeItem(output);
1594 // save all effects to the default thread
1595 if (mPlaybackThreads.size()) {
1596 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1597 if (dstThread != NULL) {
1598 // audioflinger lock is held here so the acquisition order of thread locks does not
1599 // matter
1600 Mutex::Autolock _dl(dstThread->mLock);
1601 Mutex::Autolock _sl(thread->mLock);
1602 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1603 for (size_t i = 0; i < effectChains.size(); i ++) {
1604 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001605 }
1606 }
1607 }
Glenn Kastena1117922012-01-26 10:53:32 -08001608 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609 }
1610 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001611 // The thread entity (active unit of execution) is no longer running here,
1612 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001613
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001614 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001615 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001616 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001617 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001618 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001619 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001620 }
1621 return NO_ERROR;
1622}
1623
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001624status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001625{
1626 Mutex::Autolock _l(mLock);
1627 PlaybackThread *thread = checkPlaybackThread_l(output);
1628
1629 if (thread == NULL) {
1630 return BAD_VALUE;
1631 }
1632
Steve Block3856b092011-10-20 11:56:00 +01001633 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001634 thread->suspend();
1635
1636 return NO_ERROR;
1637}
1638
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001639status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001640{
1641 Mutex::Autolock _l(mLock);
1642 PlaybackThread *thread = checkPlaybackThread_l(output);
1643
1644 if (thread == NULL) {
1645 return BAD_VALUE;
1646 }
1647
Steve Block3856b092011-10-20 11:56:00 +01001648 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001649
1650 thread->restore();
1651
1652 return NO_ERROR;
1653}
1654
Eric Laurenta4c5a552012-03-29 10:12:40 -07001655audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1656 audio_devices_t *pDevices,
1657 uint32_t *pSamplingRate,
1658 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001659 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001660{
1661 status_t status;
1662 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001663 struct audio_config config;
1664 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1665 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1666 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1667
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001668 uint32_t reqSamplingRate = config.sample_rate;
1669 audio_format_t reqFormat = config.format;
1670 audio_channel_mask_t reqChannels = config.channel_mask;
1671 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001672 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001673
1674 if (pDevices == NULL || *pDevices == 0) {
1675 return 0;
1676 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001677
Mathias Agopian65ab4712010-07-14 17:59:35 -07001678 Mutex::Autolock _l(mLock);
1679
Eric Laurenta4c5a552012-03-29 10:12:40 -07001680 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001681 if (inHwDev == NULL)
1682 return 0;
1683
John Grossmanee578c02012-07-23 17:05:46 -07001684 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001685 audio_io_handle_t id = nextUniqueId();
1686
John Grossmanee578c02012-07-23 17:05:46 -07001687 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001688 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001689 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1690 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001691 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001692 config.sample_rate,
1693 config.format,
1694 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001695 status);
1696
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001697 // If the input could not be opened with the requested parameters and we can handle the
1698 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1699 // resample the input and do mono to stereo or stereo to mono conversions on 16 bit PCM inputs.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001700 if (status == BAD_VALUE &&
1701 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1702 (config.sample_rate <= 2 * reqSamplingRate) &&
1703 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001704 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001705 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001706 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001707 }
1708
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001709 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001710
Glenn Kasten46909e72013-02-26 09:20:22 -08001711#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001712 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1713 // or (re-)create if current Pipe is idle and does not match the new format
1714 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001715 enum {
1716 TEE_SINK_NO, // don't copy input
1717 TEE_SINK_NEW, // copy input using a new pipe
1718 TEE_SINK_OLD, // copy input using an existing pipe
1719 } kind;
1720 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1721 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001722 if (!mTeeSinkInputEnabled) {
1723 kind = TEE_SINK_NO;
1724 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001725 kind = TEE_SINK_NO;
1726 } else if (mRecordTeeSink == 0) {
1727 kind = TEE_SINK_NEW;
1728 } else if (mRecordTeeSink->getStrongCount() != 1) {
1729 kind = TEE_SINK_NO;
1730 } else if (format == mRecordTeeSink->format()) {
1731 kind = TEE_SINK_OLD;
1732 } else {
1733 kind = TEE_SINK_NEW;
1734 }
1735 switch (kind) {
1736 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001737 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001738 size_t numCounterOffers = 0;
1739 const NBAIO_Format offers[1] = {format};
1740 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1741 ALOG_ASSERT(index == 0);
1742 PipeReader *pipeReader = new PipeReader(*pipe);
1743 numCounterOffers = 0;
1744 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1745 ALOG_ASSERT(index == 0);
1746 mRecordTeeSink = pipe;
1747 mRecordTeeSource = pipeReader;
1748 teeSink = pipe;
1749 }
1750 break;
1751 case TEE_SINK_OLD:
1752 teeSink = mRecordTeeSink;
1753 break;
1754 case TEE_SINK_NO:
1755 default:
1756 break;
1757 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001758#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001759
Dima Zavin799a70e2011-04-18 16:57:27 -07001760 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1761
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001762 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001763 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001764 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001765 thread = new RecordThread(this,
1766 input,
1767 reqSamplingRate,
1768 reqChannels,
1769 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001770 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001771 *pDevices
1772#ifdef TEE_SINK
1773 , teeSink
1774#endif
1775 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001776 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001777 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001778 if (pSamplingRate != NULL) {
1779 *pSamplingRate = reqSamplingRate;
1780 }
1781 if (pFormat != NULL) {
1782 *pFormat = config.format;
1783 }
1784 if (pChannelMask != NULL) {
1785 *pChannelMask = reqChannels;
1786 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001787
Mathias Agopian65ab4712010-07-14 17:59:35 -07001788 // notify client processes of the new input creation
1789 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1790 return id;
1791 }
1792
1793 return 0;
1794}
1795
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001796status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001797{
Glenn Kastend96c5722012-04-25 13:44:49 -07001798 return closeInput_nonvirtual(input);
1799}
1800
1801status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1802{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001803 // keep strong reference on the record thread so that
1804 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001805 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001806 {
1807 Mutex::Autolock _l(mLock);
1808 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001809 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001810 return BAD_VALUE;
1811 }
1812
Steve Block3856b092011-10-20 11:56:00 +01001813 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001814 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001815 mRecordThreads.removeItem(input);
1816 }
1817 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001818 // The thread entity (active unit of execution) is no longer running here,
1819 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001820
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001821 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001822 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001823 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001824 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001825 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001826
1827 return NO_ERROR;
1828}
1829
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001830status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001831{
1832 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001833 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001834
1835 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1836 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001837 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001838 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001839
1840 return NO_ERROR;
1841}
1842
1843
1844int AudioFlinger::newAudioSessionId()
1845{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001846 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001847}
1848
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001849void AudioFlinger::acquireAudioSessionId(int audioSession)
1850{
1851 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001852 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001853 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001854 size_t num = mAudioSessionRefs.size();
1855 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001856 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001857 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1858 ref->mCnt++;
1859 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001860 return;
1861 }
1862 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001863 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1864 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001865}
1866
1867void AudioFlinger::releaseAudioSessionId(int audioSession)
1868{
1869 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001870 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001871 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001872 size_t num = mAudioSessionRefs.size();
1873 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001874 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001875 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1876 ref->mCnt--;
1877 ALOGV(" decremented refcount to %d", ref->mCnt);
1878 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001879 mAudioSessionRefs.removeAt(i);
1880 delete ref;
1881 purgeStaleEffects_l();
1882 }
1883 return;
1884 }
1885 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001886 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001887}
1888
1889void AudioFlinger::purgeStaleEffects_l() {
1890
Steve Block3856b092011-10-20 11:56:00 +01001891 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001892
1893 Vector< sp<EffectChain> > chains;
1894
1895 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1896 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1897 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1898 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001899 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1900 chains.push(ec);
1901 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001902 }
1903 }
1904 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1905 sp<RecordThread> t = mRecordThreads.valueAt(i);
1906 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1907 sp<EffectChain> ec = t->mEffectChains[j];
1908 chains.push(ec);
1909 }
1910 }
1911
1912 for (size_t i = 0; i < chains.size(); i++) {
1913 sp<EffectChain> ec = chains[i];
1914 int sessionid = ec->sessionId();
1915 sp<ThreadBase> t = ec->mThread.promote();
1916 if (t == 0) {
1917 continue;
1918 }
1919 size_t numsessionrefs = mAudioSessionRefs.size();
1920 bool found = false;
1921 for (size_t k = 0; k < numsessionrefs; k++) {
1922 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001923 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001924 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001925 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001926 found = true;
1927 break;
1928 }
1929 }
1930 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001931 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001932 // remove all effects from the chain
1933 while (ec->mEffects.size()) {
1934 sp<EffectModule> effect = ec->mEffects[0];
1935 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001936 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001937 if (effect->purgeHandles()) {
1938 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001939 }
1940 AudioSystem::unregisterEffect(effect->id());
1941 }
1942 }
1943 }
1944 return;
1945}
1946
Mathias Agopian65ab4712010-07-14 17:59:35 -07001947// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001948AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001949{
Glenn Kastena1117922012-01-26 10:53:32 -08001950 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001951}
1952
1953// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001954AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955{
1956 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001957 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001958}
1959
1960// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001961AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001962{
Glenn Kastena1117922012-01-26 10:53:32 -08001963 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001964}
1965
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001966uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001967{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001968 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001969}
1970
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001971AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001972{
1973 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1974 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001975 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001976 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001977 return thread;
1978 }
1979 }
1980 return NULL;
1981}
1982
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001983audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001984{
1985 PlaybackThread *thread = primaryPlaybackThread_l();
1986
1987 if (thread == NULL) {
1988 return 0;
1989 }
1990
Eric Laurentf1c04f92012-08-28 14:26:53 -07001991 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001992}
1993
Eric Laurenta011e352012-03-29 15:51:43 -07001994sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1995 int triggerSession,
1996 int listenerSession,
1997 sync_event_callback_t callBack,
1998 void *cookie)
1999{
2000 Mutex::Autolock _l(mLock);
2001
2002 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2003 status_t playStatus = NAME_NOT_FOUND;
2004 status_t recStatus = NAME_NOT_FOUND;
2005 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2006 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2007 if (playStatus == NO_ERROR) {
2008 return event;
2009 }
2010 }
2011 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2012 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2013 if (recStatus == NO_ERROR) {
2014 return event;
2015 }
2016 }
2017 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2018 mPendingSyncEvents.add(event);
2019 } else {
2020 ALOGV("createSyncEvent() invalid event %d", event->type());
2021 event.clear();
2022 }
2023 return event;
2024}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002025
Mathias Agopian65ab4712010-07-14 17:59:35 -07002026// ----------------------------------------------------------------------------
2027// Effect management
2028// ----------------------------------------------------------------------------
2029
2030
Glenn Kastenf587ba52012-01-26 16:25:10 -08002031status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002032{
2033 Mutex::Autolock _l(mLock);
2034 return EffectQueryNumberEffects(numEffects);
2035}
2036
Glenn Kastenf587ba52012-01-26 16:25:10 -08002037status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002038{
2039 Mutex::Autolock _l(mLock);
2040 return EffectQueryEffect(index, descriptor);
2041}
2042
Glenn Kasten5e92a782012-01-30 07:40:52 -08002043status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002044 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002045{
2046 Mutex::Autolock _l(mLock);
2047 return EffectGetDescriptor(pUuid, descriptor);
2048}
2049
2050
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002051sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002052 effect_descriptor_t *pDesc,
2053 const sp<IEffectClient>& effectClient,
2054 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002055 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002056 int sessionId,
2057 status_t *status,
2058 int *id,
2059 int *enabled)
2060{
2061 status_t lStatus = NO_ERROR;
2062 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002063 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002064
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002065 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002066 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002067 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002068
2069 if (pDesc == NULL) {
2070 lStatus = BAD_VALUE;
2071 goto Exit;
2072 }
2073
Eric Laurent84e9a102010-09-23 16:10:16 -07002074 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002075 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002076 lStatus = PERMISSION_DENIED;
2077 goto Exit;
2078 }
2079
Dima Zavinfce7a472011-04-19 22:30:36 -07002080 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002081 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002082 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002083 lStatus = PERMISSION_DENIED;
2084 goto Exit;
2085 }
2086
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002087 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002088 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002089 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07002090 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07002091 lStatus = BAD_VALUE;
2092 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07002093 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002094 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002095 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002096 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002097 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002098 }
2099 }
2100
Mathias Agopian65ab4712010-07-14 17:59:35 -07002101 {
2102 Mutex::Autolock _l(mLock);
2103
Mathias Agopian65ab4712010-07-14 17:59:35 -07002104
2105 if (!EffectIsNullUuid(&pDesc->uuid)) {
2106 // if uuid is specified, request effect descriptor
2107 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2108 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002109 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002110 goto Exit;
2111 }
2112 } else {
2113 // if uuid is not specified, look for an available implementation
2114 // of the required type in effect factory
2115 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002116 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002117 lStatus = BAD_VALUE;
2118 goto Exit;
2119 }
2120 uint32_t numEffects = 0;
2121 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002122 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002123 bool found = false;
2124
2125 lStatus = EffectQueryNumberEffects(&numEffects);
2126 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002127 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002128 goto Exit;
2129 }
2130 for (uint32_t i = 0; i < numEffects; i++) {
2131 lStatus = EffectQueryEffect(i, &desc);
2132 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002133 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002134 continue;
2135 }
2136 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2137 // If matching type found save effect descriptor. If the session is
2138 // 0 and the effect is not auxiliary, continue enumeration in case
2139 // an auxiliary version of this effect type is available
2140 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002141 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002142 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002143 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2144 break;
2145 }
2146 }
2147 }
2148 if (!found) {
2149 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002150 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002151 goto Exit;
2152 }
2153 // For same effect type, chose auxiliary version over insert version if
2154 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002155 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002156 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002157 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002158 }
2159 }
2160
2161 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002162 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002163 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2164 lStatus = INVALID_OPERATION;
2165 goto Exit;
2166 }
2167
Eric Laurent59255e42011-07-27 19:49:51 -07002168 // check recording permission for visualizer
2169 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2170 !recordingAllowed()) {
2171 lStatus = PERMISSION_DENIED;
2172 goto Exit;
2173 }
2174
Mathias Agopian65ab4712010-07-14 17:59:35 -07002175 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002176 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002177
2178 // If output is not specified try to find a matching audio session ID in one of the
2179 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002180 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2181 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002182 // Note: io is never 0 when creating an effect on an input
2183 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002184 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002185 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2186 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002187 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002188 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002189 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002190 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002191 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002192 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2193 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2194 io = mRecordThreads.keyAt(i);
2195 break;
2196 }
2197 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002198 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002199 // If no output thread contains the requested session ID, default to
2200 // first output. The effect chain will be moved to the correct output
2201 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002202 if (io == 0 && mPlaybackThreads.size()) {
2203 io = mPlaybackThreads.keyAt(0);
2204 }
Steve Block3856b092011-10-20 11:56:00 +01002205 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002206 }
2207 ThreadBase *thread = checkRecordThread_l(io);
2208 if (thread == NULL) {
2209 thread = checkPlaybackThread_l(io);
2210 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002211 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002212 lStatus = BAD_VALUE;
2213 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002214 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002215 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002216
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002217 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002219 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002220 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2221 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002222 if (handle != 0 && id != NULL) {
2223 *id = handle->id();
2224 }
2225 }
2226
2227Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002228 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002229 *status = lStatus;
2230 }
2231 return handle;
2232}
2233
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002234status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2235 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002236{
Steve Block3856b092011-10-20 11:56:00 +01002237 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002238 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002239 Mutex::Autolock _l(mLock);
2240 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002241 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002242 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002243 }
Eric Laurentde070132010-07-13 04:45:46 -07002244 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2245 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002246 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002247 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002248 }
Eric Laurentde070132010-07-13 04:45:46 -07002249 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2250 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002251 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002252 return BAD_VALUE;
2253 }
2254
2255 Mutex::Autolock _dl(dstThread->mLock);
2256 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002257 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002258
Mathias Agopian65ab4712010-07-14 17:59:35 -07002259 return NO_ERROR;
2260}
2261
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002262// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002263status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002264 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002265 AudioFlinger::PlaybackThread *dstThread,
2266 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002267{
Steve Block3856b092011-10-20 11:56:00 +01002268 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002269 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002270
Eric Laurent59255e42011-07-27 19:49:51 -07002271 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002272 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002273 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002274 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002275 return INVALID_OPERATION;
2276 }
2277
Eric Laurent39e94f82010-07-28 01:32:47 -07002278 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002279 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002280 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002281 // removed.
2282 srcThread->removeEffectChain_l(chain);
2283
2284 // transfer all effects one by one so that new effect chain is created on new thread with
2285 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002286 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002287 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002288 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002289 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2290 while (effect != 0) {
2291 srcThread->removeEffect_l(effect);
2292 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002293 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2294 if (effect->state() == EffectModule::ACTIVE ||
2295 effect->state() == EffectModule::STOPPING) {
2296 effect->start();
2297 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002298 // if the move request is not received from audio policy manager, the effect must be
2299 // re-registered with the new strategy and output
2300 if (dstChain == 0) {
2301 dstChain = effect->chain().promote();
2302 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002303 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002304 srcThread->addEffect_l(effect);
2305 return NO_INIT;
2306 }
2307 strategy = dstChain->strategy();
2308 }
2309 if (reRegister) {
2310 AudioSystem::unregisterEffect(effect->id());
2311 AudioSystem::registerEffect(&effect->desc(),
2312 dstOutput,
2313 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002314 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002315 effect->id());
2316 }
Eric Laurentde070132010-07-13 04:45:46 -07002317 effect = chain->getEffectFromId_l(0);
2318 }
2319
2320 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002321}
2322
Eric Laurent813e2a72013-08-31 12:59:48 -07002323bool AudioFlinger::isGlobalEffectEnabled_l()
2324{
2325 if (mGlobalEffectEnableTime != 0 &&
2326 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2327 return true;
2328 }
2329
2330 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2331 sp<EffectChain> ec =
2332 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
2333 if (ec != 0 && ec->isEnabled()) {
2334 return true;
2335 }
2336 }
2337 return false;
2338}
2339
2340void AudioFlinger::onGlobalEffectEnable()
2341{
2342 Mutex::Autolock _l(mLock);
2343
2344 mGlobalEffectEnableTime = systemTime();
2345
2346 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2347 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2348 if (t->mType == ThreadBase::OFFLOAD) {
2349 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2350 }
2351 }
2352
2353}
2354
Glenn Kastenda6ef132013-01-10 12:31:01 -08002355struct Entry {
2356#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2357 char mName[MAX_NAME];
2358};
2359
2360int comparEntry(const void *p1, const void *p2)
2361{
2362 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2363}
2364
Glenn Kasten46909e72013-02-26 09:20:22 -08002365#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002366void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002367{
Eric Laurent81784c32012-11-19 14:55:58 -08002368 NBAIO_Source *teeSource = source.get();
2369 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002370 // .wav rotation
2371 // There is a benign race condition if 2 threads call this simultaneously.
2372 // They would both traverse the directory, but the result would simply be
2373 // failures at unlink() which are ignored. It's also unlikely since
2374 // normally dumpsys is only done by bugreport or from the command line.
2375 char teePath[32+256];
2376 strcpy(teePath, "/data/misc/media");
2377 size_t teePathLen = strlen(teePath);
2378 DIR *dir = opendir(teePath);
2379 teePath[teePathLen++] = '/';
2380 if (dir != NULL) {
2381#define MAX_SORT 20 // number of entries to sort
2382#define MAX_KEEP 10 // number of entries to keep
2383 struct Entry entries[MAX_SORT];
2384 size_t entryCount = 0;
2385 while (entryCount < MAX_SORT) {
2386 struct dirent de;
2387 struct dirent *result = NULL;
2388 int rc = readdir_r(dir, &de, &result);
2389 if (rc != 0) {
2390 ALOGW("readdir_r failed %d", rc);
2391 break;
2392 }
2393 if (result == NULL) {
2394 break;
2395 }
2396 if (result != &de) {
2397 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2398 break;
2399 }
2400 // ignore non .wav file entries
2401 size_t nameLen = strlen(de.d_name);
2402 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2403 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2404 continue;
2405 }
2406 strcpy(entries[entryCount++].mName, de.d_name);
2407 }
2408 (void) closedir(dir);
2409 if (entryCount > MAX_KEEP) {
2410 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2411 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2412 strcpy(&teePath[teePathLen], entries[i].mName);
2413 (void) unlink(teePath);
2414 }
2415 }
2416 } else {
2417 if (fd >= 0) {
2418 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2419 }
2420 }
Eric Laurent81784c32012-11-19 14:55:58 -08002421 char teeTime[16];
2422 struct timeval tv;
2423 gettimeofday(&tv, NULL);
2424 struct tm tm;
2425 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002426 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2427 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2428 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2429 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002430 if (teeFd >= 0) {
2431 char wavHeader[44];
2432 memcpy(wavHeader,
2433 "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",
2434 sizeof(wavHeader));
2435 NBAIO_Format format = teeSource->format();
2436 unsigned channelCount = Format_channelCount(format);
2437 ALOG_ASSERT(channelCount <= FCC_2);
2438 uint32_t sampleRate = Format_sampleRate(format);
2439 wavHeader[22] = channelCount; // number of channels
2440 wavHeader[24] = sampleRate; // sample rate
2441 wavHeader[25] = sampleRate >> 8;
2442 wavHeader[32] = channelCount * 2; // block alignment
2443 write(teeFd, wavHeader, sizeof(wavHeader));
2444 size_t total = 0;
2445 bool firstRead = true;
2446 for (;;) {
2447#define TEE_SINK_READ 1024
2448 short buffer[TEE_SINK_READ * FCC_2];
2449 size_t count = TEE_SINK_READ;
2450 ssize_t actual = teeSource->read(buffer, count,
2451 AudioBufferProvider::kInvalidPTS);
2452 bool wasFirstRead = firstRead;
2453 firstRead = false;
2454 if (actual <= 0) {
2455 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2456 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002457 }
Eric Laurent81784c32012-11-19 14:55:58 -08002458 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002459 }
Eric Laurent81784c32012-11-19 14:55:58 -08002460 ALOG_ASSERT(actual <= (ssize_t)count);
2461 write(teeFd, buffer, actual * channelCount * sizeof(short));
2462 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002463 }
Eric Laurent81784c32012-11-19 14:55:58 -08002464 lseek(teeFd, (off_t) 4, SEEK_SET);
2465 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2466 write(teeFd, &temp, sizeof(temp));
2467 lseek(teeFd, (off_t) 40, SEEK_SET);
2468 temp = total * channelCount * sizeof(short);
2469 write(teeFd, &temp, sizeof(temp));
2470 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002471 if (fd >= 0) {
2472 fdprintf(fd, "tee copied to %s\n", teePath);
2473 }
Eric Laurent59255e42011-07-27 19:49:51 -07002474 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002475 if (fd >= 0) {
2476 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2477 }
Eric Laurent59255e42011-07-27 19:49:51 -07002478 }
2479 }
2480}
Glenn Kasten46909e72013-02-26 09:20:22 -08002481#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002482
Mathias Agopian65ab4712010-07-14 17:59:35 -07002483// ----------------------------------------------------------------------------
2484
2485status_t AudioFlinger::onTransact(
2486 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2487{
2488 return BnAudioFlinger::onTransact(code, data, reply, flags);
2489}
2490
Mathias Agopian65ab4712010-07-14 17:59:35 -07002491}; // namespace android