blob: 711b62fe846125c15a498a6d95f6af1fd0d71026 [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 Kasten4182c4e2013-07-15 14:45:07 -070065#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080066
Mathias Agopian65ab4712010-07-14 17:59:35 -070067// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070068
John Grossman1c345192012-03-27 14:00:17 -070069// Note: the following macro is used for extremely verbose logging message. In
70// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
71// 0; but one side effect of this is to turn all LOGV's as well. Some messages
72// are so verbose that we want to suppress them even when we have ALOG_ASSERT
73// turned on. Do not uncomment the #def below unless you really know what you
74// are doing and want to see all of the extremely verbose messages.
75//#define VERY_VERY_VERBOSE_LOGGING
76#ifdef VERY_VERY_VERBOSE_LOGGING
77#define ALOGVV ALOGV
78#else
79#define ALOGVV(a...) do { } while(0)
80#endif
Eric Laurentde070132010-07-13 04:45:46 -070081
Mathias Agopian65ab4712010-07-14 17:59:35 -070082namespace android {
83
Glenn Kastenec1d6b52011-12-12 09:04:45 -080084static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
85static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070086
Glenn Kasten58912562012-04-03 10:45:00 -070087
John Grossman4ff14ba2012-02-08 16:37:41 -080088nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080089
Eric Laurent81784c32012-11-19 14:55:58 -080090uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070091
Glenn Kasten46909e72013-02-26 09:20:22 -080092#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080093bool AudioFlinger::mTeeSinkInputEnabled = false;
94bool AudioFlinger::mTeeSinkOutputEnabled = false;
95bool AudioFlinger::mTeeSinkTrackEnabled = false;
96
97size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
98size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
99size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800100#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800101
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102// ----------------------------------------------------------------------------
103
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700104static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700105{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700106 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700107 int rc;
108
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700109 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
110 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
111 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
112 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700113 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700114 }
115 rc = audio_hw_device_open(mod, dev);
116 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
117 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
118 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700119 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700120 }
121 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
122 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
123 rc = BAD_VALUE;
124 goto out;
125 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700126 return 0;
127
128out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700129 *dev = NULL;
130 return rc;
131}
132
Mathias Agopian65ab4712010-07-14 17:59:35 -0700133// ----------------------------------------------------------------------------
134
135AudioFlinger::AudioFlinger()
136 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800137 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700138 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800139 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800140 mMasterMute(false),
141 mNextUniqueId(1),
142 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700143 mBtNrecIsOff(false),
144 mIsLowRamDevice(true),
145 mIsDeviceTypeKnown(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700146{
Glenn Kasten949a9262013-04-16 12:35:20 -0700147 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800148 char value[PROPERTY_VALUE_MAX];
149 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
150 if (doLog) {
151 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
152 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800153#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800154 (void) property_get("ro.debuggable", value, "0");
155 int debuggable = atoi(value);
156 int teeEnabled = 0;
157 if (debuggable) {
158 (void) property_get("af.tee", value, "0");
159 teeEnabled = atoi(value);
160 }
161 if (teeEnabled & 1)
162 mTeeSinkInputEnabled = true;
163 if (teeEnabled & 2)
164 mTeeSinkOutputEnabled = true;
165 if (teeEnabled & 4)
166 mTeeSinkTrackEnabled = true;
Glenn Kasten46909e72013-02-26 09:20:22 -0800167#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700168}
169
170void AudioFlinger::onFirstRef()
171{
Dima Zavin799a70e2011-04-18 16:57:27 -0700172 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700173
Eric Laurent93575202011-01-18 18:39:02 -0800174 Mutex::Autolock _l(mLock);
175
Dima Zavin799a70e2011-04-18 16:57:27 -0700176 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800177 char val_str[PROPERTY_VALUE_MAX] = { 0 };
178 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
179 uint32_t int_val;
180 if (1 == sscanf(val_str, "%u", &int_val)) {
181 mStandbyTimeInNsecs = milliseconds(int_val);
182 ALOGI("Using %u mSec as standby time.", int_val);
183 } else {
184 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
185 ALOGI("Using default %u mSec as standby time.",
186 (uint32_t)(mStandbyTimeInNsecs / 1000000));
187 }
188 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189
Eric Laurenta4c5a552012-03-29 10:12:40 -0700190 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700191}
192
193AudioFlinger::~AudioFlinger()
194{
195 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700196 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700197 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700198 }
199 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700200 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700201 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700203
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800204 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
205 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700206 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
207 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208 }
209}
210
Eric Laurenta4c5a552012-03-29 10:12:40 -0700211static const char * const audio_interfaces[] = {
212 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
213 AUDIO_HARDWARE_MODULE_ID_A2DP,
214 AUDIO_HARDWARE_MODULE_ID_USB,
215};
216#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
217
John Grossmanee578c02012-07-23 17:05:46 -0700218AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
219 audio_module_handle_t module,
220 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700221{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700222 // if module is 0, the request comes from an old policy manager and we should load
223 // well known modules
224 if (module == 0) {
225 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
226 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
227 loadHwModule_l(audio_interfaces[i]);
228 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700229 // then try to find a module supporting the requested device.
230 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
231 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
232 audio_hw_device_t *dev = audioHwDevice->hwDevice();
233 if ((dev->get_supported_devices != NULL) &&
234 (dev->get_supported_devices(dev) & devices) == devices)
235 return audioHwDevice;
236 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700237 } else {
238 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700239 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
240 if (audioHwDevice != NULL) {
241 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700242 }
243 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700244
Dima Zavin799a70e2011-04-18 16:57:27 -0700245 return NULL;
246}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700247
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700248void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249{
250 const size_t SIZE = 256;
251 char buffer[SIZE];
252 String8 result;
253
254 result.append("Clients:\n");
255 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800256 sp<Client> client = mClients.valueAt(i).promote();
257 if (client != 0) {
258 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
259 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700260 }
261 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700262
263 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800264 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700265 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
266 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800267 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700268 result.append(buffer);
269 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700270 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271}
272
273
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700274void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275{
276 const size_t SIZE = 256;
277 char buffer[SIZE];
278 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800279 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280
John Grossman4ff14ba2012-02-08 16:37:41 -0800281 snprintf(buffer, SIZE, "Hardware status: %d\n"
282 "Standby Time mSec: %u\n",
283 hardwareStatus,
284 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285 result.append(buffer);
286 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287}
288
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700289void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700290{
291 const size_t SIZE = 256;
292 char buffer[SIZE];
293 String8 result;
294 snprintf(buffer, SIZE, "Permission Denial: "
295 "can't dump AudioFlinger from pid=%d, uid=%d\n",
296 IPCThreadState::self()->getCallingPid(),
297 IPCThreadState::self()->getCallingUid());
298 result.append(buffer);
299 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300}
301
Eric Laurent81784c32012-11-19 14:55:58 -0800302bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303{
304 bool locked = false;
305 for (int i = 0; i < kDumpLockRetries; ++i) {
306 if (mutex.tryLock() == NO_ERROR) {
307 locked = true;
308 break;
309 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800310 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700311 }
312 return locked;
313}
314
315status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
316{
Glenn Kasten44deb052012-02-05 18:09:08 -0800317 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700318 dumpPermissionDenial(fd, args);
319 } else {
320 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800321 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322 if (!hardwareLocked) {
323 String8 result(kHardwareLockedString);
324 write(fd, result.string(), result.size());
325 } else {
326 mHardwareLock.unlock();
327 }
328
Eric Laurent81784c32012-11-19 14:55:58 -0800329 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330
331 // failed to lock - AudioFlinger is probably deadlocked
332 if (!locked) {
333 String8 result(kDeadlockedString);
334 write(fd, result.string(), result.size());
335 }
336
337 dumpClients(fd, args);
338 dumpInternals(fd, args);
339
340 // dump playback threads
341 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
342 mPlaybackThreads.valueAt(i)->dump(fd, args);
343 }
344
345 // dump record threads
346 for (size_t i = 0; i < mRecordThreads.size(); i++) {
347 mRecordThreads.valueAt(i)->dump(fd, args);
348 }
349
Dima Zavin799a70e2011-04-18 16:57:27 -0700350 // dump all hardware devs
351 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700352 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700353 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700354 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700355
Glenn Kasten46909e72013-02-26 09:20:22 -0800356#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700357 // dump the serially shared record tee sink
358 if (mRecordTeeSource != 0) {
359 dumpTee(fd, mRecordTeeSource);
360 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800361#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700362
Glenn Kastend65d73c2012-06-22 17:21:07 -0700363 if (locked) {
364 mLock.unlock();
365 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800366
367 // append a copy of media.log here by forwarding fd to it, but don't attempt
368 // to lookup the service if it's not running, as it will block for a second
369 if (mLogMemoryDealer != 0) {
370 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
371 if (binder != 0) {
372 fdprintf(fd, "\nmedia.log:\n");
373 Vector<String16> args;
374 binder->dump(fd, args);
375 }
376 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377 }
378 return NO_ERROR;
379}
380
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800381sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
382{
383 // If pid is already in the mClients wp<> map, then use that entry
384 // (for which promote() is always != 0), otherwise create a new entry and Client.
385 sp<Client> client = mClients.valueFor(pid).promote();
386 if (client == 0) {
387 client = new Client(this, pid);
388 mClients.add(pid, client);
389 }
390
391 return client;
392}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393
Glenn Kasten9e58b552013-01-18 15:09:48 -0800394sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
395{
396 if (mLogMemoryDealer == 0) {
397 return new NBLog::Writer();
398 }
399 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
400 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
401 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
402 if (binder != 0) {
403 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
404 }
405 return writer;
406}
407
408void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
409{
Glenn Kasten685ef092013-02-04 08:15:34 -0800410 if (writer == 0) {
411 return;
412 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800413 sp<IMemory> iMemory(writer->getIMemory());
414 if (iMemory == 0) {
415 return;
416 }
417 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
418 if (binder != 0) {
419 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
420 // Now the media.log remote reference to IMemory is gone.
421 // When our last local reference to IMemory also drops to zero,
422 // the IMemory destructor will deallocate the region from mMemoryDealer.
423 }
424}
425
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426// IAudioFlinger interface
427
428
429sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800430 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800432 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700433 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800434 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800435 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800437 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800438 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439 int *sessionId,
440 status_t *status)
441{
442 sp<PlaybackThread::Track> track;
443 sp<TrackHandle> trackHandle;
444 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445 status_t lStatus;
446 int lSessionId;
447
Glenn Kasten263709e2012-01-06 08:40:01 -0800448 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
449 // but if someone uses binder directly they could bypass that and cause us to crash
450 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000451 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 lStatus = BAD_VALUE;
453 goto Exit;
454 }
455
Glenn Kasten60a83922012-06-21 12:56:37 -0700456 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
457 // and we don't yet support 8.24 or 32-bit PCM
458 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
459 ALOGE("createTrack() invalid format %d", format);
460 lStatus = BAD_VALUE;
461 goto Exit;
462 }
463
Mathias Agopian65ab4712010-07-14 17:59:35 -0700464 {
465 Mutex::Autolock _l(mLock);
466 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700467 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800469 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470 lStatus = BAD_VALUE;
471 goto Exit;
472 }
473
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800474 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800475 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476
Steve Block3856b092011-10-20 11:56:00 +0100477 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700478 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700479 // check if an effect chain with the same session ID is present on another
480 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700481 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700482 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
483 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700484 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700485 if (sessions & PlaybackThread::EFFECT_SESSION) {
486 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700487 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700488 }
Eric Laurentde070132010-07-13 04:45:46 -0700489 }
490 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491 lSessionId = *sessionId;
492 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700493 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700494 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700495 if (sessionId != NULL) {
496 *sessionId = lSessionId;
497 }
498 }
Steve Block3856b092011-10-20 11:56:00 +0100499 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700500
501 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800502 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700503
504 // move effect chain to this output thread if an effect on same session was waiting
505 // for a track to be created
506 if (lStatus == NO_ERROR && effectThread != NULL) {
507 Mutex::Autolock _dl(thread->mLock);
508 Mutex::Autolock _sl(effectThread->mLock);
509 moveEffectChain_l(lSessionId, effectThread, thread, true);
510 }
Eric Laurenta011e352012-03-29 15:51:43 -0700511
512 // Look for sync events awaiting for a session to be used.
513 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
514 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
515 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700516 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700517 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700518 } else {
519 mPendingSyncEvents[i]->cancel();
520 }
Eric Laurenta011e352012-03-29 15:51:43 -0700521 mPendingSyncEvents.removeAt(i);
522 i--;
523 }
524 }
525 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526 }
527 if (lStatus == NO_ERROR) {
528 trackHandle = new TrackHandle(track);
529 } else {
530 // remove local strong reference to Client before deleting the Track so that the Client
531 // destructor is called by the TrackBase destructor with mLock held
532 client.clear();
533 track.clear();
534 }
535
536Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700537 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538 *status = lStatus;
539 }
540 return trackHandle;
541}
542
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800543uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700544{
545 Mutex::Autolock _l(mLock);
546 PlaybackThread *thread = checkPlaybackThread_l(output);
547 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000548 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549 return 0;
550 }
551 return thread->sampleRate();
552}
553
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800554int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700555{
556 Mutex::Autolock _l(mLock);
557 PlaybackThread *thread = checkPlaybackThread_l(output);
558 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000559 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560 return 0;
561 }
562 return thread->channelCount();
563}
564
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800565audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700566{
567 Mutex::Autolock _l(mLock);
568 PlaybackThread *thread = checkPlaybackThread_l(output);
569 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000570 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800571 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572 }
573 return thread->format();
574}
575
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800576size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577{
578 Mutex::Autolock _l(mLock);
579 PlaybackThread *thread = checkPlaybackThread_l(output);
580 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000581 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 return 0;
583 }
Glenn Kasten58912562012-04-03 10:45:00 -0700584 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
585 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586 return thread->frameCount();
587}
588
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800589uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590{
591 Mutex::Autolock _l(mLock);
592 PlaybackThread *thread = checkPlaybackThread_l(output);
593 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800594 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 return 0;
596 }
597 return thread->latency();
598}
599
600status_t AudioFlinger::setMasterVolume(float value)
601{
Eric Laurenta1884f92011-08-23 08:25:03 -0700602 status_t ret = initCheck();
603 if (ret != NO_ERROR) {
604 return ret;
605 }
606
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607 // check calling permissions
608 if (!settingsAllowed()) {
609 return PERMISSION_DENIED;
610 }
611
Eric Laurenta4c5a552012-03-29 10:12:40 -0700612 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700613 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700614
John Grossmanee578c02012-07-23 17:05:46 -0700615 // Set master volume in the HALs which support it.
616 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
617 AutoMutex lock(mHardwareLock);
618 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800619
John Grossmanee578c02012-07-23 17:05:46 -0700620 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
621 if (dev->canSetMasterVolume()) {
622 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800623 }
John Grossmanee578c02012-07-23 17:05:46 -0700624 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626
John Grossmanee578c02012-07-23 17:05:46 -0700627 // Now set the master volume in each playback thread. Playback threads
628 // assigned to HALs which do not have master volume support will apply
629 // master volume during the mix operation. Threads with HALs which do
630 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800631 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700632 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633
634 return NO_ERROR;
635}
636
Glenn Kastenf78aee72012-01-04 11:00:47 -0800637status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638{
Eric Laurenta1884f92011-08-23 08:25:03 -0700639 status_t ret = initCheck();
640 if (ret != NO_ERROR) {
641 return ret;
642 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700643
644 // check calling permissions
645 if (!settingsAllowed()) {
646 return PERMISSION_DENIED;
647 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800648 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000649 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 return BAD_VALUE;
651 }
652
653 { // scope for the lock
654 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700655 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700657 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658 mHardwareStatus = AUDIO_HW_IDLE;
659 }
660
661 if (NO_ERROR == ret) {
662 Mutex::Autolock _l(mLock);
663 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800664 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700665 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 }
667
668 return ret;
669}
670
671status_t AudioFlinger::setMicMute(bool state)
672{
Eric Laurenta1884f92011-08-23 08:25:03 -0700673 status_t ret = initCheck();
674 if (ret != NO_ERROR) {
675 return ret;
676 }
677
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 // check calling permissions
679 if (!settingsAllowed()) {
680 return PERMISSION_DENIED;
681 }
682
683 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700684 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700686 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687 mHardwareStatus = AUDIO_HW_IDLE;
688 return ret;
689}
690
691bool AudioFlinger::getMicMute() const
692{
Eric Laurenta1884f92011-08-23 08:25:03 -0700693 status_t ret = initCheck();
694 if (ret != NO_ERROR) {
695 return false;
696 }
697
Dima Zavinfce7a472011-04-19 22:30:36 -0700698 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800699 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700700 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700702 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703 mHardwareStatus = AUDIO_HW_IDLE;
704 return state;
705}
706
707status_t AudioFlinger::setMasterMute(bool muted)
708{
John Grossmand8f178d2012-07-20 14:51:35 -0700709 status_t ret = initCheck();
710 if (ret != NO_ERROR) {
711 return ret;
712 }
713
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 // check calling permissions
715 if (!settingsAllowed()) {
716 return PERMISSION_DENIED;
717 }
718
John Grossmanee578c02012-07-23 17:05:46 -0700719 Mutex::Autolock _l(mLock);
720 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700721
John Grossmanee578c02012-07-23 17:05:46 -0700722 // Set master mute in the HALs which support it.
723 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
724 AutoMutex lock(mHardwareLock);
725 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700726
John Grossmanee578c02012-07-23 17:05:46 -0700727 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
728 if (dev->canSetMasterMute()) {
729 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700730 }
John Grossmanee578c02012-07-23 17:05:46 -0700731 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700732 }
733
John Grossmanee578c02012-07-23 17:05:46 -0700734 // Now set the master mute in each playback thread. Playback threads
735 // assigned to HALs which do not have master mute support will apply master
736 // mute during the mix operation. Threads with HALs which do support master
737 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800738 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700739 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740
741 return NO_ERROR;
742}
743
744float AudioFlinger::masterVolume() const
745{
Glenn Kasten98067102011-12-13 11:47:54 -0800746 Mutex::Autolock _l(mLock);
747 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748}
749
750bool AudioFlinger::masterMute() const
751{
Glenn Kasten98067102011-12-13 11:47:54 -0800752 Mutex::Autolock _l(mLock);
753 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754}
755
John Grossman4ff14ba2012-02-08 16:37:41 -0800756float AudioFlinger::masterVolume_l() const
757{
John Grossman4ff14ba2012-02-08 16:37:41 -0800758 return mMasterVolume;
759}
760
John Grossmand8f178d2012-07-20 14:51:35 -0700761bool AudioFlinger::masterMute_l() const
762{
John Grossmanee578c02012-07-23 17:05:46 -0700763 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700764}
765
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800766status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
767 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768{
769 // check calling permissions
770 if (!settingsAllowed()) {
771 return PERMISSION_DENIED;
772 }
773
Glenn Kasten263709e2012-01-06 08:40:01 -0800774 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000775 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776 return BAD_VALUE;
777 }
778
779 AutoMutex lock(mLock);
780 PlaybackThread *thread = NULL;
781 if (output) {
782 thread = checkPlaybackThread_l(output);
783 if (thread == NULL) {
784 return BAD_VALUE;
785 }
786 }
787
788 mStreamTypes[stream].volume = value;
789
790 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800791 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700792 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700793 }
794 } else {
795 thread->setStreamVolume(stream, value);
796 }
797
798 return NO_ERROR;
799}
800
Glenn Kastenfff6d712012-01-12 16:38:12 -0800801status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802{
803 // check calling permissions
804 if (!settingsAllowed()) {
805 return PERMISSION_DENIED;
806 }
807
Glenn Kasten263709e2012-01-06 08:40:01 -0800808 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700809 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000810 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811 return BAD_VALUE;
812 }
813
Eric Laurent93575202011-01-18 18:39:02 -0800814 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815 mStreamTypes[stream].mute = muted;
816 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700817 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818
819 return NO_ERROR;
820}
821
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800822float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823{
Glenn Kasten263709e2012-01-06 08:40:01 -0800824 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825 return 0.0f;
826 }
827
828 AutoMutex lock(mLock);
829 float volume;
830 if (output) {
831 PlaybackThread *thread = checkPlaybackThread_l(output);
832 if (thread == NULL) {
833 return 0.0f;
834 }
835 volume = thread->streamVolume(stream);
836 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800837 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838 }
839
840 return volume;
841}
842
Glenn Kastenfff6d712012-01-12 16:38:12 -0800843bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844{
Glenn Kasten263709e2012-01-06 08:40:01 -0800845 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 return true;
847 }
848
Glenn Kasten6637baa2012-01-09 09:40:36 -0800849 AutoMutex lock(mLock);
850 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851}
852
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800853status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700855 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
856 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800857
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 // check calling permissions
859 if (!settingsAllowed()) {
860 return PERMISSION_DENIED;
861 }
862
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863 // ioHandle == 0 means the parameters are global to the audio hardware interface
864 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700865 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700866 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800867 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700868 AutoMutex lock(mHardwareLock);
869 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
870 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
871 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
872 status_t result = dev->set_parameters(dev, keyValuePairs.string());
873 final_result = result ?: final_result;
874 }
875 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800876 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700877 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
878 AudioParameter param = AudioParameter(keyValuePairs);
879 String8 value;
880 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700881 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
882 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700883 for (size_t i = 0; i < mRecordThreads.size(); i++) {
884 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700885 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700886 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
887 // collect all of the thread's session IDs
888 KeyedVector<int, bool> ids = thread->sessionIds();
889 // suspend effects associated with those session IDs
890 for (size_t j = 0; j < ids.size(); ++j) {
891 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700892 thread->setEffectSuspended(FX_IID_AEC,
893 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700894 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700895 thread->setEffectSuspended(FX_IID_NS,
896 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700897 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700898 }
899 }
Eric Laurentbee53372011-08-29 12:42:48 -0700900 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700901 }
902 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700903 String8 screenState;
904 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
905 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800906 if (isOff != (AudioFlinger::mScreenState & 1)) {
907 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700908 }
909 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700910 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 }
912
913 // hold a strong ref on thread in case closeOutput() or closeInput() is called
914 // and the thread is exited once the lock is released
915 sp<ThreadBase> thread;
916 {
917 Mutex::Autolock _l(mLock);
918 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700919 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800921 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700922 // indicate output device change to all input threads for pre processing
923 AudioParameter param = AudioParameter(keyValuePairs);
924 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700925 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
926 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700927 for (size_t i = 0; i < mRecordThreads.size(); i++) {
928 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
929 }
930 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931 }
932 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800933 if (thread != 0) {
934 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935 }
936 return BAD_VALUE;
937}
938
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800939String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700941 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
942 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700943
Eric Laurenta4c5a552012-03-29 10:12:40 -0700944 Mutex::Autolock _l(mLock);
945
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700947 String8 out_s8;
948
Dima Zavin799a70e2011-04-18 16:57:27 -0700949 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800950 char *s;
951 {
952 AutoMutex lock(mHardwareLock);
953 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700954 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800955 s = dev->get_parameters(dev, keys.string());
956 mHardwareStatus = AUDIO_HW_IDLE;
957 }
John Grossmanef7740b2012-02-09 11:28:36 -0800958 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700959 free(s);
960 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700961 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700962 }
963
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
965 if (playbackThread != NULL) {
966 return playbackThread->getParameters(keys);
967 }
968 RecordThread *recordThread = checkRecordThread_l(ioHandle);
969 if (recordThread != NULL) {
970 return recordThread->getParameters(keys);
971 }
972 return String8("");
973}
974
Glenn Kastendd8104c2012-07-02 12:42:44 -0700975size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
976 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977{
Eric Laurenta1884f92011-08-23 08:25:03 -0700978 status_t ret = initCheck();
979 if (ret != NO_ERROR) {
980 return 0;
981 }
982
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800983 AutoMutex lock(mHardwareLock);
984 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000985 struct audio_config config;
986 memset(&config, 0, sizeof(config));
987 config.sample_rate = sampleRate;
988 config.channel_mask = channelMask;
989 config.format = format;
990
John Grossmanee578c02012-07-23 17:05:46 -0700991 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
992 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800993 mHardwareStatus = AUDIO_HW_IDLE;
994 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995}
996
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800997unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999 Mutex::Autolock _l(mLock);
1000
1001 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1002 if (recordThread != NULL) {
1003 return recordThread->getInputFramesLost();
1004 }
1005 return 0;
1006}
1007
1008status_t AudioFlinger::setVoiceVolume(float value)
1009{
Eric Laurenta1884f92011-08-23 08:25:03 -07001010 status_t ret = initCheck();
1011 if (ret != NO_ERROR) {
1012 return ret;
1013 }
1014
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015 // check calling permissions
1016 if (!settingsAllowed()) {
1017 return PERMISSION_DENIED;
1018 }
1019
1020 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001021 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001022 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001023 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001024 mHardwareStatus = AUDIO_HW_IDLE;
1025
1026 return ret;
1027}
1028
Glenn Kasten26c77552012-11-16 12:01:44 -08001029status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001030 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001031{
1032 status_t status;
1033
1034 Mutex::Autolock _l(mLock);
1035
1036 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1037 if (playbackThread != NULL) {
1038 return playbackThread->getRenderPosition(halFrames, dspFrames);
1039 }
1040
1041 return BAD_VALUE;
1042}
1043
1044void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1045{
1046
1047 Mutex::Autolock _l(mLock);
1048
Glenn Kastenbb001922012-02-03 11:10:26 -08001049 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050 if (mNotificationClients.indexOfKey(pid) < 0) {
1051 sp<NotificationClient> notificationClient = new NotificationClient(this,
1052 client,
1053 pid);
Steve Block3856b092011-10-20 11:56:00 +01001054 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001055
1056 mNotificationClients.add(pid, notificationClient);
1057
1058 sp<IBinder> binder = client->asBinder();
1059 binder->linkToDeath(notificationClient);
1060
1061 // the config change is always sent from playback or record threads to avoid deadlock
1062 // with AudioSystem::gLock
1063 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001064 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065 }
1066
1067 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001068 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001069 }
1070 }
1071}
1072
1073void AudioFlinger::removeNotificationClient(pid_t pid)
1074{
1075 Mutex::Autolock _l(mLock);
1076
Glenn Kastena3b09252012-01-20 09:19:01 -08001077 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001078
Steve Block3856b092011-10-20 11:56:00 +01001079 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001080 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001081 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001082 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001083 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001084 ALOGV(" pid %d @ %d", ref->mPid, i);
1085 if (ref->mPid == pid) {
1086 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001087 mAudioSessionRefs.removeAt(i);
1088 delete ref;
1089 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001090 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001091 } else {
1092 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001093 }
1094 }
1095 if (removed) {
1096 purgeStaleEffects_l();
1097 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001098}
1099
1100// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001101void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001102{
1103 size_t size = mNotificationClients.size();
1104 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001105 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1106 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001107 }
1108}
1109
1110// removeClient_l() must be called with AudioFlinger::mLock held
1111void AudioFlinger::removeClient_l(pid_t pid)
1112{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001113 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001114 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115 mClients.removeItem(pid);
1116}
1117
Eric Laurent717e1282012-06-29 16:36:52 -07001118// getEffectThread_l() must be called with AudioFlinger::mLock held
1119sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1120{
1121 sp<PlaybackThread> thread;
1122
1123 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1124 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1125 ALOG_ASSERT(thread == 0);
1126 thread = mPlaybackThreads.valueAt(i);
1127 }
1128 }
1129
1130 return thread;
1131}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001132
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134
1135// ----------------------------------------------------------------------------
1136
1137AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1138 : RefBase(),
1139 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001140 // 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 -07001141 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001142 mPid(pid),
1143 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001144{
1145 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1146}
1147
1148// Client destructor must be called with AudioFlinger::mLock held
1149AudioFlinger::Client::~Client()
1150{
1151 mAudioFlinger->removeClient_l(mPid);
1152}
1153
Glenn Kasten435dbe62012-01-30 10:15:48 -08001154sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155{
1156 return mMemoryDealer;
1157}
1158
John Grossman4ff14ba2012-02-08 16:37:41 -08001159// Reserve one of the limited slots for a timed audio track associated
1160// with this client
1161bool AudioFlinger::Client::reserveTimedTrack()
1162{
1163 const int kMaxTimedTracksPerClient = 4;
1164
1165 Mutex::Autolock _l(mTimedTrackLock);
1166
1167 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1168 ALOGW("can not create timed track - pid %d has exceeded the limit",
1169 mPid);
1170 return false;
1171 }
1172
1173 mTimedTrackCount++;
1174 return true;
1175}
1176
1177// Release a slot for a timed audio track
1178void AudioFlinger::Client::releaseTimedTrack()
1179{
1180 Mutex::Autolock _l(mTimedTrackLock);
1181 mTimedTrackCount--;
1182}
1183
Mathias Agopian65ab4712010-07-14 17:59:35 -07001184// ----------------------------------------------------------------------------
1185
1186AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1187 const sp<IAudioFlingerClient>& client,
1188 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001189 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001190{
1191}
1192
1193AudioFlinger::NotificationClient::~NotificationClient()
1194{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001195}
1196
1197void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1198{
1199 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001200 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201}
1202
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203
1204// ----------------------------------------------------------------------------
1205
1206sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001207 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001208 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001209 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001210 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001211 size_t frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08001212 IAudioFlinger::track_flags_t flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001213 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001214 int *sessionId,
1215 status_t *status)
1216{
1217 sp<RecordThread::RecordTrack> recordTrack;
1218 sp<RecordHandle> recordHandle;
1219 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220 status_t lStatus;
1221 RecordThread *thread;
1222 size_t inFrameCount;
1223 int lSessionId;
1224
1225 // check calling permissions
1226 if (!recordingAllowed()) {
1227 lStatus = PERMISSION_DENIED;
1228 goto Exit;
1229 }
1230
1231 // add client to list
1232 { // scope for mLock
1233 Mutex::Autolock _l(mLock);
1234 thread = checkRecordThread_l(input);
1235 if (thread == NULL) {
1236 lStatus = BAD_VALUE;
1237 goto Exit;
1238 }
1239
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001240 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001241 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001242
1243 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001244 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001245 lSessionId = *sessionId;
1246 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001247 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001248 if (sessionId != NULL) {
1249 *sessionId = lSessionId;
1250 }
1251 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001252 // create new record track.
1253 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001254 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1255 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001257 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001258 // remove local strong reference to Client before deleting the RecordTrack so that the
1259 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001260 client.clear();
1261 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262 goto Exit;
1263 }
1264
1265 // return to handle to client
1266 recordHandle = new RecordHandle(recordTrack);
1267 lStatus = NO_ERROR;
1268
1269Exit:
1270 if (status) {
1271 *status = lStatus;
1272 }
1273 return recordHandle;
1274}
1275
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001276
1277
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278// ----------------------------------------------------------------------------
1279
Eric Laurenta4c5a552012-03-29 10:12:40 -07001280audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1281{
1282 if (!settingsAllowed()) {
1283 return 0;
1284 }
1285 Mutex::Autolock _l(mLock);
1286 return loadHwModule_l(name);
1287}
1288
1289// loadHwModule_l() must be called with AudioFlinger::mLock held
1290audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1291{
1292 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1293 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1294 ALOGW("loadHwModule() module %s already loaded", name);
1295 return mAudioHwDevs.keyAt(i);
1296 }
1297 }
1298
Eric Laurenta4c5a552012-03-29 10:12:40 -07001299 audio_hw_device_t *dev;
1300
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001301 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001302 if (rc) {
1303 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1304 return 0;
1305 }
1306
1307 mHardwareStatus = AUDIO_HW_INIT;
1308 rc = dev->init_check(dev);
1309 mHardwareStatus = AUDIO_HW_IDLE;
1310 if (rc) {
1311 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1312 return 0;
1313 }
1314
John Grossmanee578c02012-07-23 17:05:46 -07001315 // Check and cache this HAL's level of support for master mute and master
1316 // volume. If this is the first HAL opened, and it supports the get
1317 // methods, use the initial values provided by the HAL as the current
1318 // master mute and volume settings.
1319
1320 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1321 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001322 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001323
1324 if (0 == mAudioHwDevs.size()) {
1325 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1326 if (NULL != dev->get_master_volume) {
1327 float mv;
1328 if (OK == dev->get_master_volume(dev, &mv)) {
1329 mMasterVolume = mv;
1330 }
1331 }
1332
1333 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1334 if (NULL != dev->get_master_mute) {
1335 bool mm;
1336 if (OK == dev->get_master_mute(dev, &mm)) {
1337 mMasterMute = mm;
1338 }
1339 }
1340 }
1341
Eric Laurenta4c5a552012-03-29 10:12:40 -07001342 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001343 if ((NULL != dev->set_master_volume) &&
1344 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1345 flags = static_cast<AudioHwDevice::Flags>(flags |
1346 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1347 }
1348
1349 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1350 if ((NULL != dev->set_master_mute) &&
1351 (OK == dev->set_master_mute(dev, mMasterMute))) {
1352 flags = static_cast<AudioHwDevice::Flags>(flags |
1353 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1354 }
1355
Eric Laurenta4c5a552012-03-29 10:12:40 -07001356 mHardwareStatus = AUDIO_HW_IDLE;
1357 }
1358
1359 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001360 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001361
1362 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001363 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001364
1365 return handle;
1366
1367}
1368
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001369// ----------------------------------------------------------------------------
1370
Glenn Kasten3b16c762012-11-14 08:44:39 -08001371uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001372{
1373 Mutex::Autolock _l(mLock);
1374 PlaybackThread *thread = primaryPlaybackThread_l();
1375 return thread != NULL ? thread->sampleRate() : 0;
1376}
1377
Glenn Kastene33054e2012-11-14 12:54:39 -08001378size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001379{
1380 Mutex::Autolock _l(mLock);
1381 PlaybackThread *thread = primaryPlaybackThread_l();
1382 return thread != NULL ? thread->frameCountHAL() : 0;
1383}
1384
1385// ----------------------------------------------------------------------------
1386
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001387status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1388{
1389 uid_t uid = IPCThreadState::self()->getCallingUid();
1390 if (uid != AID_SYSTEM) {
1391 return PERMISSION_DENIED;
1392 }
1393 Mutex::Autolock _l(mLock);
1394 if (mIsDeviceTypeKnown) {
1395 return INVALID_OPERATION;
1396 }
1397 mIsLowRamDevice = isLowRamDevice;
1398 mIsDeviceTypeKnown = true;
1399 return NO_ERROR;
1400}
1401
1402// ----------------------------------------------------------------------------
1403
Eric Laurenta4c5a552012-03-29 10:12:40 -07001404audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1405 audio_devices_t *pDevices,
1406 uint32_t *pSamplingRate,
1407 audio_format_t *pFormat,
1408 audio_channel_mask_t *pChannelMask,
1409 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001410 audio_output_flags_t flags,
1411 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412{
1413 status_t status;
1414 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001415 struct audio_config config;
1416 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1417 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1418 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1419 if (offloadInfo) {
1420 config.offload_info = *offloadInfo;
1421 }
1422
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001423 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001424 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001425
Eric Laurenta4c5a552012-03-29 10:12:40 -07001426 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
1427 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001428 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001429 config.sample_rate,
1430 config.format,
1431 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001432 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001433
1434 if (pDevices == NULL || *pDevices == 0) {
1435 return 0;
1436 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001437
Mathias Agopian65ab4712010-07-14 17:59:35 -07001438 Mutex::Autolock _l(mLock);
1439
Eric Laurenta4c5a552012-03-29 10:12:40 -07001440 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001441 if (outHwDev == NULL)
1442 return 0;
1443
John Grossmanee578c02012-07-23 17:05:46 -07001444 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001445 audio_io_handle_t id = nextUniqueId();
1446
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001447 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001448
John Grossmanee578c02012-07-23 17:05:46 -07001449 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001450 id,
1451 *pDevices,
1452 (audio_output_flags_t)flags,
1453 &config,
1454 &outStream);
1455
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001456 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001457 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, "
1458 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001459 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001460 config.sample_rate,
1461 config.format,
1462 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001463 status);
1464
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001465 if (status == NO_ERROR && outStream != NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001466 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001467
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001468 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001469 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1470 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001472 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473 } else {
1474 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001475 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001476 }
1477 mPlaybackThreads.add(id, thread);
1478
Glenn Kasten7c027242012-12-26 14:43:16 -08001479 if (pSamplingRate != NULL) {
1480 *pSamplingRate = config.sample_rate;
1481 }
1482 if (pFormat != NULL) {
1483 *pFormat = config.format;
1484 }
1485 if (pChannelMask != NULL) {
1486 *pChannelMask = config.channel_mask;
1487 }
1488 if (pLatencyMs != NULL) {
1489 *pLatencyMs = thread->latency();
1490 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001491
1492 // notify client processes of the new output creation
1493 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001494
1495 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001496 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001497 ALOGI("Using module %d has the primary audio interface", module);
1498 mPrimaryHardwareDev = outHwDev;
1499
1500 AutoMutex lock(mHardwareLock);
1501 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001502 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001503 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001504 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001505 return id;
1506 }
1507
1508 return 0;
1509}
1510
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001511audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1512 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001513{
1514 Mutex::Autolock _l(mLock);
1515 MixerThread *thread1 = checkMixerThread_l(output1);
1516 MixerThread *thread2 = checkMixerThread_l(output2);
1517
1518 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001519 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1520 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521 return 0;
1522 }
1523
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001524 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1526 thread->addOutputTrack(thread2);
1527 mPlaybackThreads.add(id, thread);
1528 // notify client processes of the new output creation
1529 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1530 return id;
1531}
1532
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001533status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001534{
Glenn Kastend96c5722012-04-25 13:44:49 -07001535 return closeOutput_nonvirtual(output);
1536}
1537
1538status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1539{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001540 // keep strong reference on the playback thread so that
1541 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001542 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543 {
1544 Mutex::Autolock _l(mLock);
1545 thread = checkPlaybackThread_l(output);
1546 if (thread == NULL) {
1547 return BAD_VALUE;
1548 }
1549
Steve Block3856b092011-10-20 11:56:00 +01001550 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001552 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001553 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001554 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001555 DuplicatingThread *dupThread =
1556 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001557 dupThread->removeOutputTrack((MixerThread *)thread.get());
1558 }
1559 }
1560 }
Glenn Kastena1117922012-01-26 10:53:32 -08001561 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562 mPlaybackThreads.removeItem(output);
1563 }
1564 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001565 // The thread entity (active unit of execution) is no longer running here,
1566 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001567
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001568 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001569 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001570 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001571 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001572 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001573 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001574 }
1575 return NO_ERROR;
1576}
1577
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001578status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001579{
1580 Mutex::Autolock _l(mLock);
1581 PlaybackThread *thread = checkPlaybackThread_l(output);
1582
1583 if (thread == NULL) {
1584 return BAD_VALUE;
1585 }
1586
Steve Block3856b092011-10-20 11:56:00 +01001587 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001588 thread->suspend();
1589
1590 return NO_ERROR;
1591}
1592
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001593status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594{
1595 Mutex::Autolock _l(mLock);
1596 PlaybackThread *thread = checkPlaybackThread_l(output);
1597
1598 if (thread == NULL) {
1599 return BAD_VALUE;
1600 }
1601
Steve Block3856b092011-10-20 11:56:00 +01001602 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001603
1604 thread->restore();
1605
1606 return NO_ERROR;
1607}
1608
Eric Laurenta4c5a552012-03-29 10:12:40 -07001609audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1610 audio_devices_t *pDevices,
1611 uint32_t *pSamplingRate,
1612 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001613 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614{
1615 status_t status;
1616 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001617 struct audio_config config;
1618 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1619 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1620 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1621
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001622 uint32_t reqSamplingRate = config.sample_rate;
1623 audio_format_t reqFormat = config.format;
1624 audio_channel_mask_t reqChannels = config.channel_mask;
1625 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001626 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001627
1628 if (pDevices == NULL || *pDevices == 0) {
1629 return 0;
1630 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001631
Mathias Agopian65ab4712010-07-14 17:59:35 -07001632 Mutex::Autolock _l(mLock);
1633
Eric Laurenta4c5a552012-03-29 10:12:40 -07001634 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001635 if (inHwDev == NULL)
1636 return 0;
1637
John Grossmanee578c02012-07-23 17:05:46 -07001638 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001639 audio_io_handle_t id = nextUniqueId();
1640
John Grossmanee578c02012-07-23 17:05:46 -07001641 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001642 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001643 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1644 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001645 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001646 config.sample_rate,
1647 config.format,
1648 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001649 status);
1650
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001651 // If the input could not be opened with the requested parameters and we can handle the
1652 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1653 // 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 -07001654 if (status == BAD_VALUE &&
1655 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1656 (config.sample_rate <= 2 * reqSamplingRate) &&
1657 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001658 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001659 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001660 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001661 }
1662
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001663 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001664
Glenn Kasten46909e72013-02-26 09:20:22 -08001665#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001666 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1667 // or (re-)create if current Pipe is idle and does not match the new format
1668 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001669 enum {
1670 TEE_SINK_NO, // don't copy input
1671 TEE_SINK_NEW, // copy input using a new pipe
1672 TEE_SINK_OLD, // copy input using an existing pipe
1673 } kind;
1674 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1675 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001676 if (!mTeeSinkInputEnabled) {
1677 kind = TEE_SINK_NO;
1678 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001679 kind = TEE_SINK_NO;
1680 } else if (mRecordTeeSink == 0) {
1681 kind = TEE_SINK_NEW;
1682 } else if (mRecordTeeSink->getStrongCount() != 1) {
1683 kind = TEE_SINK_NO;
1684 } else if (format == mRecordTeeSink->format()) {
1685 kind = TEE_SINK_OLD;
1686 } else {
1687 kind = TEE_SINK_NEW;
1688 }
1689 switch (kind) {
1690 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001691 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001692 size_t numCounterOffers = 0;
1693 const NBAIO_Format offers[1] = {format};
1694 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1695 ALOG_ASSERT(index == 0);
1696 PipeReader *pipeReader = new PipeReader(*pipe);
1697 numCounterOffers = 0;
1698 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1699 ALOG_ASSERT(index == 0);
1700 mRecordTeeSink = pipe;
1701 mRecordTeeSource = pipeReader;
1702 teeSink = pipe;
1703 }
1704 break;
1705 case TEE_SINK_OLD:
1706 teeSink = mRecordTeeSink;
1707 break;
1708 case TEE_SINK_NO:
1709 default:
1710 break;
1711 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001712#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001713
Dima Zavin799a70e2011-04-18 16:57:27 -07001714 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1715
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001716 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001717 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001718 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001719 thread = new RecordThread(this,
1720 input,
1721 reqSamplingRate,
1722 reqChannels,
1723 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001724 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001725 *pDevices
1726#ifdef TEE_SINK
1727 , teeSink
1728#endif
1729 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001730 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001731 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001732 if (pSamplingRate != NULL) {
1733 *pSamplingRate = reqSamplingRate;
1734 }
1735 if (pFormat != NULL) {
1736 *pFormat = config.format;
1737 }
1738 if (pChannelMask != NULL) {
1739 *pChannelMask = reqChannels;
1740 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001741
Mathias Agopian65ab4712010-07-14 17:59:35 -07001742 // notify client processes of the new input creation
1743 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1744 return id;
1745 }
1746
1747 return 0;
1748}
1749
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001750status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001751{
Glenn Kastend96c5722012-04-25 13:44:49 -07001752 return closeInput_nonvirtual(input);
1753}
1754
1755status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1756{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001757 // keep strong reference on the record thread so that
1758 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001759 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001760 {
1761 Mutex::Autolock _l(mLock);
1762 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001763 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001764 return BAD_VALUE;
1765 }
1766
Steve Block3856b092011-10-20 11:56:00 +01001767 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001768 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001769 mRecordThreads.removeItem(input);
1770 }
1771 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001772 // The thread entity (active unit of execution) is no longer running here,
1773 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001774
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001775 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001776 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001777 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001778 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001779 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001780
1781 return NO_ERROR;
1782}
1783
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001784status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001785{
1786 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001787 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001788
1789 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1790 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001791 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001792 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001793
1794 return NO_ERROR;
1795}
1796
1797
1798int AudioFlinger::newAudioSessionId()
1799{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001800 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801}
1802
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001803void AudioFlinger::acquireAudioSessionId(int audioSession)
1804{
1805 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001806 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001807 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001808 size_t num = mAudioSessionRefs.size();
1809 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001810 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001811 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1812 ref->mCnt++;
1813 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001814 return;
1815 }
1816 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001817 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1818 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001819}
1820
1821void AudioFlinger::releaseAudioSessionId(int audioSession)
1822{
1823 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001824 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001825 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001826 size_t num = mAudioSessionRefs.size();
1827 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001828 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001829 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1830 ref->mCnt--;
1831 ALOGV(" decremented refcount to %d", ref->mCnt);
1832 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001833 mAudioSessionRefs.removeAt(i);
1834 delete ref;
1835 purgeStaleEffects_l();
1836 }
1837 return;
1838 }
1839 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001840 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001841}
1842
1843void AudioFlinger::purgeStaleEffects_l() {
1844
Steve Block3856b092011-10-20 11:56:00 +01001845 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001846
1847 Vector< sp<EffectChain> > chains;
1848
1849 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1850 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1851 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1852 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001853 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1854 chains.push(ec);
1855 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001856 }
1857 }
1858 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1859 sp<RecordThread> t = mRecordThreads.valueAt(i);
1860 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1861 sp<EffectChain> ec = t->mEffectChains[j];
1862 chains.push(ec);
1863 }
1864 }
1865
1866 for (size_t i = 0; i < chains.size(); i++) {
1867 sp<EffectChain> ec = chains[i];
1868 int sessionid = ec->sessionId();
1869 sp<ThreadBase> t = ec->mThread.promote();
1870 if (t == 0) {
1871 continue;
1872 }
1873 size_t numsessionrefs = mAudioSessionRefs.size();
1874 bool found = false;
1875 for (size_t k = 0; k < numsessionrefs; k++) {
1876 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001877 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001878 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001879 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001880 found = true;
1881 break;
1882 }
1883 }
1884 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001885 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001886 // remove all effects from the chain
1887 while (ec->mEffects.size()) {
1888 sp<EffectModule> effect = ec->mEffects[0];
1889 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001890 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001891 if (effect->purgeHandles()) {
1892 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001893 }
1894 AudioSystem::unregisterEffect(effect->id());
1895 }
1896 }
1897 }
1898 return;
1899}
1900
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001902AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001903{
Glenn Kastena1117922012-01-26 10:53:32 -08001904 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001905}
1906
1907// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001908AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001909{
1910 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001911 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001912}
1913
1914// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001915AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001916{
Glenn Kastena1117922012-01-26 10:53:32 -08001917 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001918}
1919
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001920uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001921{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001922 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923}
1924
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001925AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001926{
1927 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1928 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001929 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001930 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001931 return thread;
1932 }
1933 }
1934 return NULL;
1935}
1936
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001937audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001938{
1939 PlaybackThread *thread = primaryPlaybackThread_l();
1940
1941 if (thread == NULL) {
1942 return 0;
1943 }
1944
Eric Laurentf1c04f92012-08-28 14:26:53 -07001945 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001946}
1947
Eric Laurenta011e352012-03-29 15:51:43 -07001948sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1949 int triggerSession,
1950 int listenerSession,
1951 sync_event_callback_t callBack,
1952 void *cookie)
1953{
1954 Mutex::Autolock _l(mLock);
1955
1956 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1957 status_t playStatus = NAME_NOT_FOUND;
1958 status_t recStatus = NAME_NOT_FOUND;
1959 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1960 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1961 if (playStatus == NO_ERROR) {
1962 return event;
1963 }
1964 }
1965 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1966 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1967 if (recStatus == NO_ERROR) {
1968 return event;
1969 }
1970 }
1971 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
1972 mPendingSyncEvents.add(event);
1973 } else {
1974 ALOGV("createSyncEvent() invalid event %d", event->type());
1975 event.clear();
1976 }
1977 return event;
1978}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001979
Mathias Agopian65ab4712010-07-14 17:59:35 -07001980// ----------------------------------------------------------------------------
1981// Effect management
1982// ----------------------------------------------------------------------------
1983
1984
Glenn Kastenf587ba52012-01-26 16:25:10 -08001985status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001986{
1987 Mutex::Autolock _l(mLock);
1988 return EffectQueryNumberEffects(numEffects);
1989}
1990
Glenn Kastenf587ba52012-01-26 16:25:10 -08001991status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001992{
1993 Mutex::Autolock _l(mLock);
1994 return EffectQueryEffect(index, descriptor);
1995}
1996
Glenn Kasten5e92a782012-01-30 07:40:52 -08001997status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08001998 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001999{
2000 Mutex::Autolock _l(mLock);
2001 return EffectGetDescriptor(pUuid, descriptor);
2002}
2003
2004
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002005sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002006 effect_descriptor_t *pDesc,
2007 const sp<IEffectClient>& effectClient,
2008 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002009 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002010 int sessionId,
2011 status_t *status,
2012 int *id,
2013 int *enabled)
2014{
2015 status_t lStatus = NO_ERROR;
2016 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002017 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002018
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002019 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002020 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002021 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002022
2023 if (pDesc == NULL) {
2024 lStatus = BAD_VALUE;
2025 goto Exit;
2026 }
2027
Eric Laurent84e9a102010-09-23 16:10:16 -07002028 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002029 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002030 lStatus = PERMISSION_DENIED;
2031 goto Exit;
2032 }
2033
Dima Zavinfce7a472011-04-19 22:30:36 -07002034 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002035 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002036 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002037 lStatus = PERMISSION_DENIED;
2038 goto Exit;
2039 }
2040
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002041 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002042 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002043 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07002044 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07002045 lStatus = BAD_VALUE;
2046 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07002047 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002048 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002049 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002050 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002051 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002052 }
2053 }
2054
Mathias Agopian65ab4712010-07-14 17:59:35 -07002055 {
2056 Mutex::Autolock _l(mLock);
2057
Mathias Agopian65ab4712010-07-14 17:59:35 -07002058
2059 if (!EffectIsNullUuid(&pDesc->uuid)) {
2060 // if uuid is specified, request effect descriptor
2061 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2062 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002063 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002064 goto Exit;
2065 }
2066 } else {
2067 // if uuid is not specified, look for an available implementation
2068 // of the required type in effect factory
2069 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002070 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002071 lStatus = BAD_VALUE;
2072 goto Exit;
2073 }
2074 uint32_t numEffects = 0;
2075 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002076 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002077 bool found = false;
2078
2079 lStatus = EffectQueryNumberEffects(&numEffects);
2080 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002081 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002082 goto Exit;
2083 }
2084 for (uint32_t i = 0; i < numEffects; i++) {
2085 lStatus = EffectQueryEffect(i, &desc);
2086 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002087 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002088 continue;
2089 }
2090 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2091 // If matching type found save effect descriptor. If the session is
2092 // 0 and the effect is not auxiliary, continue enumeration in case
2093 // an auxiliary version of this effect type is available
2094 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002095 d = desc;
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 break;
2099 }
2100 }
2101 }
2102 if (!found) {
2103 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002104 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002105 goto Exit;
2106 }
2107 // For same effect type, chose auxiliary version over insert version if
2108 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002109 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002110 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002111 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002112 }
2113 }
2114
2115 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002116 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002117 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2118 lStatus = INVALID_OPERATION;
2119 goto Exit;
2120 }
2121
Eric Laurent59255e42011-07-27 19:49:51 -07002122 // check recording permission for visualizer
2123 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2124 !recordingAllowed()) {
2125 lStatus = PERMISSION_DENIED;
2126 goto Exit;
2127 }
2128
Mathias Agopian65ab4712010-07-14 17:59:35 -07002129 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002130 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002131
2132 // If output is not specified try to find a matching audio session ID in one of the
2133 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002134 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2135 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002136 // Note: io is never 0 when creating an effect on an input
2137 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002138 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002139 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2140 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002141 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002142 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002143 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002144 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002145 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002146 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2147 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2148 io = mRecordThreads.keyAt(i);
2149 break;
2150 }
2151 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002152 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002153 // If no output thread contains the requested session ID, default to
2154 // first output. The effect chain will be moved to the correct output
2155 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002156 if (io == 0 && mPlaybackThreads.size()) {
2157 io = mPlaybackThreads.keyAt(0);
2158 }
Steve Block3856b092011-10-20 11:56:00 +01002159 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002160 }
2161 ThreadBase *thread = checkRecordThread_l(io);
2162 if (thread == NULL) {
2163 thread = checkPlaybackThread_l(io);
2164 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002165 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002166 lStatus = BAD_VALUE;
2167 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002168 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002169 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002170
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002171 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002172
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002173 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002174 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2175 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002176 if (handle != 0 && id != NULL) {
2177 *id = handle->id();
2178 }
2179 }
2180
2181Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002182 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002183 *status = lStatus;
2184 }
2185 return handle;
2186}
2187
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002188status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2189 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002190{
Steve Block3856b092011-10-20 11:56:00 +01002191 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002192 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002193 Mutex::Autolock _l(mLock);
2194 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002195 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002196 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002197 }
Eric Laurentde070132010-07-13 04:45:46 -07002198 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2199 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002200 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002201 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002202 }
Eric Laurentde070132010-07-13 04:45:46 -07002203 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2204 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002205 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002206 return BAD_VALUE;
2207 }
2208
2209 Mutex::Autolock _dl(dstThread->mLock);
2210 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002211 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002212
Mathias Agopian65ab4712010-07-14 17:59:35 -07002213 return NO_ERROR;
2214}
2215
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002216// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002217status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002218 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002219 AudioFlinger::PlaybackThread *dstThread,
2220 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002221{
Steve Block3856b092011-10-20 11:56:00 +01002222 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002223 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002224
Eric Laurent59255e42011-07-27 19:49:51 -07002225 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002226 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002227 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002228 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002229 return INVALID_OPERATION;
2230 }
2231
Eric Laurent39e94f82010-07-28 01:32:47 -07002232 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002233 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002234 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002235 // removed.
2236 srcThread->removeEffectChain_l(chain);
2237
2238 // transfer all effects one by one so that new effect chain is created on new thread with
2239 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002240 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002241 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002242 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002243 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2244 while (effect != 0) {
2245 srcThread->removeEffect_l(effect);
2246 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002247 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2248 if (effect->state() == EffectModule::ACTIVE ||
2249 effect->state() == EffectModule::STOPPING) {
2250 effect->start();
2251 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002252 // if the move request is not received from audio policy manager, the effect must be
2253 // re-registered with the new strategy and output
2254 if (dstChain == 0) {
2255 dstChain = effect->chain().promote();
2256 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002257 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002258 srcThread->addEffect_l(effect);
2259 return NO_INIT;
2260 }
2261 strategy = dstChain->strategy();
2262 }
2263 if (reRegister) {
2264 AudioSystem::unregisterEffect(effect->id());
2265 AudioSystem::registerEffect(&effect->desc(),
2266 dstOutput,
2267 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002268 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002269 effect->id());
2270 }
Eric Laurentde070132010-07-13 04:45:46 -07002271 effect = chain->getEffectFromId_l(0);
2272 }
2273
2274 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002275}
2276
Glenn Kastenda6ef132013-01-10 12:31:01 -08002277struct Entry {
2278#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2279 char mName[MAX_NAME];
2280};
2281
2282int comparEntry(const void *p1, const void *p2)
2283{
2284 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2285}
2286
Glenn Kasten46909e72013-02-26 09:20:22 -08002287#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002288void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002289{
Eric Laurent81784c32012-11-19 14:55:58 -08002290 NBAIO_Source *teeSource = source.get();
2291 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002292 // .wav rotation
2293 // There is a benign race condition if 2 threads call this simultaneously.
2294 // They would both traverse the directory, but the result would simply be
2295 // failures at unlink() which are ignored. It's also unlikely since
2296 // normally dumpsys is only done by bugreport or from the command line.
2297 char teePath[32+256];
2298 strcpy(teePath, "/data/misc/media");
2299 size_t teePathLen = strlen(teePath);
2300 DIR *dir = opendir(teePath);
2301 teePath[teePathLen++] = '/';
2302 if (dir != NULL) {
2303#define MAX_SORT 20 // number of entries to sort
2304#define MAX_KEEP 10 // number of entries to keep
2305 struct Entry entries[MAX_SORT];
2306 size_t entryCount = 0;
2307 while (entryCount < MAX_SORT) {
2308 struct dirent de;
2309 struct dirent *result = NULL;
2310 int rc = readdir_r(dir, &de, &result);
2311 if (rc != 0) {
2312 ALOGW("readdir_r failed %d", rc);
2313 break;
2314 }
2315 if (result == NULL) {
2316 break;
2317 }
2318 if (result != &de) {
2319 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2320 break;
2321 }
2322 // ignore non .wav file entries
2323 size_t nameLen = strlen(de.d_name);
2324 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2325 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2326 continue;
2327 }
2328 strcpy(entries[entryCount++].mName, de.d_name);
2329 }
2330 (void) closedir(dir);
2331 if (entryCount > MAX_KEEP) {
2332 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2333 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2334 strcpy(&teePath[teePathLen], entries[i].mName);
2335 (void) unlink(teePath);
2336 }
2337 }
2338 } else {
2339 if (fd >= 0) {
2340 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2341 }
2342 }
Eric Laurent81784c32012-11-19 14:55:58 -08002343 char teeTime[16];
2344 struct timeval tv;
2345 gettimeofday(&tv, NULL);
2346 struct tm tm;
2347 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002348 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2349 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2350 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2351 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002352 if (teeFd >= 0) {
2353 char wavHeader[44];
2354 memcpy(wavHeader,
2355 "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",
2356 sizeof(wavHeader));
2357 NBAIO_Format format = teeSource->format();
2358 unsigned channelCount = Format_channelCount(format);
2359 ALOG_ASSERT(channelCount <= FCC_2);
2360 uint32_t sampleRate = Format_sampleRate(format);
2361 wavHeader[22] = channelCount; // number of channels
2362 wavHeader[24] = sampleRate; // sample rate
2363 wavHeader[25] = sampleRate >> 8;
2364 wavHeader[32] = channelCount * 2; // block alignment
2365 write(teeFd, wavHeader, sizeof(wavHeader));
2366 size_t total = 0;
2367 bool firstRead = true;
2368 for (;;) {
2369#define TEE_SINK_READ 1024
2370 short buffer[TEE_SINK_READ * FCC_2];
2371 size_t count = TEE_SINK_READ;
2372 ssize_t actual = teeSource->read(buffer, count,
2373 AudioBufferProvider::kInvalidPTS);
2374 bool wasFirstRead = firstRead;
2375 firstRead = false;
2376 if (actual <= 0) {
2377 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2378 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002379 }
Eric Laurent81784c32012-11-19 14:55:58 -08002380 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002381 }
Eric Laurent81784c32012-11-19 14:55:58 -08002382 ALOG_ASSERT(actual <= (ssize_t)count);
2383 write(teeFd, buffer, actual * channelCount * sizeof(short));
2384 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002385 }
Eric Laurent81784c32012-11-19 14:55:58 -08002386 lseek(teeFd, (off_t) 4, SEEK_SET);
2387 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2388 write(teeFd, &temp, sizeof(temp));
2389 lseek(teeFd, (off_t) 40, SEEK_SET);
2390 temp = total * channelCount * sizeof(short);
2391 write(teeFd, &temp, sizeof(temp));
2392 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002393 if (fd >= 0) {
2394 fdprintf(fd, "tee copied to %s\n", teePath);
2395 }
Eric Laurent59255e42011-07-27 19:49:51 -07002396 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002397 if (fd >= 0) {
2398 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2399 }
Eric Laurent59255e42011-07-27 19:49:51 -07002400 }
2401 }
2402}
Glenn Kasten46909e72013-02-26 09:20:22 -08002403#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002404
Mathias Agopian65ab4712010-07-14 17:59:35 -07002405// ----------------------------------------------------------------------------
2406
2407status_t AudioFlinger::onTransact(
2408 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2409{
2410 return BnAudioFlinger::onTransact(code, data, reply, flags);
2411}
2412
Mathias Agopian65ab4712010-07-14 17:59:35 -07002413}; // namespace android