blob: b85b649ca6cee79d39dff2f410394785a59d25ef [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
Andy Hung6770c6f2015-04-07 13:43:36 -070048#include <media/AudioResamplerPublic.h>
49
Mathias Agopian65ab4712010-07-14 17:59:35 -070050#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070051#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070052#include <audio_effects/effect_ns.h>
53#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
Glenn Kasten3b21c502011-12-15 09:52:39 -080055#include <audio_utils/primitives.h>
56
Eric Laurentfeb0db62011-07-22 09:04:31 -070057#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080058
John Grossman4ff14ba2012-02-08 16:37:41 -080059#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070060
Glenn Kasten9e58b552013-01-18 15:09:48 -080061#include <media/IMediaLogService.h>
62
Glenn Kastenda6ef132013-01-10 12:31:01 -080063#include <media/nbaio/Pipe.h>
64#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070065#include <media/AudioParameter.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070066#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080067
Mathias Agopian65ab4712010-07-14 17:59:35 -070068// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070069
John Grossman1c345192012-03-27 14:00:17 -070070// Note: the following macro is used for extremely verbose logging message. In
71// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
72// 0; but one side effect of this is to turn all LOGV's as well. Some messages
73// are so verbose that we want to suppress them even when we have ALOG_ASSERT
74// turned on. Do not uncomment the #def below unless you really know what you
75// are doing and want to see all of the extremely verbose messages.
76//#define VERY_VERY_VERBOSE_LOGGING
77#ifdef VERY_VERY_VERBOSE_LOGGING
78#define ALOGVV ALOGV
79#else
80#define ALOGVV(a...) do { } while(0)
81#endif
Eric Laurentde070132010-07-13 04:45:46 -070082
Mathias Agopian65ab4712010-07-14 17:59:35 -070083namespace android {
84
Glenn Kastenec1d6b52011-12-12 09:04:45 -080085static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
86static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070087static const char kClientLockedString[] = "Client lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070088
Glenn Kasten58912562012-04-03 10:45:00 -070089
John Grossman4ff14ba2012-02-08 16:37:41 -080090nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080091
Eric Laurent81784c32012-11-19 14:55:58 -080092uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070093
Glenn Kasten46909e72013-02-26 09:20:22 -080094#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080095bool AudioFlinger::mTeeSinkInputEnabled = false;
96bool AudioFlinger::mTeeSinkOutputEnabled = false;
97bool AudioFlinger::mTeeSinkTrackEnabled = false;
98
99size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
100size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
101size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800102#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800103
Eric Laurent813e2a72013-08-31 12:59:48 -0700104// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
105// we define a minimum time during which a global effect is considered enabled.
106static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
107
Mathias Agopian65ab4712010-07-14 17:59:35 -0700108// ----------------------------------------------------------------------------
109
Marco Nelissenb2208842014-02-07 14:00:50 -0800110const char *formatToString(audio_format_t format) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530111 switch (format & AUDIO_FORMAT_MAIN_MASK) {
112 case AUDIO_FORMAT_PCM:
113 switch (format) {
114 case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
115 case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
116 case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
117 case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
118 case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
119 case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
120 default:
121 break;
122 }
123 break;
Marco Nelissenb2208842014-02-07 14:00:50 -0800124 case AUDIO_FORMAT_MP3: return "mp3";
125 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
126 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
127 case AUDIO_FORMAT_AAC: return "aac";
128 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
129 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
130 case AUDIO_FORMAT_VORBIS: return "vorbis";
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530131 case AUDIO_FORMAT_OPUS: return "opus";
132 case AUDIO_FORMAT_AC3: return "ac-3";
133 case AUDIO_FORMAT_E_AC3: return "e-ac-3";
Marco Nelissenb2208842014-02-07 14:00:50 -0800134 default:
135 break;
136 }
137 return "unknown";
138}
139
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700140static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700141{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700142 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700143 int rc;
144
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700145 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
146 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
147 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
148 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700149 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700150 }
151 rc = audio_hw_device_open(mod, dev);
152 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
153 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
154 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700155 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700156 }
Eric Laurent5806b352014-05-22 12:23:26 -0700157 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700158 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
159 rc = BAD_VALUE;
160 goto out;
161 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700162 return 0;
163
164out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700165 *dev = NULL;
166 return rc;
167}
168
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169// ----------------------------------------------------------------------------
170
171AudioFlinger::AudioFlinger()
172 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800173 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800174 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700175 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800176 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800177 mMasterMute(false),
178 mNextUniqueId(1),
179 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700180 mBtNrecIsOff(false),
181 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700182 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700183 mGlobalEffectEnableTime(0),
Eric Laurent72e3f392015-05-20 14:43:50 -0700184 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185{
Glenn Kasten949a9262013-04-16 12:35:20 -0700186 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800187 char value[PROPERTY_VALUE_MAX];
188 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
189 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800190 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
191 MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800192 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700193
Glenn Kasten46909e72013-02-26 09:20:22 -0800194#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800195 (void) property_get("ro.debuggable", value, "0");
196 int debuggable = atoi(value);
197 int teeEnabled = 0;
198 if (debuggable) {
199 (void) property_get("af.tee", value, "0");
200 teeEnabled = atoi(value);
201 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800202 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700203 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800204 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700205 }
206 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800207 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700208 }
209 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800210 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700211 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800212#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700213}
214
215void AudioFlinger::onFirstRef()
216{
Dima Zavin799a70e2011-04-18 16:57:27 -0700217 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700218
Eric Laurent93575202011-01-18 18:39:02 -0800219 Mutex::Autolock _l(mLock);
220
Dima Zavin799a70e2011-04-18 16:57:27 -0700221 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800222 char val_str[PROPERTY_VALUE_MAX] = { 0 };
223 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
224 uint32_t int_val;
225 if (1 == sscanf(val_str, "%u", &int_val)) {
226 mStandbyTimeInNsecs = milliseconds(int_val);
227 ALOGI("Using %u mSec as standby time.", int_val);
228 } else {
229 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
230 ALOGI("Using default %u mSec as standby time.",
231 (uint32_t)(mStandbyTimeInNsecs / 1000000));
232 }
233 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234
Eric Laurent1c333e22014-05-20 10:48:17 -0700235 mPatchPanel = new PatchPanel(this);
236
Eric Laurenta4c5a552012-03-29 10:12:40 -0700237 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238}
239
240AudioFlinger::~AudioFlinger()
241{
242 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700243 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700244 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700245 }
246 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700247 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700248 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700250
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800251 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
252 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700253 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
254 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700256
257 // Tell media.log service about any old writers that still need to be unregistered
258 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
259 if (binder != 0) {
260 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
261 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
262 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
263 mUnregisteredWriters.pop();
264 mediaLogService->unregisterWriter(iMemory);
265 }
266 }
267
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268}
269
Eric Laurenta4c5a552012-03-29 10:12:40 -0700270static const char * const audio_interfaces[] = {
271 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
272 AUDIO_HARDWARE_MODULE_ID_A2DP,
273 AUDIO_HARDWARE_MODULE_ID_USB,
274};
275#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
276
Phil Burk062e67a2015-02-11 13:40:50 -0800277AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700278 audio_module_handle_t module,
279 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700280{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700281 // if module is 0, the request comes from an old policy manager and we should load
282 // well known modules
283 if (module == 0) {
284 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
285 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
286 loadHwModule_l(audio_interfaces[i]);
287 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700288 // then try to find a module supporting the requested device.
289 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
290 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
291 audio_hw_device_t *dev = audioHwDevice->hwDevice();
292 if ((dev->get_supported_devices != NULL) &&
293 (dev->get_supported_devices(dev) & devices) == devices)
294 return audioHwDevice;
295 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700296 } else {
297 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700298 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
299 if (audioHwDevice != NULL) {
300 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700301 }
302 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700303
Dima Zavin799a70e2011-04-18 16:57:27 -0700304 return NULL;
305}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306
Glenn Kasten0f11b512014-01-31 16:18:54 -0800307void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700308{
309 const size_t SIZE = 256;
310 char buffer[SIZE];
311 String8 result;
312
313 result.append("Clients:\n");
314 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800315 sp<Client> client = mClients.valueAt(i).promote();
316 if (client != 0) {
317 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
318 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 }
320 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700321
Eric Laurentd1b28d42013-09-18 18:47:13 -0700322 result.append("Notification Clients:\n");
323 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
324 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
325 result.append(buffer);
326 }
327
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700328 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800329 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700330 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
331 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800332 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700333 result.append(buffer);
334 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336}
337
338
Glenn Kasten0f11b512014-01-31 16:18:54 -0800339void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700340{
341 const size_t SIZE = 256;
342 char buffer[SIZE];
343 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800344 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345
John Grossman4ff14ba2012-02-08 16:37:41 -0800346 snprintf(buffer, SIZE, "Hardware status: %d\n"
347 "Standby Time mSec: %u\n",
348 hardwareStatus,
349 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350 result.append(buffer);
351 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352}
353
Glenn Kasten0f11b512014-01-31 16:18:54 -0800354void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355{
356 const size_t SIZE = 256;
357 char buffer[SIZE];
358 String8 result;
359 snprintf(buffer, SIZE, "Permission Denial: "
360 "can't dump AudioFlinger from pid=%d, uid=%d\n",
361 IPCThreadState::self()->getCallingPid(),
362 IPCThreadState::self()->getCallingUid());
363 result.append(buffer);
364 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365}
366
Eric Laurent81784c32012-11-19 14:55:58 -0800367bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368{
369 bool locked = false;
370 for (int i = 0; i < kDumpLockRetries; ++i) {
371 if (mutex.tryLock() == NO_ERROR) {
372 locked = true;
373 break;
374 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800375 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376 }
377 return locked;
378}
379
380status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
381{
Glenn Kasten44deb052012-02-05 18:09:08 -0800382 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 dumpPermissionDenial(fd, args);
384 } else {
385 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800386 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387 if (!hardwareLocked) {
388 String8 result(kHardwareLockedString);
389 write(fd, result.string(), result.size());
390 } else {
391 mHardwareLock.unlock();
392 }
393
Eric Laurent81784c32012-11-19 14:55:58 -0800394 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395
396 // failed to lock - AudioFlinger is probably deadlocked
397 if (!locked) {
398 String8 result(kDeadlockedString);
399 write(fd, result.string(), result.size());
400 }
401
Eric Laurent021cf962014-05-13 10:18:14 -0700402 bool clientLocked = dumpTryLock(mClientLock);
403 if (!clientLocked) {
404 String8 result(kClientLockedString);
405 write(fd, result.string(), result.size());
406 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700407
408 EffectDumpEffects(fd);
409
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700411 if (clientLocked) {
412 mClientLock.unlock();
413 }
414
Mathias Agopian65ab4712010-07-14 17:59:35 -0700415 dumpInternals(fd, args);
416
417 // dump playback threads
418 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
419 mPlaybackThreads.valueAt(i)->dump(fd, args);
420 }
421
422 // dump record threads
423 for (size_t i = 0; i < mRecordThreads.size(); i++) {
424 mRecordThreads.valueAt(i)->dump(fd, args);
425 }
426
Eric Laurentaaa44472014-09-12 17:41:50 -0700427 // dump orphan effect chains
428 if (mOrphanEffectChains.size() != 0) {
429 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
430 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
431 mOrphanEffectChains.valueAt(i)->dump(fd, args);
432 }
433 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700434 // dump all hardware devs
435 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700436 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700437 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700439
Glenn Kasten46909e72013-02-26 09:20:22 -0800440#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700441 // dump the serially shared record tee sink
442 if (mRecordTeeSource != 0) {
443 dumpTee(fd, mRecordTeeSource);
444 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800445#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700446
Glenn Kastend65d73c2012-06-22 17:21:07 -0700447 if (locked) {
448 mLock.unlock();
449 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800450
451 // append a copy of media.log here by forwarding fd to it, but don't attempt
452 // to lookup the service if it's not running, as it will block for a second
453 if (mLogMemoryDealer != 0) {
454 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
455 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700456 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800457 Vector<String16> args;
458 binder->dump(fd, args);
459 }
460 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461 }
462 return NO_ERROR;
463}
464
Eric Laurent021cf962014-05-13 10:18:14 -0700465sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800466{
Eric Laurent021cf962014-05-13 10:18:14 -0700467 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800468 // If pid is already in the mClients wp<> map, then use that entry
469 // (for which promote() is always != 0), otherwise create a new entry and Client.
470 sp<Client> client = mClients.valueFor(pid).promote();
471 if (client == 0) {
472 client = new Client(this, pid);
473 mClients.add(pid, client);
474 }
475
476 return client;
477}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700478
Glenn Kasten9e58b552013-01-18 15:09:48 -0800479sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
480{
Glenn Kasten481fb672013-09-30 14:39:28 -0700481 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800482 if (mLogMemoryDealer == 0) {
483 return new NBLog::Writer();
484 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800485 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700486 // Similarly if we can't contact the media.log service, also return a dummy writer
487 if (binder == 0) {
488 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800489 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700490 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
491 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
492 // If allocation fails, consult the vector of previously unregistered writers
493 // and garbage-collect one or more them until an allocation succeeds
494 if (shared == 0) {
495 Mutex::Autolock _l(mUnregisteredWritersLock);
496 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
497 {
498 // Pick the oldest stale writer to garbage-collect
499 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
500 mUnregisteredWriters.removeAt(0);
501 mediaLogService->unregisterWriter(iMemory);
502 // Now the media.log remote reference to IMemory is gone. When our last local
503 // reference to IMemory also drops to zero at end of this block,
504 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
505 }
506 // Re-attempt the allocation
507 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
508 if (shared != 0) {
509 goto success;
510 }
511 }
512 // Even after garbage-collecting all old writers, there is still not enough memory,
513 // so return a dummy writer
514 return new NBLog::Writer();
515 }
516success:
517 mediaLogService->registerWriter(shared, size, name);
518 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800519}
520
521void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
522{
Glenn Kasten685ef092013-02-04 08:15:34 -0800523 if (writer == 0) {
524 return;
525 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800526 sp<IMemory> iMemory(writer->getIMemory());
527 if (iMemory == 0) {
528 return;
529 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700530 // Rather than removing the writer immediately, append it to a queue of old writers to
531 // be garbage-collected later. This allows us to continue to view old logs for a while.
532 Mutex::Autolock _l(mUnregisteredWritersLock);
533 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800534}
535
Mathias Agopian65ab4712010-07-14 17:59:35 -0700536// IAudioFlinger interface
537
538
539sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800540 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800542 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700543 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800544 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800545 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800547 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800548 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800550 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551 status_t *status)
552{
553 sp<PlaybackThread::Track> track;
554 sp<TrackHandle> trackHandle;
555 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 status_t lStatus;
557 int lSessionId;
558
Glenn Kasten263709e2012-01-06 08:40:01 -0800559 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
560 // but if someone uses binder directly they could bypass that and cause us to crash
561 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000562 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563 lStatus = BAD_VALUE;
564 goto Exit;
565 }
566
Glenn Kasten53b5d092014-02-05 10:00:23 -0800567 // further sample rate checks are performed by createTrack_l() depending on the thread type
568 if (sampleRate == 0) {
569 ALOGE("createTrack() invalid sample rate %u", sampleRate);
570 lStatus = BAD_VALUE;
571 goto Exit;
572 }
573
574 // further channel mask checks are performed by createTrack_l() depending on the thread type
575 if (!audio_is_output_channel(channelMask)) {
576 ALOGE("createTrack() invalid channel mask %#x", channelMask);
577 lStatus = BAD_VALUE;
578 goto Exit;
579 }
580
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700581 // further format checks are performed by createTrack_l() depending on the thread type
582 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800583 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700584 lStatus = BAD_VALUE;
585 goto Exit;
586 }
587
Glenn Kasten663c2242013-09-24 11:52:37 -0700588 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
589 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
590 lStatus = BAD_VALUE;
591 goto Exit;
592 }
593
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594 {
595 Mutex::Autolock _l(mLock);
596 PlaybackThread *thread = checkPlaybackThread_l(output);
597 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800598 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599 lStatus = BAD_VALUE;
600 goto Exit;
601 }
602
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800603 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700604 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605
Glenn Kastene848bd92014-03-13 15:00:32 -0700606 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700607 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700608 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700609 // check if an effect chain with the same session ID is present on another
610 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700611 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700612 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
613 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700614 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700615 if (sessions & PlaybackThread::EFFECT_SESSION) {
616 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700617 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700618 }
Eric Laurentde070132010-07-13 04:45:46 -0700619 }
620 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700622 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700623 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700624 if (sessionId != NULL) {
625 *sessionId = lSessionId;
626 }
627 }
Steve Block3856b092011-10-20 11:56:00 +0100628 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629
630 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800631 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800632 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700633 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700634
635 // move effect chain to this output thread if an effect on same session was waiting
636 // for a track to be created
637 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700638 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700639 Mutex::Autolock _dl(thread->mLock);
640 Mutex::Autolock _sl(effectThread->mLock);
641 moveEffectChain_l(lSessionId, effectThread, thread, true);
642 }
Eric Laurenta011e352012-03-29 15:51:43 -0700643
644 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700645 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700646 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
647 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700648 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700649 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700650 } else {
651 mPendingSyncEvents[i]->cancel();
652 }
Eric Laurenta011e352012-03-29 15:51:43 -0700653 mPendingSyncEvents.removeAt(i);
654 i--;
655 }
656 }
657 }
Glenn Kastene198c362013-08-13 09:13:36 -0700658
Eric Laurentfa90e842014-10-17 18:12:31 -0700659 setAudioHwSyncForSession_l(thread, (audio_session_t)lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660 }
Glenn Kastene198c362013-08-13 09:13:36 -0700661
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700662 if (lStatus != NO_ERROR) {
663 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700664 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700665 // Don't hold mClientLock when releasing the reference on the track as the
666 // destructor will acquire it.
667 {
668 Mutex::Autolock _cl(mClientLock);
669 client.clear();
670 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700672 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700673 }
674
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700675 // return handle to client
676 trackHandle = new TrackHandle(track);
677
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700679 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680 return trackHandle;
681}
682
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800683uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684{
685 Mutex::Autolock _l(mLock);
686 PlaybackThread *thread = checkPlaybackThread_l(output);
687 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000688 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689 return 0;
690 }
691 return thread->sampleRate();
692}
693
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800694audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695{
696 Mutex::Autolock _l(mLock);
697 PlaybackThread *thread = checkPlaybackThread_l(output);
698 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000699 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800700 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 }
702 return thread->format();
703}
704
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800705size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706{
707 Mutex::Autolock _l(mLock);
708 PlaybackThread *thread = checkPlaybackThread_l(output);
709 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000710 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 return 0;
712 }
Glenn Kasten58912562012-04-03 10:45:00 -0700713 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
714 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715 return thread->frameCount();
716}
717
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800718uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700719{
720 Mutex::Autolock _l(mLock);
721 PlaybackThread *thread = checkPlaybackThread_l(output);
722 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800723 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724 return 0;
725 }
726 return thread->latency();
727}
728
729status_t AudioFlinger::setMasterVolume(float value)
730{
Eric Laurenta1884f92011-08-23 08:25:03 -0700731 status_t ret = initCheck();
732 if (ret != NO_ERROR) {
733 return ret;
734 }
735
Mathias Agopian65ab4712010-07-14 17:59:35 -0700736 // check calling permissions
737 if (!settingsAllowed()) {
738 return PERMISSION_DENIED;
739 }
740
Eric Laurenta4c5a552012-03-29 10:12:40 -0700741 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700742 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700743
John Grossmanee578c02012-07-23 17:05:46 -0700744 // Set master volume in the HALs which support it.
745 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
746 AutoMutex lock(mHardwareLock);
747 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800748
John Grossmanee578c02012-07-23 17:05:46 -0700749 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
750 if (dev->canSetMasterVolume()) {
751 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800752 }
John Grossmanee578c02012-07-23 17:05:46 -0700753 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755
John Grossmanee578c02012-07-23 17:05:46 -0700756 // Now set the master volume in each playback thread. Playback threads
757 // assigned to HALs which do not have master volume support will apply
758 // master volume during the mix operation. Threads with HALs which do
759 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700760 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
761 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
762 continue;
763 }
John Grossmanee578c02012-07-23 17:05:46 -0700764 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700765 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766
767 return NO_ERROR;
768}
769
Glenn Kastenf78aee72012-01-04 11:00:47 -0800770status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771{
Eric Laurenta1884f92011-08-23 08:25:03 -0700772 status_t ret = initCheck();
773 if (ret != NO_ERROR) {
774 return ret;
775 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776
777 // check calling permissions
778 if (!settingsAllowed()) {
779 return PERMISSION_DENIED;
780 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800781 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000782 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783 return BAD_VALUE;
784 }
785
786 { // scope for the lock
787 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700788 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700790 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791 mHardwareStatus = AUDIO_HW_IDLE;
792 }
793
794 if (NO_ERROR == ret) {
795 Mutex::Autolock _l(mLock);
796 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800797 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700798 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700799 }
800
801 return ret;
802}
803
804status_t AudioFlinger::setMicMute(bool state)
805{
Eric Laurenta1884f92011-08-23 08:25:03 -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
816 AutoMutex lock(mHardwareLock);
817 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700818 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
819 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
820 status_t result = dev->set_mic_mute(dev, state);
821 if (result != NO_ERROR) {
822 ret = result;
823 }
824 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825 mHardwareStatus = AUDIO_HW_IDLE;
826 return ret;
827}
828
829bool AudioFlinger::getMicMute() const
830{
Eric Laurenta1884f92011-08-23 08:25:03 -0700831 status_t ret = initCheck();
832 if (ret != NO_ERROR) {
833 return false;
834 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800835 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700836 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800837 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800839 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
840 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
841 status_t result = dev->get_mic_mute(dev, &state);
842 if (result == NO_ERROR) {
843 mute = mute && state;
844 }
845 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800847
848 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849}
850
851status_t AudioFlinger::setMasterMute(bool muted)
852{
John Grossmand8f178d2012-07-20 14:51:35 -0700853 status_t ret = initCheck();
854 if (ret != NO_ERROR) {
855 return ret;
856 }
857
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 // check calling permissions
859 if (!settingsAllowed()) {
860 return PERMISSION_DENIED;
861 }
862
John Grossmanee578c02012-07-23 17:05:46 -0700863 Mutex::Autolock _l(mLock);
864 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700865
John Grossmanee578c02012-07-23 17:05:46 -0700866 // Set master mute in the HALs which support it.
867 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
868 AutoMutex lock(mHardwareLock);
869 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700870
John Grossmanee578c02012-07-23 17:05:46 -0700871 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
872 if (dev->canSetMasterMute()) {
873 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700874 }
John Grossmanee578c02012-07-23 17:05:46 -0700875 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700876 }
877
John Grossmanee578c02012-07-23 17:05:46 -0700878 // Now set the master mute in each playback thread. Playback threads
879 // assigned to HALs which do not have master mute support will apply master
880 // mute during the mix operation. Threads with HALs which do support master
881 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700882 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
883 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
884 continue;
885 }
John Grossmanee578c02012-07-23 17:05:46 -0700886 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700887 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888
889 return NO_ERROR;
890}
891
892float AudioFlinger::masterVolume() const
893{
Glenn Kasten98067102011-12-13 11:47:54 -0800894 Mutex::Autolock _l(mLock);
895 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700896}
897
898bool AudioFlinger::masterMute() const
899{
Glenn Kasten98067102011-12-13 11:47:54 -0800900 Mutex::Autolock _l(mLock);
901 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902}
903
John Grossman4ff14ba2012-02-08 16:37:41 -0800904float AudioFlinger::masterVolume_l() const
905{
John Grossman4ff14ba2012-02-08 16:37:41 -0800906 return mMasterVolume;
907}
908
John Grossmand8f178d2012-07-20 14:51:35 -0700909bool AudioFlinger::masterMute_l() const
910{
John Grossmanee578c02012-07-23 17:05:46 -0700911 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700912}
913
Eric Laurent223fd5c2014-11-11 13:43:36 -0800914status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
915{
916 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
917 ALOGW("setStreamVolume() invalid stream %d", stream);
918 return BAD_VALUE;
919 }
920 pid_t caller = IPCThreadState::self()->getCallingPid();
921 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
922 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
923 return PERMISSION_DENIED;
924 }
925
926 return NO_ERROR;
927}
928
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800929status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
930 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931{
932 // check calling permissions
933 if (!settingsAllowed()) {
934 return PERMISSION_DENIED;
935 }
936
Eric Laurent223fd5c2014-11-11 13:43:36 -0800937 status_t status = checkStreamType(stream);
938 if (status != NO_ERROR) {
939 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800941 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942
943 AutoMutex lock(mLock);
944 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700945 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 thread = checkPlaybackThread_l(output);
947 if (thread == NULL) {
948 return BAD_VALUE;
949 }
950 }
951
952 mStreamTypes[stream].volume = value;
953
954 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800955 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700956 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957 }
958 } else {
959 thread->setStreamVolume(stream, value);
960 }
961
962 return NO_ERROR;
963}
964
Glenn Kastenfff6d712012-01-12 16:38:12 -0800965status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700966{
967 // check calling permissions
968 if (!settingsAllowed()) {
969 return PERMISSION_DENIED;
970 }
971
Eric Laurent223fd5c2014-11-11 13:43:36 -0800972 status_t status = checkStreamType(stream);
973 if (status != NO_ERROR) {
974 return status;
975 }
976 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
977
978 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000979 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700980 return BAD_VALUE;
981 }
982
Eric Laurent93575202011-01-18 18:39:02 -0800983 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700985 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700986 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700987
988 return NO_ERROR;
989}
990
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800991float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800993 status_t status = checkStreamType(stream);
994 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995 return 0.0f;
996 }
997
998 AutoMutex lock(mLock);
999 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001000 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001001 PlaybackThread *thread = checkPlaybackThread_l(output);
1002 if (thread == NULL) {
1003 return 0.0f;
1004 }
1005 volume = thread->streamVolume(stream);
1006 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001007 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008 }
1009
1010 return volume;
1011}
1012
Glenn Kastenfff6d712012-01-12 16:38:12 -08001013bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001014{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001015 status_t status = checkStreamType(stream);
1016 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001017 return true;
1018 }
1019
Glenn Kasten6637baa2012-01-09 09:40:36 -08001020 AutoMutex lock(mLock);
1021 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022}
1023
Eric Laurent054d9d32015-04-24 08:48:48 -07001024
1025void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1026{
1027 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1028 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1029 }
1030}
1031
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001032status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001033{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001034 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1035 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001036
Mathias Agopian65ab4712010-07-14 17:59:35 -07001037 // check calling permissions
1038 if (!settingsAllowed()) {
1039 return PERMISSION_DENIED;
1040 }
1041
Glenn Kasten142f5192014-03-25 17:44:59 -07001042 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1043 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001044 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001045 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001046 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001047 AutoMutex lock(mHardwareLock);
1048 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1049 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1050 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1051 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1052 final_result = result ?: final_result;
1053 }
1054 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001055 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001056 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1057 AudioParameter param = AudioParameter(keyValuePairs);
1058 String8 value;
1059 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001060 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1061 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001062 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1063 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001064 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001065 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1066 // collect all of the thread's session IDs
1067 KeyedVector<int, bool> ids = thread->sessionIds();
1068 // suspend effects associated with those session IDs
1069 for (size_t j = 0; j < ids.size(); ++j) {
1070 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001071 thread->setEffectSuspended(FX_IID_AEC,
1072 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001073 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001074 thread->setEffectSuspended(FX_IID_NS,
1075 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001076 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001077 }
1078 }
Eric Laurentbee53372011-08-29 12:42:48 -07001079 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001080 }
1081 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001082 String8 screenState;
1083 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1084 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001085 if (isOff != (AudioFlinger::mScreenState & 1)) {
1086 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001087 }
1088 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001089 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090 }
1091
1092 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1093 // and the thread is exited once the lock is released
1094 sp<ThreadBase> thread;
1095 {
1096 Mutex::Autolock _l(mLock);
1097 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001098 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001099 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001100 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001101 // indicate output device change to all input threads for pre processing
1102 AudioParameter param = AudioParameter(keyValuePairs);
1103 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001104 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1105 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001106 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001107 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001108 }
1109 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001110 if (thread != 0) {
1111 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112 }
1113 return BAD_VALUE;
1114}
1115
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001116String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001118 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1119 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001120
Eric Laurenta4c5a552012-03-29 10:12:40 -07001121 Mutex::Autolock _l(mLock);
1122
Glenn Kasten142f5192014-03-25 17:44:59 -07001123 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001124 String8 out_s8;
1125
Dima Zavin799a70e2011-04-18 16:57:27 -07001126 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001127 char *s;
1128 {
1129 AutoMutex lock(mHardwareLock);
1130 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001131 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001132 s = dev->get_parameters(dev, keys.string());
1133 mHardwareStatus = AUDIO_HW_IDLE;
1134 }
John Grossmanef7740b2012-02-09 11:28:36 -08001135 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001136 free(s);
1137 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001138 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139 }
1140
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1142 if (playbackThread != NULL) {
1143 return playbackThread->getParameters(keys);
1144 }
1145 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1146 if (recordThread != NULL) {
1147 return recordThread->getParameters(keys);
1148 }
1149 return String8("");
1150}
1151
Glenn Kastendd8104c2012-07-02 12:42:44 -07001152size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1153 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154{
Eric Laurenta1884f92011-08-23 08:25:03 -07001155 status_t ret = initCheck();
1156 if (ret != NO_ERROR) {
1157 return 0;
1158 }
Andy Hung6770c6f2015-04-07 13:43:36 -07001159 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
1160 return 0;
1161 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001162
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001163 AutoMutex lock(mHardwareLock);
1164 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001165 audio_config_t config, proposed;
1166 memset(&proposed, 0, sizeof(proposed));
1167 proposed.sample_rate = sampleRate;
1168 proposed.channel_mask = channelMask;
1169 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001170
John Grossmanee578c02012-07-23 17:05:46 -07001171 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001172 size_t frames;
1173 for (;;) {
1174 // Note: config is currently a const parameter for get_input_buffer_size()
1175 // but we use a copy from proposed in case config changes from the call.
1176 config = proposed;
1177 frames = dev->get_input_buffer_size(dev, &config);
1178 if (frames != 0) {
1179 break; // hal success, config is the result
1180 }
1181 // change one parameter of the configuration each iteration to a more "common" value
1182 // to see if the device will support it.
1183 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1184 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1185 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1186 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1187 } else {
1188 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1189 "format %#x, channelMask 0x%X",
1190 sampleRate, format, channelMask);
1191 break; // retries failed, break out of loop with frames == 0.
1192 }
1193 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001194 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001195 if (frames > 0 && config.sample_rate != sampleRate) {
1196 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1197 }
1198 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199}
1200
Glenn Kasten5f972c02014-01-13 09:59:31 -08001201uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001202{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203 Mutex::Autolock _l(mLock);
1204
1205 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1206 if (recordThread != NULL) {
1207 return recordThread->getInputFramesLost();
1208 }
1209 return 0;
1210}
1211
1212status_t AudioFlinger::setVoiceVolume(float value)
1213{
Eric Laurenta1884f92011-08-23 08:25:03 -07001214 status_t ret = initCheck();
1215 if (ret != NO_ERROR) {
1216 return ret;
1217 }
1218
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219 // check calling permissions
1220 if (!settingsAllowed()) {
1221 return PERMISSION_DENIED;
1222 }
1223
1224 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001225 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001226 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001227 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001228 mHardwareStatus = AUDIO_HW_IDLE;
1229
1230 return ret;
1231}
1232
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001233status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001234 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235{
1236 status_t status;
1237
1238 Mutex::Autolock _l(mLock);
1239
1240 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1241 if (playbackThread != NULL) {
1242 return playbackThread->getRenderPosition(halFrames, dspFrames);
1243 }
1244
1245 return BAD_VALUE;
1246}
1247
1248void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1249{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001250 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001251 if (client == 0) {
1252 return;
1253 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001254 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001255 {
1256 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001257 if (mNotificationClients.indexOfKey(pid) < 0) {
1258 sp<NotificationClient> notificationClient = new NotificationClient(this,
1259 client,
1260 pid);
1261 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262
Eric Laurent021cf962014-05-13 10:18:14 -07001263 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001264
Marco Nelissen06b46062014-11-14 07:58:25 -08001265 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001266 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001267 }
1268 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269
Eric Laurent021cf962014-05-13 10:18:14 -07001270 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001271 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001272 // the config change is always sent from playback or record threads to avoid deadlock
1273 // with AudioSystem::gLock
1274 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1275 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1276 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001278 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1279 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001280 }
1281}
1282
1283void AudioFlinger::removeNotificationClient(pid_t pid)
1284{
1285 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001286 {
1287 Mutex::Autolock _cl(mClientLock);
1288 mNotificationClients.removeItem(pid);
1289 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001290
Steve Block3856b092011-10-20 11:56:00 +01001291 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001292 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001293 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001294 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001295 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001296 ALOGV(" pid %d @ %d", ref->mPid, i);
1297 if (ref->mPid == pid) {
1298 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001299 mAudioSessionRefs.removeAt(i);
1300 delete ref;
1301 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001302 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001303 } else {
1304 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001305 }
1306 }
1307 if (removed) {
1308 purgeStaleEffects_l();
1309 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310}
1311
Eric Laurent73e26b62015-04-27 16:55:58 -07001312void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001313 const sp<AudioIoDescriptor>& ioDesc,
1314 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001315{
Eric Laurent021cf962014-05-13 10:18:14 -07001316 Mutex::Autolock _l(mClientLock);
1317 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001318 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001319 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1320 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1321 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001322 }
1323}
1324
Eric Laurent021cf962014-05-13 10:18:14 -07001325// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001326void AudioFlinger::removeClient_l(pid_t pid)
1327{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001328 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001329 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001330 mClients.removeItem(pid);
1331}
1332
Eric Laurent717e1282012-06-29 16:36:52 -07001333// getEffectThread_l() must be called with AudioFlinger::mLock held
1334sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1335{
1336 sp<PlaybackThread> thread;
1337
1338 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1339 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1340 ALOG_ASSERT(thread == 0);
1341 thread = mPlaybackThreads.valueAt(i);
1342 }
1343 }
1344
1345 return thread;
1346}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001347
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349
1350// ----------------------------------------------------------------------------
1351
1352AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1353 : RefBase(),
1354 mAudioFlinger(audioFlinger),
John Grossman4ff14ba2012-02-08 16:37:41 -08001355 mPid(pid),
1356 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001357{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301358 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1359 heapSize *= 1024;
1360 if (!heapSize) {
1361 heapSize = kClientSharedHeapSizeBytes;
1362 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1363 // invalidated tracks
1364 if (!audioFlinger->isLowRamDevice()) {
1365 heapSize *= kClientSharedHeapSizeMultiplier;
1366 }
Eric Laurentaba407f2015-08-20 16:18:53 -07001367 }
1368 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001369}
1370
Eric Laurent021cf962014-05-13 10:18:14 -07001371// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372AudioFlinger::Client::~Client()
1373{
1374 mAudioFlinger->removeClient_l(mPid);
1375}
1376
Glenn Kasten435dbe62012-01-30 10:15:48 -08001377sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378{
1379 return mMemoryDealer;
1380}
1381
John Grossman4ff14ba2012-02-08 16:37:41 -08001382// Reserve one of the limited slots for a timed audio track associated
1383// with this client
1384bool AudioFlinger::Client::reserveTimedTrack()
1385{
1386 const int kMaxTimedTracksPerClient = 4;
1387
1388 Mutex::Autolock _l(mTimedTrackLock);
1389
1390 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1391 ALOGW("can not create timed track - pid %d has exceeded the limit",
1392 mPid);
1393 return false;
1394 }
1395
1396 mTimedTrackCount++;
1397 return true;
1398}
1399
1400// Release a slot for a timed audio track
1401void AudioFlinger::Client::releaseTimedTrack()
1402{
1403 Mutex::Autolock _l(mTimedTrackLock);
1404 mTimedTrackCount--;
1405}
1406
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407// ----------------------------------------------------------------------------
1408
1409AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1410 const sp<IAudioFlingerClient>& client,
1411 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001412 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413{
1414}
1415
1416AudioFlinger::NotificationClient::~NotificationClient()
1417{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001418}
1419
Glenn Kasten0f11b512014-01-31 16:18:54 -08001420void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001421{
1422 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001423 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424}
1425
Mathias Agopian65ab4712010-07-14 17:59:35 -07001426
1427// ----------------------------------------------------------------------------
1428
Jeff Brown893a5642013-08-16 20:19:26 -07001429static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1430 return audio_is_remote_submix_device(inDevice);
1431}
1432
Mathias Agopian65ab4712010-07-14 17:59:35 -07001433sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001434 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001436 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001437 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001438 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001439 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001440 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001441 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001442 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001443 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001444 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001445 sp<IMemory>& cblk,
1446 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447 status_t *status)
1448{
1449 sp<RecordThread::RecordTrack> recordTrack;
1450 sp<RecordHandle> recordHandle;
1451 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001452 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453 int lSessionId;
1454
Glenn Kastend776ac62014-05-07 09:16:09 -07001455 cblk.clear();
1456 buffers.clear();
1457
Mathias Agopian65ab4712010-07-14 17:59:35 -07001458 // check calling permissions
Svet Ganovbe71aa22015-04-28 12:06:02 -07001459 if (!recordingAllowed(opPackageName)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001460 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001461 lStatus = PERMISSION_DENIED;
1462 goto Exit;
1463 }
1464
Glenn Kasten53b5d092014-02-05 10:00:23 -08001465 // further sample rate checks are performed by createRecordTrack_l()
1466 if (sampleRate == 0) {
1467 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1468 lStatus = BAD_VALUE;
1469 goto Exit;
1470 }
1471
Andy Hung6770c6f2015-04-07 13:43:36 -07001472 // we don't yet support anything other than linear PCM
1473 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001474 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001475 lStatus = BAD_VALUE;
1476 goto Exit;
1477 }
1478
Glenn Kasten53b5d092014-02-05 10:00:23 -08001479 // further channel mask checks are performed by createRecordTrack_l()
1480 if (!audio_is_input_channel(channelMask)) {
1481 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1482 lStatus = BAD_VALUE;
1483 goto Exit;
1484 }
1485
Glenn Kasten05997e22014-03-13 15:08:33 -07001486 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001488 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001489 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001490 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001491 lStatus = BAD_VALUE;
1492 goto Exit;
1493 }
1494
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001495 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001496 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001497
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001498 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001499 lSessionId = *sessionId;
1500 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001501 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001502 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001503 if (sessionId != NULL) {
1504 *sessionId = lSessionId;
1505 }
1506 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001507 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001508
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001509 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001510 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001511 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001512 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001513 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001514
1515 if (lStatus == NO_ERROR) {
1516 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1517 // session and move it to this thread.
1518 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
1519 if (chain != 0) {
1520 Mutex::Autolock _l(thread->mLock);
1521 thread->addEffectChain_l(chain);
1522 }
1523 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001524 }
Glenn Kastene198c362013-08-13 09:13:36 -07001525
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001526 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001527 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001528 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001529 // Don't hold mClientLock when releasing the reference on the track as the
1530 // destructor will acquire it.
1531 {
1532 Mutex::Autolock _cl(mClientLock);
1533 client.clear();
1534 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001535 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001536 goto Exit;
1537 }
1538
Glenn Kastend776ac62014-05-07 09:16:09 -07001539 cblk = recordTrack->getCblk();
1540 buffers = recordTrack->getBuffers();
1541
Glenn Kasten2fc14732013-08-05 14:58:14 -07001542 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001544
1545Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001546 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001547 return recordHandle;
1548}
1549
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001550
1551
Mathias Agopian65ab4712010-07-14 17:59:35 -07001552// ----------------------------------------------------------------------------
1553
Eric Laurenta4c5a552012-03-29 10:12:40 -07001554audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1555{
Eric Laurent44622db2014-08-01 19:00:33 -07001556 if (name == NULL) {
1557 return 0;
1558 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001559 if (!settingsAllowed()) {
1560 return 0;
1561 }
1562 Mutex::Autolock _l(mLock);
1563 return loadHwModule_l(name);
1564}
1565
1566// loadHwModule_l() must be called with AudioFlinger::mLock held
1567audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1568{
1569 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1570 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1571 ALOGW("loadHwModule() module %s already loaded", name);
1572 return mAudioHwDevs.keyAt(i);
1573 }
1574 }
1575
Eric Laurenta4c5a552012-03-29 10:12:40 -07001576 audio_hw_device_t *dev;
1577
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001578 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001579 if (rc) {
1580 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1581 return 0;
1582 }
1583
1584 mHardwareStatus = AUDIO_HW_INIT;
1585 rc = dev->init_check(dev);
1586 mHardwareStatus = AUDIO_HW_IDLE;
1587 if (rc) {
1588 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1589 return 0;
1590 }
1591
John Grossmanee578c02012-07-23 17:05:46 -07001592 // Check and cache this HAL's level of support for master mute and master
1593 // volume. If this is the first HAL opened, and it supports the get
1594 // methods, use the initial values provided by the HAL as the current
1595 // master mute and volume settings.
1596
1597 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1598 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001599 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001600
1601 if (0 == mAudioHwDevs.size()) {
1602 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1603 if (NULL != dev->get_master_volume) {
1604 float mv;
1605 if (OK == dev->get_master_volume(dev, &mv)) {
1606 mMasterVolume = mv;
1607 }
1608 }
1609
1610 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1611 if (NULL != dev->get_master_mute) {
1612 bool mm;
1613 if (OK == dev->get_master_mute(dev, &mm)) {
1614 mMasterMute = mm;
1615 }
1616 }
1617 }
1618
Eric Laurenta4c5a552012-03-29 10:12:40 -07001619 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001620 if ((NULL != dev->set_master_volume) &&
1621 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1622 flags = static_cast<AudioHwDevice::Flags>(flags |
1623 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1624 }
1625
1626 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1627 if ((NULL != dev->set_master_mute) &&
1628 (OK == dev->set_master_mute(dev, mMasterMute))) {
1629 flags = static_cast<AudioHwDevice::Flags>(flags |
1630 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1631 }
1632
Eric Laurenta4c5a552012-03-29 10:12:40 -07001633 mHardwareStatus = AUDIO_HW_IDLE;
1634 }
1635
1636 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001637 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001638
1639 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001640 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001641
1642 return handle;
1643
1644}
1645
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001646// ----------------------------------------------------------------------------
1647
Glenn Kasten3b16c762012-11-14 08:44:39 -08001648uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001649{
1650 Mutex::Autolock _l(mLock);
1651 PlaybackThread *thread = primaryPlaybackThread_l();
1652 return thread != NULL ? thread->sampleRate() : 0;
1653}
1654
Glenn Kastene33054e2012-11-14 12:54:39 -08001655size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001656{
1657 Mutex::Autolock _l(mLock);
1658 PlaybackThread *thread = primaryPlaybackThread_l();
1659 return thread != NULL ? thread->frameCountHAL() : 0;
1660}
1661
1662// ----------------------------------------------------------------------------
1663
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001664status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1665{
1666 uid_t uid = IPCThreadState::self()->getCallingUid();
1667 if (uid != AID_SYSTEM) {
1668 return PERMISSION_DENIED;
1669 }
1670 Mutex::Autolock _l(mLock);
1671 if (mIsDeviceTypeKnown) {
1672 return INVALID_OPERATION;
1673 }
1674 mIsLowRamDevice = isLowRamDevice;
1675 mIsDeviceTypeKnown = true;
1676 return NO_ERROR;
1677}
1678
Eric Laurent93c3d412014-08-01 14:48:35 -07001679audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1680{
1681 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001682
1683 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1684 if (index >= 0) {
1685 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1686 mHwAvSyncIds.valueAt(index), sessionId);
1687 return mHwAvSyncIds.valueAt(index);
1688 }
1689
1690 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1691 if (dev == NULL) {
1692 return AUDIO_HW_SYNC_INVALID;
1693 }
1694 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1695 AudioParameter param = AudioParameter(String8(reply));
1696 free(reply);
1697
1698 int value;
1699 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1700 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1701 return AUDIO_HW_SYNC_INVALID;
1702 }
1703
1704 // allow only one session for a given HW A/V sync ID.
1705 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1706 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1707 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1708 value, mHwAvSyncIds.keyAt(i));
1709 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001710 break;
1711 }
1712 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001713
1714 mHwAvSyncIds.add(sessionId, value);
1715
1716 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1717 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1718 uint32_t sessions = thread->hasAudioSession(sessionId);
1719 if (sessions & PlaybackThread::TRACK_SESSION) {
1720 AudioParameter param = AudioParameter();
1721 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1722 thread->setParameters(param.toString());
1723 break;
1724 }
1725 }
1726
1727 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1728 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001729}
1730
Eric Laurent72e3f392015-05-20 14:43:50 -07001731status_t AudioFlinger::systemReady()
1732{
1733 Mutex::Autolock _l(mLock);
1734 ALOGI("%s", __FUNCTION__);
1735 if (mSystemReady) {
1736 ALOGW("%s called twice", __FUNCTION__);
1737 return NO_ERROR;
1738 }
1739 mSystemReady = true;
1740 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1741 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1742 thread->systemReady();
1743 }
1744 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1745 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1746 thread->systemReady();
1747 }
1748 return NO_ERROR;
1749}
1750
Eric Laurentfa90e842014-10-17 18:12:31 -07001751// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1752void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1753{
1754 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1755 if (index >= 0) {
1756 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1757 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1758 AudioParameter param = AudioParameter();
1759 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1760 thread->setParameters(param.toString());
1761 }
1762}
1763
1764
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001765// ----------------------------------------------------------------------------
1766
Eric Laurent83b88082014-06-20 18:31:16 -07001767
1768sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001769 audio_io_handle_t *output,
1770 audio_config_t *config,
1771 audio_devices_t devices,
1772 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001773 audio_output_flags_t flags)
1774{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001775 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001776 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001777 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001778 }
1779
1780 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001781 if (*output == AUDIO_IO_HANDLE_NONE) {
1782 *output = nextUniqueId();
1783 }
Eric Laurent83b88082014-06-20 18:31:16 -07001784
1785 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1786
Eric Laurent83b88082014-06-20 18:31:16 -07001787 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001788 // This if statement allows overriding the audio policy settings
1789 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1790 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1791 // Check only for Normal Mixing mode
1792 if (kEnableExtendedPrecision) {
1793 // Specify format (uncomment one below to choose)
1794 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1795 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1796 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1797 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001798 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001799 }
1800 if (kEnableExtendedChannels) {
1801 // Specify channel mask (uncomment one below to choose)
1802 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1803 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1804 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1805 }
Eric Laurent83b88082014-06-20 18:31:16 -07001806 }
1807
Phil Burk062e67a2015-02-11 13:40:50 -08001808 AudioStreamOut *outputStream = NULL;
1809 status_t status = outHwDev->openOutputStream(
1810 &outputStream,
1811 *output,
1812 devices,
1813 flags,
1814 config,
1815 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001816
1817 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001818
Phil Burk062e67a2015-02-11 13:40:50 -08001819 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001820
1821 PlaybackThread *thread;
1822 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001823 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001824 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001825 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1826 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001827 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001828 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001829 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001830 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001831 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001832 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001833 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001834 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001835 return thread;
1836 }
1837
1838 return 0;
1839}
1840
Eric Laurentcf2c0212014-07-25 16:20:43 -07001841status_t AudioFlinger::openOutput(audio_module_handle_t module,
1842 audio_io_handle_t *output,
1843 audio_config_t *config,
1844 audio_devices_t *devices,
1845 const String8& address,
1846 uint32_t *latencyMs,
1847 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001848{
Phil Burk062e67a2015-02-11 13:40:50 -08001849 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001850 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001851 (devices != NULL) ? *devices : 0,
1852 config->sample_rate,
1853 config->format,
1854 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001855 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001856
Eric Laurentcf2c0212014-07-25 16:20:43 -07001857 if (*devices == AUDIO_DEVICE_NONE) {
1858 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001859 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001860
Mathias Agopian65ab4712010-07-14 17:59:35 -07001861 Mutex::Autolock _l(mLock);
1862
Eric Laurentcf2c0212014-07-25 16:20:43 -07001863 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001864 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001865 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001866
1867 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001868 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001869
1870 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001871 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001872 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001873 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001874
1875 AutoMutex lock(mHardwareLock);
1876 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001877 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001878 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001879 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001880 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001881 }
1882
Eric Laurentcf2c0212014-07-25 16:20:43 -07001883 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001884}
1885
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001886audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1887 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001888{
1889 Mutex::Autolock _l(mLock);
1890 MixerThread *thread1 = checkMixerThread_l(output1);
1891 MixerThread *thread2 = checkMixerThread_l(output2);
1892
1893 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001894 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1895 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001896 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001897 }
1898
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001899 audio_io_handle_t id = nextUniqueId();
Eric Laurent72e3f392015-05-20 14:43:50 -07001900 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901 thread->addOutputTrack(thread2);
1902 mPlaybackThreads.add(id, thread);
1903 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001904 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001905 return id;
1906}
1907
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001908status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001909{
Glenn Kastend96c5722012-04-25 13:44:49 -07001910 return closeOutput_nonvirtual(output);
1911}
1912
1913status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1914{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001915 // keep strong reference on the playback thread so that
1916 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001917 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001918 {
1919 Mutex::Autolock _l(mLock);
1920 thread = checkPlaybackThread_l(output);
1921 if (thread == NULL) {
1922 return BAD_VALUE;
1923 }
1924
Steve Block3856b092011-10-20 11:56:00 +01001925 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001926
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001927 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001929 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001930 DuplicatingThread *dupThread =
1931 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001932 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001933 }
1934 }
1935 }
1936
1937
1938 mPlaybackThreads.removeItem(output);
1939 // save all effects to the default thread
1940 if (mPlaybackThreads.size()) {
1941 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1942 if (dstThread != NULL) {
1943 // audioflinger lock is held here so the acquisition order of thread locks does not
1944 // matter
1945 Mutex::Autolock _dl(dstThread->mLock);
1946 Mutex::Autolock _sl(thread->mLock);
1947 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1948 for (size_t i = 0; i < effectChains.size(); i ++) {
1949 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950 }
1951 }
1952 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001953 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1954 ioDesc->mIoHandle = output;
1955 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001956 }
1957 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001958 // The thread entity (active unit of execution) is no longer running here,
1959 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001960
Eric Laurentf6870ae2015-05-08 10:50:03 -07001961 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001962 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001963 }
Eric Laurent83b88082014-06-20 18:31:16 -07001964
Mathias Agopian65ab4712010-07-14 17:59:35 -07001965 return NO_ERROR;
1966}
1967
Eric Laurent83b88082014-06-20 18:31:16 -07001968void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1969{
1970 AudioStreamOut *out = thread->clearOutput();
1971 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1972 // from now on thread->mOutput is NULL
1973 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1974 delete out;
1975}
1976
1977void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1978{
1979 mPlaybackThreads.removeItem(thread->mId);
1980 thread->exit();
1981 closeOutputFinish(thread);
1982}
1983
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001984status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985{
1986 Mutex::Autolock _l(mLock);
1987 PlaybackThread *thread = checkPlaybackThread_l(output);
1988
1989 if (thread == NULL) {
1990 return BAD_VALUE;
1991 }
1992
Steve Block3856b092011-10-20 11:56:00 +01001993 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001994 thread->suspend();
1995
1996 return NO_ERROR;
1997}
1998
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001999status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002000{
2001 Mutex::Autolock _l(mLock);
2002 PlaybackThread *thread = checkPlaybackThread_l(output);
2003
2004 if (thread == NULL) {
2005 return BAD_VALUE;
2006 }
2007
Steve Block3856b092011-10-20 11:56:00 +01002008 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002009
2010 thread->restore();
2011
2012 return NO_ERROR;
2013}
2014
Eric Laurentcf2c0212014-07-25 16:20:43 -07002015status_t AudioFlinger::openInput(audio_module_handle_t module,
2016 audio_io_handle_t *input,
2017 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002018 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002019 const String8& address,
2020 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002021 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002022{
Eric Laurent83b88082014-06-20 18:31:16 -07002023 Mutex::Autolock _l(mLock);
2024
Glenn Kastene7d66712015-03-05 13:46:52 -08002025 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002026 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002027 }
2028
Glenn Kastene7d66712015-03-05 13:46:52 -08002029 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002030
2031 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002032 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002033 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002034 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002035 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002036 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002037}
Dima Zavin799a70e2011-04-18 16:57:27 -07002038
Eric Laurent83b88082014-06-20 18:31:16 -07002039sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002040 audio_io_handle_t *input,
2041 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002042 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002043 const String8& address,
2044 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002045 audio_input_flags_t flags)
2046{
Glenn Kastene7d66712015-03-05 13:46:52 -08002047 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002048 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002049 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002050 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002051 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002052
Eric Laurentcf2c0212014-07-25 16:20:43 -07002053 if (*input == AUDIO_IO_HANDLE_NONE) {
2054 *input = nextUniqueId();
2055 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002056
Eric Laurentcf2c0212014-07-25 16:20:43 -07002057 audio_config_t halconfig = *config;
2058 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002059 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002060 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002061 &inStream, flags, address.string(), source);
2062 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002063 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002064 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002065 halconfig.sample_rate,
2066 halconfig.format,
2067 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002068 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002069 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002070
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002071 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002072 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002073 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002074 audio_is_linear_pcm(config->format) &&
2075 audio_is_linear_pcm(halconfig.format) &&
2076 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002077 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2078 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002079 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002080 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002081 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002082 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002083 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002084 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002085 }
2086
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002087 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002088
Glenn Kasten46909e72013-02-26 09:20:22 -08002089#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002090 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2091 // or (re-)create if current Pipe is idle and does not match the new format
2092 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002093 enum {
2094 TEE_SINK_NO, // don't copy input
2095 TEE_SINK_NEW, // copy input using a new pipe
2096 TEE_SINK_OLD, // copy input using an existing pipe
2097 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002098 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2099 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002100 if (!mTeeSinkInputEnabled) {
2101 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002102 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002103 kind = TEE_SINK_NO;
2104 } else if (mRecordTeeSink == 0) {
2105 kind = TEE_SINK_NEW;
2106 } else if (mRecordTeeSink->getStrongCount() != 1) {
2107 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002108 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002109 kind = TEE_SINK_OLD;
2110 } else {
2111 kind = TEE_SINK_NEW;
2112 }
2113 switch (kind) {
2114 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002115 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002116 size_t numCounterOffers = 0;
2117 const NBAIO_Format offers[1] = {format};
2118 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2119 ALOG_ASSERT(index == 0);
2120 PipeReader *pipeReader = new PipeReader(*pipe);
2121 numCounterOffers = 0;
2122 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2123 ALOG_ASSERT(index == 0);
2124 mRecordTeeSink = pipe;
2125 mRecordTeeSource = pipeReader;
2126 teeSink = pipe;
2127 }
2128 break;
2129 case TEE_SINK_OLD:
2130 teeSink = mRecordTeeSink;
2131 break;
2132 case TEE_SINK_NO:
2133 default:
2134 break;
2135 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002136#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002137
Eric Laurentcf2c0212014-07-25 16:20:43 -07002138 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002139
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002140 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002141 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002142 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002143 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002144 inputStream,
2145 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002146 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002147 devices,
2148 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002149#ifdef TEE_SINK
2150 , teeSink
2151#endif
2152 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002153 mRecordThreads.add(*input, thread);
2154 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002155 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002156 }
2157
Eric Laurentcf2c0212014-07-25 16:20:43 -07002158 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002159 return 0;
2160}
2161
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002162status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002163{
Glenn Kastend96c5722012-04-25 13:44:49 -07002164 return closeInput_nonvirtual(input);
2165}
2166
2167status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2168{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002169 // keep strong reference on the record thread so that
2170 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002171 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002172 {
2173 Mutex::Autolock _l(mLock);
2174 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002175 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002176 return BAD_VALUE;
2177 }
2178
Steve Block3856b092011-10-20 11:56:00 +01002179 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002180
2181 // If we still have effect chains, it means that a client still holds a handle
2182 // on at least one effect. We must either move the chain to an existing thread with the
2183 // same session ID or put it aside in case a new record thread is opened for a
2184 // new capture on the same session
2185 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002186 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002187 Mutex::Autolock _sl(thread->mLock);
2188 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002189 // Note: maximum one chain per record thread
2190 if (effectChains.size() != 0) {
2191 chain = effectChains[0];
2192 }
2193 }
2194 if (chain != 0) {
2195 // first check if a record thread is already opened with a client on the same session.
2196 // This should only happen in case of overlap between one thread tear down and the
2197 // creation of its replacement
2198 size_t i;
2199 for (i = 0; i < mRecordThreads.size(); i++) {
2200 sp<RecordThread> t = mRecordThreads.valueAt(i);
2201 if (t == thread) {
2202 continue;
2203 }
2204 if (t->hasAudioSession(chain->sessionId()) != 0) {
2205 Mutex::Autolock _l(t->mLock);
2206 ALOGV("closeInput() found thread %d for effect session %d",
2207 t->id(), chain->sessionId());
2208 t->addEffectChain_l(chain);
2209 break;
2210 }
2211 }
2212 // put the chain aside if we could not find a record thread with the same session id.
2213 if (i == mRecordThreads.size()) {
2214 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002215 }
2216 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002217 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2218 ioDesc->mIoHandle = input;
2219 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002220 mRecordThreads.removeItem(input);
2221 }
Eric Laurent83b88082014-06-20 18:31:16 -07002222 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2223 // we have a different lock for notification client
2224 closeInputFinish(thread);
2225 return NO_ERROR;
2226}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002227
Eric Laurent83b88082014-06-20 18:31:16 -07002228void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2229{
2230 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002231 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002232 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002233 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002234 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002235 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002236}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237
Eric Laurent83b88082014-06-20 18:31:16 -07002238void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2239{
2240 mRecordThreads.removeItem(thread->mId);
2241 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002242}
2243
Glenn Kastend2304db2014-02-03 07:40:31 -08002244status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002245{
2246 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002247 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002248
2249 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2250 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002251 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002252 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002253
2254 return NO_ERROR;
2255}
2256
2257
Eric Laurentde3f8392014-07-27 18:38:22 -07002258audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002259{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002260 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002261}
2262
Marco Nelissend457c972014-02-11 08:47:07 -08002263void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002264{
2265 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002266 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002267 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2268 if (pid != -1 && (caller == getpid_cached)) {
2269 caller = pid;
2270 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002271
Eric Laurent021cf962014-05-13 10:18:14 -07002272 {
2273 Mutex::Autolock _cl(mClientLock);
2274 // Ignore requests received from processes not known as notification client. The request
2275 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2276 // called from a different pid leaving a stale session reference. Also we don't know how
2277 // to clear this reference if the client process dies.
2278 if (mNotificationClients.indexOfKey(caller) < 0) {
2279 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2280 return;
2281 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002282 }
2283
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002284 size_t num = mAudioSessionRefs.size();
2285 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002286 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002287 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2288 ref->mCnt++;
2289 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002290 return;
2291 }
2292 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002293 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2294 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002295}
2296
Marco Nelissend457c972014-02-11 08:47:07 -08002297void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002298{
2299 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002300 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002301 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2302 if (pid != -1 && (caller == getpid_cached)) {
2303 caller = pid;
2304 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002305 size_t num = mAudioSessionRefs.size();
2306 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002307 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002308 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2309 ref->mCnt--;
2310 ALOGV(" decremented refcount to %d", ref->mCnt);
2311 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002312 mAudioSessionRefs.removeAt(i);
2313 delete ref;
2314 purgeStaleEffects_l();
2315 }
2316 return;
2317 }
2318 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002319 // If the caller is mediaserver it is likely that the session being released was acquired
2320 // on behalf of a process not in notification clients and we ignore the warning.
2321 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002322}
2323
2324void AudioFlinger::purgeStaleEffects_l() {
2325
Steve Block3856b092011-10-20 11:56:00 +01002326 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002327
2328 Vector< sp<EffectChain> > chains;
2329
2330 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2331 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2332 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2333 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002334 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2335 chains.push(ec);
2336 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002337 }
2338 }
2339 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2340 sp<RecordThread> t = mRecordThreads.valueAt(i);
2341 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2342 sp<EffectChain> ec = t->mEffectChains[j];
2343 chains.push(ec);
2344 }
2345 }
2346
2347 for (size_t i = 0; i < chains.size(); i++) {
2348 sp<EffectChain> ec = chains[i];
2349 int sessionid = ec->sessionId();
2350 sp<ThreadBase> t = ec->mThread.promote();
2351 if (t == 0) {
2352 continue;
2353 }
2354 size_t numsessionrefs = mAudioSessionRefs.size();
2355 bool found = false;
2356 for (size_t k = 0; k < numsessionrefs; k++) {
2357 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002358 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002359 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002360 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002361 found = true;
2362 break;
2363 }
2364 }
2365 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002366 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002367 // remove all effects from the chain
2368 while (ec->mEffects.size()) {
2369 sp<EffectModule> effect = ec->mEffects[0];
2370 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002371 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002372 if (effect->purgeHandles()) {
2373 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002374 }
2375 AudioSystem::unregisterEffect(effect->id());
2376 }
2377 }
2378 }
2379 return;
2380}
2381
Mathias Agopian65ab4712010-07-14 17:59:35 -07002382// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002383AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002384{
Glenn Kastena1117922012-01-26 10:53:32 -08002385 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002386}
2387
2388// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002389AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002390{
2391 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002392 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002393}
2394
2395// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002396AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002397{
Glenn Kastena1117922012-01-26 10:53:32 -08002398 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002399}
2400
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002401uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002402{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002403 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002404}
2405
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002406AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002407{
2408 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2409 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002410 if(thread->isDuplicating()) {
2411 continue;
2412 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002413 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002414 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002415 return thread;
2416 }
2417 }
2418 return NULL;
2419}
2420
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002421audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002422{
2423 PlaybackThread *thread = primaryPlaybackThread_l();
2424
2425 if (thread == NULL) {
2426 return 0;
2427 }
2428
Eric Laurentf1c04f92012-08-28 14:26:53 -07002429 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002430}
2431
Eric Laurenta011e352012-03-29 15:51:43 -07002432sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2433 int triggerSession,
2434 int listenerSession,
2435 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002436 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002437{
2438 Mutex::Autolock _l(mLock);
2439
2440 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2441 status_t playStatus = NAME_NOT_FOUND;
2442 status_t recStatus = NAME_NOT_FOUND;
2443 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2444 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2445 if (playStatus == NO_ERROR) {
2446 return event;
2447 }
2448 }
2449 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2450 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2451 if (recStatus == NO_ERROR) {
2452 return event;
2453 }
2454 }
2455 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2456 mPendingSyncEvents.add(event);
2457 } else {
2458 ALOGV("createSyncEvent() invalid event %d", event->type());
2459 event.clear();
2460 }
2461 return event;
2462}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002463
Mathias Agopian65ab4712010-07-14 17:59:35 -07002464// ----------------------------------------------------------------------------
2465// Effect management
2466// ----------------------------------------------------------------------------
2467
2468
Glenn Kastenf587ba52012-01-26 16:25:10 -08002469status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002470{
2471 Mutex::Autolock _l(mLock);
2472 return EffectQueryNumberEffects(numEffects);
2473}
2474
Glenn Kastenf587ba52012-01-26 16:25:10 -08002475status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002476{
2477 Mutex::Autolock _l(mLock);
2478 return EffectQueryEffect(index, descriptor);
2479}
2480
Glenn Kasten5e92a782012-01-30 07:40:52 -08002481status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002482 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002483{
2484 Mutex::Autolock _l(mLock);
2485 return EffectGetDescriptor(pUuid, descriptor);
2486}
2487
2488
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002489sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002490 effect_descriptor_t *pDesc,
2491 const sp<IEffectClient>& effectClient,
2492 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002493 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002494 int sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002495 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002496 status_t *status,
2497 int *id,
2498 int *enabled)
2499{
2500 status_t lStatus = NO_ERROR;
2501 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002502 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002503
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002504 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002505 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002506 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002507
2508 if (pDesc == NULL) {
2509 lStatus = BAD_VALUE;
2510 goto Exit;
2511 }
2512
Eric Laurent84e9a102010-09-23 16:10:16 -07002513 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002514 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002515 lStatus = PERMISSION_DENIED;
2516 goto Exit;
2517 }
2518
Dima Zavinfce7a472011-04-19 22:30:36 -07002519 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002520 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002521 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002522 lStatus = PERMISSION_DENIED;
2523 goto Exit;
2524 }
2525
Mathias Agopian65ab4712010-07-14 17:59:35 -07002526 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002527 if (!EffectIsNullUuid(&pDesc->uuid)) {
2528 // if uuid is specified, request effect descriptor
2529 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2530 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002531 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002532 goto Exit;
2533 }
2534 } else {
2535 // if uuid is not specified, look for an available implementation
2536 // of the required type in effect factory
2537 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002538 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002539 lStatus = BAD_VALUE;
2540 goto Exit;
2541 }
2542 uint32_t numEffects = 0;
2543 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002544 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002545 bool found = false;
2546
2547 lStatus = EffectQueryNumberEffects(&numEffects);
2548 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002549 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002550 goto Exit;
2551 }
2552 for (uint32_t i = 0; i < numEffects; i++) {
2553 lStatus = EffectQueryEffect(i, &desc);
2554 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002555 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002556 continue;
2557 }
2558 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2559 // If matching type found save effect descriptor. If the session is
2560 // 0 and the effect is not auxiliary, continue enumeration in case
2561 // an auxiliary version of this effect type is available
2562 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002563 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002564 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002565 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2566 break;
2567 }
2568 }
2569 }
2570 if (!found) {
2571 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002572 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002573 goto Exit;
2574 }
2575 // For same effect type, chose auxiliary version over insert version if
2576 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002577 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002578 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002579 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002580 }
2581 }
2582
2583 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002584 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002585 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2586 lStatus = INVALID_OPERATION;
2587 goto Exit;
2588 }
2589
Eric Laurent59255e42011-07-27 19:49:51 -07002590 // check recording permission for visualizer
2591 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Svet Ganovbe71aa22015-04-28 12:06:02 -07002592 !recordingAllowed(opPackageName)) {
Eric Laurent59255e42011-07-27 19:49:51 -07002593 lStatus = PERMISSION_DENIED;
2594 goto Exit;
2595 }
2596
Mathias Agopian65ab4712010-07-14 17:59:35 -07002597 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002598 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002599 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002600 // if the output returned by getOutputForEffect() is removed before we lock the
2601 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2602 // and we will exit safely
2603 io = AudioSystem::getOutputForEffect(&desc);
2604 ALOGV("createEffect got output %d", io);
2605 }
2606
2607 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002608
2609 // If output is not specified try to find a matching audio session ID in one of the
2610 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002611 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2612 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002613 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002614 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002615 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2616 // output must be specified by AudioPolicyManager when using session
2617 // AUDIO_SESSION_OUTPUT_STAGE
2618 lStatus = BAD_VALUE;
2619 goto Exit;
2620 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002621 // look for the thread where the specified audio session is present
2622 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2623 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2624 io = mPlaybackThreads.keyAt(i);
2625 break;
2626 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002627 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002628 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002629 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2630 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2631 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002632 break;
2633 }
2634 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002635 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002636 // If no output thread contains the requested session ID, default to
2637 // first output. The effect chain will be moved to the correct output
2638 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002639 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002640 io = mPlaybackThreads.keyAt(0);
2641 }
Steve Block3856b092011-10-20 11:56:00 +01002642 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002643 }
2644 ThreadBase *thread = checkRecordThread_l(io);
2645 if (thread == NULL) {
2646 thread = checkPlaybackThread_l(io);
2647 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002648 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002649 lStatus = BAD_VALUE;
2650 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002651 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002652 } else {
2653 // Check if one effect chain was awaiting for an effect to be created on this
2654 // session and used it instead of creating a new one.
2655 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2656 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002657 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002658 thread->addEffectChain_l(chain);
2659 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002660 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002661
Eric Laurent021cf962014-05-13 10:18:14 -07002662 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002663
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002664 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002665 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2666 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002667 if (handle != 0 && id != NULL) {
2668 *id = handle->id();
2669 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002670 if (handle == 0) {
2671 // remove local strong reference to Client with mClientLock held
2672 Mutex::Autolock _cl(mClientLock);
2673 client.clear();
2674 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002675 }
2676
2677Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002678 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002679 return handle;
2680}
2681
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002682status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2683 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002684{
Steve Block3856b092011-10-20 11:56:00 +01002685 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002686 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002687 Mutex::Autolock _l(mLock);
2688 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002689 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002690 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002691 }
Eric Laurentde070132010-07-13 04:45:46 -07002692 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2693 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002694 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002695 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002696 }
Eric Laurentde070132010-07-13 04:45:46 -07002697 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2698 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002699 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002700 return BAD_VALUE;
2701 }
2702
2703 Mutex::Autolock _dl(dstThread->mLock);
2704 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002705 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002706}
2707
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002708// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002709status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002710 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002711 AudioFlinger::PlaybackThread *dstThread,
2712 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002713{
Steve Block3856b092011-10-20 11:56:00 +01002714 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002715 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002716
Eric Laurent59255e42011-07-27 19:49:51 -07002717 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002718 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002719 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002720 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002721 return INVALID_OPERATION;
2722 }
2723
Andy Hung9a592762014-07-21 21:56:01 -07002724 // Check whether the destination thread has a channel count of FCC_2, which is
2725 // currently required for (most) effects. Prevent moving the effect chain here rather
2726 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentf6870ae2015-05-08 10:50:03 -07002727 if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
Eric Laurentb10352f2014-11-03 16:25:39 -08002728 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002729 ALOGW("moveEffectChain_l() effect chain failed because"
2730 " destination thread %p channel count(%u) != %u",
2731 dstThread, dstThread->mChannelCount, FCC_2);
2732 return INVALID_OPERATION;
2733 }
2734
Eric Laurent39e94f82010-07-28 01:32:47 -07002735 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002736 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002737 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002738 // removed.
2739 srcThread->removeEffectChain_l(chain);
2740
2741 // transfer all effects one by one so that new effect chain is created on new thread with
2742 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002743 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002744 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002745 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002746 Vector< sp<EffectModule> > removed;
2747 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002748 while (effect != 0) {
2749 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002750 removed.add(effect);
2751 status = dstThread->addEffect_l(effect);
2752 if (status != NO_ERROR) {
2753 break;
2754 }
Eric Laurentec35a142011-10-05 17:42:25 -07002755 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2756 if (effect->state() == EffectModule::ACTIVE ||
2757 effect->state() == EffectModule::STOPPING) {
2758 effect->start();
2759 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002760 // if the move request is not received from audio policy manager, the effect must be
2761 // re-registered with the new strategy and output
2762 if (dstChain == 0) {
2763 dstChain = effect->chain().promote();
2764 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002765 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002766 status = NO_INIT;
2767 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002768 }
2769 strategy = dstChain->strategy();
2770 }
2771 if (reRegister) {
2772 AudioSystem::unregisterEffect(effect->id());
2773 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002774 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002775 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002776 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002777 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002778 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002779 }
Eric Laurentde070132010-07-13 04:45:46 -07002780 effect = chain->getEffectFromId_l(0);
2781 }
2782
Eric Laurent5baf2af2013-09-12 17:37:00 -07002783 if (status != NO_ERROR) {
2784 for (size_t i = 0; i < removed.size(); i++) {
2785 srcThread->addEffect_l(removed[i]);
2786 if (dstChain != 0 && reRegister) {
2787 AudioSystem::unregisterEffect(removed[i]->id());
2788 AudioSystem::registerEffect(&removed[i]->desc(),
2789 srcThread->id(),
2790 strategy,
2791 sessionId,
2792 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002793 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002794 }
2795 }
2796 }
2797
2798 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002799}
2800
Eric Laurent5baf2af2013-09-12 17:37:00 -07002801bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002802{
2803 if (mGlobalEffectEnableTime != 0 &&
2804 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2805 return true;
2806 }
2807
2808 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2809 sp<EffectChain> ec =
2810 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002811 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002812 return true;
2813 }
2814 }
2815 return false;
2816}
2817
Eric Laurent5baf2af2013-09-12 17:37:00 -07002818void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002819{
2820 Mutex::Autolock _l(mLock);
2821
2822 mGlobalEffectEnableTime = systemTime();
2823
2824 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2825 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2826 if (t->mType == ThreadBase::OFFLOAD) {
2827 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2828 }
2829 }
2830
2831}
2832
Eric Laurentaaa44472014-09-12 17:41:50 -07002833status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2834{
2835 audio_session_t session = (audio_session_t)chain->sessionId();
2836 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2837 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2838 if (index >= 0) {
2839 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2840 return ALREADY_EXISTS;
2841 }
2842 mOrphanEffectChains.add(session, chain);
2843 return NO_ERROR;
2844}
2845
2846sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2847{
2848 sp<EffectChain> chain;
2849 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2850 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2851 if (index >= 0) {
2852 chain = mOrphanEffectChains.valueAt(index);
2853 mOrphanEffectChains.removeItemsAt(index);
2854 }
2855 return chain;
2856}
2857
2858bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2859{
2860 Mutex::Autolock _l(mLock);
2861 audio_session_t session = (audio_session_t)effect->sessionId();
2862 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2863 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2864 if (index >= 0) {
2865 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2866 if (chain->removeEffect_l(effect) == 0) {
2867 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2868 mOrphanEffectChains.removeItemsAt(index);
2869 }
2870 return true;
2871 }
2872 return false;
2873}
2874
2875
Glenn Kastenda6ef132013-01-10 12:31:01 -08002876struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002877#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2878 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002879};
2880
2881int comparEntry(const void *p1, const void *p2)
2882{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002883 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002884}
2885
Glenn Kasten46909e72013-02-26 09:20:22 -08002886#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002887void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002888{
Eric Laurent81784c32012-11-19 14:55:58 -08002889 NBAIO_Source *teeSource = source.get();
2890 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002891 // .wav rotation
2892 // There is a benign race condition if 2 threads call this simultaneously.
2893 // They would both traverse the directory, but the result would simply be
2894 // failures at unlink() which are ignored. It's also unlikely since
2895 // normally dumpsys is only done by bugreport or from the command line.
2896 char teePath[32+256];
2897 strcpy(teePath, "/data/misc/media");
2898 size_t teePathLen = strlen(teePath);
2899 DIR *dir = opendir(teePath);
2900 teePath[teePathLen++] = '/';
2901 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002902#define TEE_MAX_SORT 20 // number of entries to sort
2903#define TEE_MAX_KEEP 10 // number of entries to keep
2904 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002905 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002906 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002907 struct dirent de;
2908 struct dirent *result = NULL;
2909 int rc = readdir_r(dir, &de, &result);
2910 if (rc != 0) {
2911 ALOGW("readdir_r failed %d", rc);
2912 break;
2913 }
2914 if (result == NULL) {
2915 break;
2916 }
2917 if (result != &de) {
2918 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2919 break;
2920 }
2921 // ignore non .wav file entries
2922 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002923 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08002924 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2925 continue;
2926 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08002927 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002928 }
2929 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002930 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002931 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002932 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
2933 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002934 (void) unlink(teePath);
2935 }
2936 }
2937 } else {
2938 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002939 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002940 }
2941 }
Eric Laurent81784c32012-11-19 14:55:58 -08002942 char teeTime[16];
2943 struct timeval tv;
2944 gettimeofday(&tv, NULL);
2945 struct tm tm;
2946 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002947 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2948 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2949 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2950 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002951 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002952 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002953 char wavHeader[44];
2954 memcpy(wavHeader,
2955 "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",
2956 sizeof(wavHeader));
2957 NBAIO_Format format = teeSource->format();
2958 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002959 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002960 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002961 wavHeader[22] = channelCount; // number of channels
2962 wavHeader[24] = sampleRate; // sample rate
2963 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002964 wavHeader[32] = frameSize; // block alignment
2965 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002966 write(teeFd, wavHeader, sizeof(wavHeader));
2967 size_t total = 0;
2968 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002969#define TEE_SINK_READ 1024 // frames per I/O operation
2970 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002971 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002972 size_t count = TEE_SINK_READ;
2973 ssize_t actual = teeSource->read(buffer, count,
2974 AudioBufferProvider::kInvalidPTS);
2975 bool wasFirstRead = firstRead;
2976 firstRead = false;
2977 if (actual <= 0) {
2978 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2979 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002980 }
Eric Laurent81784c32012-11-19 14:55:58 -08002981 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002982 }
Eric Laurent81784c32012-11-19 14:55:58 -08002983 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002984 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002985 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002986 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002987 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002988 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002989 uint32_t temp = 44 + total * frameSize - 8;
2990 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002991 write(teeFd, &temp, sizeof(temp));
2992 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002993 temp = total * frameSize;
2994 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002995 write(teeFd, &temp, sizeof(temp));
2996 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002997 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002998 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002999 }
Eric Laurent59255e42011-07-27 19:49:51 -07003000 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003001 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003002 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003003 }
Eric Laurent59255e42011-07-27 19:49:51 -07003004 }
3005 }
3006}
Glenn Kasten46909e72013-02-26 09:20:22 -08003007#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003008
Mathias Agopian65ab4712010-07-14 17:59:35 -07003009// ----------------------------------------------------------------------------
3010
3011status_t AudioFlinger::onTransact(
3012 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3013{
3014 return BnAudioFlinger::onTransact(code, data, reply, flags);
3015}
3016
Glenn Kasten63238ef2015-03-02 15:50:29 -08003017} // namespace android