blob: 0aef53f069e4dfe4d71b054d30525a8fc5ca8746 [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,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800451 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 status_t *status)
453{
454 sp<PlaybackThread::Track> track;
455 sp<TrackHandle> trackHandle;
456 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700457 status_t lStatus;
458 int lSessionId;
459
Glenn Kasten263709e2012-01-06 08:40:01 -0800460 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
461 // but if someone uses binder directly they could bypass that and cause us to crash
462 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000463 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700464 lStatus = BAD_VALUE;
465 goto Exit;
466 }
467
Glenn Kasten60a83922012-06-21 12:56:37 -0700468 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
469 // and we don't yet support 8.24 or 32-bit PCM
470 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
471 ALOGE("createTrack() invalid format %d", format);
472 lStatus = BAD_VALUE;
473 goto Exit;
474 }
475
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476 {
477 Mutex::Autolock _l(mLock);
478 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700479 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700480 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800481 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700482 lStatus = BAD_VALUE;
483 goto Exit;
484 }
485
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800486 pid_t pid = IPCThreadState::self()->getCallingPid();
Marco Nelissen9cae2172013-01-14 14:12:05 -0800487
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800488 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700489
Steve Block3856b092011-10-20 11:56:00 +0100490 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700491 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700492 // check if an effect chain with the same session ID is present on another
493 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700494 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700495 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
496 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700497 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700498 if (sessions & PlaybackThread::EFFECT_SESSION) {
499 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700500 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700501 }
Eric Laurentde070132010-07-13 04:45:46 -0700502 }
503 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700504 lSessionId = *sessionId;
505 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700506 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700507 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700508 if (sessionId != NULL) {
509 *sessionId = lSessionId;
510 }
511 }
Steve Block3856b092011-10-20 11:56:00 +0100512 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700513
514 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800515 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew Georgee010f652013-12-13 15:40:13 -0800516 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
517 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700518
519 // move effect chain to this output thread if an effect on same session was waiting
520 // for a track to be created
521 if (lStatus == NO_ERROR && effectThread != NULL) {
522 Mutex::Autolock _dl(thread->mLock);
523 Mutex::Autolock _sl(effectThread->mLock);
524 moveEffectChain_l(lSessionId, effectThread, thread, true);
525 }
Eric Laurenta011e352012-03-29 15:51:43 -0700526
527 // Look for sync events awaiting for a session to be used.
528 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
529 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
530 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700531 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700532 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700533 } else {
534 mPendingSyncEvents[i]->cancel();
535 }
Eric Laurenta011e352012-03-29 15:51:43 -0700536 mPendingSyncEvents.removeAt(i);
537 i--;
538 }
539 }
540 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 }
542 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700543 // s for server's pid, n for normal mixer name, f for fast index
544 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
545 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 trackHandle = new TrackHandle(track);
547 } else {
548 // remove local strong reference to Client before deleting the Track so that the Client
549 // destructor is called by the TrackBase destructor with mLock held
550 client.clear();
551 track.clear();
552 }
553
554Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700555 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 *status = lStatus;
557 }
558 return trackHandle;
559}
560
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800561uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562{
563 Mutex::Autolock _l(mLock);
564 PlaybackThread *thread = checkPlaybackThread_l(output);
565 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000566 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567 return 0;
568 }
569 return thread->sampleRate();
570}
571
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800572int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573{
574 Mutex::Autolock _l(mLock);
575 PlaybackThread *thread = checkPlaybackThread_l(output);
576 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000577 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578 return 0;
579 }
580 return thread->channelCount();
581}
582
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800583audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584{
585 Mutex::Autolock _l(mLock);
586 PlaybackThread *thread = checkPlaybackThread_l(output);
587 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000588 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800589 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590 }
591 return thread->format();
592}
593
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800594size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595{
596 Mutex::Autolock _l(mLock);
597 PlaybackThread *thread = checkPlaybackThread_l(output);
598 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000599 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600 return 0;
601 }
Glenn Kasten58912562012-04-03 10:45:00 -0700602 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
603 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 return thread->frameCount();
605}
606
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800607uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700608{
609 Mutex::Autolock _l(mLock);
610 PlaybackThread *thread = checkPlaybackThread_l(output);
611 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800612 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700613 return 0;
614 }
615 return thread->latency();
616}
617
618status_t AudioFlinger::setMasterVolume(float value)
619{
Eric Laurenta1884f92011-08-23 08:25:03 -0700620 status_t ret = initCheck();
621 if (ret != NO_ERROR) {
622 return ret;
623 }
624
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625 // check calling permissions
626 if (!settingsAllowed()) {
627 return PERMISSION_DENIED;
628 }
629
Eric Laurenta4c5a552012-03-29 10:12:40 -0700630 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700631 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700632
John Grossmanee578c02012-07-23 17:05:46 -0700633 // Set master volume in the HALs which support it.
634 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
635 AutoMutex lock(mHardwareLock);
636 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800637
John Grossmanee578c02012-07-23 17:05:46 -0700638 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
639 if (dev->canSetMasterVolume()) {
640 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800641 }
John Grossmanee578c02012-07-23 17:05:46 -0700642 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700643 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644
John Grossmanee578c02012-07-23 17:05:46 -0700645 // Now set the master volume in each playback thread. Playback threads
646 // assigned to HALs which do not have master volume support will apply
647 // master volume during the mix operation. Threads with HALs which do
648 // support master volume will simply ignore the setting.
Eric Laurent22ac20e2015-05-08 10:50:03 -0700649 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
650 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
651 continue;
652 }
John Grossmanee578c02012-07-23 17:05:46 -0700653 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurent22ac20e2015-05-08 10:50:03 -0700654 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655
656 return NO_ERROR;
657}
658
Glenn Kastenf78aee72012-01-04 11:00:47 -0800659status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660{
Eric Laurenta1884f92011-08-23 08:25:03 -0700661 status_t ret = initCheck();
662 if (ret != NO_ERROR) {
663 return ret;
664 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665
666 // check calling permissions
667 if (!settingsAllowed()) {
668 return PERMISSION_DENIED;
669 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800670 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000671 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 return BAD_VALUE;
673 }
674
675 { // scope for the lock
676 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700677 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700679 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680 mHardwareStatus = AUDIO_HW_IDLE;
681 }
682
683 if (NO_ERROR == ret) {
684 Mutex::Autolock _l(mLock);
685 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800686 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700687 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700688 }
689
690 return ret;
691}
692
693status_t AudioFlinger::setMicMute(bool state)
694{
Eric Laurenta1884f92011-08-23 08:25:03 -0700695 status_t ret = initCheck();
696 if (ret != NO_ERROR) {
697 return ret;
698 }
699
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700 // check calling permissions
701 if (!settingsAllowed()) {
702 return PERMISSION_DENIED;
703 }
704
705 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700706 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700708 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709 mHardwareStatus = AUDIO_HW_IDLE;
710 return ret;
711}
712
713bool AudioFlinger::getMicMute() const
714{
Eric Laurenta1884f92011-08-23 08:25:03 -0700715 status_t ret = initCheck();
716 if (ret != NO_ERROR) {
717 return false;
718 }
719
Dima Zavinfce7a472011-04-19 22:30:36 -0700720 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800721 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700722 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700724 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725 mHardwareStatus = AUDIO_HW_IDLE;
726 return state;
727}
728
729status_t AudioFlinger::setMasterMute(bool muted)
730{
John Grossmand8f178d2012-07-20 14:51:35 -0700731 status_t ret = initCheck();
732 if (ret != NO_ERROR) {
733 return ret;
734 }
735
Mathias Agopian65ab4712010-07-14 17:59:35 -0700736 // check calling permissions
737 if (!settingsAllowed()) {
738 return PERMISSION_DENIED;
739 }
740
John Grossmanee578c02012-07-23 17:05:46 -0700741 Mutex::Autolock _l(mLock);
742 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700743
John Grossmanee578c02012-07-23 17:05:46 -0700744 // Set master mute in the HALs which support it.
745 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
746 AutoMutex lock(mHardwareLock);
747 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700748
John Grossmanee578c02012-07-23 17:05:46 -0700749 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
750 if (dev->canSetMasterMute()) {
751 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700752 }
John Grossmanee578c02012-07-23 17:05:46 -0700753 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700754 }
755
John Grossmanee578c02012-07-23 17:05:46 -0700756 // Now set the master mute in each playback thread. Playback threads
757 // assigned to HALs which do not have master mute support will apply master
758 // mute during the mix operation. Threads with HALs which do support master
759 // mute will simply ignore the setting.
Eric Laurent22ac20e2015-05-08 10:50:03 -0700760 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
761 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
762 continue;
763 }
John Grossmanee578c02012-07-23 17:05:46 -0700764 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurent22ac20e2015-05-08 10:50:03 -0700765 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766
767 return NO_ERROR;
768}
769
770float AudioFlinger::masterVolume() const
771{
Glenn Kasten98067102011-12-13 11:47:54 -0800772 Mutex::Autolock _l(mLock);
773 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700774}
775
776bool AudioFlinger::masterMute() const
777{
Glenn Kasten98067102011-12-13 11:47:54 -0800778 Mutex::Autolock _l(mLock);
779 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700780}
781
John Grossman4ff14ba2012-02-08 16:37:41 -0800782float AudioFlinger::masterVolume_l() const
783{
John Grossman4ff14ba2012-02-08 16:37:41 -0800784 return mMasterVolume;
785}
786
John Grossmand8f178d2012-07-20 14:51:35 -0700787bool AudioFlinger::masterMute_l() const
788{
John Grossmanee578c02012-07-23 17:05:46 -0700789 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700790}
791
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800792status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
793 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794{
795 // check calling permissions
796 if (!settingsAllowed()) {
797 return PERMISSION_DENIED;
798 }
799
Glenn Kasten263709e2012-01-06 08:40:01 -0800800 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000801 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802 return BAD_VALUE;
803 }
804
805 AutoMutex lock(mLock);
806 PlaybackThread *thread = NULL;
807 if (output) {
808 thread = checkPlaybackThread_l(output);
809 if (thread == NULL) {
810 return BAD_VALUE;
811 }
812 }
813
814 mStreamTypes[stream].volume = value;
815
816 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800817 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700818 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 }
820 } else {
821 thread->setStreamVolume(stream, value);
822 }
823
824 return NO_ERROR;
825}
826
Glenn Kastenfff6d712012-01-12 16:38:12 -0800827status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828{
829 // check calling permissions
830 if (!settingsAllowed()) {
831 return PERMISSION_DENIED;
832 }
833
Glenn Kasten263709e2012-01-06 08:40:01 -0800834 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700835 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000836 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837 return BAD_VALUE;
838 }
839
Eric Laurent93575202011-01-18 18:39:02 -0800840 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841 mStreamTypes[stream].mute = muted;
842 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700843 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844
845 return NO_ERROR;
846}
847
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800848float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849{
Glenn Kasten263709e2012-01-06 08:40:01 -0800850 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851 return 0.0f;
852 }
853
854 AutoMutex lock(mLock);
855 float volume;
856 if (output) {
857 PlaybackThread *thread = checkPlaybackThread_l(output);
858 if (thread == NULL) {
859 return 0.0f;
860 }
861 volume = thread->streamVolume(stream);
862 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800863 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700864 }
865
866 return volume;
867}
868
Glenn Kastenfff6d712012-01-12 16:38:12 -0800869bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870{
Glenn Kasten263709e2012-01-06 08:40:01 -0800871 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 return true;
873 }
874
Glenn Kasten6637baa2012-01-09 09:40:36 -0800875 AutoMutex lock(mLock);
876 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700877}
878
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800879status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700881 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
882 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800883
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884 // check calling permissions
885 if (!settingsAllowed()) {
886 return PERMISSION_DENIED;
887 }
888
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889 // ioHandle == 0 means the parameters are global to the audio hardware interface
890 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700891 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700892 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800893 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700894 AutoMutex lock(mHardwareLock);
895 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
896 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
897 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
898 status_t result = dev->set_parameters(dev, keyValuePairs.string());
899 final_result = result ?: final_result;
900 }
901 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800902 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700903 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
904 AudioParameter param = AudioParameter(keyValuePairs);
905 String8 value;
906 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700907 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
908 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700909 for (size_t i = 0; i < mRecordThreads.size(); i++) {
910 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700911 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700912 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
913 // collect all of the thread's session IDs
914 KeyedVector<int, bool> ids = thread->sessionIds();
915 // suspend effects associated with those session IDs
916 for (size_t j = 0; j < ids.size(); ++j) {
917 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700918 thread->setEffectSuspended(FX_IID_AEC,
919 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700920 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700921 thread->setEffectSuspended(FX_IID_NS,
922 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700923 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700924 }
925 }
Eric Laurentbee53372011-08-29 12:42:48 -0700926 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700927 }
928 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700929 String8 screenState;
930 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
931 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800932 if (isOff != (AudioFlinger::mScreenState & 1)) {
933 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700934 }
935 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700936 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937 }
938
939 // hold a strong ref on thread in case closeOutput() or closeInput() is called
940 // and the thread is exited once the lock is released
941 sp<ThreadBase> thread;
942 {
943 Mutex::Autolock _l(mLock);
944 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700945 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800947 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700948 // indicate output device change to all input threads for pre processing
949 AudioParameter param = AudioParameter(keyValuePairs);
950 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700951 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
952 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700953 for (size_t i = 0; i < mRecordThreads.size(); i++) {
954 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
955 }
956 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957 }
958 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800959 if (thread != 0) {
960 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961 }
962 return BAD_VALUE;
963}
964
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800965String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700966{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700967 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
968 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969
Eric Laurenta4c5a552012-03-29 10:12:40 -0700970 Mutex::Autolock _l(mLock);
971
Mathias Agopian65ab4712010-07-14 17:59:35 -0700972 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700973 String8 out_s8;
974
Dima Zavin799a70e2011-04-18 16:57:27 -0700975 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800976 char *s;
977 {
978 AutoMutex lock(mHardwareLock);
979 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700980 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800981 s = dev->get_parameters(dev, keys.string());
982 mHardwareStatus = AUDIO_HW_IDLE;
983 }
John Grossmanef7740b2012-02-09 11:28:36 -0800984 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700985 free(s);
986 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700987 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700988 }
989
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
991 if (playbackThread != NULL) {
992 return playbackThread->getParameters(keys);
993 }
994 RecordThread *recordThread = checkRecordThread_l(ioHandle);
995 if (recordThread != NULL) {
996 return recordThread->getParameters(keys);
997 }
998 return String8("");
999}
1000
Glenn Kastendd8104c2012-07-02 12:42:44 -07001001size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1002 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001003{
Eric Laurenta1884f92011-08-23 08:25:03 -07001004 status_t ret = initCheck();
1005 if (ret != NO_ERROR) {
1006 return 0;
1007 }
1008
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001009 AutoMutex lock(mHardwareLock);
1010 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001011 struct audio_config config;
1012 memset(&config, 0, sizeof(config));
1013 config.sample_rate = sampleRate;
1014 config.channel_mask = channelMask;
1015 config.format = format;
1016
John Grossmanee578c02012-07-23 17:05:46 -07001017 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1018 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001019 mHardwareStatus = AUDIO_HW_IDLE;
1020 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021}
1022
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001023unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001024{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001025 Mutex::Autolock _l(mLock);
1026
1027 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1028 if (recordThread != NULL) {
1029 return recordThread->getInputFramesLost();
1030 }
1031 return 0;
1032}
1033
1034status_t AudioFlinger::setVoiceVolume(float value)
1035{
Eric Laurenta1884f92011-08-23 08:25:03 -07001036 status_t ret = initCheck();
1037 if (ret != NO_ERROR) {
1038 return ret;
1039 }
1040
Mathias Agopian65ab4712010-07-14 17:59:35 -07001041 // check calling permissions
1042 if (!settingsAllowed()) {
1043 return PERMISSION_DENIED;
1044 }
1045
1046 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001047 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001048 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001049 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050 mHardwareStatus = AUDIO_HW_IDLE;
1051
1052 return ret;
1053}
1054
Glenn Kasten26c77552012-11-16 12:01:44 -08001055status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001056 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001057{
1058 status_t status;
1059
1060 Mutex::Autolock _l(mLock);
1061
1062 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1063 if (playbackThread != NULL) {
1064 return playbackThread->getRenderPosition(halFrames, dspFrames);
1065 }
1066
1067 return BAD_VALUE;
1068}
1069
1070void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1071{
1072
1073 Mutex::Autolock _l(mLock);
1074
Glenn Kastenbb001922012-02-03 11:10:26 -08001075 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001076 if (mNotificationClients.indexOfKey(pid) < 0) {
1077 sp<NotificationClient> notificationClient = new NotificationClient(this,
1078 client,
1079 pid);
Steve Block3856b092011-10-20 11:56:00 +01001080 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001081
1082 mNotificationClients.add(pid, notificationClient);
1083
1084 sp<IBinder> binder = client->asBinder();
1085 binder->linkToDeath(notificationClient);
1086
1087 // the config change is always sent from playback or record threads to avoid deadlock
1088 // with AudioSystem::gLock
1089 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001090 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001091 }
1092
1093 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001094 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095 }
1096 }
1097}
1098
1099void AudioFlinger::removeNotificationClient(pid_t pid)
1100{
1101 Mutex::Autolock _l(mLock);
1102
Glenn Kastena3b09252012-01-20 09:19:01 -08001103 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001104
Steve Block3856b092011-10-20 11:56:00 +01001105 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001106 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001107 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001108 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001109 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001110 ALOGV(" pid %d @ %d", ref->mPid, i);
1111 if (ref->mPid == pid) {
1112 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001113 mAudioSessionRefs.removeAt(i);
1114 delete ref;
1115 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001116 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001117 } else {
1118 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001119 }
1120 }
1121 if (removed) {
1122 purgeStaleEffects_l();
1123 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001124}
1125
1126// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001127void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128{
1129 size_t size = mNotificationClients.size();
1130 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001131 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1132 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133 }
1134}
1135
1136// removeClient_l() must be called with AudioFlinger::mLock held
1137void AudioFlinger::removeClient_l(pid_t pid)
1138{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001139 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001140 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141 mClients.removeItem(pid);
1142}
1143
Eric Laurent717e1282012-06-29 16:36:52 -07001144// getEffectThread_l() must be called with AudioFlinger::mLock held
1145sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1146{
1147 sp<PlaybackThread> thread;
1148
1149 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1150 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1151 ALOG_ASSERT(thread == 0);
1152 thread = mPlaybackThreads.valueAt(i);
1153 }
1154 }
1155
1156 return thread;
1157}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158
Mathias Agopian65ab4712010-07-14 17:59:35 -07001159
Mathias Agopian65ab4712010-07-14 17:59:35 -07001160
1161// ----------------------------------------------------------------------------
1162
1163AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1164 : RefBase(),
1165 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001166 // 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 -07001167 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001168 mPid(pid),
1169 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170{
1171 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1172}
1173
1174// Client destructor must be called with AudioFlinger::mLock held
1175AudioFlinger::Client::~Client()
1176{
1177 mAudioFlinger->removeClient_l(mPid);
1178}
1179
Glenn Kasten435dbe62012-01-30 10:15:48 -08001180sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001181{
1182 return mMemoryDealer;
1183}
1184
John Grossman4ff14ba2012-02-08 16:37:41 -08001185// Reserve one of the limited slots for a timed audio track associated
1186// with this client
1187bool AudioFlinger::Client::reserveTimedTrack()
1188{
1189 const int kMaxTimedTracksPerClient = 4;
1190
1191 Mutex::Autolock _l(mTimedTrackLock);
1192
1193 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1194 ALOGW("can not create timed track - pid %d has exceeded the limit",
1195 mPid);
1196 return false;
1197 }
1198
1199 mTimedTrackCount++;
1200 return true;
1201}
1202
1203// Release a slot for a timed audio track
1204void AudioFlinger::Client::releaseTimedTrack()
1205{
1206 Mutex::Autolock _l(mTimedTrackLock);
1207 mTimedTrackCount--;
1208}
1209
Mathias Agopian65ab4712010-07-14 17:59:35 -07001210// ----------------------------------------------------------------------------
1211
1212AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1213 const sp<IAudioFlingerClient>& client,
1214 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001215 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001216{
1217}
1218
1219AudioFlinger::NotificationClient::~NotificationClient()
1220{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001221}
1222
1223void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1224{
1225 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001226 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001227}
1228
Mathias Agopian65ab4712010-07-14 17:59:35 -07001229
1230// ----------------------------------------------------------------------------
1231
Jeff Brown893a5642013-08-16 20:19:26 -07001232static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1233 return audio_is_remote_submix_device(inDevice);
1234}
1235
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001237 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001239 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001240 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001241 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001242 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001243 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 int *sessionId,
1245 status_t *status)
1246{
1247 sp<RecordThread::RecordTrack> recordTrack;
1248 sp<RecordHandle> recordHandle;
1249 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001250 status_t lStatus;
1251 RecordThread *thread;
1252 size_t inFrameCount;
1253 int lSessionId;
1254
1255 // check calling permissions
1256 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001257 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001258 lStatus = PERMISSION_DENIED;
1259 goto Exit;
1260 }
1261
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001262 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1263 ALOGE("openRecord() invalid format %d", format);
1264 lStatus = BAD_VALUE;
1265 goto Exit;
1266 }
1267
Mathias Agopian65ab4712010-07-14 17:59:35 -07001268 // add client to list
1269 { // scope for mLock
1270 Mutex::Autolock _l(mLock);
1271 thread = checkRecordThread_l(input);
1272 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001273 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001274 lStatus = BAD_VALUE;
1275 goto Exit;
1276 }
1277
Jeff Brown893a5642013-08-16 20:19:26 -07001278 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1279 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001280 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001281 lStatus = PERMISSION_DENIED;
1282 goto Exit;
1283 }
1284
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001285 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001286 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001287
1288 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001289 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001290 lSessionId = *sessionId;
1291 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001292 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001293 if (sessionId != NULL) {
1294 *sessionId = lSessionId;
1295 }
1296 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001297 // create new record track.
1298 // The record track uses one track in mHardwareMixerThread by convention.
Marco Nelissen9cae2172013-01-14 14:12:05 -08001299 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001300 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen9cae2172013-01-14 14:12:05 -08001301 frameCount, lSessionId,
1302 IPCThreadState::self()->getCallingUid(),
1303 flags, tid, &lStatus);
Haynes Mathew Georgee010f652013-12-13 15:40:13 -08001304 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001306 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001307 // remove local strong reference to Client before deleting the RecordTrack so that the
1308 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001309 client.clear();
1310 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001311 goto Exit;
1312 }
1313
1314 // return to handle to client
1315 recordHandle = new RecordHandle(recordTrack);
1316 lStatus = NO_ERROR;
1317
1318Exit:
1319 if (status) {
1320 *status = lStatus;
1321 }
1322 return recordHandle;
1323}
1324
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001325
1326
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327// ----------------------------------------------------------------------------
1328
Eric Laurenta4c5a552012-03-29 10:12:40 -07001329audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1330{
1331 if (!settingsAllowed()) {
1332 return 0;
1333 }
1334 Mutex::Autolock _l(mLock);
1335 return loadHwModule_l(name);
1336}
1337
1338// loadHwModule_l() must be called with AudioFlinger::mLock held
1339audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1340{
1341 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1342 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1343 ALOGW("loadHwModule() module %s already loaded", name);
1344 return mAudioHwDevs.keyAt(i);
1345 }
1346 }
1347
Eric Laurenta4c5a552012-03-29 10:12:40 -07001348 audio_hw_device_t *dev;
1349
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001350 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001351 if (rc) {
1352 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1353 return 0;
1354 }
1355
1356 mHardwareStatus = AUDIO_HW_INIT;
1357 rc = dev->init_check(dev);
1358 mHardwareStatus = AUDIO_HW_IDLE;
1359 if (rc) {
1360 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1361 return 0;
1362 }
1363
John Grossmanee578c02012-07-23 17:05:46 -07001364 // Check and cache this HAL's level of support for master mute and master
1365 // volume. If this is the first HAL opened, and it supports the get
1366 // methods, use the initial values provided by the HAL as the current
1367 // master mute and volume settings.
1368
1369 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1370 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001371 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001372
1373 if (0 == mAudioHwDevs.size()) {
1374 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1375 if (NULL != dev->get_master_volume) {
1376 float mv;
1377 if (OK == dev->get_master_volume(dev, &mv)) {
1378 mMasterVolume = mv;
1379 }
1380 }
1381
1382 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1383 if (NULL != dev->get_master_mute) {
1384 bool mm;
1385 if (OK == dev->get_master_mute(dev, &mm)) {
1386 mMasterMute = mm;
1387 }
1388 }
1389 }
1390
Eric Laurenta4c5a552012-03-29 10:12:40 -07001391 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001392 if ((NULL != dev->set_master_volume) &&
1393 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1394 flags = static_cast<AudioHwDevice::Flags>(flags |
1395 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1396 }
1397
1398 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1399 if ((NULL != dev->set_master_mute) &&
1400 (OK == dev->set_master_mute(dev, mMasterMute))) {
1401 flags = static_cast<AudioHwDevice::Flags>(flags |
1402 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1403 }
1404
Eric Laurenta4c5a552012-03-29 10:12:40 -07001405 mHardwareStatus = AUDIO_HW_IDLE;
1406 }
1407
1408 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001409 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001410
1411 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001412 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001413
1414 return handle;
1415
1416}
1417
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001418// ----------------------------------------------------------------------------
1419
Glenn Kasten3b16c762012-11-14 08:44:39 -08001420uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001421{
1422 Mutex::Autolock _l(mLock);
1423 PlaybackThread *thread = primaryPlaybackThread_l();
1424 return thread != NULL ? thread->sampleRate() : 0;
1425}
1426
Glenn Kastene33054e2012-11-14 12:54:39 -08001427size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001428{
1429 Mutex::Autolock _l(mLock);
1430 PlaybackThread *thread = primaryPlaybackThread_l();
1431 return thread != NULL ? thread->frameCountHAL() : 0;
1432}
1433
1434// ----------------------------------------------------------------------------
1435
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001436status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1437{
1438 uid_t uid = IPCThreadState::self()->getCallingUid();
1439 if (uid != AID_SYSTEM) {
1440 return PERMISSION_DENIED;
1441 }
1442 Mutex::Autolock _l(mLock);
1443 if (mIsDeviceTypeKnown) {
1444 return INVALID_OPERATION;
1445 }
1446 mIsLowRamDevice = isLowRamDevice;
1447 mIsDeviceTypeKnown = true;
1448 return NO_ERROR;
1449}
1450
1451// ----------------------------------------------------------------------------
1452
Eric Laurenta4c5a552012-03-29 10:12:40 -07001453audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1454 audio_devices_t *pDevices,
1455 uint32_t *pSamplingRate,
1456 audio_format_t *pFormat,
1457 audio_channel_mask_t *pChannelMask,
1458 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001459 audio_output_flags_t flags,
1460 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001461{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001462 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001463 struct audio_config config;
1464 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1465 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1466 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1467 if (offloadInfo) {
1468 config.offload_info = *offloadInfo;
1469 }
1470
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001471 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001472 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473
Eric Laurentbfb1b832013-01-07 09:53:42 -08001474 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001475 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001476 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001477 config.sample_rate,
1478 config.format,
1479 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001480 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001481 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1482 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001483
1484 if (pDevices == NULL || *pDevices == 0) {
1485 return 0;
1486 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001487
Mathias Agopian65ab4712010-07-14 17:59:35 -07001488 Mutex::Autolock _l(mLock);
1489
Eric Laurenta4c5a552012-03-29 10:12:40 -07001490 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001491 if (outHwDev == NULL)
1492 return 0;
1493
John Grossmanee578c02012-07-23 17:05:46 -07001494 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001495 audio_io_handle_t id = nextUniqueId();
1496
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001497 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001498
Glenn Kasten34542ac2013-06-26 11:29:02 -07001499 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001500 id,
1501 *pDevices,
1502 (audio_output_flags_t)flags,
1503 &config,
1504 &outStream);
1505
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001506 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001507 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001508 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001509 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001510 config.sample_rate,
1511 config.format,
1512 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001513 status);
1514
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001515 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001516 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001517
Eric Laurentbfb1b832013-01-07 09:53:42 -08001518 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1519 thread = new OffloadThread(this, output, id, *pDevices);
1520 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1521 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001522 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1523 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001524 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001525 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001526 } else {
1527 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001528 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529 }
1530 mPlaybackThreads.add(id, thread);
1531
Glenn Kasten7c027242012-12-26 14:43:16 -08001532 if (pSamplingRate != NULL) {
1533 *pSamplingRate = config.sample_rate;
1534 }
1535 if (pFormat != NULL) {
1536 *pFormat = config.format;
1537 }
1538 if (pChannelMask != NULL) {
1539 *pChannelMask = config.channel_mask;
1540 }
1541 if (pLatencyMs != NULL) {
1542 *pLatencyMs = thread->latency();
1543 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001544
1545 // notify client processes of the new output creation
1546 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001547
1548 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001549 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001550 ALOGI("Using module %d has the primary audio interface", module);
1551 mPrimaryHardwareDev = outHwDev;
1552
1553 AutoMutex lock(mHardwareLock);
1554 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001555 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001556 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001557 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001558 return id;
1559 }
1560
1561 return 0;
1562}
1563
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001564audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1565 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001566{
1567 Mutex::Autolock _l(mLock);
1568 MixerThread *thread1 = checkMixerThread_l(output1);
1569 MixerThread *thread2 = checkMixerThread_l(output2);
1570
1571 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001572 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1573 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001574 return 0;
1575 }
1576
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001577 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001578 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1579 thread->addOutputTrack(thread2);
1580 mPlaybackThreads.add(id, thread);
1581 // notify client processes of the new output creation
1582 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1583 return id;
1584}
1585
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001586status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001587{
Glenn Kastend96c5722012-04-25 13:44:49 -07001588 return closeOutput_nonvirtual(output);
1589}
1590
1591status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1592{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001593 // keep strong reference on the playback thread so that
1594 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001595 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001596 {
1597 Mutex::Autolock _l(mLock);
1598 thread = checkPlaybackThread_l(output);
1599 if (thread == NULL) {
1600 return BAD_VALUE;
1601 }
1602
Steve Block3856b092011-10-20 11:56:00 +01001603 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001604
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001605 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001606 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent22ac20e2015-05-08 10:50:03 -07001607 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001608 DuplicatingThread *dupThread =
1609 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001610 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001611 }
1612 }
1613 }
1614
1615
1616 mPlaybackThreads.removeItem(output);
1617 // save all effects to the default thread
1618 if (mPlaybackThreads.size()) {
1619 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1620 if (dstThread != NULL) {
1621 // audioflinger lock is held here so the acquisition order of thread locks does not
1622 // matter
1623 Mutex::Autolock _dl(dstThread->mLock);
1624 Mutex::Autolock _sl(thread->mLock);
1625 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1626 for (size_t i = 0; i < effectChains.size(); i ++) {
1627 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001628 }
1629 }
1630 }
Glenn Kastena1117922012-01-26 10:53:32 -08001631 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001632 }
1633 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001634 // The thread entity (active unit of execution) is no longer running here,
1635 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001636
Eric Laurent22ac20e2015-05-08 10:50:03 -07001637 if (!thread->isDuplicating()) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001638 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001639 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001640 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001641 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001642 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001643 }
1644 return NO_ERROR;
1645}
1646
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001647status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648{
1649 Mutex::Autolock _l(mLock);
1650 PlaybackThread *thread = checkPlaybackThread_l(output);
1651
1652 if (thread == NULL) {
1653 return BAD_VALUE;
1654 }
1655
Steve Block3856b092011-10-20 11:56:00 +01001656 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001657 thread->suspend();
1658
1659 return NO_ERROR;
1660}
1661
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001662status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001663{
1664 Mutex::Autolock _l(mLock);
1665 PlaybackThread *thread = checkPlaybackThread_l(output);
1666
1667 if (thread == NULL) {
1668 return BAD_VALUE;
1669 }
1670
Steve Block3856b092011-10-20 11:56:00 +01001671 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001672
1673 thread->restore();
1674
1675 return NO_ERROR;
1676}
1677
Eric Laurenta4c5a552012-03-29 10:12:40 -07001678audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1679 audio_devices_t *pDevices,
1680 uint32_t *pSamplingRate,
1681 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001682 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001683{
1684 status_t status;
1685 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001686 struct audio_config config;
1687 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1688 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1689 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1690
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001691 uint32_t reqSamplingRate = config.sample_rate;
1692 audio_format_t reqFormat = config.format;
1693 audio_channel_mask_t reqChannels = config.channel_mask;
1694 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001695 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001696
1697 if (pDevices == NULL || *pDevices == 0) {
1698 return 0;
1699 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001700
Mathias Agopian65ab4712010-07-14 17:59:35 -07001701 Mutex::Autolock _l(mLock);
1702
Eric Laurenta4c5a552012-03-29 10:12:40 -07001703 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001704 if (inHwDev == NULL)
1705 return 0;
1706
John Grossmanee578c02012-07-23 17:05:46 -07001707 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001708 audio_io_handle_t id = nextUniqueId();
1709
John Grossmanee578c02012-07-23 17:05:46 -07001710 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001711 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001712 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1713 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001714 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001715 config.sample_rate,
1716 config.format,
1717 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001718 status);
1719
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001720 // If the input could not be opened with the requested parameters and we can handle the
1721 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1722 // 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 -07001723 if (status == BAD_VALUE &&
1724 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1725 (config.sample_rate <= 2 * reqSamplingRate) &&
1726 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001727 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001728 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001729 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001730 }
1731
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001732 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001733
Glenn Kasten46909e72013-02-26 09:20:22 -08001734#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001735 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1736 // or (re-)create if current Pipe is idle and does not match the new format
1737 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001738 enum {
1739 TEE_SINK_NO, // don't copy input
1740 TEE_SINK_NEW, // copy input using a new pipe
1741 TEE_SINK_OLD, // copy input using an existing pipe
1742 } kind;
1743 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1744 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001745 if (!mTeeSinkInputEnabled) {
1746 kind = TEE_SINK_NO;
1747 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001748 kind = TEE_SINK_NO;
1749 } else if (mRecordTeeSink == 0) {
1750 kind = TEE_SINK_NEW;
1751 } else if (mRecordTeeSink->getStrongCount() != 1) {
1752 kind = TEE_SINK_NO;
1753 } else if (format == mRecordTeeSink->format()) {
1754 kind = TEE_SINK_OLD;
1755 } else {
1756 kind = TEE_SINK_NEW;
1757 }
1758 switch (kind) {
1759 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001760 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001761 size_t numCounterOffers = 0;
1762 const NBAIO_Format offers[1] = {format};
1763 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1764 ALOG_ASSERT(index == 0);
1765 PipeReader *pipeReader = new PipeReader(*pipe);
1766 numCounterOffers = 0;
1767 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1768 ALOG_ASSERT(index == 0);
1769 mRecordTeeSink = pipe;
1770 mRecordTeeSource = pipeReader;
1771 teeSink = pipe;
1772 }
1773 break;
1774 case TEE_SINK_OLD:
1775 teeSink = mRecordTeeSink;
1776 break;
1777 case TEE_SINK_NO:
1778 default:
1779 break;
1780 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001781#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001782
Dima Zavin799a70e2011-04-18 16:57:27 -07001783 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1784
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001785 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001786 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001787 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001788 thread = new RecordThread(this,
1789 input,
1790 reqSamplingRate,
1791 reqChannels,
1792 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001793 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001794 *pDevices
1795#ifdef TEE_SINK
1796 , teeSink
1797#endif
1798 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001799 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001800 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001801 if (pSamplingRate != NULL) {
1802 *pSamplingRate = reqSamplingRate;
1803 }
1804 if (pFormat != NULL) {
1805 *pFormat = config.format;
1806 }
1807 if (pChannelMask != NULL) {
1808 *pChannelMask = reqChannels;
1809 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001810
Mathias Agopian65ab4712010-07-14 17:59:35 -07001811 // notify client processes of the new input creation
1812 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1813 return id;
1814 }
1815
1816 return 0;
1817}
1818
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001819status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001820{
Glenn Kastend96c5722012-04-25 13:44:49 -07001821 return closeInput_nonvirtual(input);
1822}
1823
1824status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1825{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001826 // keep strong reference on the record thread so that
1827 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001828 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001829 {
1830 Mutex::Autolock _l(mLock);
1831 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001832 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001833 return BAD_VALUE;
1834 }
1835
Steve Block3856b092011-10-20 11:56:00 +01001836 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001837 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001838 mRecordThreads.removeItem(input);
1839 }
1840 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001841 // The thread entity (active unit of execution) is no longer running here,
1842 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001843
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001844 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001845 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001846 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001847 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001848 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001849
1850 return NO_ERROR;
1851}
1852
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001853status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001854{
1855 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001856 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001857
1858 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1859 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001860 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001861 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001862
1863 return NO_ERROR;
1864}
1865
1866
1867int AudioFlinger::newAudioSessionId()
1868{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001869 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001870}
1871
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001872void AudioFlinger::acquireAudioSessionId(int audioSession)
1873{
1874 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001875 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001876 ALOGV("acquiring %d from %d", audioSession, caller);
Eric Laurentd1b28d42013-09-18 18:47:13 -07001877
1878 // Ignore requests received from processes not known as notification client. The request
1879 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1880 // called from a different pid leaving a stale session reference. Also we don't know how
1881 // to clear this reference if the client process dies.
1882 if (mNotificationClients.indexOfKey(caller) < 0) {
1883 ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
1884 return;
1885 }
1886
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001887 size_t num = mAudioSessionRefs.size();
1888 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001889 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001890 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1891 ref->mCnt++;
1892 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001893 return;
1894 }
1895 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001896 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1897 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001898}
1899
1900void AudioFlinger::releaseAudioSessionId(int audioSession)
1901{
1902 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001903 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001904 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001905 size_t num = mAudioSessionRefs.size();
1906 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001907 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001908 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1909 ref->mCnt--;
1910 ALOGV(" decremented refcount to %d", ref->mCnt);
1911 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001912 mAudioSessionRefs.removeAt(i);
1913 delete ref;
1914 purgeStaleEffects_l();
1915 }
1916 return;
1917 }
1918 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001919 // If the caller is mediaserver it is likely that the session being released was acquired
1920 // on behalf of a process not in notification clients and we ignore the warning.
1921 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001922}
1923
1924void AudioFlinger::purgeStaleEffects_l() {
1925
Steve Block3856b092011-10-20 11:56:00 +01001926 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001927
1928 Vector< sp<EffectChain> > chains;
1929
1930 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1931 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1932 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1933 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001934 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1935 chains.push(ec);
1936 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001937 }
1938 }
1939 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1940 sp<RecordThread> t = mRecordThreads.valueAt(i);
1941 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1942 sp<EffectChain> ec = t->mEffectChains[j];
1943 chains.push(ec);
1944 }
1945 }
1946
1947 for (size_t i = 0; i < chains.size(); i++) {
1948 sp<EffectChain> ec = chains[i];
1949 int sessionid = ec->sessionId();
1950 sp<ThreadBase> t = ec->mThread.promote();
1951 if (t == 0) {
1952 continue;
1953 }
1954 size_t numsessionrefs = mAudioSessionRefs.size();
1955 bool found = false;
1956 for (size_t k = 0; k < numsessionrefs; k++) {
1957 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001958 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001959 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001960 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001961 found = true;
1962 break;
1963 }
1964 }
1965 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001966 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001967 // remove all effects from the chain
1968 while (ec->mEffects.size()) {
1969 sp<EffectModule> effect = ec->mEffects[0];
1970 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001971 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001972 if (effect->purgeHandles()) {
1973 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001974 }
1975 AudioSystem::unregisterEffect(effect->id());
1976 }
1977 }
1978 }
1979 return;
1980}
1981
Mathias Agopian65ab4712010-07-14 17:59:35 -07001982// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001983AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001984{
Glenn Kastena1117922012-01-26 10:53:32 -08001985 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001986}
1987
1988// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001989AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001990{
1991 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001992 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001993}
1994
1995// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001996AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001997{
Glenn Kastena1117922012-01-26 10:53:32 -08001998 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001999}
2000
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002001uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002002{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002003 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002004}
2005
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002006AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002007{
2008 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2009 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22ac20e2015-05-08 10:50:03 -07002010 if(thread->isDuplicating()) {
2011 continue;
2012 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002013 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002014 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002015 return thread;
2016 }
2017 }
2018 return NULL;
2019}
2020
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002021audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002022{
2023 PlaybackThread *thread = primaryPlaybackThread_l();
2024
2025 if (thread == NULL) {
2026 return 0;
2027 }
2028
Eric Laurentf1c04f92012-08-28 14:26:53 -07002029 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002030}
2031
Eric Laurenta011e352012-03-29 15:51:43 -07002032sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2033 int triggerSession,
2034 int listenerSession,
2035 sync_event_callback_t callBack,
2036 void *cookie)
2037{
2038 Mutex::Autolock _l(mLock);
2039
2040 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2041 status_t playStatus = NAME_NOT_FOUND;
2042 status_t recStatus = NAME_NOT_FOUND;
2043 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2044 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2045 if (playStatus == NO_ERROR) {
2046 return event;
2047 }
2048 }
2049 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2050 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2051 if (recStatus == NO_ERROR) {
2052 return event;
2053 }
2054 }
2055 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2056 mPendingSyncEvents.add(event);
2057 } else {
2058 ALOGV("createSyncEvent() invalid event %d", event->type());
2059 event.clear();
2060 }
2061 return event;
2062}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002063
Mathias Agopian65ab4712010-07-14 17:59:35 -07002064// ----------------------------------------------------------------------------
2065// Effect management
2066// ----------------------------------------------------------------------------
2067
2068
Glenn Kastenf587ba52012-01-26 16:25:10 -08002069status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002070{
2071 Mutex::Autolock _l(mLock);
2072 return EffectQueryNumberEffects(numEffects);
2073}
2074
Glenn Kastenf587ba52012-01-26 16:25:10 -08002075status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002076{
2077 Mutex::Autolock _l(mLock);
2078 return EffectQueryEffect(index, descriptor);
2079}
2080
Glenn Kasten5e92a782012-01-30 07:40:52 -08002081status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002082 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002083{
2084 Mutex::Autolock _l(mLock);
2085 return EffectGetDescriptor(pUuid, descriptor);
2086}
2087
2088
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002089sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002090 effect_descriptor_t *pDesc,
2091 const sp<IEffectClient>& effectClient,
2092 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002093 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002094 int sessionId,
2095 status_t *status,
2096 int *id,
2097 int *enabled)
2098{
2099 status_t lStatus = NO_ERROR;
2100 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002101 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002102
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002103 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002104 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002105 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002106
2107 if (pDesc == NULL) {
2108 lStatus = BAD_VALUE;
2109 goto Exit;
2110 }
2111
Eric Laurent84e9a102010-09-23 16:10:16 -07002112 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002113 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002114 lStatus = PERMISSION_DENIED;
2115 goto Exit;
2116 }
2117
Dima Zavinfce7a472011-04-19 22:30:36 -07002118 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002119 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002120 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002121 lStatus = PERMISSION_DENIED;
2122 goto Exit;
2123 }
2124
Mathias Agopian65ab4712010-07-14 17:59:35 -07002125 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002126 if (!EffectIsNullUuid(&pDesc->uuid)) {
2127 // if uuid is specified, request effect descriptor
2128 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2129 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002130 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002131 goto Exit;
2132 }
2133 } else {
2134 // if uuid is not specified, look for an available implementation
2135 // of the required type in effect factory
2136 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002137 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002138 lStatus = BAD_VALUE;
2139 goto Exit;
2140 }
2141 uint32_t numEffects = 0;
2142 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002143 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002144 bool found = false;
2145
2146 lStatus = EffectQueryNumberEffects(&numEffects);
2147 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002148 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002149 goto Exit;
2150 }
2151 for (uint32_t i = 0; i < numEffects; i++) {
2152 lStatus = EffectQueryEffect(i, &desc);
2153 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002154 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002155 continue;
2156 }
2157 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2158 // If matching type found save effect descriptor. If the session is
2159 // 0 and the effect is not auxiliary, continue enumeration in case
2160 // an auxiliary version of this effect type is available
2161 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002162 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002163 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002164 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2165 break;
2166 }
2167 }
2168 }
2169 if (!found) {
2170 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002171 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002172 goto Exit;
2173 }
2174 // For same effect type, chose auxiliary version over insert version if
2175 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002176 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002177 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002178 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002179 }
2180 }
2181
2182 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002183 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2185 lStatus = INVALID_OPERATION;
2186 goto Exit;
2187 }
2188
Eric Laurent59255e42011-07-27 19:49:51 -07002189 // check recording permission for visualizer
2190 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2191 !recordingAllowed()) {
2192 lStatus = PERMISSION_DENIED;
2193 goto Exit;
2194 }
2195
Mathias Agopian65ab4712010-07-14 17:59:35 -07002196 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002197 *pDesc = desc;
Eric Laurenteb3c3372013-09-25 12:25:29 -07002198 if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2199 // if the output returned by getOutputForEffect() is removed before we lock the
2200 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2201 // and we will exit safely
2202 io = AudioSystem::getOutputForEffect(&desc);
2203 ALOGV("createEffect got output %d", io);
2204 }
2205
2206 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002207
2208 // If output is not specified try to find a matching audio session ID in one of the
2209 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002210 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2211 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002212 // Note: io is never 0 when creating an effect on an input
2213 if (io == 0) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002214 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2215 // output must be specified by AudioPolicyManager when using session
2216 // AUDIO_SESSION_OUTPUT_STAGE
2217 lStatus = BAD_VALUE;
2218 goto Exit;
2219 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002220 // look for the thread where the specified audio session is present
2221 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2222 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2223 io = mPlaybackThreads.keyAt(i);
2224 break;
2225 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002226 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002227 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002228 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2229 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2230 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002231 break;
2232 }
2233 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002234 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002235 // If no output thread contains the requested session ID, default to
2236 // first output. The effect chain will be moved to the correct output
2237 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002238 if (io == 0 && mPlaybackThreads.size()) {
2239 io = mPlaybackThreads.keyAt(0);
2240 }
Steve Block3856b092011-10-20 11:56:00 +01002241 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002242 }
2243 ThreadBase *thread = checkRecordThread_l(io);
2244 if (thread == NULL) {
2245 thread = checkPlaybackThread_l(io);
2246 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002247 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002248 lStatus = BAD_VALUE;
2249 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002250 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002251 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002252
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002253 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002255 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002256 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2257 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002258 if (handle != 0 && id != NULL) {
2259 *id = handle->id();
2260 }
2261 }
2262
2263Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002264 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002265 *status = lStatus;
2266 }
2267 return handle;
2268}
2269
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002270status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2271 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002272{
Steve Block3856b092011-10-20 11:56:00 +01002273 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002274 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002275 Mutex::Autolock _l(mLock);
2276 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002277 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002278 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002279 }
Eric Laurentde070132010-07-13 04:45:46 -07002280 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2281 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002282 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002283 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002284 }
Eric Laurentde070132010-07-13 04:45:46 -07002285 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2286 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002287 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002288 return BAD_VALUE;
2289 }
2290
2291 Mutex::Autolock _dl(dstThread->mLock);
2292 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002293 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002294}
2295
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002296// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002297status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002298 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002299 AudioFlinger::PlaybackThread *dstThread,
2300 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002301{
Steve Block3856b092011-10-20 11:56:00 +01002302 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002303 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002304
Eric Laurent59255e42011-07-27 19:49:51 -07002305 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002306 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002307 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002308 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002309 return INVALID_OPERATION;
2310 }
2311
Eric Laurent39e94f82010-07-28 01:32:47 -07002312 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002313 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002314 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002315 // removed.
2316 srcThread->removeEffectChain_l(chain);
2317
2318 // transfer all effects one by one so that new effect chain is created on new thread with
2319 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002320 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002321 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002322 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002323 Vector< sp<EffectModule> > removed;
2324 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002325 while (effect != 0) {
2326 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002327 removed.add(effect);
2328 status = dstThread->addEffect_l(effect);
2329 if (status != NO_ERROR) {
2330 break;
2331 }
Eric Laurentec35a142011-10-05 17:42:25 -07002332 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2333 if (effect->state() == EffectModule::ACTIVE ||
2334 effect->state() == EffectModule::STOPPING) {
2335 effect->start();
2336 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002337 // if the move request is not received from audio policy manager, the effect must be
2338 // re-registered with the new strategy and output
2339 if (dstChain == 0) {
2340 dstChain = effect->chain().promote();
2341 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002342 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002343 status = NO_INIT;
2344 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002345 }
2346 strategy = dstChain->strategy();
2347 }
2348 if (reRegister) {
2349 AudioSystem::unregisterEffect(effect->id());
2350 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002351 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002352 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002353 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002354 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002355 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002356 }
Eric Laurentde070132010-07-13 04:45:46 -07002357 effect = chain->getEffectFromId_l(0);
2358 }
2359
Eric Laurent5baf2af2013-09-12 17:37:00 -07002360 if (status != NO_ERROR) {
2361 for (size_t i = 0; i < removed.size(); i++) {
2362 srcThread->addEffect_l(removed[i]);
2363 if (dstChain != 0 && reRegister) {
2364 AudioSystem::unregisterEffect(removed[i]->id());
2365 AudioSystem::registerEffect(&removed[i]->desc(),
2366 srcThread->id(),
2367 strategy,
2368 sessionId,
2369 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002370 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002371 }
2372 }
2373 }
2374
2375 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002376}
2377
Eric Laurent5baf2af2013-09-12 17:37:00 -07002378bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002379{
2380 if (mGlobalEffectEnableTime != 0 &&
2381 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2382 return true;
2383 }
2384
2385 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2386 sp<EffectChain> ec =
2387 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002388 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002389 return true;
2390 }
2391 }
2392 return false;
2393}
2394
Eric Laurent5baf2af2013-09-12 17:37:00 -07002395void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002396{
2397 Mutex::Autolock _l(mLock);
2398
2399 mGlobalEffectEnableTime = systemTime();
2400
2401 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2402 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2403 if (t->mType == ThreadBase::OFFLOAD) {
2404 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2405 }
2406 }
2407
2408}
2409
Glenn Kastenda6ef132013-01-10 12:31:01 -08002410struct Entry {
2411#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2412 char mName[MAX_NAME];
2413};
2414
2415int comparEntry(const void *p1, const void *p2)
2416{
2417 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2418}
2419
Glenn Kasten46909e72013-02-26 09:20:22 -08002420#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002421void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002422{
Eric Laurent81784c32012-11-19 14:55:58 -08002423 NBAIO_Source *teeSource = source.get();
2424 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002425 // .wav rotation
2426 // There is a benign race condition if 2 threads call this simultaneously.
2427 // They would both traverse the directory, but the result would simply be
2428 // failures at unlink() which are ignored. It's also unlikely since
2429 // normally dumpsys is only done by bugreport or from the command line.
2430 char teePath[32+256];
2431 strcpy(teePath, "/data/misc/media");
2432 size_t teePathLen = strlen(teePath);
2433 DIR *dir = opendir(teePath);
2434 teePath[teePathLen++] = '/';
2435 if (dir != NULL) {
2436#define MAX_SORT 20 // number of entries to sort
2437#define MAX_KEEP 10 // number of entries to keep
2438 struct Entry entries[MAX_SORT];
2439 size_t entryCount = 0;
2440 while (entryCount < MAX_SORT) {
2441 struct dirent de;
2442 struct dirent *result = NULL;
2443 int rc = readdir_r(dir, &de, &result);
2444 if (rc != 0) {
2445 ALOGW("readdir_r failed %d", rc);
2446 break;
2447 }
2448 if (result == NULL) {
2449 break;
2450 }
2451 if (result != &de) {
2452 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2453 break;
2454 }
2455 // ignore non .wav file entries
2456 size_t nameLen = strlen(de.d_name);
2457 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2458 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2459 continue;
2460 }
2461 strcpy(entries[entryCount++].mName, de.d_name);
2462 }
2463 (void) closedir(dir);
2464 if (entryCount > MAX_KEEP) {
2465 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2466 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2467 strcpy(&teePath[teePathLen], entries[i].mName);
2468 (void) unlink(teePath);
2469 }
2470 }
2471 } else {
2472 if (fd >= 0) {
2473 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2474 }
2475 }
Eric Laurent81784c32012-11-19 14:55:58 -08002476 char teeTime[16];
2477 struct timeval tv;
2478 gettimeofday(&tv, NULL);
2479 struct tm tm;
2480 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002481 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2482 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2483 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2484 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002485 if (teeFd >= 0) {
2486 char wavHeader[44];
2487 memcpy(wavHeader,
2488 "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",
2489 sizeof(wavHeader));
2490 NBAIO_Format format = teeSource->format();
2491 unsigned channelCount = Format_channelCount(format);
2492 ALOG_ASSERT(channelCount <= FCC_2);
2493 uint32_t sampleRate = Format_sampleRate(format);
2494 wavHeader[22] = channelCount; // number of channels
2495 wavHeader[24] = sampleRate; // sample rate
2496 wavHeader[25] = sampleRate >> 8;
2497 wavHeader[32] = channelCount * 2; // block alignment
2498 write(teeFd, wavHeader, sizeof(wavHeader));
2499 size_t total = 0;
2500 bool firstRead = true;
2501 for (;;) {
2502#define TEE_SINK_READ 1024
2503 short buffer[TEE_SINK_READ * FCC_2];
2504 size_t count = TEE_SINK_READ;
2505 ssize_t actual = teeSource->read(buffer, count,
2506 AudioBufferProvider::kInvalidPTS);
2507 bool wasFirstRead = firstRead;
2508 firstRead = false;
2509 if (actual <= 0) {
2510 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2511 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002512 }
Eric Laurent81784c32012-11-19 14:55:58 -08002513 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002514 }
Eric Laurent81784c32012-11-19 14:55:58 -08002515 ALOG_ASSERT(actual <= (ssize_t)count);
2516 write(teeFd, buffer, actual * channelCount * sizeof(short));
2517 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002518 }
Eric Laurent81784c32012-11-19 14:55:58 -08002519 lseek(teeFd, (off_t) 4, SEEK_SET);
2520 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2521 write(teeFd, &temp, sizeof(temp));
2522 lseek(teeFd, (off_t) 40, SEEK_SET);
2523 temp = total * channelCount * sizeof(short);
2524 write(teeFd, &temp, sizeof(temp));
2525 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002526 if (fd >= 0) {
2527 fdprintf(fd, "tee copied to %s\n", teePath);
2528 }
Eric Laurent59255e42011-07-27 19:49:51 -07002529 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002530 if (fd >= 0) {
2531 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2532 }
Eric Laurent59255e42011-07-27 19:49:51 -07002533 }
2534 }
2535}
Glenn Kasten46909e72013-02-26 09:20:22 -08002536#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002537
Mathias Agopian65ab4712010-07-14 17:59:35 -07002538// ----------------------------------------------------------------------------
2539
2540status_t AudioFlinger::onTransact(
2541 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2542{
2543 return BnAudioFlinger::onTransact(code, data, reply, flags);
2544}
2545
Mathias Agopian65ab4712010-07-14 17:59:35 -07002546}; // namespace android