blob: 52fa576aba0e6a568581f691bb0313487211e028 [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 Laurentca7cc822012-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 Laurentca7cc822012-11-19 14:55:58 -080060//#include <common_time/local_clock.h>
Glenn Kasten58912562012-04-03 10:45:00 -070061
Glenn Kasten011aa652013-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 Laurentca7cc822012-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 Kasten011aa652013-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 Kasten2662ac92012-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 Kasten2662ac92012-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 Laurent88959252012-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 Laurentca7cc822012-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 Laurentca7cc822012-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 Laurentca7cc822012-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 Kasten8c327342012-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 Kastena2049222012-06-22 17:21:07 -0700330 if (locked) {
331 mLock.unlock();
332 }
Glenn Kasten011aa652013-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 Kasten011aa652013-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{
377 sp<IMemory> iMemory(writer->getIMemory());
378 if (iMemory == 0) {
379 return;
380 }
381 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
382 if (binder != 0) {
383 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
384 // Now the media.log remote reference to IMemory is gone.
385 // When our last local reference to IMemory also drops to zero,
386 // the IMemory destructor will deallocate the region from mMemoryDealer.
387 }
388}
389
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390// IAudioFlinger interface
391
392
393sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800394 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800396 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700397 audio_channel_mask_t channelMask,
Glenn Kasten7da35f22012-11-14 12:54:39 -0800398 size_t frameCount,
Glenn Kastenc2674152012-11-06 15:03:34 -0800399 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800401 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800402 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403 int *sessionId,
404 status_t *status)
405{
406 sp<PlaybackThread::Track> track;
407 sp<TrackHandle> trackHandle;
408 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 status_t lStatus;
410 int lSessionId;
411
Glenn Kasten263709e2012-01-06 08:40:01 -0800412 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
413 // but if someone uses binder directly they could bypass that and cause us to crash
414 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000415 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416 lStatus = BAD_VALUE;
417 goto Exit;
418 }
419
Glenn Kasten520a9af2012-06-21 12:56:37 -0700420 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
421 // and we don't yet support 8.24 or 32-bit PCM
422 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
423 ALOGE("createTrack() invalid format %d", format);
424 lStatus = BAD_VALUE;
425 goto Exit;
426 }
427
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428 {
429 Mutex::Autolock _l(mLock);
430 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700431 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000433 ALOGE("unknown output thread");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700434 lStatus = BAD_VALUE;
435 goto Exit;
436 }
437
Glenn Kastenf37971f2012-02-03 11:06:53 -0800438 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800439 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440
Steve Block3856b092011-10-20 11:56:00 +0100441 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700442 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700443 // check if an effect chain with the same session ID is present on another
444 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700445 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700446 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
447 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700448 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700449 if (sessions & PlaybackThread::EFFECT_SESSION) {
450 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700451 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700452 }
Eric Laurentde070132010-07-13 04:45:46 -0700453 }
454 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700455 lSessionId = *sessionId;
456 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700457 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700458 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700459 if (sessionId != NULL) {
460 *sessionId = lSessionId;
461 }
462 }
Steve Block3856b092011-10-20 11:56:00 +0100463 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700464
465 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800466 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700467
468 // move effect chain to this output thread if an effect on same session was waiting
469 // for a track to be created
470 if (lStatus == NO_ERROR && effectThread != NULL) {
471 Mutex::Autolock _dl(thread->mLock);
472 Mutex::Autolock _sl(effectThread->mLock);
473 moveEffectChain_l(lSessionId, effectThread, thread, true);
474 }
Eric Laurenta011e352012-03-29 15:51:43 -0700475
476 // Look for sync events awaiting for a session to be used.
477 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
478 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
479 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700480 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700481 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700482 } else {
483 mPendingSyncEvents[i]->cancel();
484 }
Eric Laurenta011e352012-03-29 15:51:43 -0700485 mPendingSyncEvents.removeAt(i);
486 i--;
487 }
488 }
489 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700490 }
491 if (lStatus == NO_ERROR) {
492 trackHandle = new TrackHandle(track);
493 } else {
494 // remove local strong reference to Client before deleting the Track so that the Client
495 // destructor is called by the TrackBase destructor with mLock held
496 client.clear();
497 track.clear();
498 }
499
500Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700501 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700502 *status = lStatus;
503 }
504 return trackHandle;
505}
506
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800507uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700508{
509 Mutex::Autolock _l(mLock);
510 PlaybackThread *thread = checkPlaybackThread_l(output);
511 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000512 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700513 return 0;
514 }
515 return thread->sampleRate();
516}
517
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800518int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519{
520 Mutex::Autolock _l(mLock);
521 PlaybackThread *thread = checkPlaybackThread_l(output);
522 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000523 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700524 return 0;
525 }
526 return thread->channelCount();
527}
528
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800529audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700530{
531 Mutex::Autolock _l(mLock);
532 PlaybackThread *thread = checkPlaybackThread_l(output);
533 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000534 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800535 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700536 }
537 return thread->format();
538}
539
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800540size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541{
542 Mutex::Autolock _l(mLock);
543 PlaybackThread *thread = checkPlaybackThread_l(output);
544 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000545 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 return 0;
547 }
Glenn Kasten58912562012-04-03 10:45:00 -0700548 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
549 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 return thread->frameCount();
551}
552
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800553uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700554{
555 Mutex::Autolock _l(mLock);
556 PlaybackThread *thread = checkPlaybackThread_l(output);
557 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000558 ALOGW("latency() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700559 return 0;
560 }
561 return thread->latency();
562}
563
564status_t AudioFlinger::setMasterVolume(float value)
565{
Eric Laurenta1884f92011-08-23 08:25:03 -0700566 status_t ret = initCheck();
567 if (ret != NO_ERROR) {
568 return ret;
569 }
570
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571 // check calling permissions
572 if (!settingsAllowed()) {
573 return PERMISSION_DENIED;
574 }
575
Eric Laurenta4c5a552012-03-29 10:12:40 -0700576 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700577 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700578
John Grossmanee578c02012-07-23 17:05:46 -0700579 // Set master volume in the HALs which support it.
580 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
581 AutoMutex lock(mHardwareLock);
582 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800583
John Grossmanee578c02012-07-23 17:05:46 -0700584 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
585 if (dev->canSetMasterVolume()) {
586 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800587 }
John Grossmanee578c02012-07-23 17:05:46 -0700588 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590
John Grossmanee578c02012-07-23 17:05:46 -0700591 // Now set the master volume in each playback thread. Playback threads
592 // assigned to HALs which do not have master volume support will apply
593 // master volume during the mix operation. Threads with HALs which do
594 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800595 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700596 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597
598 return NO_ERROR;
599}
600
Glenn Kastenf78aee72012-01-04 11:00:47 -0800601status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602{
Eric Laurenta1884f92011-08-23 08:25:03 -0700603 status_t ret = initCheck();
604 if (ret != NO_ERROR) {
605 return ret;
606 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607
608 // check calling permissions
609 if (!settingsAllowed()) {
610 return PERMISSION_DENIED;
611 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800612 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000613 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700614 return BAD_VALUE;
615 }
616
617 { // scope for the lock
618 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700619 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700620 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700621 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622 mHardwareStatus = AUDIO_HW_IDLE;
623 }
624
625 if (NO_ERROR == ret) {
626 Mutex::Autolock _l(mLock);
627 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800628 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700629 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630 }
631
632 return ret;
633}
634
635status_t AudioFlinger::setMicMute(bool state)
636{
Eric Laurenta1884f92011-08-23 08:25:03 -0700637 status_t ret = initCheck();
638 if (ret != NO_ERROR) {
639 return ret;
640 }
641
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642 // check calling permissions
643 if (!settingsAllowed()) {
644 return PERMISSION_DENIED;
645 }
646
647 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700648 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700650 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651 mHardwareStatus = AUDIO_HW_IDLE;
652 return ret;
653}
654
655bool AudioFlinger::getMicMute() const
656{
Eric Laurenta1884f92011-08-23 08:25:03 -0700657 status_t ret = initCheck();
658 if (ret != NO_ERROR) {
659 return false;
660 }
661
Dima Zavinfce7a472011-04-19 22:30:36 -0700662 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800663 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700664 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700666 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667 mHardwareStatus = AUDIO_HW_IDLE;
668 return state;
669}
670
671status_t AudioFlinger::setMasterMute(bool muted)
672{
John Grossmand8f178d2012-07-20 14:51:35 -0700673 status_t ret = initCheck();
674 if (ret != NO_ERROR) {
675 return ret;
676 }
677
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 // check calling permissions
679 if (!settingsAllowed()) {
680 return PERMISSION_DENIED;
681 }
682
John Grossmanee578c02012-07-23 17:05:46 -0700683 Mutex::Autolock _l(mLock);
684 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700685
John Grossmanee578c02012-07-23 17:05:46 -0700686 // Set master mute in the HALs which support it.
687 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
688 AutoMutex lock(mHardwareLock);
689 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700690
John Grossmanee578c02012-07-23 17:05:46 -0700691 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
692 if (dev->canSetMasterMute()) {
693 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700694 }
John Grossmanee578c02012-07-23 17:05:46 -0700695 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700696 }
697
John Grossmanee578c02012-07-23 17:05:46 -0700698 // Now set the master mute in each playback thread. Playback threads
699 // assigned to HALs which do not have master mute support will apply master
700 // mute during the mix operation. Threads with HALs which do support master
701 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800702 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700703 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704
705 return NO_ERROR;
706}
707
708float AudioFlinger::masterVolume() const
709{
Glenn Kasten98067102011-12-13 11:47:54 -0800710 Mutex::Autolock _l(mLock);
711 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712}
713
714bool AudioFlinger::masterMute() const
715{
Glenn Kasten98067102011-12-13 11:47:54 -0800716 Mutex::Autolock _l(mLock);
717 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700718}
719
John Grossman4ff14ba2012-02-08 16:37:41 -0800720float AudioFlinger::masterVolume_l() const
721{
John Grossman4ff14ba2012-02-08 16:37:41 -0800722 return mMasterVolume;
723}
724
John Grossmand8f178d2012-07-20 14:51:35 -0700725bool AudioFlinger::masterMute_l() const
726{
John Grossmanee578c02012-07-23 17:05:46 -0700727 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700728}
729
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800730status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
731 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732{
733 // check calling permissions
734 if (!settingsAllowed()) {
735 return PERMISSION_DENIED;
736 }
737
Glenn Kasten263709e2012-01-06 08:40:01 -0800738 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000739 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740 return BAD_VALUE;
741 }
742
743 AutoMutex lock(mLock);
744 PlaybackThread *thread = NULL;
745 if (output) {
746 thread = checkPlaybackThread_l(output);
747 if (thread == NULL) {
748 return BAD_VALUE;
749 }
750 }
751
752 mStreamTypes[stream].volume = value;
753
754 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800755 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700756 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757 }
758 } else {
759 thread->setStreamVolume(stream, value);
760 }
761
762 return NO_ERROR;
763}
764
Glenn Kastenfff6d712012-01-12 16:38:12 -0800765status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766{
767 // check calling permissions
768 if (!settingsAllowed()) {
769 return PERMISSION_DENIED;
770 }
771
Glenn Kasten263709e2012-01-06 08:40:01 -0800772 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700773 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000774 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 return BAD_VALUE;
776 }
777
Eric Laurent93575202011-01-18 18:39:02 -0800778 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779 mStreamTypes[stream].mute = muted;
780 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700781 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782
783 return NO_ERROR;
784}
785
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800786float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700787{
Glenn Kasten263709e2012-01-06 08:40:01 -0800788 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789 return 0.0f;
790 }
791
792 AutoMutex lock(mLock);
793 float volume;
794 if (output) {
795 PlaybackThread *thread = checkPlaybackThread_l(output);
796 if (thread == NULL) {
797 return 0.0f;
798 }
799 volume = thread->streamVolume(stream);
800 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800801 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802 }
803
804 return volume;
805}
806
Glenn Kastenfff6d712012-01-12 16:38:12 -0800807bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808{
Glenn Kasten263709e2012-01-06 08:40:01 -0800809 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700810 return true;
811 }
812
Glenn Kasten6637baa2012-01-09 09:40:36 -0800813 AutoMutex lock(mLock);
814 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815}
816
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800817status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818{
Glenn Kasten411e4472012-11-02 10:00:06 -0700819 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
820 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurentca7cc822012-11-19 14:55:58 -0800821
Mathias Agopian65ab4712010-07-14 17:59:35 -0700822 // check calling permissions
823 if (!settingsAllowed()) {
824 return PERMISSION_DENIED;
825 }
826
Mathias Agopian65ab4712010-07-14 17:59:35 -0700827 // ioHandle == 0 means the parameters are global to the audio hardware interface
828 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700829 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700830 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800831 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700832 AutoMutex lock(mHardwareLock);
833 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
834 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
835 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
836 status_t result = dev->set_parameters(dev, keyValuePairs.string());
837 final_result = result ?: final_result;
838 }
839 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800840 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700841 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
842 AudioParameter param = AudioParameter(keyValuePairs);
843 String8 value;
844 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700845 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
846 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700847 for (size_t i = 0; i < mRecordThreads.size(); i++) {
848 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurent88959252012-08-28 14:26:53 -0700849 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700850 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
851 // collect all of the thread's session IDs
852 KeyedVector<int, bool> ids = thread->sessionIds();
853 // suspend effects associated with those session IDs
854 for (size_t j = 0; j < ids.size(); ++j) {
855 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700856 thread->setEffectSuspended(FX_IID_AEC,
857 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700858 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700859 thread->setEffectSuspended(FX_IID_NS,
860 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700861 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700862 }
863 }
Eric Laurentbee53372011-08-29 12:42:48 -0700864 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700865 }
866 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700867 String8 screenState;
868 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
869 bool isOff = screenState == "off";
Eric Laurentca7cc822012-11-19 14:55:58 -0800870 if (isOff != (AudioFlinger::mScreenState & 1)) {
871 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700872 }
873 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700874 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700875 }
876
877 // hold a strong ref on thread in case closeOutput() or closeInput() is called
878 // and the thread is exited once the lock is released
879 sp<ThreadBase> thread;
880 {
881 Mutex::Autolock _l(mLock);
882 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700883 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800885 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700886 // indicate output device change to all input threads for pre processing
887 AudioParameter param = AudioParameter(keyValuePairs);
888 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700889 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
890 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700891 for (size_t i = 0; i < mRecordThreads.size(); i++) {
892 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
893 }
894 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895 }
896 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800897 if (thread != 0) {
898 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899 }
900 return BAD_VALUE;
901}
902
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800903String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700904{
Glenn Kasten411e4472012-11-02 10:00:06 -0700905 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
906 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907
Eric Laurenta4c5a552012-03-29 10:12:40 -0700908 Mutex::Autolock _l(mLock);
909
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700911 String8 out_s8;
912
Dima Zavin799a70e2011-04-18 16:57:27 -0700913 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800914 char *s;
915 {
916 AutoMutex lock(mHardwareLock);
917 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700918 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800919 s = dev->get_parameters(dev, keys.string());
920 mHardwareStatus = AUDIO_HW_IDLE;
921 }
John Grossmanef7740b2012-02-09 11:28:36 -0800922 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700923 free(s);
924 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700925 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 }
927
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
929 if (playbackThread != NULL) {
930 return playbackThread->getParameters(keys);
931 }
932 RecordThread *recordThread = checkRecordThread_l(ioHandle);
933 if (recordThread != NULL) {
934 return recordThread->getParameters(keys);
935 }
936 return String8("");
937}
938
Glenn Kastendd8104c2012-07-02 12:42:44 -0700939size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
940 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941{
Eric Laurenta1884f92011-08-23 08:25:03 -0700942 status_t ret = initCheck();
943 if (ret != NO_ERROR) {
944 return 0;
945 }
946
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800947 AutoMutex lock(mHardwareLock);
948 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700949 struct audio_config config = {
950 sample_rate: sampleRate,
Glenn Kastendd8104c2012-07-02 12:42:44 -0700951 channel_mask: channelMask,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700952 format: format,
953 };
John Grossmanee578c02012-07-23 17:05:46 -0700954 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
955 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800956 mHardwareStatus = AUDIO_HW_IDLE;
957 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958}
959
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800960unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700962 Mutex::Autolock _l(mLock);
963
964 RecordThread *recordThread = checkRecordThread_l(ioHandle);
965 if (recordThread != NULL) {
966 return recordThread->getInputFramesLost();
967 }
968 return 0;
969}
970
971status_t AudioFlinger::setVoiceVolume(float value)
972{
Eric Laurenta1884f92011-08-23 08:25:03 -0700973 status_t ret = initCheck();
974 if (ret != NO_ERROR) {
975 return ret;
976 }
977
Mathias Agopian65ab4712010-07-14 17:59:35 -0700978 // check calling permissions
979 if (!settingsAllowed()) {
980 return PERMISSION_DENIED;
981 }
982
983 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700984 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800985 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -0700986 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700987 mHardwareStatus = AUDIO_HW_IDLE;
988
989 return ret;
990}
991
Glenn Kastenb59a5022012-11-16 12:01:44 -0800992status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800993 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700994{
995 status_t status;
996
997 Mutex::Autolock _l(mLock);
998
999 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1000 if (playbackThread != NULL) {
1001 return playbackThread->getRenderPosition(halFrames, dspFrames);
1002 }
1003
1004 return BAD_VALUE;
1005}
1006
1007void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1008{
1009
1010 Mutex::Autolock _l(mLock);
1011
Glenn Kastenbb001922012-02-03 11:10:26 -08001012 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001013 if (mNotificationClients.indexOfKey(pid) < 0) {
1014 sp<NotificationClient> notificationClient = new NotificationClient(this,
1015 client,
1016 pid);
Steve Block3856b092011-10-20 11:56:00 +01001017 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001018
1019 mNotificationClients.add(pid, notificationClient);
1020
1021 sp<IBinder> binder = client->asBinder();
1022 binder->linkToDeath(notificationClient);
1023
1024 // the config change is always sent from playback or record threads to avoid deadlock
1025 // with AudioSystem::gLock
1026 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentef6be0b2012-09-13 11:18:23 -07001027 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028 }
1029
1030 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentef6be0b2012-09-13 11:18:23 -07001031 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001032 }
1033 }
1034}
1035
1036void AudioFlinger::removeNotificationClient(pid_t pid)
1037{
1038 Mutex::Autolock _l(mLock);
1039
Glenn Kastena3b09252012-01-20 09:19:01 -08001040 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001041
Steve Block3856b092011-10-20 11:56:00 +01001042 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001043 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001044 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001045 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001046 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001047 ALOGV(" pid %d @ %d", ref->mPid, i);
1048 if (ref->mPid == pid) {
1049 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001050 mAudioSessionRefs.removeAt(i);
1051 delete ref;
1052 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001053 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001054 } else {
1055 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001056 }
1057 }
1058 if (removed) {
1059 purgeStaleEffects_l();
1060 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001061}
1062
1063// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001064void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065{
1066 size_t size = mNotificationClients.size();
1067 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001068 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1069 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001070 }
1071}
1072
1073// removeClient_l() must be called with AudioFlinger::mLock held
1074void AudioFlinger::removeClient_l(pid_t pid)
1075{
Glenn Kasten411e4472012-11-02 10:00:06 -07001076 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten8af901c2012-11-01 11:11:38 -07001077 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001078 mClients.removeItem(pid);
1079}
1080
Eric Laurent717e1282012-06-29 16:36:52 -07001081// getEffectThread_l() must be called with AudioFlinger::mLock held
1082sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1083{
1084 sp<PlaybackThread> thread;
1085
1086 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1087 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1088 ALOG_ASSERT(thread == 0);
1089 thread = mPlaybackThreads.valueAt(i);
1090 }
1091 }
1092
1093 return thread;
1094}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095
Mathias Agopian65ab4712010-07-14 17:59:35 -07001096
Mathias Agopian65ab4712010-07-14 17:59:35 -07001097
1098// ----------------------------------------------------------------------------
1099
1100AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1101 : RefBase(),
1102 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001103 // 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 -07001104 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001105 mPid(pid),
1106 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001107{
1108 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1109}
1110
1111// Client destructor must be called with AudioFlinger::mLock held
1112AudioFlinger::Client::~Client()
1113{
1114 mAudioFlinger->removeClient_l(mPid);
1115}
1116
Glenn Kasten435dbe62012-01-30 10:15:48 -08001117sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001118{
1119 return mMemoryDealer;
1120}
1121
John Grossman4ff14ba2012-02-08 16:37:41 -08001122// Reserve one of the limited slots for a timed audio track associated
1123// with this client
1124bool AudioFlinger::Client::reserveTimedTrack()
1125{
1126 const int kMaxTimedTracksPerClient = 4;
1127
1128 Mutex::Autolock _l(mTimedTrackLock);
1129
1130 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1131 ALOGW("can not create timed track - pid %d has exceeded the limit",
1132 mPid);
1133 return false;
1134 }
1135
1136 mTimedTrackCount++;
1137 return true;
1138}
1139
1140// Release a slot for a timed audio track
1141void AudioFlinger::Client::releaseTimedTrack()
1142{
1143 Mutex::Autolock _l(mTimedTrackLock);
1144 mTimedTrackCount--;
1145}
1146
Mathias Agopian65ab4712010-07-14 17:59:35 -07001147// ----------------------------------------------------------------------------
1148
1149AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1150 const sp<IAudioFlingerClient>& client,
1151 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001152 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001153{
1154}
1155
1156AudioFlinger::NotificationClient::~NotificationClient()
1157{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158}
1159
1160void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1161{
1162 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001163 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001164}
1165
Mathias Agopian65ab4712010-07-14 17:59:35 -07001166
1167// ----------------------------------------------------------------------------
1168
1169sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001170 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001171 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001172 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001173 audio_channel_mask_t channelMask,
Glenn Kasten7da35f22012-11-14 12:54:39 -08001174 size_t frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08001175 IAudioFlinger::track_flags_t flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001176 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001177 int *sessionId,
1178 status_t *status)
1179{
1180 sp<RecordThread::RecordTrack> recordTrack;
1181 sp<RecordHandle> recordHandle;
1182 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001183 status_t lStatus;
1184 RecordThread *thread;
1185 size_t inFrameCount;
1186 int lSessionId;
1187
1188 // check calling permissions
1189 if (!recordingAllowed()) {
1190 lStatus = PERMISSION_DENIED;
1191 goto Exit;
1192 }
1193
1194 // add client to list
1195 { // scope for mLock
1196 Mutex::Autolock _l(mLock);
1197 thread = checkRecordThread_l(input);
1198 if (thread == NULL) {
1199 lStatus = BAD_VALUE;
1200 goto Exit;
1201 }
1202
Glenn Kastenf37971f2012-02-03 11:06:53 -08001203 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001204 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001205
1206 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001207 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001208 lSessionId = *sessionId;
1209 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001210 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211 if (sessionId != NULL) {
1212 *sessionId = lSessionId;
1213 }
1214 }
Glenn Kasten8af901c2012-11-01 11:11:38 -07001215 // create new record track.
1216 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001217 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1218 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001220 if (lStatus != NO_ERROR) {
Glenn Kasten8af901c2012-11-01 11:11:38 -07001221 // remove local strong reference to Client before deleting the RecordTrack so that the
1222 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001223 client.clear();
1224 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001225 goto Exit;
1226 }
1227
1228 // return to handle to client
1229 recordHandle = new RecordHandle(recordTrack);
1230 lStatus = NO_ERROR;
1231
1232Exit:
1233 if (status) {
1234 *status = lStatus;
1235 }
1236 return recordHandle;
1237}
1238
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001239
1240
Mathias Agopian65ab4712010-07-14 17:59:35 -07001241// ----------------------------------------------------------------------------
1242
Eric Laurenta4c5a552012-03-29 10:12:40 -07001243audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1244{
1245 if (!settingsAllowed()) {
1246 return 0;
1247 }
1248 Mutex::Autolock _l(mLock);
1249 return loadHwModule_l(name);
1250}
1251
1252// loadHwModule_l() must be called with AudioFlinger::mLock held
1253audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1254{
1255 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1256 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1257 ALOGW("loadHwModule() module %s already loaded", name);
1258 return mAudioHwDevs.keyAt(i);
1259 }
1260 }
1261
Eric Laurenta4c5a552012-03-29 10:12:40 -07001262 audio_hw_device_t *dev;
1263
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001264 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001265 if (rc) {
1266 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1267 return 0;
1268 }
1269
1270 mHardwareStatus = AUDIO_HW_INIT;
1271 rc = dev->init_check(dev);
1272 mHardwareStatus = AUDIO_HW_IDLE;
1273 if (rc) {
1274 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1275 return 0;
1276 }
1277
John Grossmanee578c02012-07-23 17:05:46 -07001278 // Check and cache this HAL's level of support for master mute and master
1279 // volume. If this is the first HAL opened, and it supports the get
1280 // methods, use the initial values provided by the HAL as the current
1281 // master mute and volume settings.
1282
1283 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1284 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001285 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001286
1287 if (0 == mAudioHwDevs.size()) {
1288 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1289 if (NULL != dev->get_master_volume) {
1290 float mv;
1291 if (OK == dev->get_master_volume(dev, &mv)) {
1292 mMasterVolume = mv;
1293 }
1294 }
1295
1296 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1297 if (NULL != dev->get_master_mute) {
1298 bool mm;
1299 if (OK == dev->get_master_mute(dev, &mm)) {
1300 mMasterMute = mm;
1301 }
1302 }
1303 }
1304
Eric Laurenta4c5a552012-03-29 10:12:40 -07001305 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001306 if ((NULL != dev->set_master_volume) &&
1307 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1308 flags = static_cast<AudioHwDevice::Flags>(flags |
1309 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1310 }
1311
1312 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1313 if ((NULL != dev->set_master_mute) &&
1314 (OK == dev->set_master_mute(dev, mMasterMute))) {
1315 flags = static_cast<AudioHwDevice::Flags>(flags |
1316 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1317 }
1318
Eric Laurenta4c5a552012-03-29 10:12:40 -07001319 mHardwareStatus = AUDIO_HW_IDLE;
1320 }
1321
1322 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001323 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001324
1325 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001326 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001327
1328 return handle;
1329
1330}
1331
Glenn Kasten4c6db4c2012-09-24 11:27:18 -07001332// ----------------------------------------------------------------------------
1333
Glenn Kasten1127d652012-11-14 08:44:39 -08001334uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kasten4c6db4c2012-09-24 11:27:18 -07001335{
1336 Mutex::Autolock _l(mLock);
1337 PlaybackThread *thread = primaryPlaybackThread_l();
1338 return thread != NULL ? thread->sampleRate() : 0;
1339}
1340
Glenn Kasten7da35f22012-11-14 12:54:39 -08001341size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kasten4c6db4c2012-09-24 11:27:18 -07001342{
1343 Mutex::Autolock _l(mLock);
1344 PlaybackThread *thread = primaryPlaybackThread_l();
1345 return thread != NULL ? thread->frameCountHAL() : 0;
1346}
1347
1348// ----------------------------------------------------------------------------
1349
Eric Laurenta4c5a552012-03-29 10:12:40 -07001350audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1351 audio_devices_t *pDevices,
1352 uint32_t *pSamplingRate,
1353 audio_format_t *pFormat,
1354 audio_channel_mask_t *pChannelMask,
1355 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001356 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001357{
1358 status_t status;
1359 PlaybackThread *thread = NULL;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001360 struct audio_config config = {
1361 sample_rate: pSamplingRate ? *pSamplingRate : 0,
1362 channel_mask: pChannelMask ? *pChannelMask : 0,
1363 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT,
1364 };
1365 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001366 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001367
Eric Laurenta4c5a552012-03-29 10:12:40 -07001368 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
1369 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001370 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001371 config.sample_rate,
1372 config.format,
1373 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001374 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001375
1376 if (pDevices == NULL || *pDevices == 0) {
1377 return 0;
1378 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001379
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380 Mutex::Autolock _l(mLock);
1381
Eric Laurenta4c5a552012-03-29 10:12:40 -07001382 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001383 if (outHwDev == NULL)
1384 return 0;
1385
John Grossmanee578c02012-07-23 17:05:46 -07001386 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001387 audio_io_handle_t id = nextUniqueId();
1388
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001389 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001390
John Grossmanee578c02012-07-23 17:05:46 -07001391 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001392 id,
1393 *pDevices,
1394 (audio_output_flags_t)flags,
1395 &config,
1396 &outStream);
1397
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001398 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8af901c2012-11-01 11:11:38 -07001399 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, "
1400 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001401 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001402 config.sample_rate,
1403 config.format,
1404 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001405 status);
1406
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001407 if (status == NO_ERROR && outStream != NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001408 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001409
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001410 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001411 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1412 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001414 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001415 } else {
1416 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001417 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001418 }
1419 mPlaybackThreads.add(id, thread);
1420
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001421 if (pSamplingRate != NULL) *pSamplingRate = config.sample_rate;
1422 if (pFormat != NULL) *pFormat = config.format;
1423 if (pChannelMask != NULL) *pChannelMask = config.channel_mask;
Glenn Kastena0d68332012-01-27 16:47:15 -08001424 if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001425
1426 // notify client processes of the new output creation
1427 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001428
1429 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001430 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001431 ALOGI("Using module %d has the primary audio interface", module);
1432 mPrimaryHardwareDev = outHwDev;
1433
1434 AutoMutex lock(mHardwareLock);
1435 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001436 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001437 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001438 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439 return id;
1440 }
1441
1442 return 0;
1443}
1444
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001445audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1446 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447{
1448 Mutex::Autolock _l(mLock);
1449 MixerThread *thread1 = checkMixerThread_l(output1);
1450 MixerThread *thread2 = checkMixerThread_l(output2);
1451
1452 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten8af901c2012-11-01 11:11:38 -07001453 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1454 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001455 return 0;
1456 }
1457
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001458 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001459 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1460 thread->addOutputTrack(thread2);
1461 mPlaybackThreads.add(id, thread);
1462 // notify client processes of the new output creation
1463 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1464 return id;
1465}
1466
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001467status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001468{
Glenn Kastend96c5722012-04-25 13:44:49 -07001469 return closeOutput_nonvirtual(output);
1470}
1471
1472status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1473{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474 // keep strong reference on the playback thread so that
1475 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001476 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001477 {
1478 Mutex::Autolock _l(mLock);
1479 thread = checkPlaybackThread_l(output);
1480 if (thread == NULL) {
1481 return BAD_VALUE;
1482 }
1483
Steve Block3856b092011-10-20 11:56:00 +01001484 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001486 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001488 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten8af901c2012-11-01 11:11:38 -07001489 DuplicatingThread *dupThread =
1490 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001491 dupThread->removeOutputTrack((MixerThread *)thread.get());
1492 }
1493 }
1494 }
Glenn Kastena1117922012-01-26 10:53:32 -08001495 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001496 mPlaybackThreads.removeItem(output);
1497 }
1498 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001499 // The thread entity (active unit of execution) is no longer running here,
1500 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001501
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001502 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001503 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001504 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001505 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001506 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001507 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001508 }
1509 return NO_ERROR;
1510}
1511
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001512status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001513{
1514 Mutex::Autolock _l(mLock);
1515 PlaybackThread *thread = checkPlaybackThread_l(output);
1516
1517 if (thread == NULL) {
1518 return BAD_VALUE;
1519 }
1520
Steve Block3856b092011-10-20 11:56:00 +01001521 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522 thread->suspend();
1523
1524 return NO_ERROR;
1525}
1526
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001527status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001528{
1529 Mutex::Autolock _l(mLock);
1530 PlaybackThread *thread = checkPlaybackThread_l(output);
1531
1532 if (thread == NULL) {
1533 return BAD_VALUE;
1534 }
1535
Steve Block3856b092011-10-20 11:56:00 +01001536 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001537
1538 thread->restore();
1539
1540 return NO_ERROR;
1541}
1542
Eric Laurenta4c5a552012-03-29 10:12:40 -07001543audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1544 audio_devices_t *pDevices,
1545 uint32_t *pSamplingRate,
1546 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001547 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001548{
1549 status_t status;
1550 RecordThread *thread = NULL;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001551 struct audio_config config = {
1552 sample_rate: pSamplingRate ? *pSamplingRate : 0,
1553 channel_mask: pChannelMask ? *pChannelMask : 0,
1554 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT,
1555 };
1556 uint32_t reqSamplingRate = config.sample_rate;
1557 audio_format_t reqFormat = config.format;
1558 audio_channel_mask_t reqChannels = config.channel_mask;
1559 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001560 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001561
1562 if (pDevices == NULL || *pDevices == 0) {
1563 return 0;
1564 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001565
Mathias Agopian65ab4712010-07-14 17:59:35 -07001566 Mutex::Autolock _l(mLock);
1567
Eric Laurenta4c5a552012-03-29 10:12:40 -07001568 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001569 if (inHwDev == NULL)
1570 return 0;
1571
John Grossmanee578c02012-07-23 17:05:46 -07001572 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001573 audio_io_handle_t id = nextUniqueId();
1574
John Grossmanee578c02012-07-23 17:05:46 -07001575 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001576 &inStream);
Glenn Kasten8af901c2012-11-01 11:11:38 -07001577 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1578 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001579 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001580 config.sample_rate,
1581 config.format,
1582 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001583 status);
1584
Glenn Kasten8af901c2012-11-01 11:11:38 -07001585 // If the input could not be opened with the requested parameters and we can handle the
1586 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1587 // 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 -07001588 if (status == BAD_VALUE &&
1589 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1590 (config.sample_rate <= 2 * reqSamplingRate) &&
1591 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001592 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001593 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001594 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001595 }
1596
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001597 if (status == NO_ERROR && inStream != NULL) {
Glenn Kasten8c327342012-09-30 12:29:28 -07001598
1599 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1600 // or (re-)create if current Pipe is idle and does not match the new format
1601 sp<NBAIO_Sink> teeSink;
1602#ifdef TEE_SINK_INPUT_FRAMES
1603 enum {
1604 TEE_SINK_NO, // don't copy input
1605 TEE_SINK_NEW, // copy input using a new pipe
1606 TEE_SINK_OLD, // copy input using an existing pipe
1607 } kind;
1608 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1609 popcount(inStream->common.get_channels(&inStream->common)));
1610 if (format == Format_Invalid) {
1611 kind = TEE_SINK_NO;
1612 } else if (mRecordTeeSink == 0) {
1613 kind = TEE_SINK_NEW;
1614 } else if (mRecordTeeSink->getStrongCount() != 1) {
1615 kind = TEE_SINK_NO;
1616 } else if (format == mRecordTeeSink->format()) {
1617 kind = TEE_SINK_OLD;
1618 } else {
1619 kind = TEE_SINK_NEW;
1620 }
1621 switch (kind) {
1622 case TEE_SINK_NEW: {
1623 Pipe *pipe = new Pipe(TEE_SINK_INPUT_FRAMES, format);
1624 size_t numCounterOffers = 0;
1625 const NBAIO_Format offers[1] = {format};
1626 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1627 ALOG_ASSERT(index == 0);
1628 PipeReader *pipeReader = new PipeReader(*pipe);
1629 numCounterOffers = 0;
1630 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1631 ALOG_ASSERT(index == 0);
1632 mRecordTeeSink = pipe;
1633 mRecordTeeSource = pipeReader;
1634 teeSink = pipe;
1635 }
1636 break;
1637 case TEE_SINK_OLD:
1638 teeSink = mRecordTeeSink;
1639 break;
1640 case TEE_SINK_NO:
1641 default:
1642 break;
1643 }
1644#endif
Dima Zavin799a70e2011-04-18 16:57:27 -07001645 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1646
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001647 // Start record thread
1648 // RecorThread require both input and output device indication to forward to audio
1649 // pre processing modules
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001650 audio_devices_t device = (*pDevices) | primaryOutputDevice_l();
Glenn Kasten8c327342012-09-30 12:29:28 -07001651
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001652 thread = new RecordThread(this,
1653 input,
1654 reqSamplingRate,
1655 reqChannels,
1656 id,
Glenn Kasten8c327342012-09-30 12:29:28 -07001657 device, teeSink);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001658 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001659 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kastena0d68332012-01-27 16:47:15 -08001660 if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001661 if (pFormat != NULL) *pFormat = config.format;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001662 if (pChannelMask != NULL) *pChannelMask = reqChannels;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001663
Mathias Agopian65ab4712010-07-14 17:59:35 -07001664 // notify client processes of the new input creation
1665 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1666 return id;
1667 }
1668
1669 return 0;
1670}
1671
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001672status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001673{
Glenn Kastend96c5722012-04-25 13:44:49 -07001674 return closeInput_nonvirtual(input);
1675}
1676
1677status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1678{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001679 // keep strong reference on the record thread so that
1680 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001681 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001682 {
1683 Mutex::Autolock _l(mLock);
1684 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001685 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001686 return BAD_VALUE;
1687 }
1688
Steve Block3856b092011-10-20 11:56:00 +01001689 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001690 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001691 mRecordThreads.removeItem(input);
1692 }
1693 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001694 // The thread entity (active unit of execution) is no longer running here,
1695 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001696
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001697 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001698 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001699 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001700 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001701 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001702
1703 return NO_ERROR;
1704}
1705
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001706status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001707{
1708 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001709 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001710
1711 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1712 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001713 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001714 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001715
1716 return NO_ERROR;
1717}
1718
1719
1720int AudioFlinger::newAudioSessionId()
1721{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001722 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001723}
1724
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001725void AudioFlinger::acquireAudioSessionId(int audioSession)
1726{
1727 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001728 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001729 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001730 size_t num = mAudioSessionRefs.size();
1731 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001732 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001733 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1734 ref->mCnt++;
1735 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001736 return;
1737 }
1738 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001739 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1740 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001741}
1742
1743void AudioFlinger::releaseAudioSessionId(int audioSession)
1744{
1745 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001746 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001747 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001748 size_t num = mAudioSessionRefs.size();
1749 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001750 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001751 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1752 ref->mCnt--;
1753 ALOGV(" decremented refcount to %d", ref->mCnt);
1754 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001755 mAudioSessionRefs.removeAt(i);
1756 delete ref;
1757 purgeStaleEffects_l();
1758 }
1759 return;
1760 }
1761 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001762 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001763}
1764
1765void AudioFlinger::purgeStaleEffects_l() {
1766
Steve Block3856b092011-10-20 11:56:00 +01001767 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001768
1769 Vector< sp<EffectChain> > chains;
1770
1771 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1772 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1773 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1774 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001775 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1776 chains.push(ec);
1777 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001778 }
1779 }
1780 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1781 sp<RecordThread> t = mRecordThreads.valueAt(i);
1782 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1783 sp<EffectChain> ec = t->mEffectChains[j];
1784 chains.push(ec);
1785 }
1786 }
1787
1788 for (size_t i = 0; i < chains.size(); i++) {
1789 sp<EffectChain> ec = chains[i];
1790 int sessionid = ec->sessionId();
1791 sp<ThreadBase> t = ec->mThread.promote();
1792 if (t == 0) {
1793 continue;
1794 }
1795 size_t numsessionrefs = mAudioSessionRefs.size();
1796 bool found = false;
1797 for (size_t k = 0; k < numsessionrefs; k++) {
1798 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001799 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001800 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001801 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001802 found = true;
1803 break;
1804 }
1805 }
1806 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001807 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001808 // remove all effects from the chain
1809 while (ec->mEffects.size()) {
1810 sp<EffectModule> effect = ec->mEffects[0];
1811 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001812 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001813 if (effect->purgeHandles()) {
1814 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001815 }
1816 AudioSystem::unregisterEffect(effect->id());
1817 }
1818 }
1819 }
1820 return;
1821}
1822
Mathias Agopian65ab4712010-07-14 17:59:35 -07001823// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001824AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001825{
Glenn Kastena1117922012-01-26 10:53:32 -08001826 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001827}
1828
1829// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001830AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001831{
1832 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001833 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001834}
1835
1836// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001837AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001838{
Glenn Kastena1117922012-01-26 10:53:32 -08001839 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001840}
1841
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001842uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001843{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001844 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001845}
1846
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001847AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001848{
1849 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1850 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001851 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001852 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001853 return thread;
1854 }
1855 }
1856 return NULL;
1857}
1858
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001859audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001860{
1861 PlaybackThread *thread = primaryPlaybackThread_l();
1862
1863 if (thread == NULL) {
1864 return 0;
1865 }
1866
Eric Laurent88959252012-08-28 14:26:53 -07001867 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001868}
1869
Eric Laurenta011e352012-03-29 15:51:43 -07001870sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1871 int triggerSession,
1872 int listenerSession,
1873 sync_event_callback_t callBack,
1874 void *cookie)
1875{
1876 Mutex::Autolock _l(mLock);
1877
1878 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1879 status_t playStatus = NAME_NOT_FOUND;
1880 status_t recStatus = NAME_NOT_FOUND;
1881 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1882 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1883 if (playStatus == NO_ERROR) {
1884 return event;
1885 }
1886 }
1887 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1888 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1889 if (recStatus == NO_ERROR) {
1890 return event;
1891 }
1892 }
1893 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
1894 mPendingSyncEvents.add(event);
1895 } else {
1896 ALOGV("createSyncEvent() invalid event %d", event->type());
1897 event.clear();
1898 }
1899 return event;
1900}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001901
Mathias Agopian65ab4712010-07-14 17:59:35 -07001902// ----------------------------------------------------------------------------
1903// Effect management
1904// ----------------------------------------------------------------------------
1905
1906
Glenn Kastenf587ba52012-01-26 16:25:10 -08001907status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001908{
1909 Mutex::Autolock _l(mLock);
1910 return EffectQueryNumberEffects(numEffects);
1911}
1912
Glenn Kastenf587ba52012-01-26 16:25:10 -08001913status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001914{
1915 Mutex::Autolock _l(mLock);
1916 return EffectQueryEffect(index, descriptor);
1917}
1918
Glenn Kasten5e92a782012-01-30 07:40:52 -08001919status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08001920 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001921{
1922 Mutex::Autolock _l(mLock);
1923 return EffectGetDescriptor(pUuid, descriptor);
1924}
1925
1926
Glenn Kastenf37971f2012-02-03 11:06:53 -08001927sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928 effect_descriptor_t *pDesc,
1929 const sp<IEffectClient>& effectClient,
1930 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001931 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001932 int sessionId,
1933 status_t *status,
1934 int *id,
1935 int *enabled)
1936{
1937 status_t lStatus = NO_ERROR;
1938 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001940
Glenn Kastenf37971f2012-02-03 11:06:53 -08001941 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001942 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001943 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944
1945 if (pDesc == NULL) {
1946 lStatus = BAD_VALUE;
1947 goto Exit;
1948 }
1949
Eric Laurent84e9a102010-09-23 16:10:16 -07001950 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07001951 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001952 lStatus = PERMISSION_DENIED;
1953 goto Exit;
1954 }
1955
Dima Zavinfce7a472011-04-19 22:30:36 -07001956 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07001957 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08001958 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001959 lStatus = PERMISSION_DENIED;
1960 goto Exit;
1961 }
1962
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001963 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001964 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001965 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07001966 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07001967 lStatus = BAD_VALUE;
1968 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07001969 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001970 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001971 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07001972 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001973 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07001974 }
1975 }
1976
Mathias Agopian65ab4712010-07-14 17:59:35 -07001977 {
1978 Mutex::Autolock _l(mLock);
1979
Mathias Agopian65ab4712010-07-14 17:59:35 -07001980
1981 if (!EffectIsNullUuid(&pDesc->uuid)) {
1982 // if uuid is specified, request effect descriptor
1983 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
1984 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001985 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001986 goto Exit;
1987 }
1988 } else {
1989 // if uuid is not specified, look for an available implementation
1990 // of the required type in effect factory
1991 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001992 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001993 lStatus = BAD_VALUE;
1994 goto Exit;
1995 }
1996 uint32_t numEffects = 0;
1997 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001998 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07001999 bool found = false;
2000
2001 lStatus = EffectQueryNumberEffects(&numEffects);
2002 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002003 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002004 goto Exit;
2005 }
2006 for (uint32_t i = 0; i < numEffects; i++) {
2007 lStatus = EffectQueryEffect(i, &desc);
2008 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002009 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002010 continue;
2011 }
2012 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2013 // If matching type found save effect descriptor. If the session is
2014 // 0 and the effect is not auxiliary, continue enumeration in case
2015 // an auxiliary version of this effect type is available
2016 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002017 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002018 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002019 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2020 break;
2021 }
2022 }
2023 }
2024 if (!found) {
2025 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002026 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002027 goto Exit;
2028 }
2029 // For same effect type, chose auxiliary version over insert version if
2030 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002031 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002032 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002033 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002034 }
2035 }
2036
2037 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002038 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002039 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2040 lStatus = INVALID_OPERATION;
2041 goto Exit;
2042 }
2043
Eric Laurent59255e42011-07-27 19:49:51 -07002044 // check recording permission for visualizer
2045 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2046 !recordingAllowed()) {
2047 lStatus = PERMISSION_DENIED;
2048 goto Exit;
2049 }
2050
Mathias Agopian65ab4712010-07-14 17:59:35 -07002051 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002052 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002053
2054 // If output is not specified try to find a matching audio session ID in one of the
2055 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002056 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2057 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002058 // Note: io is never 0 when creating an effect on an input
2059 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002060 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002061 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2062 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002063 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002064 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002065 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002066 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002067 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002068 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2069 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2070 io = mRecordThreads.keyAt(i);
2071 break;
2072 }
2073 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002074 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002075 // If no output thread contains the requested session ID, default to
2076 // first output. The effect chain will be moved to the correct output
2077 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002078 if (io == 0 && mPlaybackThreads.size()) {
2079 io = mPlaybackThreads.keyAt(0);
2080 }
Steve Block3856b092011-10-20 11:56:00 +01002081 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002082 }
2083 ThreadBase *thread = checkRecordThread_l(io);
2084 if (thread == NULL) {
2085 thread = checkPlaybackThread_l(io);
2086 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002087 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002088 lStatus = BAD_VALUE;
2089 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002090 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002091 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002092
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002093 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002094
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002095 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002096 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2097 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002098 if (handle != 0 && id != NULL) {
2099 *id = handle->id();
2100 }
2101 }
2102
2103Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002104 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002105 *status = lStatus;
2106 }
2107 return handle;
2108}
2109
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002110status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2111 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002112{
Steve Block3856b092011-10-20 11:56:00 +01002113 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002114 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002115 Mutex::Autolock _l(mLock);
2116 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002117 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002118 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002119 }
Eric Laurentde070132010-07-13 04:45:46 -07002120 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2121 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002122 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002123 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002124 }
Eric Laurentde070132010-07-13 04:45:46 -07002125 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2126 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002127 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002128 return BAD_VALUE;
2129 }
2130
2131 Mutex::Autolock _dl(dstThread->mLock);
2132 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002133 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002134
Mathias Agopian65ab4712010-07-14 17:59:35 -07002135 return NO_ERROR;
2136}
2137
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002138// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002139status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002140 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002141 AudioFlinger::PlaybackThread *dstThread,
2142 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002143{
Steve Block3856b092011-10-20 11:56:00 +01002144 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002145 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002146
Eric Laurent59255e42011-07-27 19:49:51 -07002147 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002148 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002149 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002150 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002151 return INVALID_OPERATION;
2152 }
2153
Eric Laurent39e94f82010-07-28 01:32:47 -07002154 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002155 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002156 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002157 // removed.
2158 srcThread->removeEffectChain_l(chain);
2159
2160 // transfer all effects one by one so that new effect chain is created on new thread with
2161 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002162 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002163 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002164 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002165 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2166 while (effect != 0) {
2167 srcThread->removeEffect_l(effect);
2168 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002169 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2170 if (effect->state() == EffectModule::ACTIVE ||
2171 effect->state() == EffectModule::STOPPING) {
2172 effect->start();
2173 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002174 // if the move request is not received from audio policy manager, the effect must be
2175 // re-registered with the new strategy and output
2176 if (dstChain == 0) {
2177 dstChain = effect->chain().promote();
2178 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002179 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002180 srcThread->addEffect_l(effect);
2181 return NO_INIT;
2182 }
2183 strategy = dstChain->strategy();
2184 }
2185 if (reRegister) {
2186 AudioSystem::unregisterEffect(effect->id());
2187 AudioSystem::registerEffect(&effect->desc(),
2188 dstOutput,
2189 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002190 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002191 effect->id());
2192 }
Eric Laurentde070132010-07-13 04:45:46 -07002193 effect = chain->getEffectFromId_l(0);
2194 }
2195
2196 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002197}
2198
Eric Laurentca7cc822012-11-19 14:55:58 -08002199void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002200{
Eric Laurentca7cc822012-11-19 14:55:58 -08002201 NBAIO_Source *teeSource = source.get();
2202 if (teeSource != NULL) {
2203 char teeTime[16];
2204 struct timeval tv;
2205 gettimeofday(&tv, NULL);
2206 struct tm tm;
2207 localtime_r(&tv.tv_sec, &tm);
2208 strftime(teeTime, sizeof(teeTime), "%T", &tm);
2209 char teePath[64];
2210 sprintf(teePath, "/data/misc/media/%s_%d.wav", teeTime, id);
2211 int teeFd = open(teePath, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
2212 if (teeFd >= 0) {
2213 char wavHeader[44];
2214 memcpy(wavHeader,
2215 "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",
2216 sizeof(wavHeader));
2217 NBAIO_Format format = teeSource->format();
2218 unsigned channelCount = Format_channelCount(format);
2219 ALOG_ASSERT(channelCount <= FCC_2);
2220 uint32_t sampleRate = Format_sampleRate(format);
2221 wavHeader[22] = channelCount; // number of channels
2222 wavHeader[24] = sampleRate; // sample rate
2223 wavHeader[25] = sampleRate >> 8;
2224 wavHeader[32] = channelCount * 2; // block alignment
2225 write(teeFd, wavHeader, sizeof(wavHeader));
2226 size_t total = 0;
2227 bool firstRead = true;
2228 for (;;) {
2229#define TEE_SINK_READ 1024
2230 short buffer[TEE_SINK_READ * FCC_2];
2231 size_t count = TEE_SINK_READ;
2232 ssize_t actual = teeSource->read(buffer, count,
2233 AudioBufferProvider::kInvalidPTS);
2234 bool wasFirstRead = firstRead;
2235 firstRead = false;
2236 if (actual <= 0) {
2237 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2238 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002239 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002240 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002241 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002242 ALOG_ASSERT(actual <= (ssize_t)count);
2243 write(teeFd, buffer, actual * channelCount * sizeof(short));
2244 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002245 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002246 lseek(teeFd, (off_t) 4, SEEK_SET);
2247 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2248 write(teeFd, &temp, sizeof(temp));
2249 lseek(teeFd, (off_t) 40, SEEK_SET);
2250 temp = total * channelCount * sizeof(short);
2251 write(teeFd, &temp, sizeof(temp));
2252 close(teeFd);
2253 fdprintf(fd, "FastMixer tee copied to %s\n", teePath);
Eric Laurent59255e42011-07-27 19:49:51 -07002254 } else {
Eric Laurentca7cc822012-11-19 14:55:58 -08002255 fdprintf(fd, "FastMixer unable to create tee %s: \n", strerror(errno));
Eric Laurent59255e42011-07-27 19:49:51 -07002256 }
2257 }
2258}
2259
Mathias Agopian65ab4712010-07-14 17:59:35 -07002260// ----------------------------------------------------------------------------
2261
2262status_t AudioFlinger::onTransact(
2263 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2264{
2265 return BnAudioFlinger::onTransact(code, data, reply, flags);
2266}
2267
Mathias Agopian65ab4712010-07-14 17:59:35 -07002268}; // namespace android