blob: 0c11f3b7c88b96719573c7fbcc6c5d09da58f8a0 [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{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001414 struct audio_config config;
1415 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1416 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1417 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1418 if (offloadInfo) {
1419 config.offload_info = *offloadInfo;
1420 }
1421
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001422 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001423 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424
Eric Laurenta4c5a552012-03-29 10:12:40 -07001425 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
1426 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001427 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001428 config.sample_rate,
1429 config.format,
1430 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001431 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432
1433 if (pDevices == NULL || *pDevices == 0) {
1434 return 0;
1435 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001436
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437 Mutex::Autolock _l(mLock);
1438
Eric Laurenta4c5a552012-03-29 10:12:40 -07001439 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001440 if (outHwDev == NULL)
1441 return 0;
1442
John Grossmanee578c02012-07-23 17:05:46 -07001443 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001444 audio_io_handle_t id = nextUniqueId();
1445
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001446 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001447
Glenn Kasten34542ac2013-06-26 11:29:02 -07001448 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001449 id,
1450 *pDevices,
1451 (audio_output_flags_t)flags,
1452 &config,
1453 &outStream);
1454
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001455 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001456 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, "
1457 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001458 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001459 config.sample_rate,
1460 config.format,
1461 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001462 status);
1463
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001464 if (status == NO_ERROR && outStream != NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001465 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001466
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001467 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001468 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1469 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001470 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001471 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001472 } else {
1473 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001474 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001475 }
1476 mPlaybackThreads.add(id, thread);
1477
Glenn Kasten7c027242012-12-26 14:43:16 -08001478 if (pSamplingRate != NULL) {
1479 *pSamplingRate = config.sample_rate;
1480 }
1481 if (pFormat != NULL) {
1482 *pFormat = config.format;
1483 }
1484 if (pChannelMask != NULL) {
1485 *pChannelMask = config.channel_mask;
1486 }
1487 if (pLatencyMs != NULL) {
1488 *pLatencyMs = thread->latency();
1489 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490
1491 // notify client processes of the new output creation
1492 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001493
1494 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001495 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001496 ALOGI("Using module %d has the primary audio interface", module);
1497 mPrimaryHardwareDev = outHwDev;
1498
1499 AutoMutex lock(mHardwareLock);
1500 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001501 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001502 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001503 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001504 return id;
1505 }
1506
1507 return 0;
1508}
1509
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001510audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1511 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512{
1513 Mutex::Autolock _l(mLock);
1514 MixerThread *thread1 = checkMixerThread_l(output1);
1515 MixerThread *thread2 = checkMixerThread_l(output2);
1516
1517 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001518 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1519 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520 return 0;
1521 }
1522
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001523 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001524 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1525 thread->addOutputTrack(thread2);
1526 mPlaybackThreads.add(id, thread);
1527 // notify client processes of the new output creation
1528 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1529 return id;
1530}
1531
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001532status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533{
Glenn Kastend96c5722012-04-25 13:44:49 -07001534 return closeOutput_nonvirtual(output);
1535}
1536
1537status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1538{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001539 // keep strong reference on the playback thread so that
1540 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001541 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542 {
1543 Mutex::Autolock _l(mLock);
1544 thread = checkPlaybackThread_l(output);
1545 if (thread == NULL) {
1546 return BAD_VALUE;
1547 }
1548
Steve Block3856b092011-10-20 11:56:00 +01001549 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001550
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001551 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001552 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001553 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001554 DuplicatingThread *dupThread =
1555 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556 dupThread->removeOutputTrack((MixerThread *)thread.get());
1557 }
1558 }
1559 }
Glenn Kastena1117922012-01-26 10:53:32 -08001560 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001561 mPlaybackThreads.removeItem(output);
1562 }
1563 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001564 // The thread entity (active unit of execution) is no longer running here,
1565 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001566
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001567 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001568 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001569 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001570 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001571 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001572 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001573 }
1574 return NO_ERROR;
1575}
1576
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001577status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001578{
1579 Mutex::Autolock _l(mLock);
1580 PlaybackThread *thread = checkPlaybackThread_l(output);
1581
1582 if (thread == NULL) {
1583 return BAD_VALUE;
1584 }
1585
Steve Block3856b092011-10-20 11:56:00 +01001586 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001587 thread->suspend();
1588
1589 return NO_ERROR;
1590}
1591
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001592status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001593{
1594 Mutex::Autolock _l(mLock);
1595 PlaybackThread *thread = checkPlaybackThread_l(output);
1596
1597 if (thread == NULL) {
1598 return BAD_VALUE;
1599 }
1600
Steve Block3856b092011-10-20 11:56:00 +01001601 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602
1603 thread->restore();
1604
1605 return NO_ERROR;
1606}
1607
Eric Laurenta4c5a552012-03-29 10:12:40 -07001608audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1609 audio_devices_t *pDevices,
1610 uint32_t *pSamplingRate,
1611 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001612 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001613{
1614 status_t status;
1615 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001616 struct audio_config config;
1617 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1618 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1619 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1620
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001621 uint32_t reqSamplingRate = config.sample_rate;
1622 audio_format_t reqFormat = config.format;
1623 audio_channel_mask_t reqChannels = config.channel_mask;
1624 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001625 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001626
1627 if (pDevices == NULL || *pDevices == 0) {
1628 return 0;
1629 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001630
Mathias Agopian65ab4712010-07-14 17:59:35 -07001631 Mutex::Autolock _l(mLock);
1632
Eric Laurenta4c5a552012-03-29 10:12:40 -07001633 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001634 if (inHwDev == NULL)
1635 return 0;
1636
John Grossmanee578c02012-07-23 17:05:46 -07001637 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001638 audio_io_handle_t id = nextUniqueId();
1639
John Grossmanee578c02012-07-23 17:05:46 -07001640 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001641 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001642 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1643 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001644 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001645 config.sample_rate,
1646 config.format,
1647 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648 status);
1649
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001650 // If the input could not be opened with the requested parameters and we can handle the
1651 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1652 // 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 -07001653 if (status == BAD_VALUE &&
1654 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1655 (config.sample_rate <= 2 * reqSamplingRate) &&
1656 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001657 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001658 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001659 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001660 }
1661
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001662 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001663
Glenn Kasten46909e72013-02-26 09:20:22 -08001664#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001665 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1666 // or (re-)create if current Pipe is idle and does not match the new format
1667 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001668 enum {
1669 TEE_SINK_NO, // don't copy input
1670 TEE_SINK_NEW, // copy input using a new pipe
1671 TEE_SINK_OLD, // copy input using an existing pipe
1672 } kind;
1673 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1674 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001675 if (!mTeeSinkInputEnabled) {
1676 kind = TEE_SINK_NO;
1677 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001678 kind = TEE_SINK_NO;
1679 } else if (mRecordTeeSink == 0) {
1680 kind = TEE_SINK_NEW;
1681 } else if (mRecordTeeSink->getStrongCount() != 1) {
1682 kind = TEE_SINK_NO;
1683 } else if (format == mRecordTeeSink->format()) {
1684 kind = TEE_SINK_OLD;
1685 } else {
1686 kind = TEE_SINK_NEW;
1687 }
1688 switch (kind) {
1689 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001690 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001691 size_t numCounterOffers = 0;
1692 const NBAIO_Format offers[1] = {format};
1693 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1694 ALOG_ASSERT(index == 0);
1695 PipeReader *pipeReader = new PipeReader(*pipe);
1696 numCounterOffers = 0;
1697 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1698 ALOG_ASSERT(index == 0);
1699 mRecordTeeSink = pipe;
1700 mRecordTeeSource = pipeReader;
1701 teeSink = pipe;
1702 }
1703 break;
1704 case TEE_SINK_OLD:
1705 teeSink = mRecordTeeSink;
1706 break;
1707 case TEE_SINK_NO:
1708 default:
1709 break;
1710 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001711#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001712
Dima Zavin799a70e2011-04-18 16:57:27 -07001713 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1714
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001715 // Start record thread
1716 // RecorThread require both input and output device indication to forward to audio
1717 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001718 thread = new RecordThread(this,
1719 input,
1720 reqSamplingRate,
1721 reqChannels,
1722 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001723 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001724 *pDevices
1725#ifdef TEE_SINK
1726 , teeSink
1727#endif
1728 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001729 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001730 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001731 if (pSamplingRate != NULL) {
1732 *pSamplingRate = reqSamplingRate;
1733 }
1734 if (pFormat != NULL) {
1735 *pFormat = config.format;
1736 }
1737 if (pChannelMask != NULL) {
1738 *pChannelMask = reqChannels;
1739 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001740
Mathias Agopian65ab4712010-07-14 17:59:35 -07001741 // notify client processes of the new input creation
1742 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1743 return id;
1744 }
1745
1746 return 0;
1747}
1748
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001749status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001750{
Glenn Kastend96c5722012-04-25 13:44:49 -07001751 return closeInput_nonvirtual(input);
1752}
1753
1754status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1755{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001756 // keep strong reference on the record thread so that
1757 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001758 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001759 {
1760 Mutex::Autolock _l(mLock);
1761 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001762 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001763 return BAD_VALUE;
1764 }
1765
Steve Block3856b092011-10-20 11:56:00 +01001766 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001767 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001768 mRecordThreads.removeItem(input);
1769 }
1770 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001771 // The thread entity (active unit of execution) is no longer running here,
1772 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001773
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001774 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001775 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001776 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001777 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001778 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001779
1780 return NO_ERROR;
1781}
1782
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001783status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001784{
1785 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001786 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001787
1788 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1789 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001790 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001791 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001792
1793 return NO_ERROR;
1794}
1795
1796
1797int AudioFlinger::newAudioSessionId()
1798{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001799 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001800}
1801
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001802void AudioFlinger::acquireAudioSessionId(int audioSession)
1803{
1804 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001805 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001806 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001807 size_t num = mAudioSessionRefs.size();
1808 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001809 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001810 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1811 ref->mCnt++;
1812 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001813 return;
1814 }
1815 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001816 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1817 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001818}
1819
1820void AudioFlinger::releaseAudioSessionId(int audioSession)
1821{
1822 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001823 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001824 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001825 size_t num = mAudioSessionRefs.size();
1826 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001827 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001828 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1829 ref->mCnt--;
1830 ALOGV(" decremented refcount to %d", ref->mCnt);
1831 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001832 mAudioSessionRefs.removeAt(i);
1833 delete ref;
1834 purgeStaleEffects_l();
1835 }
1836 return;
1837 }
1838 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001839 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001840}
1841
1842void AudioFlinger::purgeStaleEffects_l() {
1843
Steve Block3856b092011-10-20 11:56:00 +01001844 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001845
1846 Vector< sp<EffectChain> > chains;
1847
1848 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1849 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1850 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1851 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001852 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1853 chains.push(ec);
1854 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001855 }
1856 }
1857 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1858 sp<RecordThread> t = mRecordThreads.valueAt(i);
1859 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1860 sp<EffectChain> ec = t->mEffectChains[j];
1861 chains.push(ec);
1862 }
1863 }
1864
1865 for (size_t i = 0; i < chains.size(); i++) {
1866 sp<EffectChain> ec = chains[i];
1867 int sessionid = ec->sessionId();
1868 sp<ThreadBase> t = ec->mThread.promote();
1869 if (t == 0) {
1870 continue;
1871 }
1872 size_t numsessionrefs = mAudioSessionRefs.size();
1873 bool found = false;
1874 for (size_t k = 0; k < numsessionrefs; k++) {
1875 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001876 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001877 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001878 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001879 found = true;
1880 break;
1881 }
1882 }
1883 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001884 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001885 // remove all effects from the chain
1886 while (ec->mEffects.size()) {
1887 sp<EffectModule> effect = ec->mEffects[0];
1888 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001889 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001890 if (effect->purgeHandles()) {
1891 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001892 }
1893 AudioSystem::unregisterEffect(effect->id());
1894 }
1895 }
1896 }
1897 return;
1898}
1899
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001901AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001902{
Glenn Kastena1117922012-01-26 10:53:32 -08001903 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001904}
1905
1906// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001907AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001908{
1909 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001910 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001911}
1912
1913// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001914AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001915{
Glenn Kastena1117922012-01-26 10:53:32 -08001916 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001917}
1918
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001919uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001920{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001921 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001922}
1923
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001924AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001925{
1926 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1927 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001928 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001929 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001930 return thread;
1931 }
1932 }
1933 return NULL;
1934}
1935
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001936audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001937{
1938 PlaybackThread *thread = primaryPlaybackThread_l();
1939
1940 if (thread == NULL) {
1941 return 0;
1942 }
1943
Eric Laurentf1c04f92012-08-28 14:26:53 -07001944 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001945}
1946
Eric Laurenta011e352012-03-29 15:51:43 -07001947sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1948 int triggerSession,
1949 int listenerSession,
1950 sync_event_callback_t callBack,
1951 void *cookie)
1952{
1953 Mutex::Autolock _l(mLock);
1954
1955 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1956 status_t playStatus = NAME_NOT_FOUND;
1957 status_t recStatus = NAME_NOT_FOUND;
1958 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1959 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1960 if (playStatus == NO_ERROR) {
1961 return event;
1962 }
1963 }
1964 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1965 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1966 if (recStatus == NO_ERROR) {
1967 return event;
1968 }
1969 }
1970 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
1971 mPendingSyncEvents.add(event);
1972 } else {
1973 ALOGV("createSyncEvent() invalid event %d", event->type());
1974 event.clear();
1975 }
1976 return event;
1977}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001978
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979// ----------------------------------------------------------------------------
1980// Effect management
1981// ----------------------------------------------------------------------------
1982
1983
Glenn Kastenf587ba52012-01-26 16:25:10 -08001984status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985{
1986 Mutex::Autolock _l(mLock);
1987 return EffectQueryNumberEffects(numEffects);
1988}
1989
Glenn Kastenf587ba52012-01-26 16:25:10 -08001990status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001991{
1992 Mutex::Autolock _l(mLock);
1993 return EffectQueryEffect(index, descriptor);
1994}
1995
Glenn Kasten5e92a782012-01-30 07:40:52 -08001996status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08001997 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001998{
1999 Mutex::Autolock _l(mLock);
2000 return EffectGetDescriptor(pUuid, descriptor);
2001}
2002
2003
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002004sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002005 effect_descriptor_t *pDesc,
2006 const sp<IEffectClient>& effectClient,
2007 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002008 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002009 int sessionId,
2010 status_t *status,
2011 int *id,
2012 int *enabled)
2013{
2014 status_t lStatus = NO_ERROR;
2015 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002016 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002017
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002018 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002019 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002020 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002021
2022 if (pDesc == NULL) {
2023 lStatus = BAD_VALUE;
2024 goto Exit;
2025 }
2026
Eric Laurent84e9a102010-09-23 16:10:16 -07002027 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002028 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002029 lStatus = PERMISSION_DENIED;
2030 goto Exit;
2031 }
2032
Dima Zavinfce7a472011-04-19 22:30:36 -07002033 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002034 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002035 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002036 lStatus = PERMISSION_DENIED;
2037 goto Exit;
2038 }
2039
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002040 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002041 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002042 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07002043 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07002044 lStatus = BAD_VALUE;
2045 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07002046 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002047 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002048 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002049 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002050 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002051 }
2052 }
2053
Mathias Agopian65ab4712010-07-14 17:59:35 -07002054 {
2055 Mutex::Autolock _l(mLock);
2056
Mathias Agopian65ab4712010-07-14 17:59:35 -07002057
2058 if (!EffectIsNullUuid(&pDesc->uuid)) {
2059 // if uuid is specified, request effect descriptor
2060 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2061 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002062 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002063 goto Exit;
2064 }
2065 } else {
2066 // if uuid is not specified, look for an available implementation
2067 // of the required type in effect factory
2068 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002069 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002070 lStatus = BAD_VALUE;
2071 goto Exit;
2072 }
2073 uint32_t numEffects = 0;
2074 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002075 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002076 bool found = false;
2077
2078 lStatus = EffectQueryNumberEffects(&numEffects);
2079 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002080 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002081 goto Exit;
2082 }
2083 for (uint32_t i = 0; i < numEffects; i++) {
2084 lStatus = EffectQueryEffect(i, &desc);
2085 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002086 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002087 continue;
2088 }
2089 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2090 // If matching type found save effect descriptor. If the session is
2091 // 0 and the effect is not auxiliary, continue enumeration in case
2092 // an auxiliary version of this effect type is available
2093 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002094 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002095 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002096 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2097 break;
2098 }
2099 }
2100 }
2101 if (!found) {
2102 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002103 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002104 goto Exit;
2105 }
2106 // For same effect type, chose auxiliary version over insert version if
2107 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002108 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002109 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002110 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002111 }
2112 }
2113
2114 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002115 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002116 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2117 lStatus = INVALID_OPERATION;
2118 goto Exit;
2119 }
2120
Eric Laurent59255e42011-07-27 19:49:51 -07002121 // check recording permission for visualizer
2122 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2123 !recordingAllowed()) {
2124 lStatus = PERMISSION_DENIED;
2125 goto Exit;
2126 }
2127
Mathias Agopian65ab4712010-07-14 17:59:35 -07002128 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002129 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002130
2131 // If output is not specified try to find a matching audio session ID in one of the
2132 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002133 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2134 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002135 // Note: io is never 0 when creating an effect on an input
2136 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002137 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002138 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2139 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002140 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002141 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002142 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002143 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002144 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002145 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2146 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2147 io = mRecordThreads.keyAt(i);
2148 break;
2149 }
2150 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002151 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002152 // If no output thread contains the requested session ID, default to
2153 // first output. The effect chain will be moved to the correct output
2154 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002155 if (io == 0 && mPlaybackThreads.size()) {
2156 io = mPlaybackThreads.keyAt(0);
2157 }
Steve Block3856b092011-10-20 11:56:00 +01002158 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002159 }
2160 ThreadBase *thread = checkRecordThread_l(io);
2161 if (thread == NULL) {
2162 thread = checkPlaybackThread_l(io);
2163 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002164 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002165 lStatus = BAD_VALUE;
2166 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002167 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002168 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002169
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002170 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002171
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002172 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002173 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2174 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002175 if (handle != 0 && id != NULL) {
2176 *id = handle->id();
2177 }
2178 }
2179
2180Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002181 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002182 *status = lStatus;
2183 }
2184 return handle;
2185}
2186
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002187status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2188 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002189{
Steve Block3856b092011-10-20 11:56:00 +01002190 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002191 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002192 Mutex::Autolock _l(mLock);
2193 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002194 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002195 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002196 }
Eric Laurentde070132010-07-13 04:45:46 -07002197 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2198 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002199 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002200 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002201 }
Eric Laurentde070132010-07-13 04:45:46 -07002202 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2203 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002204 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002205 return BAD_VALUE;
2206 }
2207
2208 Mutex::Autolock _dl(dstThread->mLock);
2209 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002210 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002211
Mathias Agopian65ab4712010-07-14 17:59:35 -07002212 return NO_ERROR;
2213}
2214
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002215// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002216status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002217 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002218 AudioFlinger::PlaybackThread *dstThread,
2219 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002220{
Steve Block3856b092011-10-20 11:56:00 +01002221 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002222 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002223
Eric Laurent59255e42011-07-27 19:49:51 -07002224 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002225 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002226 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002227 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002228 return INVALID_OPERATION;
2229 }
2230
Eric Laurent39e94f82010-07-28 01:32:47 -07002231 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002232 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002233 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002234 // removed.
2235 srcThread->removeEffectChain_l(chain);
2236
2237 // transfer all effects one by one so that new effect chain is created on new thread with
2238 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002239 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002240 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002241 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002242 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2243 while (effect != 0) {
2244 srcThread->removeEffect_l(effect);
2245 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002246 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2247 if (effect->state() == EffectModule::ACTIVE ||
2248 effect->state() == EffectModule::STOPPING) {
2249 effect->start();
2250 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002251 // if the move request is not received from audio policy manager, the effect must be
2252 // re-registered with the new strategy and output
2253 if (dstChain == 0) {
2254 dstChain = effect->chain().promote();
2255 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002256 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002257 srcThread->addEffect_l(effect);
2258 return NO_INIT;
2259 }
2260 strategy = dstChain->strategy();
2261 }
2262 if (reRegister) {
2263 AudioSystem::unregisterEffect(effect->id());
2264 AudioSystem::registerEffect(&effect->desc(),
2265 dstOutput,
2266 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002267 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002268 effect->id());
2269 }
Eric Laurentde070132010-07-13 04:45:46 -07002270 effect = chain->getEffectFromId_l(0);
2271 }
2272
2273 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002274}
2275
Glenn Kastenda6ef132013-01-10 12:31:01 -08002276struct Entry {
2277#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2278 char mName[MAX_NAME];
2279};
2280
2281int comparEntry(const void *p1, const void *p2)
2282{
2283 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2284}
2285
Glenn Kasten46909e72013-02-26 09:20:22 -08002286#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002287void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002288{
Eric Laurent81784c32012-11-19 14:55:58 -08002289 NBAIO_Source *teeSource = source.get();
2290 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002291 // .wav rotation
2292 // There is a benign race condition if 2 threads call this simultaneously.
2293 // They would both traverse the directory, but the result would simply be
2294 // failures at unlink() which are ignored. It's also unlikely since
2295 // normally dumpsys is only done by bugreport or from the command line.
2296 char teePath[32+256];
2297 strcpy(teePath, "/data/misc/media");
2298 size_t teePathLen = strlen(teePath);
2299 DIR *dir = opendir(teePath);
2300 teePath[teePathLen++] = '/';
2301 if (dir != NULL) {
2302#define MAX_SORT 20 // number of entries to sort
2303#define MAX_KEEP 10 // number of entries to keep
2304 struct Entry entries[MAX_SORT];
2305 size_t entryCount = 0;
2306 while (entryCount < MAX_SORT) {
2307 struct dirent de;
2308 struct dirent *result = NULL;
2309 int rc = readdir_r(dir, &de, &result);
2310 if (rc != 0) {
2311 ALOGW("readdir_r failed %d", rc);
2312 break;
2313 }
2314 if (result == NULL) {
2315 break;
2316 }
2317 if (result != &de) {
2318 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2319 break;
2320 }
2321 // ignore non .wav file entries
2322 size_t nameLen = strlen(de.d_name);
2323 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2324 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2325 continue;
2326 }
2327 strcpy(entries[entryCount++].mName, de.d_name);
2328 }
2329 (void) closedir(dir);
2330 if (entryCount > MAX_KEEP) {
2331 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2332 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2333 strcpy(&teePath[teePathLen], entries[i].mName);
2334 (void) unlink(teePath);
2335 }
2336 }
2337 } else {
2338 if (fd >= 0) {
2339 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2340 }
2341 }
Eric Laurent81784c32012-11-19 14:55:58 -08002342 char teeTime[16];
2343 struct timeval tv;
2344 gettimeofday(&tv, NULL);
2345 struct tm tm;
2346 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002347 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2348 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2349 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2350 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002351 if (teeFd >= 0) {
2352 char wavHeader[44];
2353 memcpy(wavHeader,
2354 "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",
2355 sizeof(wavHeader));
2356 NBAIO_Format format = teeSource->format();
2357 unsigned channelCount = Format_channelCount(format);
2358 ALOG_ASSERT(channelCount <= FCC_2);
2359 uint32_t sampleRate = Format_sampleRate(format);
2360 wavHeader[22] = channelCount; // number of channels
2361 wavHeader[24] = sampleRate; // sample rate
2362 wavHeader[25] = sampleRate >> 8;
2363 wavHeader[32] = channelCount * 2; // block alignment
2364 write(teeFd, wavHeader, sizeof(wavHeader));
2365 size_t total = 0;
2366 bool firstRead = true;
2367 for (;;) {
2368#define TEE_SINK_READ 1024
2369 short buffer[TEE_SINK_READ * FCC_2];
2370 size_t count = TEE_SINK_READ;
2371 ssize_t actual = teeSource->read(buffer, count,
2372 AudioBufferProvider::kInvalidPTS);
2373 bool wasFirstRead = firstRead;
2374 firstRead = false;
2375 if (actual <= 0) {
2376 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2377 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002378 }
Eric Laurent81784c32012-11-19 14:55:58 -08002379 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002380 }
Eric Laurent81784c32012-11-19 14:55:58 -08002381 ALOG_ASSERT(actual <= (ssize_t)count);
2382 write(teeFd, buffer, actual * channelCount * sizeof(short));
2383 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002384 }
Eric Laurent81784c32012-11-19 14:55:58 -08002385 lseek(teeFd, (off_t) 4, SEEK_SET);
2386 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2387 write(teeFd, &temp, sizeof(temp));
2388 lseek(teeFd, (off_t) 40, SEEK_SET);
2389 temp = total * channelCount * sizeof(short);
2390 write(teeFd, &temp, sizeof(temp));
2391 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002392 if (fd >= 0) {
2393 fdprintf(fd, "tee copied to %s\n", teePath);
2394 }
Eric Laurent59255e42011-07-27 19:49:51 -07002395 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002396 if (fd >= 0) {
2397 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2398 }
Eric Laurent59255e42011-07-27 19:49:51 -07002399 }
2400 }
2401}
Glenn Kasten46909e72013-02-26 09:20:22 -08002402#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002403
Mathias Agopian65ab4712010-07-14 17:59:35 -07002404// ----------------------------------------------------------------------------
2405
2406status_t AudioFlinger::onTransact(
2407 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2408{
2409 return BnAudioFlinger::onTransact(code, data, reply, flags);
2410}
2411
Mathias Agopian65ab4712010-07-14 17:59:35 -07002412}; // namespace android