blob: fce69ad563638debf702238d0db51b2184bb4f05 [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>
Andy Hung35fec5f2016-04-13 14:21:48 -070034#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/String16.h>
36#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070037#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
Dima Zavinfce7a472011-04-19 22:30:36 -070039#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070040#include <cutils/properties.h>
41
Dima Zavin64760242011-05-11 14:15:23 -070042#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070043#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
45#include "AudioMixer.h"
46#include "AudioFlinger.h"
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070047#include "EffectsFactoryHalInterface.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080048#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
Andy Hung6770c6f2015-04-07 13:43:36 -070050#include <media/AudioResamplerPublic.h>
51
Eric Laurent6d8b6942011-06-24 07:01:31 -070052#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070053#include <audio_effects/effect_ns.h>
54#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070055
Glenn Kasten3b21c502011-12-15 09:52:39 -080056#include <audio_utils/primitives.h>
57
Eric Laurentfeb0db62011-07-22 09:04:31 -070058#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080059
Glenn Kasten9e58b552013-01-18 15:09:48 -080060#include <media/IMediaLogService.h>
Andy Hung07b745e2016-05-23 16:21:07 -070061#include <media/MemoryLeakTrackUtil.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080062#include <media/nbaio/Pipe.h>
63#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070064#include <media/AudioParameter.h>
Wei Jia3f273d12015-11-24 09:06:49 -080065#include <mediautils/BatteryNotifier.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";
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070088static const char kNoEffectsFactory[] = "Effects Factory is absent\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070089
Glenn Kasten58912562012-04-03 10:45:00 -070090
John Grossman4ff14ba2012-02-08 16:37:41 -080091nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080092
Eric Laurent81784c32012-11-19 14:55:58 -080093uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070094
Glenn Kasten46909e72013-02-26 09:20:22 -080095#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080096bool AudioFlinger::mTeeSinkInputEnabled = false;
97bool AudioFlinger::mTeeSinkOutputEnabled = false;
98bool AudioFlinger::mTeeSinkTrackEnabled = false;
99
100size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
101size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
102size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800103#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800104
Eric Laurent813e2a72013-08-31 12:59:48 -0700105// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
106// we define a minimum time during which a global effect is considered enabled.
107static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
108
Mathias Agopian65ab4712010-07-14 17:59:35 -0700109// ----------------------------------------------------------------------------
110
Marco Nelissenb2208842014-02-07 14:00:50 -0800111const char *formatToString(audio_format_t format) {
Phil Burkfdb3c072016-02-09 10:47:02 -0800112 switch (audio_get_main_format(format)) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530113 case AUDIO_FORMAT_PCM:
114 switch (format) {
115 case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
116 case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
117 case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
118 case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
119 case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
120 case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
121 default:
122 break;
123 }
124 break;
Marco Nelissenb2208842014-02-07 14:00:50 -0800125 case AUDIO_FORMAT_MP3: return "mp3";
126 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
127 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
128 case AUDIO_FORMAT_AAC: return "aac";
129 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
130 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
131 case AUDIO_FORMAT_VORBIS: return "vorbis";
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530132 case AUDIO_FORMAT_OPUS: return "opus";
133 case AUDIO_FORMAT_AC3: return "ac-3";
134 case AUDIO_FORMAT_E_AC3: return "e-ac-3";
Phil Burkfdb3c072016-02-09 10:47:02 -0800135 case AUDIO_FORMAT_IEC61937: return "iec61937";
Eric Laurente30f2092016-07-07 18:56:57 -0700136 case AUDIO_FORMAT_DTS: return "dts";
137 case AUDIO_FORMAT_DTS_HD: return "dts-hd";
138 case AUDIO_FORMAT_DOLBY_TRUEHD: return "dolby-truehd";
Marco Nelissenb2208842014-02-07 14:00:50 -0800139 default:
140 break;
141 }
142 return "unknown";
143}
144
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700145static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700146{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700147 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700148 int rc;
149
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700150 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
151 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
152 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
153 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700154 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700155 }
156 rc = audio_hw_device_open(mod, dev);
157 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
158 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
159 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700160 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700161 }
Eric Laurent5806b352014-05-22 12:23:26 -0700162 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700163 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
164 rc = BAD_VALUE;
165 goto out;
166 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700167 return 0;
168
169out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700170 *dev = NULL;
171 return rc;
172}
173
Mathias Agopian65ab4712010-07-14 17:59:35 -0700174// ----------------------------------------------------------------------------
175
176AudioFlinger::AudioFlinger()
177 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800178 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800179 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700180 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800181 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800182 mMasterMute(false),
Glenn Kastend2e67e12016-04-11 08:26:37 -0700183 // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),
John Grossman4ff14ba2012-02-08 16:37:41 -0800184 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700185 mBtNrecIsOff(false),
186 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700187 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700188 mGlobalEffectEnableTime(0),
Eric Laurent72e3f392015-05-20 14:43:50 -0700189 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190{
Glenn Kastend2e67e12016-04-11 08:26:37 -0700191 // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum
192 for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) {
193 // zero ID has a special meaning, so unavailable
194 mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX;
195 }
196
Glenn Kasten949a9262013-04-16 12:35:20 -0700197 getpid_cached = getpid();
Glenn Kastenae0cff12016-02-24 13:57:49 -0800198 const bool doLog = property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800199 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800200 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
201 MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800202 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700203
Wei Jia3f273d12015-11-24 09:06:49 -0800204 // reset battery stats.
205 // if the audio service has crashed, battery stats could be left
206 // in bad state, reset the state upon service start.
207 BatteryNotifier::getInstance().noteResetAudio();
208
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700209 mEffectsFactoryHal = EffectsFactoryHalInterface::create();
210
Glenn Kasten46909e72013-02-26 09:20:22 -0800211#ifdef TEE_SINK
Glenn Kasten9a003992016-02-23 15:24:34 -0800212 char value[PROPERTY_VALUE_MAX];
Glenn Kastenda6ef132013-01-10 12:31:01 -0800213 (void) property_get("ro.debuggable", value, "0");
214 int debuggable = atoi(value);
215 int teeEnabled = 0;
216 if (debuggable) {
217 (void) property_get("af.tee", value, "0");
218 teeEnabled = atoi(value);
219 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800220 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700221 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800222 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700223 }
224 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800225 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700226 }
227 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800228 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700229 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800230#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700231}
232
233void AudioFlinger::onFirstRef()
234{
Eric Laurent93575202011-01-18 18:39:02 -0800235 Mutex::Autolock _l(mLock);
236
Dima Zavin799a70e2011-04-18 16:57:27 -0700237 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800238 char val_str[PROPERTY_VALUE_MAX] = { 0 };
239 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
240 uint32_t int_val;
241 if (1 == sscanf(val_str, "%u", &int_val)) {
242 mStandbyTimeInNsecs = milliseconds(int_val);
243 ALOGI("Using %u mSec as standby time.", int_val);
244 } else {
245 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
246 ALOGI("Using default %u mSec as standby time.",
247 (uint32_t)(mStandbyTimeInNsecs / 1000000));
248 }
249 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700250
Eric Laurent1c333e22014-05-20 10:48:17 -0700251 mPatchPanel = new PatchPanel(this);
Eric Laurent17a58b22016-08-17 13:53:12 +0200252 // FIXME: bug 30737845: trigger audioserver restart if main audioflinger lock
253 // is held continuously for more than 3 seconds
254 mLockWatch = new LockWatch(mLock, String8("AudioFlinger"));
Eric Laurenta4c5a552012-03-29 10:12:40 -0700255 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256}
257
258AudioFlinger::~AudioFlinger()
259{
260 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700261 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700262 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263 }
264 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700265 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700266 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700268
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800269 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
270 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700271 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
272 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700274
275 // Tell media.log service about any old writers that still need to be unregistered
Andy Hungce717682015-12-22 13:40:32 -0800276 if (mLogMemoryDealer != 0) {
277 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
278 if (binder != 0) {
279 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
280 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
281 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
282 mUnregisteredWriters.pop();
283 mediaLogService->unregisterWriter(iMemory);
284 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700285 }
286 }
Eric Laurent17a58b22016-08-17 13:53:12 +0200287 mLockWatch->requestExitAndWait();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700288}
289
Eric Laurenta4c5a552012-03-29 10:12:40 -0700290static const char * const audio_interfaces[] = {
291 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
292 AUDIO_HARDWARE_MODULE_ID_A2DP,
293 AUDIO_HARDWARE_MODULE_ID_USB,
294};
295#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
296
Phil Burk062e67a2015-02-11 13:40:50 -0800297AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700298 audio_module_handle_t module,
299 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700300{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700301 // if module is 0, the request comes from an old policy manager and we should load
302 // well known modules
303 if (module == 0) {
304 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
305 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
306 loadHwModule_l(audio_interfaces[i]);
307 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700308 // then try to find a module supporting the requested device.
309 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
310 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
311 audio_hw_device_t *dev = audioHwDevice->hwDevice();
312 if ((dev->get_supported_devices != NULL) &&
313 (dev->get_supported_devices(dev) & devices) == devices)
314 return audioHwDevice;
315 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700316 } else {
317 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700318 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
319 if (audioHwDevice != NULL) {
320 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700321 }
322 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700323
Dima Zavin799a70e2011-04-18 16:57:27 -0700324 return NULL;
325}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326
Glenn Kasten0f11b512014-01-31 16:18:54 -0800327void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328{
329 const size_t SIZE = 256;
330 char buffer[SIZE];
331 String8 result;
332
333 result.append("Clients:\n");
334 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800335 sp<Client> client = mClients.valueAt(i).promote();
336 if (client != 0) {
337 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
338 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700339 }
340 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700341
Eric Laurentd1b28d42013-09-18 18:47:13 -0700342 result.append("Notification Clients:\n");
343 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
344 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
345 result.append(buffer);
346 }
347
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700348 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800349 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700350 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
351 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800352 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700353 result.append(buffer);
354 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356}
357
358
Glenn Kasten0f11b512014-01-31 16:18:54 -0800359void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360{
361 const size_t SIZE = 256;
362 char buffer[SIZE];
363 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800364 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365
John Grossman4ff14ba2012-02-08 16:37:41 -0800366 snprintf(buffer, SIZE, "Hardware status: %d\n"
367 "Standby Time mSec: %u\n",
368 hardwareStatus,
369 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 result.append(buffer);
371 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372}
373
Glenn Kasten0f11b512014-01-31 16:18:54 -0800374void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375{
376 const size_t SIZE = 256;
377 char buffer[SIZE];
378 String8 result;
379 snprintf(buffer, SIZE, "Permission Denial: "
380 "can't dump AudioFlinger from pid=%d, uid=%d\n",
381 IPCThreadState::self()->getCallingPid(),
382 IPCThreadState::self()->getCallingUid());
383 result.append(buffer);
384 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385}
386
Eric Laurent81784c32012-11-19 14:55:58 -0800387bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388{
389 bool locked = false;
390 for (int i = 0; i < kDumpLockRetries; ++i) {
391 if (mutex.tryLock() == NO_ERROR) {
392 locked = true;
393 break;
394 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800395 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396 }
397 return locked;
398}
399
400status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
401{
Glenn Kasten44deb052012-02-05 18:09:08 -0800402 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403 dumpPermissionDenial(fd, args);
404 } else {
405 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800406 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407 if (!hardwareLocked) {
408 String8 result(kHardwareLockedString);
409 write(fd, result.string(), result.size());
410 } else {
411 mHardwareLock.unlock();
412 }
413
Eric Laurent81784c32012-11-19 14:55:58 -0800414 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700415
416 // failed to lock - AudioFlinger is probably deadlocked
417 if (!locked) {
418 String8 result(kDeadlockedString);
419 write(fd, result.string(), result.size());
420 }
421
Eric Laurent021cf962014-05-13 10:18:14 -0700422 bool clientLocked = dumpTryLock(mClientLock);
423 if (!clientLocked) {
424 String8 result(kClientLockedString);
425 write(fd, result.string(), result.size());
426 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700427
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700428 if (mEffectsFactoryHal.get() != NULL) {
429 mEffectsFactoryHal->dumpEffects(fd);
430 } else {
431 String8 result(kNoEffectsFactory);
432 write(fd, result.string(), result.size());
433 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700434
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700436 if (clientLocked) {
437 mClientLock.unlock();
438 }
439
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440 dumpInternals(fd, args);
441
442 // dump playback threads
443 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
444 mPlaybackThreads.valueAt(i)->dump(fd, args);
445 }
446
447 // dump record threads
448 for (size_t i = 0; i < mRecordThreads.size(); i++) {
449 mRecordThreads.valueAt(i)->dump(fd, args);
450 }
451
Eric Laurentaaa44472014-09-12 17:41:50 -0700452 // dump orphan effect chains
453 if (mOrphanEffectChains.size() != 0) {
454 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
455 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
456 mOrphanEffectChains.valueAt(i)->dump(fd, args);
457 }
458 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700459 // dump all hardware devs
460 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700461 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700462 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700464
Glenn Kasten46909e72013-02-26 09:20:22 -0800465#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700466 // dump the serially shared record tee sink
467 if (mRecordTeeSource != 0) {
468 dumpTee(fd, mRecordTeeSource);
469 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800470#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700471
Glenn Kastend65d73c2012-06-22 17:21:07 -0700472 if (locked) {
473 mLock.unlock();
474 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800475
476 // append a copy of media.log here by forwarding fd to it, but don't attempt
477 // to lookup the service if it's not running, as it will block for a second
478 if (mLogMemoryDealer != 0) {
479 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
480 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700481 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800482 Vector<String16> args;
483 binder->dump(fd, args);
484 }
485 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700486
487 // check for optional arguments
Andy Hung07b745e2016-05-23 16:21:07 -0700488 bool dumpMem = false;
Andy Hung35fec5f2016-04-13 14:21:48 -0700489 bool unreachableMemory = false;
490 for (const auto &arg : args) {
Andy Hung07b745e2016-05-23 16:21:07 -0700491 if (arg == String16("-m")) {
492 dumpMem = true;
493 } else if (arg == String16("--unreachable")) {
Andy Hung35fec5f2016-04-13 14:21:48 -0700494 unreachableMemory = true;
495 }
496 }
497
Andy Hung07b745e2016-05-23 16:21:07 -0700498 if (dumpMem) {
499 dprintf(fd, "\nDumping memory:\n");
500 std::string s = dumpMemoryAddresses(100 /* limit */);
501 write(fd, s.c_str(), s.size());
502 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700503 if (unreachableMemory) {
504 dprintf(fd, "\nDumping unreachable memory:\n");
505 // TODO - should limit be an argument parameter?
Andy Hung07b745e2016-05-23 16:21:07 -0700506 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
Andy Hung35fec5f2016-04-13 14:21:48 -0700507 write(fd, s.c_str(), s.size());
508 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700509 }
510 return NO_ERROR;
511}
512
Eric Laurent021cf962014-05-13 10:18:14 -0700513sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800514{
Eric Laurent021cf962014-05-13 10:18:14 -0700515 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800516 // If pid is already in the mClients wp<> map, then use that entry
517 // (for which promote() is always != 0), otherwise create a new entry and Client.
518 sp<Client> client = mClients.valueFor(pid).promote();
519 if (client == 0) {
520 client = new Client(this, pid);
521 mClients.add(pid, client);
522 }
523
524 return client;
525}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526
Glenn Kasten9e58b552013-01-18 15:09:48 -0800527sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
528{
Glenn Kasten481fb672013-09-30 14:39:28 -0700529 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800530 if (mLogMemoryDealer == 0) {
531 return new NBLog::Writer();
532 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800533 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700534 // Similarly if we can't contact the media.log service, also return a dummy writer
535 if (binder == 0) {
536 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800537 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700538 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
539 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
540 // If allocation fails, consult the vector of previously unregistered writers
541 // and garbage-collect one or more them until an allocation succeeds
542 if (shared == 0) {
543 Mutex::Autolock _l(mUnregisteredWritersLock);
544 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
545 {
546 // Pick the oldest stale writer to garbage-collect
547 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
548 mUnregisteredWriters.removeAt(0);
549 mediaLogService->unregisterWriter(iMemory);
550 // Now the media.log remote reference to IMemory is gone. When our last local
551 // reference to IMemory also drops to zero at end of this block,
552 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
553 }
554 // Re-attempt the allocation
555 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
556 if (shared != 0) {
557 goto success;
558 }
559 }
560 // Even after garbage-collecting all old writers, there is still not enough memory,
561 // so return a dummy writer
562 return new NBLog::Writer();
563 }
564success:
565 mediaLogService->registerWriter(shared, size, name);
566 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800567}
568
569void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
570{
Glenn Kasten685ef092013-02-04 08:15:34 -0800571 if (writer == 0) {
572 return;
573 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800574 sp<IMemory> iMemory(writer->getIMemory());
575 if (iMemory == 0) {
576 return;
577 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700578 // Rather than removing the writer immediately, append it to a queue of old writers to
579 // be garbage-collected later. This allows us to continue to view old logs for a while.
580 Mutex::Autolock _l(mUnregisteredWritersLock);
581 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800582}
583
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584// IAudioFlinger interface
585
586
587sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800588 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800590 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700591 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800592 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -0700593 audio_output_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800595 audio_io_handle_t output,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700596 pid_t pid,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800597 pid_t tid,
Glenn Kastend848eb42016-03-08 13:42:11 -0800598 audio_session_t *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800599 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600 status_t *status)
601{
602 sp<PlaybackThread::Track> track;
603 sp<TrackHandle> trackHandle;
604 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -0800606 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700608 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
609 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
610 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
611 ALOGW_IF(pid != -1 && pid != callingPid,
612 "%s uid %d pid %d tried to pass itself off as pid %d",
613 __func__, callingUid, callingPid, pid);
614 pid = callingPid;
615 }
616
Glenn Kasten263709e2012-01-06 08:40:01 -0800617 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
618 // but if someone uses binder directly they could bypass that and cause us to crash
619 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000620 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 lStatus = BAD_VALUE;
622 goto Exit;
623 }
624
Glenn Kasten53b5d092014-02-05 10:00:23 -0800625 // further sample rate checks are performed by createTrack_l() depending on the thread type
626 if (sampleRate == 0) {
627 ALOGE("createTrack() invalid sample rate %u", sampleRate);
628 lStatus = BAD_VALUE;
629 goto Exit;
630 }
631
632 // further channel mask checks are performed by createTrack_l() depending on the thread type
633 if (!audio_is_output_channel(channelMask)) {
634 ALOGE("createTrack() invalid channel mask %#x", channelMask);
635 lStatus = BAD_VALUE;
636 goto Exit;
637 }
638
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700639 // further format checks are performed by createTrack_l() depending on the thread type
640 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800641 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700642 lStatus = BAD_VALUE;
643 goto Exit;
644 }
645
Glenn Kasten663c2242013-09-24 11:52:37 -0700646 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
647 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
648 lStatus = BAD_VALUE;
649 goto Exit;
650 }
651
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652 {
653 Mutex::Autolock _l(mLock);
654 PlaybackThread *thread = checkPlaybackThread_l(output);
655 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800656 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 lStatus = BAD_VALUE;
658 goto Exit;
659 }
660
Eric Laurent021cf962014-05-13 10:18:14 -0700661 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700662
Glenn Kastene848bd92014-03-13 15:00:32 -0700663 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700664 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800665 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
666 ALOGE("createTrack() invalid session ID %d", *sessionId);
667 lStatus = BAD_VALUE;
668 goto Exit;
669 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700670 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700671 // check if an effect chain with the same session ID is present on another
672 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700673 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700674 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
675 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700676 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent4c415062016-06-17 16:14:16 -0700677 if (sessions & ThreadBase::EFFECT_SESSION) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700678 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700679 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700680 }
Eric Laurentde070132010-07-13 04:45:46 -0700681 }
682 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700684 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800685 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686 if (sessionId != NULL) {
687 *sessionId = lSessionId;
688 }
689 }
Steve Block3856b092011-10-20 11:56:00 +0100690 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691
692 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800693 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800694 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700695 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700696
697 // move effect chain to this output thread if an effect on same session was waiting
698 // for a track to be created
699 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700700 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700701 Mutex::Autolock _dl(thread->mLock);
702 Mutex::Autolock _sl(effectThread->mLock);
703 moveEffectChain_l(lSessionId, effectThread, thread, true);
704 }
Eric Laurenta011e352012-03-29 15:51:43 -0700705
706 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700707 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700708 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
709 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700710 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700711 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700712 } else {
713 mPendingSyncEvents[i]->cancel();
714 }
Eric Laurenta011e352012-03-29 15:51:43 -0700715 mPendingSyncEvents.removeAt(i);
716 i--;
717 }
718 }
719 }
Glenn Kastene198c362013-08-13 09:13:36 -0700720
Glenn Kastend848eb42016-03-08 13:42:11 -0800721 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 }
Glenn Kastene198c362013-08-13 09:13:36 -0700723
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700724 if (lStatus != NO_ERROR) {
725 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700726 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700727 // Don't hold mClientLock when releasing the reference on the track as the
728 // destructor will acquire it.
729 {
730 Mutex::Autolock _cl(mClientLock);
731 client.clear();
732 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700733 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700734 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 }
736
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700737 // return handle to client
738 trackHandle = new TrackHandle(track);
739
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700741 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742 return trackHandle;
743}
744
Glenn Kasten2c073da2016-02-26 09:14:08 -0800745uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700746{
747 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800748 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800750 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751 return 0;
752 }
753 return thread->sampleRate();
754}
755
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800756audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757{
758 Mutex::Autolock _l(mLock);
759 PlaybackThread *thread = checkPlaybackThread_l(output);
760 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000761 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800762 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763 }
764 return thread->format();
765}
766
Glenn Kasten2c073da2016-02-26 09:14:08 -0800767size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768{
769 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800770 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800772 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773 return 0;
774 }
Glenn Kasten58912562012-04-03 10:45:00 -0700775 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
776 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777 return thread->frameCount();
778}
779
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700780size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
781{
782 Mutex::Autolock _l(mLock);
783 ThreadBase *thread = checkThread_l(ioHandle);
784 if (thread == NULL) {
785 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
786 return 0;
787 }
788 return thread->frameCountHAL();
789}
790
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800791uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792{
793 Mutex::Autolock _l(mLock);
794 PlaybackThread *thread = checkPlaybackThread_l(output);
795 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800796 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797 return 0;
798 }
799 return thread->latency();
800}
801
802status_t AudioFlinger::setMasterVolume(float value)
803{
Eric Laurenta1884f92011-08-23 08:25:03 -0700804 status_t ret = initCheck();
805 if (ret != NO_ERROR) {
806 return ret;
807 }
808
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809 // check calling permissions
810 if (!settingsAllowed()) {
811 return PERMISSION_DENIED;
812 }
813
Eric Laurenta4c5a552012-03-29 10:12:40 -0700814 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700815 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700816
John Grossmanee578c02012-07-23 17:05:46 -0700817 // Set master volume in the HALs which support it.
818 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
819 AutoMutex lock(mHardwareLock);
820 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800821
John Grossmanee578c02012-07-23 17:05:46 -0700822 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
823 if (dev->canSetMasterVolume()) {
824 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800825 }
John Grossmanee578c02012-07-23 17:05:46 -0700826 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700827 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828
John Grossmanee578c02012-07-23 17:05:46 -0700829 // Now set the master volume in each playback thread. Playback threads
830 // assigned to HALs which do not have master volume support will apply
831 // master volume during the mix operation. Threads with HALs which do
832 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700833 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
834 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
835 continue;
836 }
John Grossmanee578c02012-07-23 17:05:46 -0700837 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700838 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839
840 return NO_ERROR;
841}
842
Glenn Kastenf78aee72012-01-04 11:00:47 -0800843status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844{
Eric Laurenta1884f92011-08-23 08:25:03 -0700845 status_t ret = initCheck();
846 if (ret != NO_ERROR) {
847 return ret;
848 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849
850 // check calling permissions
851 if (!settingsAllowed()) {
852 return PERMISSION_DENIED;
853 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800854 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000855 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856 return BAD_VALUE;
857 }
858
859 { // scope for the lock
860 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700861 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700863 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700864 mHardwareStatus = AUDIO_HW_IDLE;
865 }
866
867 if (NO_ERROR == ret) {
868 Mutex::Autolock _l(mLock);
869 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800870 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700871 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 }
873
874 return ret;
875}
876
877status_t AudioFlinger::setMicMute(bool state)
878{
Eric Laurenta1884f92011-08-23 08:25:03 -0700879 status_t ret = initCheck();
880 if (ret != NO_ERROR) {
881 return ret;
882 }
883
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884 // check calling permissions
885 if (!settingsAllowed()) {
886 return PERMISSION_DENIED;
887 }
888
889 AutoMutex lock(mHardwareLock);
890 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700891 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
892 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
893 status_t result = dev->set_mic_mute(dev, state);
894 if (result != NO_ERROR) {
895 ret = result;
896 }
897 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898 mHardwareStatus = AUDIO_HW_IDLE;
899 return ret;
900}
901
902bool AudioFlinger::getMicMute() const
903{
Eric Laurenta1884f92011-08-23 08:25:03 -0700904 status_t ret = initCheck();
905 if (ret != NO_ERROR) {
906 return false;
907 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800908 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700909 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800910 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800912 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
913 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
914 status_t result = dev->get_mic_mute(dev, &state);
915 if (result == NO_ERROR) {
916 mute = mute && state;
917 }
918 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800920
921 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700922}
923
924status_t AudioFlinger::setMasterMute(bool muted)
925{
John Grossmand8f178d2012-07-20 14:51:35 -0700926 status_t ret = initCheck();
927 if (ret != NO_ERROR) {
928 return ret;
929 }
930
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931 // check calling permissions
932 if (!settingsAllowed()) {
933 return PERMISSION_DENIED;
934 }
935
John Grossmanee578c02012-07-23 17:05:46 -0700936 Mutex::Autolock _l(mLock);
937 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700938
John Grossmanee578c02012-07-23 17:05:46 -0700939 // Set master mute in the HALs which support it.
940 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
941 AutoMutex lock(mHardwareLock);
942 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700943
John Grossmanee578c02012-07-23 17:05:46 -0700944 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
945 if (dev->canSetMasterMute()) {
946 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700947 }
John Grossmanee578c02012-07-23 17:05:46 -0700948 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700949 }
950
John Grossmanee578c02012-07-23 17:05:46 -0700951 // Now set the master mute in each playback thread. Playback threads
952 // assigned to HALs which do not have master mute support will apply master
953 // mute during the mix operation. Threads with HALs which do support master
954 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700955 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
956 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
957 continue;
958 }
John Grossmanee578c02012-07-23 17:05:46 -0700959 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700960 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961
962 return NO_ERROR;
963}
964
965float AudioFlinger::masterVolume() const
966{
Glenn Kasten98067102011-12-13 11:47:54 -0800967 Mutex::Autolock _l(mLock);
968 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969}
970
971bool AudioFlinger::masterMute() const
972{
Glenn Kasten98067102011-12-13 11:47:54 -0800973 Mutex::Autolock _l(mLock);
974 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975}
976
John Grossman4ff14ba2012-02-08 16:37:41 -0800977float AudioFlinger::masterVolume_l() const
978{
John Grossman4ff14ba2012-02-08 16:37:41 -0800979 return mMasterVolume;
980}
981
John Grossmand8f178d2012-07-20 14:51:35 -0700982bool AudioFlinger::masterMute_l() const
983{
John Grossmanee578c02012-07-23 17:05:46 -0700984 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700985}
986
Eric Laurent223fd5c2014-11-11 13:43:36 -0800987status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
988{
989 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
990 ALOGW("setStreamVolume() invalid stream %d", stream);
991 return BAD_VALUE;
992 }
993 pid_t caller = IPCThreadState::self()->getCallingPid();
994 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
995 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
996 return PERMISSION_DENIED;
997 }
998
999 return NO_ERROR;
1000}
1001
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001002status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
1003 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004{
1005 // check calling permissions
1006 if (!settingsAllowed()) {
1007 return PERMISSION_DENIED;
1008 }
1009
Eric Laurent223fd5c2014-11-11 13:43:36 -08001010 status_t status = checkStreamType(stream);
1011 if (status != NO_ERROR) {
1012 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001013 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001014 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015
1016 AutoMutex lock(mLock);
1017 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -07001018 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001019 thread = checkPlaybackThread_l(output);
1020 if (thread == NULL) {
1021 return BAD_VALUE;
1022 }
1023 }
1024
1025 mStreamTypes[stream].volume = value;
1026
1027 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001028 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001029 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001030 }
1031 } else {
1032 thread->setStreamVolume(stream, value);
1033 }
1034
1035 return NO_ERROR;
1036}
1037
Glenn Kastenfff6d712012-01-12 16:38:12 -08001038status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039{
1040 // check calling permissions
1041 if (!settingsAllowed()) {
1042 return PERMISSION_DENIED;
1043 }
1044
Eric Laurent223fd5c2014-11-11 13:43:36 -08001045 status_t status = checkStreamType(stream);
1046 if (status != NO_ERROR) {
1047 return status;
1048 }
1049 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1050
1051 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001052 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001053 return BAD_VALUE;
1054 }
1055
Eric Laurent93575202011-01-18 18:39:02 -08001056 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001057 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -07001058 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001059 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060
1061 return NO_ERROR;
1062}
1063
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001064float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001066 status_t status = checkStreamType(stream);
1067 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068 return 0.0f;
1069 }
1070
1071 AutoMutex lock(mLock);
1072 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001073 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074 PlaybackThread *thread = checkPlaybackThread_l(output);
1075 if (thread == NULL) {
1076 return 0.0f;
1077 }
1078 volume = thread->streamVolume(stream);
1079 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001080 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001081 }
1082
1083 return volume;
1084}
1085
Glenn Kastenfff6d712012-01-12 16:38:12 -08001086bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001087{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001088 status_t status = checkStreamType(stream);
1089 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090 return true;
1091 }
1092
Glenn Kasten6637baa2012-01-09 09:40:36 -08001093 AutoMutex lock(mLock);
1094 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095}
1096
Eric Laurent054d9d32015-04-24 08:48:48 -07001097
1098void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1099{
1100 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1101 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1102 }
1103}
1104
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001105status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001107 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1108 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001109
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110 // check calling permissions
1111 if (!settingsAllowed()) {
1112 return PERMISSION_DENIED;
1113 }
1114
Glenn Kasten142f5192014-03-25 17:44:59 -07001115 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1116 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001117 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001118 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001119 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001120 AutoMutex lock(mHardwareLock);
1121 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1122 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1123 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1124 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1125 final_result = result ?: final_result;
1126 }
1127 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001128 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001129 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1130 AudioParameter param = AudioParameter(keyValuePairs);
1131 String8 value;
1132 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001133 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1134 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001135 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1136 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001137 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001138 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1139 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001140 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001141 // suspend effects associated with those session IDs
1142 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001143 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001144 thread->setEffectSuspended(FX_IID_AEC,
1145 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001146 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001147 thread->setEffectSuspended(FX_IID_NS,
1148 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001149 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001150 }
1151 }
Eric Laurentbee53372011-08-29 12:42:48 -07001152 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001153 }
1154 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001155 String8 screenState;
1156 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1157 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001158 if (isOff != (AudioFlinger::mScreenState & 1)) {
1159 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001160 }
1161 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001162 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163 }
1164
1165 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1166 // and the thread is exited once the lock is released
1167 sp<ThreadBase> thread;
1168 {
1169 Mutex::Autolock _l(mLock);
1170 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001171 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001172 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001173 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001174 // indicate output device change to all input threads for pre processing
1175 AudioParameter param = AudioParameter(keyValuePairs);
1176 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001177 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1178 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001179 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001180 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001181 }
1182 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001183 if (thread != 0) {
1184 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185 }
1186 return BAD_VALUE;
1187}
1188
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001189String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001190{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001191 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1192 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001193
Eric Laurenta4c5a552012-03-29 10:12:40 -07001194 Mutex::Autolock _l(mLock);
1195
Glenn Kasten142f5192014-03-25 17:44:59 -07001196 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001197 String8 out_s8;
1198
Dima Zavin799a70e2011-04-18 16:57:27 -07001199 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001200 char *s;
1201 {
1202 AutoMutex lock(mHardwareLock);
1203 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001204 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001205 s = dev->get_parameters(dev, keys.string());
1206 mHardwareStatus = AUDIO_HW_IDLE;
1207 }
John Grossmanef7740b2012-02-09 11:28:36 -08001208 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001209 free(s);
1210 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001211 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001212 }
1213
Mathias Agopian65ab4712010-07-14 17:59:35 -07001214 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1215 if (playbackThread != NULL) {
1216 return playbackThread->getParameters(keys);
1217 }
1218 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1219 if (recordThread != NULL) {
1220 return recordThread->getParameters(keys);
1221 }
1222 return String8("");
1223}
1224
Glenn Kastendd8104c2012-07-02 12:42:44 -07001225size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1226 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001227{
Eric Laurenta1884f92011-08-23 08:25:03 -07001228 status_t ret = initCheck();
1229 if (ret != NO_ERROR) {
1230 return 0;
1231 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001232 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001233 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001234 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001235 return 0;
1236 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001237
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001238 AutoMutex lock(mHardwareLock);
1239 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001240 audio_config_t config, proposed;
1241 memset(&proposed, 0, sizeof(proposed));
1242 proposed.sample_rate = sampleRate;
1243 proposed.channel_mask = channelMask;
1244 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001245
John Grossmanee578c02012-07-23 17:05:46 -07001246 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001247 size_t frames;
1248 for (;;) {
1249 // Note: config is currently a const parameter for get_input_buffer_size()
1250 // but we use a copy from proposed in case config changes from the call.
1251 config = proposed;
1252 frames = dev->get_input_buffer_size(dev, &config);
1253 if (frames != 0) {
1254 break; // hal success, config is the result
1255 }
1256 // change one parameter of the configuration each iteration to a more "common" value
1257 // to see if the device will support it.
1258 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1259 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1260 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1261 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1262 } else {
1263 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1264 "format %#x, channelMask 0x%X",
1265 sampleRate, format, channelMask);
1266 break; // retries failed, break out of loop with frames == 0.
1267 }
1268 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001269 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001270 if (frames > 0 && config.sample_rate != sampleRate) {
1271 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1272 }
1273 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001274}
1275
Glenn Kasten5f972c02014-01-13 09:59:31 -08001276uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278 Mutex::Autolock _l(mLock);
1279
1280 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1281 if (recordThread != NULL) {
1282 return recordThread->getInputFramesLost();
1283 }
1284 return 0;
1285}
1286
1287status_t AudioFlinger::setVoiceVolume(float value)
1288{
Eric Laurenta1884f92011-08-23 08:25:03 -07001289 status_t ret = initCheck();
1290 if (ret != NO_ERROR) {
1291 return ret;
1292 }
1293
Mathias Agopian65ab4712010-07-14 17:59:35 -07001294 // check calling permissions
1295 if (!settingsAllowed()) {
1296 return PERMISSION_DENIED;
1297 }
1298
1299 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001300 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001301 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001302 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001303 mHardwareStatus = AUDIO_HW_IDLE;
1304
1305 return ret;
1306}
1307
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001308status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001309 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001311 Mutex::Autolock _l(mLock);
1312
1313 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1314 if (playbackThread != NULL) {
1315 return playbackThread->getRenderPosition(halFrames, dspFrames);
1316 }
1317
1318 return BAD_VALUE;
1319}
1320
1321void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1322{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001324 if (client == 0) {
1325 return;
1326 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001327 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001328 {
1329 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001330 if (mNotificationClients.indexOfKey(pid) < 0) {
1331 sp<NotificationClient> notificationClient = new NotificationClient(this,
1332 client,
1333 pid);
1334 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001335
Eric Laurent021cf962014-05-13 10:18:14 -07001336 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001337
Marco Nelissen06b46062014-11-14 07:58:25 -08001338 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001339 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001340 }
1341 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001342
Eric Laurent021cf962014-05-13 10:18:14 -07001343 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001344 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001345 // the config change is always sent from playback or record threads to avoid deadlock
1346 // with AudioSystem::gLock
1347 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1348 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1349 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001351 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1352 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001353 }
1354}
1355
1356void AudioFlinger::removeNotificationClient(pid_t pid)
1357{
1358 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001359 {
1360 Mutex::Autolock _cl(mClientLock);
1361 mNotificationClients.removeItem(pid);
1362 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001363
Steve Block3856b092011-10-20 11:56:00 +01001364 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001365 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001366 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001367 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001368 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001369 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001370 if (ref->mPid == pid) {
1371 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001372 mAudioSessionRefs.removeAt(i);
1373 delete ref;
1374 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001375 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001376 } else {
1377 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001378 }
1379 }
1380 if (removed) {
1381 purgeStaleEffects_l();
1382 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001383}
1384
Eric Laurent73e26b62015-04-27 16:55:58 -07001385void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001386 const sp<AudioIoDescriptor>& ioDesc,
1387 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388{
Eric Laurent021cf962014-05-13 10:18:14 -07001389 Mutex::Autolock _l(mClientLock);
1390 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001391 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001392 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1393 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1394 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001395 }
1396}
1397
Eric Laurent021cf962014-05-13 10:18:14 -07001398// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399void AudioFlinger::removeClient_l(pid_t pid)
1400{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001401 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001402 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403 mClients.removeItem(pid);
1404}
1405
Eric Laurent717e1282012-06-29 16:36:52 -07001406// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001407sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1408 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001409{
1410 sp<PlaybackThread> thread;
1411
1412 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1413 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1414 ALOG_ASSERT(thread == 0);
1415 thread = mPlaybackThreads.valueAt(i);
1416 }
1417 }
1418
1419 return thread;
1420}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001421
Mathias Agopian65ab4712010-07-14 17:59:35 -07001422
Mathias Agopian65ab4712010-07-14 17:59:35 -07001423
1424// ----------------------------------------------------------------------------
1425
1426AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1427 : RefBase(),
1428 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001429 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001430{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301431 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1432 heapSize *= 1024;
1433 if (!heapSize) {
1434 heapSize = kClientSharedHeapSizeBytes;
1435 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1436 // invalidated tracks
1437 if (!audioFlinger->isLowRamDevice()) {
1438 heapSize *= kClientSharedHeapSizeMultiplier;
1439 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001440 }
1441 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001442}
1443
Eric Laurent021cf962014-05-13 10:18:14 -07001444// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445AudioFlinger::Client::~Client()
1446{
1447 mAudioFlinger->removeClient_l(mPid);
1448}
1449
Glenn Kasten435dbe62012-01-30 10:15:48 -08001450sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001451{
1452 return mMemoryDealer;
1453}
1454
1455// ----------------------------------------------------------------------------
1456
1457AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1458 const sp<IAudioFlingerClient>& client,
1459 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001460 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001461{
1462}
1463
1464AudioFlinger::NotificationClient::~NotificationClient()
1465{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001466}
1467
Glenn Kasten0f11b512014-01-31 16:18:54 -08001468void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001469{
1470 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001471 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001472}
1473
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474
1475// ----------------------------------------------------------------------------
1476
1477sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001478 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001479 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001480 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001481 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001482 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001483 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -07001484 audio_input_flags_t *flags,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001485 pid_t pid,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001486 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001487 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001488 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001489 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001490 sp<IMemory>& cblk,
1491 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492 status_t *status)
1493{
1494 sp<RecordThread::RecordTrack> recordTrack;
1495 sp<RecordHandle> recordHandle;
1496 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001497 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001498 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001499
Glenn Kastend776ac62014-05-07 09:16:09 -07001500 cblk.clear();
1501 buffers.clear();
1502
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001503 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001504 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1505 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001506 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001507 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1508 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001509 updatePid = true;
1510 }
1511
1512 if (updatePid) {
1513 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
1514 ALOGW_IF(pid != -1 && pid != callingPid,
1515 "%s uid %d pid %d tried to pass itself off as pid %d",
1516 __func__, callingUid, callingPid, pid);
1517 pid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001518 }
1519
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001521 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001522 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001523 lStatus = PERMISSION_DENIED;
1524 goto Exit;
1525 }
1526
Glenn Kasten53b5d092014-02-05 10:00:23 -08001527 // further sample rate checks are performed by createRecordTrack_l()
1528 if (sampleRate == 0) {
1529 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1530 lStatus = BAD_VALUE;
1531 goto Exit;
1532 }
1533
Andy Hung6770c6f2015-04-07 13:43:36 -07001534 // we don't yet support anything other than linear PCM
1535 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001536 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001537 lStatus = BAD_VALUE;
1538 goto Exit;
1539 }
1540
Glenn Kasten53b5d092014-02-05 10:00:23 -08001541 // further channel mask checks are performed by createRecordTrack_l()
1542 if (!audio_is_input_channel(channelMask)) {
1543 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1544 lStatus = BAD_VALUE;
1545 goto Exit;
1546 }
1547
Glenn Kasten05997e22014-03-13 15:08:33 -07001548 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001549 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001550 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001552 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001553 lStatus = BAD_VALUE;
1554 goto Exit;
1555 }
1556
Eric Laurent021cf962014-05-13 10:18:14 -07001557 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001558
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001559 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001560 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1561 lStatus = BAD_VALUE;
1562 goto Exit;
1563 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001564 lSessionId = *sessionId;
1565 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001566 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001567 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001568 if (sessionId != NULL) {
1569 *sessionId = lSessionId;
1570 }
1571 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001572 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001573
Glenn Kasten1879fff2012-07-11 15:36:59 -07001574 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001575 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001576 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001577 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001578
1579 if (lStatus == NO_ERROR) {
1580 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1581 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001582 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001583 if (chain != 0) {
1584 Mutex::Autolock _l(thread->mLock);
1585 thread->addEffectChain_l(chain);
1586 }
1587 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001588 }
Glenn Kastene198c362013-08-13 09:13:36 -07001589
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001590 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001591 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001592 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001593 // Don't hold mClientLock when releasing the reference on the track as the
1594 // destructor will acquire it.
1595 {
1596 Mutex::Autolock _cl(mClientLock);
1597 client.clear();
1598 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001599 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600 goto Exit;
1601 }
1602
Glenn Kastend776ac62014-05-07 09:16:09 -07001603 cblk = recordTrack->getCblk();
1604 buffers = recordTrack->getBuffers();
1605
Glenn Kasten2fc14732013-08-05 14:58:14 -07001606 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001607 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001608
1609Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001610 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001611 return recordHandle;
1612}
1613
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001614
1615
Mathias Agopian65ab4712010-07-14 17:59:35 -07001616// ----------------------------------------------------------------------------
1617
Eric Laurenta4c5a552012-03-29 10:12:40 -07001618audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1619{
Eric Laurent44622db2014-08-01 19:00:33 -07001620 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001621 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001622 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001623 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001624 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001625 }
1626 Mutex::Autolock _l(mLock);
1627 return loadHwModule_l(name);
1628}
1629
1630// loadHwModule_l() must be called with AudioFlinger::mLock held
1631audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1632{
1633 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1634 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1635 ALOGW("loadHwModule() module %s already loaded", name);
1636 return mAudioHwDevs.keyAt(i);
1637 }
1638 }
1639
Eric Laurenta4c5a552012-03-29 10:12:40 -07001640 audio_hw_device_t *dev;
1641
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001642 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001643 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001644 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001645 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001646 }
1647
1648 mHardwareStatus = AUDIO_HW_INIT;
1649 rc = dev->init_check(dev);
1650 mHardwareStatus = AUDIO_HW_IDLE;
1651 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001652 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001653 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001654 }
1655
John Grossmanee578c02012-07-23 17:05:46 -07001656 // Check and cache this HAL's level of support for master mute and master
1657 // volume. If this is the first HAL opened, and it supports the get
1658 // methods, use the initial values provided by the HAL as the current
1659 // master mute and volume settings.
1660
1661 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1662 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001663 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001664
1665 if (0 == mAudioHwDevs.size()) {
1666 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1667 if (NULL != dev->get_master_volume) {
1668 float mv;
1669 if (OK == dev->get_master_volume(dev, &mv)) {
1670 mMasterVolume = mv;
1671 }
1672 }
1673
1674 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1675 if (NULL != dev->get_master_mute) {
1676 bool mm;
1677 if (OK == dev->get_master_mute(dev, &mm)) {
1678 mMasterMute = mm;
1679 }
1680 }
1681 }
1682
Eric Laurenta4c5a552012-03-29 10:12:40 -07001683 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001684 if ((NULL != dev->set_master_volume) &&
1685 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1686 flags = static_cast<AudioHwDevice::Flags>(flags |
1687 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1688 }
1689
1690 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1691 if ((NULL != dev->set_master_mute) &&
1692 (OK == dev->set_master_mute(dev, mMasterMute))) {
1693 flags = static_cast<AudioHwDevice::Flags>(flags |
1694 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1695 }
1696
Eric Laurenta4c5a552012-03-29 10:12:40 -07001697 mHardwareStatus = AUDIO_HW_IDLE;
1698 }
1699
Glenn Kastena13cde92016-03-28 15:26:02 -07001700 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001701 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001702
1703 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001704 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001705
1706 return handle;
1707
1708}
1709
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001710// ----------------------------------------------------------------------------
1711
Glenn Kasten3b16c762012-11-14 08:44:39 -08001712uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001713{
1714 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001715 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001716 return thread != NULL ? thread->sampleRate() : 0;
1717}
1718
Glenn Kastene33054e2012-11-14 12:54:39 -08001719size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001720{
1721 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001722 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001723 return thread != NULL ? thread->frameCountHAL() : 0;
1724}
1725
1726// ----------------------------------------------------------------------------
1727
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001728status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1729{
1730 uid_t uid = IPCThreadState::self()->getCallingUid();
1731 if (uid != AID_SYSTEM) {
1732 return PERMISSION_DENIED;
1733 }
1734 Mutex::Autolock _l(mLock);
1735 if (mIsDeviceTypeKnown) {
1736 return INVALID_OPERATION;
1737 }
1738 mIsLowRamDevice = isLowRamDevice;
1739 mIsDeviceTypeKnown = true;
1740 return NO_ERROR;
1741}
1742
Eric Laurent93c3d412014-08-01 14:48:35 -07001743audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1744{
1745 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001746
1747 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1748 if (index >= 0) {
1749 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1750 mHwAvSyncIds.valueAt(index), sessionId);
1751 return mHwAvSyncIds.valueAt(index);
1752 }
1753
1754 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1755 if (dev == NULL) {
1756 return AUDIO_HW_SYNC_INVALID;
1757 }
1758 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1759 AudioParameter param = AudioParameter(String8(reply));
1760 free(reply);
1761
1762 int value;
1763 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1764 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1765 return AUDIO_HW_SYNC_INVALID;
1766 }
1767
1768 // allow only one session for a given HW A/V sync ID.
1769 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1770 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1771 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1772 value, mHwAvSyncIds.keyAt(i));
1773 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001774 break;
1775 }
1776 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001777
1778 mHwAvSyncIds.add(sessionId, value);
1779
1780 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1781 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1782 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07001783 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001784 AudioParameter param = AudioParameter();
1785 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1786 thread->setParameters(param.toString());
1787 break;
1788 }
1789 }
1790
1791 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1792 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001793}
1794
Eric Laurent72e3f392015-05-20 14:43:50 -07001795status_t AudioFlinger::systemReady()
1796{
1797 Mutex::Autolock _l(mLock);
1798 ALOGI("%s", __FUNCTION__);
1799 if (mSystemReady) {
1800 ALOGW("%s called twice", __FUNCTION__);
1801 return NO_ERROR;
1802 }
1803 mSystemReady = true;
1804 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1805 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1806 thread->systemReady();
1807 }
1808 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1809 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1810 thread->systemReady();
1811 }
1812 return NO_ERROR;
1813}
1814
Eric Laurentfa90e842014-10-17 18:12:31 -07001815// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1816void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1817{
1818 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1819 if (index >= 0) {
1820 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1821 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1822 AudioParameter param = AudioParameter();
1823 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1824 thread->setParameters(param.toString());
1825 }
1826}
1827
1828
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001829// ----------------------------------------------------------------------------
1830
Eric Laurent83b88082014-06-20 18:31:16 -07001831
1832sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001833 audio_io_handle_t *output,
1834 audio_config_t *config,
1835 audio_devices_t devices,
1836 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001837 audio_output_flags_t flags)
1838{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001839 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001840 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001841 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001842 }
1843
Eric Laurentcf2c0212014-07-25 16:20:43 -07001844 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001845 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1846 } else {
1847 // Audio Policy does not currently request a specific output handle.
1848 // If this is ever needed, see openInput_l() for example code.
1849 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1850 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001851 }
Eric Laurent83b88082014-06-20 18:31:16 -07001852
1853 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1854
Eric Laurent83b88082014-06-20 18:31:16 -07001855 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001856 // This if statement allows overriding the audio policy settings
1857 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1858 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1859 // Check only for Normal Mixing mode
1860 if (kEnableExtendedPrecision) {
1861 // Specify format (uncomment one below to choose)
1862 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1863 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1864 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1865 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001866 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001867 }
1868 if (kEnableExtendedChannels) {
1869 // Specify channel mask (uncomment one below to choose)
1870 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1871 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1872 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1873 }
Eric Laurent83b88082014-06-20 18:31:16 -07001874 }
1875
Phil Burk062e67a2015-02-11 13:40:50 -08001876 AudioStreamOut *outputStream = NULL;
1877 status_t status = outHwDev->openOutputStream(
1878 &outputStream,
1879 *output,
1880 devices,
1881 flags,
1882 config,
1883 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001884
1885 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001886
Phil Burk062e67a2015-02-11 13:40:50 -08001887 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001888
1889 PlaybackThread *thread;
1890 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurente93cc032016-05-05 10:15:10 -07001891 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001892 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001893 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1894 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001895 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001896 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001897 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001898 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001899 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001900 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001901 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001902 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001903 return thread;
1904 }
1905
1906 return 0;
1907}
1908
Eric Laurentcf2c0212014-07-25 16:20:43 -07001909status_t AudioFlinger::openOutput(audio_module_handle_t module,
1910 audio_io_handle_t *output,
1911 audio_config_t *config,
1912 audio_devices_t *devices,
1913 const String8& address,
1914 uint32_t *latencyMs,
1915 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001916{
Phil Burk062e67a2015-02-11 13:40:50 -08001917 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001918 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001919 (devices != NULL) ? *devices : 0,
1920 config->sample_rate,
1921 config->format,
1922 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001923 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001924
Eric Laurentcf2c0212014-07-25 16:20:43 -07001925 if (*devices == AUDIO_DEVICE_NONE) {
1926 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001927 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001928
Mathias Agopian65ab4712010-07-14 17:59:35 -07001929 Mutex::Autolock _l(mLock);
1930
Eric Laurentcf2c0212014-07-25 16:20:43 -07001931 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001932 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001933 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001934
1935 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001936 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001937
1938 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001939 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001940 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001941 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001942
1943 AutoMutex lock(mHardwareLock);
1944 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001945 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001946 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001947 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001948 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001949 }
1950
Eric Laurentcf2c0212014-07-25 16:20:43 -07001951 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001952}
1953
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001954audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1955 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001956{
1957 Mutex::Autolock _l(mLock);
1958 MixerThread *thread1 = checkMixerThread_l(output1);
1959 MixerThread *thread2 = checkMixerThread_l(output2);
1960
1961 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001962 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1963 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001964 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001965 }
1966
Glenn Kasteneeecb982016-02-26 10:44:04 -08001967 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07001968 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001969 thread->addOutputTrack(thread2);
1970 mPlaybackThreads.add(id, thread);
1971 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001972 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001973 return id;
1974}
1975
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001976status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001977{
Glenn Kastend96c5722012-04-25 13:44:49 -07001978 return closeOutput_nonvirtual(output);
1979}
1980
1981status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1982{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001983 // keep strong reference on the playback thread so that
1984 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001985 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001986 {
1987 Mutex::Autolock _l(mLock);
1988 thread = checkPlaybackThread_l(output);
1989 if (thread == NULL) {
1990 return BAD_VALUE;
1991 }
1992
Steve Block3856b092011-10-20 11:56:00 +01001993 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001994
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001995 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001996 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001997 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001998 DuplicatingThread *dupThread =
1999 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002000 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08002001 }
2002 }
2003 }
2004
2005
2006 mPlaybackThreads.removeItem(output);
2007 // save all effects to the default thread
2008 if (mPlaybackThreads.size()) {
2009 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
2010 if (dstThread != NULL) {
2011 // audioflinger lock is held here so the acquisition order of thread locks does not
2012 // matter
2013 Mutex::Autolock _dl(dstThread->mLock);
2014 Mutex::Autolock _sl(thread->mLock);
2015 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
2016 for (size_t i = 0; i < effectChains.size(); i ++) {
2017 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002018 }
2019 }
2020 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002021 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2022 ioDesc->mIoHandle = output;
2023 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024 }
2025 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08002026 // The thread entity (active unit of execution) is no longer running here,
2027 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002028
Eric Laurentf6870ae2015-05-08 10:50:03 -07002029 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07002030 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002031 }
Eric Laurent83b88082014-06-20 18:31:16 -07002032
Mathias Agopian65ab4712010-07-14 17:59:35 -07002033 return NO_ERROR;
2034}
2035
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002036void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002037{
2038 AudioStreamOut *out = thread->clearOutput();
2039 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2040 // from now on thread->mOutput is NULL
2041 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
2042 delete out;
2043}
2044
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002045void AudioFlinger::closeOutputInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002046{
2047 mPlaybackThreads.removeItem(thread->mId);
2048 thread->exit();
2049 closeOutputFinish(thread);
2050}
2051
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002052status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002053{
2054 Mutex::Autolock _l(mLock);
2055 PlaybackThread *thread = checkPlaybackThread_l(output);
2056
2057 if (thread == NULL) {
2058 return BAD_VALUE;
2059 }
2060
Steve Block3856b092011-10-20 11:56:00 +01002061 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002062 thread->suspend();
2063
2064 return NO_ERROR;
2065}
2066
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002067status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002068{
2069 Mutex::Autolock _l(mLock);
2070 PlaybackThread *thread = checkPlaybackThread_l(output);
2071
2072 if (thread == NULL) {
2073 return BAD_VALUE;
2074 }
2075
Steve Block3856b092011-10-20 11:56:00 +01002076 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002077
2078 thread->restore();
2079
2080 return NO_ERROR;
2081}
2082
Eric Laurentcf2c0212014-07-25 16:20:43 -07002083status_t AudioFlinger::openInput(audio_module_handle_t module,
2084 audio_io_handle_t *input,
2085 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002086 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002087 const String8& address,
2088 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002089 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002090{
Eric Laurent83b88082014-06-20 18:31:16 -07002091 Mutex::Autolock _l(mLock);
2092
Glenn Kastene7d66712015-03-05 13:46:52 -08002093 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002094 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002095 }
2096
Glenn Kastene7d66712015-03-05 13:46:52 -08002097 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002098
2099 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002100 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002101 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002102 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002103 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002104 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002105}
Dima Zavin799a70e2011-04-18 16:57:27 -07002106
Eric Laurent83b88082014-06-20 18:31:16 -07002107sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002108 audio_io_handle_t *input,
2109 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002110 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002111 const String8& address,
2112 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002113 audio_input_flags_t flags)
2114{
Glenn Kastene7d66712015-03-05 13:46:52 -08002115 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002116 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002117 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002118 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002119 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002120
Glenn Kasteneeecb982016-02-26 10:44:04 -08002121 // Audio Policy can request a specific handle for hardware hotword.
2122 // The goal here is not to re-open an already opened input.
2123 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002124 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002125 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2126 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2127 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2128 return 0;
2129 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2130 // This should not happen in a transient state with current design.
2131 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2132 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002133 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002134
Eric Laurentcf2c0212014-07-25 16:20:43 -07002135 audio_config_t halconfig = *config;
2136 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002137 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002138 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002139 &inStream, flags, address.string(), source);
2140 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002141 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002142 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002143 halconfig.sample_rate,
2144 halconfig.format,
2145 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002146 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002147 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002148
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002149 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002150 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002151 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002152 audio_is_linear_pcm(config->format) &&
2153 audio_is_linear_pcm(halconfig.format) &&
2154 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002155 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2156 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002157 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002158 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002159 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002160 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002161 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002162 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002163 }
2164
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002165 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002166
Glenn Kasten46909e72013-02-26 09:20:22 -08002167#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002168 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2169 // or (re-)create if current Pipe is idle and does not match the new format
2170 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002171 enum {
2172 TEE_SINK_NO, // don't copy input
2173 TEE_SINK_NEW, // copy input using a new pipe
2174 TEE_SINK_OLD, // copy input using an existing pipe
2175 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002176 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2177 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002178 if (!mTeeSinkInputEnabled) {
2179 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002180 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002181 kind = TEE_SINK_NO;
2182 } else if (mRecordTeeSink == 0) {
2183 kind = TEE_SINK_NEW;
2184 } else if (mRecordTeeSink->getStrongCount() != 1) {
2185 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002186 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002187 kind = TEE_SINK_OLD;
2188 } else {
2189 kind = TEE_SINK_NEW;
2190 }
2191 switch (kind) {
2192 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002193 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002194 size_t numCounterOffers = 0;
2195 const NBAIO_Format offers[1] = {format};
2196 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2197 ALOG_ASSERT(index == 0);
2198 PipeReader *pipeReader = new PipeReader(*pipe);
2199 numCounterOffers = 0;
2200 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2201 ALOG_ASSERT(index == 0);
2202 mRecordTeeSink = pipe;
2203 mRecordTeeSource = pipeReader;
2204 teeSink = pipe;
2205 }
2206 break;
2207 case TEE_SINK_OLD:
2208 teeSink = mRecordTeeSink;
2209 break;
2210 case TEE_SINK_NO:
2211 default:
2212 break;
2213 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002214#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002215
Eric Laurent05067782016-06-01 18:27:28 -07002216 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07002217
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002218 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002219 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002220 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002221 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002222 inputStream,
2223 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002224 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002225 devices,
2226 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002227#ifdef TEE_SINK
2228 , teeSink
2229#endif
2230 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002231 mRecordThreads.add(*input, thread);
2232 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002233 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002234 }
2235
Eric Laurentcf2c0212014-07-25 16:20:43 -07002236 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237 return 0;
2238}
2239
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002240status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002241{
Glenn Kastend96c5722012-04-25 13:44:49 -07002242 return closeInput_nonvirtual(input);
2243}
2244
2245status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2246{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002247 // keep strong reference on the record thread so that
2248 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002249 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002250 {
2251 Mutex::Autolock _l(mLock);
2252 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002253 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254 return BAD_VALUE;
2255 }
2256
Steve Block3856b092011-10-20 11:56:00 +01002257 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002258
2259 // If we still have effect chains, it means that a client still holds a handle
2260 // on at least one effect. We must either move the chain to an existing thread with the
2261 // same session ID or put it aside in case a new record thread is opened for a
2262 // new capture on the same session
2263 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002264 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002265 Mutex::Autolock _sl(thread->mLock);
2266 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002267 // Note: maximum one chain per record thread
2268 if (effectChains.size() != 0) {
2269 chain = effectChains[0];
2270 }
2271 }
2272 if (chain != 0) {
2273 // first check if a record thread is already opened with a client on the same session.
2274 // This should only happen in case of overlap between one thread tear down and the
2275 // creation of its replacement
2276 size_t i;
2277 for (i = 0; i < mRecordThreads.size(); i++) {
2278 sp<RecordThread> t = mRecordThreads.valueAt(i);
2279 if (t == thread) {
2280 continue;
2281 }
2282 if (t->hasAudioSession(chain->sessionId()) != 0) {
2283 Mutex::Autolock _l(t->mLock);
2284 ALOGV("closeInput() found thread %d for effect session %d",
2285 t->id(), chain->sessionId());
2286 t->addEffectChain_l(chain);
2287 break;
2288 }
2289 }
2290 // put the chain aside if we could not find a record thread with the same session id.
2291 if (i == mRecordThreads.size()) {
2292 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002293 }
2294 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002295 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2296 ioDesc->mIoHandle = input;
2297 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002298 mRecordThreads.removeItem(input);
2299 }
Eric Laurent83b88082014-06-20 18:31:16 -07002300 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2301 // we have a different lock for notification client
2302 closeInputFinish(thread);
2303 return NO_ERROR;
2304}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002305
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002306void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002307{
2308 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002309 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002310 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002311 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002312 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002313 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002314}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002315
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002316void AudioFlinger::closeInputInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002317{
2318 mRecordThreads.removeItem(thread->mId);
2319 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002320}
2321
Glenn Kastend2304db2014-02-03 07:40:31 -08002322status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002323{
2324 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002325 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002326
2327 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2328 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002329 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002330 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002331
2332 return NO_ERROR;
2333}
2334
2335
Glenn Kasteneeecb982016-02-26 10:44:04 -08002336audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002337{
Glenn Kasten9d003132016-04-06 14:38:09 -07002338 // This is a binder API, so a malicious client could pass in a bad parameter.
2339 // Check for that before calling the internal API nextUniqueId().
2340 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2341 ALOGE("newAudioUniqueId invalid use %d", use);
2342 return AUDIO_UNIQUE_ID_ALLOCATE;
2343 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002344 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002345}
2346
Glenn Kastend848eb42016-03-08 13:42:11 -08002347void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002348{
2349 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002350 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002351 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2352 if (pid != -1 && (caller == getpid_cached)) {
2353 caller = pid;
2354 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002355
Eric Laurent021cf962014-05-13 10:18:14 -07002356 {
2357 Mutex::Autolock _cl(mClientLock);
2358 // Ignore requests received from processes not known as notification client. The request
2359 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2360 // called from a different pid leaving a stale session reference. Also we don't know how
2361 // to clear this reference if the client process dies.
2362 if (mNotificationClients.indexOfKey(caller) < 0) {
2363 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2364 return;
2365 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002366 }
2367
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002368 size_t num = mAudioSessionRefs.size();
2369 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002370 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002371 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2372 ref->mCnt++;
2373 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002374 return;
2375 }
2376 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002377 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2378 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002379}
2380
Glenn Kastend848eb42016-03-08 13:42:11 -08002381void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002382{
2383 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002384 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002385 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2386 if (pid != -1 && (caller == getpid_cached)) {
2387 caller = pid;
2388 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002389 size_t num = mAudioSessionRefs.size();
2390 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002391 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002392 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2393 ref->mCnt--;
2394 ALOGV(" decremented refcount to %d", ref->mCnt);
2395 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002396 mAudioSessionRefs.removeAt(i);
2397 delete ref;
2398 purgeStaleEffects_l();
2399 }
2400 return;
2401 }
2402 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002403 // If the caller is mediaserver it is likely that the session being released was acquired
2404 // on behalf of a process not in notification clients and we ignore the warning.
2405 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002406}
2407
2408void AudioFlinger::purgeStaleEffects_l() {
2409
Steve Block3856b092011-10-20 11:56:00 +01002410 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002411
2412 Vector< sp<EffectChain> > chains;
2413
2414 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2415 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2416 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2417 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002418 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2419 chains.push(ec);
2420 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002421 }
2422 }
2423 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2424 sp<RecordThread> t = mRecordThreads.valueAt(i);
2425 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2426 sp<EffectChain> ec = t->mEffectChains[j];
2427 chains.push(ec);
2428 }
2429 }
2430
2431 for (size_t i = 0; i < chains.size(); i++) {
2432 sp<EffectChain> ec = chains[i];
2433 int sessionid = ec->sessionId();
2434 sp<ThreadBase> t = ec->mThread.promote();
2435 if (t == 0) {
2436 continue;
2437 }
2438 size_t numsessionrefs = mAudioSessionRefs.size();
2439 bool found = false;
2440 for (size_t k = 0; k < numsessionrefs; k++) {
2441 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002442 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002443 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002444 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002445 found = true;
2446 break;
2447 }
2448 }
2449 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002450 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002451 // remove all effects from the chain
2452 while (ec->mEffects.size()) {
2453 sp<EffectModule> effect = ec->mEffects[0];
2454 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002455 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002456 if (effect->purgeHandles()) {
2457 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002458 }
2459 AudioSystem::unregisterEffect(effect->id());
2460 }
2461 }
2462 }
2463 return;
2464}
2465
Glenn Kasteneeecb982016-02-26 10:44:04 -08002466// checkThread_l() must be called with AudioFlinger::mLock held
2467AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2468{
2469 ThreadBase *thread = NULL;
2470 switch (audio_unique_id_get_use(ioHandle)) {
2471 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2472 thread = checkPlaybackThread_l(ioHandle);
2473 break;
2474 case AUDIO_UNIQUE_ID_USE_INPUT:
2475 thread = checkRecordThread_l(ioHandle);
2476 break;
2477 default:
2478 break;
2479 }
2480 return thread;
2481}
2482
Mathias Agopian65ab4712010-07-14 17:59:35 -07002483// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002484AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002485{
Glenn Kastena1117922012-01-26 10:53:32 -08002486 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002487}
2488
2489// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002490AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002491{
2492 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002493 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002494}
2495
2496// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002497AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002498{
Glenn Kastena1117922012-01-26 10:53:32 -08002499 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002500}
2501
Glenn Kasteneeecb982016-02-26 10:44:04 -08002502audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002503{
Glenn Kasten9d003132016-04-06 14:38:09 -07002504 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002505 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002506 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2507 for (int retry = 0; retry < maxRetries; retry++) {
2508 // The cast allows wraparound from max positive to min negative instead of abort
2509 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2510 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2511 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2512 // allow wrap by skipping 0 and -1 for session ids
2513 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2514 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2515 return (audio_unique_id_t) (base | use);
2516 }
2517 }
2518 // We have no way of recovering from wraparound
2519 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2520 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002521}
2522
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002523AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002524{
2525 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2526 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002527 if(thread->isDuplicating()) {
2528 continue;
2529 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002530 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002531 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002532 return thread;
2533 }
2534 }
2535 return NULL;
2536}
2537
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002538audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002539{
2540 PlaybackThread *thread = primaryPlaybackThread_l();
2541
2542 if (thread == NULL) {
2543 return 0;
2544 }
2545
Eric Laurentf1c04f92012-08-28 14:26:53 -07002546 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002547}
2548
Glenn Kastena7335632016-06-09 17:09:53 -07002549AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
2550{
2551 size_t minFrameCount = 0;
2552 PlaybackThread *minThread = NULL;
2553 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2554 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2555 if (!thread->isDuplicating()) {
2556 size_t frameCount = thread->frameCountHAL();
2557 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
2558 (frameCount == minFrameCount && thread->hasFastMixer() &&
2559 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
2560 minFrameCount = frameCount;
2561 minThread = thread;
2562 }
2563 }
2564 }
2565 return minThread;
2566}
2567
Eric Laurenta011e352012-03-29 15:51:43 -07002568sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002569 audio_session_t triggerSession,
2570 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002571 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002572 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002573{
2574 Mutex::Autolock _l(mLock);
2575
2576 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2577 status_t playStatus = NAME_NOT_FOUND;
2578 status_t recStatus = NAME_NOT_FOUND;
2579 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2580 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2581 if (playStatus == NO_ERROR) {
2582 return event;
2583 }
2584 }
2585 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2586 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2587 if (recStatus == NO_ERROR) {
2588 return event;
2589 }
2590 }
2591 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2592 mPendingSyncEvents.add(event);
2593 } else {
2594 ALOGV("createSyncEvent() invalid event %d", event->type());
2595 event.clear();
2596 }
2597 return event;
2598}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002599
Mathias Agopian65ab4712010-07-14 17:59:35 -07002600// ----------------------------------------------------------------------------
2601// Effect management
2602// ----------------------------------------------------------------------------
2603
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002604sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
2605 return mEffectsFactoryHal;
2606}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002607
Glenn Kastenf587ba52012-01-26 16:25:10 -08002608status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002609{
2610 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002611 if (mEffectsFactoryHal.get()) {
2612 return mEffectsFactoryHal->queryNumberEffects(numEffects);
2613 } else {
2614 return -ENODEV;
2615 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002616}
2617
Glenn Kastenf587ba52012-01-26 16:25:10 -08002618status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002619{
2620 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002621 if (mEffectsFactoryHal.get()) {
2622 return mEffectsFactoryHal->getDescriptor(index, descriptor);
2623 } else {
2624 return -ENODEV;
2625 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002626}
2627
Glenn Kasten5e92a782012-01-30 07:40:52 -08002628status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002629 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002630{
2631 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002632 if (mEffectsFactoryHal.get()) {
2633 return mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
2634 } else {
2635 return -ENODEV;
2636 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002637}
2638
2639
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002640sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002641 effect_descriptor_t *pDesc,
2642 const sp<IEffectClient>& effectClient,
2643 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002644 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002645 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002646 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002647 status_t *status,
2648 int *id,
2649 int *enabled)
2650{
2651 status_t lStatus = NO_ERROR;
2652 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002653 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002654
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002655 pid_t pid = IPCThreadState::self()->getCallingPid();
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002656 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
2657 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002658
2659 if (pDesc == NULL) {
2660 lStatus = BAD_VALUE;
2661 goto Exit;
2662 }
2663
Eric Laurent84e9a102010-09-23 16:10:16 -07002664 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002665 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002666 lStatus = PERMISSION_DENIED;
2667 goto Exit;
2668 }
2669
Dima Zavinfce7a472011-04-19 22:30:36 -07002670 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002671 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002672 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002673 lStatus = PERMISSION_DENIED;
2674 goto Exit;
2675 }
2676
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002677 if (mEffectsFactoryHal.get() == NULL) {
2678 lStatus = NO_INIT;
2679 goto Exit;
2680 }
2681
Mathias Agopian65ab4712010-07-14 17:59:35 -07002682 {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002683 if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002684 // if uuid is specified, request effect descriptor
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002685 lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002686 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002687 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002688 goto Exit;
2689 }
2690 } else {
2691 // if uuid is not specified, look for an available implementation
2692 // of the required type in effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002693 if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002694 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002695 lStatus = BAD_VALUE;
2696 goto Exit;
2697 }
2698 uint32_t numEffects = 0;
2699 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002700 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002701 bool found = false;
2702
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002703 lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002704 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002705 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002706 goto Exit;
2707 }
2708 for (uint32_t i = 0; i < numEffects; i++) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002709 lStatus = mEffectsFactoryHal->getDescriptor(i, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002710 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002711 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002712 continue;
2713 }
2714 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2715 // If matching type found save effect descriptor. If the session is
2716 // 0 and the effect is not auxiliary, continue enumeration in case
2717 // an auxiliary version of this effect type is available
2718 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002719 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002720 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002721 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2722 break;
2723 }
2724 }
2725 }
2726 if (!found) {
2727 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002728 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002729 goto Exit;
2730 }
2731 // For same effect type, chose auxiliary version over insert version if
2732 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002733 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002734 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002735 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002736 }
2737 }
2738
2739 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002740 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002741 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2742 lStatus = INVALID_OPERATION;
2743 goto Exit;
2744 }
2745
Eric Laurent59255e42011-07-27 19:49:51 -07002746 // check recording permission for visualizer
2747 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002748 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002749 lStatus = PERMISSION_DENIED;
2750 goto Exit;
2751 }
2752
Mathias Agopian65ab4712010-07-14 17:59:35 -07002753 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002754 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002755 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002756 // if the output returned by getOutputForEffect() is removed before we lock the
2757 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2758 // and we will exit safely
2759 io = AudioSystem::getOutputForEffect(&desc);
2760 ALOGV("createEffect got output %d", io);
2761 }
2762
2763 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002764
2765 // If output is not specified try to find a matching audio session ID in one of the
2766 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002767 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2768 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002769 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002770 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002771 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2772 // output must be specified by AudioPolicyManager when using session
2773 // AUDIO_SESSION_OUTPUT_STAGE
2774 lStatus = BAD_VALUE;
2775 goto Exit;
2776 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002777 // look for the thread where the specified audio session is present
2778 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2779 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2780 io = mPlaybackThreads.keyAt(i);
2781 break;
2782 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002783 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002784 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002785 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2786 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2787 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002788 break;
2789 }
2790 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002791 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002792 // If no output thread contains the requested session ID, default to
2793 // first output. The effect chain will be moved to the correct output
2794 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002795 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002796 io = mPlaybackThreads.keyAt(0);
2797 }
Steve Block3856b092011-10-20 11:56:00 +01002798 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002799 }
2800 ThreadBase *thread = checkRecordThread_l(io);
2801 if (thread == NULL) {
2802 thread = checkPlaybackThread_l(io);
2803 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002804 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002805 lStatus = BAD_VALUE;
2806 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002807 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002808 } else {
2809 // Check if one effect chain was awaiting for an effect to be created on this
2810 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08002811 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07002812 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002813 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002814 thread->addEffectChain_l(chain);
2815 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002816 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002817
Eric Laurent021cf962014-05-13 10:18:14 -07002818 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002819
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002820 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002821 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2822 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002823 if (handle != 0 && id != NULL) {
2824 *id = handle->id();
2825 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002826 if (handle == 0) {
2827 // remove local strong reference to Client with mClientLock held
2828 Mutex::Autolock _cl(mClientLock);
2829 client.clear();
2830 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002831 }
2832
2833Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002834 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002835 return handle;
2836}
2837
Glenn Kastend848eb42016-03-08 13:42:11 -08002838status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002839 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002840{
Steve Block3856b092011-10-20 11:56:00 +01002841 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002842 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002843 Mutex::Autolock _l(mLock);
2844 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002845 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002846 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002847 }
Eric Laurentde070132010-07-13 04:45:46 -07002848 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2849 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002850 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002851 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002852 }
Eric Laurentde070132010-07-13 04:45:46 -07002853 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2854 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002855 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002856 return BAD_VALUE;
2857 }
2858
2859 Mutex::Autolock _dl(dstThread->mLock);
2860 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002861 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002862}
2863
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002864// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08002865status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002866 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002867 AudioFlinger::PlaybackThread *dstThread,
2868 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002869{
Steve Block3856b092011-10-20 11:56:00 +01002870 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002871 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002872
Eric Laurent59255e42011-07-27 19:49:51 -07002873 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002874 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002875 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002876 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002877 return INVALID_OPERATION;
2878 }
2879
Eric Laurent4c415062016-06-17 16:14:16 -07002880 // Check whether the destination thread and all effects in the chain are compatible
2881 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07002882 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07002883 " destination thread %p is not compatible with effects in the chain",
2884 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07002885 return INVALID_OPERATION;
2886 }
2887
Eric Laurent39e94f82010-07-28 01:32:47 -07002888 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002889 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002890 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002891 // removed.
2892 srcThread->removeEffectChain_l(chain);
2893
2894 // transfer all effects one by one so that new effect chain is created on new thread with
2895 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002896 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002897 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002898 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002899 Vector< sp<EffectModule> > removed;
2900 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002901 while (effect != 0) {
2902 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002903 removed.add(effect);
2904 status = dstThread->addEffect_l(effect);
2905 if (status != NO_ERROR) {
2906 break;
2907 }
Eric Laurentec35a142011-10-05 17:42:25 -07002908 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2909 if (effect->state() == EffectModule::ACTIVE ||
2910 effect->state() == EffectModule::STOPPING) {
2911 effect->start();
2912 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002913 // if the move request is not received from audio policy manager, the effect must be
2914 // re-registered with the new strategy and output
2915 if (dstChain == 0) {
2916 dstChain = effect->chain().promote();
2917 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002918 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002919 status = NO_INIT;
2920 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002921 }
2922 strategy = dstChain->strategy();
2923 }
2924 if (reRegister) {
2925 AudioSystem::unregisterEffect(effect->id());
2926 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002927 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002928 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002929 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002930 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002931 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002932 }
Eric Laurentde070132010-07-13 04:45:46 -07002933 effect = chain->getEffectFromId_l(0);
2934 }
2935
Eric Laurent5baf2af2013-09-12 17:37:00 -07002936 if (status != NO_ERROR) {
2937 for (size_t i = 0; i < removed.size(); i++) {
2938 srcThread->addEffect_l(removed[i]);
2939 if (dstChain != 0 && reRegister) {
2940 AudioSystem::unregisterEffect(removed[i]->id());
2941 AudioSystem::registerEffect(&removed[i]->desc(),
2942 srcThread->id(),
2943 strategy,
2944 sessionId,
2945 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002946 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002947 }
2948 }
2949 }
2950
2951 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002952}
2953
Eric Laurent5baf2af2013-09-12 17:37:00 -07002954bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002955{
2956 if (mGlobalEffectEnableTime != 0 &&
2957 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2958 return true;
2959 }
2960
2961 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2962 sp<EffectChain> ec =
2963 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002964 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002965 return true;
2966 }
2967 }
2968 return false;
2969}
2970
Eric Laurent5baf2af2013-09-12 17:37:00 -07002971void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002972{
2973 Mutex::Autolock _l(mLock);
2974
2975 mGlobalEffectEnableTime = systemTime();
2976
2977 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2978 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2979 if (t->mType == ThreadBase::OFFLOAD) {
2980 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2981 }
2982 }
2983
2984}
2985
Eric Laurentaaa44472014-09-12 17:41:50 -07002986status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2987{
Glenn Kastend848eb42016-03-08 13:42:11 -08002988 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002989 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002990 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002991 if (index >= 0) {
2992 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2993 return ALREADY_EXISTS;
2994 }
2995 mOrphanEffectChains.add(session, chain);
2996 return NO_ERROR;
2997}
2998
2999sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
3000{
3001 sp<EffectChain> chain;
3002 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003003 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003004 if (index >= 0) {
3005 chain = mOrphanEffectChains.valueAt(index);
3006 mOrphanEffectChains.removeItemsAt(index);
3007 }
3008 return chain;
3009}
3010
3011bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
3012{
3013 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08003014 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003015 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003016 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003017 if (index >= 0) {
3018 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
3019 if (chain->removeEffect_l(effect) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003020 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003021 mOrphanEffectChains.removeItemsAt(index);
3022 }
3023 return true;
3024 }
3025 return false;
3026}
3027
3028
Glenn Kastenda6ef132013-01-10 12:31:01 -08003029struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003030#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
3031 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003032};
3033
3034int comparEntry(const void *p1, const void *p2)
3035{
Glenn Kasten2f55e762015-03-05 16:05:08 -08003036 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003037}
3038
Glenn Kasten46909e72013-02-26 09:20:22 -08003039#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08003040void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003041{
Eric Laurent81784c32012-11-19 14:55:58 -08003042 NBAIO_Source *teeSource = source.get();
3043 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003044 // .wav rotation
3045 // There is a benign race condition if 2 threads call this simultaneously.
3046 // They would both traverse the directory, but the result would simply be
3047 // failures at unlink() which are ignored. It's also unlikely since
3048 // normally dumpsys is only done by bugreport or from the command line.
3049 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08003050 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08003051 size_t teePathLen = strlen(teePath);
3052 DIR *dir = opendir(teePath);
3053 teePath[teePathLen++] = '/';
3054 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003055#define TEE_MAX_SORT 20 // number of entries to sort
3056#define TEE_MAX_KEEP 10 // number of entries to keep
3057 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003058 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08003059 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003060 struct dirent de;
3061 struct dirent *result = NULL;
3062 int rc = readdir_r(dir, &de, &result);
3063 if (rc != 0) {
3064 ALOGW("readdir_r failed %d", rc);
3065 break;
3066 }
3067 if (result == NULL) {
3068 break;
3069 }
3070 if (result != &de) {
3071 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
3072 break;
3073 }
3074 // ignore non .wav file entries
3075 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003076 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08003077 strcmp(&de.d_name[nameLen - 4], ".wav")) {
3078 continue;
3079 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08003080 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003081 }
3082 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003083 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003084 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003085 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
3086 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003087 (void) unlink(teePath);
3088 }
3089 }
3090 } else {
3091 if (fd >= 0) {
Glenn Kastenfbd87e82016-07-18 15:45:56 -07003092 dprintf(fd, "unable to rotate tees in %.*s: %s\n", (int) teePathLen, teePath,
Glenn Kasten9a003992016-02-23 15:24:34 -08003093 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003094 }
3095 }
Eric Laurent81784c32012-11-19 14:55:58 -08003096 char teeTime[16];
3097 struct timeval tv;
3098 gettimeofday(&tv, NULL);
3099 struct tm tm;
3100 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003101 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
3102 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
3103 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3104 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003105 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003106 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003107 char wavHeader[44];
3108 memcpy(wavHeader,
3109 "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",
3110 sizeof(wavHeader));
3111 NBAIO_Format format = teeSource->format();
3112 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003113 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003114 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003115 wavHeader[22] = channelCount; // number of channels
3116 wavHeader[24] = sampleRate; // sample rate
3117 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003118 wavHeader[32] = frameSize; // block alignment
3119 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003120 write(teeFd, wavHeader, sizeof(wavHeader));
3121 size_t total = 0;
3122 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003123#define TEE_SINK_READ 1024 // frames per I/O operation
3124 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003125 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003126 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003127 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003128 bool wasFirstRead = firstRead;
3129 firstRead = false;
3130 if (actual <= 0) {
3131 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3132 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003133 }
Eric Laurent81784c32012-11-19 14:55:58 -08003134 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003135 }
Eric Laurent81784c32012-11-19 14:55:58 -08003136 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003137 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003138 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003139 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003140 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003141 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003142 uint32_t temp = 44 + total * frameSize - 8;
3143 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003144 write(teeFd, &temp, sizeof(temp));
3145 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003146 temp = total * frameSize;
3147 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003148 write(teeFd, &temp, sizeof(temp));
3149 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003150 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003151 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003152 }
Eric Laurent59255e42011-07-27 19:49:51 -07003153 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003154 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003155 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003156 }
Eric Laurent59255e42011-07-27 19:49:51 -07003157 }
3158 }
3159}
Glenn Kasten46909e72013-02-26 09:20:22 -08003160#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003161
Mathias Agopian65ab4712010-07-14 17:59:35 -07003162// ----------------------------------------------------------------------------
3163
3164status_t AudioFlinger::onTransact(
3165 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3166{
3167 return BnAudioFlinger::onTransact(code, data, reply, flags);
3168}
3169
Glenn Kasten63238ef2015-03-02 15:50:29 -08003170} // namespace android