blob: 6919721b71c4c0748a4199f93229b8a8ce844a7c [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>
40
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
44#include "AudioMixer.h"
45#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080046#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070049#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070050#include <audio_effects/effect_ns.h>
51#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten3b21c502011-12-15 09:52:39 -080053#include <audio_utils/primitives.h>
54
Eric Laurentfeb0db62011-07-22 09:04:31 -070055#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080056
John Grossman4ff14ba2012-02-08 16:37:41 -080057#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070058
Glenn Kasten9e58b552013-01-18 15:09:48 -080059#include <media/IMediaLogService.h>
60
Glenn Kastenda6ef132013-01-10 12:31:01 -080061#include <media/nbaio/Pipe.h>
62#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070063#include <media/AudioParameter.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070064#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080065
Mathias Agopian65ab4712010-07-14 17:59:35 -070066// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
John Grossman1c345192012-03-27 14:00:17 -070068// Note: the following macro is used for extremely verbose logging message. In
69// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
70// 0; but one side effect of this is to turn all LOGV's as well. Some messages
71// are so verbose that we want to suppress them even when we have ALOG_ASSERT
72// turned on. Do not uncomment the #def below unless you really know what you
73// are doing and want to see all of the extremely verbose messages.
74//#define VERY_VERY_VERBOSE_LOGGING
75#ifdef VERY_VERY_VERBOSE_LOGGING
76#define ALOGVV ALOGV
77#else
78#define ALOGVV(a...) do { } while(0)
79#endif
Eric Laurentde070132010-07-13 04:45:46 -070080
Mathias Agopian65ab4712010-07-14 17:59:35 -070081namespace android {
82
Glenn Kastenec1d6b52011-12-12 09:04:45 -080083static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
84static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070085
Glenn Kasten58912562012-04-03 10:45:00 -070086
John Grossman4ff14ba2012-02-08 16:37:41 -080087nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080088
Eric Laurent81784c32012-11-19 14:55:58 -080089uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070090
Glenn Kasten46909e72013-02-26 09:20:22 -080091#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080092bool AudioFlinger::mTeeSinkInputEnabled = false;
93bool AudioFlinger::mTeeSinkOutputEnabled = false;
94bool AudioFlinger::mTeeSinkTrackEnabled = false;
95
96size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
97size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
98size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -080099#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800100
Eric Laurent813e2a72013-08-31 12:59:48 -0700101// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
102// we define a minimum time during which a global effect is considered enabled.
103static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
104
Mathias Agopian65ab4712010-07-14 17:59:35 -0700105// ----------------------------------------------------------------------------
106
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700107static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700108{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700109 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700110 int rc;
111
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700112 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
113 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
114 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
115 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700116 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700117 }
118 rc = audio_hw_device_open(mod, dev);
119 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
120 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
121 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700122 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700123 }
124 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
125 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
126 rc = BAD_VALUE;
127 goto out;
128 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700129 return 0;
130
131out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700132 *dev = NULL;
133 return rc;
134}
135
Mathias Agopian65ab4712010-07-14 17:59:35 -0700136// ----------------------------------------------------------------------------
137
138AudioFlinger::AudioFlinger()
139 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800140 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700141 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800142 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800143 mMasterMute(false),
144 mNextUniqueId(1),
145 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700146 mBtNrecIsOff(false),
147 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700148 mIsDeviceTypeKnown(false),
149 mGlobalEffectEnableTime(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700150{
Glenn Kasten949a9262013-04-16 12:35:20 -0700151 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800152 char value[PROPERTY_VALUE_MAX];
153 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
154 if (doLog) {
155 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
156 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800157#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800158 (void) property_get("ro.debuggable", value, "0");
159 int debuggable = atoi(value);
160 int teeEnabled = 0;
161 if (debuggable) {
162 (void) property_get("af.tee", value, "0");
163 teeEnabled = atoi(value);
164 }
165 if (teeEnabled & 1)
166 mTeeSinkInputEnabled = true;
167 if (teeEnabled & 2)
168 mTeeSinkOutputEnabled = true;
169 if (teeEnabled & 4)
170 mTeeSinkTrackEnabled = true;
Glenn Kasten46909e72013-02-26 09:20:22 -0800171#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700172}
173
174void AudioFlinger::onFirstRef()
175{
Dima Zavin799a70e2011-04-18 16:57:27 -0700176 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700177
Eric Laurent93575202011-01-18 18:39:02 -0800178 Mutex::Autolock _l(mLock);
179
Dima Zavin799a70e2011-04-18 16:57:27 -0700180 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800181 char val_str[PROPERTY_VALUE_MAX] = { 0 };
182 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
183 uint32_t int_val;
184 if (1 == sscanf(val_str, "%u", &int_val)) {
185 mStandbyTimeInNsecs = milliseconds(int_val);
186 ALOGI("Using %u mSec as standby time.", int_val);
187 } else {
188 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
189 ALOGI("Using default %u mSec as standby time.",
190 (uint32_t)(mStandbyTimeInNsecs / 1000000));
191 }
192 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193
Eric Laurenta4c5a552012-03-29 10:12:40 -0700194 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195}
196
197AudioFlinger::~AudioFlinger()
198{
199 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700200 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700201 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 }
203 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700204 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700205 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700206 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700207
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800208 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
209 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700210 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
211 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212 }
213}
214
Eric Laurenta4c5a552012-03-29 10:12:40 -0700215static const char * const audio_interfaces[] = {
216 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
217 AUDIO_HARDWARE_MODULE_ID_A2DP,
218 AUDIO_HARDWARE_MODULE_ID_USB,
219};
220#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
221
John Grossmanee578c02012-07-23 17:05:46 -0700222AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
223 audio_module_handle_t module,
224 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700225{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700226 // if module is 0, the request comes from an old policy manager and we should load
227 // well known modules
228 if (module == 0) {
229 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
230 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
231 loadHwModule_l(audio_interfaces[i]);
232 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700233 // then try to find a module supporting the requested device.
234 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
235 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
236 audio_hw_device_t *dev = audioHwDevice->hwDevice();
237 if ((dev->get_supported_devices != NULL) &&
238 (dev->get_supported_devices(dev) & devices) == devices)
239 return audioHwDevice;
240 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700241 } else {
242 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700243 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
244 if (audioHwDevice != NULL) {
245 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700246 }
247 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700248
Dima Zavin799a70e2011-04-18 16:57:27 -0700249 return NULL;
250}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700252void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253{
254 const size_t SIZE = 256;
255 char buffer[SIZE];
256 String8 result;
257
258 result.append("Clients:\n");
259 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800260 sp<Client> client = mClients.valueAt(i).promote();
261 if (client != 0) {
262 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
263 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264 }
265 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700266
Eric Laurentd1b28d42013-09-18 18:47:13 -0700267 result.append("Notification Clients:\n");
268 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
269 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
270 result.append(buffer);
271 }
272
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700273 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800274 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700275 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
276 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800277 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700278 result.append(buffer);
279 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281}
282
283
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700284void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285{
286 const size_t SIZE = 256;
287 char buffer[SIZE];
288 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800289 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700290
John Grossman4ff14ba2012-02-08 16:37:41 -0800291 snprintf(buffer, SIZE, "Hardware status: %d\n"
292 "Standby Time mSec: %u\n",
293 hardwareStatus,
294 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295 result.append(buffer);
296 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700297}
298
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700299void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300{
301 const size_t SIZE = 256;
302 char buffer[SIZE];
303 String8 result;
304 snprintf(buffer, SIZE, "Permission Denial: "
305 "can't dump AudioFlinger from pid=%d, uid=%d\n",
306 IPCThreadState::self()->getCallingPid(),
307 IPCThreadState::self()->getCallingUid());
308 result.append(buffer);
309 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310}
311
Eric Laurent81784c32012-11-19 14:55:58 -0800312bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313{
314 bool locked = false;
315 for (int i = 0; i < kDumpLockRetries; ++i) {
316 if (mutex.tryLock() == NO_ERROR) {
317 locked = true;
318 break;
319 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800320 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700321 }
322 return locked;
323}
324
325status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
326{
Glenn Kasten44deb052012-02-05 18:09:08 -0800327 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328 dumpPermissionDenial(fd, args);
329 } else {
330 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800331 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332 if (!hardwareLocked) {
333 String8 result(kHardwareLockedString);
334 write(fd, result.string(), result.size());
335 } else {
336 mHardwareLock.unlock();
337 }
338
Eric Laurent81784c32012-11-19 14:55:58 -0800339 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700340
341 // failed to lock - AudioFlinger is probably deadlocked
342 if (!locked) {
343 String8 result(kDeadlockedString);
344 write(fd, result.string(), result.size());
345 }
346
347 dumpClients(fd, args);
348 dumpInternals(fd, args);
349
350 // dump playback threads
351 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
352 mPlaybackThreads.valueAt(i)->dump(fd, args);
353 }
354
355 // dump record threads
356 for (size_t i = 0; i < mRecordThreads.size(); i++) {
357 mRecordThreads.valueAt(i)->dump(fd, args);
358 }
359
Dima Zavin799a70e2011-04-18 16:57:27 -0700360 // dump all hardware devs
361 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700362 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700363 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700365
Glenn Kasten46909e72013-02-26 09:20:22 -0800366#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700367 // dump the serially shared record tee sink
368 if (mRecordTeeSource != 0) {
369 dumpTee(fd, mRecordTeeSource);
370 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800371#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700372
Glenn Kastend65d73c2012-06-22 17:21:07 -0700373 if (locked) {
374 mLock.unlock();
375 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800376
377 // append a copy of media.log here by forwarding fd to it, but don't attempt
378 // to lookup the service if it's not running, as it will block for a second
379 if (mLogMemoryDealer != 0) {
380 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
381 if (binder != 0) {
382 fdprintf(fd, "\nmedia.log:\n");
383 Vector<String16> args;
384 binder->dump(fd, args);
385 }
386 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387 }
388 return NO_ERROR;
389}
390
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800391sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
392{
393 // If pid is already in the mClients wp<> map, then use that entry
394 // (for which promote() is always != 0), otherwise create a new entry and Client.
395 sp<Client> client = mClients.valueFor(pid).promote();
396 if (client == 0) {
397 client = new Client(this, pid);
398 mClients.add(pid, client);
399 }
400
401 return client;
402}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403
Glenn Kasten9e58b552013-01-18 15:09:48 -0800404sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
405{
406 if (mLogMemoryDealer == 0) {
407 return new NBLog::Writer();
408 }
409 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
410 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
411 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
412 if (binder != 0) {
413 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
414 }
415 return writer;
416}
417
418void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
419{
Glenn Kasten685ef092013-02-04 08:15:34 -0800420 if (writer == 0) {
421 return;
422 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800423 sp<IMemory> iMemory(writer->getIMemory());
424 if (iMemory == 0) {
425 return;
426 }
427 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
428 if (binder != 0) {
429 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
430 // Now the media.log remote reference to IMemory is gone.
431 // When our last local reference to IMemory also drops to zero,
432 // the IMemory destructor will deallocate the region from mMemoryDealer.
433 }
434}
435
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436// IAudioFlinger interface
437
438
439sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800440 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800442 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700443 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800444 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800445 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700446 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800447 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800448 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700449 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700450 String8& name,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700451 status_t *status)
452{
453 sp<PlaybackThread::Track> track;
454 sp<TrackHandle> trackHandle;
455 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700456 status_t lStatus;
457 int lSessionId;
458
Glenn Kasten263709e2012-01-06 08:40:01 -0800459 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
460 // but if someone uses binder directly they could bypass that and cause us to crash
461 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000462 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463 lStatus = BAD_VALUE;
464 goto Exit;
465 }
466
Glenn Kasten60a83922012-06-21 12:56:37 -0700467 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
468 // and we don't yet support 8.24 or 32-bit PCM
469 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
470 ALOGE("createTrack() invalid format %d", format);
471 lStatus = BAD_VALUE;
472 goto Exit;
473 }
474
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475 {
476 Mutex::Autolock _l(mLock);
477 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700478 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700479 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800480 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700481 lStatus = BAD_VALUE;
482 goto Exit;
483 }
484
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800485 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800486 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700487
Steve Block3856b092011-10-20 11:56:00 +0100488 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700489 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700490 // check if an effect chain with the same session ID is present on another
491 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700492 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700493 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
494 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700495 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700496 if (sessions & PlaybackThread::EFFECT_SESSION) {
497 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700498 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700499 }
Eric Laurentde070132010-07-13 04:45:46 -0700500 }
501 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700502 lSessionId = *sessionId;
503 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700504 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700505 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700506 if (sessionId != NULL) {
507 *sessionId = lSessionId;
508 }
509 }
Steve Block3856b092011-10-20 11:56:00 +0100510 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511
512 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800513 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700514
515 // move effect chain to this output thread if an effect on same session was waiting
516 // for a track to be created
517 if (lStatus == NO_ERROR && effectThread != NULL) {
518 Mutex::Autolock _dl(thread->mLock);
519 Mutex::Autolock _sl(effectThread->mLock);
520 moveEffectChain_l(lSessionId, effectThread, thread, true);
521 }
Eric Laurenta011e352012-03-29 15:51:43 -0700522
523 // Look for sync events awaiting for a session to be used.
524 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
525 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
526 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700527 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700528 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700529 } else {
530 mPendingSyncEvents[i]->cancel();
531 }
Eric Laurenta011e352012-03-29 15:51:43 -0700532 mPendingSyncEvents.removeAt(i);
533 i--;
534 }
535 }
536 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537 }
538 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700539 // s for server's pid, n for normal mixer name, f for fast index
540 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
541 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700542 trackHandle = new TrackHandle(track);
543 } else {
544 // remove local strong reference to Client before deleting the Track so that the Client
545 // destructor is called by the TrackBase destructor with mLock held
546 client.clear();
547 track.clear();
548 }
549
550Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700551 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552 *status = lStatus;
553 }
554 return trackHandle;
555}
556
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800557uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558{
559 Mutex::Autolock _l(mLock);
560 PlaybackThread *thread = checkPlaybackThread_l(output);
561 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000562 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563 return 0;
564 }
565 return thread->sampleRate();
566}
567
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800568int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569{
570 Mutex::Autolock _l(mLock);
571 PlaybackThread *thread = checkPlaybackThread_l(output);
572 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000573 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574 return 0;
575 }
576 return thread->channelCount();
577}
578
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800579audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580{
581 Mutex::Autolock _l(mLock);
582 PlaybackThread *thread = checkPlaybackThread_l(output);
583 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000584 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800585 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586 }
587 return thread->format();
588}
589
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800590size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591{
592 Mutex::Autolock _l(mLock);
593 PlaybackThread *thread = checkPlaybackThread_l(output);
594 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000595 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 return 0;
597 }
Glenn Kasten58912562012-04-03 10:45:00 -0700598 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
599 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600 return thread->frameCount();
601}
602
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800603uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604{
605 Mutex::Autolock _l(mLock);
606 PlaybackThread *thread = checkPlaybackThread_l(output);
607 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800608 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609 return 0;
610 }
611 return thread->latency();
612}
613
614status_t AudioFlinger::setMasterVolume(float value)
615{
Eric Laurenta1884f92011-08-23 08:25:03 -0700616 status_t ret = initCheck();
617 if (ret != NO_ERROR) {
618 return ret;
619 }
620
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 // check calling permissions
622 if (!settingsAllowed()) {
623 return PERMISSION_DENIED;
624 }
625
Eric Laurenta4c5a552012-03-29 10:12:40 -0700626 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700627 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700628
John Grossmanee578c02012-07-23 17:05:46 -0700629 // Set master volume in the HALs which support it.
630 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
631 AutoMutex lock(mHardwareLock);
632 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800633
John Grossmanee578c02012-07-23 17:05:46 -0700634 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
635 if (dev->canSetMasterVolume()) {
636 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800637 }
John Grossmanee578c02012-07-23 17:05:46 -0700638 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640
John Grossmanee578c02012-07-23 17:05:46 -0700641 // Now set the master volume in each playback thread. Playback threads
642 // assigned to HALs which do not have master volume support will apply
643 // master volume during the mix operation. Threads with HALs which do
644 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800645 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700646 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647
648 return NO_ERROR;
649}
650
Glenn Kastenf78aee72012-01-04 11:00:47 -0800651status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652{
Eric Laurenta1884f92011-08-23 08:25:03 -0700653 status_t ret = initCheck();
654 if (ret != NO_ERROR) {
655 return ret;
656 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657
658 // check calling permissions
659 if (!settingsAllowed()) {
660 return PERMISSION_DENIED;
661 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800662 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000663 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664 return BAD_VALUE;
665 }
666
667 { // scope for the lock
668 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700669 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700671 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 mHardwareStatus = AUDIO_HW_IDLE;
673 }
674
675 if (NO_ERROR == ret) {
676 Mutex::Autolock _l(mLock);
677 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800678 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700679 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680 }
681
682 return ret;
683}
684
685status_t AudioFlinger::setMicMute(bool state)
686{
Eric Laurenta1884f92011-08-23 08:25:03 -0700687 status_t ret = initCheck();
688 if (ret != NO_ERROR) {
689 return ret;
690 }
691
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692 // check calling permissions
693 if (!settingsAllowed()) {
694 return PERMISSION_DENIED;
695 }
696
697 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700698 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700700 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 mHardwareStatus = AUDIO_HW_IDLE;
702 return ret;
703}
704
705bool AudioFlinger::getMicMute() const
706{
Eric Laurenta1884f92011-08-23 08:25:03 -0700707 status_t ret = initCheck();
708 if (ret != NO_ERROR) {
709 return false;
710 }
711
Dima Zavinfce7a472011-04-19 22:30:36 -0700712 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800713 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700714 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700716 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717 mHardwareStatus = AUDIO_HW_IDLE;
718 return state;
719}
720
721status_t AudioFlinger::setMasterMute(bool muted)
722{
John Grossmand8f178d2012-07-20 14:51:35 -0700723 status_t ret = initCheck();
724 if (ret != NO_ERROR) {
725 return ret;
726 }
727
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728 // check calling permissions
729 if (!settingsAllowed()) {
730 return PERMISSION_DENIED;
731 }
732
John Grossmanee578c02012-07-23 17:05:46 -0700733 Mutex::Autolock _l(mLock);
734 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700735
John Grossmanee578c02012-07-23 17:05:46 -0700736 // Set master mute in the HALs which support it.
737 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
738 AutoMutex lock(mHardwareLock);
739 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700740
John Grossmanee578c02012-07-23 17:05:46 -0700741 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
742 if (dev->canSetMasterMute()) {
743 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700744 }
John Grossmanee578c02012-07-23 17:05:46 -0700745 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700746 }
747
John Grossmanee578c02012-07-23 17:05:46 -0700748 // Now set the master mute in each playback thread. Playback threads
749 // assigned to HALs which do not have master mute support will apply master
750 // mute during the mix operation. Threads with HALs which do support master
751 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800752 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700753 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754
755 return NO_ERROR;
756}
757
758float AudioFlinger::masterVolume() const
759{
Glenn Kasten98067102011-12-13 11:47:54 -0800760 Mutex::Autolock _l(mLock);
761 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700762}
763
764bool AudioFlinger::masterMute() const
765{
Glenn Kasten98067102011-12-13 11:47:54 -0800766 Mutex::Autolock _l(mLock);
767 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768}
769
John Grossman4ff14ba2012-02-08 16:37:41 -0800770float AudioFlinger::masterVolume_l() const
771{
John Grossman4ff14ba2012-02-08 16:37:41 -0800772 return mMasterVolume;
773}
774
John Grossmand8f178d2012-07-20 14:51:35 -0700775bool AudioFlinger::masterMute_l() const
776{
John Grossmanee578c02012-07-23 17:05:46 -0700777 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700778}
779
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800780status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
781 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782{
783 // check calling permissions
784 if (!settingsAllowed()) {
785 return PERMISSION_DENIED;
786 }
787
Glenn Kasten263709e2012-01-06 08:40:01 -0800788 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000789 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700790 return BAD_VALUE;
791 }
792
793 AutoMutex lock(mLock);
794 PlaybackThread *thread = NULL;
795 if (output) {
796 thread = checkPlaybackThread_l(output);
797 if (thread == NULL) {
798 return BAD_VALUE;
799 }
800 }
801
802 mStreamTypes[stream].volume = value;
803
804 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800805 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700806 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807 }
808 } else {
809 thread->setStreamVolume(stream, value);
810 }
811
812 return NO_ERROR;
813}
814
Glenn Kastenfff6d712012-01-12 16:38:12 -0800815status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816{
817 // check calling permissions
818 if (!settingsAllowed()) {
819 return PERMISSION_DENIED;
820 }
821
Glenn Kasten263709e2012-01-06 08:40:01 -0800822 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700823 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000824 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825 return BAD_VALUE;
826 }
827
Eric Laurent93575202011-01-18 18:39:02 -0800828 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700829 mStreamTypes[stream].mute = muted;
830 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700831 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832
833 return NO_ERROR;
834}
835
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800836float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837{
Glenn Kasten263709e2012-01-06 08:40:01 -0800838 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839 return 0.0f;
840 }
841
842 AutoMutex lock(mLock);
843 float volume;
844 if (output) {
845 PlaybackThread *thread = checkPlaybackThread_l(output);
846 if (thread == NULL) {
847 return 0.0f;
848 }
849 volume = thread->streamVolume(stream);
850 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800851 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852 }
853
854 return volume;
855}
856
Glenn Kastenfff6d712012-01-12 16:38:12 -0800857bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858{
Glenn Kasten263709e2012-01-06 08:40:01 -0800859 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860 return true;
861 }
862
Glenn Kasten6637baa2012-01-09 09:40:36 -0800863 AutoMutex lock(mLock);
864 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865}
866
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800867status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700868{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700869 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
870 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800871
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 // check calling permissions
873 if (!settingsAllowed()) {
874 return PERMISSION_DENIED;
875 }
876
Mathias Agopian65ab4712010-07-14 17:59:35 -0700877 // ioHandle == 0 means the parameters are global to the audio hardware interface
878 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700879 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700880 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800881 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700882 AutoMutex lock(mHardwareLock);
883 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
884 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
885 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
886 status_t result = dev->set_parameters(dev, keyValuePairs.string());
887 final_result = result ?: final_result;
888 }
889 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800890 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700891 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
892 AudioParameter param = AudioParameter(keyValuePairs);
893 String8 value;
894 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700895 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
896 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700897 for (size_t i = 0; i < mRecordThreads.size(); i++) {
898 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700899 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700900 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
901 // collect all of the thread's session IDs
902 KeyedVector<int, bool> ids = thread->sessionIds();
903 // suspend effects associated with those session IDs
904 for (size_t j = 0; j < ids.size(); ++j) {
905 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700906 thread->setEffectSuspended(FX_IID_AEC,
907 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700908 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700909 thread->setEffectSuspended(FX_IID_NS,
910 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700911 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700912 }
913 }
Eric Laurentbee53372011-08-29 12:42:48 -0700914 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700915 }
916 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700917 String8 screenState;
918 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
919 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800920 if (isOff != (AudioFlinger::mScreenState & 1)) {
921 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700922 }
923 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700924 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925 }
926
927 // hold a strong ref on thread in case closeOutput() or closeInput() is called
928 // and the thread is exited once the lock is released
929 sp<ThreadBase> thread;
930 {
931 Mutex::Autolock _l(mLock);
932 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700933 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800935 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700936 // indicate output device change to all input threads for pre processing
937 AudioParameter param = AudioParameter(keyValuePairs);
938 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700939 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
940 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700941 for (size_t i = 0; i < mRecordThreads.size(); i++) {
942 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
943 }
944 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945 }
946 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800947 if (thread != 0) {
948 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949 }
950 return BAD_VALUE;
951}
952
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800953String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700955 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
956 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957
Eric Laurenta4c5a552012-03-29 10:12:40 -0700958 Mutex::Autolock _l(mLock);
959
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700961 String8 out_s8;
962
Dima Zavin799a70e2011-04-18 16:57:27 -0700963 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800964 char *s;
965 {
966 AutoMutex lock(mHardwareLock);
967 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700968 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800969 s = dev->get_parameters(dev, keys.string());
970 mHardwareStatus = AUDIO_HW_IDLE;
971 }
John Grossmanef7740b2012-02-09 11:28:36 -0800972 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700973 free(s);
974 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700975 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700976 }
977
Mathias Agopian65ab4712010-07-14 17:59:35 -0700978 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
979 if (playbackThread != NULL) {
980 return playbackThread->getParameters(keys);
981 }
982 RecordThread *recordThread = checkRecordThread_l(ioHandle);
983 if (recordThread != NULL) {
984 return recordThread->getParameters(keys);
985 }
986 return String8("");
987}
988
Glenn Kastendd8104c2012-07-02 12:42:44 -0700989size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
990 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991{
Eric Laurenta1884f92011-08-23 08:25:03 -0700992 status_t ret = initCheck();
993 if (ret != NO_ERROR) {
994 return 0;
995 }
996
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800997 AutoMutex lock(mHardwareLock);
998 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000999 struct audio_config config;
1000 memset(&config, 0, sizeof(config));
1001 config.sample_rate = sampleRate;
1002 config.channel_mask = channelMask;
1003 config.format = format;
1004
John Grossmanee578c02012-07-23 17:05:46 -07001005 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1006 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001007 mHardwareStatus = AUDIO_HW_IDLE;
1008 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001009}
1010
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001011unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001013 Mutex::Autolock _l(mLock);
1014
1015 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1016 if (recordThread != NULL) {
1017 return recordThread->getInputFramesLost();
1018 }
1019 return 0;
1020}
1021
1022status_t AudioFlinger::setVoiceVolume(float value)
1023{
Eric Laurenta1884f92011-08-23 08:25:03 -07001024 status_t ret = initCheck();
1025 if (ret != NO_ERROR) {
1026 return ret;
1027 }
1028
Mathias Agopian65ab4712010-07-14 17:59:35 -07001029 // check calling permissions
1030 if (!settingsAllowed()) {
1031 return PERMISSION_DENIED;
1032 }
1033
1034 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001035 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001036 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001037 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001038 mHardwareStatus = AUDIO_HW_IDLE;
1039
1040 return ret;
1041}
1042
Glenn Kasten26c77552012-11-16 12:01:44 -08001043status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001044 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045{
1046 status_t status;
1047
1048 Mutex::Autolock _l(mLock);
1049
1050 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1051 if (playbackThread != NULL) {
1052 return playbackThread->getRenderPosition(halFrames, dspFrames);
1053 }
1054
1055 return BAD_VALUE;
1056}
1057
1058void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1059{
1060
1061 Mutex::Autolock _l(mLock);
1062
Glenn Kastenbb001922012-02-03 11:10:26 -08001063 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064 if (mNotificationClients.indexOfKey(pid) < 0) {
1065 sp<NotificationClient> notificationClient = new NotificationClient(this,
1066 client,
1067 pid);
Steve Block3856b092011-10-20 11:56:00 +01001068 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001069
1070 mNotificationClients.add(pid, notificationClient);
1071
1072 sp<IBinder> binder = client->asBinder();
1073 binder->linkToDeath(notificationClient);
1074
1075 // the config change is always sent from playback or record threads to avoid deadlock
1076 // with AudioSystem::gLock
1077 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001078 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079 }
1080
1081 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001082 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001083 }
1084 }
1085}
1086
1087void AudioFlinger::removeNotificationClient(pid_t pid)
1088{
1089 Mutex::Autolock _l(mLock);
1090
Glenn Kastena3b09252012-01-20 09:19:01 -08001091 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001092
Steve Block3856b092011-10-20 11:56:00 +01001093 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001094 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001095 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001096 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001097 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001098 ALOGV(" pid %d @ %d", ref->mPid, i);
1099 if (ref->mPid == pid) {
1100 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001101 mAudioSessionRefs.removeAt(i);
1102 delete ref;
1103 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001104 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001105 } else {
1106 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001107 }
1108 }
1109 if (removed) {
1110 purgeStaleEffects_l();
1111 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112}
1113
1114// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001115void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001116{
1117 size_t size = mNotificationClients.size();
1118 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001119 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1120 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121 }
1122}
1123
1124// removeClient_l() must be called with AudioFlinger::mLock held
1125void AudioFlinger::removeClient_l(pid_t pid)
1126{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001127 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001128 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129 mClients.removeItem(pid);
1130}
1131
Eric Laurent717e1282012-06-29 16:36:52 -07001132// getEffectThread_l() must be called with AudioFlinger::mLock held
1133sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1134{
1135 sp<PlaybackThread> thread;
1136
1137 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1138 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1139 ALOG_ASSERT(thread == 0);
1140 thread = mPlaybackThreads.valueAt(i);
1141 }
1142 }
1143
1144 return thread;
1145}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146
Mathias Agopian65ab4712010-07-14 17:59:35 -07001147
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148
1149// ----------------------------------------------------------------------------
1150
1151AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1152 : RefBase(),
1153 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001154 // 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 -07001155 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001156 mPid(pid),
1157 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158{
1159 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1160}
1161
1162// Client destructor must be called with AudioFlinger::mLock held
1163AudioFlinger::Client::~Client()
1164{
1165 mAudioFlinger->removeClient_l(mPid);
1166}
1167
Glenn Kasten435dbe62012-01-30 10:15:48 -08001168sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001169{
1170 return mMemoryDealer;
1171}
1172
John Grossman4ff14ba2012-02-08 16:37:41 -08001173// Reserve one of the limited slots for a timed audio track associated
1174// with this client
1175bool AudioFlinger::Client::reserveTimedTrack()
1176{
1177 const int kMaxTimedTracksPerClient = 4;
1178
1179 Mutex::Autolock _l(mTimedTrackLock);
1180
1181 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1182 ALOGW("can not create timed track - pid %d has exceeded the limit",
1183 mPid);
1184 return false;
1185 }
1186
1187 mTimedTrackCount++;
1188 return true;
1189}
1190
1191// Release a slot for a timed audio track
1192void AudioFlinger::Client::releaseTimedTrack()
1193{
1194 Mutex::Autolock _l(mTimedTrackLock);
1195 mTimedTrackCount--;
1196}
1197
Mathias Agopian65ab4712010-07-14 17:59:35 -07001198// ----------------------------------------------------------------------------
1199
1200AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1201 const sp<IAudioFlingerClient>& client,
1202 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001203 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204{
1205}
1206
1207AudioFlinger::NotificationClient::~NotificationClient()
1208{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001209}
1210
1211void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1212{
1213 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001214 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001215}
1216
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217
1218// ----------------------------------------------------------------------------
1219
Jeff Brown893a5642013-08-16 20:19:26 -07001220static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1221 return audio_is_remote_submix_device(inDevice);
1222}
1223
Mathias Agopian65ab4712010-07-14 17:59:35 -07001224sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001225 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001227 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001228 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001229 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001230 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001231 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001232 int *sessionId,
1233 status_t *status)
1234{
1235 sp<RecordThread::RecordTrack> recordTrack;
1236 sp<RecordHandle> recordHandle;
1237 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238 status_t lStatus;
1239 RecordThread *thread;
1240 size_t inFrameCount;
1241 int lSessionId;
1242
1243 // check calling permissions
1244 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001245 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001246 lStatus = PERMISSION_DENIED;
1247 goto Exit;
1248 }
1249
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001250 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1251 ALOGE("openRecord() invalid format %d", format);
1252 lStatus = BAD_VALUE;
1253 goto Exit;
1254 }
1255
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256 // add client to list
1257 { // scope for mLock
1258 Mutex::Autolock _l(mLock);
1259 thread = checkRecordThread_l(input);
1260 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001261 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262 lStatus = BAD_VALUE;
1263 goto Exit;
1264 }
1265
Jeff Brown893a5642013-08-16 20:19:26 -07001266 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1267 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001268 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001269 lStatus = PERMISSION_DENIED;
1270 goto Exit;
1271 }
1272
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001273 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001274 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275
1276 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001277 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278 lSessionId = *sessionId;
1279 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001280 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001281 if (sessionId != NULL) {
1282 *sessionId = lSessionId;
1283 }
1284 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001285 // create new record track.
1286 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001287 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kastenddb0ccf2013-07-31 16:14:50 -07001288 frameCount, lSessionId, flags, tid, &lStatus);
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001289 LOG_ALWAYS_FATAL_IF((recordTrack != 0) != (lStatus == NO_ERROR));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001290 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001291 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001292 // remove local strong reference to Client before deleting the RecordTrack so that the
1293 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001294 client.clear();
1295 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001296 goto Exit;
1297 }
1298
1299 // return to handle to client
1300 recordHandle = new RecordHandle(recordTrack);
1301 lStatus = NO_ERROR;
1302
1303Exit:
1304 if (status) {
1305 *status = lStatus;
1306 }
1307 return recordHandle;
1308}
1309
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001310
1311
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312// ----------------------------------------------------------------------------
1313
Eric Laurenta4c5a552012-03-29 10:12:40 -07001314audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1315{
1316 if (!settingsAllowed()) {
1317 return 0;
1318 }
1319 Mutex::Autolock _l(mLock);
1320 return loadHwModule_l(name);
1321}
1322
1323// loadHwModule_l() must be called with AudioFlinger::mLock held
1324audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1325{
1326 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1327 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1328 ALOGW("loadHwModule() module %s already loaded", name);
1329 return mAudioHwDevs.keyAt(i);
1330 }
1331 }
1332
Eric Laurenta4c5a552012-03-29 10:12:40 -07001333 audio_hw_device_t *dev;
1334
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001335 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001336 if (rc) {
1337 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1338 return 0;
1339 }
1340
1341 mHardwareStatus = AUDIO_HW_INIT;
1342 rc = dev->init_check(dev);
1343 mHardwareStatus = AUDIO_HW_IDLE;
1344 if (rc) {
1345 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1346 return 0;
1347 }
1348
John Grossmanee578c02012-07-23 17:05:46 -07001349 // Check and cache this HAL's level of support for master mute and master
1350 // volume. If this is the first HAL opened, and it supports the get
1351 // methods, use the initial values provided by the HAL as the current
1352 // master mute and volume settings.
1353
1354 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1355 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001356 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001357
1358 if (0 == mAudioHwDevs.size()) {
1359 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1360 if (NULL != dev->get_master_volume) {
1361 float mv;
1362 if (OK == dev->get_master_volume(dev, &mv)) {
1363 mMasterVolume = mv;
1364 }
1365 }
1366
1367 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1368 if (NULL != dev->get_master_mute) {
1369 bool mm;
1370 if (OK == dev->get_master_mute(dev, &mm)) {
1371 mMasterMute = mm;
1372 }
1373 }
1374 }
1375
Eric Laurenta4c5a552012-03-29 10:12:40 -07001376 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001377 if ((NULL != dev->set_master_volume) &&
1378 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1379 flags = static_cast<AudioHwDevice::Flags>(flags |
1380 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1381 }
1382
1383 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1384 if ((NULL != dev->set_master_mute) &&
1385 (OK == dev->set_master_mute(dev, mMasterMute))) {
1386 flags = static_cast<AudioHwDevice::Flags>(flags |
1387 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1388 }
1389
Eric Laurenta4c5a552012-03-29 10:12:40 -07001390 mHardwareStatus = AUDIO_HW_IDLE;
1391 }
1392
1393 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001394 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001395
1396 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001397 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001398
1399 return handle;
1400
1401}
1402
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001403// ----------------------------------------------------------------------------
1404
Glenn Kasten3b16c762012-11-14 08:44:39 -08001405uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001406{
1407 Mutex::Autolock _l(mLock);
1408 PlaybackThread *thread = primaryPlaybackThread_l();
1409 return thread != NULL ? thread->sampleRate() : 0;
1410}
1411
Glenn Kastene33054e2012-11-14 12:54:39 -08001412size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001413{
1414 Mutex::Autolock _l(mLock);
1415 PlaybackThread *thread = primaryPlaybackThread_l();
1416 return thread != NULL ? thread->frameCountHAL() : 0;
1417}
1418
1419// ----------------------------------------------------------------------------
1420
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001421status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1422{
1423 uid_t uid = IPCThreadState::self()->getCallingUid();
1424 if (uid != AID_SYSTEM) {
1425 return PERMISSION_DENIED;
1426 }
1427 Mutex::Autolock _l(mLock);
1428 if (mIsDeviceTypeKnown) {
1429 return INVALID_OPERATION;
1430 }
1431 mIsLowRamDevice = isLowRamDevice;
1432 mIsDeviceTypeKnown = true;
1433 return NO_ERROR;
1434}
1435
1436// ----------------------------------------------------------------------------
1437
Eric Laurenta4c5a552012-03-29 10:12:40 -07001438audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1439 audio_devices_t *pDevices,
1440 uint32_t *pSamplingRate,
1441 audio_format_t *pFormat,
1442 audio_channel_mask_t *pChannelMask,
1443 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001444 audio_output_flags_t flags,
1445 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001446{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001448 struct audio_config config;
1449 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1450 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1451 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1452 if (offloadInfo) {
1453 config.offload_info = *offloadInfo;
1454 }
1455
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001456 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001457 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001458
Eric Laurentbfb1b832013-01-07 09:53:42 -08001459 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001460 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001461 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001462 config.sample_rate,
1463 config.format,
1464 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001465 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001466 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1467 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001468
1469 if (pDevices == NULL || *pDevices == 0) {
1470 return 0;
1471 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001472
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473 Mutex::Autolock _l(mLock);
1474
Eric Laurenta4c5a552012-03-29 10:12:40 -07001475 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001476 if (outHwDev == NULL)
1477 return 0;
1478
John Grossmanee578c02012-07-23 17:05:46 -07001479 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001480 audio_io_handle_t id = nextUniqueId();
1481
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001482 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001483
Glenn Kasten34542ac2013-06-26 11:29:02 -07001484 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001485 id,
1486 *pDevices,
1487 (audio_output_flags_t)flags,
1488 &config,
1489 &outStream);
1490
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001491 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001492 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001493 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001494 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001495 config.sample_rate,
1496 config.format,
1497 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498 status);
1499
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001500 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001501 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001502
Eric Laurentbfb1b832013-01-07 09:53:42 -08001503 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1504 thread = new OffloadThread(this, output, id, *pDevices);
1505 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1506 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001507 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1508 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001509 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001510 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001511 } else {
1512 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001513 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001514 }
1515 mPlaybackThreads.add(id, thread);
1516
Glenn Kasten7c027242012-12-26 14:43:16 -08001517 if (pSamplingRate != NULL) {
1518 *pSamplingRate = config.sample_rate;
1519 }
1520 if (pFormat != NULL) {
1521 *pFormat = config.format;
1522 }
1523 if (pChannelMask != NULL) {
1524 *pChannelMask = config.channel_mask;
1525 }
1526 if (pLatencyMs != NULL) {
1527 *pLatencyMs = thread->latency();
1528 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529
1530 // notify client processes of the new output creation
1531 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001532
1533 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001534 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001535 ALOGI("Using module %d has the primary audio interface", module);
1536 mPrimaryHardwareDev = outHwDev;
1537
1538 AutoMutex lock(mHardwareLock);
1539 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001540 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001541 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001542 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543 return id;
1544 }
1545
1546 return 0;
1547}
1548
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001549audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1550 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551{
1552 Mutex::Autolock _l(mLock);
1553 MixerThread *thread1 = checkMixerThread_l(output1);
1554 MixerThread *thread2 = checkMixerThread_l(output2);
1555
1556 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001557 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1558 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001559 return 0;
1560 }
1561
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001562 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1564 thread->addOutputTrack(thread2);
1565 mPlaybackThreads.add(id, thread);
1566 // notify client processes of the new output creation
1567 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1568 return id;
1569}
1570
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001571status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001572{
Glenn Kastend96c5722012-04-25 13:44:49 -07001573 return closeOutput_nonvirtual(output);
1574}
1575
1576status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1577{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001578 // keep strong reference on the playback thread so that
1579 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001580 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001581 {
1582 Mutex::Autolock _l(mLock);
1583 thread = checkPlaybackThread_l(output);
1584 if (thread == NULL) {
1585 return BAD_VALUE;
1586 }
1587
Steve Block3856b092011-10-20 11:56:00 +01001588 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001589
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001590 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001591 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001592 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001593 DuplicatingThread *dupThread =
1594 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001595 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001596
1597 }
1598 }
1599 }
1600
1601
1602 mPlaybackThreads.removeItem(output);
1603 // save all effects to the default thread
1604 if (mPlaybackThreads.size()) {
1605 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1606 if (dstThread != NULL) {
1607 // audioflinger lock is held here so the acquisition order of thread locks does not
1608 // matter
1609 Mutex::Autolock _dl(dstThread->mLock);
1610 Mutex::Autolock _sl(thread->mLock);
1611 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1612 for (size_t i = 0; i < effectChains.size(); i ++) {
1613 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614 }
1615 }
1616 }
Glenn Kastena1117922012-01-26 10:53:32 -08001617 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001618 }
1619 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001620 // The thread entity (active unit of execution) is no longer running here,
1621 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001622
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001623 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001624 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001625 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001626 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001627 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001628 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001629 }
1630 return NO_ERROR;
1631}
1632
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001633status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001634{
1635 Mutex::Autolock _l(mLock);
1636 PlaybackThread *thread = checkPlaybackThread_l(output);
1637
1638 if (thread == NULL) {
1639 return BAD_VALUE;
1640 }
1641
Steve Block3856b092011-10-20 11:56:00 +01001642 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001643 thread->suspend();
1644
1645 return NO_ERROR;
1646}
1647
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001648status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001649{
1650 Mutex::Autolock _l(mLock);
1651 PlaybackThread *thread = checkPlaybackThread_l(output);
1652
1653 if (thread == NULL) {
1654 return BAD_VALUE;
1655 }
1656
Steve Block3856b092011-10-20 11:56:00 +01001657 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001658
1659 thread->restore();
1660
1661 return NO_ERROR;
1662}
1663
Eric Laurenta4c5a552012-03-29 10:12:40 -07001664audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1665 audio_devices_t *pDevices,
1666 uint32_t *pSamplingRate,
1667 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001668 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001669{
1670 status_t status;
1671 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001672 struct audio_config config;
1673 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1674 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1675 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1676
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001677 uint32_t reqSamplingRate = config.sample_rate;
1678 audio_format_t reqFormat = config.format;
1679 audio_channel_mask_t reqChannels = config.channel_mask;
1680 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001681 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001682
1683 if (pDevices == NULL || *pDevices == 0) {
1684 return 0;
1685 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001686
Mathias Agopian65ab4712010-07-14 17:59:35 -07001687 Mutex::Autolock _l(mLock);
1688
Eric Laurenta4c5a552012-03-29 10:12:40 -07001689 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001690 if (inHwDev == NULL)
1691 return 0;
1692
John Grossmanee578c02012-07-23 17:05:46 -07001693 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001694 audio_io_handle_t id = nextUniqueId();
1695
John Grossmanee578c02012-07-23 17:05:46 -07001696 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001697 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001698 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1699 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001700 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001701 config.sample_rate,
1702 config.format,
1703 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001704 status);
1705
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001706 // If the input could not be opened with the requested parameters and we can handle the
1707 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1708 // 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 -07001709 if (status == BAD_VALUE &&
1710 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1711 (config.sample_rate <= 2 * reqSamplingRate) &&
1712 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001713 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001714 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001715 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001716 }
1717
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001718 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001719
Glenn Kasten46909e72013-02-26 09:20:22 -08001720#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001721 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1722 // or (re-)create if current Pipe is idle and does not match the new format
1723 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001724 enum {
1725 TEE_SINK_NO, // don't copy input
1726 TEE_SINK_NEW, // copy input using a new pipe
1727 TEE_SINK_OLD, // copy input using an existing pipe
1728 } kind;
1729 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1730 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001731 if (!mTeeSinkInputEnabled) {
1732 kind = TEE_SINK_NO;
1733 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001734 kind = TEE_SINK_NO;
1735 } else if (mRecordTeeSink == 0) {
1736 kind = TEE_SINK_NEW;
1737 } else if (mRecordTeeSink->getStrongCount() != 1) {
1738 kind = TEE_SINK_NO;
1739 } else if (format == mRecordTeeSink->format()) {
1740 kind = TEE_SINK_OLD;
1741 } else {
1742 kind = TEE_SINK_NEW;
1743 }
1744 switch (kind) {
1745 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001746 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001747 size_t numCounterOffers = 0;
1748 const NBAIO_Format offers[1] = {format};
1749 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1750 ALOG_ASSERT(index == 0);
1751 PipeReader *pipeReader = new PipeReader(*pipe);
1752 numCounterOffers = 0;
1753 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1754 ALOG_ASSERT(index == 0);
1755 mRecordTeeSink = pipe;
1756 mRecordTeeSource = pipeReader;
1757 teeSink = pipe;
1758 }
1759 break;
1760 case TEE_SINK_OLD:
1761 teeSink = mRecordTeeSink;
1762 break;
1763 case TEE_SINK_NO:
1764 default:
1765 break;
1766 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001767#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001768
Dima Zavin799a70e2011-04-18 16:57:27 -07001769 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1770
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001771 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001772 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001773 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001774 thread = new RecordThread(this,
1775 input,
1776 reqSamplingRate,
1777 reqChannels,
1778 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001779 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001780 *pDevices
1781#ifdef TEE_SINK
1782 , teeSink
1783#endif
1784 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001785 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001786 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001787 if (pSamplingRate != NULL) {
1788 *pSamplingRate = reqSamplingRate;
1789 }
1790 if (pFormat != NULL) {
1791 *pFormat = config.format;
1792 }
1793 if (pChannelMask != NULL) {
1794 *pChannelMask = reqChannels;
1795 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001796
Mathias Agopian65ab4712010-07-14 17:59:35 -07001797 // notify client processes of the new input creation
1798 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1799 return id;
1800 }
1801
1802 return 0;
1803}
1804
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001805status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001806{
Glenn Kastend96c5722012-04-25 13:44:49 -07001807 return closeInput_nonvirtual(input);
1808}
1809
1810status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1811{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001812 // keep strong reference on the record thread so that
1813 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001814 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001815 {
1816 Mutex::Autolock _l(mLock);
1817 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001818 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001819 return BAD_VALUE;
1820 }
1821
Steve Block3856b092011-10-20 11:56:00 +01001822 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001823 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001824 mRecordThreads.removeItem(input);
1825 }
1826 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001827 // The thread entity (active unit of execution) is no longer running here,
1828 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001829
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001830 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001831 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001832 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001833 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001834 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001835
1836 return NO_ERROR;
1837}
1838
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001839status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001840{
1841 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001842 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001843
1844 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1845 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001846 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001847 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001848
1849 return NO_ERROR;
1850}
1851
1852
1853int AudioFlinger::newAudioSessionId()
1854{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001855 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001856}
1857
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001858void AudioFlinger::acquireAudioSessionId(int audioSession)
1859{
1860 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001861 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001862 ALOGV("acquiring %d from %d", audioSession, caller);
Eric Laurentd1b28d42013-09-18 18:47:13 -07001863
1864 // Ignore requests received from processes not known as notification client. The request
1865 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1866 // called from a different pid leaving a stale session reference. Also we don't know how
1867 // to clear this reference if the client process dies.
1868 if (mNotificationClients.indexOfKey(caller) < 0) {
1869 ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
1870 return;
1871 }
1872
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001873 size_t num = mAudioSessionRefs.size();
1874 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001875 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001876 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1877 ref->mCnt++;
1878 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001879 return;
1880 }
1881 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001882 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1883 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001884}
1885
1886void AudioFlinger::releaseAudioSessionId(int audioSession)
1887{
1888 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001889 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001890 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001891 size_t num = mAudioSessionRefs.size();
1892 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001893 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001894 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1895 ref->mCnt--;
1896 ALOGV(" decremented refcount to %d", ref->mCnt);
1897 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001898 mAudioSessionRefs.removeAt(i);
1899 delete ref;
1900 purgeStaleEffects_l();
1901 }
1902 return;
1903 }
1904 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001905 // If the caller is mediaserver it is likely that the session being released was acquired
1906 // on behalf of a process not in notification clients and we ignore the warning.
1907 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001908}
1909
1910void AudioFlinger::purgeStaleEffects_l() {
1911
Steve Block3856b092011-10-20 11:56:00 +01001912 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001913
1914 Vector< sp<EffectChain> > chains;
1915
1916 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1917 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1918 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1919 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001920 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1921 chains.push(ec);
1922 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001923 }
1924 }
1925 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1926 sp<RecordThread> t = mRecordThreads.valueAt(i);
1927 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1928 sp<EffectChain> ec = t->mEffectChains[j];
1929 chains.push(ec);
1930 }
1931 }
1932
1933 for (size_t i = 0; i < chains.size(); i++) {
1934 sp<EffectChain> ec = chains[i];
1935 int sessionid = ec->sessionId();
1936 sp<ThreadBase> t = ec->mThread.promote();
1937 if (t == 0) {
1938 continue;
1939 }
1940 size_t numsessionrefs = mAudioSessionRefs.size();
1941 bool found = false;
1942 for (size_t k = 0; k < numsessionrefs; k++) {
1943 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001944 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001945 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001946 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001947 found = true;
1948 break;
1949 }
1950 }
1951 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001952 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001953 // remove all effects from the chain
1954 while (ec->mEffects.size()) {
1955 sp<EffectModule> effect = ec->mEffects[0];
1956 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001957 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001958 if (effect->purgeHandles()) {
1959 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001960 }
1961 AudioSystem::unregisterEffect(effect->id());
1962 }
1963 }
1964 }
1965 return;
1966}
1967
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001969AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001970{
Glenn Kastena1117922012-01-26 10:53:32 -08001971 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001972}
1973
1974// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001975AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001976{
1977 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001978 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979}
1980
1981// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001982AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001983{
Glenn Kastena1117922012-01-26 10:53:32 -08001984 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985}
1986
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001987uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001988{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001989 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001990}
1991
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001992AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001993{
1994 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1995 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001996 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001997 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001998 return thread;
1999 }
2000 }
2001 return NULL;
2002}
2003
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002004audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002005{
2006 PlaybackThread *thread = primaryPlaybackThread_l();
2007
2008 if (thread == NULL) {
2009 return 0;
2010 }
2011
Eric Laurentf1c04f92012-08-28 14:26:53 -07002012 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002013}
2014
Eric Laurenta011e352012-03-29 15:51:43 -07002015sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2016 int triggerSession,
2017 int listenerSession,
2018 sync_event_callback_t callBack,
2019 void *cookie)
2020{
2021 Mutex::Autolock _l(mLock);
2022
2023 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2024 status_t playStatus = NAME_NOT_FOUND;
2025 status_t recStatus = NAME_NOT_FOUND;
2026 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2027 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2028 if (playStatus == NO_ERROR) {
2029 return event;
2030 }
2031 }
2032 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2033 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2034 if (recStatus == NO_ERROR) {
2035 return event;
2036 }
2037 }
2038 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2039 mPendingSyncEvents.add(event);
2040 } else {
2041 ALOGV("createSyncEvent() invalid event %d", event->type());
2042 event.clear();
2043 }
2044 return event;
2045}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002046
Mathias Agopian65ab4712010-07-14 17:59:35 -07002047// ----------------------------------------------------------------------------
2048// Effect management
2049// ----------------------------------------------------------------------------
2050
2051
Glenn Kastenf587ba52012-01-26 16:25:10 -08002052status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002053{
2054 Mutex::Autolock _l(mLock);
2055 return EffectQueryNumberEffects(numEffects);
2056}
2057
Glenn Kastenf587ba52012-01-26 16:25:10 -08002058status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002059{
2060 Mutex::Autolock _l(mLock);
2061 return EffectQueryEffect(index, descriptor);
2062}
2063
Glenn Kasten5e92a782012-01-30 07:40:52 -08002064status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002065 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002066{
2067 Mutex::Autolock _l(mLock);
2068 return EffectGetDescriptor(pUuid, descriptor);
2069}
2070
2071
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002072sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002073 effect_descriptor_t *pDesc,
2074 const sp<IEffectClient>& effectClient,
2075 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002076 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002077 int sessionId,
2078 status_t *status,
2079 int *id,
2080 int *enabled)
2081{
2082 status_t lStatus = NO_ERROR;
2083 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002084 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002085
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002086 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002087 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002088 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002089
2090 if (pDesc == NULL) {
2091 lStatus = BAD_VALUE;
2092 goto Exit;
2093 }
2094
Eric Laurent84e9a102010-09-23 16:10:16 -07002095 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002096 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002097 lStatus = PERMISSION_DENIED;
2098 goto Exit;
2099 }
2100
Dima Zavinfce7a472011-04-19 22:30:36 -07002101 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002102 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002103 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002104 lStatus = PERMISSION_DENIED;
2105 goto Exit;
2106 }
2107
Mathias Agopian65ab4712010-07-14 17:59:35 -07002108 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002109 if (!EffectIsNullUuid(&pDesc->uuid)) {
2110 // if uuid is specified, request effect descriptor
2111 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2112 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002113 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002114 goto Exit;
2115 }
2116 } else {
2117 // if uuid is not specified, look for an available implementation
2118 // of the required type in effect factory
2119 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002120 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002121 lStatus = BAD_VALUE;
2122 goto Exit;
2123 }
2124 uint32_t numEffects = 0;
2125 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002126 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002127 bool found = false;
2128
2129 lStatus = EffectQueryNumberEffects(&numEffects);
2130 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002131 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002132 goto Exit;
2133 }
2134 for (uint32_t i = 0; i < numEffects; i++) {
2135 lStatus = EffectQueryEffect(i, &desc);
2136 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002137 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002138 continue;
2139 }
2140 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2141 // If matching type found save effect descriptor. If the session is
2142 // 0 and the effect is not auxiliary, continue enumeration in case
2143 // an auxiliary version of this effect type is available
2144 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002145 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002146 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002147 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2148 break;
2149 }
2150 }
2151 }
2152 if (!found) {
2153 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002154 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002155 goto Exit;
2156 }
2157 // For same effect type, chose auxiliary version over insert version if
2158 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002159 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002160 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002161 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002162 }
2163 }
2164
2165 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002166 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002167 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2168 lStatus = INVALID_OPERATION;
2169 goto Exit;
2170 }
2171
Eric Laurent59255e42011-07-27 19:49:51 -07002172 // check recording permission for visualizer
2173 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2174 !recordingAllowed()) {
2175 lStatus = PERMISSION_DENIED;
2176 goto Exit;
2177 }
2178
Mathias Agopian65ab4712010-07-14 17:59:35 -07002179 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002180 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002181 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2182 // if the output returned by getOutputForEffect() is removed before we lock the
2183 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2184 // and we will exit safely
2185 io = AudioSystem::getOutputForEffect(&desc);
2186 ALOGV("createEffect got output %d", io);
2187 }
2188
2189 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002190
2191 // If output is not specified try to find a matching audio session ID in one of the
2192 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002193 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2194 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002195 // Note: io is never 0 when creating an effect on an input
2196 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002197 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2198 // output must be specified by AudioPolicyManager when using session
2199 // AUDIO_SESSION_OUTPUT_STAGE
2200 lStatus = BAD_VALUE;
2201 goto Exit;
2202 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002203 // look for the thread where the specified audio session is present
2204 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2205 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2206 io = mPlaybackThreads.keyAt(i);
2207 break;
2208 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002209 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002210 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002211 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2212 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2213 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002214 break;
2215 }
2216 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002217 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002218 // If no output thread contains the requested session ID, default to
2219 // first output. The effect chain will be moved to the correct output
2220 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002221 if (io == 0 && mPlaybackThreads.size()) {
2222 io = mPlaybackThreads.keyAt(0);
2223 }
Steve Block3856b092011-10-20 11:56:00 +01002224 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002225 }
2226 ThreadBase *thread = checkRecordThread_l(io);
2227 if (thread == NULL) {
2228 thread = checkPlaybackThread_l(io);
2229 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002230 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002231 lStatus = BAD_VALUE;
2232 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002233 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002234 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002235
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002236 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002238 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002239 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2240 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002241 if (handle != 0 && id != NULL) {
2242 *id = handle->id();
2243 }
2244 }
2245
2246Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002247 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002248 *status = lStatus;
2249 }
2250 return handle;
2251}
2252
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002253status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2254 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002255{
Steve Block3856b092011-10-20 11:56:00 +01002256 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002257 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002258 Mutex::Autolock _l(mLock);
2259 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002260 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002261 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002262 }
Eric Laurentde070132010-07-13 04:45:46 -07002263 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2264 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002265 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002266 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002267 }
Eric Laurentde070132010-07-13 04:45:46 -07002268 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2269 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002270 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002271 return BAD_VALUE;
2272 }
2273
2274 Mutex::Autolock _dl(dstThread->mLock);
2275 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002276 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002277}
2278
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002279// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002280status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002281 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002282 AudioFlinger::PlaybackThread *dstThread,
2283 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002284{
Steve Block3856b092011-10-20 11:56:00 +01002285 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002286 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002287
Eric Laurent59255e42011-07-27 19:49:51 -07002288 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002289 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002290 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002291 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002292 return INVALID_OPERATION;
2293 }
2294
Eric Laurent39e94f82010-07-28 01:32:47 -07002295 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002296 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002297 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002298 // removed.
2299 srcThread->removeEffectChain_l(chain);
2300
2301 // transfer all effects one by one so that new effect chain is created on new thread with
2302 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002303 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002304 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002305 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002306 Vector< sp<EffectModule> > removed;
2307 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002308 while (effect != 0) {
2309 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002310 removed.add(effect);
2311 status = dstThread->addEffect_l(effect);
2312 if (status != NO_ERROR) {
2313 break;
2314 }
Eric Laurentec35a142011-10-05 17:42:25 -07002315 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2316 if (effect->state() == EffectModule::ACTIVE ||
2317 effect->state() == EffectModule::STOPPING) {
2318 effect->start();
2319 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002320 // if the move request is not received from audio policy manager, the effect must be
2321 // re-registered with the new strategy and output
2322 if (dstChain == 0) {
2323 dstChain = effect->chain().promote();
2324 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002325 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002326 status = NO_INIT;
2327 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002328 }
2329 strategy = dstChain->strategy();
2330 }
2331 if (reRegister) {
2332 AudioSystem::unregisterEffect(effect->id());
2333 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002334 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002335 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002336 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002337 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002338 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002339 }
Eric Laurentde070132010-07-13 04:45:46 -07002340 effect = chain->getEffectFromId_l(0);
2341 }
2342
Eric Laurent5baf2af2013-09-12 17:37:00 -07002343 if (status != NO_ERROR) {
2344 for (size_t i = 0; i < removed.size(); i++) {
2345 srcThread->addEffect_l(removed[i]);
2346 if (dstChain != 0 && reRegister) {
2347 AudioSystem::unregisterEffect(removed[i]->id());
2348 AudioSystem::registerEffect(&removed[i]->desc(),
2349 srcThread->id(),
2350 strategy,
2351 sessionId,
2352 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002353 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002354 }
2355 }
2356 }
2357
2358 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002359}
2360
Eric Laurent5baf2af2013-09-12 17:37:00 -07002361bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002362{
2363 if (mGlobalEffectEnableTime != 0 &&
2364 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2365 return true;
2366 }
2367
2368 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2369 sp<EffectChain> ec =
2370 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002371 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002372 return true;
2373 }
2374 }
2375 return false;
2376}
2377
Eric Laurent5baf2af2013-09-12 17:37:00 -07002378void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002379{
2380 Mutex::Autolock _l(mLock);
2381
2382 mGlobalEffectEnableTime = systemTime();
2383
2384 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2385 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2386 if (t->mType == ThreadBase::OFFLOAD) {
2387 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2388 }
2389 }
2390
2391}
2392
Glenn Kastenda6ef132013-01-10 12:31:01 -08002393struct Entry {
2394#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2395 char mName[MAX_NAME];
2396};
2397
2398int comparEntry(const void *p1, const void *p2)
2399{
2400 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2401}
2402
Glenn Kasten46909e72013-02-26 09:20:22 -08002403#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002404void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002405{
Eric Laurent81784c32012-11-19 14:55:58 -08002406 NBAIO_Source *teeSource = source.get();
2407 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002408 // .wav rotation
2409 // There is a benign race condition if 2 threads call this simultaneously.
2410 // They would both traverse the directory, but the result would simply be
2411 // failures at unlink() which are ignored. It's also unlikely since
2412 // normally dumpsys is only done by bugreport or from the command line.
2413 char teePath[32+256];
2414 strcpy(teePath, "/data/misc/media");
2415 size_t teePathLen = strlen(teePath);
2416 DIR *dir = opendir(teePath);
2417 teePath[teePathLen++] = '/';
2418 if (dir != NULL) {
2419#define MAX_SORT 20 // number of entries to sort
2420#define MAX_KEEP 10 // number of entries to keep
2421 struct Entry entries[MAX_SORT];
2422 size_t entryCount = 0;
2423 while (entryCount < MAX_SORT) {
2424 struct dirent de;
2425 struct dirent *result = NULL;
2426 int rc = readdir_r(dir, &de, &result);
2427 if (rc != 0) {
2428 ALOGW("readdir_r failed %d", rc);
2429 break;
2430 }
2431 if (result == NULL) {
2432 break;
2433 }
2434 if (result != &de) {
2435 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2436 break;
2437 }
2438 // ignore non .wav file entries
2439 size_t nameLen = strlen(de.d_name);
2440 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2441 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2442 continue;
2443 }
2444 strcpy(entries[entryCount++].mName, de.d_name);
2445 }
2446 (void) closedir(dir);
2447 if (entryCount > MAX_KEEP) {
2448 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2449 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2450 strcpy(&teePath[teePathLen], entries[i].mName);
2451 (void) unlink(teePath);
2452 }
2453 }
2454 } else {
2455 if (fd >= 0) {
2456 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2457 }
2458 }
Eric Laurent81784c32012-11-19 14:55:58 -08002459 char teeTime[16];
2460 struct timeval tv;
2461 gettimeofday(&tv, NULL);
2462 struct tm tm;
2463 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002464 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2465 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2466 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2467 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002468 if (teeFd >= 0) {
2469 char wavHeader[44];
2470 memcpy(wavHeader,
2471 "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",
2472 sizeof(wavHeader));
2473 NBAIO_Format format = teeSource->format();
2474 unsigned channelCount = Format_channelCount(format);
2475 ALOG_ASSERT(channelCount <= FCC_2);
2476 uint32_t sampleRate = Format_sampleRate(format);
2477 wavHeader[22] = channelCount; // number of channels
2478 wavHeader[24] = sampleRate; // sample rate
2479 wavHeader[25] = sampleRate >> 8;
2480 wavHeader[32] = channelCount * 2; // block alignment
2481 write(teeFd, wavHeader, sizeof(wavHeader));
2482 size_t total = 0;
2483 bool firstRead = true;
2484 for (;;) {
2485#define TEE_SINK_READ 1024
2486 short buffer[TEE_SINK_READ * FCC_2];
2487 size_t count = TEE_SINK_READ;
2488 ssize_t actual = teeSource->read(buffer, count,
2489 AudioBufferProvider::kInvalidPTS);
2490 bool wasFirstRead = firstRead;
2491 firstRead = false;
2492 if (actual <= 0) {
2493 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2494 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002495 }
Eric Laurent81784c32012-11-19 14:55:58 -08002496 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002497 }
Eric Laurent81784c32012-11-19 14:55:58 -08002498 ALOG_ASSERT(actual <= (ssize_t)count);
2499 write(teeFd, buffer, actual * channelCount * sizeof(short));
2500 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002501 }
Eric Laurent81784c32012-11-19 14:55:58 -08002502 lseek(teeFd, (off_t) 4, SEEK_SET);
2503 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2504 write(teeFd, &temp, sizeof(temp));
2505 lseek(teeFd, (off_t) 40, SEEK_SET);
2506 temp = total * channelCount * sizeof(short);
2507 write(teeFd, &temp, sizeof(temp));
2508 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002509 if (fd >= 0) {
2510 fdprintf(fd, "tee copied to %s\n", teePath);
2511 }
Eric Laurent59255e42011-07-27 19:49:51 -07002512 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002513 if (fd >= 0) {
2514 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2515 }
Eric Laurent59255e42011-07-27 19:49:51 -07002516 }
2517 }
2518}
Glenn Kasten46909e72013-02-26 09:20:22 -08002519#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002520
Mathias Agopian65ab4712010-07-14 17:59:35 -07002521// ----------------------------------------------------------------------------
2522
2523status_t AudioFlinger::onTransact(
2524 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2525{
2526 return BnAudioFlinger::onTransact(code, data, reply, flags);
2527}
2528
Mathias Agopian65ab4712010-07-14 17:59:35 -07002529}; // namespace android