blob: 6a3007b699d040855540e492604b7c22c550e4cc [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080023#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <math.h>
25#include <signal.h>
26#include <sys/time.h>
27#include <sys/resource.h>
28
Gloria Wang9ee159b2011-02-24 14:51:45 -080029#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <binder/IServiceManager.h>
31#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070032#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070036#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
Dima Zavinfce7a472011-04-19 22:30:36 -070038#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039#include <cutils/properties.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080040#include <cutils/compiler.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070041
Dima Zavin64760242011-05-11 14:15:23 -070042#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070043#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
45#include "AudioMixer.h"
46#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080047#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
Mathias Agopian65ab4712010-07-14 17:59:35 -070049#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070050#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070051#include <audio_effects/effect_ns.h>
52#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Glenn Kasten3b21c502011-12-15 09:52:39 -080054#include <audio_utils/primitives.h>
55
Eric Laurentfeb0db62011-07-22 09:04:31 -070056#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080057
John Grossman4ff14ba2012-02-08 16:37:41 -080058#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070059
Glenn Kasten9e58b552013-01-18 15:09:48 -080060#include <media/IMediaLogService.h>
61
Glenn Kastenda6ef132013-01-10 12:31:01 -080062#include <media/nbaio/Pipe.h>
63#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070064#include <media/AudioParameter.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080065
Mathias Agopian65ab4712010-07-14 17:59:35 -070066// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
John Grossman1c345192012-03-27 14:00:17 -070068// Note: the following macro is used for extremely verbose logging message. In
69// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
70// 0; but one side effect of this is to turn all LOGV's as well. Some messages
71// are so verbose that we want to suppress them even when we have ALOG_ASSERT
72// turned on. Do not uncomment the #def below unless you really know what you
73// are doing and want to see all of the extremely verbose messages.
74//#define VERY_VERY_VERBOSE_LOGGING
75#ifdef VERY_VERY_VERBOSE_LOGGING
76#define ALOGVV ALOGV
77#else
78#define ALOGVV(a...) do { } while(0)
79#endif
Eric Laurentde070132010-07-13 04:45:46 -070080
Mathias Agopian65ab4712010-07-14 17:59:35 -070081namespace android {
82
Glenn Kastenec1d6b52011-12-12 09:04:45 -080083static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
84static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070085
Glenn Kasten58912562012-04-03 10:45:00 -070086
John Grossman4ff14ba2012-02-08 16:37:41 -080087nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080088
Eric Laurent81784c32012-11-19 14:55:58 -080089uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070090
Glenn Kasten46909e72013-02-26 09:20:22 -080091#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080092bool AudioFlinger::mTeeSinkInputEnabled = false;
93bool AudioFlinger::mTeeSinkOutputEnabled = false;
94bool AudioFlinger::mTeeSinkTrackEnabled = false;
95
96size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
97size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
98size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -080099#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800100
Mathias Agopian65ab4712010-07-14 17:59:35 -0700101// ----------------------------------------------------------------------------
102
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700103static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700104{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700105 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700106 int rc;
107
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700108 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
109 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
110 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
111 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700112 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700113 }
114 rc = audio_hw_device_open(mod, dev);
115 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
116 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
117 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700118 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700119 }
120 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
121 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
122 rc = BAD_VALUE;
123 goto out;
124 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700125 return 0;
126
127out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700128 *dev = NULL;
129 return rc;
130}
131
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132// ----------------------------------------------------------------------------
133
134AudioFlinger::AudioFlinger()
135 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800136 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700137 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800138 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800139 mMasterMute(false),
140 mNextUniqueId(1),
141 mMode(AUDIO_MODE_INVALID),
142 mBtNrecIsOff(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143{
Glenn Kasten949a9262013-04-16 12:35:20 -0700144 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800145 char value[PROPERTY_VALUE_MAX];
146 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
147 if (doLog) {
148 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
149 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800150#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800151 (void) property_get("ro.debuggable", value, "0");
152 int debuggable = atoi(value);
153 int teeEnabled = 0;
154 if (debuggable) {
155 (void) property_get("af.tee", value, "0");
156 teeEnabled = atoi(value);
157 }
158 if (teeEnabled & 1)
159 mTeeSinkInputEnabled = true;
160 if (teeEnabled & 2)
161 mTeeSinkOutputEnabled = true;
162 if (teeEnabled & 4)
163 mTeeSinkTrackEnabled = true;
Glenn Kasten46909e72013-02-26 09:20:22 -0800164#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700165}
166
167void AudioFlinger::onFirstRef()
168{
Dima Zavin799a70e2011-04-18 16:57:27 -0700169 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700170
Eric Laurent93575202011-01-18 18:39:02 -0800171 Mutex::Autolock _l(mLock);
172
Dima Zavin799a70e2011-04-18 16:57:27 -0700173 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800174 char val_str[PROPERTY_VALUE_MAX] = { 0 };
175 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
176 uint32_t int_val;
177 if (1 == sscanf(val_str, "%u", &int_val)) {
178 mStandbyTimeInNsecs = milliseconds(int_val);
179 ALOGI("Using %u mSec as standby time.", int_val);
180 } else {
181 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
182 ALOGI("Using default %u mSec as standby time.",
183 (uint32_t)(mStandbyTimeInNsecs / 1000000));
184 }
185 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186
Eric Laurenta4c5a552012-03-29 10:12:40 -0700187 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700188}
189
190AudioFlinger::~AudioFlinger()
191{
192 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700193 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700194 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195 }
196 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700197 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700198 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700199 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700200
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800201 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
202 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700203 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
204 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 }
206}
207
Eric Laurenta4c5a552012-03-29 10:12:40 -0700208static const char * const audio_interfaces[] = {
209 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
210 AUDIO_HARDWARE_MODULE_ID_A2DP,
211 AUDIO_HARDWARE_MODULE_ID_USB,
212};
213#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
214
John Grossmanee578c02012-07-23 17:05:46 -0700215AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
216 audio_module_handle_t module,
217 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700218{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700219 // if module is 0, the request comes from an old policy manager and we should load
220 // well known modules
221 if (module == 0) {
222 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
223 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
224 loadHwModule_l(audio_interfaces[i]);
225 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700226 // then try to find a module supporting the requested device.
227 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
228 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
229 audio_hw_device_t *dev = audioHwDevice->hwDevice();
230 if ((dev->get_supported_devices != NULL) &&
231 (dev->get_supported_devices(dev) & devices) == devices)
232 return audioHwDevice;
233 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700234 } else {
235 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700236 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
237 if (audioHwDevice != NULL) {
238 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700239 }
240 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700241
Dima Zavin799a70e2011-04-18 16:57:27 -0700242 return NULL;
243}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700245void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246{
247 const size_t SIZE = 256;
248 char buffer[SIZE];
249 String8 result;
250
251 result.append("Clients:\n");
252 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800253 sp<Client> client = mClients.valueAt(i).promote();
254 if (client != 0) {
255 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
256 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700257 }
258 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700259
260 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800261 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700262 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
263 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800264 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700265 result.append(buffer);
266 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268}
269
270
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700271void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700272{
273 const size_t SIZE = 256;
274 char buffer[SIZE];
275 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800276 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277
John Grossman4ff14ba2012-02-08 16:37:41 -0800278 snprintf(buffer, SIZE, "Hardware status: %d\n"
279 "Standby Time mSec: %u\n",
280 hardwareStatus,
281 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282 result.append(buffer);
283 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284}
285
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700286void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287{
288 const size_t SIZE = 256;
289 char buffer[SIZE];
290 String8 result;
291 snprintf(buffer, SIZE, "Permission Denial: "
292 "can't dump AudioFlinger from pid=%d, uid=%d\n",
293 IPCThreadState::self()->getCallingPid(),
294 IPCThreadState::self()->getCallingUid());
295 result.append(buffer);
296 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700297}
298
Eric Laurent81784c32012-11-19 14:55:58 -0800299bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300{
301 bool locked = false;
302 for (int i = 0; i < kDumpLockRetries; ++i) {
303 if (mutex.tryLock() == NO_ERROR) {
304 locked = true;
305 break;
306 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800307 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700308 }
309 return locked;
310}
311
312status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
313{
Glenn Kasten44deb052012-02-05 18:09:08 -0800314 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315 dumpPermissionDenial(fd, args);
316 } else {
317 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800318 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 if (!hardwareLocked) {
320 String8 result(kHardwareLockedString);
321 write(fd, result.string(), result.size());
322 } else {
323 mHardwareLock.unlock();
324 }
325
Eric Laurent81784c32012-11-19 14:55:58 -0800326 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327
328 // failed to lock - AudioFlinger is probably deadlocked
329 if (!locked) {
330 String8 result(kDeadlockedString);
331 write(fd, result.string(), result.size());
332 }
333
334 dumpClients(fd, args);
335 dumpInternals(fd, args);
336
337 // dump playback threads
338 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
339 mPlaybackThreads.valueAt(i)->dump(fd, args);
340 }
341
342 // dump record threads
343 for (size_t i = 0; i < mRecordThreads.size(); i++) {
344 mRecordThreads.valueAt(i)->dump(fd, args);
345 }
346
Dima Zavin799a70e2011-04-18 16:57:27 -0700347 // dump all hardware devs
348 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700349 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700350 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700352
Glenn Kasten46909e72013-02-26 09:20:22 -0800353#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700354 // dump the serially shared record tee sink
355 if (mRecordTeeSource != 0) {
356 dumpTee(fd, mRecordTeeSource);
357 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800358#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700359
Glenn Kastend65d73c2012-06-22 17:21:07 -0700360 if (locked) {
361 mLock.unlock();
362 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800363
364 // append a copy of media.log here by forwarding fd to it, but don't attempt
365 // to lookup the service if it's not running, as it will block for a second
366 if (mLogMemoryDealer != 0) {
367 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
368 if (binder != 0) {
369 fdprintf(fd, "\nmedia.log:\n");
370 Vector<String16> args;
371 binder->dump(fd, args);
372 }
373 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374 }
375 return NO_ERROR;
376}
377
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800378sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
379{
380 // If pid is already in the mClients wp<> map, then use that entry
381 // (for which promote() is always != 0), otherwise create a new entry and Client.
382 sp<Client> client = mClients.valueFor(pid).promote();
383 if (client == 0) {
384 client = new Client(this, pid);
385 mClients.add(pid, client);
386 }
387
388 return client;
389}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390
Glenn Kasten9e58b552013-01-18 15:09:48 -0800391sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
392{
393 if (mLogMemoryDealer == 0) {
394 return new NBLog::Writer();
395 }
396 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
397 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
398 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
399 if (binder != 0) {
400 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
401 }
402 return writer;
403}
404
405void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
406{
Glenn Kasten685ef092013-02-04 08:15:34 -0800407 if (writer == 0) {
408 return;
409 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800410 sp<IMemory> iMemory(writer->getIMemory());
411 if (iMemory == 0) {
412 return;
413 }
414 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
415 if (binder != 0) {
416 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
417 // Now the media.log remote reference to IMemory is gone.
418 // When our last local reference to IMemory also drops to zero,
419 // the IMemory destructor will deallocate the region from mMemoryDealer.
420 }
421}
422
Mathias Agopian65ab4712010-07-14 17:59:35 -0700423// IAudioFlinger interface
424
425
426sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800427 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800429 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700430 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800431 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800432 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700433 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800434 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800435 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436 int *sessionId,
437 status_t *status)
438{
439 sp<PlaybackThread::Track> track;
440 sp<TrackHandle> trackHandle;
441 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442 status_t lStatus;
443 int lSessionId;
444
Glenn Kasten263709e2012-01-06 08:40:01 -0800445 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
446 // but if someone uses binder directly they could bypass that and cause us to crash
447 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000448 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700449 lStatus = BAD_VALUE;
450 goto Exit;
451 }
452
Glenn Kasten60a83922012-06-21 12:56:37 -0700453 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
454 // and we don't yet support 8.24 or 32-bit PCM
455 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
456 ALOGE("createTrack() invalid format %d", format);
457 lStatus = BAD_VALUE;
458 goto Exit;
459 }
460
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461 {
462 Mutex::Autolock _l(mLock);
463 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700464 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700465 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800466 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700467 lStatus = BAD_VALUE;
468 goto Exit;
469 }
470
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800471 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800472 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700473
Steve Block3856b092011-10-20 11:56:00 +0100474 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700475 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700476 // check if an effect chain with the same session ID is present on another
477 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700478 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700479 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
480 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700481 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700482 if (sessions & PlaybackThread::EFFECT_SESSION) {
483 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700484 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700485 }
Eric Laurentde070132010-07-13 04:45:46 -0700486 }
487 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700488 lSessionId = *sessionId;
489 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700490 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700491 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700492 if (sessionId != NULL) {
493 *sessionId = lSessionId;
494 }
495 }
Steve Block3856b092011-10-20 11:56:00 +0100496 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700497
498 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800499 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700500
501 // move effect chain to this output thread if an effect on same session was waiting
502 // for a track to be created
503 if (lStatus == NO_ERROR && effectThread != NULL) {
504 Mutex::Autolock _dl(thread->mLock);
505 Mutex::Autolock _sl(effectThread->mLock);
506 moveEffectChain_l(lSessionId, effectThread, thread, true);
507 }
Eric Laurenta011e352012-03-29 15:51:43 -0700508
509 // Look for sync events awaiting for a session to be used.
510 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
511 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
512 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700513 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700514 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700515 } else {
516 mPendingSyncEvents[i]->cancel();
517 }
Eric Laurenta011e352012-03-29 15:51:43 -0700518 mPendingSyncEvents.removeAt(i);
519 i--;
520 }
521 }
522 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700523 }
524 if (lStatus == NO_ERROR) {
525 trackHandle = new TrackHandle(track);
526 } else {
527 // remove local strong reference to Client before deleting the Track so that the Client
528 // destructor is called by the TrackBase destructor with mLock held
529 client.clear();
530 track.clear();
531 }
532
533Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700534 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700535 *status = lStatus;
536 }
537 return trackHandle;
538}
539
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800540uint32_t AudioFlinger::sampleRate(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("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 return 0;
547 }
548 return thread->sampleRate();
549}
550
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800551int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552{
553 Mutex::Autolock _l(mLock);
554 PlaybackThread *thread = checkPlaybackThread_l(output);
555 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000556 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557 return 0;
558 }
559 return thread->channelCount();
560}
561
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800562audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563{
564 Mutex::Autolock _l(mLock);
565 PlaybackThread *thread = checkPlaybackThread_l(output);
566 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000567 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800568 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569 }
570 return thread->format();
571}
572
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800573size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574{
575 Mutex::Autolock _l(mLock);
576 PlaybackThread *thread = checkPlaybackThread_l(output);
577 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000578 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700579 return 0;
580 }
Glenn Kasten58912562012-04-03 10:45:00 -0700581 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
582 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583 return thread->frameCount();
584}
585
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800586uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587{
588 Mutex::Autolock _l(mLock);
589 PlaybackThread *thread = checkPlaybackThread_l(output);
590 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800591 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592 return 0;
593 }
594 return thread->latency();
595}
596
597status_t AudioFlinger::setMasterVolume(float value)
598{
Eric Laurenta1884f92011-08-23 08:25:03 -0700599 status_t ret = initCheck();
600 if (ret != NO_ERROR) {
601 return ret;
602 }
603
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 // check calling permissions
605 if (!settingsAllowed()) {
606 return PERMISSION_DENIED;
607 }
608
Eric Laurenta4c5a552012-03-29 10:12:40 -0700609 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700610 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700611
John Grossmanee578c02012-07-23 17:05:46 -0700612 // Set master volume in the HALs which support it.
613 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
614 AutoMutex lock(mHardwareLock);
615 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800616
John Grossmanee578c02012-07-23 17:05:46 -0700617 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
618 if (dev->canSetMasterVolume()) {
619 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800620 }
John Grossmanee578c02012-07-23 17:05:46 -0700621 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623
John Grossmanee578c02012-07-23 17:05:46 -0700624 // Now set the master volume in each playback thread. Playback threads
625 // assigned to HALs which do not have master volume support will apply
626 // master volume during the mix operation. Threads with HALs which do
627 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800628 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700629 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630
631 return NO_ERROR;
632}
633
Glenn Kastenf78aee72012-01-04 11:00:47 -0800634status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635{
Eric Laurenta1884f92011-08-23 08:25:03 -0700636 status_t ret = initCheck();
637 if (ret != NO_ERROR) {
638 return ret;
639 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640
641 // check calling permissions
642 if (!settingsAllowed()) {
643 return PERMISSION_DENIED;
644 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800645 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000646 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647 return BAD_VALUE;
648 }
649
650 { // scope for the lock
651 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700652 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700654 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655 mHardwareStatus = AUDIO_HW_IDLE;
656 }
657
658 if (NO_ERROR == ret) {
659 Mutex::Autolock _l(mLock);
660 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800661 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700662 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663 }
664
665 return ret;
666}
667
668status_t AudioFlinger::setMicMute(bool state)
669{
Eric Laurenta1884f92011-08-23 08:25:03 -0700670 status_t ret = initCheck();
671 if (ret != NO_ERROR) {
672 return ret;
673 }
674
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675 // check calling permissions
676 if (!settingsAllowed()) {
677 return PERMISSION_DENIED;
678 }
679
680 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700681 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700683 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684 mHardwareStatus = AUDIO_HW_IDLE;
685 return ret;
686}
687
688bool AudioFlinger::getMicMute() const
689{
Eric Laurenta1884f92011-08-23 08:25:03 -0700690 status_t ret = initCheck();
691 if (ret != NO_ERROR) {
692 return false;
693 }
694
Dima Zavinfce7a472011-04-19 22:30:36 -0700695 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800696 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700697 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700699 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700 mHardwareStatus = AUDIO_HW_IDLE;
701 return state;
702}
703
704status_t AudioFlinger::setMasterMute(bool muted)
705{
John Grossmand8f178d2012-07-20 14:51:35 -0700706 status_t ret = initCheck();
707 if (ret != NO_ERROR) {
708 return ret;
709 }
710
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 // check calling permissions
712 if (!settingsAllowed()) {
713 return PERMISSION_DENIED;
714 }
715
John Grossmanee578c02012-07-23 17:05:46 -0700716 Mutex::Autolock _l(mLock);
717 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700718
John Grossmanee578c02012-07-23 17:05:46 -0700719 // Set master mute in the HALs which support it.
720 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
721 AutoMutex lock(mHardwareLock);
722 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700723
John Grossmanee578c02012-07-23 17:05:46 -0700724 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
725 if (dev->canSetMasterMute()) {
726 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700727 }
John Grossmanee578c02012-07-23 17:05:46 -0700728 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700729 }
730
John Grossmanee578c02012-07-23 17:05:46 -0700731 // Now set the master mute in each playback thread. Playback threads
732 // assigned to HALs which do not have master mute support will apply master
733 // mute during the mix operation. Threads with HALs which do support master
734 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800735 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700736 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700737
738 return NO_ERROR;
739}
740
741float AudioFlinger::masterVolume() const
742{
Glenn Kasten98067102011-12-13 11:47:54 -0800743 Mutex::Autolock _l(mLock);
744 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700745}
746
747bool AudioFlinger::masterMute() const
748{
Glenn Kasten98067102011-12-13 11:47:54 -0800749 Mutex::Autolock _l(mLock);
750 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751}
752
John Grossman4ff14ba2012-02-08 16:37:41 -0800753float AudioFlinger::masterVolume_l() const
754{
John Grossman4ff14ba2012-02-08 16:37:41 -0800755 return mMasterVolume;
756}
757
John Grossmand8f178d2012-07-20 14:51:35 -0700758bool AudioFlinger::masterMute_l() const
759{
John Grossmanee578c02012-07-23 17:05:46 -0700760 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700761}
762
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800763status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
764 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765{
766 // check calling permissions
767 if (!settingsAllowed()) {
768 return PERMISSION_DENIED;
769 }
770
Glenn Kasten263709e2012-01-06 08:40:01 -0800771 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000772 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773 return BAD_VALUE;
774 }
775
776 AutoMutex lock(mLock);
777 PlaybackThread *thread = NULL;
778 if (output) {
779 thread = checkPlaybackThread_l(output);
780 if (thread == NULL) {
781 return BAD_VALUE;
782 }
783 }
784
785 mStreamTypes[stream].volume = value;
786
787 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800788 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700789 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700790 }
791 } else {
792 thread->setStreamVolume(stream, value);
793 }
794
795 return NO_ERROR;
796}
797
Glenn Kastenfff6d712012-01-12 16:38:12 -0800798status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700799{
800 // check calling permissions
801 if (!settingsAllowed()) {
802 return PERMISSION_DENIED;
803 }
804
Glenn Kasten263709e2012-01-06 08:40:01 -0800805 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700806 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000807 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808 return BAD_VALUE;
809 }
810
Eric Laurent93575202011-01-18 18:39:02 -0800811 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 mStreamTypes[stream].mute = muted;
813 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700814 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815
816 return NO_ERROR;
817}
818
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800819float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820{
Glenn Kasten263709e2012-01-06 08:40:01 -0800821 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700822 return 0.0f;
823 }
824
825 AutoMutex lock(mLock);
826 float volume;
827 if (output) {
828 PlaybackThread *thread = checkPlaybackThread_l(output);
829 if (thread == NULL) {
830 return 0.0f;
831 }
832 volume = thread->streamVolume(stream);
833 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800834 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835 }
836
837 return volume;
838}
839
Glenn Kastenfff6d712012-01-12 16:38:12 -0800840bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841{
Glenn Kasten263709e2012-01-06 08:40:01 -0800842 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843 return true;
844 }
845
Glenn Kasten6637baa2012-01-09 09:40:36 -0800846 AutoMutex lock(mLock);
847 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848}
849
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800850status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700852 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
853 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800854
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 // check calling permissions
856 if (!settingsAllowed()) {
857 return PERMISSION_DENIED;
858 }
859
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860 // ioHandle == 0 means the parameters are global to the audio hardware interface
861 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700862 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700863 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800864 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700865 AutoMutex lock(mHardwareLock);
866 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
867 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
868 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
869 status_t result = dev->set_parameters(dev, keyValuePairs.string());
870 final_result = result ?: final_result;
871 }
872 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800873 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700874 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
875 AudioParameter param = AudioParameter(keyValuePairs);
876 String8 value;
877 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700878 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
879 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700880 for (size_t i = 0; i < mRecordThreads.size(); i++) {
881 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700882 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700883 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
884 // collect all of the thread's session IDs
885 KeyedVector<int, bool> ids = thread->sessionIds();
886 // suspend effects associated with those session IDs
887 for (size_t j = 0; j < ids.size(); ++j) {
888 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700889 thread->setEffectSuspended(FX_IID_AEC,
890 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700891 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700892 thread->setEffectSuspended(FX_IID_NS,
893 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700894 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700895 }
896 }
Eric Laurentbee53372011-08-29 12:42:48 -0700897 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700898 }
899 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700900 String8 screenState;
901 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
902 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800903 if (isOff != (AudioFlinger::mScreenState & 1)) {
904 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700905 }
906 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700907 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700908 }
909
910 // hold a strong ref on thread in case closeOutput() or closeInput() is called
911 // and the thread is exited once the lock is released
912 sp<ThreadBase> thread;
913 {
914 Mutex::Autolock _l(mLock);
915 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700916 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800918 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700919 // indicate output device change to all input threads for pre processing
920 AudioParameter param = AudioParameter(keyValuePairs);
921 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700922 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
923 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700924 for (size_t i = 0; i < mRecordThreads.size(); i++) {
925 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
926 }
927 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 }
929 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800930 if (thread != 0) {
931 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932 }
933 return BAD_VALUE;
934}
935
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800936String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700938 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
939 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940
Eric Laurenta4c5a552012-03-29 10:12:40 -0700941 Mutex::Autolock _l(mLock);
942
Mathias Agopian65ab4712010-07-14 17:59:35 -0700943 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700944 String8 out_s8;
945
Dima Zavin799a70e2011-04-18 16:57:27 -0700946 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800947 char *s;
948 {
949 AutoMutex lock(mHardwareLock);
950 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700951 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800952 s = dev->get_parameters(dev, keys.string());
953 mHardwareStatus = AUDIO_HW_IDLE;
954 }
John Grossmanef7740b2012-02-09 11:28:36 -0800955 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700956 free(s);
957 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700958 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700959 }
960
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
962 if (playbackThread != NULL) {
963 return playbackThread->getParameters(keys);
964 }
965 RecordThread *recordThread = checkRecordThread_l(ioHandle);
966 if (recordThread != NULL) {
967 return recordThread->getParameters(keys);
968 }
969 return String8("");
970}
971
Glenn Kastendd8104c2012-07-02 12:42:44 -0700972size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
973 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700974{
Eric Laurenta1884f92011-08-23 08:25:03 -0700975 status_t ret = initCheck();
976 if (ret != NO_ERROR) {
977 return 0;
978 }
979
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800980 AutoMutex lock(mHardwareLock);
981 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000982 struct audio_config config;
983 memset(&config, 0, sizeof(config));
984 config.sample_rate = sampleRate;
985 config.channel_mask = channelMask;
986 config.format = format;
987
John Grossmanee578c02012-07-23 17:05:46 -0700988 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
989 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800990 mHardwareStatus = AUDIO_HW_IDLE;
991 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992}
993
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800994unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996 Mutex::Autolock _l(mLock);
997
998 RecordThread *recordThread = checkRecordThread_l(ioHandle);
999 if (recordThread != NULL) {
1000 return recordThread->getInputFramesLost();
1001 }
1002 return 0;
1003}
1004
1005status_t AudioFlinger::setVoiceVolume(float value)
1006{
Eric Laurenta1884f92011-08-23 08:25:03 -07001007 status_t ret = initCheck();
1008 if (ret != NO_ERROR) {
1009 return ret;
1010 }
1011
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012 // check calling permissions
1013 if (!settingsAllowed()) {
1014 return PERMISSION_DENIED;
1015 }
1016
1017 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001018 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001019 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001020 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021 mHardwareStatus = AUDIO_HW_IDLE;
1022
1023 return ret;
1024}
1025
Glenn Kasten26c77552012-11-16 12:01:44 -08001026status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001027 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028{
1029 status_t status;
1030
1031 Mutex::Autolock _l(mLock);
1032
1033 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1034 if (playbackThread != NULL) {
1035 return playbackThread->getRenderPosition(halFrames, dspFrames);
1036 }
1037
1038 return BAD_VALUE;
1039}
1040
1041void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1042{
1043
1044 Mutex::Autolock _l(mLock);
1045
Glenn Kastenbb001922012-02-03 11:10:26 -08001046 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047 if (mNotificationClients.indexOfKey(pid) < 0) {
1048 sp<NotificationClient> notificationClient = new NotificationClient(this,
1049 client,
1050 pid);
Steve Block3856b092011-10-20 11:56:00 +01001051 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052
1053 mNotificationClients.add(pid, notificationClient);
1054
1055 sp<IBinder> binder = client->asBinder();
1056 binder->linkToDeath(notificationClient);
1057
1058 // the config change is always sent from playback or record threads to avoid deadlock
1059 // with AudioSystem::gLock
1060 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001061 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 }
1063
1064 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001065 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001066 }
1067 }
1068}
1069
1070void AudioFlinger::removeNotificationClient(pid_t pid)
1071{
1072 Mutex::Autolock _l(mLock);
1073
Glenn Kastena3b09252012-01-20 09:19:01 -08001074 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001075
Steve Block3856b092011-10-20 11:56:00 +01001076 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001077 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001078 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001079 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001080 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001081 ALOGV(" pid %d @ %d", ref->mPid, i);
1082 if (ref->mPid == pid) {
1083 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001084 mAudioSessionRefs.removeAt(i);
1085 delete ref;
1086 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001087 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001088 } else {
1089 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001090 }
1091 }
1092 if (removed) {
1093 purgeStaleEffects_l();
1094 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095}
1096
1097// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001098void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001099{
1100 size_t size = mNotificationClients.size();
1101 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001102 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1103 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104 }
1105}
1106
1107// removeClient_l() must be called with AudioFlinger::mLock held
1108void AudioFlinger::removeClient_l(pid_t pid)
1109{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001110 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001111 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112 mClients.removeItem(pid);
1113}
1114
Eric Laurent717e1282012-06-29 16:36:52 -07001115// getEffectThread_l() must be called with AudioFlinger::mLock held
1116sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1117{
1118 sp<PlaybackThread> thread;
1119
1120 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1121 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1122 ALOG_ASSERT(thread == 0);
1123 thread = mPlaybackThreads.valueAt(i);
1124 }
1125 }
1126
1127 return thread;
1128}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130
Mathias Agopian65ab4712010-07-14 17:59:35 -07001131
1132// ----------------------------------------------------------------------------
1133
1134AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1135 : RefBase(),
1136 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001137 // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below
Mathias Agopian65ab4712010-07-14 17:59:35 -07001138 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001139 mPid(pid),
1140 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141{
1142 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1143}
1144
1145// Client destructor must be called with AudioFlinger::mLock held
1146AudioFlinger::Client::~Client()
1147{
1148 mAudioFlinger->removeClient_l(mPid);
1149}
1150
Glenn Kasten435dbe62012-01-30 10:15:48 -08001151sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152{
1153 return mMemoryDealer;
1154}
1155
John Grossman4ff14ba2012-02-08 16:37:41 -08001156// Reserve one of the limited slots for a timed audio track associated
1157// with this client
1158bool AudioFlinger::Client::reserveTimedTrack()
1159{
1160 const int kMaxTimedTracksPerClient = 4;
1161
1162 Mutex::Autolock _l(mTimedTrackLock);
1163
1164 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1165 ALOGW("can not create timed track - pid %d has exceeded the limit",
1166 mPid);
1167 return false;
1168 }
1169
1170 mTimedTrackCount++;
1171 return true;
1172}
1173
1174// Release a slot for a timed audio track
1175void AudioFlinger::Client::releaseTimedTrack()
1176{
1177 Mutex::Autolock _l(mTimedTrackLock);
1178 mTimedTrackCount--;
1179}
1180
Mathias Agopian65ab4712010-07-14 17:59:35 -07001181// ----------------------------------------------------------------------------
1182
1183AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1184 const sp<IAudioFlingerClient>& client,
1185 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001186 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001187{
1188}
1189
1190AudioFlinger::NotificationClient::~NotificationClient()
1191{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001192}
1193
1194void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1195{
1196 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001197 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001198}
1199
Mathias Agopian65ab4712010-07-14 17:59:35 -07001200
1201// ----------------------------------------------------------------------------
1202
1203sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001204 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001205 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001206 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001207 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001208 size_t frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08001209 IAudioFlinger::track_flags_t flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001210 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211 int *sessionId,
1212 status_t *status)
1213{
1214 sp<RecordThread::RecordTrack> recordTrack;
1215 sp<RecordHandle> recordHandle;
1216 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217 status_t lStatus;
1218 RecordThread *thread;
1219 size_t inFrameCount;
1220 int lSessionId;
1221
1222 // check calling permissions
1223 if (!recordingAllowed()) {
1224 lStatus = PERMISSION_DENIED;
1225 goto Exit;
1226 }
1227
1228 // add client to list
1229 { // scope for mLock
1230 Mutex::Autolock _l(mLock);
1231 thread = checkRecordThread_l(input);
1232 if (thread == NULL) {
1233 lStatus = BAD_VALUE;
1234 goto Exit;
1235 }
1236
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001237 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001238 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001239
1240 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001241 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001242 lSessionId = *sessionId;
1243 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001244 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001245 if (sessionId != NULL) {
1246 *sessionId = lSessionId;
1247 }
1248 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001249 // create new record track.
1250 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001251 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1252 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001253 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001254 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001255 // remove local strong reference to Client before deleting the RecordTrack so that the
1256 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257 client.clear();
1258 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 goto Exit;
1260 }
1261
1262 // return to handle to client
1263 recordHandle = new RecordHandle(recordTrack);
1264 lStatus = NO_ERROR;
1265
1266Exit:
1267 if (status) {
1268 *status = lStatus;
1269 }
1270 return recordHandle;
1271}
1272
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001273
1274
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275// ----------------------------------------------------------------------------
1276
Eric Laurenta4c5a552012-03-29 10:12:40 -07001277audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1278{
1279 if (!settingsAllowed()) {
1280 return 0;
1281 }
1282 Mutex::Autolock _l(mLock);
1283 return loadHwModule_l(name);
1284}
1285
1286// loadHwModule_l() must be called with AudioFlinger::mLock held
1287audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1288{
1289 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1290 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1291 ALOGW("loadHwModule() module %s already loaded", name);
1292 return mAudioHwDevs.keyAt(i);
1293 }
1294 }
1295
Eric Laurenta4c5a552012-03-29 10:12:40 -07001296 audio_hw_device_t *dev;
1297
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001298 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001299 if (rc) {
1300 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1301 return 0;
1302 }
1303
1304 mHardwareStatus = AUDIO_HW_INIT;
1305 rc = dev->init_check(dev);
1306 mHardwareStatus = AUDIO_HW_IDLE;
1307 if (rc) {
1308 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1309 return 0;
1310 }
1311
John Grossmanee578c02012-07-23 17:05:46 -07001312 // Check and cache this HAL's level of support for master mute and master
1313 // volume. If this is the first HAL opened, and it supports the get
1314 // methods, use the initial values provided by the HAL as the current
1315 // master mute and volume settings.
1316
1317 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1318 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001319 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001320
1321 if (0 == mAudioHwDevs.size()) {
1322 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1323 if (NULL != dev->get_master_volume) {
1324 float mv;
1325 if (OK == dev->get_master_volume(dev, &mv)) {
1326 mMasterVolume = mv;
1327 }
1328 }
1329
1330 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1331 if (NULL != dev->get_master_mute) {
1332 bool mm;
1333 if (OK == dev->get_master_mute(dev, &mm)) {
1334 mMasterMute = mm;
1335 }
1336 }
1337 }
1338
Eric Laurenta4c5a552012-03-29 10:12:40 -07001339 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001340 if ((NULL != dev->set_master_volume) &&
1341 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1342 flags = static_cast<AudioHwDevice::Flags>(flags |
1343 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1344 }
1345
1346 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1347 if ((NULL != dev->set_master_mute) &&
1348 (OK == dev->set_master_mute(dev, mMasterMute))) {
1349 flags = static_cast<AudioHwDevice::Flags>(flags |
1350 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1351 }
1352
Eric Laurenta4c5a552012-03-29 10:12:40 -07001353 mHardwareStatus = AUDIO_HW_IDLE;
1354 }
1355
1356 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001357 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001358
1359 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001360 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001361
1362 return handle;
1363
1364}
1365
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001366// ----------------------------------------------------------------------------
1367
Glenn Kasten3b16c762012-11-14 08:44:39 -08001368uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001369{
1370 Mutex::Autolock _l(mLock);
1371 PlaybackThread *thread = primaryPlaybackThread_l();
1372 return thread != NULL ? thread->sampleRate() : 0;
1373}
1374
Glenn Kastene33054e2012-11-14 12:54:39 -08001375size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001376{
1377 Mutex::Autolock _l(mLock);
1378 PlaybackThread *thread = primaryPlaybackThread_l();
1379 return thread != NULL ? thread->frameCountHAL() : 0;
1380}
1381
1382// ----------------------------------------------------------------------------
1383
Eric Laurenta4c5a552012-03-29 10:12:40 -07001384audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1385 audio_devices_t *pDevices,
1386 uint32_t *pSamplingRate,
1387 audio_format_t *pFormat,
1388 audio_channel_mask_t *pChannelMask,
1389 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001390 audio_output_flags_t flags,
1391 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001392{
1393 status_t status;
1394 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001395 struct audio_config config;
1396 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1397 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1398 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1399 if (offloadInfo) {
1400 config.offload_info = *offloadInfo;
1401 }
1402
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001403 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001404 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001405
Eric Laurenta4c5a552012-03-29 10:12:40 -07001406 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
1407 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001408 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001409 config.sample_rate,
1410 config.format,
1411 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001412 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413
1414 if (pDevices == NULL || *pDevices == 0) {
1415 return 0;
1416 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001417
Mathias Agopian65ab4712010-07-14 17:59:35 -07001418 Mutex::Autolock _l(mLock);
1419
Eric Laurenta4c5a552012-03-29 10:12:40 -07001420 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001421 if (outHwDev == NULL)
1422 return 0;
1423
John Grossmanee578c02012-07-23 17:05:46 -07001424 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001425 audio_io_handle_t id = nextUniqueId();
1426
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001427 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001428
John Grossmanee578c02012-07-23 17:05:46 -07001429 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001430 id,
1431 *pDevices,
1432 (audio_output_flags_t)flags,
1433 &config,
1434 &outStream);
1435
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001436 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001437 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, "
1438 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001439 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001440 config.sample_rate,
1441 config.format,
1442 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001443 status);
1444
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001445 if (status == NO_ERROR && outStream != NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001446 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001447
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001448 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001449 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1450 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001451 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001452 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453 } else {
1454 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001455 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456 }
1457 mPlaybackThreads.add(id, thread);
1458
Glenn Kasten7c027242012-12-26 14:43:16 -08001459 if (pSamplingRate != NULL) {
1460 *pSamplingRate = config.sample_rate;
1461 }
1462 if (pFormat != NULL) {
1463 *pFormat = config.format;
1464 }
1465 if (pChannelMask != NULL) {
1466 *pChannelMask = config.channel_mask;
1467 }
1468 if (pLatencyMs != NULL) {
1469 *pLatencyMs = thread->latency();
1470 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471
1472 // notify client processes of the new output creation
1473 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001474
1475 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001476 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001477 ALOGI("Using module %d has the primary audio interface", module);
1478 mPrimaryHardwareDev = outHwDev;
1479
1480 AutoMutex lock(mHardwareLock);
1481 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001482 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001483 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001484 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485 return id;
1486 }
1487
1488 return 0;
1489}
1490
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001491audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1492 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001493{
1494 Mutex::Autolock _l(mLock);
1495 MixerThread *thread1 = checkMixerThread_l(output1);
1496 MixerThread *thread2 = checkMixerThread_l(output2);
1497
1498 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001499 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1500 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001501 return 0;
1502 }
1503
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001504 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001505 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1506 thread->addOutputTrack(thread2);
1507 mPlaybackThreads.add(id, thread);
1508 // notify client processes of the new output creation
1509 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1510 return id;
1511}
1512
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001513status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001514{
Glenn Kastend96c5722012-04-25 13:44:49 -07001515 return closeOutput_nonvirtual(output);
1516}
1517
1518status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1519{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520 // keep strong reference on the playback thread so that
1521 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001522 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001523 {
1524 Mutex::Autolock _l(mLock);
1525 thread = checkPlaybackThread_l(output);
1526 if (thread == NULL) {
1527 return BAD_VALUE;
1528 }
1529
Steve Block3856b092011-10-20 11:56:00 +01001530 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001531
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001532 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001534 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001535 DuplicatingThread *dupThread =
1536 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001537 dupThread->removeOutputTrack((MixerThread *)thread.get());
1538 }
1539 }
1540 }
Glenn Kastena1117922012-01-26 10:53:32 -08001541 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542 mPlaybackThreads.removeItem(output);
1543 }
1544 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001545 // The thread entity (active unit of execution) is no longer running here,
1546 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001547
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001548 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001549 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001550 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001551 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001552 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001553 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001554 }
1555 return NO_ERROR;
1556}
1557
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001558status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001559{
1560 Mutex::Autolock _l(mLock);
1561 PlaybackThread *thread = checkPlaybackThread_l(output);
1562
1563 if (thread == NULL) {
1564 return BAD_VALUE;
1565 }
1566
Steve Block3856b092011-10-20 11:56:00 +01001567 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001568 thread->suspend();
1569
1570 return NO_ERROR;
1571}
1572
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001573status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001574{
1575 Mutex::Autolock _l(mLock);
1576 PlaybackThread *thread = checkPlaybackThread_l(output);
1577
1578 if (thread == NULL) {
1579 return BAD_VALUE;
1580 }
1581
Steve Block3856b092011-10-20 11:56:00 +01001582 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001583
1584 thread->restore();
1585
1586 return NO_ERROR;
1587}
1588
Eric Laurenta4c5a552012-03-29 10:12:40 -07001589audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1590 audio_devices_t *pDevices,
1591 uint32_t *pSamplingRate,
1592 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001593 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594{
1595 status_t status;
1596 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001597 struct audio_config config;
1598 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1599 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1600 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1601
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001602 uint32_t reqSamplingRate = config.sample_rate;
1603 audio_format_t reqFormat = config.format;
1604 audio_channel_mask_t reqChannels = config.channel_mask;
1605 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001606 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001607
1608 if (pDevices == NULL || *pDevices == 0) {
1609 return 0;
1610 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001611
Mathias Agopian65ab4712010-07-14 17:59:35 -07001612 Mutex::Autolock _l(mLock);
1613
Eric Laurenta4c5a552012-03-29 10:12:40 -07001614 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001615 if (inHwDev == NULL)
1616 return 0;
1617
John Grossmanee578c02012-07-23 17:05:46 -07001618 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001619 audio_io_handle_t id = nextUniqueId();
1620
John Grossmanee578c02012-07-23 17:05:46 -07001621 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001622 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001623 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1624 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001625 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001626 config.sample_rate,
1627 config.format,
1628 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001629 status);
1630
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001631 // If the input could not be opened with the requested parameters and we can handle the
1632 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1633 // 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 -07001634 if (status == BAD_VALUE &&
1635 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1636 (config.sample_rate <= 2 * reqSamplingRate) &&
1637 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001638 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001639 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001640 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001641 }
1642
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001643 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001644
Glenn Kasten46909e72013-02-26 09:20:22 -08001645#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001646 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1647 // or (re-)create if current Pipe is idle and does not match the new format
1648 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001649 enum {
1650 TEE_SINK_NO, // don't copy input
1651 TEE_SINK_NEW, // copy input using a new pipe
1652 TEE_SINK_OLD, // copy input using an existing pipe
1653 } kind;
1654 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1655 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001656 if (!mTeeSinkInputEnabled) {
1657 kind = TEE_SINK_NO;
1658 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001659 kind = TEE_SINK_NO;
1660 } else if (mRecordTeeSink == 0) {
1661 kind = TEE_SINK_NEW;
1662 } else if (mRecordTeeSink->getStrongCount() != 1) {
1663 kind = TEE_SINK_NO;
1664 } else if (format == mRecordTeeSink->format()) {
1665 kind = TEE_SINK_OLD;
1666 } else {
1667 kind = TEE_SINK_NEW;
1668 }
1669 switch (kind) {
1670 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001671 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001672 size_t numCounterOffers = 0;
1673 const NBAIO_Format offers[1] = {format};
1674 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1675 ALOG_ASSERT(index == 0);
1676 PipeReader *pipeReader = new PipeReader(*pipe);
1677 numCounterOffers = 0;
1678 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1679 ALOG_ASSERT(index == 0);
1680 mRecordTeeSink = pipe;
1681 mRecordTeeSource = pipeReader;
1682 teeSink = pipe;
1683 }
1684 break;
1685 case TEE_SINK_OLD:
1686 teeSink = mRecordTeeSink;
1687 break;
1688 case TEE_SINK_NO:
1689 default:
1690 break;
1691 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001692#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001693
Dima Zavin799a70e2011-04-18 16:57:27 -07001694 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1695
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001696 // Start record thread
1697 // RecorThread require both input and output device indication to forward to audio
1698 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001699 thread = new RecordThread(this,
1700 input,
1701 reqSamplingRate,
1702 reqChannels,
1703 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001704 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001705 *pDevices
1706#ifdef TEE_SINK
1707 , teeSink
1708#endif
1709 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001710 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001711 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001712 if (pSamplingRate != NULL) {
1713 *pSamplingRate = reqSamplingRate;
1714 }
1715 if (pFormat != NULL) {
1716 *pFormat = config.format;
1717 }
1718 if (pChannelMask != NULL) {
1719 *pChannelMask = reqChannels;
1720 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001721
Mathias Agopian65ab4712010-07-14 17:59:35 -07001722 // notify client processes of the new input creation
1723 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1724 return id;
1725 }
1726
1727 return 0;
1728}
1729
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001730status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001731{
Glenn Kastend96c5722012-04-25 13:44:49 -07001732 return closeInput_nonvirtual(input);
1733}
1734
1735status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1736{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001737 // keep strong reference on the record thread so that
1738 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001739 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001740 {
1741 Mutex::Autolock _l(mLock);
1742 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001743 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001744 return BAD_VALUE;
1745 }
1746
Steve Block3856b092011-10-20 11:56:00 +01001747 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001748 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001749 mRecordThreads.removeItem(input);
1750 }
1751 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001752 // The thread entity (active unit of execution) is no longer running here,
1753 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001754
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001755 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001756 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001757 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001758 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001759 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001760
1761 return NO_ERROR;
1762}
1763
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001764status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001765{
1766 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001767 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001768
1769 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1770 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001771 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001772 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001773
1774 return NO_ERROR;
1775}
1776
1777
1778int AudioFlinger::newAudioSessionId()
1779{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001780 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001781}
1782
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001783void AudioFlinger::acquireAudioSessionId(int audioSession)
1784{
1785 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001786 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001787 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001788 size_t num = mAudioSessionRefs.size();
1789 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001790 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001791 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1792 ref->mCnt++;
1793 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001794 return;
1795 }
1796 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001797 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1798 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001799}
1800
1801void AudioFlinger::releaseAudioSessionId(int audioSession)
1802{
1803 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001804 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001805 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001806 size_t num = mAudioSessionRefs.size();
1807 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001808 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001809 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1810 ref->mCnt--;
1811 ALOGV(" decremented refcount to %d", ref->mCnt);
1812 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001813 mAudioSessionRefs.removeAt(i);
1814 delete ref;
1815 purgeStaleEffects_l();
1816 }
1817 return;
1818 }
1819 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001820 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001821}
1822
1823void AudioFlinger::purgeStaleEffects_l() {
1824
Steve Block3856b092011-10-20 11:56:00 +01001825 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001826
1827 Vector< sp<EffectChain> > chains;
1828
1829 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1830 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1831 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1832 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001833 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1834 chains.push(ec);
1835 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001836 }
1837 }
1838 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1839 sp<RecordThread> t = mRecordThreads.valueAt(i);
1840 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1841 sp<EffectChain> ec = t->mEffectChains[j];
1842 chains.push(ec);
1843 }
1844 }
1845
1846 for (size_t i = 0; i < chains.size(); i++) {
1847 sp<EffectChain> ec = chains[i];
1848 int sessionid = ec->sessionId();
1849 sp<ThreadBase> t = ec->mThread.promote();
1850 if (t == 0) {
1851 continue;
1852 }
1853 size_t numsessionrefs = mAudioSessionRefs.size();
1854 bool found = false;
1855 for (size_t k = 0; k < numsessionrefs; k++) {
1856 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001857 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001858 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001859 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001860 found = true;
1861 break;
1862 }
1863 }
1864 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001865 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001866 // remove all effects from the chain
1867 while (ec->mEffects.size()) {
1868 sp<EffectModule> effect = ec->mEffects[0];
1869 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001870 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001871 if (effect->purgeHandles()) {
1872 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001873 }
1874 AudioSystem::unregisterEffect(effect->id());
1875 }
1876 }
1877 }
1878 return;
1879}
1880
Mathias Agopian65ab4712010-07-14 17:59:35 -07001881// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001882AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001883{
Glenn Kastena1117922012-01-26 10:53:32 -08001884 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885}
1886
1887// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001888AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001889{
1890 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001891 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001892}
1893
1894// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001895AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001896{
Glenn Kastena1117922012-01-26 10:53:32 -08001897 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001898}
1899
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001900uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001902 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001903}
1904
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001905AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001906{
1907 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1908 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001909 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001910 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001911 return thread;
1912 }
1913 }
1914 return NULL;
1915}
1916
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001917audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001918{
1919 PlaybackThread *thread = primaryPlaybackThread_l();
1920
1921 if (thread == NULL) {
1922 return 0;
1923 }
1924
Eric Laurentf1c04f92012-08-28 14:26:53 -07001925 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001926}
1927
Eric Laurenta011e352012-03-29 15:51:43 -07001928sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1929 int triggerSession,
1930 int listenerSession,
1931 sync_event_callback_t callBack,
1932 void *cookie)
1933{
1934 Mutex::Autolock _l(mLock);
1935
1936 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1937 status_t playStatus = NAME_NOT_FOUND;
1938 status_t recStatus = NAME_NOT_FOUND;
1939 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1940 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1941 if (playStatus == NO_ERROR) {
1942 return event;
1943 }
1944 }
1945 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1946 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1947 if (recStatus == NO_ERROR) {
1948 return event;
1949 }
1950 }
1951 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
1952 mPendingSyncEvents.add(event);
1953 } else {
1954 ALOGV("createSyncEvent() invalid event %d", event->type());
1955 event.clear();
1956 }
1957 return event;
1958}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001959
Mathias Agopian65ab4712010-07-14 17:59:35 -07001960// ----------------------------------------------------------------------------
1961// Effect management
1962// ----------------------------------------------------------------------------
1963
1964
Glenn Kastenf587ba52012-01-26 16:25:10 -08001965status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001966{
1967 Mutex::Autolock _l(mLock);
1968 return EffectQueryNumberEffects(numEffects);
1969}
1970
Glenn Kastenf587ba52012-01-26 16:25:10 -08001971status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001972{
1973 Mutex::Autolock _l(mLock);
1974 return EffectQueryEffect(index, descriptor);
1975}
1976
Glenn Kasten5e92a782012-01-30 07:40:52 -08001977status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08001978 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979{
1980 Mutex::Autolock _l(mLock);
1981 return EffectGetDescriptor(pUuid, descriptor);
1982}
1983
1984
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001985sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07001986 effect_descriptor_t *pDesc,
1987 const sp<IEffectClient>& effectClient,
1988 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001989 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001990 int sessionId,
1991 status_t *status,
1992 int *id,
1993 int *enabled)
1994{
1995 status_t lStatus = NO_ERROR;
1996 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001997 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001998
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001999 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002000 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002001 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002002
2003 if (pDesc == NULL) {
2004 lStatus = BAD_VALUE;
2005 goto Exit;
2006 }
2007
Eric Laurent84e9a102010-09-23 16:10:16 -07002008 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002009 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002010 lStatus = PERMISSION_DENIED;
2011 goto Exit;
2012 }
2013
Dima Zavinfce7a472011-04-19 22:30:36 -07002014 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002015 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002016 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002017 lStatus = PERMISSION_DENIED;
2018 goto Exit;
2019 }
2020
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002021 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002022 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002023 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07002024 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07002025 lStatus = BAD_VALUE;
2026 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07002027 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002028 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002029 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002030 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002031 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002032 }
2033 }
2034
Mathias Agopian65ab4712010-07-14 17:59:35 -07002035 {
2036 Mutex::Autolock _l(mLock);
2037
Mathias Agopian65ab4712010-07-14 17:59:35 -07002038
2039 if (!EffectIsNullUuid(&pDesc->uuid)) {
2040 // if uuid is specified, request effect descriptor
2041 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2042 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002043 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002044 goto Exit;
2045 }
2046 } else {
2047 // if uuid is not specified, look for an available implementation
2048 // of the required type in effect factory
2049 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002050 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002051 lStatus = BAD_VALUE;
2052 goto Exit;
2053 }
2054 uint32_t numEffects = 0;
2055 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002056 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002057 bool found = false;
2058
2059 lStatus = EffectQueryNumberEffects(&numEffects);
2060 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002061 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002062 goto Exit;
2063 }
2064 for (uint32_t i = 0; i < numEffects; i++) {
2065 lStatus = EffectQueryEffect(i, &desc);
2066 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002067 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002068 continue;
2069 }
2070 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2071 // If matching type found save effect descriptor. If the session is
2072 // 0 and the effect is not auxiliary, continue enumeration in case
2073 // an auxiliary version of this effect type is available
2074 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002075 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002076 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002077 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2078 break;
2079 }
2080 }
2081 }
2082 if (!found) {
2083 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002084 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002085 goto Exit;
2086 }
2087 // For same effect type, chose auxiliary version over insert version if
2088 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002089 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002090 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002091 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002092 }
2093 }
2094
2095 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002096 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002097 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2098 lStatus = INVALID_OPERATION;
2099 goto Exit;
2100 }
2101
Eric Laurent59255e42011-07-27 19:49:51 -07002102 // check recording permission for visualizer
2103 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2104 !recordingAllowed()) {
2105 lStatus = PERMISSION_DENIED;
2106 goto Exit;
2107 }
2108
Mathias Agopian65ab4712010-07-14 17:59:35 -07002109 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002110 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002111
2112 // If output is not specified try to find a matching audio session ID in one of the
2113 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002114 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2115 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002116 // Note: io is never 0 when creating an effect on an input
2117 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002118 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002119 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2120 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002121 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002122 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002123 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002124 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002125 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002126 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2127 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2128 io = mRecordThreads.keyAt(i);
2129 break;
2130 }
2131 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002132 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002133 // If no output thread contains the requested session ID, default to
2134 // first output. The effect chain will be moved to the correct output
2135 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002136 if (io == 0 && mPlaybackThreads.size()) {
2137 io = mPlaybackThreads.keyAt(0);
2138 }
Steve Block3856b092011-10-20 11:56:00 +01002139 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002140 }
2141 ThreadBase *thread = checkRecordThread_l(io);
2142 if (thread == NULL) {
2143 thread = checkPlaybackThread_l(io);
2144 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002145 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002146 lStatus = BAD_VALUE;
2147 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002148 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002149 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002150
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002151 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002152
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002153 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002154 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2155 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002156 if (handle != 0 && id != NULL) {
2157 *id = handle->id();
2158 }
2159 }
2160
2161Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002162 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002163 *status = lStatus;
2164 }
2165 return handle;
2166}
2167
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002168status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2169 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002170{
Steve Block3856b092011-10-20 11:56:00 +01002171 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002172 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002173 Mutex::Autolock _l(mLock);
2174 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002175 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002176 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002177 }
Eric Laurentde070132010-07-13 04:45:46 -07002178 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2179 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002180 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002181 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002182 }
Eric Laurentde070132010-07-13 04:45:46 -07002183 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2184 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002185 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002186 return BAD_VALUE;
2187 }
2188
2189 Mutex::Autolock _dl(dstThread->mLock);
2190 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002191 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002192
Mathias Agopian65ab4712010-07-14 17:59:35 -07002193 return NO_ERROR;
2194}
2195
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002196// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002197status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002198 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002199 AudioFlinger::PlaybackThread *dstThread,
2200 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002201{
Steve Block3856b092011-10-20 11:56:00 +01002202 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002203 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002204
Eric Laurent59255e42011-07-27 19:49:51 -07002205 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002206 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002207 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002208 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002209 return INVALID_OPERATION;
2210 }
2211
Eric Laurent39e94f82010-07-28 01:32:47 -07002212 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002213 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002214 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002215 // removed.
2216 srcThread->removeEffectChain_l(chain);
2217
2218 // transfer all effects one by one so that new effect chain is created on new thread with
2219 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002220 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002221 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002222 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002223 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2224 while (effect != 0) {
2225 srcThread->removeEffect_l(effect);
2226 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002227 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2228 if (effect->state() == EffectModule::ACTIVE ||
2229 effect->state() == EffectModule::STOPPING) {
2230 effect->start();
2231 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002232 // if the move request is not received from audio policy manager, the effect must be
2233 // re-registered with the new strategy and output
2234 if (dstChain == 0) {
2235 dstChain = effect->chain().promote();
2236 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002237 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002238 srcThread->addEffect_l(effect);
2239 return NO_INIT;
2240 }
2241 strategy = dstChain->strategy();
2242 }
2243 if (reRegister) {
2244 AudioSystem::unregisterEffect(effect->id());
2245 AudioSystem::registerEffect(&effect->desc(),
2246 dstOutput,
2247 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002248 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002249 effect->id());
2250 }
Eric Laurentde070132010-07-13 04:45:46 -07002251 effect = chain->getEffectFromId_l(0);
2252 }
2253
2254 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002255}
2256
Glenn Kastenda6ef132013-01-10 12:31:01 -08002257struct Entry {
2258#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2259 char mName[MAX_NAME];
2260};
2261
2262int comparEntry(const void *p1, const void *p2)
2263{
2264 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2265}
2266
Glenn Kasten46909e72013-02-26 09:20:22 -08002267#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002268void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002269{
Eric Laurent81784c32012-11-19 14:55:58 -08002270 NBAIO_Source *teeSource = source.get();
2271 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002272 // .wav rotation
2273 // There is a benign race condition if 2 threads call this simultaneously.
2274 // They would both traverse the directory, but the result would simply be
2275 // failures at unlink() which are ignored. It's also unlikely since
2276 // normally dumpsys is only done by bugreport or from the command line.
2277 char teePath[32+256];
2278 strcpy(teePath, "/data/misc/media");
2279 size_t teePathLen = strlen(teePath);
2280 DIR *dir = opendir(teePath);
2281 teePath[teePathLen++] = '/';
2282 if (dir != NULL) {
2283#define MAX_SORT 20 // number of entries to sort
2284#define MAX_KEEP 10 // number of entries to keep
2285 struct Entry entries[MAX_SORT];
2286 size_t entryCount = 0;
2287 while (entryCount < MAX_SORT) {
2288 struct dirent de;
2289 struct dirent *result = NULL;
2290 int rc = readdir_r(dir, &de, &result);
2291 if (rc != 0) {
2292 ALOGW("readdir_r failed %d", rc);
2293 break;
2294 }
2295 if (result == NULL) {
2296 break;
2297 }
2298 if (result != &de) {
2299 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2300 break;
2301 }
2302 // ignore non .wav file entries
2303 size_t nameLen = strlen(de.d_name);
2304 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2305 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2306 continue;
2307 }
2308 strcpy(entries[entryCount++].mName, de.d_name);
2309 }
2310 (void) closedir(dir);
2311 if (entryCount > MAX_KEEP) {
2312 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2313 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2314 strcpy(&teePath[teePathLen], entries[i].mName);
2315 (void) unlink(teePath);
2316 }
2317 }
2318 } else {
2319 if (fd >= 0) {
2320 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2321 }
2322 }
Eric Laurent81784c32012-11-19 14:55:58 -08002323 char teeTime[16];
2324 struct timeval tv;
2325 gettimeofday(&tv, NULL);
2326 struct tm tm;
2327 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002328 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2329 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2330 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2331 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002332 if (teeFd >= 0) {
2333 char wavHeader[44];
2334 memcpy(wavHeader,
2335 "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",
2336 sizeof(wavHeader));
2337 NBAIO_Format format = teeSource->format();
2338 unsigned channelCount = Format_channelCount(format);
2339 ALOG_ASSERT(channelCount <= FCC_2);
2340 uint32_t sampleRate = Format_sampleRate(format);
2341 wavHeader[22] = channelCount; // number of channels
2342 wavHeader[24] = sampleRate; // sample rate
2343 wavHeader[25] = sampleRate >> 8;
2344 wavHeader[32] = channelCount * 2; // block alignment
2345 write(teeFd, wavHeader, sizeof(wavHeader));
2346 size_t total = 0;
2347 bool firstRead = true;
2348 for (;;) {
2349#define TEE_SINK_READ 1024
2350 short buffer[TEE_SINK_READ * FCC_2];
2351 size_t count = TEE_SINK_READ;
2352 ssize_t actual = teeSource->read(buffer, count,
2353 AudioBufferProvider::kInvalidPTS);
2354 bool wasFirstRead = firstRead;
2355 firstRead = false;
2356 if (actual <= 0) {
2357 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2358 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002359 }
Eric Laurent81784c32012-11-19 14:55:58 -08002360 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002361 }
Eric Laurent81784c32012-11-19 14:55:58 -08002362 ALOG_ASSERT(actual <= (ssize_t)count);
2363 write(teeFd, buffer, actual * channelCount * sizeof(short));
2364 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002365 }
Eric Laurent81784c32012-11-19 14:55:58 -08002366 lseek(teeFd, (off_t) 4, SEEK_SET);
2367 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2368 write(teeFd, &temp, sizeof(temp));
2369 lseek(teeFd, (off_t) 40, SEEK_SET);
2370 temp = total * channelCount * sizeof(short);
2371 write(teeFd, &temp, sizeof(temp));
2372 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002373 if (fd >= 0) {
2374 fdprintf(fd, "tee copied to %s\n", teePath);
2375 }
Eric Laurent59255e42011-07-27 19:49:51 -07002376 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002377 if (fd >= 0) {
2378 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2379 }
Eric Laurent59255e42011-07-27 19:49:51 -07002380 }
2381 }
2382}
Glenn Kasten46909e72013-02-26 09:20:22 -08002383#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002384
Mathias Agopian65ab4712010-07-14 17:59:35 -07002385// ----------------------------------------------------------------------------
2386
2387status_t AudioFlinger::onTransact(
2388 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2389{
2390 return BnAudioFlinger::onTransact(code, data, reply, flags);
2391}
2392
Mathias Agopian65ab4712010-07-14 17:59:35 -07002393}; // namespace android