blob: 17a69faf95b030d29061619d0a9ebef49deffce0 [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 Kastenda6ef132013-01-10 12:31:01 -080022#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070023#include <math.h>
24#include <signal.h>
25#include <sys/time.h>
26#include <sys/resource.h>
27
Gloria Wang9ee159b2011-02-24 14:51:45 -080028#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#include <binder/IServiceManager.h>
30#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070031#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070032#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <utils/String16.h>
34#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070035#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070036
Dima Zavinfce7a472011-04-19 22:30:36 -070037#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <cutils/properties.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080039#include <cutils/compiler.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070040
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 Kastenda6ef132013-01-10 12:31:01 -080064
Mathias Agopian65ab4712010-07-14 17:59:35 -070065// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070066
John Grossman1c345192012-03-27 14:00:17 -070067// Note: the following macro is used for extremely verbose logging message. In
68// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
69// 0; but one side effect of this is to turn all LOGV's as well. Some messages
70// are so verbose that we want to suppress them even when we have ALOG_ASSERT
71// turned on. Do not uncomment the #def below unless you really know what you
72// are doing and want to see all of the extremely verbose messages.
73//#define VERY_VERY_VERBOSE_LOGGING
74#ifdef VERY_VERY_VERBOSE_LOGGING
75#define ALOGVV ALOGV
76#else
77#define ALOGVV(a...) do { } while(0)
78#endif
Eric Laurentde070132010-07-13 04:45:46 -070079
Mathias Agopian65ab4712010-07-14 17:59:35 -070080namespace android {
81
Glenn Kastenec1d6b52011-12-12 09:04:45 -080082static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
83static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070084
Glenn Kasten58912562012-04-03 10:45:00 -070085
John Grossman4ff14ba2012-02-08 16:37:41 -080086nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080087
Eric Laurent81784c32012-11-19 14:55:58 -080088uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070089
Glenn Kasten46909e72013-02-26 09:20:22 -080090#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080091bool AudioFlinger::mTeeSinkInputEnabled = false;
92bool AudioFlinger::mTeeSinkOutputEnabled = false;
93bool AudioFlinger::mTeeSinkTrackEnabled = false;
94
95size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
96size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
97size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -080098#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -080099
Mathias Agopian65ab4712010-07-14 17:59:35 -0700100// ----------------------------------------------------------------------------
101
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700102static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700103{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700104 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700105 int rc;
106
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700107 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
108 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
109 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
110 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700111 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700112 }
113 rc = audio_hw_device_open(mod, dev);
114 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
115 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
116 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700117 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700118 }
119 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
120 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
121 rc = BAD_VALUE;
122 goto out;
123 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700124 return 0;
125
126out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700127 *dev = NULL;
128 return rc;
129}
130
Mathias Agopian65ab4712010-07-14 17:59:35 -0700131// ----------------------------------------------------------------------------
132
133AudioFlinger::AudioFlinger()
134 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800135 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700136 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800137 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800138 mMasterMute(false),
139 mNextUniqueId(1),
140 mMode(AUDIO_MODE_INVALID),
141 mBtNrecIsOff(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142{
Glenn Kasten949a9262013-04-16 12:35:20 -0700143 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800144 char value[PROPERTY_VALUE_MAX];
145 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
146 if (doLog) {
147 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
148 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800149#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800150 (void) property_get("ro.debuggable", value, "0");
151 int debuggable = atoi(value);
152 int teeEnabled = 0;
153 if (debuggable) {
154 (void) property_get("af.tee", value, "0");
155 teeEnabled = atoi(value);
156 }
157 if (teeEnabled & 1)
158 mTeeSinkInputEnabled = true;
159 if (teeEnabled & 2)
160 mTeeSinkOutputEnabled = true;
161 if (teeEnabled & 4)
162 mTeeSinkTrackEnabled = true;
Glenn Kasten46909e72013-02-26 09:20:22 -0800163#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700164}
165
166void AudioFlinger::onFirstRef()
167{
Dima Zavin799a70e2011-04-18 16:57:27 -0700168 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700169
Eric Laurent93575202011-01-18 18:39:02 -0800170 Mutex::Autolock _l(mLock);
171
Dima Zavin799a70e2011-04-18 16:57:27 -0700172 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800173 char val_str[PROPERTY_VALUE_MAX] = { 0 };
174 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
175 uint32_t int_val;
176 if (1 == sscanf(val_str, "%u", &int_val)) {
177 mStandbyTimeInNsecs = milliseconds(int_val);
178 ALOGI("Using %u mSec as standby time.", int_val);
179 } else {
180 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
181 ALOGI("Using default %u mSec as standby time.",
182 (uint32_t)(mStandbyTimeInNsecs / 1000000));
183 }
184 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185
Eric Laurenta4c5a552012-03-29 10:12:40 -0700186 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700187}
188
189AudioFlinger::~AudioFlinger()
190{
191 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700192 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700193 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194 }
195 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700196 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700197 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700198 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700199
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800200 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
201 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700202 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
203 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700204 }
205}
206
Eric Laurenta4c5a552012-03-29 10:12:40 -0700207static const char * const audio_interfaces[] = {
208 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
209 AUDIO_HARDWARE_MODULE_ID_A2DP,
210 AUDIO_HARDWARE_MODULE_ID_USB,
211};
212#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
213
John Grossmanee578c02012-07-23 17:05:46 -0700214AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
215 audio_module_handle_t module,
216 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700217{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700218 // if module is 0, the request comes from an old policy manager and we should load
219 // well known modules
220 if (module == 0) {
221 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
222 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
223 loadHwModule_l(audio_interfaces[i]);
224 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700225 // then try to find a module supporting the requested device.
226 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
227 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
228 audio_hw_device_t *dev = audioHwDevice->hwDevice();
229 if ((dev->get_supported_devices != NULL) &&
230 (dev->get_supported_devices(dev) & devices) == devices)
231 return audioHwDevice;
232 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700233 } else {
234 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700235 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
236 if (audioHwDevice != NULL) {
237 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700238 }
239 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700240
Dima Zavin799a70e2011-04-18 16:57:27 -0700241 return NULL;
242}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700244void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700245{
246 const size_t SIZE = 256;
247 char buffer[SIZE];
248 String8 result;
249
250 result.append("Clients:\n");
251 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800252 sp<Client> client = mClients.valueAt(i).promote();
253 if (client != 0) {
254 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
255 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256 }
257 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700258
259 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800260 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700261 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
262 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800263 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700264 result.append(buffer);
265 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267}
268
269
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700270void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271{
272 const size_t SIZE = 256;
273 char buffer[SIZE];
274 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800275 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276
John Grossman4ff14ba2012-02-08 16:37:41 -0800277 snprintf(buffer, SIZE, "Hardware status: %d\n"
278 "Standby Time mSec: %u\n",
279 hardwareStatus,
280 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281 result.append(buffer);
282 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700283}
284
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700285void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286{
287 const size_t SIZE = 256;
288 char buffer[SIZE];
289 String8 result;
290 snprintf(buffer, SIZE, "Permission Denial: "
291 "can't dump AudioFlinger from pid=%d, uid=%d\n",
292 IPCThreadState::self()->getCallingPid(),
293 IPCThreadState::self()->getCallingUid());
294 result.append(buffer);
295 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700296}
297
Eric Laurent81784c32012-11-19 14:55:58 -0800298bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299{
300 bool locked = false;
301 for (int i = 0; i < kDumpLockRetries; ++i) {
302 if (mutex.tryLock() == NO_ERROR) {
303 locked = true;
304 break;
305 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800306 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307 }
308 return locked;
309}
310
311status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
312{
Glenn Kasten44deb052012-02-05 18:09:08 -0800313 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700314 dumpPermissionDenial(fd, args);
315 } else {
316 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800317 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700318 if (!hardwareLocked) {
319 String8 result(kHardwareLockedString);
320 write(fd, result.string(), result.size());
321 } else {
322 mHardwareLock.unlock();
323 }
324
Eric Laurent81784c32012-11-19 14:55:58 -0800325 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326
327 // failed to lock - AudioFlinger is probably deadlocked
328 if (!locked) {
329 String8 result(kDeadlockedString);
330 write(fd, result.string(), result.size());
331 }
332
333 dumpClients(fd, args);
334 dumpInternals(fd, args);
335
336 // dump playback threads
337 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
338 mPlaybackThreads.valueAt(i)->dump(fd, args);
339 }
340
341 // dump record threads
342 for (size_t i = 0; i < mRecordThreads.size(); i++) {
343 mRecordThreads.valueAt(i)->dump(fd, args);
344 }
345
Dima Zavin799a70e2011-04-18 16:57:27 -0700346 // dump all hardware devs
347 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700348 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700349 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700351
Glenn Kasten46909e72013-02-26 09:20:22 -0800352#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700353 // dump the serially shared record tee sink
354 if (mRecordTeeSource != 0) {
355 dumpTee(fd, mRecordTeeSource);
356 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800357#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700358
Glenn Kastend65d73c2012-06-22 17:21:07 -0700359 if (locked) {
360 mLock.unlock();
361 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800362
363 // append a copy of media.log here by forwarding fd to it, but don't attempt
364 // to lookup the service if it's not running, as it will block for a second
365 if (mLogMemoryDealer != 0) {
366 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
367 if (binder != 0) {
368 fdprintf(fd, "\nmedia.log:\n");
369 Vector<String16> args;
370 binder->dump(fd, args);
371 }
372 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 }
374 return NO_ERROR;
375}
376
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800377sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
378{
379 // If pid is already in the mClients wp<> map, then use that entry
380 // (for which promote() is always != 0), otherwise create a new entry and Client.
381 sp<Client> client = mClients.valueFor(pid).promote();
382 if (client == 0) {
383 client = new Client(this, pid);
384 mClients.add(pid, client);
385 }
386
387 return client;
388}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389
Glenn Kasten9e58b552013-01-18 15:09:48 -0800390sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
391{
392 if (mLogMemoryDealer == 0) {
393 return new NBLog::Writer();
394 }
395 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
396 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
397 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
398 if (binder != 0) {
399 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
400 }
401 return writer;
402}
403
404void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
405{
Glenn Kasten685ef092013-02-04 08:15:34 -0800406 if (writer == 0) {
407 return;
408 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800409 sp<IMemory> iMemory(writer->getIMemory());
410 if (iMemory == 0) {
411 return;
412 }
413 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
414 if (binder != 0) {
415 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
416 // Now the media.log remote reference to IMemory is gone.
417 // When our last local reference to IMemory also drops to zero,
418 // the IMemory destructor will deallocate the region from mMemoryDealer.
419 }
420}
421
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422// IAudioFlinger interface
423
424
425sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800426 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700427 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800428 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700429 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800430 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800431 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800433 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800434 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 int *sessionId,
436 status_t *status)
437{
438 sp<PlaybackThread::Track> track;
439 sp<TrackHandle> trackHandle;
440 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441 status_t lStatus;
442 int lSessionId;
443
Glenn Kasten263709e2012-01-06 08:40:01 -0800444 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
445 // but if someone uses binder directly they could bypass that and cause us to crash
446 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000447 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700448 lStatus = BAD_VALUE;
449 goto Exit;
450 }
451
Glenn Kasten60a83922012-06-21 12:56:37 -0700452 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
453 // and we don't yet support 8.24 or 32-bit PCM
454 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
455 ALOGE("createTrack() invalid format %d", format);
456 lStatus = BAD_VALUE;
457 goto Exit;
458 }
459
Mathias Agopian65ab4712010-07-14 17:59:35 -0700460 {
461 Mutex::Autolock _l(mLock);
462 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700463 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700464 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800465 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466 lStatus = BAD_VALUE;
467 goto Exit;
468 }
469
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800470 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800471 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700472
Steve Block3856b092011-10-20 11:56:00 +0100473 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700474 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700475 // check if an effect chain with the same session ID is present on another
476 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700477 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700478 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
479 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700480 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700481 if (sessions & PlaybackThread::EFFECT_SESSION) {
482 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700483 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700484 }
Eric Laurentde070132010-07-13 04:45:46 -0700485 }
486 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700487 lSessionId = *sessionId;
488 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700489 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700490 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491 if (sessionId != NULL) {
492 *sessionId = lSessionId;
493 }
494 }
Steve Block3856b092011-10-20 11:56:00 +0100495 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700496
497 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800498 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700499
500 // move effect chain to this output thread if an effect on same session was waiting
501 // for a track to be created
502 if (lStatus == NO_ERROR && effectThread != NULL) {
503 Mutex::Autolock _dl(thread->mLock);
504 Mutex::Autolock _sl(effectThread->mLock);
505 moveEffectChain_l(lSessionId, effectThread, thread, true);
506 }
Eric Laurenta011e352012-03-29 15:51:43 -0700507
508 // Look for sync events awaiting for a session to be used.
509 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
510 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
511 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700512 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700513 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700514 } else {
515 mPendingSyncEvents[i]->cancel();
516 }
Eric Laurenta011e352012-03-29 15:51:43 -0700517 mPendingSyncEvents.removeAt(i);
518 i--;
519 }
520 }
521 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700522 }
523 if (lStatus == NO_ERROR) {
524 trackHandle = new TrackHandle(track);
525 } else {
526 // remove local strong reference to Client before deleting the Track so that the Client
527 // destructor is called by the TrackBase destructor with mLock held
528 client.clear();
529 track.clear();
530 }
531
532Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700533 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700534 *status = lStatus;
535 }
536 return trackHandle;
537}
538
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800539uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540{
541 Mutex::Autolock _l(mLock);
542 PlaybackThread *thread = checkPlaybackThread_l(output);
543 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000544 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545 return 0;
546 }
547 return thread->sampleRate();
548}
549
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800550int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551{
552 Mutex::Autolock _l(mLock);
553 PlaybackThread *thread = checkPlaybackThread_l(output);
554 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000555 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 return 0;
557 }
558 return thread->channelCount();
559}
560
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800561audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562{
563 Mutex::Autolock _l(mLock);
564 PlaybackThread *thread = checkPlaybackThread_l(output);
565 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000566 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800567 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700568 }
569 return thread->format();
570}
571
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800572size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573{
574 Mutex::Autolock _l(mLock);
575 PlaybackThread *thread = checkPlaybackThread_l(output);
576 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000577 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578 return 0;
579 }
Glenn Kasten58912562012-04-03 10:45:00 -0700580 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
581 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 return thread->frameCount();
583}
584
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800585uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586{
587 Mutex::Autolock _l(mLock);
588 PlaybackThread *thread = checkPlaybackThread_l(output);
589 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800590 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 return 0;
592 }
593 return thread->latency();
594}
595
596status_t AudioFlinger::setMasterVolume(float value)
597{
Eric Laurenta1884f92011-08-23 08:25:03 -0700598 status_t ret = initCheck();
599 if (ret != NO_ERROR) {
600 return ret;
601 }
602
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603 // check calling permissions
604 if (!settingsAllowed()) {
605 return PERMISSION_DENIED;
606 }
607
Eric Laurenta4c5a552012-03-29 10:12:40 -0700608 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700609 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700610
John Grossmanee578c02012-07-23 17:05:46 -0700611 // Set master volume in the HALs which support it.
612 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
613 AutoMutex lock(mHardwareLock);
614 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800615
John Grossmanee578c02012-07-23 17:05:46 -0700616 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
617 if (dev->canSetMasterVolume()) {
618 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800619 }
John Grossmanee578c02012-07-23 17:05:46 -0700620 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622
John Grossmanee578c02012-07-23 17:05:46 -0700623 // Now set the master volume in each playback thread. Playback threads
624 // assigned to HALs which do not have master volume support will apply
625 // master volume during the mix operation. Threads with HALs which do
626 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800627 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700628 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629
630 return NO_ERROR;
631}
632
Glenn Kastenf78aee72012-01-04 11:00:47 -0800633status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634{
Eric Laurenta1884f92011-08-23 08:25:03 -0700635 status_t ret = initCheck();
636 if (ret != NO_ERROR) {
637 return ret;
638 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639
640 // check calling permissions
641 if (!settingsAllowed()) {
642 return PERMISSION_DENIED;
643 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800644 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000645 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 return BAD_VALUE;
647 }
648
649 { // scope for the lock
650 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700651 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700653 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654 mHardwareStatus = AUDIO_HW_IDLE;
655 }
656
657 if (NO_ERROR == ret) {
658 Mutex::Autolock _l(mLock);
659 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800660 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700661 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700662 }
663
664 return ret;
665}
666
667status_t AudioFlinger::setMicMute(bool state)
668{
Eric Laurenta1884f92011-08-23 08:25:03 -0700669 status_t ret = initCheck();
670 if (ret != NO_ERROR) {
671 return ret;
672 }
673
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 // check calling permissions
675 if (!settingsAllowed()) {
676 return PERMISSION_DENIED;
677 }
678
679 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700680 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700682 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683 mHardwareStatus = AUDIO_HW_IDLE;
684 return ret;
685}
686
687bool AudioFlinger::getMicMute() const
688{
Eric Laurenta1884f92011-08-23 08:25:03 -0700689 status_t ret = initCheck();
690 if (ret != NO_ERROR) {
691 return false;
692 }
693
Dima Zavinfce7a472011-04-19 22:30:36 -0700694 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800695 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700696 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700698 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 mHardwareStatus = AUDIO_HW_IDLE;
700 return state;
701}
702
703status_t AudioFlinger::setMasterMute(bool muted)
704{
John Grossmand8f178d2012-07-20 14:51:35 -0700705 status_t ret = initCheck();
706 if (ret != NO_ERROR) {
707 return ret;
708 }
709
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710 // check calling permissions
711 if (!settingsAllowed()) {
712 return PERMISSION_DENIED;
713 }
714
John Grossmanee578c02012-07-23 17:05:46 -0700715 Mutex::Autolock _l(mLock);
716 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700717
John Grossmanee578c02012-07-23 17:05:46 -0700718 // Set master mute in the HALs which support it.
719 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
720 AutoMutex lock(mHardwareLock);
721 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700722
John Grossmanee578c02012-07-23 17:05:46 -0700723 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
724 if (dev->canSetMasterMute()) {
725 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700726 }
John Grossmanee578c02012-07-23 17:05:46 -0700727 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700728 }
729
John Grossmanee578c02012-07-23 17:05:46 -0700730 // Now set the master mute in each playback thread. Playback threads
731 // assigned to HALs which do not have master mute support will apply master
732 // mute during the mix operation. Threads with HALs which do support master
733 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800734 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700735 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700736
737 return NO_ERROR;
738}
739
740float AudioFlinger::masterVolume() const
741{
Glenn Kasten98067102011-12-13 11:47:54 -0800742 Mutex::Autolock _l(mLock);
743 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700744}
745
746bool AudioFlinger::masterMute() const
747{
Glenn Kasten98067102011-12-13 11:47:54 -0800748 Mutex::Autolock _l(mLock);
749 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750}
751
John Grossman4ff14ba2012-02-08 16:37:41 -0800752float AudioFlinger::masterVolume_l() const
753{
John Grossman4ff14ba2012-02-08 16:37:41 -0800754 return mMasterVolume;
755}
756
John Grossmand8f178d2012-07-20 14:51:35 -0700757bool AudioFlinger::masterMute_l() const
758{
John Grossmanee578c02012-07-23 17:05:46 -0700759 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700760}
761
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800762status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
763 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700764{
765 // check calling permissions
766 if (!settingsAllowed()) {
767 return PERMISSION_DENIED;
768 }
769
Glenn Kasten263709e2012-01-06 08:40:01 -0800770 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000771 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700772 return BAD_VALUE;
773 }
774
775 AutoMutex lock(mLock);
776 PlaybackThread *thread = NULL;
777 if (output) {
778 thread = checkPlaybackThread_l(output);
779 if (thread == NULL) {
780 return BAD_VALUE;
781 }
782 }
783
784 mStreamTypes[stream].volume = value;
785
786 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800787 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700788 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789 }
790 } else {
791 thread->setStreamVolume(stream, value);
792 }
793
794 return NO_ERROR;
795}
796
Glenn Kastenfff6d712012-01-12 16:38:12 -0800797status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798{
799 // check calling permissions
800 if (!settingsAllowed()) {
801 return PERMISSION_DENIED;
802 }
803
Glenn Kasten263709e2012-01-06 08:40:01 -0800804 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700805 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000806 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807 return BAD_VALUE;
808 }
809
Eric Laurent93575202011-01-18 18:39:02 -0800810 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811 mStreamTypes[stream].mute = muted;
812 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700813 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814
815 return NO_ERROR;
816}
817
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800818float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819{
Glenn Kasten263709e2012-01-06 08:40:01 -0800820 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700821 return 0.0f;
822 }
823
824 AutoMutex lock(mLock);
825 float volume;
826 if (output) {
827 PlaybackThread *thread = checkPlaybackThread_l(output);
828 if (thread == NULL) {
829 return 0.0f;
830 }
831 volume = thread->streamVolume(stream);
832 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800833 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 }
835
836 return volume;
837}
838
Glenn Kastenfff6d712012-01-12 16:38:12 -0800839bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840{
Glenn Kasten263709e2012-01-06 08:40:01 -0800841 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700842 return true;
843 }
844
Glenn Kasten6637baa2012-01-09 09:40:36 -0800845 AutoMutex lock(mLock);
846 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847}
848
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800849status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700851 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
852 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800853
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854 // check calling permissions
855 if (!settingsAllowed()) {
856 return PERMISSION_DENIED;
857 }
858
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 // ioHandle == 0 means the parameters are global to the audio hardware interface
860 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700861 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700862 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800863 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700864 AutoMutex lock(mHardwareLock);
865 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
866 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
867 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
868 status_t result = dev->set_parameters(dev, keyValuePairs.string());
869 final_result = result ?: final_result;
870 }
871 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800872 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700873 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
874 AudioParameter param = AudioParameter(keyValuePairs);
875 String8 value;
876 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700877 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
878 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700879 for (size_t i = 0; i < mRecordThreads.size(); i++) {
880 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700881 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700882 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
883 // collect all of the thread's session IDs
884 KeyedVector<int, bool> ids = thread->sessionIds();
885 // suspend effects associated with those session IDs
886 for (size_t j = 0; j < ids.size(); ++j) {
887 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700888 thread->setEffectSuspended(FX_IID_AEC,
889 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700890 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700891 thread->setEffectSuspended(FX_IID_NS,
892 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700893 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700894 }
895 }
Eric Laurentbee53372011-08-29 12:42:48 -0700896 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700897 }
898 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700899 String8 screenState;
900 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
901 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800902 if (isOff != (AudioFlinger::mScreenState & 1)) {
903 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700904 }
905 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700906 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907 }
908
909 // hold a strong ref on thread in case closeOutput() or closeInput() is called
910 // and the thread is exited once the lock is released
911 sp<ThreadBase> thread;
912 {
913 Mutex::Autolock _l(mLock);
914 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700915 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700916 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800917 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700918 // indicate output device change to all input threads for pre processing
919 AudioParameter param = AudioParameter(keyValuePairs);
920 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700921 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
922 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700923 for (size_t i = 0; i < mRecordThreads.size(); i++) {
924 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
925 }
926 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927 }
928 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800929 if (thread != 0) {
930 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931 }
932 return BAD_VALUE;
933}
934
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800935String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700937 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
938 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939
Eric Laurenta4c5a552012-03-29 10:12:40 -0700940 Mutex::Autolock _l(mLock);
941
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700943 String8 out_s8;
944
Dima Zavin799a70e2011-04-18 16:57:27 -0700945 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800946 char *s;
947 {
948 AutoMutex lock(mHardwareLock);
949 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700950 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800951 s = dev->get_parameters(dev, keys.string());
952 mHardwareStatus = AUDIO_HW_IDLE;
953 }
John Grossmanef7740b2012-02-09 11:28:36 -0800954 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700955 free(s);
956 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700957 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 }
959
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
961 if (playbackThread != NULL) {
962 return playbackThread->getParameters(keys);
963 }
964 RecordThread *recordThread = checkRecordThread_l(ioHandle);
965 if (recordThread != NULL) {
966 return recordThread->getParameters(keys);
967 }
968 return String8("");
969}
970
Glenn Kastendd8104c2012-07-02 12:42:44 -0700971size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
972 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973{
Eric Laurenta1884f92011-08-23 08:25:03 -0700974 status_t ret = initCheck();
975 if (ret != NO_ERROR) {
976 return 0;
977 }
978
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800979 AutoMutex lock(mHardwareLock);
980 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000981 struct audio_config config;
982 memset(&config, 0, sizeof(config));
983 config.sample_rate = sampleRate;
984 config.channel_mask = channelMask;
985 config.format = format;
986
John Grossmanee578c02012-07-23 17:05:46 -0700987 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
988 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800989 mHardwareStatus = AUDIO_HW_IDLE;
990 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991}
992
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800993unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700994{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995 Mutex::Autolock _l(mLock);
996
997 RecordThread *recordThread = checkRecordThread_l(ioHandle);
998 if (recordThread != NULL) {
999 return recordThread->getInputFramesLost();
1000 }
1001 return 0;
1002}
1003
1004status_t AudioFlinger::setVoiceVolume(float value)
1005{
Eric Laurenta1884f92011-08-23 08:25:03 -07001006 status_t ret = initCheck();
1007 if (ret != NO_ERROR) {
1008 return ret;
1009 }
1010
Mathias Agopian65ab4712010-07-14 17:59:35 -07001011 // check calling permissions
1012 if (!settingsAllowed()) {
1013 return PERMISSION_DENIED;
1014 }
1015
1016 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001017 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001018 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001019 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001020 mHardwareStatus = AUDIO_HW_IDLE;
1021
1022 return ret;
1023}
1024
Glenn Kasten26c77552012-11-16 12:01:44 -08001025status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001026 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001027{
1028 status_t status;
1029
1030 Mutex::Autolock _l(mLock);
1031
1032 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1033 if (playbackThread != NULL) {
1034 return playbackThread->getRenderPosition(halFrames, dspFrames);
1035 }
1036
1037 return BAD_VALUE;
1038}
1039
1040void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1041{
1042
1043 Mutex::Autolock _l(mLock);
1044
Glenn Kastenbb001922012-02-03 11:10:26 -08001045 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046 if (mNotificationClients.indexOfKey(pid) < 0) {
1047 sp<NotificationClient> notificationClient = new NotificationClient(this,
1048 client,
1049 pid);
Steve Block3856b092011-10-20 11:56:00 +01001050 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051
1052 mNotificationClients.add(pid, notificationClient);
1053
1054 sp<IBinder> binder = client->asBinder();
1055 binder->linkToDeath(notificationClient);
1056
1057 // the config change is always sent from playback or record threads to avoid deadlock
1058 // with AudioSystem::gLock
1059 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001060 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001061 }
1062
1063 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001064 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065 }
1066 }
1067}
1068
1069void AudioFlinger::removeNotificationClient(pid_t pid)
1070{
1071 Mutex::Autolock _l(mLock);
1072
Glenn Kastena3b09252012-01-20 09:19:01 -08001073 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001074
Steve Block3856b092011-10-20 11:56:00 +01001075 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001076 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001077 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001078 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001079 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001080 ALOGV(" pid %d @ %d", ref->mPid, i);
1081 if (ref->mPid == pid) {
1082 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001083 mAudioSessionRefs.removeAt(i);
1084 delete ref;
1085 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001086 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001087 } else {
1088 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001089 }
1090 }
1091 if (removed) {
1092 purgeStaleEffects_l();
1093 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001094}
1095
1096// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001097void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001098{
1099 size_t size = mNotificationClients.size();
1100 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001101 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1102 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001103 }
1104}
1105
1106// removeClient_l() must be called with AudioFlinger::mLock held
1107void AudioFlinger::removeClient_l(pid_t pid)
1108{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001109 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001110 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001111 mClients.removeItem(pid);
1112}
1113
Eric Laurent717e1282012-06-29 16:36:52 -07001114// getEffectThread_l() must be called with AudioFlinger::mLock held
1115sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1116{
1117 sp<PlaybackThread> thread;
1118
1119 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1120 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1121 ALOG_ASSERT(thread == 0);
1122 thread = mPlaybackThreads.valueAt(i);
1123 }
1124 }
1125
1126 return thread;
1127}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130
1131// ----------------------------------------------------------------------------
1132
1133AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1134 : RefBase(),
1135 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001136 // 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 -07001137 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001138 mPid(pid),
1139 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001140{
1141 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1142}
1143
1144// Client destructor must be called with AudioFlinger::mLock held
1145AudioFlinger::Client::~Client()
1146{
1147 mAudioFlinger->removeClient_l(mPid);
1148}
1149
Glenn Kasten435dbe62012-01-30 10:15:48 -08001150sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151{
1152 return mMemoryDealer;
1153}
1154
John Grossman4ff14ba2012-02-08 16:37:41 -08001155// Reserve one of the limited slots for a timed audio track associated
1156// with this client
1157bool AudioFlinger::Client::reserveTimedTrack()
1158{
1159 const int kMaxTimedTracksPerClient = 4;
1160
1161 Mutex::Autolock _l(mTimedTrackLock);
1162
1163 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1164 ALOGW("can not create timed track - pid %d has exceeded the limit",
1165 mPid);
1166 return false;
1167 }
1168
1169 mTimedTrackCount++;
1170 return true;
1171}
1172
1173// Release a slot for a timed audio track
1174void AudioFlinger::Client::releaseTimedTrack()
1175{
1176 Mutex::Autolock _l(mTimedTrackLock);
1177 mTimedTrackCount--;
1178}
1179
Mathias Agopian65ab4712010-07-14 17:59:35 -07001180// ----------------------------------------------------------------------------
1181
1182AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1183 const sp<IAudioFlingerClient>& client,
1184 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001185 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186{
1187}
1188
1189AudioFlinger::NotificationClient::~NotificationClient()
1190{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001191}
1192
1193void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1194{
1195 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001196 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001197}
1198
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199
1200// ----------------------------------------------------------------------------
1201
1202sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001203 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001205 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001206 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001207 size_t frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08001208 IAudioFlinger::track_flags_t flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001209 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001210 int *sessionId,
1211 status_t *status)
1212{
1213 sp<RecordThread::RecordTrack> recordTrack;
1214 sp<RecordHandle> recordHandle;
1215 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001216 status_t lStatus;
1217 RecordThread *thread;
1218 size_t inFrameCount;
1219 int lSessionId;
1220
1221 // check calling permissions
1222 if (!recordingAllowed()) {
1223 lStatus = PERMISSION_DENIED;
1224 goto Exit;
1225 }
1226
1227 // add client to list
1228 { // scope for mLock
1229 Mutex::Autolock _l(mLock);
1230 thread = checkRecordThread_l(input);
1231 if (thread == NULL) {
1232 lStatus = BAD_VALUE;
1233 goto Exit;
1234 }
1235
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001236 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001237 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238
1239 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001240 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001241 lSessionId = *sessionId;
1242 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001243 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 if (sessionId != NULL) {
1245 *sessionId = lSessionId;
1246 }
1247 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001248 // create new record track.
1249 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001250 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1251 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001252 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001253 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001254 // remove local strong reference to Client before deleting the RecordTrack so that the
1255 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256 client.clear();
1257 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001258 goto Exit;
1259 }
1260
1261 // return to handle to client
1262 recordHandle = new RecordHandle(recordTrack);
1263 lStatus = NO_ERROR;
1264
1265Exit:
1266 if (status) {
1267 *status = lStatus;
1268 }
1269 return recordHandle;
1270}
1271
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001272
1273
Mathias Agopian65ab4712010-07-14 17:59:35 -07001274// ----------------------------------------------------------------------------
1275
Eric Laurenta4c5a552012-03-29 10:12:40 -07001276audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1277{
1278 if (!settingsAllowed()) {
1279 return 0;
1280 }
1281 Mutex::Autolock _l(mLock);
1282 return loadHwModule_l(name);
1283}
1284
1285// loadHwModule_l() must be called with AudioFlinger::mLock held
1286audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1287{
1288 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1289 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1290 ALOGW("loadHwModule() module %s already loaded", name);
1291 return mAudioHwDevs.keyAt(i);
1292 }
1293 }
1294
Eric Laurenta4c5a552012-03-29 10:12:40 -07001295 audio_hw_device_t *dev;
1296
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001297 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001298 if (rc) {
1299 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1300 return 0;
1301 }
1302
1303 mHardwareStatus = AUDIO_HW_INIT;
1304 rc = dev->init_check(dev);
1305 mHardwareStatus = AUDIO_HW_IDLE;
1306 if (rc) {
1307 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1308 return 0;
1309 }
1310
John Grossmanee578c02012-07-23 17:05:46 -07001311 // Check and cache this HAL's level of support for master mute and master
1312 // volume. If this is the first HAL opened, and it supports the get
1313 // methods, use the initial values provided by the HAL as the current
1314 // master mute and volume settings.
1315
1316 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1317 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001318 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001319
1320 if (0 == mAudioHwDevs.size()) {
1321 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1322 if (NULL != dev->get_master_volume) {
1323 float mv;
1324 if (OK == dev->get_master_volume(dev, &mv)) {
1325 mMasterVolume = mv;
1326 }
1327 }
1328
1329 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1330 if (NULL != dev->get_master_mute) {
1331 bool mm;
1332 if (OK == dev->get_master_mute(dev, &mm)) {
1333 mMasterMute = mm;
1334 }
1335 }
1336 }
1337
Eric Laurenta4c5a552012-03-29 10:12:40 -07001338 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001339 if ((NULL != dev->set_master_volume) &&
1340 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1341 flags = static_cast<AudioHwDevice::Flags>(flags |
1342 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1343 }
1344
1345 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1346 if ((NULL != dev->set_master_mute) &&
1347 (OK == dev->set_master_mute(dev, mMasterMute))) {
1348 flags = static_cast<AudioHwDevice::Flags>(flags |
1349 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1350 }
1351
Eric Laurenta4c5a552012-03-29 10:12:40 -07001352 mHardwareStatus = AUDIO_HW_IDLE;
1353 }
1354
1355 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001356 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001357
1358 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001359 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001360
1361 return handle;
1362
1363}
1364
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001365// ----------------------------------------------------------------------------
1366
Glenn Kasten3b16c762012-11-14 08:44:39 -08001367uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001368{
1369 Mutex::Autolock _l(mLock);
1370 PlaybackThread *thread = primaryPlaybackThread_l();
1371 return thread != NULL ? thread->sampleRate() : 0;
1372}
1373
Glenn Kastene33054e2012-11-14 12:54:39 -08001374size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001375{
1376 Mutex::Autolock _l(mLock);
1377 PlaybackThread *thread = primaryPlaybackThread_l();
1378 return thread != NULL ? thread->frameCountHAL() : 0;
1379}
1380
1381// ----------------------------------------------------------------------------
1382
Eric Laurenta4c5a552012-03-29 10:12:40 -07001383audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1384 audio_devices_t *pDevices,
1385 uint32_t *pSamplingRate,
1386 audio_format_t *pFormat,
1387 audio_channel_mask_t *pChannelMask,
1388 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001389 audio_output_flags_t flags,
1390 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001391{
1392 status_t status;
1393 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001394 struct audio_config config;
1395 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1396 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1397 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1398 if (offloadInfo) {
1399 config.offload_info = *offloadInfo;
1400 }
1401
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001402 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001403 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404
Eric Laurenta4c5a552012-03-29 10:12:40 -07001405 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
1406 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001407 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001408 config.sample_rate,
1409 config.format,
1410 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001411 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412
1413 if (pDevices == NULL || *pDevices == 0) {
1414 return 0;
1415 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001416
Mathias Agopian65ab4712010-07-14 17:59:35 -07001417 Mutex::Autolock _l(mLock);
1418
Eric Laurenta4c5a552012-03-29 10:12:40 -07001419 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001420 if (outHwDev == NULL)
1421 return 0;
1422
John Grossmanee578c02012-07-23 17:05:46 -07001423 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001424 audio_io_handle_t id = nextUniqueId();
1425
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001426 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001427
John Grossmanee578c02012-07-23 17:05:46 -07001428 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001429 id,
1430 *pDevices,
1431 (audio_output_flags_t)flags,
1432 &config,
1433 &outStream);
1434
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001435 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001436 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, "
1437 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001438 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001439 config.sample_rate,
1440 config.format,
1441 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001442 status);
1443
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001444 if (status == NO_ERROR && outStream != NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001445 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001446
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001447 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001448 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1449 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001450 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001451 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001452 } else {
1453 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001454 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001455 }
1456 mPlaybackThreads.add(id, thread);
1457
Glenn Kasten7c027242012-12-26 14:43:16 -08001458 if (pSamplingRate != NULL) {
1459 *pSamplingRate = config.sample_rate;
1460 }
1461 if (pFormat != NULL) {
1462 *pFormat = config.format;
1463 }
1464 if (pChannelMask != NULL) {
1465 *pChannelMask = config.channel_mask;
1466 }
1467 if (pLatencyMs != NULL) {
1468 *pLatencyMs = thread->latency();
1469 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001470
1471 // notify client processes of the new output creation
1472 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001473
1474 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001475 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001476 ALOGI("Using module %d has the primary audio interface", module);
1477 mPrimaryHardwareDev = outHwDev;
1478
1479 AutoMutex lock(mHardwareLock);
1480 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001481 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001482 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001483 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001484 return id;
1485 }
1486
1487 return 0;
1488}
1489
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001490audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1491 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492{
1493 Mutex::Autolock _l(mLock);
1494 MixerThread *thread1 = checkMixerThread_l(output1);
1495 MixerThread *thread2 = checkMixerThread_l(output2);
1496
1497 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001498 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1499 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001500 return 0;
1501 }
1502
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001503 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001504 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1505 thread->addOutputTrack(thread2);
1506 mPlaybackThreads.add(id, thread);
1507 // notify client processes of the new output creation
1508 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1509 return id;
1510}
1511
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001512status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001513{
Glenn Kastend96c5722012-04-25 13:44:49 -07001514 return closeOutput_nonvirtual(output);
1515}
1516
1517status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1518{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519 // keep strong reference on the playback thread so that
1520 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001521 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522 {
1523 Mutex::Autolock _l(mLock);
1524 thread = checkPlaybackThread_l(output);
1525 if (thread == NULL) {
1526 return BAD_VALUE;
1527 }
1528
Steve Block3856b092011-10-20 11:56:00 +01001529 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001530
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001531 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001532 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001533 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001534 DuplicatingThread *dupThread =
1535 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001536 dupThread->removeOutputTrack((MixerThread *)thread.get());
1537 }
1538 }
1539 }
Glenn Kastena1117922012-01-26 10:53:32 -08001540 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001541 mPlaybackThreads.removeItem(output);
1542 }
1543 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001544 // The thread entity (active unit of execution) is no longer running here,
1545 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001546
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001547 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001548 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001549 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001550 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001551 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001552 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001553 }
1554 return NO_ERROR;
1555}
1556
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001557status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001558{
1559 Mutex::Autolock _l(mLock);
1560 PlaybackThread *thread = checkPlaybackThread_l(output);
1561
1562 if (thread == NULL) {
1563 return BAD_VALUE;
1564 }
1565
Steve Block3856b092011-10-20 11:56:00 +01001566 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001567 thread->suspend();
1568
1569 return NO_ERROR;
1570}
1571
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001572status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001573{
1574 Mutex::Autolock _l(mLock);
1575 PlaybackThread *thread = checkPlaybackThread_l(output);
1576
1577 if (thread == NULL) {
1578 return BAD_VALUE;
1579 }
1580
Steve Block3856b092011-10-20 11:56:00 +01001581 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001582
1583 thread->restore();
1584
1585 return NO_ERROR;
1586}
1587
Eric Laurenta4c5a552012-03-29 10:12:40 -07001588audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1589 audio_devices_t *pDevices,
1590 uint32_t *pSamplingRate,
1591 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001592 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001593{
1594 status_t status;
1595 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001596 struct audio_config config;
1597 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1598 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1599 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1600
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001601 uint32_t reqSamplingRate = config.sample_rate;
1602 audio_format_t reqFormat = config.format;
1603 audio_channel_mask_t reqChannels = config.channel_mask;
1604 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001605 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001606
1607 if (pDevices == NULL || *pDevices == 0) {
1608 return 0;
1609 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001610
Mathias Agopian65ab4712010-07-14 17:59:35 -07001611 Mutex::Autolock _l(mLock);
1612
Eric Laurenta4c5a552012-03-29 10:12:40 -07001613 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001614 if (inHwDev == NULL)
1615 return 0;
1616
John Grossmanee578c02012-07-23 17:05:46 -07001617 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001618 audio_io_handle_t id = nextUniqueId();
1619
John Grossmanee578c02012-07-23 17:05:46 -07001620 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001621 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001622 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1623 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001624 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001625 config.sample_rate,
1626 config.format,
1627 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001628 status);
1629
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001630 // If the input could not be opened with the requested parameters and we can handle the
1631 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1632 // 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 -07001633 if (status == BAD_VALUE &&
1634 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1635 (config.sample_rate <= 2 * reqSamplingRate) &&
1636 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001637 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001638 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001639 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001640 }
1641
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001642 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001643
Glenn Kasten46909e72013-02-26 09:20:22 -08001644#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001645 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1646 // or (re-)create if current Pipe is idle and does not match the new format
1647 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001648 enum {
1649 TEE_SINK_NO, // don't copy input
1650 TEE_SINK_NEW, // copy input using a new pipe
1651 TEE_SINK_OLD, // copy input using an existing pipe
1652 } kind;
1653 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1654 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001655 if (!mTeeSinkInputEnabled) {
1656 kind = TEE_SINK_NO;
1657 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001658 kind = TEE_SINK_NO;
1659 } else if (mRecordTeeSink == 0) {
1660 kind = TEE_SINK_NEW;
1661 } else if (mRecordTeeSink->getStrongCount() != 1) {
1662 kind = TEE_SINK_NO;
1663 } else if (format == mRecordTeeSink->format()) {
1664 kind = TEE_SINK_OLD;
1665 } else {
1666 kind = TEE_SINK_NEW;
1667 }
1668 switch (kind) {
1669 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001670 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001671 size_t numCounterOffers = 0;
1672 const NBAIO_Format offers[1] = {format};
1673 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1674 ALOG_ASSERT(index == 0);
1675 PipeReader *pipeReader = new PipeReader(*pipe);
1676 numCounterOffers = 0;
1677 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1678 ALOG_ASSERT(index == 0);
1679 mRecordTeeSink = pipe;
1680 mRecordTeeSource = pipeReader;
1681 teeSink = pipe;
1682 }
1683 break;
1684 case TEE_SINK_OLD:
1685 teeSink = mRecordTeeSink;
1686 break;
1687 case TEE_SINK_NO:
1688 default:
1689 break;
1690 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001691#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001692
Dima Zavin799a70e2011-04-18 16:57:27 -07001693 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1694
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001695 // Start record thread
1696 // RecorThread require both input and output device indication to forward to audio
1697 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001698 thread = new RecordThread(this,
1699 input,
1700 reqSamplingRate,
1701 reqChannels,
1702 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001703 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001704 *pDevices
1705#ifdef TEE_SINK
1706 , teeSink
1707#endif
1708 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001709 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001710 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001711 if (pSamplingRate != NULL) {
1712 *pSamplingRate = reqSamplingRate;
1713 }
1714 if (pFormat != NULL) {
1715 *pFormat = config.format;
1716 }
1717 if (pChannelMask != NULL) {
1718 *pChannelMask = reqChannels;
1719 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001720
Mathias Agopian65ab4712010-07-14 17:59:35 -07001721 // notify client processes of the new input creation
1722 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1723 return id;
1724 }
1725
1726 return 0;
1727}
1728
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001729status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001730{
Glenn Kastend96c5722012-04-25 13:44:49 -07001731 return closeInput_nonvirtual(input);
1732}
1733
1734status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1735{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001736 // keep strong reference on the record thread so that
1737 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001738 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001739 {
1740 Mutex::Autolock _l(mLock);
1741 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001742 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001743 return BAD_VALUE;
1744 }
1745
Steve Block3856b092011-10-20 11:56:00 +01001746 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001747 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001748 mRecordThreads.removeItem(input);
1749 }
1750 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001751 // The thread entity (active unit of execution) is no longer running here,
1752 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001753
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001754 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001755 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001756 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001757 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001758 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001759
1760 return NO_ERROR;
1761}
1762
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001763status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001764{
1765 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001766 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001767
1768 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1769 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001770 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001771 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001772
1773 return NO_ERROR;
1774}
1775
1776
1777int AudioFlinger::newAudioSessionId()
1778{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001779 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001780}
1781
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001782void AudioFlinger::acquireAudioSessionId(int audioSession)
1783{
1784 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001785 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001786 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001787 size_t num = mAudioSessionRefs.size();
1788 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001789 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001790 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1791 ref->mCnt++;
1792 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001793 return;
1794 }
1795 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001796 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1797 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001798}
1799
1800void AudioFlinger::releaseAudioSessionId(int audioSession)
1801{
1802 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001803 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001804 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001805 size_t num = mAudioSessionRefs.size();
1806 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001807 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001808 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1809 ref->mCnt--;
1810 ALOGV(" decremented refcount to %d", ref->mCnt);
1811 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001812 mAudioSessionRefs.removeAt(i);
1813 delete ref;
1814 purgeStaleEffects_l();
1815 }
1816 return;
1817 }
1818 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001819 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001820}
1821
1822void AudioFlinger::purgeStaleEffects_l() {
1823
Steve Block3856b092011-10-20 11:56:00 +01001824 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001825
1826 Vector< sp<EffectChain> > chains;
1827
1828 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1829 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1830 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1831 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001832 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1833 chains.push(ec);
1834 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001835 }
1836 }
1837 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1838 sp<RecordThread> t = mRecordThreads.valueAt(i);
1839 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1840 sp<EffectChain> ec = t->mEffectChains[j];
1841 chains.push(ec);
1842 }
1843 }
1844
1845 for (size_t i = 0; i < chains.size(); i++) {
1846 sp<EffectChain> ec = chains[i];
1847 int sessionid = ec->sessionId();
1848 sp<ThreadBase> t = ec->mThread.promote();
1849 if (t == 0) {
1850 continue;
1851 }
1852 size_t numsessionrefs = mAudioSessionRefs.size();
1853 bool found = false;
1854 for (size_t k = 0; k < numsessionrefs; k++) {
1855 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001856 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001857 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001858 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001859 found = true;
1860 break;
1861 }
1862 }
1863 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001864 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001865 // remove all effects from the chain
1866 while (ec->mEffects.size()) {
1867 sp<EffectModule> effect = ec->mEffects[0];
1868 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001869 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001870 if (effect->purgeHandles()) {
1871 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001872 }
1873 AudioSystem::unregisterEffect(effect->id());
1874 }
1875 }
1876 }
1877 return;
1878}
1879
Mathias Agopian65ab4712010-07-14 17:59:35 -07001880// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001881AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001882{
Glenn Kastena1117922012-01-26 10:53:32 -08001883 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001884}
1885
1886// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001887AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001888{
1889 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001890 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001891}
1892
1893// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001894AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001895{
Glenn Kastena1117922012-01-26 10:53:32 -08001896 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001897}
1898
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001899uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001901 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001902}
1903
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001904AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001905{
1906 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1907 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001908 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001909 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001910 return thread;
1911 }
1912 }
1913 return NULL;
1914}
1915
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001916audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001917{
1918 PlaybackThread *thread = primaryPlaybackThread_l();
1919
1920 if (thread == NULL) {
1921 return 0;
1922 }
1923
Eric Laurentf1c04f92012-08-28 14:26:53 -07001924 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001925}
1926
Eric Laurenta011e352012-03-29 15:51:43 -07001927sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1928 int triggerSession,
1929 int listenerSession,
1930 sync_event_callback_t callBack,
1931 void *cookie)
1932{
1933 Mutex::Autolock _l(mLock);
1934
1935 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1936 status_t playStatus = NAME_NOT_FOUND;
1937 status_t recStatus = NAME_NOT_FOUND;
1938 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1939 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1940 if (playStatus == NO_ERROR) {
1941 return event;
1942 }
1943 }
1944 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1945 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1946 if (recStatus == NO_ERROR) {
1947 return event;
1948 }
1949 }
1950 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
1951 mPendingSyncEvents.add(event);
1952 } else {
1953 ALOGV("createSyncEvent() invalid event %d", event->type());
1954 event.clear();
1955 }
1956 return event;
1957}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001958
Mathias Agopian65ab4712010-07-14 17:59:35 -07001959// ----------------------------------------------------------------------------
1960// Effect management
1961// ----------------------------------------------------------------------------
1962
1963
Glenn Kastenf587ba52012-01-26 16:25:10 -08001964status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001965{
1966 Mutex::Autolock _l(mLock);
1967 return EffectQueryNumberEffects(numEffects);
1968}
1969
Glenn Kastenf587ba52012-01-26 16:25:10 -08001970status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001971{
1972 Mutex::Autolock _l(mLock);
1973 return EffectQueryEffect(index, descriptor);
1974}
1975
Glenn Kasten5e92a782012-01-30 07:40:52 -08001976status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08001977 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001978{
1979 Mutex::Autolock _l(mLock);
1980 return EffectGetDescriptor(pUuid, descriptor);
1981}
1982
1983
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001984sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985 effect_descriptor_t *pDesc,
1986 const sp<IEffectClient>& effectClient,
1987 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001988 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001989 int sessionId,
1990 status_t *status,
1991 int *id,
1992 int *enabled)
1993{
1994 status_t lStatus = NO_ERROR;
1995 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001996 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001997
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001998 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001999 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002000 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002001
2002 if (pDesc == NULL) {
2003 lStatus = BAD_VALUE;
2004 goto Exit;
2005 }
2006
Eric Laurent84e9a102010-09-23 16:10:16 -07002007 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002008 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002009 lStatus = PERMISSION_DENIED;
2010 goto Exit;
2011 }
2012
Dima Zavinfce7a472011-04-19 22:30:36 -07002013 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002014 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002015 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002016 lStatus = PERMISSION_DENIED;
2017 goto Exit;
2018 }
2019
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002020 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002021 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002022 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07002023 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07002024 lStatus = BAD_VALUE;
2025 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07002026 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002027 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002028 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002029 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002030 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002031 }
2032 }
2033
Mathias Agopian65ab4712010-07-14 17:59:35 -07002034 {
2035 Mutex::Autolock _l(mLock);
2036
Mathias Agopian65ab4712010-07-14 17:59:35 -07002037
2038 if (!EffectIsNullUuid(&pDesc->uuid)) {
2039 // if uuid is specified, request effect descriptor
2040 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2041 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002042 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002043 goto Exit;
2044 }
2045 } else {
2046 // if uuid is not specified, look for an available implementation
2047 // of the required type in effect factory
2048 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002049 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002050 lStatus = BAD_VALUE;
2051 goto Exit;
2052 }
2053 uint32_t numEffects = 0;
2054 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002055 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002056 bool found = false;
2057
2058 lStatus = EffectQueryNumberEffects(&numEffects);
2059 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002060 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002061 goto Exit;
2062 }
2063 for (uint32_t i = 0; i < numEffects; i++) {
2064 lStatus = EffectQueryEffect(i, &desc);
2065 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002066 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002067 continue;
2068 }
2069 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2070 // If matching type found save effect descriptor. If the session is
2071 // 0 and the effect is not auxiliary, continue enumeration in case
2072 // an auxiliary version of this effect type is available
2073 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002074 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002075 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002076 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2077 break;
2078 }
2079 }
2080 }
2081 if (!found) {
2082 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002083 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002084 goto Exit;
2085 }
2086 // For same effect type, chose auxiliary version over insert version if
2087 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002088 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002089 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002090 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002091 }
2092 }
2093
2094 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002095 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002096 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2097 lStatus = INVALID_OPERATION;
2098 goto Exit;
2099 }
2100
Eric Laurent59255e42011-07-27 19:49:51 -07002101 // check recording permission for visualizer
2102 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2103 !recordingAllowed()) {
2104 lStatus = PERMISSION_DENIED;
2105 goto Exit;
2106 }
2107
Mathias Agopian65ab4712010-07-14 17:59:35 -07002108 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002109 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002110
2111 // If output is not specified try to find a matching audio session ID in one of the
2112 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002113 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2114 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002115 // Note: io is never 0 when creating an effect on an input
2116 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002117 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002118 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2119 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002120 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002121 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002122 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002123 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002124 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002125 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2126 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2127 io = mRecordThreads.keyAt(i);
2128 break;
2129 }
2130 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002131 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002132 // If no output thread contains the requested session ID, default to
2133 // first output. The effect chain will be moved to the correct output
2134 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002135 if (io == 0 && mPlaybackThreads.size()) {
2136 io = mPlaybackThreads.keyAt(0);
2137 }
Steve Block3856b092011-10-20 11:56:00 +01002138 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002139 }
2140 ThreadBase *thread = checkRecordThread_l(io);
2141 if (thread == NULL) {
2142 thread = checkPlaybackThread_l(io);
2143 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002144 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002145 lStatus = BAD_VALUE;
2146 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002147 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002148 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002149
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002150 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002151
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002152 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002153 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2154 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002155 if (handle != 0 && id != NULL) {
2156 *id = handle->id();
2157 }
2158 }
2159
2160Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002161 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002162 *status = lStatus;
2163 }
2164 return handle;
2165}
2166
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002167status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2168 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002169{
Steve Block3856b092011-10-20 11:56:00 +01002170 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002171 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002172 Mutex::Autolock _l(mLock);
2173 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002174 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002175 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002176 }
Eric Laurentde070132010-07-13 04:45:46 -07002177 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2178 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002179 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002180 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002181 }
Eric Laurentde070132010-07-13 04:45:46 -07002182 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2183 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002184 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002185 return BAD_VALUE;
2186 }
2187
2188 Mutex::Autolock _dl(dstThread->mLock);
2189 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002190 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002191
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192 return NO_ERROR;
2193}
2194
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002195// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002196status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002197 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002198 AudioFlinger::PlaybackThread *dstThread,
2199 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002200{
Steve Block3856b092011-10-20 11:56:00 +01002201 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002202 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002203
Eric Laurent59255e42011-07-27 19:49:51 -07002204 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002205 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002206 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002207 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002208 return INVALID_OPERATION;
2209 }
2210
Eric Laurent39e94f82010-07-28 01:32:47 -07002211 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002212 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002213 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002214 // removed.
2215 srcThread->removeEffectChain_l(chain);
2216
2217 // transfer all effects one by one so that new effect chain is created on new thread with
2218 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002219 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002220 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002221 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002222 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2223 while (effect != 0) {
2224 srcThread->removeEffect_l(effect);
2225 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002226 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2227 if (effect->state() == EffectModule::ACTIVE ||
2228 effect->state() == EffectModule::STOPPING) {
2229 effect->start();
2230 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002231 // if the move request is not received from audio policy manager, the effect must be
2232 // re-registered with the new strategy and output
2233 if (dstChain == 0) {
2234 dstChain = effect->chain().promote();
2235 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002236 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002237 srcThread->addEffect_l(effect);
2238 return NO_INIT;
2239 }
2240 strategy = dstChain->strategy();
2241 }
2242 if (reRegister) {
2243 AudioSystem::unregisterEffect(effect->id());
2244 AudioSystem::registerEffect(&effect->desc(),
2245 dstOutput,
2246 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002247 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002248 effect->id());
2249 }
Eric Laurentde070132010-07-13 04:45:46 -07002250 effect = chain->getEffectFromId_l(0);
2251 }
2252
2253 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254}
2255
Glenn Kastenda6ef132013-01-10 12:31:01 -08002256struct Entry {
2257#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2258 char mName[MAX_NAME];
2259};
2260
2261int comparEntry(const void *p1, const void *p2)
2262{
2263 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2264}
2265
Glenn Kasten46909e72013-02-26 09:20:22 -08002266#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002267void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002268{
Eric Laurent81784c32012-11-19 14:55:58 -08002269 NBAIO_Source *teeSource = source.get();
2270 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002271 // .wav rotation
2272 // There is a benign race condition if 2 threads call this simultaneously.
2273 // They would both traverse the directory, but the result would simply be
2274 // failures at unlink() which are ignored. It's also unlikely since
2275 // normally dumpsys is only done by bugreport or from the command line.
2276 char teePath[32+256];
2277 strcpy(teePath, "/data/misc/media");
2278 size_t teePathLen = strlen(teePath);
2279 DIR *dir = opendir(teePath);
2280 teePath[teePathLen++] = '/';
2281 if (dir != NULL) {
2282#define MAX_SORT 20 // number of entries to sort
2283#define MAX_KEEP 10 // number of entries to keep
2284 struct Entry entries[MAX_SORT];
2285 size_t entryCount = 0;
2286 while (entryCount < MAX_SORT) {
2287 struct dirent de;
2288 struct dirent *result = NULL;
2289 int rc = readdir_r(dir, &de, &result);
2290 if (rc != 0) {
2291 ALOGW("readdir_r failed %d", rc);
2292 break;
2293 }
2294 if (result == NULL) {
2295 break;
2296 }
2297 if (result != &de) {
2298 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2299 break;
2300 }
2301 // ignore non .wav file entries
2302 size_t nameLen = strlen(de.d_name);
2303 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2304 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2305 continue;
2306 }
2307 strcpy(entries[entryCount++].mName, de.d_name);
2308 }
2309 (void) closedir(dir);
2310 if (entryCount > MAX_KEEP) {
2311 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2312 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2313 strcpy(&teePath[teePathLen], entries[i].mName);
2314 (void) unlink(teePath);
2315 }
2316 }
2317 } else {
2318 if (fd >= 0) {
2319 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2320 }
2321 }
Eric Laurent81784c32012-11-19 14:55:58 -08002322 char teeTime[16];
2323 struct timeval tv;
2324 gettimeofday(&tv, NULL);
2325 struct tm tm;
2326 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002327 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2328 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2329 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2330 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002331 if (teeFd >= 0) {
2332 char wavHeader[44];
2333 memcpy(wavHeader,
2334 "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",
2335 sizeof(wavHeader));
2336 NBAIO_Format format = teeSource->format();
2337 unsigned channelCount = Format_channelCount(format);
2338 ALOG_ASSERT(channelCount <= FCC_2);
2339 uint32_t sampleRate = Format_sampleRate(format);
2340 wavHeader[22] = channelCount; // number of channels
2341 wavHeader[24] = sampleRate; // sample rate
2342 wavHeader[25] = sampleRate >> 8;
2343 wavHeader[32] = channelCount * 2; // block alignment
2344 write(teeFd, wavHeader, sizeof(wavHeader));
2345 size_t total = 0;
2346 bool firstRead = true;
2347 for (;;) {
2348#define TEE_SINK_READ 1024
2349 short buffer[TEE_SINK_READ * FCC_2];
2350 size_t count = TEE_SINK_READ;
2351 ssize_t actual = teeSource->read(buffer, count,
2352 AudioBufferProvider::kInvalidPTS);
2353 bool wasFirstRead = firstRead;
2354 firstRead = false;
2355 if (actual <= 0) {
2356 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2357 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002358 }
Eric Laurent81784c32012-11-19 14:55:58 -08002359 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002360 }
Eric Laurent81784c32012-11-19 14:55:58 -08002361 ALOG_ASSERT(actual <= (ssize_t)count);
2362 write(teeFd, buffer, actual * channelCount * sizeof(short));
2363 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002364 }
Eric Laurent81784c32012-11-19 14:55:58 -08002365 lseek(teeFd, (off_t) 4, SEEK_SET);
2366 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2367 write(teeFd, &temp, sizeof(temp));
2368 lseek(teeFd, (off_t) 40, SEEK_SET);
2369 temp = total * channelCount * sizeof(short);
2370 write(teeFd, &temp, sizeof(temp));
2371 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002372 if (fd >= 0) {
2373 fdprintf(fd, "tee copied to %s\n", teePath);
2374 }
Eric Laurent59255e42011-07-27 19:49:51 -07002375 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002376 if (fd >= 0) {
2377 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2378 }
Eric Laurent59255e42011-07-27 19:49:51 -07002379 }
2380 }
2381}
Glenn Kasten46909e72013-02-26 09:20:22 -08002382#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002383
Mathias Agopian65ab4712010-07-14 17:59:35 -07002384// ----------------------------------------------------------------------------
2385
2386status_t AudioFlinger::onTransact(
2387 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2388{
2389 return BnAudioFlinger::onTransact(code, data, reply, flags);
2390}
2391
Mathias Agopian65ab4712010-07-14 17:59:35 -07002392}; // namespace android