blob: 2fc83cc8bb0713a9305548d60aa41ec07dd9ce73 [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{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001425 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001426 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001427 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001428 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1429 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1430 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001431 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001432 config.offload_info = *offloadInfo;
1433 }
1434
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001435 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001436 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437
Eric Laurentbfb1b832013-01-07 09:53:42 -08001438 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001439 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001440 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001441 config.sample_rate,
1442 config.format,
1443 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001444 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001445 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001446 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447
1448 if (pDevices == NULL || *pDevices == 0) {
1449 return 0;
1450 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001451
Mathias Agopian65ab4712010-07-14 17:59:35 -07001452 Mutex::Autolock _l(mLock);
1453
Eric Laurenta4c5a552012-03-29 10:12:40 -07001454 outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001455 if (outHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001456 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001457 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001458
John Grossmanee578c02012-07-23 17:05:46 -07001459 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001460 audio_io_handle_t id = nextUniqueId();
1461
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001462 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001463
Glenn Kasten34542ac2013-06-26 11:29:02 -07001464 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001465 id,
1466 *pDevices,
1467 (audio_output_flags_t)flags,
1468 &config,
1469 &outStream);
1470
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001471 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001472 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001473 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001474 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001475 config.sample_rate,
1476 config.format,
1477 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001478 status);
1479
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001480 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001481 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001482
Eric Laurentbfb1b832013-01-07 09:53:42 -08001483 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1484 thread = new OffloadThread(this, output, id, *pDevices);
1485 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1486 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001487 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1488 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001489 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001490 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001491 } else {
1492 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001493 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001494 }
1495 mPlaybackThreads.add(id, thread);
1496
Glenn Kasten7c027242012-12-26 14:43:16 -08001497 if (pSamplingRate != NULL) {
1498 *pSamplingRate = config.sample_rate;
1499 }
1500 if (pFormat != NULL) {
1501 *pFormat = config.format;
1502 }
1503 if (pChannelMask != NULL) {
1504 *pChannelMask = config.channel_mask;
1505 }
1506 if (pLatencyMs != NULL) {
1507 *pLatencyMs = thread->latency();
1508 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001509
1510 // notify client processes of the new output creation
1511 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001512
1513 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001514 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001515 ALOGI("Using module %d has the primary audio interface", module);
1516 mPrimaryHardwareDev = outHwDev;
1517
1518 AutoMutex lock(mHardwareLock);
1519 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001520 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001521 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001522 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001523 return id;
1524 }
1525
1526 return 0;
1527}
1528
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001529audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1530 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001531{
1532 Mutex::Autolock _l(mLock);
1533 MixerThread *thread1 = checkMixerThread_l(output1);
1534 MixerThread *thread2 = checkMixerThread_l(output2);
1535
1536 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001537 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1538 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001539 return 0;
1540 }
1541
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001542 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1544 thread->addOutputTrack(thread2);
1545 mPlaybackThreads.add(id, thread);
1546 // notify client processes of the new output creation
1547 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1548 return id;
1549}
1550
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001551status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001552{
Glenn Kastend96c5722012-04-25 13:44:49 -07001553 return closeOutput_nonvirtual(output);
1554}
1555
1556status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1557{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001558 // keep strong reference on the playback thread so that
1559 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001560 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001561 {
1562 Mutex::Autolock _l(mLock);
1563 thread = checkPlaybackThread_l(output);
1564 if (thread == NULL) {
1565 return BAD_VALUE;
1566 }
1567
Steve Block3856b092011-10-20 11:56:00 +01001568 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001570 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001571 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001572 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001573 DuplicatingThread *dupThread =
1574 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001575 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001576
1577 }
1578 }
1579 }
1580
1581
1582 mPlaybackThreads.removeItem(output);
1583 // save all effects to the default thread
1584 if (mPlaybackThreads.size()) {
1585 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1586 if (dstThread != NULL) {
1587 // audioflinger lock is held here so the acquisition order of thread locks does not
1588 // matter
1589 Mutex::Autolock _dl(dstThread->mLock);
1590 Mutex::Autolock _sl(thread->mLock);
1591 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1592 for (size_t i = 0; i < effectChains.size(); i ++) {
1593 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594 }
1595 }
1596 }
Glenn Kastena1117922012-01-26 10:53:32 -08001597 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001598 }
1599 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001600 // The thread entity (active unit of execution) is no longer running here,
1601 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001603 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001604 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001605 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001606 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001607 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001608 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609 }
1610 return NO_ERROR;
1611}
1612
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001613status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614{
1615 Mutex::Autolock _l(mLock);
1616 PlaybackThread *thread = checkPlaybackThread_l(output);
1617
1618 if (thread == NULL) {
1619 return BAD_VALUE;
1620 }
1621
Steve Block3856b092011-10-20 11:56:00 +01001622 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001623 thread->suspend();
1624
1625 return NO_ERROR;
1626}
1627
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001628status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001629{
1630 Mutex::Autolock _l(mLock);
1631 PlaybackThread *thread = checkPlaybackThread_l(output);
1632
1633 if (thread == NULL) {
1634 return BAD_VALUE;
1635 }
1636
Steve Block3856b092011-10-20 11:56:00 +01001637 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001638
1639 thread->restore();
1640
1641 return NO_ERROR;
1642}
1643
Eric Laurenta4c5a552012-03-29 10:12:40 -07001644audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1645 audio_devices_t *pDevices,
1646 uint32_t *pSamplingRate,
1647 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001648 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001649{
1650 status_t status;
1651 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001652 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001653 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001654 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1655 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1656 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1657
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001658 uint32_t reqSamplingRate = config.sample_rate;
1659 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001660 audio_channel_mask_t reqChannelMask = config.channel_mask;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001661 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001662 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001663
1664 if (pDevices == NULL || *pDevices == 0) {
1665 return 0;
1666 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001667
Mathias Agopian65ab4712010-07-14 17:59:35 -07001668 Mutex::Autolock _l(mLock);
1669
Eric Laurenta4c5a552012-03-29 10:12:40 -07001670 inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001671 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001672 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001673 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001674
John Grossmanee578c02012-07-23 17:05:46 -07001675 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001676 audio_io_handle_t id = nextUniqueId();
1677
John Grossmanee578c02012-07-23 17:05:46 -07001678 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001679 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001680 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1681 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001682 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001683 config.sample_rate,
1684 config.format,
1685 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001686 status);
1687
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001688 // If the input could not be opened with the requested parameters and we can handle the
1689 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1690 // 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 -07001691 if (status == BAD_VALUE &&
1692 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1693 (config.sample_rate <= 2 * reqSamplingRate) &&
Glenn Kastenf506e942013-08-06 10:49:43 -07001694 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001695 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001696 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001697 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001698 }
1699
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001700 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001701
Glenn Kasten46909e72013-02-26 09:20:22 -08001702#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001703 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1704 // or (re-)create if current Pipe is idle and does not match the new format
1705 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001706 enum {
1707 TEE_SINK_NO, // don't copy input
1708 TEE_SINK_NEW, // copy input using a new pipe
1709 TEE_SINK_OLD, // copy input using an existing pipe
1710 } kind;
1711 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1712 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001713 if (!mTeeSinkInputEnabled) {
1714 kind = TEE_SINK_NO;
1715 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001716 kind = TEE_SINK_NO;
1717 } else if (mRecordTeeSink == 0) {
1718 kind = TEE_SINK_NEW;
1719 } else if (mRecordTeeSink->getStrongCount() != 1) {
1720 kind = TEE_SINK_NO;
1721 } else if (format == mRecordTeeSink->format()) {
1722 kind = TEE_SINK_OLD;
1723 } else {
1724 kind = TEE_SINK_NEW;
1725 }
1726 switch (kind) {
1727 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001728 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001729 size_t numCounterOffers = 0;
1730 const NBAIO_Format offers[1] = {format};
1731 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1732 ALOG_ASSERT(index == 0);
1733 PipeReader *pipeReader = new PipeReader(*pipe);
1734 numCounterOffers = 0;
1735 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1736 ALOG_ASSERT(index == 0);
1737 mRecordTeeSink = pipe;
1738 mRecordTeeSource = pipeReader;
1739 teeSink = pipe;
1740 }
1741 break;
1742 case TEE_SINK_OLD:
1743 teeSink = mRecordTeeSink;
1744 break;
1745 case TEE_SINK_NO:
1746 default:
1747 break;
1748 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001749#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001750
Dima Zavin799a70e2011-04-18 16:57:27 -07001751 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1752
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001753 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001754 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001755 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001756 thread = new RecordThread(this,
1757 input,
1758 reqSamplingRate,
Glenn Kastenf506e942013-08-06 10:49:43 -07001759 reqChannelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001760 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001761 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001762 *pDevices
1763#ifdef TEE_SINK
1764 , teeSink
1765#endif
1766 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001767 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001768 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001769 if (pSamplingRate != NULL) {
1770 *pSamplingRate = reqSamplingRate;
1771 }
1772 if (pFormat != NULL) {
1773 *pFormat = config.format;
1774 }
1775 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001776 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001777 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001778
Mathias Agopian65ab4712010-07-14 17:59:35 -07001779 // notify client processes of the new input creation
1780 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1781 return id;
1782 }
1783
1784 return 0;
1785}
1786
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001787status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001788{
Glenn Kastend96c5722012-04-25 13:44:49 -07001789 return closeInput_nonvirtual(input);
1790}
1791
1792status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1793{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001794 // keep strong reference on the record thread so that
1795 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001796 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001797 {
1798 Mutex::Autolock _l(mLock);
1799 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001800 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801 return BAD_VALUE;
1802 }
1803
Steve Block3856b092011-10-20 11:56:00 +01001804 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001805 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001806 mRecordThreads.removeItem(input);
1807 }
1808 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001809 // The thread entity (active unit of execution) is no longer running here,
1810 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001811
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001812 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001813 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001814 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001815 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001816 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001817
1818 return NO_ERROR;
1819}
1820
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001821status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001822{
1823 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001824 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001825
1826 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1827 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001828 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001829 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001830
1831 return NO_ERROR;
1832}
1833
1834
1835int AudioFlinger::newAudioSessionId()
1836{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001837 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001838}
1839
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001840void AudioFlinger::acquireAudioSessionId(int audioSession)
1841{
1842 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001843 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001844 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001845 size_t num = mAudioSessionRefs.size();
1846 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001847 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001848 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1849 ref->mCnt++;
1850 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001851 return;
1852 }
1853 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001854 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1855 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001856}
1857
1858void AudioFlinger::releaseAudioSessionId(int audioSession)
1859{
1860 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001861 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001862 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001863 size_t num = mAudioSessionRefs.size();
1864 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001865 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001866 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1867 ref->mCnt--;
1868 ALOGV(" decremented refcount to %d", ref->mCnt);
1869 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001870 mAudioSessionRefs.removeAt(i);
1871 delete ref;
1872 purgeStaleEffects_l();
1873 }
1874 return;
1875 }
1876 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001877 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001878}
1879
1880void AudioFlinger::purgeStaleEffects_l() {
1881
Steve Block3856b092011-10-20 11:56:00 +01001882 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001883
1884 Vector< sp<EffectChain> > chains;
1885
1886 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1887 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1888 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1889 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001890 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1891 chains.push(ec);
1892 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001893 }
1894 }
1895 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1896 sp<RecordThread> t = mRecordThreads.valueAt(i);
1897 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1898 sp<EffectChain> ec = t->mEffectChains[j];
1899 chains.push(ec);
1900 }
1901 }
1902
1903 for (size_t i = 0; i < chains.size(); i++) {
1904 sp<EffectChain> ec = chains[i];
1905 int sessionid = ec->sessionId();
1906 sp<ThreadBase> t = ec->mThread.promote();
1907 if (t == 0) {
1908 continue;
1909 }
1910 size_t numsessionrefs = mAudioSessionRefs.size();
1911 bool found = false;
1912 for (size_t k = 0; k < numsessionrefs; k++) {
1913 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001914 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001915 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001916 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001917 found = true;
1918 break;
1919 }
1920 }
1921 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07001922 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001923 // remove all effects from the chain
1924 while (ec->mEffects.size()) {
1925 sp<EffectModule> effect = ec->mEffects[0];
1926 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001927 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001928 if (effect->purgeHandles()) {
1929 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001930 }
1931 AudioSystem::unregisterEffect(effect->id());
1932 }
1933 }
1934 }
1935 return;
1936}
1937
Mathias Agopian65ab4712010-07-14 17:59:35 -07001938// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001939AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001940{
Glenn Kastena1117922012-01-26 10:53:32 -08001941 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001942}
1943
1944// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001945AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001946{
1947 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001948 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001949}
1950
1951// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001952AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001953{
Glenn Kastena1117922012-01-26 10:53:32 -08001954 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955}
1956
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001957uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001958{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001959 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001960}
1961
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001962AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001963{
1964 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1965 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001966 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001967 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001968 return thread;
1969 }
1970 }
1971 return NULL;
1972}
1973
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001974audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001975{
1976 PlaybackThread *thread = primaryPlaybackThread_l();
1977
1978 if (thread == NULL) {
1979 return 0;
1980 }
1981
Eric Laurentf1c04f92012-08-28 14:26:53 -07001982 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001983}
1984
Eric Laurenta011e352012-03-29 15:51:43 -07001985sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1986 int triggerSession,
1987 int listenerSession,
1988 sync_event_callback_t callBack,
1989 void *cookie)
1990{
1991 Mutex::Autolock _l(mLock);
1992
1993 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1994 status_t playStatus = NAME_NOT_FOUND;
1995 status_t recStatus = NAME_NOT_FOUND;
1996 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1997 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1998 if (playStatus == NO_ERROR) {
1999 return event;
2000 }
2001 }
2002 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2003 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2004 if (recStatus == NO_ERROR) {
2005 return event;
2006 }
2007 }
2008 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2009 mPendingSyncEvents.add(event);
2010 } else {
2011 ALOGV("createSyncEvent() invalid event %d", event->type());
2012 event.clear();
2013 }
2014 return event;
2015}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002016
Mathias Agopian65ab4712010-07-14 17:59:35 -07002017// ----------------------------------------------------------------------------
2018// Effect management
2019// ----------------------------------------------------------------------------
2020
2021
Glenn Kastenf587ba52012-01-26 16:25:10 -08002022status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002023{
2024 Mutex::Autolock _l(mLock);
2025 return EffectQueryNumberEffects(numEffects);
2026}
2027
Glenn Kastenf587ba52012-01-26 16:25:10 -08002028status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002029{
2030 Mutex::Autolock _l(mLock);
2031 return EffectQueryEffect(index, descriptor);
2032}
2033
Glenn Kasten5e92a782012-01-30 07:40:52 -08002034status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002035 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002036{
2037 Mutex::Autolock _l(mLock);
2038 return EffectGetDescriptor(pUuid, descriptor);
2039}
2040
2041
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002042sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002043 effect_descriptor_t *pDesc,
2044 const sp<IEffectClient>& effectClient,
2045 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002046 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002047 int sessionId,
2048 status_t *status,
2049 int *id,
2050 int *enabled)
2051{
2052 status_t lStatus = NO_ERROR;
2053 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002054 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002055
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002056 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002057 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002058 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002059
2060 if (pDesc == NULL) {
2061 lStatus = BAD_VALUE;
2062 goto Exit;
2063 }
2064
Eric Laurent84e9a102010-09-23 16:10:16 -07002065 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002066 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002067 lStatus = PERMISSION_DENIED;
2068 goto Exit;
2069 }
2070
Dima Zavinfce7a472011-04-19 22:30:36 -07002071 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002072 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002073 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002074 lStatus = PERMISSION_DENIED;
2075 goto Exit;
2076 }
2077
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002078 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002079 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002080 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07002081 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07002082 lStatus = BAD_VALUE;
2083 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07002084 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002085 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002086 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002087 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002088 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002089 }
2090 }
2091
Mathias Agopian65ab4712010-07-14 17:59:35 -07002092 {
2093 Mutex::Autolock _l(mLock);
2094
Mathias Agopian65ab4712010-07-14 17:59:35 -07002095
2096 if (!EffectIsNullUuid(&pDesc->uuid)) {
2097 // if uuid is specified, request effect descriptor
2098 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2099 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002100 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002101 goto Exit;
2102 }
2103 } else {
2104 // if uuid is not specified, look for an available implementation
2105 // of the required type in effect factory
2106 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002107 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002108 lStatus = BAD_VALUE;
2109 goto Exit;
2110 }
2111 uint32_t numEffects = 0;
2112 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002113 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002114 bool found = false;
2115
2116 lStatus = EffectQueryNumberEffects(&numEffects);
2117 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002118 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002119 goto Exit;
2120 }
2121 for (uint32_t i = 0; i < numEffects; i++) {
2122 lStatus = EffectQueryEffect(i, &desc);
2123 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002124 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002125 continue;
2126 }
2127 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2128 // If matching type found save effect descriptor. If the session is
2129 // 0 and the effect is not auxiliary, continue enumeration in case
2130 // an auxiliary version of this effect type is available
2131 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002132 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002133 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002134 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2135 break;
2136 }
2137 }
2138 }
2139 if (!found) {
2140 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002141 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002142 goto Exit;
2143 }
2144 // For same effect type, chose auxiliary version over insert version if
2145 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002146 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002147 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002148 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002149 }
2150 }
2151
2152 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002153 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002154 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2155 lStatus = INVALID_OPERATION;
2156 goto Exit;
2157 }
2158
Eric Laurent59255e42011-07-27 19:49:51 -07002159 // check recording permission for visualizer
2160 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2161 !recordingAllowed()) {
2162 lStatus = PERMISSION_DENIED;
2163 goto Exit;
2164 }
2165
Mathias Agopian65ab4712010-07-14 17:59:35 -07002166 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002167 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002168
2169 // If output is not specified try to find a matching audio session ID in one of the
2170 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002171 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2172 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002173 // Note: io is never 0 when creating an effect on an input
2174 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002175 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002176 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2177 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002178 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002179 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002180 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002181 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002182 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002183 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2184 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2185 io = mRecordThreads.keyAt(i);
2186 break;
2187 }
2188 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002189 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002190 // If no output thread contains the requested session ID, default to
2191 // first output. The effect chain will be moved to the correct output
2192 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002193 if (io == 0 && mPlaybackThreads.size()) {
2194 io = mPlaybackThreads.keyAt(0);
2195 }
Steve Block3856b092011-10-20 11:56:00 +01002196 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002197 }
2198 ThreadBase *thread = checkRecordThread_l(io);
2199 if (thread == NULL) {
2200 thread = checkPlaybackThread_l(io);
2201 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002202 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002203 lStatus = BAD_VALUE;
2204 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002205 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002206 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002207
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002208 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002209
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002210 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002211 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2212 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002213 if (handle != 0 && id != NULL) {
2214 *id = handle->id();
2215 }
2216 }
2217
2218Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002219 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002220 return handle;
2221}
2222
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002223status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2224 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002225{
Steve Block3856b092011-10-20 11:56:00 +01002226 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002227 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002228 Mutex::Autolock _l(mLock);
2229 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002230 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002231 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002232 }
Eric Laurentde070132010-07-13 04:45:46 -07002233 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2234 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002235 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002236 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237 }
Eric Laurentde070132010-07-13 04:45:46 -07002238 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2239 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002240 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002241 return BAD_VALUE;
2242 }
2243
2244 Mutex::Autolock _dl(dstThread->mLock);
2245 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002246 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002247
Mathias Agopian65ab4712010-07-14 17:59:35 -07002248 return NO_ERROR;
2249}
2250
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002251// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002252status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002253 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002254 AudioFlinger::PlaybackThread *dstThread,
2255 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002256{
Steve Block3856b092011-10-20 11:56:00 +01002257 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002258 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002259
Eric Laurent59255e42011-07-27 19:49:51 -07002260 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002261 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002262 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002263 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002264 return INVALID_OPERATION;
2265 }
2266
Eric Laurent39e94f82010-07-28 01:32:47 -07002267 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002268 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002269 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002270 // removed.
2271 srcThread->removeEffectChain_l(chain);
2272
2273 // transfer all effects one by one so that new effect chain is created on new thread with
2274 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002275 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002276 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002277 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002278 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2279 while (effect != 0) {
2280 srcThread->removeEffect_l(effect);
2281 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002282 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2283 if (effect->state() == EffectModule::ACTIVE ||
2284 effect->state() == EffectModule::STOPPING) {
2285 effect->start();
2286 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002287 // if the move request is not received from audio policy manager, the effect must be
2288 // re-registered with the new strategy and output
2289 if (dstChain == 0) {
2290 dstChain = effect->chain().promote();
2291 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002292 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002293 srcThread->addEffect_l(effect);
2294 return NO_INIT;
2295 }
2296 strategy = dstChain->strategy();
2297 }
2298 if (reRegister) {
2299 AudioSystem::unregisterEffect(effect->id());
2300 AudioSystem::registerEffect(&effect->desc(),
2301 dstOutput,
2302 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002303 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002304 effect->id());
2305 }
Eric Laurentde070132010-07-13 04:45:46 -07002306 effect = chain->getEffectFromId_l(0);
2307 }
2308
2309 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002310}
2311
Glenn Kastenda6ef132013-01-10 12:31:01 -08002312struct Entry {
2313#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2314 char mName[MAX_NAME];
2315};
2316
2317int comparEntry(const void *p1, const void *p2)
2318{
2319 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2320}
2321
Glenn Kasten46909e72013-02-26 09:20:22 -08002322#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002323void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002324{
Eric Laurent81784c32012-11-19 14:55:58 -08002325 NBAIO_Source *teeSource = source.get();
2326 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002327 // .wav rotation
2328 // There is a benign race condition if 2 threads call this simultaneously.
2329 // They would both traverse the directory, but the result would simply be
2330 // failures at unlink() which are ignored. It's also unlikely since
2331 // normally dumpsys is only done by bugreport or from the command line.
2332 char teePath[32+256];
2333 strcpy(teePath, "/data/misc/media");
2334 size_t teePathLen = strlen(teePath);
2335 DIR *dir = opendir(teePath);
2336 teePath[teePathLen++] = '/';
2337 if (dir != NULL) {
2338#define MAX_SORT 20 // number of entries to sort
2339#define MAX_KEEP 10 // number of entries to keep
2340 struct Entry entries[MAX_SORT];
2341 size_t entryCount = 0;
2342 while (entryCount < MAX_SORT) {
2343 struct dirent de;
2344 struct dirent *result = NULL;
2345 int rc = readdir_r(dir, &de, &result);
2346 if (rc != 0) {
2347 ALOGW("readdir_r failed %d", rc);
2348 break;
2349 }
2350 if (result == NULL) {
2351 break;
2352 }
2353 if (result != &de) {
2354 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2355 break;
2356 }
2357 // ignore non .wav file entries
2358 size_t nameLen = strlen(de.d_name);
2359 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2360 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2361 continue;
2362 }
2363 strcpy(entries[entryCount++].mName, de.d_name);
2364 }
2365 (void) closedir(dir);
2366 if (entryCount > MAX_KEEP) {
2367 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2368 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2369 strcpy(&teePath[teePathLen], entries[i].mName);
2370 (void) unlink(teePath);
2371 }
2372 }
2373 } else {
2374 if (fd >= 0) {
2375 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2376 }
2377 }
Eric Laurent81784c32012-11-19 14:55:58 -08002378 char teeTime[16];
2379 struct timeval tv;
2380 gettimeofday(&tv, NULL);
2381 struct tm tm;
2382 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002383 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2384 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2385 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2386 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002387 if (teeFd >= 0) {
2388 char wavHeader[44];
2389 memcpy(wavHeader,
2390 "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",
2391 sizeof(wavHeader));
2392 NBAIO_Format format = teeSource->format();
2393 unsigned channelCount = Format_channelCount(format);
2394 ALOG_ASSERT(channelCount <= FCC_2);
2395 uint32_t sampleRate = Format_sampleRate(format);
2396 wavHeader[22] = channelCount; // number of channels
2397 wavHeader[24] = sampleRate; // sample rate
2398 wavHeader[25] = sampleRate >> 8;
2399 wavHeader[32] = channelCount * 2; // block alignment
2400 write(teeFd, wavHeader, sizeof(wavHeader));
2401 size_t total = 0;
2402 bool firstRead = true;
2403 for (;;) {
2404#define TEE_SINK_READ 1024
2405 short buffer[TEE_SINK_READ * FCC_2];
2406 size_t count = TEE_SINK_READ;
2407 ssize_t actual = teeSource->read(buffer, count,
2408 AudioBufferProvider::kInvalidPTS);
2409 bool wasFirstRead = firstRead;
2410 firstRead = false;
2411 if (actual <= 0) {
2412 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2413 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002414 }
Eric Laurent81784c32012-11-19 14:55:58 -08002415 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002416 }
Eric Laurent81784c32012-11-19 14:55:58 -08002417 ALOG_ASSERT(actual <= (ssize_t)count);
2418 write(teeFd, buffer, actual * channelCount * sizeof(short));
2419 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002420 }
Eric Laurent81784c32012-11-19 14:55:58 -08002421 lseek(teeFd, (off_t) 4, SEEK_SET);
2422 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2423 write(teeFd, &temp, sizeof(temp));
2424 lseek(teeFd, (off_t) 40, SEEK_SET);
2425 temp = total * channelCount * sizeof(short);
2426 write(teeFd, &temp, sizeof(temp));
2427 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002428 if (fd >= 0) {
2429 fdprintf(fd, "tee copied to %s\n", teePath);
2430 }
Eric Laurent59255e42011-07-27 19:49:51 -07002431 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002432 if (fd >= 0) {
2433 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2434 }
Eric Laurent59255e42011-07-27 19:49:51 -07002435 }
2436 }
2437}
Glenn Kasten46909e72013-02-26 09:20:22 -08002438#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002439
Mathias Agopian65ab4712010-07-14 17:59:35 -07002440// ----------------------------------------------------------------------------
2441
2442status_t AudioFlinger::onTransact(
2443 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2444{
2445 return BnAudioFlinger::onTransact(code, data, reply, flags);
2446}
2447
Mathias Agopian65ab4712010-07-14 17:59:35 -07002448}; // namespace android