blob: aa14d7b91fbd16a216a9da27c7fcb564f550224d [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
Mathias Agopian65ab4712010-07-14 17:59:35 -0700101// ----------------------------------------------------------------------------
102
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700103static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700104{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700105 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700106 int rc;
107
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700108 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
109 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
110 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
111 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700112 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700113 }
114 rc = audio_hw_device_open(mod, dev);
115 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
116 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
117 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700118 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700119 }
120 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
121 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
122 rc = BAD_VALUE;
123 goto out;
124 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700125 return 0;
126
127out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700128 *dev = NULL;
129 return rc;
130}
131
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132// ----------------------------------------------------------------------------
133
134AudioFlinger::AudioFlinger()
135 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800136 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700137 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800138 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800139 mMasterMute(false),
140 mNextUniqueId(1),
141 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700142 mBtNrecIsOff(false),
143 mIsLowRamDevice(true),
144 mIsDeviceTypeKnown(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145{
Glenn Kasten949a9262013-04-16 12:35:20 -0700146 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800147 char value[PROPERTY_VALUE_MAX];
148 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
149 if (doLog) {
150 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
151 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800152#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800153 (void) property_get("ro.debuggable", value, "0");
154 int debuggable = atoi(value);
155 int teeEnabled = 0;
156 if (debuggable) {
157 (void) property_get("af.tee", value, "0");
158 teeEnabled = atoi(value);
159 }
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700160 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800161 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700162 }
163 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800164 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700165 }
166 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800167 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700168 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800169#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700170}
171
172void AudioFlinger::onFirstRef()
173{
Dima Zavin799a70e2011-04-18 16:57:27 -0700174 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700175
Eric Laurent93575202011-01-18 18:39:02 -0800176 Mutex::Autolock _l(mLock);
177
Dima Zavin799a70e2011-04-18 16:57:27 -0700178 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800179 char val_str[PROPERTY_VALUE_MAX] = { 0 };
180 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
181 uint32_t int_val;
182 if (1 == sscanf(val_str, "%u", &int_val)) {
183 mStandbyTimeInNsecs = milliseconds(int_val);
184 ALOGI("Using %u mSec as standby time.", int_val);
185 } else {
186 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
187 ALOGI("Using default %u mSec as standby time.",
188 (uint32_t)(mStandbyTimeInNsecs / 1000000));
189 }
190 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700191
Eric Laurenta4c5a552012-03-29 10:12:40 -0700192 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193}
194
195AudioFlinger::~AudioFlinger()
196{
197 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700198 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700199 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700200 }
201 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700202 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700203 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700204 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700205
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800206 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
207 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700208 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
209 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700210 }
211}
212
Eric Laurenta4c5a552012-03-29 10:12:40 -0700213static const char * const audio_interfaces[] = {
214 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
215 AUDIO_HARDWARE_MODULE_ID_A2DP,
216 AUDIO_HARDWARE_MODULE_ID_USB,
217};
218#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
219
John Grossmanee578c02012-07-23 17:05:46 -0700220AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
221 audio_module_handle_t module,
222 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700223{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700224 // if module is 0, the request comes from an old policy manager and we should load
225 // well known modules
226 if (module == 0) {
227 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
228 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
229 loadHwModule_l(audio_interfaces[i]);
230 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700231 // then try to find a module supporting the requested device.
232 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
233 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
234 audio_hw_device_t *dev = audioHwDevice->hwDevice();
235 if ((dev->get_supported_devices != NULL) &&
236 (dev->get_supported_devices(dev) & devices) == devices)
237 return audioHwDevice;
238 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700239 } else {
240 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700241 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
242 if (audioHwDevice != NULL) {
243 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700244 }
245 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700246
Dima Zavin799a70e2011-04-18 16:57:27 -0700247 return NULL;
248}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700250void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251{
252 const size_t SIZE = 256;
253 char buffer[SIZE];
254 String8 result;
255
256 result.append("Clients:\n");
257 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800258 sp<Client> client = mClients.valueAt(i).promote();
259 if (client != 0) {
260 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
261 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700262 }
263 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700264
265 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800266 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700267 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
268 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800269 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700270 result.append(buffer);
271 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700272 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273}
274
275
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700276void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277{
278 const size_t SIZE = 256;
279 char buffer[SIZE];
280 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800281 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282
John Grossman4ff14ba2012-02-08 16:37:41 -0800283 snprintf(buffer, SIZE, "Hardware status: %d\n"
284 "Standby Time mSec: %u\n",
285 hardwareStatus,
286 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287 result.append(buffer);
288 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289}
290
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700291void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292{
293 const size_t SIZE = 256;
294 char buffer[SIZE];
295 String8 result;
296 snprintf(buffer, SIZE, "Permission Denial: "
297 "can't dump AudioFlinger from pid=%d, uid=%d\n",
298 IPCThreadState::self()->getCallingPid(),
299 IPCThreadState::self()->getCallingUid());
300 result.append(buffer);
301 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302}
303
Eric Laurent81784c32012-11-19 14:55:58 -0800304bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305{
306 bool locked = false;
307 for (int i = 0; i < kDumpLockRetries; ++i) {
308 if (mutex.tryLock() == NO_ERROR) {
309 locked = true;
310 break;
311 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800312 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313 }
314 return locked;
315}
316
317status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
318{
Glenn Kasten44deb052012-02-05 18:09:08 -0800319 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 dumpPermissionDenial(fd, args);
321 } else {
322 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800323 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 if (!hardwareLocked) {
325 String8 result(kHardwareLockedString);
326 write(fd, result.string(), result.size());
327 } else {
328 mHardwareLock.unlock();
329 }
330
Eric Laurent81784c32012-11-19 14:55:58 -0800331 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332
333 // failed to lock - AudioFlinger is probably deadlocked
334 if (!locked) {
335 String8 result(kDeadlockedString);
336 write(fd, result.string(), result.size());
337 }
338
339 dumpClients(fd, args);
340 dumpInternals(fd, args);
341
342 // dump playback threads
343 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
344 mPlaybackThreads.valueAt(i)->dump(fd, args);
345 }
346
347 // dump record threads
348 for (size_t i = 0; i < mRecordThreads.size(); i++) {
349 mRecordThreads.valueAt(i)->dump(fd, args);
350 }
351
Dima Zavin799a70e2011-04-18 16:57:27 -0700352 // dump all hardware devs
353 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700354 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700355 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700357
Glenn Kasten46909e72013-02-26 09:20:22 -0800358#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700359 // dump the serially shared record tee sink
360 if (mRecordTeeSource != 0) {
361 dumpTee(fd, mRecordTeeSource);
362 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800363#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700364
Glenn Kastend65d73c2012-06-22 17:21:07 -0700365 if (locked) {
366 mLock.unlock();
367 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800368
369 // append a copy of media.log here by forwarding fd to it, but don't attempt
370 // to lookup the service if it's not running, as it will block for a second
371 if (mLogMemoryDealer != 0) {
372 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
373 if (binder != 0) {
374 fdprintf(fd, "\nmedia.log:\n");
375 Vector<String16> args;
376 binder->dump(fd, args);
377 }
378 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379 }
380 return NO_ERROR;
381}
382
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800383sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
384{
385 // If pid is already in the mClients wp<> map, then use that entry
386 // (for which promote() is always != 0), otherwise create a new entry and Client.
387 sp<Client> client = mClients.valueFor(pid).promote();
388 if (client == 0) {
389 client = new Client(this, pid);
390 mClients.add(pid, client);
391 }
392
393 return client;
394}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395
Glenn Kasten9e58b552013-01-18 15:09:48 -0800396sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
397{
398 if (mLogMemoryDealer == 0) {
399 return new NBLog::Writer();
400 }
401 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
402 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
403 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
404 if (binder != 0) {
405 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
406 }
407 return writer;
408}
409
410void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
411{
Glenn Kasten685ef092013-02-04 08:15:34 -0800412 if (writer == 0) {
413 return;
414 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800415 sp<IMemory> iMemory(writer->getIMemory());
416 if (iMemory == 0) {
417 return;
418 }
419 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
420 if (binder != 0) {
421 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
422 // Now the media.log remote reference to IMemory is gone.
423 // When our last local reference to IMemory also drops to zero,
424 // the IMemory destructor will deallocate the region from mMemoryDealer.
425 }
426}
427
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428// IAudioFlinger interface
429
430
431sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800432 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700433 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800434 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700435 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800436 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800437 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800439 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800440 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700442 String8& name,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443 status_t *status)
444{
445 sp<PlaybackThread::Track> track;
446 sp<TrackHandle> trackHandle;
447 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700448 status_t lStatus;
449 int lSessionId;
450
Glenn Kasten263709e2012-01-06 08:40:01 -0800451 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
452 // but if someone uses binder directly they could bypass that and cause us to crash
453 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000454 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700455 lStatus = BAD_VALUE;
456 goto Exit;
457 }
458
Glenn Kasten60a83922012-06-21 12:56:37 -0700459 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
460 // and we don't yet support 8.24 or 32-bit PCM
461 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
462 ALOGE("createTrack() invalid format %d", format);
463 lStatus = BAD_VALUE;
464 goto Exit;
465 }
466
Mathias Agopian65ab4712010-07-14 17:59:35 -0700467 {
468 Mutex::Autolock _l(mLock);
469 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700470 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700471 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800472 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700473 lStatus = BAD_VALUE;
474 goto Exit;
475 }
476
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800477 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800478 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700479
Steve Block3856b092011-10-20 11:56:00 +0100480 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700481 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700482 // check if an effect chain with the same session ID is present on another
483 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700484 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700485 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
486 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700487 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700488 if (sessions & PlaybackThread::EFFECT_SESSION) {
489 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700490 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700491 }
Eric Laurentde070132010-07-13 04:45:46 -0700492 }
493 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700494 lSessionId = *sessionId;
495 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700496 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700497 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700498 if (sessionId != NULL) {
499 *sessionId = lSessionId;
500 }
501 }
Steve Block3856b092011-10-20 11:56:00 +0100502 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503
504 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800505 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Glenn Kasten2fc14732013-08-05 14:58:14 -0700506 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700507
508 // move effect chain to this output thread if an effect on same session was waiting
509 // for a track to be created
510 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700511 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700512 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 }
Glenn Kastene198c362013-08-13 09:13:36 -0700531
Mathias Agopian65ab4712010-07-14 17:59:35 -0700532 }
Glenn Kastene198c362013-08-13 09:13:36 -0700533
Mathias Agopian65ab4712010-07-14 17:59:35 -0700534 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700535 // s for server's pid, n for normal mixer name, f for fast index
536 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
537 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538 trackHandle = new TrackHandle(track);
539 } else {
540 // remove local strong reference to Client before deleting the Track so that the Client
541 // destructor is called by the TrackBase destructor with mLock held
542 client.clear();
543 track.clear();
544 }
545
546Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700547 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 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
1214sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001215 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001216 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001217 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001218 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001219 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001220 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001221 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222 int *sessionId,
1223 status_t *status)
1224{
1225 sp<RecordThread::RecordTrack> recordTrack;
1226 sp<RecordHandle> recordHandle;
1227 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001228 status_t lStatus;
1229 RecordThread *thread;
1230 size_t inFrameCount;
1231 int lSessionId;
1232
1233 // check calling permissions
1234 if (!recordingAllowed()) {
1235 lStatus = PERMISSION_DENIED;
1236 goto Exit;
1237 }
1238
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001239 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1240 ALOGE("openRecord() invalid format %d", format);
1241 lStatus = BAD_VALUE;
1242 goto Exit;
1243 }
1244
Mathias Agopian65ab4712010-07-14 17:59:35 -07001245 // add client to list
1246 { // scope for mLock
1247 Mutex::Autolock _l(mLock);
1248 thread = checkRecordThread_l(input);
1249 if (thread == NULL) {
1250 lStatus = BAD_VALUE;
1251 goto Exit;
1252 }
1253
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001254 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001255 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256
1257 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001258 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 lSessionId = *sessionId;
1260 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001261 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262 if (sessionId != NULL) {
1263 *sessionId = lSessionId;
1264 }
1265 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001266 // create new record track.
1267 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001268 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kastenddb0ccf2013-07-31 16:14:50 -07001269 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001270 }
Glenn Kastene198c362013-08-13 09:13:36 -07001271
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001272 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001273 // remove local strong reference to Client before deleting the RecordTrack so that the
1274 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275 client.clear();
1276 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277 goto Exit;
1278 }
1279
Glenn Kasten2fc14732013-08-05 14:58:14 -07001280 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001281 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001282
1283Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001284 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001285 return recordHandle;
1286}
1287
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001288
1289
Mathias Agopian65ab4712010-07-14 17:59:35 -07001290// ----------------------------------------------------------------------------
1291
Eric Laurenta4c5a552012-03-29 10:12:40 -07001292audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1293{
1294 if (!settingsAllowed()) {
1295 return 0;
1296 }
1297 Mutex::Autolock _l(mLock);
1298 return loadHwModule_l(name);
1299}
1300
1301// loadHwModule_l() must be called with AudioFlinger::mLock held
1302audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1303{
1304 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1305 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1306 ALOGW("loadHwModule() module %s already loaded", name);
1307 return mAudioHwDevs.keyAt(i);
1308 }
1309 }
1310
Eric Laurenta4c5a552012-03-29 10:12:40 -07001311 audio_hw_device_t *dev;
1312
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001313 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001314 if (rc) {
1315 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1316 return 0;
1317 }
1318
1319 mHardwareStatus = AUDIO_HW_INIT;
1320 rc = dev->init_check(dev);
1321 mHardwareStatus = AUDIO_HW_IDLE;
1322 if (rc) {
1323 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1324 return 0;
1325 }
1326
John Grossmanee578c02012-07-23 17:05:46 -07001327 // Check and cache this HAL's level of support for master mute and master
1328 // volume. If this is the first HAL opened, and it supports the get
1329 // methods, use the initial values provided by the HAL as the current
1330 // master mute and volume settings.
1331
1332 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1333 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001334 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001335
1336 if (0 == mAudioHwDevs.size()) {
1337 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1338 if (NULL != dev->get_master_volume) {
1339 float mv;
1340 if (OK == dev->get_master_volume(dev, &mv)) {
1341 mMasterVolume = mv;
1342 }
1343 }
1344
1345 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1346 if (NULL != dev->get_master_mute) {
1347 bool mm;
1348 if (OK == dev->get_master_mute(dev, &mm)) {
1349 mMasterMute = mm;
1350 }
1351 }
1352 }
1353
Eric Laurenta4c5a552012-03-29 10:12:40 -07001354 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001355 if ((NULL != dev->set_master_volume) &&
1356 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1357 flags = static_cast<AudioHwDevice::Flags>(flags |
1358 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1359 }
1360
1361 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1362 if ((NULL != dev->set_master_mute) &&
1363 (OK == dev->set_master_mute(dev, mMasterMute))) {
1364 flags = static_cast<AudioHwDevice::Flags>(flags |
1365 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1366 }
1367
Eric Laurenta4c5a552012-03-29 10:12:40 -07001368 mHardwareStatus = AUDIO_HW_IDLE;
1369 }
1370
1371 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001372 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001373
1374 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001375 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001376
1377 return handle;
1378
1379}
1380
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001381// ----------------------------------------------------------------------------
1382
Glenn Kasten3b16c762012-11-14 08:44:39 -08001383uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001384{
1385 Mutex::Autolock _l(mLock);
1386 PlaybackThread *thread = primaryPlaybackThread_l();
1387 return thread != NULL ? thread->sampleRate() : 0;
1388}
1389
Glenn Kastene33054e2012-11-14 12:54:39 -08001390size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001391{
1392 Mutex::Autolock _l(mLock);
1393 PlaybackThread *thread = primaryPlaybackThread_l();
1394 return thread != NULL ? thread->frameCountHAL() : 0;
1395}
1396
1397// ----------------------------------------------------------------------------
1398
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001399status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1400{
1401 uid_t uid = IPCThreadState::self()->getCallingUid();
1402 if (uid != AID_SYSTEM) {
1403 return PERMISSION_DENIED;
1404 }
1405 Mutex::Autolock _l(mLock);
1406 if (mIsDeviceTypeKnown) {
1407 return INVALID_OPERATION;
1408 }
1409 mIsLowRamDevice = isLowRamDevice;
1410 mIsDeviceTypeKnown = true;
1411 return NO_ERROR;
1412}
1413
1414// ----------------------------------------------------------------------------
1415
Eric Laurenta4c5a552012-03-29 10:12:40 -07001416audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1417 audio_devices_t *pDevices,
1418 uint32_t *pSamplingRate,
1419 audio_format_t *pFormat,
1420 audio_channel_mask_t *pChannelMask,
1421 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001422 audio_output_flags_t flags,
1423 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001425 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001426 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001427 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1428 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1429 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001430 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001431 config.offload_info = *offloadInfo;
1432 }
1433
Eric Laurentbfb1b832013-01-07 09:53:42 -08001434 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001435 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001436 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001437 config.sample_rate,
1438 config.format,
1439 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001440 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001441 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001442 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001443
1444 if (pDevices == NULL || *pDevices == 0) {
1445 return 0;
1446 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001447
Mathias Agopian65ab4712010-07-14 17:59:35 -07001448 Mutex::Autolock _l(mLock);
1449
Glenn Kasten32550952013-08-06 10:45:10 -07001450 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001451 if (outHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001452 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001453 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001454
John Grossmanee578c02012-07-23 17:05:46 -07001455 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001456 audio_io_handle_t id = nextUniqueId();
1457
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001458 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001459
Glenn Kasten32550952013-08-06 10:45:10 -07001460 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001461 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001462 id,
1463 *pDevices,
1464 (audio_output_flags_t)flags,
1465 &config,
1466 &outStream);
1467
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001468 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001469 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001470 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001471 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001472 config.sample_rate,
1473 config.format,
1474 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001475 status);
1476
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001477 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001478 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001479
Glenn Kasten32550952013-08-06 10:45:10 -07001480 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001481 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1482 thread = new OffloadThread(this, output, id, *pDevices);
1483 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1484 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001485 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1486 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001488 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001489 } else {
1490 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001491 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492 }
1493 mPlaybackThreads.add(id, thread);
1494
Glenn Kasten7c027242012-12-26 14:43:16 -08001495 if (pSamplingRate != NULL) {
1496 *pSamplingRate = config.sample_rate;
1497 }
1498 if (pFormat != NULL) {
1499 *pFormat = config.format;
1500 }
1501 if (pChannelMask != NULL) {
1502 *pChannelMask = config.channel_mask;
1503 }
1504 if (pLatencyMs != NULL) {
1505 *pLatencyMs = thread->latency();
1506 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001507
1508 // notify client processes of the new output creation
1509 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001510
1511 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001512 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001513 ALOGI("Using module %d has the primary audio interface", module);
1514 mPrimaryHardwareDev = outHwDev;
1515
1516 AutoMutex lock(mHardwareLock);
1517 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001518 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001519 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001520 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521 return id;
1522 }
1523
1524 return 0;
1525}
1526
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001527audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1528 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529{
1530 Mutex::Autolock _l(mLock);
1531 MixerThread *thread1 = checkMixerThread_l(output1);
1532 MixerThread *thread2 = checkMixerThread_l(output2);
1533
1534 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001535 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1536 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001537 return 0;
1538 }
1539
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001540 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001541 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1542 thread->addOutputTrack(thread2);
1543 mPlaybackThreads.add(id, thread);
1544 // notify client processes of the new output creation
1545 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1546 return id;
1547}
1548
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001549status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001550{
Glenn Kastend96c5722012-04-25 13:44:49 -07001551 return closeOutput_nonvirtual(output);
1552}
1553
1554status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1555{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556 // keep strong reference on the playback thread so that
1557 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001558 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001559 {
1560 Mutex::Autolock _l(mLock);
1561 thread = checkPlaybackThread_l(output);
1562 if (thread == NULL) {
1563 return BAD_VALUE;
1564 }
1565
Steve Block3856b092011-10-20 11:56:00 +01001566 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001567
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001568 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001570 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001571 DuplicatingThread *dupThread =
1572 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001573 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001574
1575 }
1576 }
1577 }
1578
1579
1580 mPlaybackThreads.removeItem(output);
1581 // save all effects to the default thread
1582 if (mPlaybackThreads.size()) {
1583 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1584 if (dstThread != NULL) {
1585 // audioflinger lock is held here so the acquisition order of thread locks does not
1586 // matter
1587 Mutex::Autolock _dl(dstThread->mLock);
1588 Mutex::Autolock _sl(thread->mLock);
1589 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1590 for (size_t i = 0; i < effectChains.size(); i ++) {
1591 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001592 }
1593 }
1594 }
Glenn Kastena1117922012-01-26 10:53:32 -08001595 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001596 }
1597 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001598 // The thread entity (active unit of execution) is no longer running here,
1599 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001601 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001602 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001603 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001604 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001605 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001606 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001607 }
1608 return NO_ERROR;
1609}
1610
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001611status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001612{
1613 Mutex::Autolock _l(mLock);
1614 PlaybackThread *thread = checkPlaybackThread_l(output);
1615
1616 if (thread == NULL) {
1617 return BAD_VALUE;
1618 }
1619
Steve Block3856b092011-10-20 11:56:00 +01001620 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001621 thread->suspend();
1622
1623 return NO_ERROR;
1624}
1625
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001626status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001627{
1628 Mutex::Autolock _l(mLock);
1629 PlaybackThread *thread = checkPlaybackThread_l(output);
1630
1631 if (thread == NULL) {
1632 return BAD_VALUE;
1633 }
1634
Steve Block3856b092011-10-20 11:56:00 +01001635 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001636
1637 thread->restore();
1638
1639 return NO_ERROR;
1640}
1641
Eric Laurenta4c5a552012-03-29 10:12:40 -07001642audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1643 audio_devices_t *pDevices,
1644 uint32_t *pSamplingRate,
1645 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001646 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001647{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001648 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001649 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001650 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1651 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1652 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1653
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001654 uint32_t reqSamplingRate = config.sample_rate;
1655 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001656 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001657
1658 if (pDevices == NULL || *pDevices == 0) {
1659 return 0;
1660 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001661
Mathias Agopian65ab4712010-07-14 17:59:35 -07001662 Mutex::Autolock _l(mLock);
1663
Glenn Kasten32550952013-08-06 10:45:10 -07001664 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001665 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001666 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001667 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001668
John Grossmanee578c02012-07-23 17:05:46 -07001669 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001670 audio_io_handle_t id = nextUniqueId();
1671
Glenn Kasten32550952013-08-06 10:45:10 -07001672 audio_stream_in_t *inStream = NULL;
1673 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001674 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001675 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1676 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001677 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001678 config.sample_rate,
1679 config.format,
1680 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001681 status);
1682
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001683 // If the input could not be opened with the requested parameters and we can handle the
1684 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1685 // 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 -07001686 if (status == BAD_VALUE &&
1687 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1688 (config.sample_rate <= 2 * reqSamplingRate) &&
Glenn Kastenf506e942013-08-06 10:49:43 -07001689 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001690 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001691 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001692 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001693 }
1694
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001695 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001696
Glenn Kasten46909e72013-02-26 09:20:22 -08001697#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001698 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1699 // or (re-)create if current Pipe is idle and does not match the new format
1700 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001701 enum {
1702 TEE_SINK_NO, // don't copy input
1703 TEE_SINK_NEW, // copy input using a new pipe
1704 TEE_SINK_OLD, // copy input using an existing pipe
1705 } kind;
1706 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1707 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001708 if (!mTeeSinkInputEnabled) {
1709 kind = TEE_SINK_NO;
1710 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001711 kind = TEE_SINK_NO;
1712 } else if (mRecordTeeSink == 0) {
1713 kind = TEE_SINK_NEW;
1714 } else if (mRecordTeeSink->getStrongCount() != 1) {
1715 kind = TEE_SINK_NO;
1716 } else if (format == mRecordTeeSink->format()) {
1717 kind = TEE_SINK_OLD;
1718 } else {
1719 kind = TEE_SINK_NEW;
1720 }
1721 switch (kind) {
1722 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001723 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001724 size_t numCounterOffers = 0;
1725 const NBAIO_Format offers[1] = {format};
1726 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1727 ALOG_ASSERT(index == 0);
1728 PipeReader *pipeReader = new PipeReader(*pipe);
1729 numCounterOffers = 0;
1730 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1731 ALOG_ASSERT(index == 0);
1732 mRecordTeeSink = pipe;
1733 mRecordTeeSource = pipeReader;
1734 teeSink = pipe;
1735 }
1736 break;
1737 case TEE_SINK_OLD:
1738 teeSink = mRecordTeeSink;
1739 break;
1740 case TEE_SINK_NO:
1741 default:
1742 break;
1743 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001744#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001745
Dima Zavin799a70e2011-04-18 16:57:27 -07001746 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1747
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001748 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001749 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001750 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001751 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001752 input,
1753 reqSamplingRate,
Glenn Kastenf506e942013-08-06 10:49:43 -07001754 reqChannelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001755 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001756 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001757 *pDevices
1758#ifdef TEE_SINK
1759 , teeSink
1760#endif
1761 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001762 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001763 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001764 if (pSamplingRate != NULL) {
1765 *pSamplingRate = reqSamplingRate;
1766 }
1767 if (pFormat != NULL) {
1768 *pFormat = config.format;
1769 }
1770 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001771 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001772 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001773
Mathias Agopian65ab4712010-07-14 17:59:35 -07001774 // notify client processes of the new input creation
1775 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1776 return id;
1777 }
1778
1779 return 0;
1780}
1781
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001782status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001783{
Glenn Kastend96c5722012-04-25 13:44:49 -07001784 return closeInput_nonvirtual(input);
1785}
1786
1787status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1788{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001789 // keep strong reference on the record thread so that
1790 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001791 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001792 {
1793 Mutex::Autolock _l(mLock);
1794 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001795 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001796 return BAD_VALUE;
1797 }
1798
Steve Block3856b092011-10-20 11:56:00 +01001799 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001800 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801 mRecordThreads.removeItem(input);
1802 }
1803 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001804 // The thread entity (active unit of execution) is no longer running here,
1805 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001806
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001807 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001808 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001809 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001810 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001811 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001812
1813 return NO_ERROR;
1814}
1815
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001816status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001817{
1818 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001819 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001820
1821 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1822 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001823 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001824 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001825
1826 return NO_ERROR;
1827}
1828
1829
1830int AudioFlinger::newAudioSessionId()
1831{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001832 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001833}
1834
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001835void AudioFlinger::acquireAudioSessionId(int audioSession)
1836{
1837 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001838 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001839 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001840 size_t num = mAudioSessionRefs.size();
1841 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001842 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001843 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1844 ref->mCnt++;
1845 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001846 return;
1847 }
1848 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001849 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1850 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001851}
1852
1853void AudioFlinger::releaseAudioSessionId(int audioSession)
1854{
1855 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001856 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001857 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001858 size_t num = mAudioSessionRefs.size();
1859 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001860 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001861 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1862 ref->mCnt--;
1863 ALOGV(" decremented refcount to %d", ref->mCnt);
1864 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001865 mAudioSessionRefs.removeAt(i);
1866 delete ref;
1867 purgeStaleEffects_l();
1868 }
1869 return;
1870 }
1871 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001872 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001873}
1874
1875void AudioFlinger::purgeStaleEffects_l() {
1876
Steve Block3856b092011-10-20 11:56:00 +01001877 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001878
1879 Vector< sp<EffectChain> > chains;
1880
1881 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1882 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1883 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1884 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001885 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1886 chains.push(ec);
1887 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001888 }
1889 }
1890 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1891 sp<RecordThread> t = mRecordThreads.valueAt(i);
1892 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1893 sp<EffectChain> ec = t->mEffectChains[j];
1894 chains.push(ec);
1895 }
1896 }
1897
1898 for (size_t i = 0; i < chains.size(); i++) {
1899 sp<EffectChain> ec = chains[i];
1900 int sessionid = ec->sessionId();
1901 sp<ThreadBase> t = ec->mThread.promote();
1902 if (t == 0) {
1903 continue;
1904 }
1905 size_t numsessionrefs = mAudioSessionRefs.size();
1906 bool found = false;
1907 for (size_t k = 0; k < numsessionrefs; k++) {
1908 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001909 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001910 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001911 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001912 found = true;
1913 break;
1914 }
1915 }
1916 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07001917 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001918 // remove all effects from the chain
1919 while (ec->mEffects.size()) {
1920 sp<EffectModule> effect = ec->mEffects[0];
1921 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001922 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001923 if (effect->purgeHandles()) {
1924 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001925 }
1926 AudioSystem::unregisterEffect(effect->id());
1927 }
1928 }
1929 }
1930 return;
1931}
1932
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001934AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935{
Glenn Kastena1117922012-01-26 10:53:32 -08001936 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937}
1938
1939// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001940AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941{
1942 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001943 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944}
1945
1946// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001947AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948{
Glenn Kastena1117922012-01-26 10:53:32 -08001949 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950}
1951
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001952uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001953{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001954 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955}
1956
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001957AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001958{
1959 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1960 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001961 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001962 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001963 return thread;
1964 }
1965 }
1966 return NULL;
1967}
1968
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001969audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001970{
1971 PlaybackThread *thread = primaryPlaybackThread_l();
1972
1973 if (thread == NULL) {
1974 return 0;
1975 }
1976
Eric Laurentf1c04f92012-08-28 14:26:53 -07001977 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001978}
1979
Eric Laurenta011e352012-03-29 15:51:43 -07001980sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1981 int triggerSession,
1982 int listenerSession,
1983 sync_event_callback_t callBack,
1984 void *cookie)
1985{
1986 Mutex::Autolock _l(mLock);
1987
1988 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1989 status_t playStatus = NAME_NOT_FOUND;
1990 status_t recStatus = NAME_NOT_FOUND;
1991 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1992 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1993 if (playStatus == NO_ERROR) {
1994 return event;
1995 }
1996 }
1997 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1998 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1999 if (recStatus == NO_ERROR) {
2000 return event;
2001 }
2002 }
2003 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2004 mPendingSyncEvents.add(event);
2005 } else {
2006 ALOGV("createSyncEvent() invalid event %d", event->type());
2007 event.clear();
2008 }
2009 return event;
2010}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002011
Mathias Agopian65ab4712010-07-14 17:59:35 -07002012// ----------------------------------------------------------------------------
2013// Effect management
2014// ----------------------------------------------------------------------------
2015
2016
Glenn Kastenf587ba52012-01-26 16:25:10 -08002017status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002018{
2019 Mutex::Autolock _l(mLock);
2020 return EffectQueryNumberEffects(numEffects);
2021}
2022
Glenn Kastenf587ba52012-01-26 16:25:10 -08002023status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024{
2025 Mutex::Autolock _l(mLock);
2026 return EffectQueryEffect(index, descriptor);
2027}
2028
Glenn Kasten5e92a782012-01-30 07:40:52 -08002029status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002030 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002031{
2032 Mutex::Autolock _l(mLock);
2033 return EffectGetDescriptor(pUuid, descriptor);
2034}
2035
2036
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002037sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002038 effect_descriptor_t *pDesc,
2039 const sp<IEffectClient>& effectClient,
2040 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002041 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002042 int sessionId,
2043 status_t *status,
2044 int *id,
2045 int *enabled)
2046{
2047 status_t lStatus = NO_ERROR;
2048 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002049 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002050
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002051 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002052 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002053 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002054
2055 if (pDesc == NULL) {
2056 lStatus = BAD_VALUE;
2057 goto Exit;
2058 }
2059
Eric Laurent84e9a102010-09-23 16:10:16 -07002060 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002061 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002062 lStatus = PERMISSION_DENIED;
2063 goto Exit;
2064 }
2065
Dima Zavinfce7a472011-04-19 22:30:36 -07002066 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002067 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002068 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002069 lStatus = PERMISSION_DENIED;
2070 goto Exit;
2071 }
2072
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002073 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002074 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002075 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07002076 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07002077 lStatus = BAD_VALUE;
2078 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07002079 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002080 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002081 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002082 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002083 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002084 }
2085 }
2086
Mathias Agopian65ab4712010-07-14 17:59:35 -07002087 {
2088 Mutex::Autolock _l(mLock);
2089
Mathias Agopian65ab4712010-07-14 17:59:35 -07002090
2091 if (!EffectIsNullUuid(&pDesc->uuid)) {
2092 // if uuid is specified, request effect descriptor
2093 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2094 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002095 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002096 goto Exit;
2097 }
2098 } else {
2099 // if uuid is not specified, look for an available implementation
2100 // of the required type in effect factory
2101 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002102 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002103 lStatus = BAD_VALUE;
2104 goto Exit;
2105 }
2106 uint32_t numEffects = 0;
2107 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002108 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002109 bool found = false;
2110
2111 lStatus = EffectQueryNumberEffects(&numEffects);
2112 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002113 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002114 goto Exit;
2115 }
2116 for (uint32_t i = 0; i < numEffects; i++) {
2117 lStatus = EffectQueryEffect(i, &desc);
2118 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002119 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002120 continue;
2121 }
2122 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2123 // If matching type found save effect descriptor. If the session is
2124 // 0 and the effect is not auxiliary, continue enumeration in case
2125 // an auxiliary version of this effect type is available
2126 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002127 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002128 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002129 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2130 break;
2131 }
2132 }
2133 }
2134 if (!found) {
2135 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002136 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002137 goto Exit;
2138 }
2139 // For same effect type, chose auxiliary version over insert version if
2140 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002141 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002142 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002143 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002144 }
2145 }
2146
2147 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002148 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002149 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2150 lStatus = INVALID_OPERATION;
2151 goto Exit;
2152 }
2153
Eric Laurent59255e42011-07-27 19:49:51 -07002154 // check recording permission for visualizer
2155 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2156 !recordingAllowed()) {
2157 lStatus = PERMISSION_DENIED;
2158 goto Exit;
2159 }
2160
Mathias Agopian65ab4712010-07-14 17:59:35 -07002161 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002162 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002163
2164 // If output is not specified try to find a matching audio session ID in one of the
2165 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002166 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2167 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002168 // Note: io is never 0 when creating an effect on an input
2169 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002170 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002171 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2172 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002173 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002174 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002175 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002176 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002177 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002178 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2179 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2180 io = mRecordThreads.keyAt(i);
2181 break;
2182 }
2183 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002184 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002185 // If no output thread contains the requested session ID, default to
2186 // first output. The effect chain will be moved to the correct output
2187 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002188 if (io == 0 && mPlaybackThreads.size()) {
2189 io = mPlaybackThreads.keyAt(0);
2190 }
Steve Block3856b092011-10-20 11:56:00 +01002191 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002192 }
2193 ThreadBase *thread = checkRecordThread_l(io);
2194 if (thread == NULL) {
2195 thread = checkPlaybackThread_l(io);
2196 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002197 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002198 lStatus = BAD_VALUE;
2199 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002200 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002201 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002202
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002203 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002204
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002205 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002206 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2207 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002208 if (handle != 0 && id != NULL) {
2209 *id = handle->id();
2210 }
2211 }
2212
2213Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002214 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002215 return handle;
2216}
2217
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002218status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2219 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002220{
Steve Block3856b092011-10-20 11:56:00 +01002221 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002222 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002223 Mutex::Autolock _l(mLock);
2224 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002225 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002226 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002227 }
Eric Laurentde070132010-07-13 04:45:46 -07002228 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2229 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002230 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002231 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002232 }
Eric Laurentde070132010-07-13 04:45:46 -07002233 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2234 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002235 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002236 return BAD_VALUE;
2237 }
2238
2239 Mutex::Autolock _dl(dstThread->mLock);
2240 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002241 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002242
Mathias Agopian65ab4712010-07-14 17:59:35 -07002243 return NO_ERROR;
2244}
2245
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002246// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002247status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002248 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002249 AudioFlinger::PlaybackThread *dstThread,
2250 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002251{
Steve Block3856b092011-10-20 11:56:00 +01002252 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002253 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002254
Eric Laurent59255e42011-07-27 19:49:51 -07002255 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002256 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002257 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002258 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002259 return INVALID_OPERATION;
2260 }
2261
Eric Laurent39e94f82010-07-28 01:32:47 -07002262 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002263 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002264 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002265 // removed.
2266 srcThread->removeEffectChain_l(chain);
2267
2268 // transfer all effects one by one so that new effect chain is created on new thread with
2269 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002270 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002271 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002272 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002273 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2274 while (effect != 0) {
2275 srcThread->removeEffect_l(effect);
2276 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002277 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2278 if (effect->state() == EffectModule::ACTIVE ||
2279 effect->state() == EffectModule::STOPPING) {
2280 effect->start();
2281 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002282 // if the move request is not received from audio policy manager, the effect must be
2283 // re-registered with the new strategy and output
2284 if (dstChain == 0) {
2285 dstChain = effect->chain().promote();
2286 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002287 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002288 srcThread->addEffect_l(effect);
2289 return NO_INIT;
2290 }
2291 strategy = dstChain->strategy();
2292 }
2293 if (reRegister) {
2294 AudioSystem::unregisterEffect(effect->id());
2295 AudioSystem::registerEffect(&effect->desc(),
2296 dstOutput,
2297 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002298 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002299 effect->id());
2300 }
Eric Laurentde070132010-07-13 04:45:46 -07002301 effect = chain->getEffectFromId_l(0);
2302 }
2303
2304 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002305}
2306
Glenn Kastenda6ef132013-01-10 12:31:01 -08002307struct Entry {
2308#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2309 char mName[MAX_NAME];
2310};
2311
2312int comparEntry(const void *p1, const void *p2)
2313{
2314 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2315}
2316
Glenn Kasten46909e72013-02-26 09:20:22 -08002317#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002318void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002319{
Eric Laurent81784c32012-11-19 14:55:58 -08002320 NBAIO_Source *teeSource = source.get();
2321 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002322 // .wav rotation
2323 // There is a benign race condition if 2 threads call this simultaneously.
2324 // They would both traverse the directory, but the result would simply be
2325 // failures at unlink() which are ignored. It's also unlikely since
2326 // normally dumpsys is only done by bugreport or from the command line.
2327 char teePath[32+256];
2328 strcpy(teePath, "/data/misc/media");
2329 size_t teePathLen = strlen(teePath);
2330 DIR *dir = opendir(teePath);
2331 teePath[teePathLen++] = '/';
2332 if (dir != NULL) {
2333#define MAX_SORT 20 // number of entries to sort
2334#define MAX_KEEP 10 // number of entries to keep
2335 struct Entry entries[MAX_SORT];
2336 size_t entryCount = 0;
2337 while (entryCount < MAX_SORT) {
2338 struct dirent de;
2339 struct dirent *result = NULL;
2340 int rc = readdir_r(dir, &de, &result);
2341 if (rc != 0) {
2342 ALOGW("readdir_r failed %d", rc);
2343 break;
2344 }
2345 if (result == NULL) {
2346 break;
2347 }
2348 if (result != &de) {
2349 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2350 break;
2351 }
2352 // ignore non .wav file entries
2353 size_t nameLen = strlen(de.d_name);
2354 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2355 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2356 continue;
2357 }
2358 strcpy(entries[entryCount++].mName, de.d_name);
2359 }
2360 (void) closedir(dir);
2361 if (entryCount > MAX_KEEP) {
2362 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2363 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2364 strcpy(&teePath[teePathLen], entries[i].mName);
2365 (void) unlink(teePath);
2366 }
2367 }
2368 } else {
2369 if (fd >= 0) {
2370 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2371 }
2372 }
Eric Laurent81784c32012-11-19 14:55:58 -08002373 char teeTime[16];
2374 struct timeval tv;
2375 gettimeofday(&tv, NULL);
2376 struct tm tm;
2377 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002378 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2379 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2380 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2381 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002382 if (teeFd >= 0) {
2383 char wavHeader[44];
2384 memcpy(wavHeader,
2385 "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",
2386 sizeof(wavHeader));
2387 NBAIO_Format format = teeSource->format();
2388 unsigned channelCount = Format_channelCount(format);
2389 ALOG_ASSERT(channelCount <= FCC_2);
2390 uint32_t sampleRate = Format_sampleRate(format);
2391 wavHeader[22] = channelCount; // number of channels
2392 wavHeader[24] = sampleRate; // sample rate
2393 wavHeader[25] = sampleRate >> 8;
2394 wavHeader[32] = channelCount * 2; // block alignment
2395 write(teeFd, wavHeader, sizeof(wavHeader));
2396 size_t total = 0;
2397 bool firstRead = true;
2398 for (;;) {
2399#define TEE_SINK_READ 1024
2400 short buffer[TEE_SINK_READ * FCC_2];
2401 size_t count = TEE_SINK_READ;
2402 ssize_t actual = teeSource->read(buffer, count,
2403 AudioBufferProvider::kInvalidPTS);
2404 bool wasFirstRead = firstRead;
2405 firstRead = false;
2406 if (actual <= 0) {
2407 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2408 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002409 }
Eric Laurent81784c32012-11-19 14:55:58 -08002410 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002411 }
Eric Laurent81784c32012-11-19 14:55:58 -08002412 ALOG_ASSERT(actual <= (ssize_t)count);
2413 write(teeFd, buffer, actual * channelCount * sizeof(short));
2414 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002415 }
Eric Laurent81784c32012-11-19 14:55:58 -08002416 lseek(teeFd, (off_t) 4, SEEK_SET);
2417 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2418 write(teeFd, &temp, sizeof(temp));
2419 lseek(teeFd, (off_t) 40, SEEK_SET);
2420 temp = total * channelCount * sizeof(short);
2421 write(teeFd, &temp, sizeof(temp));
2422 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002423 if (fd >= 0) {
2424 fdprintf(fd, "tee copied to %s\n", teePath);
2425 }
Eric Laurent59255e42011-07-27 19:49:51 -07002426 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002427 if (fd >= 0) {
2428 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2429 }
Eric Laurent59255e42011-07-27 19:49:51 -07002430 }
2431 }
2432}
Glenn Kasten46909e72013-02-26 09:20:22 -08002433#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002434
Mathias Agopian65ab4712010-07-14 17:59:35 -07002435// ----------------------------------------------------------------------------
2436
2437status_t AudioFlinger::onTransact(
2438 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2439{
2440 return BnAudioFlinger::onTransact(code, data, reply, flags);
2441}
2442
Mathias Agopian65ab4712010-07-14 17:59:35 -07002443}; // namespace android