blob: 46d1211ef521e3335004f9b6581baf74da138089 [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
Marco Nelissenb2208842014-02-07 14:00:50 -0800107const char *formatToString(audio_format_t format) {
108 switch(format) {
109 case AUDIO_FORMAT_PCM_SUB_8_BIT: return "pcm8";
110 case AUDIO_FORMAT_PCM_SUB_16_BIT: return "pcm16";
111 case AUDIO_FORMAT_PCM_SUB_32_BIT: return "pcm32";
112 case AUDIO_FORMAT_PCM_SUB_8_24_BIT: return "pcm8.24";
113 case AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED: return "pcm24";
114 case AUDIO_FORMAT_PCM_SUB_FLOAT: return "pcmfloat";
115 case AUDIO_FORMAT_MP3: return "mp3";
116 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
117 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
118 case AUDIO_FORMAT_AAC: return "aac";
119 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
120 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
121 case AUDIO_FORMAT_VORBIS: return "vorbis";
122 default:
123 break;
124 }
125 return "unknown";
126}
127
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700128static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700129{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700130 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700131 int rc;
132
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700133 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
134 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
135 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
136 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700137 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700138 }
139 rc = audio_hw_device_open(mod, dev);
140 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
141 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
142 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700143 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700144 }
145 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
146 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
147 rc = BAD_VALUE;
148 goto out;
149 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700150 return 0;
151
152out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700153 *dev = NULL;
154 return rc;
155}
156
Mathias Agopian65ab4712010-07-14 17:59:35 -0700157// ----------------------------------------------------------------------------
158
159AudioFlinger::AudioFlinger()
160 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800161 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800162 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700163 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800164 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800165 mMasterMute(false),
166 mNextUniqueId(1),
167 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700168 mBtNrecIsOff(false),
169 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700170 mIsDeviceTypeKnown(false),
171 mGlobalEffectEnableTime(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700172{
Glenn Kasten949a9262013-04-16 12:35:20 -0700173 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800174 char value[PROPERTY_VALUE_MAX];
175 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
176 if (doLog) {
Glenn Kastencf515ab2014-03-14 16:01:58 -0700177 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800178 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800179#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800180 (void) property_get("ro.debuggable", value, "0");
181 int debuggable = atoi(value);
182 int teeEnabled = 0;
183 if (debuggable) {
184 (void) property_get("af.tee", value, "0");
185 teeEnabled = atoi(value);
186 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800187 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700188 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800189 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700190 }
191 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800192 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700193 }
194 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800195 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700196 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800197#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700198}
199
200void AudioFlinger::onFirstRef()
201{
Dima Zavin799a70e2011-04-18 16:57:27 -0700202 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700203
Eric Laurent93575202011-01-18 18:39:02 -0800204 Mutex::Autolock _l(mLock);
205
Dima Zavin799a70e2011-04-18 16:57:27 -0700206 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800207 char val_str[PROPERTY_VALUE_MAX] = { 0 };
208 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
209 uint32_t int_val;
210 if (1 == sscanf(val_str, "%u", &int_val)) {
211 mStandbyTimeInNsecs = milliseconds(int_val);
212 ALOGI("Using %u mSec as standby time.", int_val);
213 } else {
214 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
215 ALOGI("Using default %u mSec as standby time.",
216 (uint32_t)(mStandbyTimeInNsecs / 1000000));
217 }
218 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700219
Eric Laurenta4c5a552012-03-29 10:12:40 -0700220 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700221}
222
223AudioFlinger::~AudioFlinger()
224{
225 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700226 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700227 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228 }
229 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700230 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700231 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700232 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700233
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800234 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
235 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700236 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
237 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700239
240 // Tell media.log service about any old writers that still need to be unregistered
241 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
242 if (binder != 0) {
243 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
244 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
245 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
246 mUnregisteredWriters.pop();
247 mediaLogService->unregisterWriter(iMemory);
248 }
249 }
250
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251}
252
Eric Laurenta4c5a552012-03-29 10:12:40 -0700253static const char * const audio_interfaces[] = {
254 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
255 AUDIO_HARDWARE_MODULE_ID_A2DP,
256 AUDIO_HARDWARE_MODULE_ID_USB,
257};
258#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
259
John Grossmanee578c02012-07-23 17:05:46 -0700260AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
261 audio_module_handle_t module,
262 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700263{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700264 // if module is 0, the request comes from an old policy manager and we should load
265 // well known modules
266 if (module == 0) {
267 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
268 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
269 loadHwModule_l(audio_interfaces[i]);
270 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700271 // then try to find a module supporting the requested device.
272 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
273 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
274 audio_hw_device_t *dev = audioHwDevice->hwDevice();
275 if ((dev->get_supported_devices != NULL) &&
276 (dev->get_supported_devices(dev) & devices) == devices)
277 return audioHwDevice;
278 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700279 } else {
280 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700281 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
282 if (audioHwDevice != NULL) {
283 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700284 }
285 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700286
Dima Zavin799a70e2011-04-18 16:57:27 -0700287 return NULL;
288}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289
Glenn Kasten0f11b512014-01-31 16:18:54 -0800290void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291{
292 const size_t SIZE = 256;
293 char buffer[SIZE];
294 String8 result;
295
296 result.append("Clients:\n");
297 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800298 sp<Client> client = mClients.valueAt(i).promote();
299 if (client != 0) {
300 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
301 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302 }
303 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700304
Eric Laurentd1b28d42013-09-18 18:47:13 -0700305 result.append("Notification Clients:\n");
306 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
307 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
308 result.append(buffer);
309 }
310
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700311 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800312 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700313 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
314 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800315 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700316 result.append(buffer);
317 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700318 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319}
320
321
Glenn Kasten0f11b512014-01-31 16:18:54 -0800322void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323{
324 const size_t SIZE = 256;
325 char buffer[SIZE];
326 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800327 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328
John Grossman4ff14ba2012-02-08 16:37:41 -0800329 snprintf(buffer, SIZE, "Hardware status: %d\n"
330 "Standby Time mSec: %u\n",
331 hardwareStatus,
332 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333 result.append(buffer);
334 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335}
336
Glenn Kasten0f11b512014-01-31 16:18:54 -0800337void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338{
339 const size_t SIZE = 256;
340 char buffer[SIZE];
341 String8 result;
342 snprintf(buffer, SIZE, "Permission Denial: "
343 "can't dump AudioFlinger from pid=%d, uid=%d\n",
344 IPCThreadState::self()->getCallingPid(),
345 IPCThreadState::self()->getCallingUid());
346 result.append(buffer);
347 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348}
349
Eric Laurent81784c32012-11-19 14:55:58 -0800350bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351{
352 bool locked = false;
353 for (int i = 0; i < kDumpLockRetries; ++i) {
354 if (mutex.tryLock() == NO_ERROR) {
355 locked = true;
356 break;
357 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800358 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359 }
360 return locked;
361}
362
363status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
364{
Glenn Kasten44deb052012-02-05 18:09:08 -0800365 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700366 dumpPermissionDenial(fd, args);
367 } else {
368 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800369 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 if (!hardwareLocked) {
371 String8 result(kHardwareLockedString);
372 write(fd, result.string(), result.size());
373 } else {
374 mHardwareLock.unlock();
375 }
376
Eric Laurent81784c32012-11-19 14:55:58 -0800377 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378
379 // failed to lock - AudioFlinger is probably deadlocked
380 if (!locked) {
381 String8 result(kDeadlockedString);
382 write(fd, result.string(), result.size());
383 }
384
385 dumpClients(fd, args);
386 dumpInternals(fd, args);
387
388 // dump playback threads
389 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
390 mPlaybackThreads.valueAt(i)->dump(fd, args);
391 }
392
393 // dump record threads
394 for (size_t i = 0; i < mRecordThreads.size(); i++) {
395 mRecordThreads.valueAt(i)->dump(fd, args);
396 }
397
Dima Zavin799a70e2011-04-18 16:57:27 -0700398 // dump all hardware devs
399 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700400 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700401 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700403
Glenn Kasten46909e72013-02-26 09:20:22 -0800404#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700405 // dump the serially shared record tee sink
406 if (mRecordTeeSource != 0) {
407 dumpTee(fd, mRecordTeeSource);
408 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800409#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700410
Glenn Kastend65d73c2012-06-22 17:21:07 -0700411 if (locked) {
412 mLock.unlock();
413 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800414
415 // append a copy of media.log here by forwarding fd to it, but don't attempt
416 // to lookup the service if it's not running, as it will block for a second
417 if (mLogMemoryDealer != 0) {
418 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
419 if (binder != 0) {
420 fdprintf(fd, "\nmedia.log:\n");
421 Vector<String16> args;
422 binder->dump(fd, args);
423 }
424 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425 }
426 return NO_ERROR;
427}
428
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800429sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
430{
431 // If pid is already in the mClients wp<> map, then use that entry
432 // (for which promote() is always != 0), otherwise create a new entry and Client.
433 sp<Client> client = mClients.valueFor(pid).promote();
434 if (client == 0) {
435 client = new Client(this, pid);
436 mClients.add(pid, client);
437 }
438
439 return client;
440}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441
Glenn Kasten9e58b552013-01-18 15:09:48 -0800442sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
443{
Glenn Kasten481fb672013-09-30 14:39:28 -0700444 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800445 if (mLogMemoryDealer == 0) {
446 return new NBLog::Writer();
447 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800448 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700449 // Similarly if we can't contact the media.log service, also return a dummy writer
450 if (binder == 0) {
451 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800452 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700453 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
454 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
455 // If allocation fails, consult the vector of previously unregistered writers
456 // and garbage-collect one or more them until an allocation succeeds
457 if (shared == 0) {
458 Mutex::Autolock _l(mUnregisteredWritersLock);
459 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
460 {
461 // Pick the oldest stale writer to garbage-collect
462 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
463 mUnregisteredWriters.removeAt(0);
464 mediaLogService->unregisterWriter(iMemory);
465 // Now the media.log remote reference to IMemory is gone. When our last local
466 // reference to IMemory also drops to zero at end of this block,
467 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
468 }
469 // Re-attempt the allocation
470 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
471 if (shared != 0) {
472 goto success;
473 }
474 }
475 // Even after garbage-collecting all old writers, there is still not enough memory,
476 // so return a dummy writer
477 return new NBLog::Writer();
478 }
479success:
480 mediaLogService->registerWriter(shared, size, name);
481 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800482}
483
484void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
485{
Glenn Kasten685ef092013-02-04 08:15:34 -0800486 if (writer == 0) {
487 return;
488 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800489 sp<IMemory> iMemory(writer->getIMemory());
490 if (iMemory == 0) {
491 return;
492 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700493 // Rather than removing the writer immediately, append it to a queue of old writers to
494 // be garbage-collected later. This allows us to continue to view old logs for a while.
495 Mutex::Autolock _l(mUnregisteredWritersLock);
496 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800497}
498
Mathias Agopian65ab4712010-07-14 17:59:35 -0700499// IAudioFlinger interface
500
501
502sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800503 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700504 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800505 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700506 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800507 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800508 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700509 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800510 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800511 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800513 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700514 status_t *status)
515{
516 sp<PlaybackThread::Track> track;
517 sp<TrackHandle> trackHandle;
518 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519 status_t lStatus;
520 int lSessionId;
521
Glenn Kasten263709e2012-01-06 08:40:01 -0800522 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
523 // but if someone uses binder directly they could bypass that and cause us to crash
524 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000525 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526 lStatus = BAD_VALUE;
527 goto Exit;
528 }
529
Glenn Kasten53b5d092014-02-05 10:00:23 -0800530 // further sample rate checks are performed by createTrack_l() depending on the thread type
531 if (sampleRate == 0) {
532 ALOGE("createTrack() invalid sample rate %u", sampleRate);
533 lStatus = BAD_VALUE;
534 goto Exit;
535 }
536
537 // further channel mask checks are performed by createTrack_l() depending on the thread type
538 if (!audio_is_output_channel(channelMask)) {
539 ALOGE("createTrack() invalid channel mask %#x", channelMask);
540 lStatus = BAD_VALUE;
541 goto Exit;
542 }
543
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700544 // further format checks are performed by createTrack_l() depending on the thread type
545 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800546 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700547 lStatus = BAD_VALUE;
548 goto Exit;
549 }
550
Glenn Kasten663c2242013-09-24 11:52:37 -0700551 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
552 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
553 lStatus = BAD_VALUE;
554 goto Exit;
555 }
556
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557 {
558 Mutex::Autolock _l(mLock);
559 PlaybackThread *thread = checkPlaybackThread_l(output);
560 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800561 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562 lStatus = BAD_VALUE;
563 goto Exit;
564 }
565
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800566 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800567 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700568
Glenn Kastene848bd92014-03-13 15:00:32 -0700569 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700570 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700571 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700572 // check if an effect chain with the same session ID is present on another
573 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700574 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700575 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
576 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700577 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700578 if (sessions & PlaybackThread::EFFECT_SESSION) {
579 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700580 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700581 }
Eric Laurentde070132010-07-13 04:45:46 -0700582 }
583 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700585 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700586 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 if (sessionId != NULL) {
588 *sessionId = lSessionId;
589 }
590 }
Steve Block3856b092011-10-20 11:56:00 +0100591 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592
593 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800594 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800595 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700596 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700597
598 // move effect chain to this output thread if an effect on same session was waiting
599 // for a track to be created
600 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700601 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700602 Mutex::Autolock _dl(thread->mLock);
603 Mutex::Autolock _sl(effectThread->mLock);
604 moveEffectChain_l(lSessionId, effectThread, thread, true);
605 }
Eric Laurenta011e352012-03-29 15:51:43 -0700606
607 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700608 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700609 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
610 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700611 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700612 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700613 } else {
614 mPendingSyncEvents[i]->cancel();
615 }
Eric Laurenta011e352012-03-29 15:51:43 -0700616 mPendingSyncEvents.removeAt(i);
617 i--;
618 }
619 }
620 }
Glenn Kastene198c362013-08-13 09:13:36 -0700621
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622 }
Glenn Kastene198c362013-08-13 09:13:36 -0700623
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700624 if (lStatus != NO_ERROR) {
625 // remove local strong reference to Client before deleting the Track so that the
626 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -0700627 client.clear();
628 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700629 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630 }
631
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700632 // return handle to client
633 trackHandle = new TrackHandle(track);
634
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700636 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 return trackHandle;
638}
639
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800640uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641{
642 Mutex::Autolock _l(mLock);
643 PlaybackThread *thread = checkPlaybackThread_l(output);
644 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000645 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 return 0;
647 }
648 return thread->sampleRate();
649}
650
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800651int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652{
653 Mutex::Autolock _l(mLock);
654 PlaybackThread *thread = checkPlaybackThread_l(output);
655 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000656 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 return 0;
658 }
659 return thread->channelCount();
660}
661
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800662audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663{
664 Mutex::Autolock _l(mLock);
665 PlaybackThread *thread = checkPlaybackThread_l(output);
666 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000667 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800668 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 }
670 return thread->format();
671}
672
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800673size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674{
675 Mutex::Autolock _l(mLock);
676 PlaybackThread *thread = checkPlaybackThread_l(output);
677 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000678 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679 return 0;
680 }
Glenn Kasten58912562012-04-03 10:45:00 -0700681 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
682 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683 return thread->frameCount();
684}
685
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800686uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687{
688 Mutex::Autolock _l(mLock);
689 PlaybackThread *thread = checkPlaybackThread_l(output);
690 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800691 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692 return 0;
693 }
694 return thread->latency();
695}
696
697status_t AudioFlinger::setMasterVolume(float value)
698{
Eric Laurenta1884f92011-08-23 08:25:03 -0700699 status_t ret = initCheck();
700 if (ret != NO_ERROR) {
701 return ret;
702 }
703
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 // check calling permissions
705 if (!settingsAllowed()) {
706 return PERMISSION_DENIED;
707 }
708
Eric Laurenta4c5a552012-03-29 10:12:40 -0700709 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700710 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700711
John Grossmanee578c02012-07-23 17:05:46 -0700712 // Set master volume in the HALs which support it.
713 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
714 AutoMutex lock(mHardwareLock);
715 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800716
John Grossmanee578c02012-07-23 17:05:46 -0700717 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
718 if (dev->canSetMasterVolume()) {
719 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800720 }
John Grossmanee578c02012-07-23 17:05:46 -0700721 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723
John Grossmanee578c02012-07-23 17:05:46 -0700724 // Now set the master volume in each playback thread. Playback threads
725 // assigned to HALs which do not have master volume support will apply
726 // master volume during the mix operation. Threads with HALs which do
727 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800728 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700729 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700730
731 return NO_ERROR;
732}
733
Glenn Kastenf78aee72012-01-04 11:00:47 -0800734status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735{
Eric Laurenta1884f92011-08-23 08:25:03 -0700736 status_t ret = initCheck();
737 if (ret != NO_ERROR) {
738 return ret;
739 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740
741 // check calling permissions
742 if (!settingsAllowed()) {
743 return PERMISSION_DENIED;
744 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800745 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000746 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747 return BAD_VALUE;
748 }
749
750 { // scope for the lock
751 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700752 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700754 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755 mHardwareStatus = AUDIO_HW_IDLE;
756 }
757
758 if (NO_ERROR == ret) {
759 Mutex::Autolock _l(mLock);
760 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800761 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700762 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763 }
764
765 return ret;
766}
767
768status_t AudioFlinger::setMicMute(bool state)
769{
Eric Laurenta1884f92011-08-23 08:25:03 -0700770 status_t ret = initCheck();
771 if (ret != NO_ERROR) {
772 return ret;
773 }
774
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 // check calling permissions
776 if (!settingsAllowed()) {
777 return PERMISSION_DENIED;
778 }
779
780 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700781 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700783 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784 mHardwareStatus = AUDIO_HW_IDLE;
785 return ret;
786}
787
788bool AudioFlinger::getMicMute() const
789{
Eric Laurenta1884f92011-08-23 08:25:03 -0700790 status_t ret = initCheck();
791 if (ret != NO_ERROR) {
792 return false;
793 }
794
Dima Zavinfce7a472011-04-19 22:30:36 -0700795 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800796 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700797 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700799 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 mHardwareStatus = AUDIO_HW_IDLE;
801 return state;
802}
803
804status_t AudioFlinger::setMasterMute(bool muted)
805{
John Grossmand8f178d2012-07-20 14:51:35 -0700806 status_t ret = initCheck();
807 if (ret != NO_ERROR) {
808 return ret;
809 }
810
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811 // check calling permissions
812 if (!settingsAllowed()) {
813 return PERMISSION_DENIED;
814 }
815
John Grossmanee578c02012-07-23 17:05:46 -0700816 Mutex::Autolock _l(mLock);
817 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700818
John Grossmanee578c02012-07-23 17:05:46 -0700819 // Set master mute in the HALs which support it.
820 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
821 AutoMutex lock(mHardwareLock);
822 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700823
John Grossmanee578c02012-07-23 17:05:46 -0700824 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
825 if (dev->canSetMasterMute()) {
826 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700827 }
John Grossmanee578c02012-07-23 17:05:46 -0700828 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700829 }
830
John Grossmanee578c02012-07-23 17:05:46 -0700831 // Now set the master mute in each playback thread. Playback threads
832 // assigned to HALs which do not have master mute support will apply master
833 // mute during the mix operation. Threads with HALs which do support master
834 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800835 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700836 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837
838 return NO_ERROR;
839}
840
841float AudioFlinger::masterVolume() const
842{
Glenn Kasten98067102011-12-13 11:47:54 -0800843 Mutex::Autolock _l(mLock);
844 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845}
846
847bool AudioFlinger::masterMute() const
848{
Glenn Kasten98067102011-12-13 11:47:54 -0800849 Mutex::Autolock _l(mLock);
850 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851}
852
John Grossman4ff14ba2012-02-08 16:37:41 -0800853float AudioFlinger::masterVolume_l() const
854{
John Grossman4ff14ba2012-02-08 16:37:41 -0800855 return mMasterVolume;
856}
857
John Grossmand8f178d2012-07-20 14:51:35 -0700858bool AudioFlinger::masterMute_l() const
859{
John Grossmanee578c02012-07-23 17:05:46 -0700860 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700861}
862
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800863status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
864 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865{
866 // check calling permissions
867 if (!settingsAllowed()) {
868 return PERMISSION_DENIED;
869 }
870
Glenn Kasten263709e2012-01-06 08:40:01 -0800871 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000872 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700873 return BAD_VALUE;
874 }
875
876 AutoMutex lock(mLock);
877 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700878 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879 thread = checkPlaybackThread_l(output);
880 if (thread == NULL) {
881 return BAD_VALUE;
882 }
883 }
884
885 mStreamTypes[stream].volume = value;
886
887 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800888 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700889 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890 }
891 } else {
892 thread->setStreamVolume(stream, value);
893 }
894
895 return NO_ERROR;
896}
897
Glenn Kastenfff6d712012-01-12 16:38:12 -0800898status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899{
900 // check calling permissions
901 if (!settingsAllowed()) {
902 return PERMISSION_DENIED;
903 }
904
Glenn Kasten263709e2012-01-06 08:40:01 -0800905 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700906 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000907 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700908 return BAD_VALUE;
909 }
910
Eric Laurent93575202011-01-18 18:39:02 -0800911 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700913 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700914 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700915
916 return NO_ERROR;
917}
918
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800919float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920{
Glenn Kasten263709e2012-01-06 08:40:01 -0800921 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700922 return 0.0f;
923 }
924
925 AutoMutex lock(mLock);
926 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -0700927 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 PlaybackThread *thread = checkPlaybackThread_l(output);
929 if (thread == NULL) {
930 return 0.0f;
931 }
932 volume = thread->streamVolume(stream);
933 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800934 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935 }
936
937 return volume;
938}
939
Glenn Kastenfff6d712012-01-12 16:38:12 -0800940bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941{
Glenn Kasten263709e2012-01-06 08:40:01 -0800942 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700943 return true;
944 }
945
Glenn Kasten6637baa2012-01-09 09:40:36 -0800946 AutoMutex lock(mLock);
947 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948}
949
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800950status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700952 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
953 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800954
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 // check calling permissions
956 if (!settingsAllowed()) {
957 return PERMISSION_DENIED;
958 }
959
Glenn Kasten142f5192014-03-25 17:44:59 -0700960 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
961 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700962 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700963 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800964 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700965 AutoMutex lock(mHardwareLock);
966 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
967 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
968 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
969 status_t result = dev->set_parameters(dev, keyValuePairs.string());
970 final_result = result ?: final_result;
971 }
972 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800973 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700974 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
975 AudioParameter param = AudioParameter(keyValuePairs);
976 String8 value;
977 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700978 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
979 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700980 for (size_t i = 0; i < mRecordThreads.size(); i++) {
981 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700982 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700983 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
984 // collect all of the thread's session IDs
985 KeyedVector<int, bool> ids = thread->sessionIds();
986 // suspend effects associated with those session IDs
987 for (size_t j = 0; j < ids.size(); ++j) {
988 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700989 thread->setEffectSuspended(FX_IID_AEC,
990 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700991 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700992 thread->setEffectSuspended(FX_IID_NS,
993 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700994 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700995 }
996 }
Eric Laurentbee53372011-08-29 12:42:48 -0700997 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700998 }
999 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001000 String8 screenState;
1001 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1002 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001003 if (isOff != (AudioFlinger::mScreenState & 1)) {
1004 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001005 }
1006 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001007 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008 }
1009
1010 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1011 // and the thread is exited once the lock is released
1012 sp<ThreadBase> thread;
1013 {
1014 Mutex::Autolock _l(mLock);
1015 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001016 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001017 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001018 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001019 // indicate output device change to all input threads for pre processing
1020 AudioParameter param = AudioParameter(keyValuePairs);
1021 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001022 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1023 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001024 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1025 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1026 }
1027 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028 }
1029 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001030 if (thread != 0) {
1031 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001032 }
1033 return BAD_VALUE;
1034}
1035
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001036String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001037{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001038 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1039 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040
Eric Laurenta4c5a552012-03-29 10:12:40 -07001041 Mutex::Autolock _l(mLock);
1042
Glenn Kasten142f5192014-03-25 17:44:59 -07001043 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001044 String8 out_s8;
1045
Dima Zavin799a70e2011-04-18 16:57:27 -07001046 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001047 char *s;
1048 {
1049 AutoMutex lock(mHardwareLock);
1050 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001051 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001052 s = dev->get_parameters(dev, keys.string());
1053 mHardwareStatus = AUDIO_HW_IDLE;
1054 }
John Grossmanef7740b2012-02-09 11:28:36 -08001055 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001056 free(s);
1057 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001058 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001059 }
1060
Mathias Agopian65ab4712010-07-14 17:59:35 -07001061 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1062 if (playbackThread != NULL) {
1063 return playbackThread->getParameters(keys);
1064 }
1065 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1066 if (recordThread != NULL) {
1067 return recordThread->getParameters(keys);
1068 }
1069 return String8("");
1070}
1071
Glenn Kastendd8104c2012-07-02 12:42:44 -07001072size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1073 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074{
Eric Laurenta1884f92011-08-23 08:25:03 -07001075 status_t ret = initCheck();
1076 if (ret != NO_ERROR) {
1077 return 0;
1078 }
1079
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001080 AutoMutex lock(mHardwareLock);
1081 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001082 struct audio_config config;
1083 memset(&config, 0, sizeof(config));
1084 config.sample_rate = sampleRate;
1085 config.channel_mask = channelMask;
1086 config.format = format;
1087
John Grossmanee578c02012-07-23 17:05:46 -07001088 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1089 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001090 mHardwareStatus = AUDIO_HW_IDLE;
1091 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001092}
1093
Glenn Kasten5f972c02014-01-13 09:59:31 -08001094uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001096 Mutex::Autolock _l(mLock);
1097
1098 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1099 if (recordThread != NULL) {
1100 return recordThread->getInputFramesLost();
1101 }
1102 return 0;
1103}
1104
1105status_t AudioFlinger::setVoiceVolume(float value)
1106{
Eric Laurenta1884f92011-08-23 08:25:03 -07001107 status_t ret = initCheck();
1108 if (ret != NO_ERROR) {
1109 return ret;
1110 }
1111
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112 // check calling permissions
1113 if (!settingsAllowed()) {
1114 return PERMISSION_DENIED;
1115 }
1116
1117 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001118 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001119 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001120 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121 mHardwareStatus = AUDIO_HW_IDLE;
1122
1123 return ret;
1124}
1125
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001126status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001127 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128{
1129 status_t status;
1130
1131 Mutex::Autolock _l(mLock);
1132
1133 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1134 if (playbackThread != NULL) {
1135 return playbackThread->getRenderPosition(halFrames, dspFrames);
1136 }
1137
1138 return BAD_VALUE;
1139}
1140
1141void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1142{
1143
1144 Mutex::Autolock _l(mLock);
1145
Glenn Kastenbb001922012-02-03 11:10:26 -08001146 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001147 if (mNotificationClients.indexOfKey(pid) < 0) {
1148 sp<NotificationClient> notificationClient = new NotificationClient(this,
1149 client,
1150 pid);
Steve Block3856b092011-10-20 11:56:00 +01001151 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152
1153 mNotificationClients.add(pid, notificationClient);
1154
1155 sp<IBinder> binder = client->asBinder();
1156 binder->linkToDeath(notificationClient);
1157
1158 // the config change is always sent from playback or record threads to avoid deadlock
1159 // with AudioSystem::gLock
1160 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001161 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162 }
1163
1164 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001165 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001166 }
1167 }
1168}
1169
1170void AudioFlinger::removeNotificationClient(pid_t pid)
1171{
1172 Mutex::Autolock _l(mLock);
1173
Glenn Kastena3b09252012-01-20 09:19:01 -08001174 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001175
Steve Block3856b092011-10-20 11:56:00 +01001176 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001177 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001178 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001179 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001180 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001181 ALOGV(" pid %d @ %d", ref->mPid, i);
1182 if (ref->mPid == pid) {
1183 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001184 mAudioSessionRefs.removeAt(i);
1185 delete ref;
1186 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001187 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001188 } else {
1189 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001190 }
1191 }
1192 if (removed) {
1193 purgeStaleEffects_l();
1194 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001195}
1196
1197// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Eric Laurent10351942014-05-08 18:49:52 -07001198void AudioFlinger::audioConfigChanged_l(
1199 const DefaultKeyedVector< pid_t,sp<NotificationClient> >& notificationClients,
1200 int event,
1201 audio_io_handle_t ioHandle,
1202 const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203{
Eric Laurent10351942014-05-08 18:49:52 -07001204 size_t size = notificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001205 for (size_t i = 0; i < size; i++) {
Eric Laurent10351942014-05-08 18:49:52 -07001206 notificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
1207 ioHandle,
1208 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001209 }
1210}
1211
1212// removeClient_l() must be called with AudioFlinger::mLock held
1213void AudioFlinger::removeClient_l(pid_t pid)
1214{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001215 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001216 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217 mClients.removeItem(pid);
1218}
1219
Eric Laurent717e1282012-06-29 16:36:52 -07001220// getEffectThread_l() must be called with AudioFlinger::mLock held
1221sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1222{
1223 sp<PlaybackThread> thread;
1224
1225 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1226 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1227 ALOG_ASSERT(thread == 0);
1228 thread = mPlaybackThreads.valueAt(i);
1229 }
1230 }
1231
1232 return thread;
1233}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001234
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236
1237// ----------------------------------------------------------------------------
1238
1239AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1240 : RefBase(),
1241 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001242 // 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 -07001243 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001244 mPid(pid),
1245 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001246{
1247 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1248}
1249
1250// Client destructor must be called with AudioFlinger::mLock held
1251AudioFlinger::Client::~Client()
1252{
1253 mAudioFlinger->removeClient_l(mPid);
1254}
1255
Glenn Kasten435dbe62012-01-30 10:15:48 -08001256sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257{
1258 return mMemoryDealer;
1259}
1260
John Grossman4ff14ba2012-02-08 16:37:41 -08001261// Reserve one of the limited slots for a timed audio track associated
1262// with this client
1263bool AudioFlinger::Client::reserveTimedTrack()
1264{
1265 const int kMaxTimedTracksPerClient = 4;
1266
1267 Mutex::Autolock _l(mTimedTrackLock);
1268
1269 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1270 ALOGW("can not create timed track - pid %d has exceeded the limit",
1271 mPid);
1272 return false;
1273 }
1274
1275 mTimedTrackCount++;
1276 return true;
1277}
1278
1279// Release a slot for a timed audio track
1280void AudioFlinger::Client::releaseTimedTrack()
1281{
1282 Mutex::Autolock _l(mTimedTrackLock);
1283 mTimedTrackCount--;
1284}
1285
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286// ----------------------------------------------------------------------------
1287
1288AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1289 const sp<IAudioFlingerClient>& client,
1290 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001291 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001292{
1293}
1294
1295AudioFlinger::NotificationClient::~NotificationClient()
1296{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001297}
1298
Glenn Kasten0f11b512014-01-31 16:18:54 -08001299void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300{
1301 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001302 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001303}
1304
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305
1306// ----------------------------------------------------------------------------
1307
Jeff Brown893a5642013-08-16 20:19:26 -07001308static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1309 return audio_is_remote_submix_device(inDevice);
1310}
1311
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001313 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001315 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001316 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001317 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001318 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001319 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320 int *sessionId,
1321 status_t *status)
1322{
1323 sp<RecordThread::RecordTrack> recordTrack;
1324 sp<RecordHandle> recordHandle;
1325 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001326 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327 int lSessionId;
1328
1329 // check calling permissions
1330 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001331 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001332 lStatus = PERMISSION_DENIED;
1333 goto Exit;
1334 }
1335
Glenn Kasten53b5d092014-02-05 10:00:23 -08001336 // further sample rate checks are performed by createRecordTrack_l()
1337 if (sampleRate == 0) {
1338 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1339 lStatus = BAD_VALUE;
1340 goto Exit;
1341 }
1342
Glenn Kasten0e0e8462014-03-13 15:02:40 -07001343 // we don't yet support anything other than 16-bit PCM
1344 if (!(audio_is_valid_format(format) &&
1345 audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001346 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001347 lStatus = BAD_VALUE;
1348 goto Exit;
1349 }
1350
Glenn Kasten53b5d092014-02-05 10:00:23 -08001351 // further channel mask checks are performed by createRecordTrack_l()
1352 if (!audio_is_input_channel(channelMask)) {
1353 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1354 lStatus = BAD_VALUE;
1355 goto Exit;
1356 }
1357
Glenn Kasten05997e22014-03-13 15:08:33 -07001358 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001359 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001360 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001361 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001362 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001363 lStatus = BAD_VALUE;
1364 goto Exit;
1365 }
1366
Jeff Brown893a5642013-08-16 20:19:26 -07001367 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1368 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001369 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001370 lStatus = PERMISSION_DENIED;
1371 goto Exit;
1372 }
1373
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001374 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001375 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001376
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001377 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378 lSessionId = *sessionId;
1379 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001380 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001381 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001382 if (sessionId != NULL) {
1383 *sessionId = lSessionId;
1384 }
1385 }
Glenn Kasten570f6332014-03-13 15:01:06 -07001386 ALOGV("openRecord() lSessionId: %d", lSessionId);
1387
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001388 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001389 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001390 frameCount, lSessionId,
1391 IPCThreadState::self()->getCallingUid(),
1392 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001393 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001394 }
Glenn Kastene198c362013-08-13 09:13:36 -07001395
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001396 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001397 // remove local strong reference to Client before deleting the RecordTrack so that the
1398 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399 client.clear();
1400 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401 goto Exit;
1402 }
1403
Glenn Kasten2fc14732013-08-05 14:58:14 -07001404 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001405 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406
1407Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001408 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409 return recordHandle;
1410}
1411
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001412
1413
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414// ----------------------------------------------------------------------------
1415
Eric Laurenta4c5a552012-03-29 10:12:40 -07001416audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1417{
1418 if (!settingsAllowed()) {
1419 return 0;
1420 }
1421 Mutex::Autolock _l(mLock);
1422 return loadHwModule_l(name);
1423}
1424
1425// loadHwModule_l() must be called with AudioFlinger::mLock held
1426audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1427{
1428 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1429 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1430 ALOGW("loadHwModule() module %s already loaded", name);
1431 return mAudioHwDevs.keyAt(i);
1432 }
1433 }
1434
Eric Laurenta4c5a552012-03-29 10:12:40 -07001435 audio_hw_device_t *dev;
1436
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001437 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001438 if (rc) {
1439 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1440 return 0;
1441 }
1442
1443 mHardwareStatus = AUDIO_HW_INIT;
1444 rc = dev->init_check(dev);
1445 mHardwareStatus = AUDIO_HW_IDLE;
1446 if (rc) {
1447 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1448 return 0;
1449 }
1450
John Grossmanee578c02012-07-23 17:05:46 -07001451 // Check and cache this HAL's level of support for master mute and master
1452 // volume. If this is the first HAL opened, and it supports the get
1453 // methods, use the initial values provided by the HAL as the current
1454 // master mute and volume settings.
1455
1456 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1457 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001458 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001459
1460 if (0 == mAudioHwDevs.size()) {
1461 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1462 if (NULL != dev->get_master_volume) {
1463 float mv;
1464 if (OK == dev->get_master_volume(dev, &mv)) {
1465 mMasterVolume = mv;
1466 }
1467 }
1468
1469 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1470 if (NULL != dev->get_master_mute) {
1471 bool mm;
1472 if (OK == dev->get_master_mute(dev, &mm)) {
1473 mMasterMute = mm;
1474 }
1475 }
1476 }
1477
Eric Laurenta4c5a552012-03-29 10:12:40 -07001478 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001479 if ((NULL != dev->set_master_volume) &&
1480 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1481 flags = static_cast<AudioHwDevice::Flags>(flags |
1482 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1483 }
1484
1485 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1486 if ((NULL != dev->set_master_mute) &&
1487 (OK == dev->set_master_mute(dev, mMasterMute))) {
1488 flags = static_cast<AudioHwDevice::Flags>(flags |
1489 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1490 }
1491
Eric Laurenta4c5a552012-03-29 10:12:40 -07001492 mHardwareStatus = AUDIO_HW_IDLE;
1493 }
1494
1495 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001496 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001497
1498 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001499 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001500
1501 return handle;
1502
1503}
1504
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001505// ----------------------------------------------------------------------------
1506
Glenn Kasten3b16c762012-11-14 08:44:39 -08001507uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001508{
1509 Mutex::Autolock _l(mLock);
1510 PlaybackThread *thread = primaryPlaybackThread_l();
1511 return thread != NULL ? thread->sampleRate() : 0;
1512}
1513
Glenn Kastene33054e2012-11-14 12:54:39 -08001514size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001515{
1516 Mutex::Autolock _l(mLock);
1517 PlaybackThread *thread = primaryPlaybackThread_l();
1518 return thread != NULL ? thread->frameCountHAL() : 0;
1519}
1520
1521// ----------------------------------------------------------------------------
1522
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001523status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1524{
1525 uid_t uid = IPCThreadState::self()->getCallingUid();
1526 if (uid != AID_SYSTEM) {
1527 return PERMISSION_DENIED;
1528 }
1529 Mutex::Autolock _l(mLock);
1530 if (mIsDeviceTypeKnown) {
1531 return INVALID_OPERATION;
1532 }
1533 mIsLowRamDevice = isLowRamDevice;
1534 mIsDeviceTypeKnown = true;
1535 return NO_ERROR;
1536}
1537
1538// ----------------------------------------------------------------------------
1539
Eric Laurenta4c5a552012-03-29 10:12:40 -07001540audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1541 audio_devices_t *pDevices,
1542 uint32_t *pSamplingRate,
1543 audio_format_t *pFormat,
1544 audio_channel_mask_t *pChannelMask,
1545 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001546 audio_output_flags_t flags,
1547 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001548{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001549 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001550 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001551 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1552 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1553 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001554 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001555 config.offload_info = *offloadInfo;
1556 }
1557
Eric Laurentbfb1b832013-01-07 09:53:42 -08001558 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001559 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001560 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001561 config.sample_rate,
1562 config.format,
1563 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001564 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001565 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001566 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001567
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001568 if (pDevices == NULL || *pDevices == AUDIO_DEVICE_NONE) {
Glenn Kasten142f5192014-03-25 17:44:59 -07001569 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001570 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001571
Mathias Agopian65ab4712010-07-14 17:59:35 -07001572 Mutex::Autolock _l(mLock);
1573
Glenn Kasten32550952013-08-06 10:45:10 -07001574 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001575 if (outHwDev == NULL) {
Glenn Kasten142f5192014-03-25 17:44:59 -07001576 return AUDIO_IO_HANDLE_NONE;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001577 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001578
John Grossmanee578c02012-07-23 17:05:46 -07001579 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001580 audio_io_handle_t id = nextUniqueId();
1581
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001582 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001583
Glenn Kasten32550952013-08-06 10:45:10 -07001584 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001585 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001586 id,
1587 *pDevices,
1588 (audio_output_flags_t)flags,
1589 &config,
1590 &outStream);
1591
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001592 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001593 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001594 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001595 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001596 config.sample_rate,
1597 config.format,
1598 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001599 status);
1600
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001601 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001602 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001603
Glenn Kasten32550952013-08-06 10:45:10 -07001604 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001605 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1606 thread = new OffloadThread(this, output, id, *pDevices);
1607 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1608 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001609 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1610 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001611 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001612 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001613 } else {
1614 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001615 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001616 }
1617 mPlaybackThreads.add(id, thread);
1618
Glenn Kasten7c027242012-12-26 14:43:16 -08001619 if (pSamplingRate != NULL) {
1620 *pSamplingRate = config.sample_rate;
1621 }
1622 if (pFormat != NULL) {
1623 *pFormat = config.format;
1624 }
1625 if (pChannelMask != NULL) {
1626 *pChannelMask = config.channel_mask;
1627 }
1628 if (pLatencyMs != NULL) {
1629 *pLatencyMs = thread->latency();
1630 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001631
1632 // notify client processes of the new output creation
Eric Laurent10351942014-05-08 18:49:52 -07001633 thread->audioConfigChanged_l(mNotificationClients, AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001634
1635 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001636 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001637 ALOGI("Using module %d has the primary audio interface", module);
1638 mPrimaryHardwareDev = outHwDev;
1639
1640 AutoMutex lock(mHardwareLock);
1641 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001642 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001643 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001644 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001645 return id;
1646 }
1647
Glenn Kasten142f5192014-03-25 17:44:59 -07001648 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001649}
1650
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001651audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1652 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001653{
1654 Mutex::Autolock _l(mLock);
1655 MixerThread *thread1 = checkMixerThread_l(output1);
1656 MixerThread *thread2 = checkMixerThread_l(output2);
1657
1658 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001659 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1660 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001661 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001662 }
1663
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001664 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001665 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1666 thread->addOutputTrack(thread2);
1667 mPlaybackThreads.add(id, thread);
1668 // notify client processes of the new output creation
Eric Laurent10351942014-05-08 18:49:52 -07001669 thread->audioConfigChanged_l(mNotificationClients, AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001670 return id;
1671}
1672
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001673status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001674{
Glenn Kastend96c5722012-04-25 13:44:49 -07001675 return closeOutput_nonvirtual(output);
1676}
1677
1678status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1679{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001680 // keep strong reference on the playback thread so that
1681 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001682 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001683 {
1684 Mutex::Autolock _l(mLock);
1685 thread = checkPlaybackThread_l(output);
1686 if (thread == NULL) {
1687 return BAD_VALUE;
1688 }
1689
Steve Block3856b092011-10-20 11:56:00 +01001690 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001691
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001692 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001693 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001694 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001695 DuplicatingThread *dupThread =
1696 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001697 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001698
1699 }
1700 }
1701 }
1702
1703
1704 mPlaybackThreads.removeItem(output);
1705 // save all effects to the default thread
1706 if (mPlaybackThreads.size()) {
1707 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1708 if (dstThread != NULL) {
1709 // audioflinger lock is held here so the acquisition order of thread locks does not
1710 // matter
1711 Mutex::Autolock _dl(dstThread->mLock);
1712 Mutex::Autolock _sl(thread->mLock);
1713 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1714 for (size_t i = 0; i < effectChains.size(); i ++) {
1715 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001716 }
1717 }
1718 }
Eric Laurent10351942014-05-08 18:49:52 -07001719 audioConfigChanged_l(mNotificationClients, AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001720 }
1721 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001722 // The thread entity (active unit of execution) is no longer running here,
1723 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001724
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001725 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001726 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001727 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001728 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001729 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001730 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001731 }
1732 return NO_ERROR;
1733}
1734
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001735status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001736{
1737 Mutex::Autolock _l(mLock);
1738 PlaybackThread *thread = checkPlaybackThread_l(output);
1739
1740 if (thread == NULL) {
1741 return BAD_VALUE;
1742 }
1743
Steve Block3856b092011-10-20 11:56:00 +01001744 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001745 thread->suspend();
1746
1747 return NO_ERROR;
1748}
1749
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001750status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001751{
1752 Mutex::Autolock _l(mLock);
1753 PlaybackThread *thread = checkPlaybackThread_l(output);
1754
1755 if (thread == NULL) {
1756 return BAD_VALUE;
1757 }
1758
Steve Block3856b092011-10-20 11:56:00 +01001759 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001760
1761 thread->restore();
1762
1763 return NO_ERROR;
1764}
1765
Eric Laurenta4c5a552012-03-29 10:12:40 -07001766audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1767 audio_devices_t *pDevices,
1768 uint32_t *pSamplingRate,
1769 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001770 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001771{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001772 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001773 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001774 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1775 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1776 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1777
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001778 uint32_t reqSamplingRate = config.sample_rate;
1779 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001780 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001781
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001782 if (pDevices == NULL || *pDevices == AUDIO_DEVICE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001783 return 0;
1784 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001785
Mathias Agopian65ab4712010-07-14 17:59:35 -07001786 Mutex::Autolock _l(mLock);
1787
Glenn Kasten32550952013-08-06 10:45:10 -07001788 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001789 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001790 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001791 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001792
John Grossmanee578c02012-07-23 17:05:46 -07001793 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001794 audio_io_handle_t id = nextUniqueId();
1795
Glenn Kasten32550952013-08-06 10:45:10 -07001796 audio_stream_in_t *inStream = NULL;
1797 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001798 &inStream);
Glenn Kastencac3daa2014-02-07 09:47:14 -08001799 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %#x, Channels %x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001800 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001801 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001802 config.sample_rate,
1803 config.format,
1804 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001805 status);
1806
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001807 // If the input could not be opened with the requested parameters and we can handle the
1808 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1809 // 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 -07001810 if (status == BAD_VALUE &&
1811 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1812 (config.sample_rate <= 2 * reqSamplingRate) &&
Glenn Kastenf506e942013-08-06 10:49:43 -07001813 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001814 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Glenn Kasten254af182012-07-03 14:59:05 -07001815 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001816 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001817 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07001818 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001819 }
1820
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001821 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001822
Glenn Kasten46909e72013-02-26 09:20:22 -08001823#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001824 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1825 // or (re-)create if current Pipe is idle and does not match the new format
1826 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001827 enum {
1828 TEE_SINK_NO, // don't copy input
1829 TEE_SINK_NEW, // copy input using a new pipe
1830 TEE_SINK_OLD, // copy input using an existing pipe
1831 } kind;
1832 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1833 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001834 if (!mTeeSinkInputEnabled) {
1835 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08001836 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001837 kind = TEE_SINK_NO;
1838 } else if (mRecordTeeSink == 0) {
1839 kind = TEE_SINK_NEW;
1840 } else if (mRecordTeeSink->getStrongCount() != 1) {
1841 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08001842 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001843 kind = TEE_SINK_OLD;
1844 } else {
1845 kind = TEE_SINK_NEW;
1846 }
1847 switch (kind) {
1848 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001849 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001850 size_t numCounterOffers = 0;
1851 const NBAIO_Format offers[1] = {format};
1852 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1853 ALOG_ASSERT(index == 0);
1854 PipeReader *pipeReader = new PipeReader(*pipe);
1855 numCounterOffers = 0;
1856 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1857 ALOG_ASSERT(index == 0);
1858 mRecordTeeSink = pipe;
1859 mRecordTeeSource = pipeReader;
1860 teeSink = pipe;
1861 }
1862 break;
1863 case TEE_SINK_OLD:
1864 teeSink = mRecordTeeSink;
1865 break;
1866 case TEE_SINK_NO:
1867 default:
1868 break;
1869 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001870#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001871
Dima Zavin799a70e2011-04-18 16:57:27 -07001872 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1873
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001874 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001875 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001876 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001877 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001878 input,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001879 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001880 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001881 *pDevices
1882#ifdef TEE_SINK
1883 , teeSink
1884#endif
1885 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001886 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001887 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001888 if (pSamplingRate != NULL) {
1889 *pSamplingRate = reqSamplingRate;
1890 }
1891 if (pFormat != NULL) {
1892 *pFormat = config.format;
1893 }
1894 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001895 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001896 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001897
Mathias Agopian65ab4712010-07-14 17:59:35 -07001898 // notify client processes of the new input creation
Eric Laurent10351942014-05-08 18:49:52 -07001899 thread->audioConfigChanged_l(mNotificationClients, AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900 return id;
1901 }
1902
1903 return 0;
1904}
1905
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001906status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001907{
Glenn Kastend96c5722012-04-25 13:44:49 -07001908 return closeInput_nonvirtual(input);
1909}
1910
1911status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1912{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001913 // keep strong reference on the record thread so that
1914 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001915 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001916 {
1917 Mutex::Autolock _l(mLock);
1918 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001919 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001920 return BAD_VALUE;
1921 }
1922
Steve Block3856b092011-10-20 11:56:00 +01001923 ALOGV("closeInput() %d", input);
Eric Laurent10351942014-05-08 18:49:52 -07001924 audioConfigChanged_l(mNotificationClients, AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001925 mRecordThreads.removeItem(input);
1926 }
1927 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001928 // The thread entity (active unit of execution) is no longer running here,
1929 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001930
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001931 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001932 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001933 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001934 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001935 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001936
1937 return NO_ERROR;
1938}
1939
Glenn Kastend2304db2014-02-03 07:40:31 -08001940status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941{
1942 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08001943 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944
1945 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1946 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001947 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001948 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001949
1950 return NO_ERROR;
1951}
1952
1953
1954int AudioFlinger::newAudioSessionId()
1955{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001956 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957}
1958
Marco Nelissend457c972014-02-11 08:47:07 -08001959void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001960{
1961 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001962 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08001963 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
1964 if (pid != -1 && (caller == getpid_cached)) {
1965 caller = pid;
1966 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001967
1968 // Ignore requests received from processes not known as notification client. The request
1969 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1970 // called from a different pid leaving a stale session reference. Also we don't know how
1971 // to clear this reference if the client process dies.
1972 if (mNotificationClients.indexOfKey(caller) < 0) {
Marco Nelissend457c972014-02-11 08:47:07 -08001973 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
Eric Laurentd1b28d42013-09-18 18:47:13 -07001974 return;
1975 }
1976
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001977 size_t num = mAudioSessionRefs.size();
1978 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001979 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001980 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1981 ref->mCnt++;
1982 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001983 return;
1984 }
1985 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001986 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1987 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001988}
1989
Marco Nelissend457c972014-02-11 08:47:07 -08001990void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001991{
1992 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001993 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08001994 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
1995 if (pid != -1 && (caller == getpid_cached)) {
1996 caller = pid;
1997 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001998 size_t num = mAudioSessionRefs.size();
1999 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002000 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002001 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2002 ref->mCnt--;
2003 ALOGV(" decremented refcount to %d", ref->mCnt);
2004 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002005 mAudioSessionRefs.removeAt(i);
2006 delete ref;
2007 purgeStaleEffects_l();
2008 }
2009 return;
2010 }
2011 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002012 // If the caller is mediaserver it is likely that the session being released was acquired
2013 // on behalf of a process not in notification clients and we ignore the warning.
2014 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002015}
2016
2017void AudioFlinger::purgeStaleEffects_l() {
2018
Steve Block3856b092011-10-20 11:56:00 +01002019 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002020
2021 Vector< sp<EffectChain> > chains;
2022
2023 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2024 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2025 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2026 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002027 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2028 chains.push(ec);
2029 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002030 }
2031 }
2032 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2033 sp<RecordThread> t = mRecordThreads.valueAt(i);
2034 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2035 sp<EffectChain> ec = t->mEffectChains[j];
2036 chains.push(ec);
2037 }
2038 }
2039
2040 for (size_t i = 0; i < chains.size(); i++) {
2041 sp<EffectChain> ec = chains[i];
2042 int sessionid = ec->sessionId();
2043 sp<ThreadBase> t = ec->mThread.promote();
2044 if (t == 0) {
2045 continue;
2046 }
2047 size_t numsessionrefs = mAudioSessionRefs.size();
2048 bool found = false;
2049 for (size_t k = 0; k < numsessionrefs; k++) {
2050 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002051 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002052 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002053 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002054 found = true;
2055 break;
2056 }
2057 }
2058 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002059 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002060 // remove all effects from the chain
2061 while (ec->mEffects.size()) {
2062 sp<EffectModule> effect = ec->mEffects[0];
2063 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002064 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002065 if (effect->purgeHandles()) {
2066 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002067 }
2068 AudioSystem::unregisterEffect(effect->id());
2069 }
2070 }
2071 }
2072 return;
2073}
2074
Mathias Agopian65ab4712010-07-14 17:59:35 -07002075// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002076AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002077{
Glenn Kastena1117922012-01-26 10:53:32 -08002078 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002079}
2080
2081// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002082AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002083{
2084 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002085 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002086}
2087
2088// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002089AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002090{
Glenn Kastena1117922012-01-26 10:53:32 -08002091 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002092}
2093
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002094uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002095{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002096 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002097}
2098
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002099AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002100{
2101 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2102 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002103 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002104 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002105 return thread;
2106 }
2107 }
2108 return NULL;
2109}
2110
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002111audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002112{
2113 PlaybackThread *thread = primaryPlaybackThread_l();
2114
2115 if (thread == NULL) {
2116 return 0;
2117 }
2118
Eric Laurentf1c04f92012-08-28 14:26:53 -07002119 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002120}
2121
Eric Laurenta011e352012-03-29 15:51:43 -07002122sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2123 int triggerSession,
2124 int listenerSession,
2125 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002126 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002127{
2128 Mutex::Autolock _l(mLock);
2129
2130 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2131 status_t playStatus = NAME_NOT_FOUND;
2132 status_t recStatus = NAME_NOT_FOUND;
2133 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2134 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2135 if (playStatus == NO_ERROR) {
2136 return event;
2137 }
2138 }
2139 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2140 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2141 if (recStatus == NO_ERROR) {
2142 return event;
2143 }
2144 }
2145 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2146 mPendingSyncEvents.add(event);
2147 } else {
2148 ALOGV("createSyncEvent() invalid event %d", event->type());
2149 event.clear();
2150 }
2151 return event;
2152}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002153
Mathias Agopian65ab4712010-07-14 17:59:35 -07002154// ----------------------------------------------------------------------------
2155// Effect management
2156// ----------------------------------------------------------------------------
2157
2158
Glenn Kastenf587ba52012-01-26 16:25:10 -08002159status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002160{
2161 Mutex::Autolock _l(mLock);
2162 return EffectQueryNumberEffects(numEffects);
2163}
2164
Glenn Kastenf587ba52012-01-26 16:25:10 -08002165status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002166{
2167 Mutex::Autolock _l(mLock);
2168 return EffectQueryEffect(index, descriptor);
2169}
2170
Glenn Kasten5e92a782012-01-30 07:40:52 -08002171status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002172 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002173{
2174 Mutex::Autolock _l(mLock);
2175 return EffectGetDescriptor(pUuid, descriptor);
2176}
2177
2178
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002179sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002180 effect_descriptor_t *pDesc,
2181 const sp<IEffectClient>& effectClient,
2182 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002183 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184 int sessionId,
2185 status_t *status,
2186 int *id,
2187 int *enabled)
2188{
2189 status_t lStatus = NO_ERROR;
2190 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002191 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002193 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002194 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002195 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002196
2197 if (pDesc == NULL) {
2198 lStatus = BAD_VALUE;
2199 goto Exit;
2200 }
2201
Eric Laurent84e9a102010-09-23 16:10:16 -07002202 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002203 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002204 lStatus = PERMISSION_DENIED;
2205 goto Exit;
2206 }
2207
Dima Zavinfce7a472011-04-19 22:30:36 -07002208 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002209 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002210 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002211 lStatus = PERMISSION_DENIED;
2212 goto Exit;
2213 }
2214
Mathias Agopian65ab4712010-07-14 17:59:35 -07002215 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002216 if (!EffectIsNullUuid(&pDesc->uuid)) {
2217 // if uuid is specified, request effect descriptor
2218 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2219 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002220 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002221 goto Exit;
2222 }
2223 } else {
2224 // if uuid is not specified, look for an available implementation
2225 // of the required type in effect factory
2226 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002227 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002228 lStatus = BAD_VALUE;
2229 goto Exit;
2230 }
2231 uint32_t numEffects = 0;
2232 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002233 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002234 bool found = false;
2235
2236 lStatus = EffectQueryNumberEffects(&numEffects);
2237 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002238 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002239 goto Exit;
2240 }
2241 for (uint32_t i = 0; i < numEffects; i++) {
2242 lStatus = EffectQueryEffect(i, &desc);
2243 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002244 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002245 continue;
2246 }
2247 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2248 // If matching type found save effect descriptor. If the session is
2249 // 0 and the effect is not auxiliary, continue enumeration in case
2250 // an auxiliary version of this effect type is available
2251 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002252 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002253 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2255 break;
2256 }
2257 }
2258 }
2259 if (!found) {
2260 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002261 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002262 goto Exit;
2263 }
2264 // For same effect type, chose auxiliary version over insert version if
2265 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002266 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002267 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002268 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002269 }
2270 }
2271
2272 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002273 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002274 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2275 lStatus = INVALID_OPERATION;
2276 goto Exit;
2277 }
2278
Eric Laurent59255e42011-07-27 19:49:51 -07002279 // check recording permission for visualizer
2280 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2281 !recordingAllowed()) {
2282 lStatus = PERMISSION_DENIED;
2283 goto Exit;
2284 }
2285
Mathias Agopian65ab4712010-07-14 17:59:35 -07002286 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002287 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002288 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002289 // if the output returned by getOutputForEffect() is removed before we lock the
2290 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2291 // and we will exit safely
2292 io = AudioSystem::getOutputForEffect(&desc);
2293 ALOGV("createEffect got output %d", io);
2294 }
2295
2296 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002297
2298 // If output is not specified try to find a matching audio session ID in one of the
2299 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002300 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2301 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002302 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002303 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002304 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2305 // output must be specified by AudioPolicyManager when using session
2306 // AUDIO_SESSION_OUTPUT_STAGE
2307 lStatus = BAD_VALUE;
2308 goto Exit;
2309 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002310 // look for the thread where the specified audio session is present
2311 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2312 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2313 io = mPlaybackThreads.keyAt(i);
2314 break;
2315 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002316 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002317 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002318 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2319 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2320 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002321 break;
2322 }
2323 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002324 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002325 // If no output thread contains the requested session ID, default to
2326 // first output. The effect chain will be moved to the correct output
2327 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002328 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002329 io = mPlaybackThreads.keyAt(0);
2330 }
Steve Block3856b092011-10-20 11:56:00 +01002331 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002332 }
2333 ThreadBase *thread = checkRecordThread_l(io);
2334 if (thread == NULL) {
2335 thread = checkPlaybackThread_l(io);
2336 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002337 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002338 lStatus = BAD_VALUE;
2339 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002340 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002341 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002342
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002343 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002344
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002345 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002346 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2347 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002348 if (handle != 0 && id != NULL) {
2349 *id = handle->id();
2350 }
2351 }
2352
2353Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002354 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002355 return handle;
2356}
2357
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002358status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2359 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002360{
Steve Block3856b092011-10-20 11:56:00 +01002361 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002362 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002363 Mutex::Autolock _l(mLock);
2364 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002365 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002366 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002367 }
Eric Laurentde070132010-07-13 04:45:46 -07002368 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2369 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002370 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002371 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002372 }
Eric Laurentde070132010-07-13 04:45:46 -07002373 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2374 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002375 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002376 return BAD_VALUE;
2377 }
2378
2379 Mutex::Autolock _dl(dstThread->mLock);
2380 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002381 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002382}
2383
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002384// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002385status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002386 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002387 AudioFlinger::PlaybackThread *dstThread,
2388 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002389{
Steve Block3856b092011-10-20 11:56:00 +01002390 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002391 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002392
Eric Laurent59255e42011-07-27 19:49:51 -07002393 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002394 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002395 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002396 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002397 return INVALID_OPERATION;
2398 }
2399
Eric Laurent39e94f82010-07-28 01:32:47 -07002400 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002401 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002402 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002403 // removed.
2404 srcThread->removeEffectChain_l(chain);
2405
2406 // transfer all effects one by one so that new effect chain is created on new thread with
2407 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002408 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002409 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002410 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002411 Vector< sp<EffectModule> > removed;
2412 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002413 while (effect != 0) {
2414 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002415 removed.add(effect);
2416 status = dstThread->addEffect_l(effect);
2417 if (status != NO_ERROR) {
2418 break;
2419 }
Eric Laurentec35a142011-10-05 17:42:25 -07002420 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2421 if (effect->state() == EffectModule::ACTIVE ||
2422 effect->state() == EffectModule::STOPPING) {
2423 effect->start();
2424 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002425 // if the move request is not received from audio policy manager, the effect must be
2426 // re-registered with the new strategy and output
2427 if (dstChain == 0) {
2428 dstChain = effect->chain().promote();
2429 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002430 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002431 status = NO_INIT;
2432 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002433 }
2434 strategy = dstChain->strategy();
2435 }
2436 if (reRegister) {
2437 AudioSystem::unregisterEffect(effect->id());
2438 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002439 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002440 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002441 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002442 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002443 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002444 }
Eric Laurentde070132010-07-13 04:45:46 -07002445 effect = chain->getEffectFromId_l(0);
2446 }
2447
Eric Laurent5baf2af2013-09-12 17:37:00 -07002448 if (status != NO_ERROR) {
2449 for (size_t i = 0; i < removed.size(); i++) {
2450 srcThread->addEffect_l(removed[i]);
2451 if (dstChain != 0 && reRegister) {
2452 AudioSystem::unregisterEffect(removed[i]->id());
2453 AudioSystem::registerEffect(&removed[i]->desc(),
2454 srcThread->id(),
2455 strategy,
2456 sessionId,
2457 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002458 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002459 }
2460 }
2461 }
2462
2463 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002464}
2465
Eric Laurent5baf2af2013-09-12 17:37:00 -07002466bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002467{
2468 if (mGlobalEffectEnableTime != 0 &&
2469 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2470 return true;
2471 }
2472
2473 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2474 sp<EffectChain> ec =
2475 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002476 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002477 return true;
2478 }
2479 }
2480 return false;
2481}
2482
Eric Laurent5baf2af2013-09-12 17:37:00 -07002483void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002484{
2485 Mutex::Autolock _l(mLock);
2486
2487 mGlobalEffectEnableTime = systemTime();
2488
2489 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2490 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2491 if (t->mType == ThreadBase::OFFLOAD) {
2492 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2493 }
2494 }
2495
2496}
2497
Glenn Kastenda6ef132013-01-10 12:31:01 -08002498struct Entry {
2499#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2500 char mName[MAX_NAME];
2501};
2502
2503int comparEntry(const void *p1, const void *p2)
2504{
2505 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2506}
2507
Glenn Kasten46909e72013-02-26 09:20:22 -08002508#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002509void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002510{
Eric Laurent81784c32012-11-19 14:55:58 -08002511 NBAIO_Source *teeSource = source.get();
2512 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002513 // .wav rotation
2514 // There is a benign race condition if 2 threads call this simultaneously.
2515 // They would both traverse the directory, but the result would simply be
2516 // failures at unlink() which are ignored. It's also unlikely since
2517 // normally dumpsys is only done by bugreport or from the command line.
2518 char teePath[32+256];
2519 strcpy(teePath, "/data/misc/media");
2520 size_t teePathLen = strlen(teePath);
2521 DIR *dir = opendir(teePath);
2522 teePath[teePathLen++] = '/';
2523 if (dir != NULL) {
2524#define MAX_SORT 20 // number of entries to sort
2525#define MAX_KEEP 10 // number of entries to keep
2526 struct Entry entries[MAX_SORT];
2527 size_t entryCount = 0;
2528 while (entryCount < MAX_SORT) {
2529 struct dirent de;
2530 struct dirent *result = NULL;
2531 int rc = readdir_r(dir, &de, &result);
2532 if (rc != 0) {
2533 ALOGW("readdir_r failed %d", rc);
2534 break;
2535 }
2536 if (result == NULL) {
2537 break;
2538 }
2539 if (result != &de) {
2540 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2541 break;
2542 }
2543 // ignore non .wav file entries
2544 size_t nameLen = strlen(de.d_name);
2545 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2546 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2547 continue;
2548 }
2549 strcpy(entries[entryCount++].mName, de.d_name);
2550 }
2551 (void) closedir(dir);
2552 if (entryCount > MAX_KEEP) {
2553 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2554 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2555 strcpy(&teePath[teePathLen], entries[i].mName);
2556 (void) unlink(teePath);
2557 }
2558 }
2559 } else {
2560 if (fd >= 0) {
2561 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2562 }
2563 }
Eric Laurent81784c32012-11-19 14:55:58 -08002564 char teeTime[16];
2565 struct timeval tv;
2566 gettimeofday(&tv, NULL);
2567 struct tm tm;
2568 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002569 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2570 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2571 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2572 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002573 if (teeFd >= 0) {
2574 char wavHeader[44];
2575 memcpy(wavHeader,
2576 "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",
2577 sizeof(wavHeader));
2578 NBAIO_Format format = teeSource->format();
2579 unsigned channelCount = Format_channelCount(format);
2580 ALOG_ASSERT(channelCount <= FCC_2);
2581 uint32_t sampleRate = Format_sampleRate(format);
2582 wavHeader[22] = channelCount; // number of channels
2583 wavHeader[24] = sampleRate; // sample rate
2584 wavHeader[25] = sampleRate >> 8;
2585 wavHeader[32] = channelCount * 2; // block alignment
2586 write(teeFd, wavHeader, sizeof(wavHeader));
2587 size_t total = 0;
2588 bool firstRead = true;
2589 for (;;) {
2590#define TEE_SINK_READ 1024
2591 short buffer[TEE_SINK_READ * FCC_2];
2592 size_t count = TEE_SINK_READ;
2593 ssize_t actual = teeSource->read(buffer, count,
2594 AudioBufferProvider::kInvalidPTS);
2595 bool wasFirstRead = firstRead;
2596 firstRead = false;
2597 if (actual <= 0) {
2598 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2599 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002600 }
Eric Laurent81784c32012-11-19 14:55:58 -08002601 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002602 }
Eric Laurent81784c32012-11-19 14:55:58 -08002603 ALOG_ASSERT(actual <= (ssize_t)count);
2604 write(teeFd, buffer, actual * channelCount * sizeof(short));
2605 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002606 }
Eric Laurent81784c32012-11-19 14:55:58 -08002607 lseek(teeFd, (off_t) 4, SEEK_SET);
2608 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2609 write(teeFd, &temp, sizeof(temp));
2610 lseek(teeFd, (off_t) 40, SEEK_SET);
2611 temp = total * channelCount * sizeof(short);
2612 write(teeFd, &temp, sizeof(temp));
2613 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002614 if (fd >= 0) {
2615 fdprintf(fd, "tee copied to %s\n", teePath);
2616 }
Eric Laurent59255e42011-07-27 19:49:51 -07002617 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002618 if (fd >= 0) {
2619 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2620 }
Eric Laurent59255e42011-07-27 19:49:51 -07002621 }
2622 }
2623}
Glenn Kasten46909e72013-02-26 09:20:22 -08002624#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002625
Mathias Agopian65ab4712010-07-14 17:59:35 -07002626// ----------------------------------------------------------------------------
2627
2628status_t AudioFlinger::onTransact(
2629 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2630{
2631 return BnAudioFlinger::onTransact(code, data, reply, flags);
2632}
2633
Mathias Agopian65ab4712010-07-14 17:59:35 -07002634}; // namespace android