blob: e81267fea16e2e2bd7bf2492d3a93ab95bf83678 [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 Kastendd4abb52013-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
Eric Laurentca7cc822012-11-19 14:55:58 -080041//#include <private/media/AudioTrackShared.h>
42//#include <private/media/AudioEffectShared.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070043
Dima Zavin64760242011-05-11 14:15:23 -070044#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070045#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070046
47#include "AudioMixer.h"
48#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080049#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
Mathias Agopian65ab4712010-07-14 17:59:35 -070051#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070052#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070053#include <audio_effects/effect_ns.h>
54#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070055
Glenn Kasten3b21c502011-12-15 09:52:39 -080056#include <audio_utils/primitives.h>
57
Eric Laurentfeb0db62011-07-22 09:04:31 -070058#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080059
John Grossman4ff14ba2012-02-08 16:37:41 -080060#include <common_time/cc_helper.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080061//#include <common_time/local_clock.h>
Glenn Kasten58912562012-04-03 10:45:00 -070062
Glenn Kasten011aa652013-01-18 15:09:48 -080063#include <media/IMediaLogService.h>
64
Glenn Kastendd4abb52013-01-10 12:31:01 -080065#include <media/nbaio/Pipe.h>
66#include <media/nbaio/PipeReader.h>
67
Mathias Agopian65ab4712010-07-14 17:59:35 -070068// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070069
John Grossman1c345192012-03-27 14:00:17 -070070// Note: the following macro is used for extremely verbose logging message. In
71// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
72// 0; but one side effect of this is to turn all LOGV's as well. Some messages
73// are so verbose that we want to suppress them even when we have ALOG_ASSERT
74// turned on. Do not uncomment the #def below unless you really know what you
75// are doing and want to see all of the extremely verbose messages.
76//#define VERY_VERY_VERBOSE_LOGGING
77#ifdef VERY_VERY_VERBOSE_LOGGING
78#define ALOGVV ALOGV
79#else
80#define ALOGVV(a...) do { } while(0)
81#endif
Eric Laurentde070132010-07-13 04:45:46 -070082
Mathias Agopian65ab4712010-07-14 17:59:35 -070083namespace android {
84
Glenn Kastenec1d6b52011-12-12 09:04:45 -080085static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
86static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070087
Glenn Kasten58912562012-04-03 10:45:00 -070088
John Grossman4ff14ba2012-02-08 16:37:41 -080089nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080090
Eric Laurentca7cc822012-11-19 14:55:58 -080091uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070092
Glenn Kastendd0bda02013-02-26 09:20:22 -080093#ifdef TEE_SINK
Glenn Kastendd4abb52013-01-10 12:31:01 -080094bool AudioFlinger::mTeeSinkInputEnabled = false;
95bool AudioFlinger::mTeeSinkOutputEnabled = false;
96bool AudioFlinger::mTeeSinkTrackEnabled = false;
97
98size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
99size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
100size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kastendd0bda02013-02-26 09:20:22 -0800101#endif
Glenn Kastendd4abb52013-01-10 12:31:01 -0800102
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103// ----------------------------------------------------------------------------
104
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700105static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700106{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700107 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700108 int rc;
109
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700110 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
111 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
112 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
113 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700114 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700115 }
116 rc = audio_hw_device_open(mod, dev);
117 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
118 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
119 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700120 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700121 }
122 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
123 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
124 rc = BAD_VALUE;
125 goto out;
126 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700127 return 0;
128
129out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700130 *dev = NULL;
131 return rc;
132}
133
Mathias Agopian65ab4712010-07-14 17:59:35 -0700134// ----------------------------------------------------------------------------
135
136AudioFlinger::AudioFlinger()
137 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800138 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700139 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800140 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800141 mMasterMute(false),
142 mNextUniqueId(1),
143 mMode(AUDIO_MODE_INVALID),
144 mBtNrecIsOff(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145{
Glenn Kasten011aa652013-01-18 15:09:48 -0800146 char value[PROPERTY_VALUE_MAX];
147 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
148 if (doLog) {
149 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
150 }
Glenn Kastendd0bda02013-02-26 09:20:22 -0800151#ifdef TEE_SINK
Glenn Kastendd4abb52013-01-10 12:31:01 -0800152 (void) property_get("ro.debuggable", value, "0");
153 int debuggable = atoi(value);
154 int teeEnabled = 0;
155 if (debuggable) {
156 (void) property_get("af.tee", value, "0");
157 teeEnabled = atoi(value);
158 }
159 if (teeEnabled & 1)
160 mTeeSinkInputEnabled = true;
161 if (teeEnabled & 2)
162 mTeeSinkOutputEnabled = true;
163 if (teeEnabled & 4)
164 mTeeSinkTrackEnabled = true;
Glenn Kastendd0bda02013-02-26 09:20:22 -0800165#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700166}
167
168void AudioFlinger::onFirstRef()
169{
Dima Zavin799a70e2011-04-18 16:57:27 -0700170 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700171
Eric Laurent93575202011-01-18 18:39:02 -0800172 Mutex::Autolock _l(mLock);
173
Dima Zavin799a70e2011-04-18 16:57:27 -0700174 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800175 char val_str[PROPERTY_VALUE_MAX] = { 0 };
176 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
177 uint32_t int_val;
178 if (1 == sscanf(val_str, "%u", &int_val)) {
179 mStandbyTimeInNsecs = milliseconds(int_val);
180 ALOGI("Using %u mSec as standby time.", int_val);
181 } else {
182 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
183 ALOGI("Using default %u mSec as standby time.",
184 (uint32_t)(mStandbyTimeInNsecs / 1000000));
185 }
186 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700187
Eric Laurenta4c5a552012-03-29 10:12:40 -0700188 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189}
190
191AudioFlinger::~AudioFlinger()
192{
193 while (!mRecordThreads.isEmpty()) {
Glenn Kasten2662ac92012-07-30 10:59:30 -0700194 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700195 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196 }
197 while (!mPlaybackThreads.isEmpty()) {
Glenn Kasten2662ac92012-07-30 10:59:30 -0700198 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700199 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700200 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700201
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800202 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
203 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700204 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
205 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700206 }
207}
208
Eric Laurenta4c5a552012-03-29 10:12:40 -0700209static const char * const audio_interfaces[] = {
210 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
211 AUDIO_HARDWARE_MODULE_ID_A2DP,
212 AUDIO_HARDWARE_MODULE_ID_USB,
213};
214#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
215
John Grossmanee578c02012-07-23 17:05:46 -0700216AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
217 audio_module_handle_t module,
218 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700219{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700220 // if module is 0, the request comes from an old policy manager and we should load
221 // well known modules
222 if (module == 0) {
223 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
224 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
225 loadHwModule_l(audio_interfaces[i]);
226 }
Eric Laurent88959252012-08-28 14:26:53 -0700227 // then try to find a module supporting the requested device.
228 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
229 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
230 audio_hw_device_t *dev = audioHwDevice->hwDevice();
231 if ((dev->get_supported_devices != NULL) &&
232 (dev->get_supported_devices(dev) & devices) == devices)
233 return audioHwDevice;
234 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700235 } else {
236 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700237 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
238 if (audioHwDevice != NULL) {
239 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700240 }
241 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700242
Dima Zavin799a70e2011-04-18 16:57:27 -0700243 return NULL;
244}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700245
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700246void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700247{
248 const size_t SIZE = 256;
249 char buffer[SIZE];
250 String8 result;
251
252 result.append("Clients:\n");
253 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800254 sp<Client> client = mClients.valueAt(i).promote();
255 if (client != 0) {
256 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
257 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700258 }
259 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700260
261 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800262 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700263 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
264 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800265 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700266 result.append(buffer);
267 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269}
270
271
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700272void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273{
274 const size_t SIZE = 256;
275 char buffer[SIZE];
276 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800277 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278
John Grossman4ff14ba2012-02-08 16:37:41 -0800279 snprintf(buffer, SIZE, "Hardware status: %d\n"
280 "Standby Time mSec: %u\n",
281 hardwareStatus,
282 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700283 result.append(buffer);
284 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285}
286
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700287void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700288{
289 const size_t SIZE = 256;
290 char buffer[SIZE];
291 String8 result;
292 snprintf(buffer, SIZE, "Permission Denial: "
293 "can't dump AudioFlinger from pid=%d, uid=%d\n",
294 IPCThreadState::self()->getCallingPid(),
295 IPCThreadState::self()->getCallingUid());
296 result.append(buffer);
297 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700298}
299
Eric Laurentca7cc822012-11-19 14:55:58 -0800300bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700301{
302 bool locked = false;
303 for (int i = 0; i < kDumpLockRetries; ++i) {
304 if (mutex.tryLock() == NO_ERROR) {
305 locked = true;
306 break;
307 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800308 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309 }
310 return locked;
311}
312
313status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
314{
Glenn Kasten44deb052012-02-05 18:09:08 -0800315 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 dumpPermissionDenial(fd, args);
317 } else {
318 // get state of hardware lock
Eric Laurentca7cc822012-11-19 14:55:58 -0800319 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 if (!hardwareLocked) {
321 String8 result(kHardwareLockedString);
322 write(fd, result.string(), result.size());
323 } else {
324 mHardwareLock.unlock();
325 }
326
Eric Laurentca7cc822012-11-19 14:55:58 -0800327 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328
329 // failed to lock - AudioFlinger is probably deadlocked
330 if (!locked) {
331 String8 result(kDeadlockedString);
332 write(fd, result.string(), result.size());
333 }
334
335 dumpClients(fd, args);
336 dumpInternals(fd, args);
337
338 // dump playback threads
339 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
340 mPlaybackThreads.valueAt(i)->dump(fd, args);
341 }
342
343 // dump record threads
344 for (size_t i = 0; i < mRecordThreads.size(); i++) {
345 mRecordThreads.valueAt(i)->dump(fd, args);
346 }
347
Dima Zavin799a70e2011-04-18 16:57:27 -0700348 // dump all hardware devs
349 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700350 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700351 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352 }
Glenn Kasten8c327342012-09-30 12:29:28 -0700353
Glenn Kastendd0bda02013-02-26 09:20:22 -0800354#ifdef TEE_SINK
Glenn Kasten8c327342012-09-30 12:29:28 -0700355 // dump the serially shared record tee sink
356 if (mRecordTeeSource != 0) {
357 dumpTee(fd, mRecordTeeSource);
358 }
Glenn Kastendd0bda02013-02-26 09:20:22 -0800359#endif
Glenn Kasten8c327342012-09-30 12:29:28 -0700360
Glenn Kastena2049222012-06-22 17:21:07 -0700361 if (locked) {
362 mLock.unlock();
363 }
Glenn Kasten011aa652013-01-18 15:09:48 -0800364
365 // append a copy of media.log here by forwarding fd to it, but don't attempt
366 // to lookup the service if it's not running, as it will block for a second
367 if (mLogMemoryDealer != 0) {
368 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
369 if (binder != 0) {
370 fdprintf(fd, "\nmedia.log:\n");
371 Vector<String16> args;
372 binder->dump(fd, args);
373 }
374 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375 }
376 return NO_ERROR;
377}
378
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800379sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
380{
381 // If pid is already in the mClients wp<> map, then use that entry
382 // (for which promote() is always != 0), otherwise create a new entry and Client.
383 sp<Client> client = mClients.valueFor(pid).promote();
384 if (client == 0) {
385 client = new Client(this, pid);
386 mClients.add(pid, client);
387 }
388
389 return client;
390}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391
Glenn Kasten011aa652013-01-18 15:09:48 -0800392sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
393{
394 if (mLogMemoryDealer == 0) {
395 return new NBLog::Writer();
396 }
397 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
398 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
399 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
400 if (binder != 0) {
401 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
402 }
403 return writer;
404}
405
406void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
407{
Glenn Kasten5fa419b2013-02-04 08:15:34 -0800408 if (writer == 0) {
409 return;
410 }
Glenn Kasten011aa652013-01-18 15:09:48 -0800411 sp<IMemory> iMemory(writer->getIMemory());
412 if (iMemory == 0) {
413 return;
414 }
415 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
416 if (binder != 0) {
417 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
418 // Now the media.log remote reference to IMemory is gone.
419 // When our last local reference to IMemory also drops to zero,
420 // the IMemory destructor will deallocate the region from mMemoryDealer.
421 }
422}
423
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424// IAudioFlinger interface
425
426
427sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800428 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700429 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800430 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700431 audio_channel_mask_t channelMask,
Glenn Kasten7da35f22012-11-14 12:54:39 -0800432 size_t frameCount,
Glenn Kastenc2674152012-11-06 15:03:34 -0800433 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700434 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800435 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800436 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700437 int *sessionId,
438 status_t *status)
439{
440 sp<PlaybackThread::Track> track;
441 sp<TrackHandle> trackHandle;
442 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443 status_t lStatus;
444 int lSessionId;
445
Glenn Kasten263709e2012-01-06 08:40:01 -0800446 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
447 // but if someone uses binder directly they could bypass that and cause us to crash
448 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000449 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700450 lStatus = BAD_VALUE;
451 goto Exit;
452 }
453
Glenn Kasten520a9af2012-06-21 12:56:37 -0700454 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
455 // and we don't yet support 8.24 or 32-bit PCM
456 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
457 ALOGE("createTrack() invalid format %d", format);
458 lStatus = BAD_VALUE;
459 goto Exit;
460 }
461
Mathias Agopian65ab4712010-07-14 17:59:35 -0700462 {
463 Mutex::Autolock _l(mLock);
464 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700465 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000467 ALOGE("unknown output thread");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468 lStatus = BAD_VALUE;
469 goto Exit;
470 }
471
Glenn Kastenf37971f2012-02-03 11:06:53 -0800472 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800473 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700474
Steve Block3856b092011-10-20 11:56:00 +0100475 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700476 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700477 // check if an effect chain with the same session ID is present on another
478 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700479 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700480 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
481 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700482 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700483 if (sessions & PlaybackThread::EFFECT_SESSION) {
484 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700485 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700486 }
Eric Laurentde070132010-07-13 04:45:46 -0700487 }
488 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700489 lSessionId = *sessionId;
490 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700491 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700492 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700493 if (sessionId != NULL) {
494 *sessionId = lSessionId;
495 }
496 }
Steve Block3856b092011-10-20 11:56:00 +0100497 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700498
499 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800500 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700501
502 // move effect chain to this output thread if an effect on same session was waiting
503 // for a track to be created
504 if (lStatus == NO_ERROR && effectThread != NULL) {
505 Mutex::Autolock _dl(thread->mLock);
506 Mutex::Autolock _sl(effectThread->mLock);
507 moveEffectChain_l(lSessionId, effectThread, thread, true);
508 }
Eric Laurenta011e352012-03-29 15:51:43 -0700509
510 // Look for sync events awaiting for a session to be used.
511 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
512 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
513 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700514 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700515 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700516 } else {
517 mPendingSyncEvents[i]->cancel();
518 }
Eric Laurenta011e352012-03-29 15:51:43 -0700519 mPendingSyncEvents.removeAt(i);
520 i--;
521 }
522 }
523 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700524 }
525 if (lStatus == NO_ERROR) {
526 trackHandle = new TrackHandle(track);
527 } else {
528 // remove local strong reference to Client before deleting the Track so that the Client
529 // destructor is called by the TrackBase destructor with mLock held
530 client.clear();
531 track.clear();
532 }
533
534Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700535 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700536 *status = lStatus;
537 }
538 return trackHandle;
539}
540
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800541uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700542{
543 Mutex::Autolock _l(mLock);
544 PlaybackThread *thread = checkPlaybackThread_l(output);
545 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000546 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547 return 0;
548 }
549 return thread->sampleRate();
550}
551
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800552int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553{
554 Mutex::Autolock _l(mLock);
555 PlaybackThread *thread = checkPlaybackThread_l(output);
556 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000557 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558 return 0;
559 }
560 return thread->channelCount();
561}
562
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800563audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700564{
565 Mutex::Autolock _l(mLock);
566 PlaybackThread *thread = checkPlaybackThread_l(output);
567 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000568 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800569 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700570 }
571 return thread->format();
572}
573
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800574size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575{
576 Mutex::Autolock _l(mLock);
577 PlaybackThread *thread = checkPlaybackThread_l(output);
578 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000579 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580 return 0;
581 }
Glenn Kasten58912562012-04-03 10:45:00 -0700582 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
583 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584 return thread->frameCount();
585}
586
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800587uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700588{
589 Mutex::Autolock _l(mLock);
590 PlaybackThread *thread = checkPlaybackThread_l(output);
591 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000592 ALOGW("latency() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593 return 0;
594 }
595 return thread->latency();
596}
597
598status_t AudioFlinger::setMasterVolume(float value)
599{
Eric Laurenta1884f92011-08-23 08:25:03 -0700600 status_t ret = initCheck();
601 if (ret != NO_ERROR) {
602 return ret;
603 }
604
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605 // check calling permissions
606 if (!settingsAllowed()) {
607 return PERMISSION_DENIED;
608 }
609
Eric Laurenta4c5a552012-03-29 10:12:40 -0700610 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700611 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700612
John Grossmanee578c02012-07-23 17:05:46 -0700613 // Set master volume in the HALs which support it.
614 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
615 AutoMutex lock(mHardwareLock);
616 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800617
John Grossmanee578c02012-07-23 17:05:46 -0700618 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
619 if (dev->canSetMasterVolume()) {
620 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800621 }
John Grossmanee578c02012-07-23 17:05:46 -0700622 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700624
John Grossmanee578c02012-07-23 17:05:46 -0700625 // Now set the master volume in each playback thread. Playback threads
626 // assigned to HALs which do not have master volume support will apply
627 // master volume during the mix operation. Threads with HALs which do
628 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800629 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700630 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631
632 return NO_ERROR;
633}
634
Glenn Kastenf78aee72012-01-04 11:00:47 -0800635status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636{
Eric Laurenta1884f92011-08-23 08:25:03 -0700637 status_t ret = initCheck();
638 if (ret != NO_ERROR) {
639 return ret;
640 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641
642 // check calling permissions
643 if (!settingsAllowed()) {
644 return PERMISSION_DENIED;
645 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800646 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000647 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648 return BAD_VALUE;
649 }
650
651 { // scope for the lock
652 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700653 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700655 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656 mHardwareStatus = AUDIO_HW_IDLE;
657 }
658
659 if (NO_ERROR == ret) {
660 Mutex::Autolock _l(mLock);
661 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800662 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700663 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664 }
665
666 return ret;
667}
668
669status_t AudioFlinger::setMicMute(bool state)
670{
Eric Laurenta1884f92011-08-23 08:25:03 -0700671 status_t ret = initCheck();
672 if (ret != NO_ERROR) {
673 return ret;
674 }
675
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676 // check calling permissions
677 if (!settingsAllowed()) {
678 return PERMISSION_DENIED;
679 }
680
681 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700682 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700684 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 mHardwareStatus = AUDIO_HW_IDLE;
686 return ret;
687}
688
689bool AudioFlinger::getMicMute() const
690{
Eric Laurenta1884f92011-08-23 08:25:03 -0700691 status_t ret = initCheck();
692 if (ret != NO_ERROR) {
693 return false;
694 }
695
Dima Zavinfce7a472011-04-19 22:30:36 -0700696 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800697 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700698 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700700 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 mHardwareStatus = AUDIO_HW_IDLE;
702 return state;
703}
704
705status_t AudioFlinger::setMasterMute(bool muted)
706{
John Grossmand8f178d2012-07-20 14:51:35 -0700707 status_t ret = initCheck();
708 if (ret != NO_ERROR) {
709 return ret;
710 }
711
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 // check calling permissions
713 if (!settingsAllowed()) {
714 return PERMISSION_DENIED;
715 }
716
John Grossmanee578c02012-07-23 17:05:46 -0700717 Mutex::Autolock _l(mLock);
718 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700719
John Grossmanee578c02012-07-23 17:05:46 -0700720 // Set master mute in the HALs which support it.
721 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
722 AutoMutex lock(mHardwareLock);
723 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700724
John Grossmanee578c02012-07-23 17:05:46 -0700725 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
726 if (dev->canSetMasterMute()) {
727 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700728 }
John Grossmanee578c02012-07-23 17:05:46 -0700729 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700730 }
731
John Grossmanee578c02012-07-23 17:05:46 -0700732 // Now set the master mute in each playback thread. Playback threads
733 // assigned to HALs which do not have master mute support will apply master
734 // mute during the mix operation. Threads with HALs which do support master
735 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800736 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700737 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700738
739 return NO_ERROR;
740}
741
742float AudioFlinger::masterVolume() const
743{
Glenn Kasten98067102011-12-13 11:47:54 -0800744 Mutex::Autolock _l(mLock);
745 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700746}
747
748bool AudioFlinger::masterMute() const
749{
Glenn Kasten98067102011-12-13 11:47:54 -0800750 Mutex::Autolock _l(mLock);
751 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752}
753
John Grossman4ff14ba2012-02-08 16:37:41 -0800754float AudioFlinger::masterVolume_l() const
755{
John Grossman4ff14ba2012-02-08 16:37:41 -0800756 return mMasterVolume;
757}
758
John Grossmand8f178d2012-07-20 14:51:35 -0700759bool AudioFlinger::masterMute_l() const
760{
John Grossmanee578c02012-07-23 17:05:46 -0700761 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700762}
763
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800764status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
765 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766{
767 // check calling permissions
768 if (!settingsAllowed()) {
769 return PERMISSION_DENIED;
770 }
771
Glenn Kasten263709e2012-01-06 08:40:01 -0800772 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000773 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700774 return BAD_VALUE;
775 }
776
777 AutoMutex lock(mLock);
778 PlaybackThread *thread = NULL;
779 if (output) {
780 thread = checkPlaybackThread_l(output);
781 if (thread == NULL) {
782 return BAD_VALUE;
783 }
784 }
785
786 mStreamTypes[stream].volume = value;
787
788 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800789 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700790 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791 }
792 } else {
793 thread->setStreamVolume(stream, value);
794 }
795
796 return NO_ERROR;
797}
798
Glenn Kastenfff6d712012-01-12 16:38:12 -0800799status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800{
801 // check calling permissions
802 if (!settingsAllowed()) {
803 return PERMISSION_DENIED;
804 }
805
Glenn Kasten263709e2012-01-06 08:40:01 -0800806 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700807 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000808 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809 return BAD_VALUE;
810 }
811
Eric Laurent93575202011-01-18 18:39:02 -0800812 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700813 mStreamTypes[stream].mute = muted;
814 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700815 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816
817 return NO_ERROR;
818}
819
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800820float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700821{
Glenn Kasten263709e2012-01-06 08:40:01 -0800822 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823 return 0.0f;
824 }
825
826 AutoMutex lock(mLock);
827 float volume;
828 if (output) {
829 PlaybackThread *thread = checkPlaybackThread_l(output);
830 if (thread == NULL) {
831 return 0.0f;
832 }
833 volume = thread->streamVolume(stream);
834 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800835 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700836 }
837
838 return volume;
839}
840
Glenn Kastenfff6d712012-01-12 16:38:12 -0800841bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700842{
Glenn Kasten263709e2012-01-06 08:40:01 -0800843 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844 return true;
845 }
846
Glenn Kasten6637baa2012-01-09 09:40:36 -0800847 AutoMutex lock(mLock);
848 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849}
850
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800851status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852{
Glenn Kasten411e4472012-11-02 10:00:06 -0700853 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
854 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurentca7cc822012-11-19 14:55:58 -0800855
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856 // check calling permissions
857 if (!settingsAllowed()) {
858 return PERMISSION_DENIED;
859 }
860
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 // ioHandle == 0 means the parameters are global to the audio hardware interface
862 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700863 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700864 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800865 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700866 AutoMutex lock(mHardwareLock);
867 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
868 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
869 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
870 status_t result = dev->set_parameters(dev, keyValuePairs.string());
871 final_result = result ?: final_result;
872 }
873 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800874 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700875 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
876 AudioParameter param = AudioParameter(keyValuePairs);
877 String8 value;
878 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700879 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
880 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700881 for (size_t i = 0; i < mRecordThreads.size(); i++) {
882 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurent88959252012-08-28 14:26:53 -0700883 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700884 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
885 // collect all of the thread's session IDs
886 KeyedVector<int, bool> ids = thread->sessionIds();
887 // suspend effects associated with those session IDs
888 for (size_t j = 0; j < ids.size(); ++j) {
889 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700890 thread->setEffectSuspended(FX_IID_AEC,
891 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700892 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700893 thread->setEffectSuspended(FX_IID_NS,
894 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700895 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700896 }
897 }
Eric Laurentbee53372011-08-29 12:42:48 -0700898 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700899 }
900 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700901 String8 screenState;
902 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
903 bool isOff = screenState == "off";
Eric Laurentca7cc822012-11-19 14:55:58 -0800904 if (isOff != (AudioFlinger::mScreenState & 1)) {
905 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700906 }
907 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700908 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 }
910
911 // hold a strong ref on thread in case closeOutput() or closeInput() is called
912 // and the thread is exited once the lock is released
913 sp<ThreadBase> thread;
914 {
915 Mutex::Autolock _l(mLock);
916 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700917 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800919 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700920 // indicate output device change to all input threads for pre processing
921 AudioParameter param = AudioParameter(keyValuePairs);
922 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700923 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
924 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700925 for (size_t i = 0; i < mRecordThreads.size(); i++) {
926 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
927 }
928 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700929 }
930 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800931 if (thread != 0) {
932 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933 }
934 return BAD_VALUE;
935}
936
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800937String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938{
Glenn Kasten411e4472012-11-02 10:00:06 -0700939 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
940 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941
Eric Laurenta4c5a552012-03-29 10:12:40 -0700942 Mutex::Autolock _l(mLock);
943
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700945 String8 out_s8;
946
Dima Zavin799a70e2011-04-18 16:57:27 -0700947 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800948 char *s;
949 {
950 AutoMutex lock(mHardwareLock);
951 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700952 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800953 s = dev->get_parameters(dev, keys.string());
954 mHardwareStatus = AUDIO_HW_IDLE;
955 }
John Grossmanef7740b2012-02-09 11:28:36 -0800956 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700957 free(s);
958 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700959 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 }
961
Mathias Agopian65ab4712010-07-14 17:59:35 -0700962 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
963 if (playbackThread != NULL) {
964 return playbackThread->getParameters(keys);
965 }
966 RecordThread *recordThread = checkRecordThread_l(ioHandle);
967 if (recordThread != NULL) {
968 return recordThread->getParameters(keys);
969 }
970 return String8("");
971}
972
Glenn Kastendd8104c2012-07-02 12:42:44 -0700973size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
974 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975{
Eric Laurenta1884f92011-08-23 08:25:03 -0700976 status_t ret = initCheck();
977 if (ret != NO_ERROR) {
978 return 0;
979 }
980
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800981 AutoMutex lock(mHardwareLock);
982 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700983 struct audio_config config = {
984 sample_rate: sampleRate,
Glenn Kastendd8104c2012-07-02 12:42:44 -0700985 channel_mask: channelMask,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700986 format: format,
987 };
John Grossmanee578c02012-07-23 17:05:46 -0700988 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
989 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800990 mHardwareStatus = AUDIO_HW_IDLE;
991 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992}
993
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800994unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996 Mutex::Autolock _l(mLock);
997
998 RecordThread *recordThread = checkRecordThread_l(ioHandle);
999 if (recordThread != NULL) {
1000 return recordThread->getInputFramesLost();
1001 }
1002 return 0;
1003}
1004
1005status_t AudioFlinger::setVoiceVolume(float value)
1006{
Eric Laurenta1884f92011-08-23 08:25:03 -07001007 status_t ret = initCheck();
1008 if (ret != NO_ERROR) {
1009 return ret;
1010 }
1011
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012 // check calling permissions
1013 if (!settingsAllowed()) {
1014 return PERMISSION_DENIED;
1015 }
1016
1017 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001018 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001019 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001020 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021 mHardwareStatus = AUDIO_HW_IDLE;
1022
1023 return ret;
1024}
1025
Glenn Kastenb59a5022012-11-16 12:01:44 -08001026status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001027 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028{
1029 status_t status;
1030
1031 Mutex::Autolock _l(mLock);
1032
1033 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1034 if (playbackThread != NULL) {
1035 return playbackThread->getRenderPosition(halFrames, dspFrames);
1036 }
1037
1038 return BAD_VALUE;
1039}
1040
1041void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1042{
1043
1044 Mutex::Autolock _l(mLock);
1045
Glenn Kastenbb001922012-02-03 11:10:26 -08001046 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047 if (mNotificationClients.indexOfKey(pid) < 0) {
1048 sp<NotificationClient> notificationClient = new NotificationClient(this,
1049 client,
1050 pid);
Steve Block3856b092011-10-20 11:56:00 +01001051 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052
1053 mNotificationClients.add(pid, notificationClient);
1054
1055 sp<IBinder> binder = client->asBinder();
1056 binder->linkToDeath(notificationClient);
1057
1058 // the config change is always sent from playback or record threads to avoid deadlock
1059 // with AudioSystem::gLock
1060 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentef6be0b2012-09-13 11:18:23 -07001061 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 }
1063
1064 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentef6be0b2012-09-13 11:18:23 -07001065 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001066 }
1067 }
1068}
1069
1070void AudioFlinger::removeNotificationClient(pid_t pid)
1071{
1072 Mutex::Autolock _l(mLock);
1073
Glenn Kastena3b09252012-01-20 09:19:01 -08001074 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001075
Steve Block3856b092011-10-20 11:56:00 +01001076 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001077 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001078 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001079 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001080 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001081 ALOGV(" pid %d @ %d", ref->mPid, i);
1082 if (ref->mPid == pid) {
1083 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001084 mAudioSessionRefs.removeAt(i);
1085 delete ref;
1086 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001087 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001088 } else {
1089 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001090 }
1091 }
1092 if (removed) {
1093 purgeStaleEffects_l();
1094 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095}
1096
1097// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001098void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001099{
1100 size_t size = mNotificationClients.size();
1101 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001102 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1103 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104 }
1105}
1106
1107// removeClient_l() must be called with AudioFlinger::mLock held
1108void AudioFlinger::removeClient_l(pid_t pid)
1109{
Glenn Kasten411e4472012-11-02 10:00:06 -07001110 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten8af901c2012-11-01 11:11:38 -07001111 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112 mClients.removeItem(pid);
1113}
1114
Eric Laurent717e1282012-06-29 16:36:52 -07001115// getEffectThread_l() must be called with AudioFlinger::mLock held
1116sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1117{
1118 sp<PlaybackThread> thread;
1119
1120 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1121 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1122 ALOG_ASSERT(thread == 0);
1123 thread = mPlaybackThreads.valueAt(i);
1124 }
1125 }
1126
1127 return thread;
1128}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130
Mathias Agopian65ab4712010-07-14 17:59:35 -07001131
1132// ----------------------------------------------------------------------------
1133
1134AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1135 : RefBase(),
1136 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001137 // 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 -07001138 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001139 mPid(pid),
1140 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141{
1142 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1143}
1144
1145// Client destructor must be called with AudioFlinger::mLock held
1146AudioFlinger::Client::~Client()
1147{
1148 mAudioFlinger->removeClient_l(mPid);
1149}
1150
Glenn Kasten435dbe62012-01-30 10:15:48 -08001151sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152{
1153 return mMemoryDealer;
1154}
1155
John Grossman4ff14ba2012-02-08 16:37:41 -08001156// Reserve one of the limited slots for a timed audio track associated
1157// with this client
1158bool AudioFlinger::Client::reserveTimedTrack()
1159{
1160 const int kMaxTimedTracksPerClient = 4;
1161
1162 Mutex::Autolock _l(mTimedTrackLock);
1163
1164 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1165 ALOGW("can not create timed track - pid %d has exceeded the limit",
1166 mPid);
1167 return false;
1168 }
1169
1170 mTimedTrackCount++;
1171 return true;
1172}
1173
1174// Release a slot for a timed audio track
1175void AudioFlinger::Client::releaseTimedTrack()
1176{
1177 Mutex::Autolock _l(mTimedTrackLock);
1178 mTimedTrackCount--;
1179}
1180
Mathias Agopian65ab4712010-07-14 17:59:35 -07001181// ----------------------------------------------------------------------------
1182
1183AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1184 const sp<IAudioFlingerClient>& client,
1185 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001186 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001187{
1188}
1189
1190AudioFlinger::NotificationClient::~NotificationClient()
1191{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001192}
1193
1194void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1195{
1196 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001197 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001198}
1199
Mathias Agopian65ab4712010-07-14 17:59:35 -07001200
1201// ----------------------------------------------------------------------------
1202
1203sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001204 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001205 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001206 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001207 audio_channel_mask_t channelMask,
Glenn Kasten7da35f22012-11-14 12:54:39 -08001208 size_t frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08001209 IAudioFlinger::track_flags_t flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001210 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211 int *sessionId,
1212 status_t *status)
1213{
1214 sp<RecordThread::RecordTrack> recordTrack;
1215 sp<RecordHandle> recordHandle;
1216 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217 status_t lStatus;
1218 RecordThread *thread;
1219 size_t inFrameCount;
1220 int lSessionId;
1221
1222 // check calling permissions
1223 if (!recordingAllowed()) {
1224 lStatus = PERMISSION_DENIED;
1225 goto Exit;
1226 }
1227
1228 // add client to list
1229 { // scope for mLock
1230 Mutex::Autolock _l(mLock);
1231 thread = checkRecordThread_l(input);
1232 if (thread == NULL) {
1233 lStatus = BAD_VALUE;
1234 goto Exit;
1235 }
1236
Glenn Kastenf37971f2012-02-03 11:06:53 -08001237 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001238 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001239
1240 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001241 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001242 lSessionId = *sessionId;
1243 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001244 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001245 if (sessionId != NULL) {
1246 *sessionId = lSessionId;
1247 }
1248 }
Glenn Kasten8af901c2012-11-01 11:11:38 -07001249 // create new record track.
1250 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001251 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1252 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001253 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001254 if (lStatus != NO_ERROR) {
Glenn Kasten8af901c2012-11-01 11:11:38 -07001255 // remove local strong reference to Client before deleting the RecordTrack so that the
1256 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257 client.clear();
1258 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 goto Exit;
1260 }
1261
1262 // return to handle to client
1263 recordHandle = new RecordHandle(recordTrack);
1264 lStatus = NO_ERROR;
1265
1266Exit:
1267 if (status) {
1268 *status = lStatus;
1269 }
1270 return recordHandle;
1271}
1272
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001273
1274
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275// ----------------------------------------------------------------------------
1276
Eric Laurenta4c5a552012-03-29 10:12:40 -07001277audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1278{
1279 if (!settingsAllowed()) {
1280 return 0;
1281 }
1282 Mutex::Autolock _l(mLock);
1283 return loadHwModule_l(name);
1284}
1285
1286// loadHwModule_l() must be called with AudioFlinger::mLock held
1287audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1288{
1289 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1290 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1291 ALOGW("loadHwModule() module %s already loaded", name);
1292 return mAudioHwDevs.keyAt(i);
1293 }
1294 }
1295
Eric Laurenta4c5a552012-03-29 10:12:40 -07001296 audio_hw_device_t *dev;
1297
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001298 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001299 if (rc) {
1300 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1301 return 0;
1302 }
1303
1304 mHardwareStatus = AUDIO_HW_INIT;
1305 rc = dev->init_check(dev);
1306 mHardwareStatus = AUDIO_HW_IDLE;
1307 if (rc) {
1308 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1309 return 0;
1310 }
1311
John Grossmanee578c02012-07-23 17:05:46 -07001312 // Check and cache this HAL's level of support for master mute and master
1313 // volume. If this is the first HAL opened, and it supports the get
1314 // methods, use the initial values provided by the HAL as the current
1315 // master mute and volume settings.
1316
1317 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1318 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001319 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001320
1321 if (0 == mAudioHwDevs.size()) {
1322 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1323 if (NULL != dev->get_master_volume) {
1324 float mv;
1325 if (OK == dev->get_master_volume(dev, &mv)) {
1326 mMasterVolume = mv;
1327 }
1328 }
1329
1330 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1331 if (NULL != dev->get_master_mute) {
1332 bool mm;
1333 if (OK == dev->get_master_mute(dev, &mm)) {
1334 mMasterMute = mm;
1335 }
1336 }
1337 }
1338
Eric Laurenta4c5a552012-03-29 10:12:40 -07001339 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001340 if ((NULL != dev->set_master_volume) &&
1341 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1342 flags = static_cast<AudioHwDevice::Flags>(flags |
1343 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1344 }
1345
1346 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1347 if ((NULL != dev->set_master_mute) &&
1348 (OK == dev->set_master_mute(dev, mMasterMute))) {
1349 flags = static_cast<AudioHwDevice::Flags>(flags |
1350 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1351 }
1352
Eric Laurenta4c5a552012-03-29 10:12:40 -07001353 mHardwareStatus = AUDIO_HW_IDLE;
1354 }
1355
1356 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001357 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001358
1359 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001360 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001361
1362 return handle;
1363
1364}
1365
Glenn Kasten4c6db4c2012-09-24 11:27:18 -07001366// ----------------------------------------------------------------------------
1367
Glenn Kasten1127d652012-11-14 08:44:39 -08001368uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kasten4c6db4c2012-09-24 11:27:18 -07001369{
1370 Mutex::Autolock _l(mLock);
1371 PlaybackThread *thread = primaryPlaybackThread_l();
1372 return thread != NULL ? thread->sampleRate() : 0;
1373}
1374
Glenn Kasten7da35f22012-11-14 12:54:39 -08001375size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kasten4c6db4c2012-09-24 11:27:18 -07001376{
1377 Mutex::Autolock _l(mLock);
1378 PlaybackThread *thread = primaryPlaybackThread_l();
1379 return thread != NULL ? thread->frameCountHAL() : 0;
1380}
1381
1382// ----------------------------------------------------------------------------
1383
Eric Laurenta4c5a552012-03-29 10:12:40 -07001384audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1385 audio_devices_t *pDevices,
1386 uint32_t *pSamplingRate,
1387 audio_format_t *pFormat,
1388 audio_channel_mask_t *pChannelMask,
1389 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001390 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001391{
1392 status_t status;
1393 PlaybackThread *thread = NULL;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001394 struct audio_config config = {
1395 sample_rate: pSamplingRate ? *pSamplingRate : 0,
1396 channel_mask: pChannelMask ? *pChannelMask : 0,
1397 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT,
1398 };
1399 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001400 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401
Eric Laurenta4c5a552012-03-29 10:12:40 -07001402 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
1403 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001404 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001405 config.sample_rate,
1406 config.format,
1407 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001408 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409
1410 if (pDevices == NULL || *pDevices == 0) {
1411 return 0;
1412 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001413
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414 Mutex::Autolock _l(mLock);
1415
Eric Laurenta4c5a552012-03-29 10:12:40 -07001416 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001417 if (outHwDev == NULL)
1418 return 0;
1419
John Grossmanee578c02012-07-23 17:05:46 -07001420 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001421 audio_io_handle_t id = nextUniqueId();
1422
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001423 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001424
John Grossmanee578c02012-07-23 17:05:46 -07001425 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001426 id,
1427 *pDevices,
1428 (audio_output_flags_t)flags,
1429 &config,
1430 &outStream);
1431
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001432 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8af901c2012-11-01 11:11:38 -07001433 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, "
1434 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001435 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001436 config.sample_rate,
1437 config.format,
1438 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439 status);
1440
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001441 if (status == NO_ERROR && outStream != NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001442 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001443
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001444 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001445 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1446 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001448 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449 } else {
1450 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001451 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001452 }
1453 mPlaybackThreads.add(id, thread);
1454
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001455 if (pSamplingRate != NULL) *pSamplingRate = config.sample_rate;
1456 if (pFormat != NULL) *pFormat = config.format;
1457 if (pChannelMask != NULL) *pChannelMask = config.channel_mask;
Glenn Kastena0d68332012-01-27 16:47:15 -08001458 if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001459
1460 // notify client processes of the new output creation
1461 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001462
1463 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001464 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001465 ALOGI("Using module %d has the primary audio interface", module);
1466 mPrimaryHardwareDev = outHwDev;
1467
1468 AutoMutex lock(mHardwareLock);
1469 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001470 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001471 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001472 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473 return id;
1474 }
1475
1476 return 0;
1477}
1478
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001479audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1480 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001481{
1482 Mutex::Autolock _l(mLock);
1483 MixerThread *thread1 = checkMixerThread_l(output1);
1484 MixerThread *thread2 = checkMixerThread_l(output2);
1485
1486 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten8af901c2012-11-01 11:11:38 -07001487 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1488 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001489 return 0;
1490 }
1491
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001492 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001493 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1494 thread->addOutputTrack(thread2);
1495 mPlaybackThreads.add(id, thread);
1496 // notify client processes of the new output creation
1497 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1498 return id;
1499}
1500
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001501status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001502{
Glenn Kastend96c5722012-04-25 13:44:49 -07001503 return closeOutput_nonvirtual(output);
1504}
1505
1506status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1507{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001508 // keep strong reference on the playback thread so that
1509 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001510 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001511 {
1512 Mutex::Autolock _l(mLock);
1513 thread = checkPlaybackThread_l(output);
1514 if (thread == NULL) {
1515 return BAD_VALUE;
1516 }
1517
Steve Block3856b092011-10-20 11:56:00 +01001518 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001520 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001522 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten8af901c2012-11-01 11:11:38 -07001523 DuplicatingThread *dupThread =
1524 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525 dupThread->removeOutputTrack((MixerThread *)thread.get());
1526 }
1527 }
1528 }
Glenn Kastena1117922012-01-26 10:53:32 -08001529 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001530 mPlaybackThreads.removeItem(output);
1531 }
1532 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001533 // The thread entity (active unit of execution) is no longer running here,
1534 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001535
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001536 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001537 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001538 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001539 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001540 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001541 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542 }
1543 return NO_ERROR;
1544}
1545
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001546status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001547{
1548 Mutex::Autolock _l(mLock);
1549 PlaybackThread *thread = checkPlaybackThread_l(output);
1550
1551 if (thread == NULL) {
1552 return BAD_VALUE;
1553 }
1554
Steve Block3856b092011-10-20 11:56:00 +01001555 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556 thread->suspend();
1557
1558 return NO_ERROR;
1559}
1560
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001561status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562{
1563 Mutex::Autolock _l(mLock);
1564 PlaybackThread *thread = checkPlaybackThread_l(output);
1565
1566 if (thread == NULL) {
1567 return BAD_VALUE;
1568 }
1569
Steve Block3856b092011-10-20 11:56:00 +01001570 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001571
1572 thread->restore();
1573
1574 return NO_ERROR;
1575}
1576
Eric Laurenta4c5a552012-03-29 10:12:40 -07001577audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1578 audio_devices_t *pDevices,
1579 uint32_t *pSamplingRate,
1580 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001581 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001582{
1583 status_t status;
1584 RecordThread *thread = NULL;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001585 struct audio_config config = {
1586 sample_rate: pSamplingRate ? *pSamplingRate : 0,
1587 channel_mask: pChannelMask ? *pChannelMask : 0,
1588 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT,
1589 };
1590 uint32_t reqSamplingRate = config.sample_rate;
1591 audio_format_t reqFormat = config.format;
1592 audio_channel_mask_t reqChannels = config.channel_mask;
1593 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001594 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001595
1596 if (pDevices == NULL || *pDevices == 0) {
1597 return 0;
1598 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001599
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600 Mutex::Autolock _l(mLock);
1601
Eric Laurenta4c5a552012-03-29 10:12:40 -07001602 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001603 if (inHwDev == NULL)
1604 return 0;
1605
John Grossmanee578c02012-07-23 17:05:46 -07001606 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001607 audio_io_handle_t id = nextUniqueId();
1608
John Grossmanee578c02012-07-23 17:05:46 -07001609 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001610 &inStream);
Glenn Kasten8af901c2012-11-01 11:11:38 -07001611 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1612 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001613 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001614 config.sample_rate,
1615 config.format,
1616 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001617 status);
1618
Glenn Kasten8af901c2012-11-01 11:11:38 -07001619 // If the input could not be opened with the requested parameters and we can handle the
1620 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1621 // 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 -07001622 if (status == BAD_VALUE &&
1623 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1624 (config.sample_rate <= 2 * reqSamplingRate) &&
1625 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001626 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001627 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001628 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001629 }
1630
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001631 if (status == NO_ERROR && inStream != NULL) {
Glenn Kasten8c327342012-09-30 12:29:28 -07001632
Glenn Kastendd0bda02013-02-26 09:20:22 -08001633#ifdef TEE_SINK
Glenn Kasten8c327342012-09-30 12:29:28 -07001634 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1635 // or (re-)create if current Pipe is idle and does not match the new format
1636 sp<NBAIO_Sink> teeSink;
Glenn Kasten8c327342012-09-30 12:29:28 -07001637 enum {
1638 TEE_SINK_NO, // don't copy input
1639 TEE_SINK_NEW, // copy input using a new pipe
1640 TEE_SINK_OLD, // copy input using an existing pipe
1641 } kind;
1642 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1643 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastendd4abb52013-01-10 12:31:01 -08001644 if (!mTeeSinkInputEnabled) {
1645 kind = TEE_SINK_NO;
1646 } else if (format == Format_Invalid) {
Glenn Kasten8c327342012-09-30 12:29:28 -07001647 kind = TEE_SINK_NO;
1648 } else if (mRecordTeeSink == 0) {
1649 kind = TEE_SINK_NEW;
1650 } else if (mRecordTeeSink->getStrongCount() != 1) {
1651 kind = TEE_SINK_NO;
1652 } else if (format == mRecordTeeSink->format()) {
1653 kind = TEE_SINK_OLD;
1654 } else {
1655 kind = TEE_SINK_NEW;
1656 }
1657 switch (kind) {
1658 case TEE_SINK_NEW: {
Glenn Kastendd4abb52013-01-10 12:31:01 -08001659 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kasten8c327342012-09-30 12:29:28 -07001660 size_t numCounterOffers = 0;
1661 const NBAIO_Format offers[1] = {format};
1662 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1663 ALOG_ASSERT(index == 0);
1664 PipeReader *pipeReader = new PipeReader(*pipe);
1665 numCounterOffers = 0;
1666 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1667 ALOG_ASSERT(index == 0);
1668 mRecordTeeSink = pipe;
1669 mRecordTeeSource = pipeReader;
1670 teeSink = pipe;
1671 }
1672 break;
1673 case TEE_SINK_OLD:
1674 teeSink = mRecordTeeSink;
1675 break;
1676 case TEE_SINK_NO:
1677 default:
1678 break;
1679 }
Glenn Kastendd0bda02013-02-26 09:20:22 -08001680#endif
Glenn Kastendd4abb52013-01-10 12:31:01 -08001681
Dima Zavin799a70e2011-04-18 16:57:27 -07001682 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1683
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001684 // Start record thread
1685 // RecorThread require both input and output device indication to forward to audio
1686 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001687 thread = new RecordThread(this,
1688 input,
1689 reqSamplingRate,
1690 reqChannels,
1691 id,
Eric Laurent201fc9c2013-02-01 17:57:04 -08001692 primaryOutputDevice_l(),
Glenn Kastendd0bda02013-02-26 09:20:22 -08001693 *pDevices
1694#ifdef TEE_SINK
1695 , teeSink
1696#endif
1697 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001698 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001699 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kastena0d68332012-01-27 16:47:15 -08001700 if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001701 if (pFormat != NULL) *pFormat = config.format;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001702 if (pChannelMask != NULL) *pChannelMask = reqChannels;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001703
Mathias Agopian65ab4712010-07-14 17:59:35 -07001704 // notify client processes of the new input creation
1705 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1706 return id;
1707 }
1708
1709 return 0;
1710}
1711
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001712status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001713{
Glenn Kastend96c5722012-04-25 13:44:49 -07001714 return closeInput_nonvirtual(input);
1715}
1716
1717status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1718{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001719 // keep strong reference on the record thread so that
1720 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001721 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001722 {
1723 Mutex::Autolock _l(mLock);
1724 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001725 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001726 return BAD_VALUE;
1727 }
1728
Steve Block3856b092011-10-20 11:56:00 +01001729 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001730 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001731 mRecordThreads.removeItem(input);
1732 }
1733 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001734 // The thread entity (active unit of execution) is no longer running here,
1735 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001736
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001737 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001738 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001739 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001740 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001741 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001742
1743 return NO_ERROR;
1744}
1745
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001746status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001747{
1748 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001749 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001750
1751 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1752 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001753 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001754 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001755
1756 return NO_ERROR;
1757}
1758
1759
1760int AudioFlinger::newAudioSessionId()
1761{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001762 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001763}
1764
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001765void AudioFlinger::acquireAudioSessionId(int audioSession)
1766{
1767 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001768 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001769 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001770 size_t num = mAudioSessionRefs.size();
1771 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001772 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001773 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1774 ref->mCnt++;
1775 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001776 return;
1777 }
1778 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001779 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1780 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001781}
1782
1783void AudioFlinger::releaseAudioSessionId(int audioSession)
1784{
1785 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001786 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001787 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001788 size_t num = mAudioSessionRefs.size();
1789 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001790 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001791 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1792 ref->mCnt--;
1793 ALOGV(" decremented refcount to %d", ref->mCnt);
1794 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001795 mAudioSessionRefs.removeAt(i);
1796 delete ref;
1797 purgeStaleEffects_l();
1798 }
1799 return;
1800 }
1801 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001802 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001803}
1804
1805void AudioFlinger::purgeStaleEffects_l() {
1806
Steve Block3856b092011-10-20 11:56:00 +01001807 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001808
1809 Vector< sp<EffectChain> > chains;
1810
1811 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1812 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1813 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1814 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001815 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1816 chains.push(ec);
1817 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001818 }
1819 }
1820 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1821 sp<RecordThread> t = mRecordThreads.valueAt(i);
1822 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1823 sp<EffectChain> ec = t->mEffectChains[j];
1824 chains.push(ec);
1825 }
1826 }
1827
1828 for (size_t i = 0; i < chains.size(); i++) {
1829 sp<EffectChain> ec = chains[i];
1830 int sessionid = ec->sessionId();
1831 sp<ThreadBase> t = ec->mThread.promote();
1832 if (t == 0) {
1833 continue;
1834 }
1835 size_t numsessionrefs = mAudioSessionRefs.size();
1836 bool found = false;
1837 for (size_t k = 0; k < numsessionrefs; k++) {
1838 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001839 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001840 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001841 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001842 found = true;
1843 break;
1844 }
1845 }
1846 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001847 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001848 // remove all effects from the chain
1849 while (ec->mEffects.size()) {
1850 sp<EffectModule> effect = ec->mEffects[0];
1851 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001852 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001853 if (effect->purgeHandles()) {
1854 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001855 }
1856 AudioSystem::unregisterEffect(effect->id());
1857 }
1858 }
1859 }
1860 return;
1861}
1862
Mathias Agopian65ab4712010-07-14 17:59:35 -07001863// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001864AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001865{
Glenn Kastena1117922012-01-26 10:53:32 -08001866 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001867}
1868
1869// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001870AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001871{
1872 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001873 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001874}
1875
1876// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001877AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878{
Glenn Kastena1117922012-01-26 10:53:32 -08001879 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001880}
1881
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001882uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001883{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001884 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885}
1886
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001887AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001888{
1889 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1890 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001891 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001892 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001893 return thread;
1894 }
1895 }
1896 return NULL;
1897}
1898
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001899audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001900{
1901 PlaybackThread *thread = primaryPlaybackThread_l();
1902
1903 if (thread == NULL) {
1904 return 0;
1905 }
1906
Eric Laurent88959252012-08-28 14:26:53 -07001907 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001908}
1909
Eric Laurenta011e352012-03-29 15:51:43 -07001910sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1911 int triggerSession,
1912 int listenerSession,
1913 sync_event_callback_t callBack,
1914 void *cookie)
1915{
1916 Mutex::Autolock _l(mLock);
1917
1918 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1919 status_t playStatus = NAME_NOT_FOUND;
1920 status_t recStatus = NAME_NOT_FOUND;
1921 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1922 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1923 if (playStatus == NO_ERROR) {
1924 return event;
1925 }
1926 }
1927 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1928 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1929 if (recStatus == NO_ERROR) {
1930 return event;
1931 }
1932 }
1933 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
1934 mPendingSyncEvents.add(event);
1935 } else {
1936 ALOGV("createSyncEvent() invalid event %d", event->type());
1937 event.clear();
1938 }
1939 return event;
1940}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001941
Mathias Agopian65ab4712010-07-14 17:59:35 -07001942// ----------------------------------------------------------------------------
1943// Effect management
1944// ----------------------------------------------------------------------------
1945
1946
Glenn Kastenf587ba52012-01-26 16:25:10 -08001947status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948{
1949 Mutex::Autolock _l(mLock);
1950 return EffectQueryNumberEffects(numEffects);
1951}
1952
Glenn Kastenf587ba52012-01-26 16:25:10 -08001953status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001954{
1955 Mutex::Autolock _l(mLock);
1956 return EffectQueryEffect(index, descriptor);
1957}
1958
Glenn Kasten5e92a782012-01-30 07:40:52 -08001959status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08001960 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001961{
1962 Mutex::Autolock _l(mLock);
1963 return EffectGetDescriptor(pUuid, descriptor);
1964}
1965
1966
Glenn Kastenf37971f2012-02-03 11:06:53 -08001967sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968 effect_descriptor_t *pDesc,
1969 const sp<IEffectClient>& effectClient,
1970 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001971 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001972 int sessionId,
1973 status_t *status,
1974 int *id,
1975 int *enabled)
1976{
1977 status_t lStatus = NO_ERROR;
1978 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001980
Glenn Kastenf37971f2012-02-03 11:06:53 -08001981 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001982 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001983 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001984
1985 if (pDesc == NULL) {
1986 lStatus = BAD_VALUE;
1987 goto Exit;
1988 }
1989
Eric Laurent84e9a102010-09-23 16:10:16 -07001990 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07001991 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001992 lStatus = PERMISSION_DENIED;
1993 goto Exit;
1994 }
1995
Dima Zavinfce7a472011-04-19 22:30:36 -07001996 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07001997 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08001998 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001999 lStatus = PERMISSION_DENIED;
2000 goto Exit;
2001 }
2002
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002003 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002004 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002005 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07002006 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07002007 lStatus = BAD_VALUE;
2008 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07002009 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002010 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002011 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002012 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002013 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002014 }
2015 }
2016
Mathias Agopian65ab4712010-07-14 17:59:35 -07002017 {
2018 Mutex::Autolock _l(mLock);
2019
Mathias Agopian65ab4712010-07-14 17:59:35 -07002020
2021 if (!EffectIsNullUuid(&pDesc->uuid)) {
2022 // if uuid is specified, request effect descriptor
2023 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2024 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002025 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002026 goto Exit;
2027 }
2028 } else {
2029 // if uuid is not specified, look for an available implementation
2030 // of the required type in effect factory
2031 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002032 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002033 lStatus = BAD_VALUE;
2034 goto Exit;
2035 }
2036 uint32_t numEffects = 0;
2037 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002038 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002039 bool found = false;
2040
2041 lStatus = EffectQueryNumberEffects(&numEffects);
2042 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002043 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002044 goto Exit;
2045 }
2046 for (uint32_t i = 0; i < numEffects; i++) {
2047 lStatus = EffectQueryEffect(i, &desc);
2048 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002049 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002050 continue;
2051 }
2052 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2053 // If matching type found save effect descriptor. If the session is
2054 // 0 and the effect is not auxiliary, continue enumeration in case
2055 // an auxiliary version of this effect type is available
2056 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002057 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002058 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002059 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2060 break;
2061 }
2062 }
2063 }
2064 if (!found) {
2065 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002066 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002067 goto Exit;
2068 }
2069 // For same effect type, chose auxiliary version over insert version if
2070 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002071 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002072 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002073 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002074 }
2075 }
2076
2077 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002078 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002079 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2080 lStatus = INVALID_OPERATION;
2081 goto Exit;
2082 }
2083
Eric Laurent59255e42011-07-27 19:49:51 -07002084 // check recording permission for visualizer
2085 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2086 !recordingAllowed()) {
2087 lStatus = PERMISSION_DENIED;
2088 goto Exit;
2089 }
2090
Mathias Agopian65ab4712010-07-14 17:59:35 -07002091 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002092 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002093
2094 // If output is not specified try to find a matching audio session ID in one of the
2095 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002096 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2097 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002098 // Note: io is never 0 when creating an effect on an input
2099 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002100 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002101 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2102 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002103 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002104 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002105 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002106 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002107 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002108 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2109 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2110 io = mRecordThreads.keyAt(i);
2111 break;
2112 }
2113 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002114 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002115 // If no output thread contains the requested session ID, default to
2116 // first output. The effect chain will be moved to the correct output
2117 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002118 if (io == 0 && mPlaybackThreads.size()) {
2119 io = mPlaybackThreads.keyAt(0);
2120 }
Steve Block3856b092011-10-20 11:56:00 +01002121 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002122 }
2123 ThreadBase *thread = checkRecordThread_l(io);
2124 if (thread == NULL) {
2125 thread = checkPlaybackThread_l(io);
2126 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002127 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002128 lStatus = BAD_VALUE;
2129 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002130 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002131 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002132
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002133 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002134
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002135 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002136 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2137 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002138 if (handle != 0 && id != NULL) {
2139 *id = handle->id();
2140 }
2141 }
2142
2143Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002144 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002145 *status = lStatus;
2146 }
2147 return handle;
2148}
2149
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002150status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2151 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002152{
Steve Block3856b092011-10-20 11:56:00 +01002153 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002154 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002155 Mutex::Autolock _l(mLock);
2156 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002157 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002158 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002159 }
Eric Laurentde070132010-07-13 04:45:46 -07002160 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2161 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002162 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002163 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002164 }
Eric Laurentde070132010-07-13 04:45:46 -07002165 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2166 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002167 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002168 return BAD_VALUE;
2169 }
2170
2171 Mutex::Autolock _dl(dstThread->mLock);
2172 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002173 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002174
Mathias Agopian65ab4712010-07-14 17:59:35 -07002175 return NO_ERROR;
2176}
2177
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002178// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002179status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002180 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002181 AudioFlinger::PlaybackThread *dstThread,
2182 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002183{
Steve Block3856b092011-10-20 11:56:00 +01002184 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002185 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002186
Eric Laurent59255e42011-07-27 19:49:51 -07002187 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002188 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002189 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002190 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002191 return INVALID_OPERATION;
2192 }
2193
Eric Laurent39e94f82010-07-28 01:32:47 -07002194 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002195 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002196 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002197 // removed.
2198 srcThread->removeEffectChain_l(chain);
2199
2200 // transfer all effects one by one so that new effect chain is created on new thread with
2201 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002202 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002203 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002204 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002205 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2206 while (effect != 0) {
2207 srcThread->removeEffect_l(effect);
2208 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002209 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2210 if (effect->state() == EffectModule::ACTIVE ||
2211 effect->state() == EffectModule::STOPPING) {
2212 effect->start();
2213 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002214 // if the move request is not received from audio policy manager, the effect must be
2215 // re-registered with the new strategy and output
2216 if (dstChain == 0) {
2217 dstChain = effect->chain().promote();
2218 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002219 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002220 srcThread->addEffect_l(effect);
2221 return NO_INIT;
2222 }
2223 strategy = dstChain->strategy();
2224 }
2225 if (reRegister) {
2226 AudioSystem::unregisterEffect(effect->id());
2227 AudioSystem::registerEffect(&effect->desc(),
2228 dstOutput,
2229 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002230 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002231 effect->id());
2232 }
Eric Laurentde070132010-07-13 04:45:46 -07002233 effect = chain->getEffectFromId_l(0);
2234 }
2235
2236 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237}
2238
Glenn Kastendd4abb52013-01-10 12:31:01 -08002239struct Entry {
2240#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2241 char mName[MAX_NAME];
2242};
2243
2244int comparEntry(const void *p1, const void *p2)
2245{
2246 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2247}
2248
Glenn Kastendd0bda02013-02-26 09:20:22 -08002249#ifdef TEE_SINK
Eric Laurentca7cc822012-11-19 14:55:58 -08002250void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002251{
Eric Laurentca7cc822012-11-19 14:55:58 -08002252 NBAIO_Source *teeSource = source.get();
2253 if (teeSource != NULL) {
Glenn Kastendd4abb52013-01-10 12:31:01 -08002254 // .wav rotation
2255 // There is a benign race condition if 2 threads call this simultaneously.
2256 // They would both traverse the directory, but the result would simply be
2257 // failures at unlink() which are ignored. It's also unlikely since
2258 // normally dumpsys is only done by bugreport or from the command line.
2259 char teePath[32+256];
2260 strcpy(teePath, "/data/misc/media");
2261 size_t teePathLen = strlen(teePath);
2262 DIR *dir = opendir(teePath);
2263 teePath[teePathLen++] = '/';
2264 if (dir != NULL) {
2265#define MAX_SORT 20 // number of entries to sort
2266#define MAX_KEEP 10 // number of entries to keep
2267 struct Entry entries[MAX_SORT];
2268 size_t entryCount = 0;
2269 while (entryCount < MAX_SORT) {
2270 struct dirent de;
2271 struct dirent *result = NULL;
2272 int rc = readdir_r(dir, &de, &result);
2273 if (rc != 0) {
2274 ALOGW("readdir_r failed %d", rc);
2275 break;
2276 }
2277 if (result == NULL) {
2278 break;
2279 }
2280 if (result != &de) {
2281 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2282 break;
2283 }
2284 // ignore non .wav file entries
2285 size_t nameLen = strlen(de.d_name);
2286 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2287 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2288 continue;
2289 }
2290 strcpy(entries[entryCount++].mName, de.d_name);
2291 }
2292 (void) closedir(dir);
2293 if (entryCount > MAX_KEEP) {
2294 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2295 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2296 strcpy(&teePath[teePathLen], entries[i].mName);
2297 (void) unlink(teePath);
2298 }
2299 }
2300 } else {
2301 if (fd >= 0) {
2302 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2303 }
2304 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002305 char teeTime[16];
2306 struct timeval tv;
2307 gettimeofday(&tv, NULL);
2308 struct tm tm;
2309 localtime_r(&tv.tv_sec, &tm);
Glenn Kastendd4abb52013-01-10 12:31:01 -08002310 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2311 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2312 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2313 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurentca7cc822012-11-19 14:55:58 -08002314 if (teeFd >= 0) {
2315 char wavHeader[44];
2316 memcpy(wavHeader,
2317 "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",
2318 sizeof(wavHeader));
2319 NBAIO_Format format = teeSource->format();
2320 unsigned channelCount = Format_channelCount(format);
2321 ALOG_ASSERT(channelCount <= FCC_2);
2322 uint32_t sampleRate = Format_sampleRate(format);
2323 wavHeader[22] = channelCount; // number of channels
2324 wavHeader[24] = sampleRate; // sample rate
2325 wavHeader[25] = sampleRate >> 8;
2326 wavHeader[32] = channelCount * 2; // block alignment
2327 write(teeFd, wavHeader, sizeof(wavHeader));
2328 size_t total = 0;
2329 bool firstRead = true;
2330 for (;;) {
2331#define TEE_SINK_READ 1024
2332 short buffer[TEE_SINK_READ * FCC_2];
2333 size_t count = TEE_SINK_READ;
2334 ssize_t actual = teeSource->read(buffer, count,
2335 AudioBufferProvider::kInvalidPTS);
2336 bool wasFirstRead = firstRead;
2337 firstRead = false;
2338 if (actual <= 0) {
2339 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2340 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002341 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002342 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002343 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002344 ALOG_ASSERT(actual <= (ssize_t)count);
2345 write(teeFd, buffer, actual * channelCount * sizeof(short));
2346 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002347 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002348 lseek(teeFd, (off_t) 4, SEEK_SET);
2349 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2350 write(teeFd, &temp, sizeof(temp));
2351 lseek(teeFd, (off_t) 40, SEEK_SET);
2352 temp = total * channelCount * sizeof(short);
2353 write(teeFd, &temp, sizeof(temp));
2354 close(teeFd);
Glenn Kastendd4abb52013-01-10 12:31:01 -08002355 if (fd >= 0) {
2356 fdprintf(fd, "tee copied to %s\n", teePath);
2357 }
Eric Laurent59255e42011-07-27 19:49:51 -07002358 } else {
Glenn Kastendd4abb52013-01-10 12:31:01 -08002359 if (fd >= 0) {
2360 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2361 }
Eric Laurent59255e42011-07-27 19:49:51 -07002362 }
2363 }
2364}
Glenn Kastendd0bda02013-02-26 09:20:22 -08002365#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002366
Mathias Agopian65ab4712010-07-14 17:59:35 -07002367// ----------------------------------------------------------------------------
2368
2369status_t AudioFlinger::onTransact(
2370 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2371{
2372 return BnAudioFlinger::onTransact(code, data, reply, flags);
2373}
2374
Mathias Agopian65ab4712010-07-14 17:59:35 -07002375}; // namespace android