blob: 47c2772829ed7dbc4011f6a15bbbe19858ae6235 [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
22#include <math.h>
23#include <signal.h>
24#include <sys/time.h>
25#include <sys/resource.h>
26
Gloria Wang9ee159b2011-02-24 14:51:45 -080027#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028#include <binder/IServiceManager.h>
29#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070030#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070031#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070032#include <utils/String16.h>
33#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070034#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035
Dima Zavinfce7a472011-04-19 22:30:36 -070036#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <cutils/properties.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080038#include <cutils/compiler.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039
Eric Laurent81784c32012-11-19 14:55:58 -080040//#include <private/media/AudioTrackShared.h>
41//#include <private/media/AudioEffectShared.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045
46#include "AudioMixer.h"
47#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080048#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
Mathias Agopian65ab4712010-07-14 17:59:35 -070050#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070051#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070052#include <audio_effects/effect_ns.h>
53#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
Glenn Kasten3b21c502011-12-15 09:52:39 -080055#include <audio_utils/primitives.h>
56
Eric Laurentfeb0db62011-07-22 09:04:31 -070057#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080058
John Grossman4ff14ba2012-02-08 16:37:41 -080059#include <common_time/cc_helper.h>
Eric Laurent81784c32012-11-19 14:55:58 -080060//#include <common_time/local_clock.h>
Glenn Kasten58912562012-04-03 10:45:00 -070061
Glenn Kasten9e58b552013-01-18 15:09:48 -080062#include <media/IMediaLogService.h>
63
Mathias Agopian65ab4712010-07-14 17:59:35 -070064// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070065
John Grossman1c345192012-03-27 14:00:17 -070066// Note: the following macro is used for extremely verbose logging message. In
67// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
68// 0; but one side effect of this is to turn all LOGV's as well. Some messages
69// are so verbose that we want to suppress them even when we have ALOG_ASSERT
70// turned on. Do not uncomment the #def below unless you really know what you
71// are doing and want to see all of the extremely verbose messages.
72//#define VERY_VERY_VERBOSE_LOGGING
73#ifdef VERY_VERY_VERBOSE_LOGGING
74#define ALOGVV ALOGV
75#else
76#define ALOGVV(a...) do { } while(0)
77#endif
Eric Laurentde070132010-07-13 04:45:46 -070078
Mathias Agopian65ab4712010-07-14 17:59:35 -070079namespace android {
80
Glenn Kastenec1d6b52011-12-12 09:04:45 -080081static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
82static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070083
Glenn Kasten58912562012-04-03 10:45:00 -070084
John Grossman4ff14ba2012-02-08 16:37:41 -080085nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080086
Eric Laurent81784c32012-11-19 14:55:58 -080087uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070088
Mathias Agopian65ab4712010-07-14 17:59:35 -070089// ----------------------------------------------------------------------------
90
Eric Laurentf7ffb8b2012-04-14 09:06:57 -070091static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -070092{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -070093 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -070094 int rc;
95
Eric Laurentf7ffb8b2012-04-14 09:06:57 -070096 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
97 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
98 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
99 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700100 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700101 }
102 rc = audio_hw_device_open(mod, dev);
103 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
104 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
105 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700106 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700107 }
108 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
109 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
110 rc = BAD_VALUE;
111 goto out;
112 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700113 return 0;
114
115out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700116 *dev = NULL;
117 return rc;
118}
119
Mathias Agopian65ab4712010-07-14 17:59:35 -0700120// ----------------------------------------------------------------------------
121
122AudioFlinger::AudioFlinger()
123 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800124 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700125 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800126 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800127 mMasterMute(false),
128 mNextUniqueId(1),
129 mMode(AUDIO_MODE_INVALID),
130 mBtNrecIsOff(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700131{
Glenn Kasten9e58b552013-01-18 15:09:48 -0800132 char value[PROPERTY_VALUE_MAX];
133 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
134 if (doLog) {
135 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
136 }
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700137}
138
139void AudioFlinger::onFirstRef()
140{
Dima Zavin799a70e2011-04-18 16:57:27 -0700141 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700142
Eric Laurent93575202011-01-18 18:39:02 -0800143 Mutex::Autolock _l(mLock);
144
Dima Zavin799a70e2011-04-18 16:57:27 -0700145 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800146 char val_str[PROPERTY_VALUE_MAX] = { 0 };
147 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
148 uint32_t int_val;
149 if (1 == sscanf(val_str, "%u", &int_val)) {
150 mStandbyTimeInNsecs = milliseconds(int_val);
151 ALOGI("Using %u mSec as standby time.", int_val);
152 } else {
153 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
154 ALOGI("Using default %u mSec as standby time.",
155 (uint32_t)(mStandbyTimeInNsecs / 1000000));
156 }
157 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158
Eric Laurenta4c5a552012-03-29 10:12:40 -0700159 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160}
161
162AudioFlinger::~AudioFlinger()
163{
164 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700165 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700166 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167 }
168 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700169 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700170 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700171 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700172
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800173 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
174 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700175 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
176 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700177 }
178}
179
Eric Laurenta4c5a552012-03-29 10:12:40 -0700180static const char * const audio_interfaces[] = {
181 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
182 AUDIO_HARDWARE_MODULE_ID_A2DP,
183 AUDIO_HARDWARE_MODULE_ID_USB,
184};
185#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
186
John Grossmanee578c02012-07-23 17:05:46 -0700187AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
188 audio_module_handle_t module,
189 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700190{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700191 // if module is 0, the request comes from an old policy manager and we should load
192 // well known modules
193 if (module == 0) {
194 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
195 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
196 loadHwModule_l(audio_interfaces[i]);
197 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700198 // then try to find a module supporting the requested device.
199 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
200 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
201 audio_hw_device_t *dev = audioHwDevice->hwDevice();
202 if ((dev->get_supported_devices != NULL) &&
203 (dev->get_supported_devices(dev) & devices) == devices)
204 return audioHwDevice;
205 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700206 } else {
207 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700208 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
209 if (audioHwDevice != NULL) {
210 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700211 }
212 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700213
Dima Zavin799a70e2011-04-18 16:57:27 -0700214 return NULL;
215}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700217void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700218{
219 const size_t SIZE = 256;
220 char buffer[SIZE];
221 String8 result;
222
223 result.append("Clients:\n");
224 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800225 sp<Client> client = mClients.valueAt(i).promote();
226 if (client != 0) {
227 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
228 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700229 }
230 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700231
232 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800233 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700234 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
235 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800236 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700237 result.append(buffer);
238 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700240}
241
242
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700243void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244{
245 const size_t SIZE = 256;
246 char buffer[SIZE];
247 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800248 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249
John Grossman4ff14ba2012-02-08 16:37:41 -0800250 snprintf(buffer, SIZE, "Hardware status: %d\n"
251 "Standby Time mSec: %u\n",
252 hardwareStatus,
253 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254 result.append(buffer);
255 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256}
257
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700258void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259{
260 const size_t SIZE = 256;
261 char buffer[SIZE];
262 String8 result;
263 snprintf(buffer, SIZE, "Permission Denial: "
264 "can't dump AudioFlinger from pid=%d, uid=%d\n",
265 IPCThreadState::self()->getCallingPid(),
266 IPCThreadState::self()->getCallingUid());
267 result.append(buffer);
268 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269}
270
Eric Laurent81784c32012-11-19 14:55:58 -0800271bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700272{
273 bool locked = false;
274 for (int i = 0; i < kDumpLockRetries; ++i) {
275 if (mutex.tryLock() == NO_ERROR) {
276 locked = true;
277 break;
278 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800279 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280 }
281 return locked;
282}
283
284status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
285{
Glenn Kasten44deb052012-02-05 18:09:08 -0800286 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287 dumpPermissionDenial(fd, args);
288 } else {
289 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800290 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291 if (!hardwareLocked) {
292 String8 result(kHardwareLockedString);
293 write(fd, result.string(), result.size());
294 } else {
295 mHardwareLock.unlock();
296 }
297
Eric Laurent81784c32012-11-19 14:55:58 -0800298 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299
300 // failed to lock - AudioFlinger is probably deadlocked
301 if (!locked) {
302 String8 result(kDeadlockedString);
303 write(fd, result.string(), result.size());
304 }
305
306 dumpClients(fd, args);
307 dumpInternals(fd, args);
308
309 // dump playback threads
310 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
311 mPlaybackThreads.valueAt(i)->dump(fd, args);
312 }
313
314 // dump record threads
315 for (size_t i = 0; i < mRecordThreads.size(); i++) {
316 mRecordThreads.valueAt(i)->dump(fd, args);
317 }
318
Dima Zavin799a70e2011-04-18 16:57:27 -0700319 // dump all hardware devs
320 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700321 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700322 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700324
325 // dump the serially shared record tee sink
326 if (mRecordTeeSource != 0) {
327 dumpTee(fd, mRecordTeeSource);
328 }
329
Glenn Kastend65d73c2012-06-22 17:21:07 -0700330 if (locked) {
331 mLock.unlock();
332 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800333
334 // append a copy of media.log here by forwarding fd to it, but don't attempt
335 // to lookup the service if it's not running, as it will block for a second
336 if (mLogMemoryDealer != 0) {
337 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
338 if (binder != 0) {
339 fdprintf(fd, "\nmedia.log:\n");
340 Vector<String16> args;
341 binder->dump(fd, args);
342 }
343 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700344 }
345 return NO_ERROR;
346}
347
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800348sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
349{
350 // If pid is already in the mClients wp<> map, then use that entry
351 // (for which promote() is always != 0), otherwise create a new entry and Client.
352 sp<Client> client = mClients.valueFor(pid).promote();
353 if (client == 0) {
354 client = new Client(this, pid);
355 mClients.add(pid, client);
356 }
357
358 return client;
359}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360
Glenn Kasten9e58b552013-01-18 15:09:48 -0800361sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
362{
363 if (mLogMemoryDealer == 0) {
364 return new NBLog::Writer();
365 }
366 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
367 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
368 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
369 if (binder != 0) {
370 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
371 }
372 return writer;
373}
374
375void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
376{
Glenn Kasten685ef092013-02-04 08:15:34 -0800377 if (writer == 0) {
378 return;
379 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800380 sp<IMemory> iMemory(writer->getIMemory());
381 if (iMemory == 0) {
382 return;
383 }
384 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
385 if (binder != 0) {
386 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
387 // Now the media.log remote reference to IMemory is gone.
388 // When our last local reference to IMemory also drops to zero,
389 // the IMemory destructor will deallocate the region from mMemoryDealer.
390 }
391}
392
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393// IAudioFlinger interface
394
395
396sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800397 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700398 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800399 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700400 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800401 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800402 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800404 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800405 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700406 int *sessionId,
407 status_t *status)
408{
409 sp<PlaybackThread::Track> track;
410 sp<TrackHandle> trackHandle;
411 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 status_t lStatus;
413 int lSessionId;
414
Glenn Kasten263709e2012-01-06 08:40:01 -0800415 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
416 // but if someone uses binder directly they could bypass that and cause us to crash
417 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000418 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700419 lStatus = BAD_VALUE;
420 goto Exit;
421 }
422
Glenn Kasten60a83922012-06-21 12:56:37 -0700423 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
424 // and we don't yet support 8.24 or 32-bit PCM
425 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
426 ALOGE("createTrack() invalid format %d", format);
427 lStatus = BAD_VALUE;
428 goto Exit;
429 }
430
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431 {
432 Mutex::Autolock _l(mLock);
433 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700434 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000436 ALOGE("unknown output thread");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700437 lStatus = BAD_VALUE;
438 goto Exit;
439 }
440
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800441 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800442 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443
Steve Block3856b092011-10-20 11:56:00 +0100444 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700445 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700446 // check if an effect chain with the same session ID is present on another
447 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700448 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700449 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
450 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700451 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700452 if (sessions & PlaybackThread::EFFECT_SESSION) {
453 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700454 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700455 }
Eric Laurentde070132010-07-13 04:45:46 -0700456 }
457 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700458 lSessionId = *sessionId;
459 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700460 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700461 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700462 if (sessionId != NULL) {
463 *sessionId = lSessionId;
464 }
465 }
Steve Block3856b092011-10-20 11:56:00 +0100466 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700467
468 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800469 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700470
471 // move effect chain to this output thread if an effect on same session was waiting
472 // for a track to be created
473 if (lStatus == NO_ERROR && effectThread != NULL) {
474 Mutex::Autolock _dl(thread->mLock);
475 Mutex::Autolock _sl(effectThread->mLock);
476 moveEffectChain_l(lSessionId, effectThread, thread, true);
477 }
Eric Laurenta011e352012-03-29 15:51:43 -0700478
479 // Look for sync events awaiting for a session to be used.
480 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
481 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
482 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700483 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700484 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700485 } else {
486 mPendingSyncEvents[i]->cancel();
487 }
Eric Laurenta011e352012-03-29 15:51:43 -0700488 mPendingSyncEvents.removeAt(i);
489 i--;
490 }
491 }
492 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700493 }
494 if (lStatus == NO_ERROR) {
495 trackHandle = new TrackHandle(track);
496 } else {
497 // remove local strong reference to Client before deleting the Track so that the Client
498 // destructor is called by the TrackBase destructor with mLock held
499 client.clear();
500 track.clear();
501 }
502
503Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700504 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505 *status = lStatus;
506 }
507 return trackHandle;
508}
509
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800510uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511{
512 Mutex::Autolock _l(mLock);
513 PlaybackThread *thread = checkPlaybackThread_l(output);
514 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000515 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700516 return 0;
517 }
518 return thread->sampleRate();
519}
520
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800521int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700522{
523 Mutex::Autolock _l(mLock);
524 PlaybackThread *thread = checkPlaybackThread_l(output);
525 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000526 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700527 return 0;
528 }
529 return thread->channelCount();
530}
531
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800532audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700533{
534 Mutex::Autolock _l(mLock);
535 PlaybackThread *thread = checkPlaybackThread_l(output);
536 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000537 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800538 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700539 }
540 return thread->format();
541}
542
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800543size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700544{
545 Mutex::Autolock _l(mLock);
546 PlaybackThread *thread = checkPlaybackThread_l(output);
547 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000548 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549 return 0;
550 }
Glenn Kasten58912562012-04-03 10:45:00 -0700551 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
552 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 return thread->frameCount();
554}
555
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800556uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557{
558 Mutex::Autolock _l(mLock);
559 PlaybackThread *thread = checkPlaybackThread_l(output);
560 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000561 ALOGW("latency() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562 return 0;
563 }
564 return thread->latency();
565}
566
567status_t AudioFlinger::setMasterVolume(float value)
568{
Eric Laurenta1884f92011-08-23 08:25:03 -0700569 status_t ret = initCheck();
570 if (ret != NO_ERROR) {
571 return ret;
572 }
573
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574 // check calling permissions
575 if (!settingsAllowed()) {
576 return PERMISSION_DENIED;
577 }
578
Eric Laurenta4c5a552012-03-29 10:12:40 -0700579 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700580 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700581
John Grossmanee578c02012-07-23 17:05:46 -0700582 // Set master volume in the HALs which support it.
583 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
584 AutoMutex lock(mHardwareLock);
585 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800586
John Grossmanee578c02012-07-23 17:05:46 -0700587 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
588 if (dev->canSetMasterVolume()) {
589 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800590 }
John Grossmanee578c02012-07-23 17:05:46 -0700591 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593
John Grossmanee578c02012-07-23 17:05:46 -0700594 // Now set the master volume in each playback thread. Playback threads
595 // assigned to HALs which do not have master volume support will apply
596 // master volume during the mix operation. Threads with HALs which do
597 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800598 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700599 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600
601 return NO_ERROR;
602}
603
Glenn Kastenf78aee72012-01-04 11:00:47 -0800604status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605{
Eric Laurenta1884f92011-08-23 08:25:03 -0700606 status_t ret = initCheck();
607 if (ret != NO_ERROR) {
608 return ret;
609 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700610
611 // check calling permissions
612 if (!settingsAllowed()) {
613 return PERMISSION_DENIED;
614 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800615 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000616 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700617 return BAD_VALUE;
618 }
619
620 { // scope for the lock
621 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700622 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700624 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625 mHardwareStatus = AUDIO_HW_IDLE;
626 }
627
628 if (NO_ERROR == ret) {
629 Mutex::Autolock _l(mLock);
630 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800631 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700632 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633 }
634
635 return ret;
636}
637
638status_t AudioFlinger::setMicMute(bool state)
639{
Eric Laurenta1884f92011-08-23 08:25:03 -0700640 status_t ret = initCheck();
641 if (ret != NO_ERROR) {
642 return ret;
643 }
644
Mathias Agopian65ab4712010-07-14 17:59:35 -0700645 // check calling permissions
646 if (!settingsAllowed()) {
647 return PERMISSION_DENIED;
648 }
649
650 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700651 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700653 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654 mHardwareStatus = AUDIO_HW_IDLE;
655 return ret;
656}
657
658bool AudioFlinger::getMicMute() const
659{
Eric Laurenta1884f92011-08-23 08:25:03 -0700660 status_t ret = initCheck();
661 if (ret != NO_ERROR) {
662 return false;
663 }
664
Dima Zavinfce7a472011-04-19 22:30:36 -0700665 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800666 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700667 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700669 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 mHardwareStatus = AUDIO_HW_IDLE;
671 return state;
672}
673
674status_t AudioFlinger::setMasterMute(bool muted)
675{
John Grossmand8f178d2012-07-20 14:51:35 -0700676 status_t ret = initCheck();
677 if (ret != NO_ERROR) {
678 return ret;
679 }
680
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 // check calling permissions
682 if (!settingsAllowed()) {
683 return PERMISSION_DENIED;
684 }
685
John Grossmanee578c02012-07-23 17:05:46 -0700686 Mutex::Autolock _l(mLock);
687 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700688
John Grossmanee578c02012-07-23 17:05:46 -0700689 // Set master mute in the HALs which support it.
690 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
691 AutoMutex lock(mHardwareLock);
692 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700693
John Grossmanee578c02012-07-23 17:05:46 -0700694 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
695 if (dev->canSetMasterMute()) {
696 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700697 }
John Grossmanee578c02012-07-23 17:05:46 -0700698 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700699 }
700
John Grossmanee578c02012-07-23 17:05:46 -0700701 // Now set the master mute in each playback thread. Playback threads
702 // assigned to HALs which do not have master mute support will apply master
703 // mute during the mix operation. Threads with HALs which do support master
704 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800705 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700706 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707
708 return NO_ERROR;
709}
710
711float AudioFlinger::masterVolume() const
712{
Glenn Kasten98067102011-12-13 11:47:54 -0800713 Mutex::Autolock _l(mLock);
714 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715}
716
717bool AudioFlinger::masterMute() const
718{
Glenn Kasten98067102011-12-13 11:47:54 -0800719 Mutex::Autolock _l(mLock);
720 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700721}
722
John Grossman4ff14ba2012-02-08 16:37:41 -0800723float AudioFlinger::masterVolume_l() const
724{
John Grossman4ff14ba2012-02-08 16:37:41 -0800725 return mMasterVolume;
726}
727
John Grossmand8f178d2012-07-20 14:51:35 -0700728bool AudioFlinger::masterMute_l() const
729{
John Grossmanee578c02012-07-23 17:05:46 -0700730 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700731}
732
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800733status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
734 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735{
736 // check calling permissions
737 if (!settingsAllowed()) {
738 return PERMISSION_DENIED;
739 }
740
Glenn Kasten263709e2012-01-06 08:40:01 -0800741 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000742 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743 return BAD_VALUE;
744 }
745
746 AutoMutex lock(mLock);
747 PlaybackThread *thread = NULL;
748 if (output) {
749 thread = checkPlaybackThread_l(output);
750 if (thread == NULL) {
751 return BAD_VALUE;
752 }
753 }
754
755 mStreamTypes[stream].volume = value;
756
757 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800758 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700759 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760 }
761 } else {
762 thread->setStreamVolume(stream, value);
763 }
764
765 return NO_ERROR;
766}
767
Glenn Kastenfff6d712012-01-12 16:38:12 -0800768status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769{
770 // check calling permissions
771 if (!settingsAllowed()) {
772 return PERMISSION_DENIED;
773 }
774
Glenn Kasten263709e2012-01-06 08:40:01 -0800775 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700776 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000777 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700778 return BAD_VALUE;
779 }
780
Eric Laurent93575202011-01-18 18:39:02 -0800781 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782 mStreamTypes[stream].mute = muted;
783 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700784 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700785
786 return NO_ERROR;
787}
788
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800789float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700790{
Glenn Kasten263709e2012-01-06 08:40:01 -0800791 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792 return 0.0f;
793 }
794
795 AutoMutex lock(mLock);
796 float volume;
797 if (output) {
798 PlaybackThread *thread = checkPlaybackThread_l(output);
799 if (thread == NULL) {
800 return 0.0f;
801 }
802 volume = thread->streamVolume(stream);
803 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800804 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805 }
806
807 return volume;
808}
809
Glenn Kastenfff6d712012-01-12 16:38:12 -0800810bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811{
Glenn Kasten263709e2012-01-06 08:40:01 -0800812 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700813 return true;
814 }
815
Glenn Kasten6637baa2012-01-09 09:40:36 -0800816 AutoMutex lock(mLock);
817 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818}
819
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800820status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700821{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700822 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
823 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800824
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825 // check calling permissions
826 if (!settingsAllowed()) {
827 return PERMISSION_DENIED;
828 }
829
Mathias Agopian65ab4712010-07-14 17:59:35 -0700830 // ioHandle == 0 means the parameters are global to the audio hardware interface
831 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700832 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700833 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800834 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700835 AutoMutex lock(mHardwareLock);
836 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
837 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
838 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
839 status_t result = dev->set_parameters(dev, keyValuePairs.string());
840 final_result = result ?: final_result;
841 }
842 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800843 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700844 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
845 AudioParameter param = AudioParameter(keyValuePairs);
846 String8 value;
847 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700848 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
849 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700850 for (size_t i = 0; i < mRecordThreads.size(); i++) {
851 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700852 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700853 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
854 // collect all of the thread's session IDs
855 KeyedVector<int, bool> ids = thread->sessionIds();
856 // suspend effects associated with those session IDs
857 for (size_t j = 0; j < ids.size(); ++j) {
858 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700859 thread->setEffectSuspended(FX_IID_AEC,
860 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700861 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700862 thread->setEffectSuspended(FX_IID_NS,
863 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700864 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700865 }
866 }
Eric Laurentbee53372011-08-29 12:42:48 -0700867 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700868 }
869 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700870 String8 screenState;
871 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
872 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800873 if (isOff != (AudioFlinger::mScreenState & 1)) {
874 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700875 }
876 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700877 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878 }
879
880 // hold a strong ref on thread in case closeOutput() or closeInput() is called
881 // and the thread is exited once the lock is released
882 sp<ThreadBase> thread;
883 {
884 Mutex::Autolock _l(mLock);
885 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700886 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700887 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800888 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700889 // indicate output device change to all input threads for pre processing
890 AudioParameter param = AudioParameter(keyValuePairs);
891 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700892 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
893 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700894 for (size_t i = 0; i < mRecordThreads.size(); i++) {
895 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
896 }
897 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898 }
899 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800900 if (thread != 0) {
901 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902 }
903 return BAD_VALUE;
904}
905
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800906String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700908 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
909 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910
Eric Laurenta4c5a552012-03-29 10:12:40 -0700911 Mutex::Autolock _l(mLock);
912
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700914 String8 out_s8;
915
Dima Zavin799a70e2011-04-18 16:57:27 -0700916 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800917 char *s;
918 {
919 AutoMutex lock(mHardwareLock);
920 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700921 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800922 s = dev->get_parameters(dev, keys.string());
923 mHardwareStatus = AUDIO_HW_IDLE;
924 }
John Grossmanef7740b2012-02-09 11:28:36 -0800925 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700926 free(s);
927 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700928 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700929 }
930
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
932 if (playbackThread != NULL) {
933 return playbackThread->getParameters(keys);
934 }
935 RecordThread *recordThread = checkRecordThread_l(ioHandle);
936 if (recordThread != NULL) {
937 return recordThread->getParameters(keys);
938 }
939 return String8("");
940}
941
Glenn Kastendd8104c2012-07-02 12:42:44 -0700942size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
943 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944{
Eric Laurenta1884f92011-08-23 08:25:03 -0700945 status_t ret = initCheck();
946 if (ret != NO_ERROR) {
947 return 0;
948 }
949
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800950 AutoMutex lock(mHardwareLock);
951 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700952 struct audio_config config = {
953 sample_rate: sampleRate,
Glenn Kastendd8104c2012-07-02 12:42:44 -0700954 channel_mask: channelMask,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700955 format: format,
956 };
John Grossmanee578c02012-07-23 17:05:46 -0700957 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
958 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800959 mHardwareStatus = AUDIO_HW_IDLE;
960 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961}
962
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800963unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965 Mutex::Autolock _l(mLock);
966
967 RecordThread *recordThread = checkRecordThread_l(ioHandle);
968 if (recordThread != NULL) {
969 return recordThread->getInputFramesLost();
970 }
971 return 0;
972}
973
974status_t AudioFlinger::setVoiceVolume(float value)
975{
Eric Laurenta1884f92011-08-23 08:25:03 -0700976 status_t ret = initCheck();
977 if (ret != NO_ERROR) {
978 return ret;
979 }
980
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 // check calling permissions
982 if (!settingsAllowed()) {
983 return PERMISSION_DENIED;
984 }
985
986 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700987 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800988 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -0700989 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990 mHardwareStatus = AUDIO_HW_IDLE;
991
992 return ret;
993}
994
Glenn Kasten26c77552012-11-16 12:01:44 -0800995status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800996 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700997{
998 status_t status;
999
1000 Mutex::Autolock _l(mLock);
1001
1002 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1003 if (playbackThread != NULL) {
1004 return playbackThread->getRenderPosition(halFrames, dspFrames);
1005 }
1006
1007 return BAD_VALUE;
1008}
1009
1010void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1011{
1012
1013 Mutex::Autolock _l(mLock);
1014
Glenn Kastenbb001922012-02-03 11:10:26 -08001015 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016 if (mNotificationClients.indexOfKey(pid) < 0) {
1017 sp<NotificationClient> notificationClient = new NotificationClient(this,
1018 client,
1019 pid);
Steve Block3856b092011-10-20 11:56:00 +01001020 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021
1022 mNotificationClients.add(pid, notificationClient);
1023
1024 sp<IBinder> binder = client->asBinder();
1025 binder->linkToDeath(notificationClient);
1026
1027 // the config change is always sent from playback or record threads to avoid deadlock
1028 // with AudioSystem::gLock
1029 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001030 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001031 }
1032
1033 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001034 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001035 }
1036 }
1037}
1038
1039void AudioFlinger::removeNotificationClient(pid_t pid)
1040{
1041 Mutex::Autolock _l(mLock);
1042
Glenn Kastena3b09252012-01-20 09:19:01 -08001043 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001044
Steve Block3856b092011-10-20 11:56:00 +01001045 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001046 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001047 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001048 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001049 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001050 ALOGV(" pid %d @ %d", ref->mPid, i);
1051 if (ref->mPid == pid) {
1052 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001053 mAudioSessionRefs.removeAt(i);
1054 delete ref;
1055 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001056 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001057 } else {
1058 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001059 }
1060 }
1061 if (removed) {
1062 purgeStaleEffects_l();
1063 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064}
1065
1066// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001067void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068{
1069 size_t size = mNotificationClients.size();
1070 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001071 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1072 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073 }
1074}
1075
1076// removeClient_l() must be called with AudioFlinger::mLock held
1077void AudioFlinger::removeClient_l(pid_t pid)
1078{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001079 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001080 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001081 mClients.removeItem(pid);
1082}
1083
Eric Laurent717e1282012-06-29 16:36:52 -07001084// getEffectThread_l() must be called with AudioFlinger::mLock held
1085sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1086{
1087 sp<PlaybackThread> thread;
1088
1089 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1090 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1091 ALOG_ASSERT(thread == 0);
1092 thread = mPlaybackThreads.valueAt(i);
1093 }
1094 }
1095
1096 return thread;
1097}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001098
Mathias Agopian65ab4712010-07-14 17:59:35 -07001099
Mathias Agopian65ab4712010-07-14 17:59:35 -07001100
1101// ----------------------------------------------------------------------------
1102
1103AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1104 : RefBase(),
1105 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001106 // 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 -07001107 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001108 mPid(pid),
1109 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110{
1111 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1112}
1113
1114// Client destructor must be called with AudioFlinger::mLock held
1115AudioFlinger::Client::~Client()
1116{
1117 mAudioFlinger->removeClient_l(mPid);
1118}
1119
Glenn Kasten435dbe62012-01-30 10:15:48 -08001120sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121{
1122 return mMemoryDealer;
1123}
1124
John Grossman4ff14ba2012-02-08 16:37:41 -08001125// Reserve one of the limited slots for a timed audio track associated
1126// with this client
1127bool AudioFlinger::Client::reserveTimedTrack()
1128{
1129 const int kMaxTimedTracksPerClient = 4;
1130
1131 Mutex::Autolock _l(mTimedTrackLock);
1132
1133 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1134 ALOGW("can not create timed track - pid %d has exceeded the limit",
1135 mPid);
1136 return false;
1137 }
1138
1139 mTimedTrackCount++;
1140 return true;
1141}
1142
1143// Release a slot for a timed audio track
1144void AudioFlinger::Client::releaseTimedTrack()
1145{
1146 Mutex::Autolock _l(mTimedTrackLock);
1147 mTimedTrackCount--;
1148}
1149
Mathias Agopian65ab4712010-07-14 17:59:35 -07001150// ----------------------------------------------------------------------------
1151
1152AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1153 const sp<IAudioFlingerClient>& client,
1154 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001155 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001156{
1157}
1158
1159AudioFlinger::NotificationClient::~NotificationClient()
1160{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001161}
1162
1163void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1164{
1165 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001166 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167}
1168
Mathias Agopian65ab4712010-07-14 17:59:35 -07001169
1170// ----------------------------------------------------------------------------
1171
1172sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001173 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001174 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001175 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001176 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001177 size_t frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08001178 IAudioFlinger::track_flags_t flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001179 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001180 int *sessionId,
1181 status_t *status)
1182{
1183 sp<RecordThread::RecordTrack> recordTrack;
1184 sp<RecordHandle> recordHandle;
1185 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186 status_t lStatus;
1187 RecordThread *thread;
1188 size_t inFrameCount;
1189 int lSessionId;
1190
1191 // check calling permissions
1192 if (!recordingAllowed()) {
1193 lStatus = PERMISSION_DENIED;
1194 goto Exit;
1195 }
1196
1197 // add client to list
1198 { // scope for mLock
1199 Mutex::Autolock _l(mLock);
1200 thread = checkRecordThread_l(input);
1201 if (thread == NULL) {
1202 lStatus = BAD_VALUE;
1203 goto Exit;
1204 }
1205
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001206 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001207 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001208
1209 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001210 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211 lSessionId = *sessionId;
1212 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001213 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001214 if (sessionId != NULL) {
1215 *sessionId = lSessionId;
1216 }
1217 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001218 // create new record track.
1219 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001220 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1221 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001223 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001224 // remove local strong reference to Client before deleting the RecordTrack so that the
1225 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226 client.clear();
1227 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001228 goto Exit;
1229 }
1230
1231 // return to handle to client
1232 recordHandle = new RecordHandle(recordTrack);
1233 lStatus = NO_ERROR;
1234
1235Exit:
1236 if (status) {
1237 *status = lStatus;
1238 }
1239 return recordHandle;
1240}
1241
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001242
1243
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244// ----------------------------------------------------------------------------
1245
Eric Laurenta4c5a552012-03-29 10:12:40 -07001246audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1247{
1248 if (!settingsAllowed()) {
1249 return 0;
1250 }
1251 Mutex::Autolock _l(mLock);
1252 return loadHwModule_l(name);
1253}
1254
1255// loadHwModule_l() must be called with AudioFlinger::mLock held
1256audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1257{
1258 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1259 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1260 ALOGW("loadHwModule() module %s already loaded", name);
1261 return mAudioHwDevs.keyAt(i);
1262 }
1263 }
1264
Eric Laurenta4c5a552012-03-29 10:12:40 -07001265 audio_hw_device_t *dev;
1266
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001267 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001268 if (rc) {
1269 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1270 return 0;
1271 }
1272
1273 mHardwareStatus = AUDIO_HW_INIT;
1274 rc = dev->init_check(dev);
1275 mHardwareStatus = AUDIO_HW_IDLE;
1276 if (rc) {
1277 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1278 return 0;
1279 }
1280
John Grossmanee578c02012-07-23 17:05:46 -07001281 // Check and cache this HAL's level of support for master mute and master
1282 // volume. If this is the first HAL opened, and it supports the get
1283 // methods, use the initial values provided by the HAL as the current
1284 // master mute and volume settings.
1285
1286 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1287 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001288 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001289
1290 if (0 == mAudioHwDevs.size()) {
1291 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1292 if (NULL != dev->get_master_volume) {
1293 float mv;
1294 if (OK == dev->get_master_volume(dev, &mv)) {
1295 mMasterVolume = mv;
1296 }
1297 }
1298
1299 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1300 if (NULL != dev->get_master_mute) {
1301 bool mm;
1302 if (OK == dev->get_master_mute(dev, &mm)) {
1303 mMasterMute = mm;
1304 }
1305 }
1306 }
1307
Eric Laurenta4c5a552012-03-29 10:12:40 -07001308 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001309 if ((NULL != dev->set_master_volume) &&
1310 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1311 flags = static_cast<AudioHwDevice::Flags>(flags |
1312 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1313 }
1314
1315 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1316 if ((NULL != dev->set_master_mute) &&
1317 (OK == dev->set_master_mute(dev, mMasterMute))) {
1318 flags = static_cast<AudioHwDevice::Flags>(flags |
1319 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1320 }
1321
Eric Laurenta4c5a552012-03-29 10:12:40 -07001322 mHardwareStatus = AUDIO_HW_IDLE;
1323 }
1324
1325 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001326 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001327
1328 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001329 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001330
1331 return handle;
1332
1333}
1334
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001335// ----------------------------------------------------------------------------
1336
Glenn Kasten3b16c762012-11-14 08:44:39 -08001337uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001338{
1339 Mutex::Autolock _l(mLock);
1340 PlaybackThread *thread = primaryPlaybackThread_l();
1341 return thread != NULL ? thread->sampleRate() : 0;
1342}
1343
Glenn Kastene33054e2012-11-14 12:54:39 -08001344size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001345{
1346 Mutex::Autolock _l(mLock);
1347 PlaybackThread *thread = primaryPlaybackThread_l();
1348 return thread != NULL ? thread->frameCountHAL() : 0;
1349}
1350
1351// ----------------------------------------------------------------------------
1352
Eric Laurenta4c5a552012-03-29 10:12:40 -07001353audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1354 audio_devices_t *pDevices,
1355 uint32_t *pSamplingRate,
1356 audio_format_t *pFormat,
1357 audio_channel_mask_t *pChannelMask,
1358 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001359 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360{
1361 status_t status;
1362 PlaybackThread *thread = NULL;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001363 struct audio_config config = {
1364 sample_rate: pSamplingRate ? *pSamplingRate : 0,
1365 channel_mask: pChannelMask ? *pChannelMask : 0,
1366 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT,
1367 };
1368 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001369 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001370
Eric Laurenta4c5a552012-03-29 10:12:40 -07001371 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
1372 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001373 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001374 config.sample_rate,
1375 config.format,
1376 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001377 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378
1379 if (pDevices == NULL || *pDevices == 0) {
1380 return 0;
1381 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001382
Mathias Agopian65ab4712010-07-14 17:59:35 -07001383 Mutex::Autolock _l(mLock);
1384
Eric Laurenta4c5a552012-03-29 10:12:40 -07001385 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001386 if (outHwDev == NULL)
1387 return 0;
1388
John Grossmanee578c02012-07-23 17:05:46 -07001389 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001390 audio_io_handle_t id = nextUniqueId();
1391
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001392 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001393
John Grossmanee578c02012-07-23 17:05:46 -07001394 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001395 id,
1396 *pDevices,
1397 (audio_output_flags_t)flags,
1398 &config,
1399 &outStream);
1400
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001401 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001402 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, "
1403 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001404 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001405 config.sample_rate,
1406 config.format,
1407 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408 status);
1409
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001410 if (status == NO_ERROR && outStream != NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001411 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001412
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001413 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001414 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1415 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001416 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001417 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001418 } else {
1419 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001420 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001421 }
1422 mPlaybackThreads.add(id, thread);
1423
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001424 if (pSamplingRate != NULL) *pSamplingRate = config.sample_rate;
1425 if (pFormat != NULL) *pFormat = config.format;
1426 if (pChannelMask != NULL) *pChannelMask = config.channel_mask;
Glenn Kastena0d68332012-01-27 16:47:15 -08001427 if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428
1429 // notify client processes of the new output creation
1430 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001431
1432 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001433 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001434 ALOGI("Using module %d has the primary audio interface", module);
1435 mPrimaryHardwareDev = outHwDev;
1436
1437 AutoMutex lock(mHardwareLock);
1438 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001439 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001440 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001441 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001442 return id;
1443 }
1444
1445 return 0;
1446}
1447
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001448audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1449 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001450{
1451 Mutex::Autolock _l(mLock);
1452 MixerThread *thread1 = checkMixerThread_l(output1);
1453 MixerThread *thread2 = checkMixerThread_l(output2);
1454
1455 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001456 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1457 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001458 return 0;
1459 }
1460
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001461 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001462 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1463 thread->addOutputTrack(thread2);
1464 mPlaybackThreads.add(id, thread);
1465 // notify client processes of the new output creation
1466 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1467 return id;
1468}
1469
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001470status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471{
Glenn Kastend96c5722012-04-25 13:44:49 -07001472 return closeOutput_nonvirtual(output);
1473}
1474
1475status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1476{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001477 // keep strong reference on the playback thread so that
1478 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001479 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001480 {
1481 Mutex::Autolock _l(mLock);
1482 thread = checkPlaybackThread_l(output);
1483 if (thread == NULL) {
1484 return BAD_VALUE;
1485 }
1486
Steve Block3856b092011-10-20 11:56:00 +01001487 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001488
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001489 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001491 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001492 DuplicatingThread *dupThread =
1493 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001494 dupThread->removeOutputTrack((MixerThread *)thread.get());
1495 }
1496 }
1497 }
Glenn Kastena1117922012-01-26 10:53:32 -08001498 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001499 mPlaybackThreads.removeItem(output);
1500 }
1501 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001502 // The thread entity (active unit of execution) is no longer running here,
1503 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001504
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001505 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001506 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001507 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001508 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001509 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001510 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001511 }
1512 return NO_ERROR;
1513}
1514
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001515status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516{
1517 Mutex::Autolock _l(mLock);
1518 PlaybackThread *thread = checkPlaybackThread_l(output);
1519
1520 if (thread == NULL) {
1521 return BAD_VALUE;
1522 }
1523
Steve Block3856b092011-10-20 11:56:00 +01001524 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525 thread->suspend();
1526
1527 return NO_ERROR;
1528}
1529
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001530status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001531{
1532 Mutex::Autolock _l(mLock);
1533 PlaybackThread *thread = checkPlaybackThread_l(output);
1534
1535 if (thread == NULL) {
1536 return BAD_VALUE;
1537 }
1538
Steve Block3856b092011-10-20 11:56:00 +01001539 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001540
1541 thread->restore();
1542
1543 return NO_ERROR;
1544}
1545
Eric Laurenta4c5a552012-03-29 10:12:40 -07001546audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1547 audio_devices_t *pDevices,
1548 uint32_t *pSamplingRate,
1549 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001550 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551{
1552 status_t status;
1553 RecordThread *thread = NULL;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001554 struct audio_config config = {
1555 sample_rate: pSamplingRate ? *pSamplingRate : 0,
1556 channel_mask: pChannelMask ? *pChannelMask : 0,
1557 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT,
1558 };
1559 uint32_t reqSamplingRate = config.sample_rate;
1560 audio_format_t reqFormat = config.format;
1561 audio_channel_mask_t reqChannels = config.channel_mask;
1562 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001563 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001564
1565 if (pDevices == NULL || *pDevices == 0) {
1566 return 0;
1567 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001568
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569 Mutex::Autolock _l(mLock);
1570
Eric Laurenta4c5a552012-03-29 10:12:40 -07001571 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001572 if (inHwDev == NULL)
1573 return 0;
1574
John Grossmanee578c02012-07-23 17:05:46 -07001575 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001576 audio_io_handle_t id = nextUniqueId();
1577
John Grossmanee578c02012-07-23 17:05:46 -07001578 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001579 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001580 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1581 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001582 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001583 config.sample_rate,
1584 config.format,
1585 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001586 status);
1587
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001588 // If the input could not be opened with the requested parameters and we can handle the
1589 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1590 // 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 -07001591 if (status == BAD_VALUE &&
1592 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1593 (config.sample_rate <= 2 * reqSamplingRate) &&
1594 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001595 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001596 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001597 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001598 }
1599
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001600 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001601
1602 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1603 // or (re-)create if current Pipe is idle and does not match the new format
1604 sp<NBAIO_Sink> teeSink;
1605#ifdef TEE_SINK_INPUT_FRAMES
1606 enum {
1607 TEE_SINK_NO, // don't copy input
1608 TEE_SINK_NEW, // copy input using a new pipe
1609 TEE_SINK_OLD, // copy input using an existing pipe
1610 } kind;
1611 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1612 popcount(inStream->common.get_channels(&inStream->common)));
1613 if (format == Format_Invalid) {
1614 kind = TEE_SINK_NO;
1615 } else if (mRecordTeeSink == 0) {
1616 kind = TEE_SINK_NEW;
1617 } else if (mRecordTeeSink->getStrongCount() != 1) {
1618 kind = TEE_SINK_NO;
1619 } else if (format == mRecordTeeSink->format()) {
1620 kind = TEE_SINK_OLD;
1621 } else {
1622 kind = TEE_SINK_NEW;
1623 }
1624 switch (kind) {
1625 case TEE_SINK_NEW: {
1626 Pipe *pipe = new Pipe(TEE_SINK_INPUT_FRAMES, format);
1627 size_t numCounterOffers = 0;
1628 const NBAIO_Format offers[1] = {format};
1629 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1630 ALOG_ASSERT(index == 0);
1631 PipeReader *pipeReader = new PipeReader(*pipe);
1632 numCounterOffers = 0;
1633 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1634 ALOG_ASSERT(index == 0);
1635 mRecordTeeSink = pipe;
1636 mRecordTeeSource = pipeReader;
1637 teeSink = pipe;
1638 }
1639 break;
1640 case TEE_SINK_OLD:
1641 teeSink = mRecordTeeSink;
1642 break;
1643 case TEE_SINK_NO:
1644 default:
1645 break;
1646 }
1647#endif
Dima Zavin799a70e2011-04-18 16:57:27 -07001648 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1649
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001650 // Start record thread
1651 // RecorThread require both input and output device indication to forward to audio
1652 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001653 thread = new RecordThread(this,
1654 input,
1655 reqSamplingRate,
1656 reqChannels,
1657 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001658 primaryOutputDevice_l(),
1659 *pDevices,
1660 teeSink);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001661 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001662 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kastena0d68332012-01-27 16:47:15 -08001663 if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001664 if (pFormat != NULL) *pFormat = config.format;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001665 if (pChannelMask != NULL) *pChannelMask = reqChannels;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001666
Mathias Agopian65ab4712010-07-14 17:59:35 -07001667 // notify client processes of the new input creation
1668 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1669 return id;
1670 }
1671
1672 return 0;
1673}
1674
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001675status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001676{
Glenn Kastend96c5722012-04-25 13:44:49 -07001677 return closeInput_nonvirtual(input);
1678}
1679
1680status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1681{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001682 // keep strong reference on the record thread so that
1683 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001684 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001685 {
1686 Mutex::Autolock _l(mLock);
1687 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001688 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001689 return BAD_VALUE;
1690 }
1691
Steve Block3856b092011-10-20 11:56:00 +01001692 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001693 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001694 mRecordThreads.removeItem(input);
1695 }
1696 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001697 // The thread entity (active unit of execution) is no longer running here,
1698 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001699
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001700 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001701 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001702 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001703 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001704 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001705
1706 return NO_ERROR;
1707}
1708
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001709status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001710{
1711 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001712 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001713
1714 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1715 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001716 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001717 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001718
1719 return NO_ERROR;
1720}
1721
1722
1723int AudioFlinger::newAudioSessionId()
1724{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001725 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001726}
1727
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001728void AudioFlinger::acquireAudioSessionId(int audioSession)
1729{
1730 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001731 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001732 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001733 size_t num = mAudioSessionRefs.size();
1734 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001735 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001736 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1737 ref->mCnt++;
1738 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001739 return;
1740 }
1741 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001742 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1743 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001744}
1745
1746void AudioFlinger::releaseAudioSessionId(int audioSession)
1747{
1748 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001749 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001750 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001751 size_t num = mAudioSessionRefs.size();
1752 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001753 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001754 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1755 ref->mCnt--;
1756 ALOGV(" decremented refcount to %d", ref->mCnt);
1757 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001758 mAudioSessionRefs.removeAt(i);
1759 delete ref;
1760 purgeStaleEffects_l();
1761 }
1762 return;
1763 }
1764 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001765 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001766}
1767
1768void AudioFlinger::purgeStaleEffects_l() {
1769
Steve Block3856b092011-10-20 11:56:00 +01001770 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001771
1772 Vector< sp<EffectChain> > chains;
1773
1774 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1775 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1776 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1777 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001778 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1779 chains.push(ec);
1780 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001781 }
1782 }
1783 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1784 sp<RecordThread> t = mRecordThreads.valueAt(i);
1785 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1786 sp<EffectChain> ec = t->mEffectChains[j];
1787 chains.push(ec);
1788 }
1789 }
1790
1791 for (size_t i = 0; i < chains.size(); i++) {
1792 sp<EffectChain> ec = chains[i];
1793 int sessionid = ec->sessionId();
1794 sp<ThreadBase> t = ec->mThread.promote();
1795 if (t == 0) {
1796 continue;
1797 }
1798 size_t numsessionrefs = mAudioSessionRefs.size();
1799 bool found = false;
1800 for (size_t k = 0; k < numsessionrefs; k++) {
1801 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001802 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001803 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001804 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001805 found = true;
1806 break;
1807 }
1808 }
1809 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001810 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001811 // remove all effects from the chain
1812 while (ec->mEffects.size()) {
1813 sp<EffectModule> effect = ec->mEffects[0];
1814 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001815 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001816 if (effect->purgeHandles()) {
1817 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001818 }
1819 AudioSystem::unregisterEffect(effect->id());
1820 }
1821 }
1822 }
1823 return;
1824}
1825
Mathias Agopian65ab4712010-07-14 17:59:35 -07001826// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001827AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001828{
Glenn Kastena1117922012-01-26 10:53:32 -08001829 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001830}
1831
1832// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001833AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001834{
1835 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001836 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001837}
1838
1839// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001840AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001841{
Glenn Kastena1117922012-01-26 10:53:32 -08001842 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001843}
1844
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001845uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001846{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001847 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001848}
1849
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001850AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001851{
1852 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1853 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001854 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001855 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001856 return thread;
1857 }
1858 }
1859 return NULL;
1860}
1861
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001862audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001863{
1864 PlaybackThread *thread = primaryPlaybackThread_l();
1865
1866 if (thread == NULL) {
1867 return 0;
1868 }
1869
Eric Laurentf1c04f92012-08-28 14:26:53 -07001870 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001871}
1872
Eric Laurenta011e352012-03-29 15:51:43 -07001873sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1874 int triggerSession,
1875 int listenerSession,
1876 sync_event_callback_t callBack,
1877 void *cookie)
1878{
1879 Mutex::Autolock _l(mLock);
1880
1881 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1882 status_t playStatus = NAME_NOT_FOUND;
1883 status_t recStatus = NAME_NOT_FOUND;
1884 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1885 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1886 if (playStatus == NO_ERROR) {
1887 return event;
1888 }
1889 }
1890 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1891 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1892 if (recStatus == NO_ERROR) {
1893 return event;
1894 }
1895 }
1896 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
1897 mPendingSyncEvents.add(event);
1898 } else {
1899 ALOGV("createSyncEvent() invalid event %d", event->type());
1900 event.clear();
1901 }
1902 return event;
1903}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001904
Mathias Agopian65ab4712010-07-14 17:59:35 -07001905// ----------------------------------------------------------------------------
1906// Effect management
1907// ----------------------------------------------------------------------------
1908
1909
Glenn Kastenf587ba52012-01-26 16:25:10 -08001910status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001911{
1912 Mutex::Autolock _l(mLock);
1913 return EffectQueryNumberEffects(numEffects);
1914}
1915
Glenn Kastenf587ba52012-01-26 16:25:10 -08001916status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001917{
1918 Mutex::Autolock _l(mLock);
1919 return EffectQueryEffect(index, descriptor);
1920}
1921
Glenn Kasten5e92a782012-01-30 07:40:52 -08001922status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08001923 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001924{
1925 Mutex::Autolock _l(mLock);
1926 return EffectGetDescriptor(pUuid, descriptor);
1927}
1928
1929
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001930sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07001931 effect_descriptor_t *pDesc,
1932 const sp<IEffectClient>& effectClient,
1933 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001934 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935 int sessionId,
1936 status_t *status,
1937 int *id,
1938 int *enabled)
1939{
1940 status_t lStatus = NO_ERROR;
1941 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001942 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001943
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001944 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001945 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001946 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001947
1948 if (pDesc == NULL) {
1949 lStatus = BAD_VALUE;
1950 goto Exit;
1951 }
1952
Eric Laurent84e9a102010-09-23 16:10:16 -07001953 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07001954 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001955 lStatus = PERMISSION_DENIED;
1956 goto Exit;
1957 }
1958
Dima Zavinfce7a472011-04-19 22:30:36 -07001959 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07001960 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08001961 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001962 lStatus = PERMISSION_DENIED;
1963 goto Exit;
1964 }
1965
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001966 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001967 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001968 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07001969 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07001970 lStatus = BAD_VALUE;
1971 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07001972 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001973 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001974 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07001975 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001976 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07001977 }
1978 }
1979
Mathias Agopian65ab4712010-07-14 17:59:35 -07001980 {
1981 Mutex::Autolock _l(mLock);
1982
Mathias Agopian65ab4712010-07-14 17:59:35 -07001983
1984 if (!EffectIsNullUuid(&pDesc->uuid)) {
1985 // if uuid is specified, request effect descriptor
1986 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
1987 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001988 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001989 goto Exit;
1990 }
1991 } else {
1992 // if uuid is not specified, look for an available implementation
1993 // of the required type in effect factory
1994 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001995 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001996 lStatus = BAD_VALUE;
1997 goto Exit;
1998 }
1999 uint32_t numEffects = 0;
2000 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002001 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002002 bool found = false;
2003
2004 lStatus = EffectQueryNumberEffects(&numEffects);
2005 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002006 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002007 goto Exit;
2008 }
2009 for (uint32_t i = 0; i < numEffects; i++) {
2010 lStatus = EffectQueryEffect(i, &desc);
2011 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002012 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002013 continue;
2014 }
2015 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2016 // If matching type found save effect descriptor. If the session is
2017 // 0 and the effect is not auxiliary, continue enumeration in case
2018 // an auxiliary version of this effect type is available
2019 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002020 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002021 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002022 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2023 break;
2024 }
2025 }
2026 }
2027 if (!found) {
2028 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002029 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002030 goto Exit;
2031 }
2032 // For same effect type, chose auxiliary version over insert version if
2033 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002034 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002035 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002036 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002037 }
2038 }
2039
2040 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002041 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002042 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2043 lStatus = INVALID_OPERATION;
2044 goto Exit;
2045 }
2046
Eric Laurent59255e42011-07-27 19:49:51 -07002047 // check recording permission for visualizer
2048 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2049 !recordingAllowed()) {
2050 lStatus = PERMISSION_DENIED;
2051 goto Exit;
2052 }
2053
Mathias Agopian65ab4712010-07-14 17:59:35 -07002054 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002055 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002056
2057 // If output is not specified try to find a matching audio session ID in one of the
2058 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002059 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2060 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002061 // Note: io is never 0 when creating an effect on an input
2062 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002063 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002064 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2065 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002066 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002067 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002068 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002069 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002070 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002071 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2072 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2073 io = mRecordThreads.keyAt(i);
2074 break;
2075 }
2076 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002077 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002078 // If no output thread contains the requested session ID, default to
2079 // first output. The effect chain will be moved to the correct output
2080 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002081 if (io == 0 && mPlaybackThreads.size()) {
2082 io = mPlaybackThreads.keyAt(0);
2083 }
Steve Block3856b092011-10-20 11:56:00 +01002084 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002085 }
2086 ThreadBase *thread = checkRecordThread_l(io);
2087 if (thread == NULL) {
2088 thread = checkPlaybackThread_l(io);
2089 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002090 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002091 lStatus = BAD_VALUE;
2092 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002093 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002094 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002095
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002096 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002097
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002098 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002099 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2100 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002101 if (handle != 0 && id != NULL) {
2102 *id = handle->id();
2103 }
2104 }
2105
2106Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002107 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002108 *status = lStatus;
2109 }
2110 return handle;
2111}
2112
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002113status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2114 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002115{
Steve Block3856b092011-10-20 11:56:00 +01002116 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002117 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002118 Mutex::Autolock _l(mLock);
2119 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002120 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002121 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002122 }
Eric Laurentde070132010-07-13 04:45:46 -07002123 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2124 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002125 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002126 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002127 }
Eric Laurentde070132010-07-13 04:45:46 -07002128 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2129 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002130 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002131 return BAD_VALUE;
2132 }
2133
2134 Mutex::Autolock _dl(dstThread->mLock);
2135 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002136 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002137
Mathias Agopian65ab4712010-07-14 17:59:35 -07002138 return NO_ERROR;
2139}
2140
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002141// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002142status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002143 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002144 AudioFlinger::PlaybackThread *dstThread,
2145 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002146{
Steve Block3856b092011-10-20 11:56:00 +01002147 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002148 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002149
Eric Laurent59255e42011-07-27 19:49:51 -07002150 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002151 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002152 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002153 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002154 return INVALID_OPERATION;
2155 }
2156
Eric Laurent39e94f82010-07-28 01:32:47 -07002157 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002158 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002159 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002160 // removed.
2161 srcThread->removeEffectChain_l(chain);
2162
2163 // transfer all effects one by one so that new effect chain is created on new thread with
2164 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002165 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002166 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002167 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002168 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2169 while (effect != 0) {
2170 srcThread->removeEffect_l(effect);
2171 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002172 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2173 if (effect->state() == EffectModule::ACTIVE ||
2174 effect->state() == EffectModule::STOPPING) {
2175 effect->start();
2176 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002177 // if the move request is not received from audio policy manager, the effect must be
2178 // re-registered with the new strategy and output
2179 if (dstChain == 0) {
2180 dstChain = effect->chain().promote();
2181 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002182 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002183 srcThread->addEffect_l(effect);
2184 return NO_INIT;
2185 }
2186 strategy = dstChain->strategy();
2187 }
2188 if (reRegister) {
2189 AudioSystem::unregisterEffect(effect->id());
2190 AudioSystem::registerEffect(&effect->desc(),
2191 dstOutput,
2192 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002193 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002194 effect->id());
2195 }
Eric Laurentde070132010-07-13 04:45:46 -07002196 effect = chain->getEffectFromId_l(0);
2197 }
2198
2199 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002200}
2201
Eric Laurent81784c32012-11-19 14:55:58 -08002202void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002203{
Eric Laurent81784c32012-11-19 14:55:58 -08002204 NBAIO_Source *teeSource = source.get();
2205 if (teeSource != NULL) {
2206 char teeTime[16];
2207 struct timeval tv;
2208 gettimeofday(&tv, NULL);
2209 struct tm tm;
2210 localtime_r(&tv.tv_sec, &tm);
2211 strftime(teeTime, sizeof(teeTime), "%T", &tm);
2212 char teePath[64];
2213 sprintf(teePath, "/data/misc/media/%s_%d.wav", teeTime, id);
2214 int teeFd = open(teePath, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
2215 if (teeFd >= 0) {
2216 char wavHeader[44];
2217 memcpy(wavHeader,
2218 "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",
2219 sizeof(wavHeader));
2220 NBAIO_Format format = teeSource->format();
2221 unsigned channelCount = Format_channelCount(format);
2222 ALOG_ASSERT(channelCount <= FCC_2);
2223 uint32_t sampleRate = Format_sampleRate(format);
2224 wavHeader[22] = channelCount; // number of channels
2225 wavHeader[24] = sampleRate; // sample rate
2226 wavHeader[25] = sampleRate >> 8;
2227 wavHeader[32] = channelCount * 2; // block alignment
2228 write(teeFd, wavHeader, sizeof(wavHeader));
2229 size_t total = 0;
2230 bool firstRead = true;
2231 for (;;) {
2232#define TEE_SINK_READ 1024
2233 short buffer[TEE_SINK_READ * FCC_2];
2234 size_t count = TEE_SINK_READ;
2235 ssize_t actual = teeSource->read(buffer, count,
2236 AudioBufferProvider::kInvalidPTS);
2237 bool wasFirstRead = firstRead;
2238 firstRead = false;
2239 if (actual <= 0) {
2240 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2241 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002242 }
Eric Laurent81784c32012-11-19 14:55:58 -08002243 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002244 }
Eric Laurent81784c32012-11-19 14:55:58 -08002245 ALOG_ASSERT(actual <= (ssize_t)count);
2246 write(teeFd, buffer, actual * channelCount * sizeof(short));
2247 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002248 }
Eric Laurent81784c32012-11-19 14:55:58 -08002249 lseek(teeFd, (off_t) 4, SEEK_SET);
2250 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2251 write(teeFd, &temp, sizeof(temp));
2252 lseek(teeFd, (off_t) 40, SEEK_SET);
2253 temp = total * channelCount * sizeof(short);
2254 write(teeFd, &temp, sizeof(temp));
2255 close(teeFd);
2256 fdprintf(fd, "FastMixer tee copied to %s\n", teePath);
Eric Laurent59255e42011-07-27 19:49:51 -07002257 } else {
Eric Laurent81784c32012-11-19 14:55:58 -08002258 fdprintf(fd, "FastMixer unable to create tee %s: \n", strerror(errno));
Eric Laurent59255e42011-07-27 19:49:51 -07002259 }
2260 }
2261}
2262
Mathias Agopian65ab4712010-07-14 17:59:35 -07002263// ----------------------------------------------------------------------------
2264
2265status_t AudioFlinger::onTransact(
2266 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2267{
2268 return BnAudioFlinger::onTransact(code, data, reply, flags);
2269}
2270
Mathias Agopian65ab4712010-07-14 17:59:35 -07002271}; // namespace android