blob: 8fbac42450450344a8cd334b0a799bf42d1d6a9c [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// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
102// we define a minimum time during which a global effect is considered enabled.
103static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
104
Mathias Agopian65ab4712010-07-14 17:59:35 -0700105// ----------------------------------------------------------------------------
106
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700107static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700108{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700109 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700110 int rc;
111
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700112 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
113 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
114 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
115 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700116 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700117 }
118 rc = audio_hw_device_open(mod, dev);
119 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
120 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
121 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700122 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700123 }
124 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
125 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
126 rc = BAD_VALUE;
127 goto out;
128 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700129 return 0;
130
131out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700132 *dev = NULL;
133 return rc;
134}
135
Mathias Agopian65ab4712010-07-14 17:59:35 -0700136// ----------------------------------------------------------------------------
137
138AudioFlinger::AudioFlinger()
139 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800140 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700141 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800142 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800143 mMasterMute(false),
144 mNextUniqueId(1),
145 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700146 mBtNrecIsOff(false),
147 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700148 mIsDeviceTypeKnown(false),
149 mGlobalEffectEnableTime(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700150{
Glenn Kasten949a9262013-04-16 12:35:20 -0700151 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800152 char value[PROPERTY_VALUE_MAX];
153 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
154 if (doLog) {
155 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
156 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800157#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800158 (void) property_get("ro.debuggable", value, "0");
159 int debuggable = atoi(value);
160 int teeEnabled = 0;
161 if (debuggable) {
162 (void) property_get("af.tee", value, "0");
163 teeEnabled = atoi(value);
164 }
165 if (teeEnabled & 1)
166 mTeeSinkInputEnabled = true;
167 if (teeEnabled & 2)
168 mTeeSinkOutputEnabled = true;
169 if (teeEnabled & 4)
170 mTeeSinkTrackEnabled = true;
Glenn Kasten46909e72013-02-26 09:20:22 -0800171#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700172}
173
174void AudioFlinger::onFirstRef()
175{
Dima Zavin799a70e2011-04-18 16:57:27 -0700176 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700177
Eric Laurent93575202011-01-18 18:39:02 -0800178 Mutex::Autolock _l(mLock);
179
Dima Zavin799a70e2011-04-18 16:57:27 -0700180 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800181 char val_str[PROPERTY_VALUE_MAX] = { 0 };
182 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
183 uint32_t int_val;
184 if (1 == sscanf(val_str, "%u", &int_val)) {
185 mStandbyTimeInNsecs = milliseconds(int_val);
186 ALOGI("Using %u mSec as standby time.", int_val);
187 } else {
188 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
189 ALOGI("Using default %u mSec as standby time.",
190 (uint32_t)(mStandbyTimeInNsecs / 1000000));
191 }
192 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193
Eric Laurenta4c5a552012-03-29 10:12:40 -0700194 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195}
196
197AudioFlinger::~AudioFlinger()
198{
199 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700200 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700201 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 }
203 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700204 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700205 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700206 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700207
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800208 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
209 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700210 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
211 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212 }
213}
214
Eric Laurenta4c5a552012-03-29 10:12:40 -0700215static const char * const audio_interfaces[] = {
216 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
217 AUDIO_HARDWARE_MODULE_ID_A2DP,
218 AUDIO_HARDWARE_MODULE_ID_USB,
219};
220#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
221
John Grossmanee578c02012-07-23 17:05:46 -0700222AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
223 audio_module_handle_t module,
224 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700225{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700226 // if module is 0, the request comes from an old policy manager and we should load
227 // well known modules
228 if (module == 0) {
229 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
230 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
231 loadHwModule_l(audio_interfaces[i]);
232 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700233 // then try to find a module supporting the requested device.
234 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
235 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
236 audio_hw_device_t *dev = audioHwDevice->hwDevice();
237 if ((dev->get_supported_devices != NULL) &&
238 (dev->get_supported_devices(dev) & devices) == devices)
239 return audioHwDevice;
240 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700241 } else {
242 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700243 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
244 if (audioHwDevice != NULL) {
245 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700246 }
247 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700248
Dima Zavin799a70e2011-04-18 16:57:27 -0700249 return NULL;
250}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700252void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253{
254 const size_t SIZE = 256;
255 char buffer[SIZE];
256 String8 result;
257
258 result.append("Clients:\n");
259 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800260 sp<Client> client = mClients.valueAt(i).promote();
261 if (client != 0) {
262 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
263 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264 }
265 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700266
267 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800268 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700269 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
270 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800271 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700272 result.append(buffer);
273 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275}
276
277
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700278void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279{
280 const size_t SIZE = 256;
281 char buffer[SIZE];
282 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800283 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284
John Grossman4ff14ba2012-02-08 16:37:41 -0800285 snprintf(buffer, SIZE, "Hardware status: %d\n"
286 "Standby Time mSec: %u\n",
287 hardwareStatus,
288 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289 result.append(buffer);
290 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291}
292
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700293void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294{
295 const size_t SIZE = 256;
296 char buffer[SIZE];
297 String8 result;
298 snprintf(buffer, SIZE, "Permission Denial: "
299 "can't dump AudioFlinger from pid=%d, uid=%d\n",
300 IPCThreadState::self()->getCallingPid(),
301 IPCThreadState::self()->getCallingUid());
302 result.append(buffer);
303 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304}
305
Eric Laurent81784c32012-11-19 14:55:58 -0800306bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307{
308 bool locked = false;
309 for (int i = 0; i < kDumpLockRetries; ++i) {
310 if (mutex.tryLock() == NO_ERROR) {
311 locked = true;
312 break;
313 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800314 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315 }
316 return locked;
317}
318
319status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
320{
Glenn Kasten44deb052012-02-05 18:09:08 -0800321 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322 dumpPermissionDenial(fd, args);
323 } else {
324 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800325 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326 if (!hardwareLocked) {
327 String8 result(kHardwareLockedString);
328 write(fd, result.string(), result.size());
329 } else {
330 mHardwareLock.unlock();
331 }
332
Eric Laurent81784c32012-11-19 14:55:58 -0800333 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334
335 // failed to lock - AudioFlinger is probably deadlocked
336 if (!locked) {
337 String8 result(kDeadlockedString);
338 write(fd, result.string(), result.size());
339 }
340
341 dumpClients(fd, args);
342 dumpInternals(fd, args);
343
344 // dump playback threads
345 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
346 mPlaybackThreads.valueAt(i)->dump(fd, args);
347 }
348
349 // dump record threads
350 for (size_t i = 0; i < mRecordThreads.size(); i++) {
351 mRecordThreads.valueAt(i)->dump(fd, args);
352 }
353
Dima Zavin799a70e2011-04-18 16:57:27 -0700354 // dump all hardware devs
355 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700356 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700357 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700359
Glenn Kasten46909e72013-02-26 09:20:22 -0800360#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700361 // dump the serially shared record tee sink
362 if (mRecordTeeSource != 0) {
363 dumpTee(fd, mRecordTeeSource);
364 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800365#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700366
Glenn Kastend65d73c2012-06-22 17:21:07 -0700367 if (locked) {
368 mLock.unlock();
369 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800370
371 // append a copy of media.log here by forwarding fd to it, but don't attempt
372 // to lookup the service if it's not running, as it will block for a second
373 if (mLogMemoryDealer != 0) {
374 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
375 if (binder != 0) {
376 fdprintf(fd, "\nmedia.log:\n");
377 Vector<String16> args;
378 binder->dump(fd, args);
379 }
380 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381 }
382 return NO_ERROR;
383}
384
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800385sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
386{
387 // If pid is already in the mClients wp<> map, then use that entry
388 // (for which promote() is always != 0), otherwise create a new entry and Client.
389 sp<Client> client = mClients.valueFor(pid).promote();
390 if (client == 0) {
391 client = new Client(this, pid);
392 mClients.add(pid, client);
393 }
394
395 return client;
396}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397
Glenn Kasten9e58b552013-01-18 15:09:48 -0800398sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
399{
400 if (mLogMemoryDealer == 0) {
401 return new NBLog::Writer();
402 }
403 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
404 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
405 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
406 if (binder != 0) {
407 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
408 }
409 return writer;
410}
411
412void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
413{
Glenn Kasten685ef092013-02-04 08:15:34 -0800414 if (writer == 0) {
415 return;
416 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800417 sp<IMemory> iMemory(writer->getIMemory());
418 if (iMemory == 0) {
419 return;
420 }
421 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
422 if (binder != 0) {
423 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
424 // Now the media.log remote reference to IMemory is gone.
425 // When our last local reference to IMemory also drops to zero,
426 // the IMemory destructor will deallocate the region from mMemoryDealer.
427 }
428}
429
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430// IAudioFlinger interface
431
432
433sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800434 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800436 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700437 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800438 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800439 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800441 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800442 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700444 String8& name,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445 status_t *status)
446{
447 sp<PlaybackThread::Track> track;
448 sp<TrackHandle> trackHandle;
449 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700450 status_t lStatus;
451 int lSessionId;
452
Glenn Kasten263709e2012-01-06 08:40:01 -0800453 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
454 // but if someone uses binder directly they could bypass that and cause us to crash
455 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000456 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700457 lStatus = BAD_VALUE;
458 goto Exit;
459 }
460
Glenn Kasten60a83922012-06-21 12:56:37 -0700461 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
462 // and we don't yet support 8.24 or 32-bit PCM
463 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
464 ALOGE("createTrack() invalid format %d", format);
465 lStatus = BAD_VALUE;
466 goto Exit;
467 }
468
Mathias Agopian65ab4712010-07-14 17:59:35 -0700469 {
470 Mutex::Autolock _l(mLock);
471 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700472 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700473 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800474 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475 lStatus = BAD_VALUE;
476 goto Exit;
477 }
478
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800479 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800480 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700481
Steve Block3856b092011-10-20 11:56:00 +0100482 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700483 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700484 // check if an effect chain with the same session ID is present on another
485 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700486 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700487 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
488 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700489 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700490 if (sessions & PlaybackThread::EFFECT_SESSION) {
491 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700492 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700493 }
Eric Laurentde070132010-07-13 04:45:46 -0700494 }
495 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700496 lSessionId = *sessionId;
497 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700498 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700499 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700500 if (sessionId != NULL) {
501 *sessionId = lSessionId;
502 }
503 }
Steve Block3856b092011-10-20 11:56:00 +0100504 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505
506 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800507 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700508
509 // move effect chain to this output thread if an effect on same session was waiting
510 // for a track to be created
511 if (lStatus == NO_ERROR && effectThread != NULL) {
512 Mutex::Autolock _dl(thread->mLock);
513 Mutex::Autolock _sl(effectThread->mLock);
514 moveEffectChain_l(lSessionId, effectThread, thread, true);
515 }
Eric Laurenta011e352012-03-29 15:51:43 -0700516
517 // Look for sync events awaiting for a session to be used.
518 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
519 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
520 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700521 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700522 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700523 } else {
524 mPendingSyncEvents[i]->cancel();
525 }
Eric Laurenta011e352012-03-29 15:51:43 -0700526 mPendingSyncEvents.removeAt(i);
527 i--;
528 }
529 }
530 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 }
532 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700533 // s for server's pid, n for normal mixer name, f for fast index
534 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
535 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700536 trackHandle = new TrackHandle(track);
537 } else {
538 // remove local strong reference to Client before deleting the Track so that the Client
539 // destructor is called by the TrackBase destructor with mLock held
540 client.clear();
541 track.clear();
542 }
543
544Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700545 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 *status = lStatus;
547 }
548 return trackHandle;
549}
550
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800551uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552{
553 Mutex::Autolock _l(mLock);
554 PlaybackThread *thread = checkPlaybackThread_l(output);
555 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000556 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557 return 0;
558 }
559 return thread->sampleRate();
560}
561
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800562int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563{
564 Mutex::Autolock _l(mLock);
565 PlaybackThread *thread = checkPlaybackThread_l(output);
566 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000567 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700568 return 0;
569 }
570 return thread->channelCount();
571}
572
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800573audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574{
575 Mutex::Autolock _l(mLock);
576 PlaybackThread *thread = checkPlaybackThread_l(output);
577 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000578 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800579 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580 }
581 return thread->format();
582}
583
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800584size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585{
586 Mutex::Autolock _l(mLock);
587 PlaybackThread *thread = checkPlaybackThread_l(output);
588 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000589 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590 return 0;
591 }
Glenn Kasten58912562012-04-03 10:45:00 -0700592 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
593 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594 return thread->frameCount();
595}
596
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800597uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700598{
599 Mutex::Autolock _l(mLock);
600 PlaybackThread *thread = checkPlaybackThread_l(output);
601 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800602 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603 return 0;
604 }
605 return thread->latency();
606}
607
608status_t AudioFlinger::setMasterVolume(float value)
609{
Eric Laurenta1884f92011-08-23 08:25:03 -0700610 status_t ret = initCheck();
611 if (ret != NO_ERROR) {
612 return ret;
613 }
614
Mathias Agopian65ab4712010-07-14 17:59:35 -0700615 // check calling permissions
616 if (!settingsAllowed()) {
617 return PERMISSION_DENIED;
618 }
619
Eric Laurenta4c5a552012-03-29 10:12:40 -0700620 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700621 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700622
John Grossmanee578c02012-07-23 17:05:46 -0700623 // Set master volume in the HALs which support it.
624 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
625 AutoMutex lock(mHardwareLock);
626 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800627
John Grossmanee578c02012-07-23 17:05:46 -0700628 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
629 if (dev->canSetMasterVolume()) {
630 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800631 }
John Grossmanee578c02012-07-23 17:05:46 -0700632 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634
John Grossmanee578c02012-07-23 17:05:46 -0700635 // Now set the master volume in each playback thread. Playback threads
636 // assigned to HALs which do not have master volume support will apply
637 // master volume during the mix operation. Threads with HALs which do
638 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800639 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700640 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641
642 return NO_ERROR;
643}
644
Glenn Kastenf78aee72012-01-04 11:00:47 -0800645status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646{
Eric Laurenta1884f92011-08-23 08:25:03 -0700647 status_t ret = initCheck();
648 if (ret != NO_ERROR) {
649 return ret;
650 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651
652 // check calling permissions
653 if (!settingsAllowed()) {
654 return PERMISSION_DENIED;
655 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800656 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000657 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658 return BAD_VALUE;
659 }
660
661 { // scope for the lock
662 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700663 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700665 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 mHardwareStatus = AUDIO_HW_IDLE;
667 }
668
669 if (NO_ERROR == ret) {
670 Mutex::Autolock _l(mLock);
671 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800672 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700673 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 }
675
676 return ret;
677}
678
679status_t AudioFlinger::setMicMute(bool state)
680{
Eric Laurenta1884f92011-08-23 08:25:03 -0700681 status_t ret = initCheck();
682 if (ret != NO_ERROR) {
683 return ret;
684 }
685
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686 // check calling permissions
687 if (!settingsAllowed()) {
688 return PERMISSION_DENIED;
689 }
690
691 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700692 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700693 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700694 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 mHardwareStatus = AUDIO_HW_IDLE;
696 return ret;
697}
698
699bool AudioFlinger::getMicMute() const
700{
Eric Laurenta1884f92011-08-23 08:25:03 -0700701 status_t ret = initCheck();
702 if (ret != NO_ERROR) {
703 return false;
704 }
705
Dima Zavinfce7a472011-04-19 22:30:36 -0700706 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800707 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700708 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700710 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 mHardwareStatus = AUDIO_HW_IDLE;
712 return state;
713}
714
715status_t AudioFlinger::setMasterMute(bool muted)
716{
John Grossmand8f178d2012-07-20 14:51:35 -0700717 status_t ret = initCheck();
718 if (ret != NO_ERROR) {
719 return ret;
720 }
721
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 // check calling permissions
723 if (!settingsAllowed()) {
724 return PERMISSION_DENIED;
725 }
726
John Grossmanee578c02012-07-23 17:05:46 -0700727 Mutex::Autolock _l(mLock);
728 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700729
John Grossmanee578c02012-07-23 17:05:46 -0700730 // Set master mute in the HALs which support it.
731 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
732 AutoMutex lock(mHardwareLock);
733 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700734
John Grossmanee578c02012-07-23 17:05:46 -0700735 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
736 if (dev->canSetMasterMute()) {
737 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700738 }
John Grossmanee578c02012-07-23 17:05:46 -0700739 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700740 }
741
John Grossmanee578c02012-07-23 17:05:46 -0700742 // Now set the master mute in each playback thread. Playback threads
743 // assigned to HALs which do not have master mute support will apply master
744 // mute during the mix operation. Threads with HALs which do support master
745 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800746 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700747 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748
749 return NO_ERROR;
750}
751
752float AudioFlinger::masterVolume() const
753{
Glenn Kasten98067102011-12-13 11:47:54 -0800754 Mutex::Autolock _l(mLock);
755 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700756}
757
758bool AudioFlinger::masterMute() const
759{
Glenn Kasten98067102011-12-13 11:47:54 -0800760 Mutex::Autolock _l(mLock);
761 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700762}
763
John Grossman4ff14ba2012-02-08 16:37:41 -0800764float AudioFlinger::masterVolume_l() const
765{
John Grossman4ff14ba2012-02-08 16:37:41 -0800766 return mMasterVolume;
767}
768
John Grossmand8f178d2012-07-20 14:51:35 -0700769bool AudioFlinger::masterMute_l() const
770{
John Grossmanee578c02012-07-23 17:05:46 -0700771 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700772}
773
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800774status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
775 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776{
777 // check calling permissions
778 if (!settingsAllowed()) {
779 return PERMISSION_DENIED;
780 }
781
Glenn Kasten263709e2012-01-06 08:40:01 -0800782 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000783 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784 return BAD_VALUE;
785 }
786
787 AutoMutex lock(mLock);
788 PlaybackThread *thread = NULL;
789 if (output) {
790 thread = checkPlaybackThread_l(output);
791 if (thread == NULL) {
792 return BAD_VALUE;
793 }
794 }
795
796 mStreamTypes[stream].volume = value;
797
798 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800799 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700800 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801 }
802 } else {
803 thread->setStreamVolume(stream, value);
804 }
805
806 return NO_ERROR;
807}
808
Glenn Kastenfff6d712012-01-12 16:38:12 -0800809status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700810{
811 // check calling permissions
812 if (!settingsAllowed()) {
813 return PERMISSION_DENIED;
814 }
815
Glenn Kasten263709e2012-01-06 08:40:01 -0800816 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700817 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000818 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 return BAD_VALUE;
820 }
821
Eric Laurent93575202011-01-18 18:39:02 -0800822 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823 mStreamTypes[stream].mute = muted;
824 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700825 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826
827 return NO_ERROR;
828}
829
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800830float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831{
Glenn Kasten263709e2012-01-06 08:40:01 -0800832 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 return 0.0f;
834 }
835
836 AutoMutex lock(mLock);
837 float volume;
838 if (output) {
839 PlaybackThread *thread = checkPlaybackThread_l(output);
840 if (thread == NULL) {
841 return 0.0f;
842 }
843 volume = thread->streamVolume(stream);
844 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800845 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 }
847
848 return volume;
849}
850
Glenn Kastenfff6d712012-01-12 16:38:12 -0800851bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852{
Glenn Kasten263709e2012-01-06 08:40:01 -0800853 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854 return true;
855 }
856
Glenn Kasten6637baa2012-01-09 09:40:36 -0800857 AutoMutex lock(mLock);
858 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859}
860
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800861status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700863 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
864 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800865
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866 // check calling permissions
867 if (!settingsAllowed()) {
868 return PERMISSION_DENIED;
869 }
870
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871 // ioHandle == 0 means the parameters are global to the audio hardware interface
872 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700873 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700874 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800875 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700876 AutoMutex lock(mHardwareLock);
877 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
878 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
879 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
880 status_t result = dev->set_parameters(dev, keyValuePairs.string());
881 final_result = result ?: final_result;
882 }
883 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800884 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700885 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
886 AudioParameter param = AudioParameter(keyValuePairs);
887 String8 value;
888 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700889 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
890 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700891 for (size_t i = 0; i < mRecordThreads.size(); i++) {
892 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700893 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700894 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
895 // collect all of the thread's session IDs
896 KeyedVector<int, bool> ids = thread->sessionIds();
897 // suspend effects associated with those session IDs
898 for (size_t j = 0; j < ids.size(); ++j) {
899 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700900 thread->setEffectSuspended(FX_IID_AEC,
901 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700902 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700903 thread->setEffectSuspended(FX_IID_NS,
904 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700905 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700906 }
907 }
Eric Laurentbee53372011-08-29 12:42:48 -0700908 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700909 }
910 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700911 String8 screenState;
912 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
913 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800914 if (isOff != (AudioFlinger::mScreenState & 1)) {
915 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700916 }
917 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700918 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919 }
920
921 // hold a strong ref on thread in case closeOutput() or closeInput() is called
922 // and the thread is exited once the lock is released
923 sp<ThreadBase> thread;
924 {
925 Mutex::Autolock _l(mLock);
926 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700927 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800929 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700930 // indicate output device change to all input threads for pre processing
931 AudioParameter param = AudioParameter(keyValuePairs);
932 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700933 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
934 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700935 for (size_t i = 0; i < mRecordThreads.size(); i++) {
936 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
937 }
938 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939 }
940 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800941 if (thread != 0) {
942 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700943 }
944 return BAD_VALUE;
945}
946
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800947String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700949 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
950 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951
Eric Laurenta4c5a552012-03-29 10:12:40 -0700952 Mutex::Autolock _l(mLock);
953
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700955 String8 out_s8;
956
Dima Zavin799a70e2011-04-18 16:57:27 -0700957 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800958 char *s;
959 {
960 AutoMutex lock(mHardwareLock);
961 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700962 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800963 s = dev->get_parameters(dev, keys.string());
964 mHardwareStatus = AUDIO_HW_IDLE;
965 }
John Grossmanef7740b2012-02-09 11:28:36 -0800966 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700967 free(s);
968 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700969 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970 }
971
Mathias Agopian65ab4712010-07-14 17:59:35 -0700972 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
973 if (playbackThread != NULL) {
974 return playbackThread->getParameters(keys);
975 }
976 RecordThread *recordThread = checkRecordThread_l(ioHandle);
977 if (recordThread != NULL) {
978 return recordThread->getParameters(keys);
979 }
980 return String8("");
981}
982
Glenn Kastendd8104c2012-07-02 12:42:44 -0700983size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
984 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985{
Eric Laurenta1884f92011-08-23 08:25:03 -0700986 status_t ret = initCheck();
987 if (ret != NO_ERROR) {
988 return 0;
989 }
990
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800991 AutoMutex lock(mHardwareLock);
992 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000993 struct audio_config config;
994 memset(&config, 0, sizeof(config));
995 config.sample_rate = sampleRate;
996 config.channel_mask = channelMask;
997 config.format = format;
998
John Grossmanee578c02012-07-23 17:05:46 -0700999 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1000 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001001 mHardwareStatus = AUDIO_HW_IDLE;
1002 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001003}
1004
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001005unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001006{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001007 Mutex::Autolock _l(mLock);
1008
1009 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1010 if (recordThread != NULL) {
1011 return recordThread->getInputFramesLost();
1012 }
1013 return 0;
1014}
1015
1016status_t AudioFlinger::setVoiceVolume(float value)
1017{
Eric Laurenta1884f92011-08-23 08:25:03 -07001018 status_t ret = initCheck();
1019 if (ret != NO_ERROR) {
1020 return ret;
1021 }
1022
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023 // check calling permissions
1024 if (!settingsAllowed()) {
1025 return PERMISSION_DENIED;
1026 }
1027
1028 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001029 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001030 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001031 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001032 mHardwareStatus = AUDIO_HW_IDLE;
1033
1034 return ret;
1035}
1036
Glenn Kasten26c77552012-11-16 12:01:44 -08001037status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001038 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039{
1040 status_t status;
1041
1042 Mutex::Autolock _l(mLock);
1043
1044 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1045 if (playbackThread != NULL) {
1046 return playbackThread->getRenderPosition(halFrames, dspFrames);
1047 }
1048
1049 return BAD_VALUE;
1050}
1051
1052void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1053{
1054
1055 Mutex::Autolock _l(mLock);
1056
Glenn Kastenbb001922012-02-03 11:10:26 -08001057 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058 if (mNotificationClients.indexOfKey(pid) < 0) {
1059 sp<NotificationClient> notificationClient = new NotificationClient(this,
1060 client,
1061 pid);
Steve Block3856b092011-10-20 11:56:00 +01001062 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063
1064 mNotificationClients.add(pid, notificationClient);
1065
1066 sp<IBinder> binder = client->asBinder();
1067 binder->linkToDeath(notificationClient);
1068
1069 // the config change is always sent from playback or record threads to avoid deadlock
1070 // with AudioSystem::gLock
1071 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001072 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073 }
1074
1075 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001076 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077 }
1078 }
1079}
1080
1081void AudioFlinger::removeNotificationClient(pid_t pid)
1082{
1083 Mutex::Autolock _l(mLock);
1084
Glenn Kastena3b09252012-01-20 09:19:01 -08001085 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001086
Steve Block3856b092011-10-20 11:56:00 +01001087 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001088 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001089 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001090 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001091 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001092 ALOGV(" pid %d @ %d", ref->mPid, i);
1093 if (ref->mPid == pid) {
1094 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001095 mAudioSessionRefs.removeAt(i);
1096 delete ref;
1097 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001098 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001099 } else {
1100 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001101 }
1102 }
1103 if (removed) {
1104 purgeStaleEffects_l();
1105 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106}
1107
1108// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001109void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110{
1111 size_t size = mNotificationClients.size();
1112 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001113 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1114 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115 }
1116}
1117
1118// removeClient_l() must be called with AudioFlinger::mLock held
1119void AudioFlinger::removeClient_l(pid_t pid)
1120{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001121 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001122 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123 mClients.removeItem(pid);
1124}
1125
Eric Laurent717e1282012-06-29 16:36:52 -07001126// getEffectThread_l() must be called with AudioFlinger::mLock held
1127sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1128{
1129 sp<PlaybackThread> thread;
1130
1131 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1132 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1133 ALOG_ASSERT(thread == 0);
1134 thread = mPlaybackThreads.valueAt(i);
1135 }
1136 }
1137
1138 return thread;
1139}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001140
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142
1143// ----------------------------------------------------------------------------
1144
1145AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1146 : RefBase(),
1147 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001148 // 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 -07001149 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001150 mPid(pid),
1151 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152{
1153 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1154}
1155
1156// Client destructor must be called with AudioFlinger::mLock held
1157AudioFlinger::Client::~Client()
1158{
1159 mAudioFlinger->removeClient_l(mPid);
1160}
1161
Glenn Kasten435dbe62012-01-30 10:15:48 -08001162sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163{
1164 return mMemoryDealer;
1165}
1166
John Grossman4ff14ba2012-02-08 16:37:41 -08001167// Reserve one of the limited slots for a timed audio track associated
1168// with this client
1169bool AudioFlinger::Client::reserveTimedTrack()
1170{
1171 const int kMaxTimedTracksPerClient = 4;
1172
1173 Mutex::Autolock _l(mTimedTrackLock);
1174
1175 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1176 ALOGW("can not create timed track - pid %d has exceeded the limit",
1177 mPid);
1178 return false;
1179 }
1180
1181 mTimedTrackCount++;
1182 return true;
1183}
1184
1185// Release a slot for a timed audio track
1186void AudioFlinger::Client::releaseTimedTrack()
1187{
1188 Mutex::Autolock _l(mTimedTrackLock);
1189 mTimedTrackCount--;
1190}
1191
Mathias Agopian65ab4712010-07-14 17:59:35 -07001192// ----------------------------------------------------------------------------
1193
1194AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1195 const sp<IAudioFlingerClient>& client,
1196 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001197 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001198{
1199}
1200
1201AudioFlinger::NotificationClient::~NotificationClient()
1202{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203}
1204
1205void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1206{
1207 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001208 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001209}
1210
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211
1212// ----------------------------------------------------------------------------
1213
Jeff Brown893a5642013-08-16 20:19:26 -07001214static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1215 return audio_is_remote_submix_device(inDevice);
1216}
1217
Mathias Agopian65ab4712010-07-14 17:59:35 -07001218sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001219 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001221 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001222 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001223 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001224 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001225 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226 int *sessionId,
1227 status_t *status)
1228{
1229 sp<RecordThread::RecordTrack> recordTrack;
1230 sp<RecordHandle> recordHandle;
1231 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001232 status_t lStatus;
1233 RecordThread *thread;
1234 size_t inFrameCount;
1235 int lSessionId;
1236
1237 // check calling permissions
1238 if (!recordingAllowed()) {
1239 lStatus = PERMISSION_DENIED;
1240 goto Exit;
1241 }
1242
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001243 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1244 ALOGE("openRecord() invalid format %d", format);
1245 lStatus = BAD_VALUE;
1246 goto Exit;
1247 }
1248
Mathias Agopian65ab4712010-07-14 17:59:35 -07001249 // add client to list
1250 { // scope for mLock
1251 Mutex::Autolock _l(mLock);
1252 thread = checkRecordThread_l(input);
1253 if (thread == NULL) {
1254 lStatus = BAD_VALUE;
1255 goto Exit;
1256 }
1257
Jeff Brown893a5642013-08-16 20:19:26 -07001258 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1259 && !captureAudioOutputAllowed()) {
1260 lStatus = PERMISSION_DENIED;
1261 goto Exit;
1262 }
1263
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001264 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001265 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001266
1267 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001268 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269 lSessionId = *sessionId;
1270 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001271 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272 if (sessionId != NULL) {
1273 *sessionId = lSessionId;
1274 }
1275 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001276 // create new record track.
1277 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001278 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kastenddb0ccf2013-07-31 16:14:50 -07001279 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001280 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001281 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001282 // remove local strong reference to Client before deleting the RecordTrack so that the
1283 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001284 client.clear();
1285 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286 goto Exit;
1287 }
1288
1289 // return to handle to client
1290 recordHandle = new RecordHandle(recordTrack);
1291 lStatus = NO_ERROR;
1292
1293Exit:
1294 if (status) {
1295 *status = lStatus;
1296 }
1297 return recordHandle;
1298}
1299
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001300
1301
Mathias Agopian65ab4712010-07-14 17:59:35 -07001302// ----------------------------------------------------------------------------
1303
Eric Laurenta4c5a552012-03-29 10:12:40 -07001304audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1305{
1306 if (!settingsAllowed()) {
1307 return 0;
1308 }
1309 Mutex::Autolock _l(mLock);
1310 return loadHwModule_l(name);
1311}
1312
1313// loadHwModule_l() must be called with AudioFlinger::mLock held
1314audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1315{
1316 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1317 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1318 ALOGW("loadHwModule() module %s already loaded", name);
1319 return mAudioHwDevs.keyAt(i);
1320 }
1321 }
1322
Eric Laurenta4c5a552012-03-29 10:12:40 -07001323 audio_hw_device_t *dev;
1324
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001325 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001326 if (rc) {
1327 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1328 return 0;
1329 }
1330
1331 mHardwareStatus = AUDIO_HW_INIT;
1332 rc = dev->init_check(dev);
1333 mHardwareStatus = AUDIO_HW_IDLE;
1334 if (rc) {
1335 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1336 return 0;
1337 }
1338
John Grossmanee578c02012-07-23 17:05:46 -07001339 // Check and cache this HAL's level of support for master mute and master
1340 // volume. If this is the first HAL opened, and it supports the get
1341 // methods, use the initial values provided by the HAL as the current
1342 // master mute and volume settings.
1343
1344 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1345 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001346 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001347
1348 if (0 == mAudioHwDevs.size()) {
1349 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1350 if (NULL != dev->get_master_volume) {
1351 float mv;
1352 if (OK == dev->get_master_volume(dev, &mv)) {
1353 mMasterVolume = mv;
1354 }
1355 }
1356
1357 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1358 if (NULL != dev->get_master_mute) {
1359 bool mm;
1360 if (OK == dev->get_master_mute(dev, &mm)) {
1361 mMasterMute = mm;
1362 }
1363 }
1364 }
1365
Eric Laurenta4c5a552012-03-29 10:12:40 -07001366 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001367 if ((NULL != dev->set_master_volume) &&
1368 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1369 flags = static_cast<AudioHwDevice::Flags>(flags |
1370 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1371 }
1372
1373 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1374 if ((NULL != dev->set_master_mute) &&
1375 (OK == dev->set_master_mute(dev, mMasterMute))) {
1376 flags = static_cast<AudioHwDevice::Flags>(flags |
1377 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1378 }
1379
Eric Laurenta4c5a552012-03-29 10:12:40 -07001380 mHardwareStatus = AUDIO_HW_IDLE;
1381 }
1382
1383 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001384 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001385
1386 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001387 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001388
1389 return handle;
1390
1391}
1392
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001393// ----------------------------------------------------------------------------
1394
Glenn Kasten3b16c762012-11-14 08:44:39 -08001395uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001396{
1397 Mutex::Autolock _l(mLock);
1398 PlaybackThread *thread = primaryPlaybackThread_l();
1399 return thread != NULL ? thread->sampleRate() : 0;
1400}
1401
Glenn Kastene33054e2012-11-14 12:54:39 -08001402size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001403{
1404 Mutex::Autolock _l(mLock);
1405 PlaybackThread *thread = primaryPlaybackThread_l();
1406 return thread != NULL ? thread->frameCountHAL() : 0;
1407}
1408
1409// ----------------------------------------------------------------------------
1410
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001411status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1412{
1413 uid_t uid = IPCThreadState::self()->getCallingUid();
1414 if (uid != AID_SYSTEM) {
1415 return PERMISSION_DENIED;
1416 }
1417 Mutex::Autolock _l(mLock);
1418 if (mIsDeviceTypeKnown) {
1419 return INVALID_OPERATION;
1420 }
1421 mIsLowRamDevice = isLowRamDevice;
1422 mIsDeviceTypeKnown = true;
1423 return NO_ERROR;
1424}
1425
1426// ----------------------------------------------------------------------------
1427
Eric Laurenta4c5a552012-03-29 10:12:40 -07001428audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1429 audio_devices_t *pDevices,
1430 uint32_t *pSamplingRate,
1431 audio_format_t *pFormat,
1432 audio_channel_mask_t *pChannelMask,
1433 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001434 audio_output_flags_t flags,
1435 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001436{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001438 struct audio_config config;
1439 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1440 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1441 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1442 if (offloadInfo) {
1443 config.offload_info = *offloadInfo;
1444 }
1445
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001446 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001447 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001448
Eric Laurentbfb1b832013-01-07 09:53:42 -08001449 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001450 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001451 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001452 config.sample_rate,
1453 config.format,
1454 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001455 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001456 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1457 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001458
1459 if (pDevices == NULL || *pDevices == 0) {
1460 return 0;
1461 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001462
Mathias Agopian65ab4712010-07-14 17:59:35 -07001463 Mutex::Autolock _l(mLock);
1464
Eric Laurenta4c5a552012-03-29 10:12:40 -07001465 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001466 if (outHwDev == NULL)
1467 return 0;
1468
John Grossmanee578c02012-07-23 17:05:46 -07001469 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001470 audio_io_handle_t id = nextUniqueId();
1471
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001472 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001473
Glenn Kasten34542ac2013-06-26 11:29:02 -07001474 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001475 id,
1476 *pDevices,
1477 (audio_output_flags_t)flags,
1478 &config,
1479 &outStream);
1480
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001481 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001482 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001483 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001484 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001485 config.sample_rate,
1486 config.format,
1487 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001488 status);
1489
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001490 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001491 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001492
Eric Laurentbfb1b832013-01-07 09:53:42 -08001493 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1494 thread = new OffloadThread(this, output, id, *pDevices);
1495 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1496 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001497 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1498 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001499 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001500 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001501 } else {
1502 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001503 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001504 }
1505 mPlaybackThreads.add(id, thread);
1506
Glenn Kasten7c027242012-12-26 14:43:16 -08001507 if (pSamplingRate != NULL) {
1508 *pSamplingRate = config.sample_rate;
1509 }
1510 if (pFormat != NULL) {
1511 *pFormat = config.format;
1512 }
1513 if (pChannelMask != NULL) {
1514 *pChannelMask = config.channel_mask;
1515 }
1516 if (pLatencyMs != NULL) {
1517 *pLatencyMs = thread->latency();
1518 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519
1520 // notify client processes of the new output creation
1521 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001522
1523 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001524 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001525 ALOGI("Using module %d has the primary audio interface", module);
1526 mPrimaryHardwareDev = outHwDev;
1527
1528 AutoMutex lock(mHardwareLock);
1529 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001530 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001531 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001532 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533 return id;
1534 }
1535
1536 return 0;
1537}
1538
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001539audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1540 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001541{
1542 Mutex::Autolock _l(mLock);
1543 MixerThread *thread1 = checkMixerThread_l(output1);
1544 MixerThread *thread2 = checkMixerThread_l(output2);
1545
1546 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001547 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1548 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001549 return 0;
1550 }
1551
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001552 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001553 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1554 thread->addOutputTrack(thread2);
1555 mPlaybackThreads.add(id, thread);
1556 // notify client processes of the new output creation
1557 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1558 return id;
1559}
1560
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001561status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562{
Glenn Kastend96c5722012-04-25 13:44:49 -07001563 return closeOutput_nonvirtual(output);
1564}
1565
1566status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1567{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001568 // keep strong reference on the playback thread so that
1569 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001570 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001571 {
1572 Mutex::Autolock _l(mLock);
1573 thread = checkPlaybackThread_l(output);
1574 if (thread == NULL) {
1575 return BAD_VALUE;
1576 }
1577
Steve Block3856b092011-10-20 11:56:00 +01001578 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001579
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001580 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001581 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001582 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001583 DuplicatingThread *dupThread =
1584 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001585 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001586
1587 }
1588 }
1589 }
1590
1591
1592 mPlaybackThreads.removeItem(output);
1593 // save all effects to the default thread
1594 if (mPlaybackThreads.size()) {
1595 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1596 if (dstThread != NULL) {
1597 // audioflinger lock is held here so the acquisition order of thread locks does not
1598 // matter
1599 Mutex::Autolock _dl(dstThread->mLock);
1600 Mutex::Autolock _sl(thread->mLock);
1601 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1602 for (size_t i = 0; i < effectChains.size(); i ++) {
1603 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001604 }
1605 }
1606 }
Glenn Kastena1117922012-01-26 10:53:32 -08001607 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001608 }
1609 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001610 // The thread entity (active unit of execution) is no longer running here,
1611 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001612
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001613 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001614 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001615 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001616 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001617 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001618 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001619 }
1620 return NO_ERROR;
1621}
1622
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001623status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001624{
1625 Mutex::Autolock _l(mLock);
1626 PlaybackThread *thread = checkPlaybackThread_l(output);
1627
1628 if (thread == NULL) {
1629 return BAD_VALUE;
1630 }
1631
Steve Block3856b092011-10-20 11:56:00 +01001632 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001633 thread->suspend();
1634
1635 return NO_ERROR;
1636}
1637
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001638status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001639{
1640 Mutex::Autolock _l(mLock);
1641 PlaybackThread *thread = checkPlaybackThread_l(output);
1642
1643 if (thread == NULL) {
1644 return BAD_VALUE;
1645 }
1646
Steve Block3856b092011-10-20 11:56:00 +01001647 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648
1649 thread->restore();
1650
1651 return NO_ERROR;
1652}
1653
Eric Laurenta4c5a552012-03-29 10:12:40 -07001654audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1655 audio_devices_t *pDevices,
1656 uint32_t *pSamplingRate,
1657 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001658 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001659{
1660 status_t status;
1661 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001662 struct audio_config config;
1663 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1664 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1665 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1666
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001667 uint32_t reqSamplingRate = config.sample_rate;
1668 audio_format_t reqFormat = config.format;
1669 audio_channel_mask_t reqChannels = config.channel_mask;
1670 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001671 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001672
1673 if (pDevices == NULL || *pDevices == 0) {
1674 return 0;
1675 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001676
Mathias Agopian65ab4712010-07-14 17:59:35 -07001677 Mutex::Autolock _l(mLock);
1678
Eric Laurenta4c5a552012-03-29 10:12:40 -07001679 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001680 if (inHwDev == NULL)
1681 return 0;
1682
John Grossmanee578c02012-07-23 17:05:46 -07001683 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001684 audio_io_handle_t id = nextUniqueId();
1685
John Grossmanee578c02012-07-23 17:05:46 -07001686 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001687 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001688 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1689 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001690 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001691 config.sample_rate,
1692 config.format,
1693 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001694 status);
1695
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001696 // If the input could not be opened with the requested parameters and we can handle the
1697 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1698 // 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 -07001699 if (status == BAD_VALUE &&
1700 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1701 (config.sample_rate <= 2 * reqSamplingRate) &&
1702 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001703 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001704 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001705 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001706 }
1707
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001708 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001709
Glenn Kasten46909e72013-02-26 09:20:22 -08001710#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001711 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1712 // or (re-)create if current Pipe is idle and does not match the new format
1713 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001714 enum {
1715 TEE_SINK_NO, // don't copy input
1716 TEE_SINK_NEW, // copy input using a new pipe
1717 TEE_SINK_OLD, // copy input using an existing pipe
1718 } kind;
1719 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1720 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001721 if (!mTeeSinkInputEnabled) {
1722 kind = TEE_SINK_NO;
1723 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001724 kind = TEE_SINK_NO;
1725 } else if (mRecordTeeSink == 0) {
1726 kind = TEE_SINK_NEW;
1727 } else if (mRecordTeeSink->getStrongCount() != 1) {
1728 kind = TEE_SINK_NO;
1729 } else if (format == mRecordTeeSink->format()) {
1730 kind = TEE_SINK_OLD;
1731 } else {
1732 kind = TEE_SINK_NEW;
1733 }
1734 switch (kind) {
1735 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001736 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001737 size_t numCounterOffers = 0;
1738 const NBAIO_Format offers[1] = {format};
1739 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1740 ALOG_ASSERT(index == 0);
1741 PipeReader *pipeReader = new PipeReader(*pipe);
1742 numCounterOffers = 0;
1743 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1744 ALOG_ASSERT(index == 0);
1745 mRecordTeeSink = pipe;
1746 mRecordTeeSource = pipeReader;
1747 teeSink = pipe;
1748 }
1749 break;
1750 case TEE_SINK_OLD:
1751 teeSink = mRecordTeeSink;
1752 break;
1753 case TEE_SINK_NO:
1754 default:
1755 break;
1756 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001757#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001758
Dima Zavin799a70e2011-04-18 16:57:27 -07001759 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1760
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001761 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001762 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001763 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001764 thread = new RecordThread(this,
1765 input,
1766 reqSamplingRate,
1767 reqChannels,
1768 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001769 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001770 *pDevices
1771#ifdef TEE_SINK
1772 , teeSink
1773#endif
1774 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001775 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001776 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001777 if (pSamplingRate != NULL) {
1778 *pSamplingRate = reqSamplingRate;
1779 }
1780 if (pFormat != NULL) {
1781 *pFormat = config.format;
1782 }
1783 if (pChannelMask != NULL) {
1784 *pChannelMask = reqChannels;
1785 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001786
Mathias Agopian65ab4712010-07-14 17:59:35 -07001787 // notify client processes of the new input creation
1788 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1789 return id;
1790 }
1791
1792 return 0;
1793}
1794
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001795status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001796{
Glenn Kastend96c5722012-04-25 13:44:49 -07001797 return closeInput_nonvirtual(input);
1798}
1799
1800status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1801{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001802 // keep strong reference on the record thread so that
1803 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001804 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001805 {
1806 Mutex::Autolock _l(mLock);
1807 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001808 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001809 return BAD_VALUE;
1810 }
1811
Steve Block3856b092011-10-20 11:56:00 +01001812 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001813 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001814 mRecordThreads.removeItem(input);
1815 }
1816 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001817 // The thread entity (active unit of execution) is no longer running here,
1818 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001819
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001820 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001821 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001822 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001823 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001824 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001825
1826 return NO_ERROR;
1827}
1828
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001829status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001830{
1831 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001832 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001833
1834 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1835 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001836 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001837 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001838
1839 return NO_ERROR;
1840}
1841
1842
1843int AudioFlinger::newAudioSessionId()
1844{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001845 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001846}
1847
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001848void AudioFlinger::acquireAudioSessionId(int audioSession)
1849{
1850 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001851 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001852 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001853 size_t num = mAudioSessionRefs.size();
1854 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001855 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001856 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1857 ref->mCnt++;
1858 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001859 return;
1860 }
1861 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001862 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1863 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001864}
1865
1866void AudioFlinger::releaseAudioSessionId(int audioSession)
1867{
1868 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001869 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001870 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001871 size_t num = mAudioSessionRefs.size();
1872 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001873 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001874 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1875 ref->mCnt--;
1876 ALOGV(" decremented refcount to %d", ref->mCnt);
1877 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001878 mAudioSessionRefs.removeAt(i);
1879 delete ref;
1880 purgeStaleEffects_l();
1881 }
1882 return;
1883 }
1884 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001885 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001886}
1887
1888void AudioFlinger::purgeStaleEffects_l() {
1889
Steve Block3856b092011-10-20 11:56:00 +01001890 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001891
1892 Vector< sp<EffectChain> > chains;
1893
1894 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1895 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1896 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1897 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001898 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1899 chains.push(ec);
1900 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001901 }
1902 }
1903 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1904 sp<RecordThread> t = mRecordThreads.valueAt(i);
1905 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1906 sp<EffectChain> ec = t->mEffectChains[j];
1907 chains.push(ec);
1908 }
1909 }
1910
1911 for (size_t i = 0; i < chains.size(); i++) {
1912 sp<EffectChain> ec = chains[i];
1913 int sessionid = ec->sessionId();
1914 sp<ThreadBase> t = ec->mThread.promote();
1915 if (t == 0) {
1916 continue;
1917 }
1918 size_t numsessionrefs = mAudioSessionRefs.size();
1919 bool found = false;
1920 for (size_t k = 0; k < numsessionrefs; k++) {
1921 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001922 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001923 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001924 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001925 found = true;
1926 break;
1927 }
1928 }
1929 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001930 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001931 // remove all effects from the chain
1932 while (ec->mEffects.size()) {
1933 sp<EffectModule> effect = ec->mEffects[0];
1934 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001935 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001936 if (effect->purgeHandles()) {
1937 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001938 }
1939 AudioSystem::unregisterEffect(effect->id());
1940 }
1941 }
1942 }
1943 return;
1944}
1945
Mathias Agopian65ab4712010-07-14 17:59:35 -07001946// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001947AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948{
Glenn Kastena1117922012-01-26 10:53:32 -08001949 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950}
1951
1952// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001953AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001954{
1955 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001956 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957}
1958
1959// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001960AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001961{
Glenn Kastena1117922012-01-26 10:53:32 -08001962 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001963}
1964
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001965uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001966{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001967 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968}
1969
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001970AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001971{
1972 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1973 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001974 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001975 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001976 return thread;
1977 }
1978 }
1979 return NULL;
1980}
1981
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001982audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001983{
1984 PlaybackThread *thread = primaryPlaybackThread_l();
1985
1986 if (thread == NULL) {
1987 return 0;
1988 }
1989
Eric Laurentf1c04f92012-08-28 14:26:53 -07001990 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001991}
1992
Eric Laurenta011e352012-03-29 15:51:43 -07001993sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1994 int triggerSession,
1995 int listenerSession,
1996 sync_event_callback_t callBack,
1997 void *cookie)
1998{
1999 Mutex::Autolock _l(mLock);
2000
2001 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2002 status_t playStatus = NAME_NOT_FOUND;
2003 status_t recStatus = NAME_NOT_FOUND;
2004 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2005 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2006 if (playStatus == NO_ERROR) {
2007 return event;
2008 }
2009 }
2010 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2011 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2012 if (recStatus == NO_ERROR) {
2013 return event;
2014 }
2015 }
2016 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2017 mPendingSyncEvents.add(event);
2018 } else {
2019 ALOGV("createSyncEvent() invalid event %d", event->type());
2020 event.clear();
2021 }
2022 return event;
2023}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002024
Mathias Agopian65ab4712010-07-14 17:59:35 -07002025// ----------------------------------------------------------------------------
2026// Effect management
2027// ----------------------------------------------------------------------------
2028
2029
Glenn Kastenf587ba52012-01-26 16:25:10 -08002030status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002031{
2032 Mutex::Autolock _l(mLock);
2033 return EffectQueryNumberEffects(numEffects);
2034}
2035
Glenn Kastenf587ba52012-01-26 16:25:10 -08002036status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002037{
2038 Mutex::Autolock _l(mLock);
2039 return EffectQueryEffect(index, descriptor);
2040}
2041
Glenn Kasten5e92a782012-01-30 07:40:52 -08002042status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002043 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002044{
2045 Mutex::Autolock _l(mLock);
2046 return EffectGetDescriptor(pUuid, descriptor);
2047}
2048
2049
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002050sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002051 effect_descriptor_t *pDesc,
2052 const sp<IEffectClient>& effectClient,
2053 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002054 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002055 int sessionId,
2056 status_t *status,
2057 int *id,
2058 int *enabled)
2059{
2060 status_t lStatus = NO_ERROR;
2061 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002062 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002063
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002064 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002065 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002066 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002067
2068 if (pDesc == NULL) {
2069 lStatus = BAD_VALUE;
2070 goto Exit;
2071 }
2072
Eric Laurent84e9a102010-09-23 16:10:16 -07002073 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002074 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002075 lStatus = PERMISSION_DENIED;
2076 goto Exit;
2077 }
2078
Dima Zavinfce7a472011-04-19 22:30:36 -07002079 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002080 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002081 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002082 lStatus = PERMISSION_DENIED;
2083 goto Exit;
2084 }
2085
Mathias Agopian65ab4712010-07-14 17:59:35 -07002086 {
2087 Mutex::Autolock _l(mLock);
2088
Mathias Agopian65ab4712010-07-14 17:59:35 -07002089
2090 if (!EffectIsNullUuid(&pDesc->uuid)) {
2091 // if uuid is specified, request effect descriptor
2092 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2093 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002094 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002095 goto Exit;
2096 }
2097 } else {
2098 // if uuid is not specified, look for an available implementation
2099 // of the required type in effect factory
2100 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002101 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002102 lStatus = BAD_VALUE;
2103 goto Exit;
2104 }
2105 uint32_t numEffects = 0;
2106 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002107 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002108 bool found = false;
2109
2110 lStatus = EffectQueryNumberEffects(&numEffects);
2111 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002112 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002113 goto Exit;
2114 }
2115 for (uint32_t i = 0; i < numEffects; i++) {
2116 lStatus = EffectQueryEffect(i, &desc);
2117 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002118 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002119 continue;
2120 }
2121 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2122 // If matching type found save effect descriptor. If the session is
2123 // 0 and the effect is not auxiliary, continue enumeration in case
2124 // an auxiliary version of this effect type is available
2125 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002126 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002127 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002128 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2129 break;
2130 }
2131 }
2132 }
2133 if (!found) {
2134 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002135 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002136 goto Exit;
2137 }
2138 // For same effect type, chose auxiliary version over insert version if
2139 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002140 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002141 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002142 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002143 }
2144 }
2145
2146 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002147 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002148 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2149 lStatus = INVALID_OPERATION;
2150 goto Exit;
2151 }
2152
Eric Laurent59255e42011-07-27 19:49:51 -07002153 // check recording permission for visualizer
2154 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2155 !recordingAllowed()) {
2156 lStatus = PERMISSION_DENIED;
2157 goto Exit;
2158 }
2159
Mathias Agopian65ab4712010-07-14 17:59:35 -07002160 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002161 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002162
2163 // If output is not specified try to find a matching audio session ID in one of the
2164 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002165 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2166 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002167 // Note: io is never 0 when creating an effect on an input
2168 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002169 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2170 // output must be specified by AudioPolicyManager when using session
2171 // AUDIO_SESSION_OUTPUT_STAGE
2172 lStatus = BAD_VALUE;
2173 goto Exit;
2174 }
2175 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2176 // if the output returned by getOutputForEffect() is removed before we lock the
2177 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2178 // and we will exit safely
2179 io = AudioSystem::getOutputForEffect(&desc);
2180 ALOGV("createEffect got output %d", io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002181 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002182 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002183 // look for the thread where the specified audio session is present
2184 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2185 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2186 io = mPlaybackThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002187 break;
2188 }
2189 }
Eric Laurent5baf2af2013-09-12 17:37:00 -07002190 if (io == 0) {
2191 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2192 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2193 io = mRecordThreads.keyAt(i);
2194 break;
2195 }
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 Laurent5baf2af2013-09-12 17:37:00 -07002257 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002258}
2259
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002260// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002261status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002262 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002263 AudioFlinger::PlaybackThread *dstThread,
2264 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002265{
Steve Block3856b092011-10-20 11:56:00 +01002266 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002267 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002268
Eric Laurent59255e42011-07-27 19:49:51 -07002269 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002270 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002271 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002272 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002273 return INVALID_OPERATION;
2274 }
2275
Eric Laurent39e94f82010-07-28 01:32:47 -07002276 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002277 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002278 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002279 // removed.
2280 srcThread->removeEffectChain_l(chain);
2281
2282 // transfer all effects one by one so that new effect chain is created on new thread with
2283 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002284 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002285 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002286 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002287 Vector< sp<EffectModule> > removed;
2288 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002289 while (effect != 0) {
2290 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002291 removed.add(effect);
2292 status = dstThread->addEffect_l(effect);
2293 if (status != NO_ERROR) {
2294 break;
2295 }
Eric Laurentec35a142011-10-05 17:42:25 -07002296 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2297 if (effect->state() == EffectModule::ACTIVE ||
2298 effect->state() == EffectModule::STOPPING) {
2299 effect->start();
2300 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002301 // if the move request is not received from audio policy manager, the effect must be
2302 // re-registered with the new strategy and output
2303 if (dstChain == 0) {
2304 dstChain = effect->chain().promote();
2305 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002306 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002307 status = NO_INIT;
2308 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002309 }
2310 strategy = dstChain->strategy();
2311 }
2312 if (reRegister) {
2313 AudioSystem::unregisterEffect(effect->id());
2314 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002315 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002316 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002317 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002318 effect->id());
2319 }
Eric Laurentde070132010-07-13 04:45:46 -07002320 effect = chain->getEffectFromId_l(0);
2321 }
2322
Eric Laurent5baf2af2013-09-12 17:37:00 -07002323 if (status != NO_ERROR) {
2324 for (size_t i = 0; i < removed.size(); i++) {
2325 srcThread->addEffect_l(removed[i]);
2326 if (dstChain != 0 && reRegister) {
2327 AudioSystem::unregisterEffect(removed[i]->id());
2328 AudioSystem::registerEffect(&removed[i]->desc(),
2329 srcThread->id(),
2330 strategy,
2331 sessionId,
2332 removed[i]->id());
2333 }
2334 }
2335 }
2336
2337 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002338}
2339
Eric Laurent5baf2af2013-09-12 17:37:00 -07002340bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002341{
2342 if (mGlobalEffectEnableTime != 0 &&
2343 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2344 return true;
2345 }
2346
2347 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2348 sp<EffectChain> ec =
2349 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002350 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002351 return true;
2352 }
2353 }
2354 return false;
2355}
2356
Eric Laurent5baf2af2013-09-12 17:37:00 -07002357void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002358{
2359 Mutex::Autolock _l(mLock);
2360
2361 mGlobalEffectEnableTime = systemTime();
2362
2363 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2364 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2365 if (t->mType == ThreadBase::OFFLOAD) {
2366 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2367 }
2368 }
2369
2370}
2371
Glenn Kastenda6ef132013-01-10 12:31:01 -08002372struct Entry {
2373#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2374 char mName[MAX_NAME];
2375};
2376
2377int comparEntry(const void *p1, const void *p2)
2378{
2379 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2380}
2381
Glenn Kasten46909e72013-02-26 09:20:22 -08002382#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002383void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002384{
Eric Laurent81784c32012-11-19 14:55:58 -08002385 NBAIO_Source *teeSource = source.get();
2386 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002387 // .wav rotation
2388 // There is a benign race condition if 2 threads call this simultaneously.
2389 // They would both traverse the directory, but the result would simply be
2390 // failures at unlink() which are ignored. It's also unlikely since
2391 // normally dumpsys is only done by bugreport or from the command line.
2392 char teePath[32+256];
2393 strcpy(teePath, "/data/misc/media");
2394 size_t teePathLen = strlen(teePath);
2395 DIR *dir = opendir(teePath);
2396 teePath[teePathLen++] = '/';
2397 if (dir != NULL) {
2398#define MAX_SORT 20 // number of entries to sort
2399#define MAX_KEEP 10 // number of entries to keep
2400 struct Entry entries[MAX_SORT];
2401 size_t entryCount = 0;
2402 while (entryCount < MAX_SORT) {
2403 struct dirent de;
2404 struct dirent *result = NULL;
2405 int rc = readdir_r(dir, &de, &result);
2406 if (rc != 0) {
2407 ALOGW("readdir_r failed %d", rc);
2408 break;
2409 }
2410 if (result == NULL) {
2411 break;
2412 }
2413 if (result != &de) {
2414 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2415 break;
2416 }
2417 // ignore non .wav file entries
2418 size_t nameLen = strlen(de.d_name);
2419 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2420 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2421 continue;
2422 }
2423 strcpy(entries[entryCount++].mName, de.d_name);
2424 }
2425 (void) closedir(dir);
2426 if (entryCount > MAX_KEEP) {
2427 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2428 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2429 strcpy(&teePath[teePathLen], entries[i].mName);
2430 (void) unlink(teePath);
2431 }
2432 }
2433 } else {
2434 if (fd >= 0) {
2435 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2436 }
2437 }
Eric Laurent81784c32012-11-19 14:55:58 -08002438 char teeTime[16];
2439 struct timeval tv;
2440 gettimeofday(&tv, NULL);
2441 struct tm tm;
2442 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002443 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2444 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2445 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2446 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002447 if (teeFd >= 0) {
2448 char wavHeader[44];
2449 memcpy(wavHeader,
2450 "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",
2451 sizeof(wavHeader));
2452 NBAIO_Format format = teeSource->format();
2453 unsigned channelCount = Format_channelCount(format);
2454 ALOG_ASSERT(channelCount <= FCC_2);
2455 uint32_t sampleRate = Format_sampleRate(format);
2456 wavHeader[22] = channelCount; // number of channels
2457 wavHeader[24] = sampleRate; // sample rate
2458 wavHeader[25] = sampleRate >> 8;
2459 wavHeader[32] = channelCount * 2; // block alignment
2460 write(teeFd, wavHeader, sizeof(wavHeader));
2461 size_t total = 0;
2462 bool firstRead = true;
2463 for (;;) {
2464#define TEE_SINK_READ 1024
2465 short buffer[TEE_SINK_READ * FCC_2];
2466 size_t count = TEE_SINK_READ;
2467 ssize_t actual = teeSource->read(buffer, count,
2468 AudioBufferProvider::kInvalidPTS);
2469 bool wasFirstRead = firstRead;
2470 firstRead = false;
2471 if (actual <= 0) {
2472 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2473 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002474 }
Eric Laurent81784c32012-11-19 14:55:58 -08002475 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002476 }
Eric Laurent81784c32012-11-19 14:55:58 -08002477 ALOG_ASSERT(actual <= (ssize_t)count);
2478 write(teeFd, buffer, actual * channelCount * sizeof(short));
2479 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002480 }
Eric Laurent81784c32012-11-19 14:55:58 -08002481 lseek(teeFd, (off_t) 4, SEEK_SET);
2482 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2483 write(teeFd, &temp, sizeof(temp));
2484 lseek(teeFd, (off_t) 40, SEEK_SET);
2485 temp = total * channelCount * sizeof(short);
2486 write(teeFd, &temp, sizeof(temp));
2487 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002488 if (fd >= 0) {
2489 fdprintf(fd, "tee copied to %s\n", teePath);
2490 }
Eric Laurent59255e42011-07-27 19:49:51 -07002491 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002492 if (fd >= 0) {
2493 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2494 }
Eric Laurent59255e42011-07-27 19:49:51 -07002495 }
2496 }
2497}
Glenn Kasten46909e72013-02-26 09:20:22 -08002498#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002499
Mathias Agopian65ab4712010-07-14 17:59:35 -07002500// ----------------------------------------------------------------------------
2501
2502status_t AudioFlinger::onTransact(
2503 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2504{
2505 return BnAudioFlinger::onTransact(code, data, reply, flags);
2506}
2507
Mathias Agopian65ab4712010-07-14 17:59:35 -07002508}; // namespace android