blob: 0beb86cf9463d694d00d5b64296eb0f80a365308 [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>
Mikhail Naganova0c91332016-09-19 10:01:12 -070034#include <media/audiohal/DeviceHalInterface.h>
35#include <media/audiohal/DevicesFactoryHalInterface.h>
36#include <media/audiohal/EffectsFactoryHalInterface.h>
Mikhail Naganov00260b52016-10-13 12:54:24 -070037#include <media/AudioParameter.h>
Mikhail Naganov913d06c2016-11-01 12:49:22 -070038#include <media/TypeConverter.h>
Andy Hung35fec5f2016-04-13 14:21:48 -070039#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070040#include <utils/String16.h>
41#include <utils/threads.h>
42
Steven Morelandf0c02ce2018-02-23 14:53:55 -080043#include <cutils/atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070044#include <cutils/properties.h>
45
Dima Zavin64760242011-05-11 14:15:23 -070046#include <system/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include "AudioFlinger.h"
Andy Hung8946a282018-04-19 20:04:56 -070049#include "NBAIO_Tee.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
Andy Hung6770c6f2015-04-07 13:43:36 -070051#include <media/AudioResamplerPublic.h>
52
Mikhail Naganov9fe94012016-10-14 14:57:40 -070053#include <system/audio_effects/effect_visualizer.h>
54#include <system/audio_effects/effect_ns.h>
55#include <system/audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
Glenn Kasten3b21c502011-12-15 09:52:39 -080057#include <audio_utils/primitives.h>
58
Eric Laurentfeb0db62011-07-22 09:04:31 -070059#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080060
Glenn Kasten9e58b552013-01-18 15:09:48 -080061#include <media/IMediaLogService.h>
Andy Hung07b745e2016-05-23 16:21:07 -070062#include <media/MemoryLeakTrackUtil.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080063#include <media/nbaio/Pipe.h>
64#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070065#include <media/AudioParameter.h>
Wei Jia3f273d12015-11-24 09:06:49 -080066#include <mediautils/BatteryNotifier.h>
Andy Hungab7ef302018-05-15 19:35:29 -070067#include <mediautils/ServiceUtilities.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070068#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080069
rago3bd1c872016-09-26 12:58:14 -070070//#define BUFLOG_NDEBUG 0
71#include <BufLog.h>
72
Nicolas Rouletfe1e1442017-01-30 12:02:03 -080073#include "TypedLogger.h"
74
Mathias Agopian65ab4712010-07-14 17:59:35 -070075// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070076
John Grossman1c345192012-03-27 14:00:17 -070077// Note: the following macro is used for extremely verbose logging message. In
78// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
79// 0; but one side effect of this is to turn all LOGV's as well. Some messages
80// are so verbose that we want to suppress them even when we have ALOG_ASSERT
81// turned on. Do not uncomment the #def below unless you really know what you
82// are doing and want to see all of the extremely verbose messages.
83//#define VERY_VERY_VERBOSE_LOGGING
84#ifdef VERY_VERY_VERBOSE_LOGGING
85#define ALOGVV ALOGV
86#else
87#define ALOGVV(a...) do { } while(0)
88#endif
Eric Laurentde070132010-07-13 04:45:46 -070089
Mathias Agopian65ab4712010-07-14 17:59:35 -070090namespace android {
91
Glenn Kastenec1d6b52011-12-12 09:04:45 -080092static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
93static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070094static const char kClientLockedString[] = "Client lock is taken\n";
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070095static const char kNoEffectsFactory[] = "Effects Factory is absent\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070096
Glenn Kasten58912562012-04-03 10:45:00 -070097
John Grossman4ff14ba2012-02-08 16:37:41 -080098nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080099
Eric Laurent81784c32012-11-19 14:55:58 -0800100uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -0700101
Eric Laurent813e2a72013-08-31 12:59:48 -0700102// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
103// we define a minimum time during which a global effect is considered enabled.
104static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
105
Eric Laurentfc235202016-12-20 18:48:17 -0800106Mutex gLock;
107wp<AudioFlinger> gAudioFlinger;
108
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700109// Keep a strong reference to media.log service around forever.
110// The service is within our parent process so it can never die in a way that we could observe.
111// These two variables are const after initialization.
112static sp<IBinder> sMediaLogServiceAsBinder;
113static sp<IMediaLogService> sMediaLogService;
114
115static pthread_once_t sMediaLogOnce = PTHREAD_ONCE_INIT;
116
117static void sMediaLogInit()
118{
119 sMediaLogServiceAsBinder = defaultServiceManager()->getService(String16("media.log"));
120 if (sMediaLogServiceAsBinder != 0) {
121 sMediaLogService = interface_cast<IMediaLogService>(sMediaLogServiceAsBinder);
122 }
123}
124
Mathias Agopian65ab4712010-07-14 17:59:35 -0700125// ----------------------------------------------------------------------------
126
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700127std::string formatToString(audio_format_t format) {
128 std::string result;
129 FormatConverter::toString(format, result);
130 return result;
Marco Nelissenb2208842014-02-07 14:00:50 -0800131}
132
Mathias Agopian65ab4712010-07-14 17:59:35 -0700133// ----------------------------------------------------------------------------
134
135AudioFlinger::AudioFlinger()
136 : BnAudioFlinger(),
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -0800137 mMediaLogNotifier(new AudioFlinger::MediaLogNotifier()),
John Grossman4ff14ba2012-02-08 16:37:41 -0800138 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800139 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700140 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800141 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800142 mMasterMute(false),
Glenn Kastend2e67e12016-04-11 08:26:37 -0700143 // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),
John Grossman4ff14ba2012-02-08 16:37:41 -0800144 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700145 mBtNrecIsOff(false),
146 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700147 mIsDeviceTypeKnown(false),
Andy Hung6f248bb2018-01-23 14:04:37 -0800148 mTotalMemory(0),
149 mClientSharedHeapSize(kMinimumClientSharedHeapSizeBytes),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700150 mGlobalEffectEnableTime(0),
Mikhail Naganovdea53042018-04-26 13:10:21 -0700151 mPatchPanel(this),
Eric Laurent72e3f392015-05-20 14:43:50 -0700152 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700153{
Glenn Kastend2e67e12016-04-11 08:26:37 -0700154 // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum
155 for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) {
156 // zero ID has a special meaning, so unavailable
157 mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX;
158 }
159
Glenn Kastenae0cff12016-02-24 13:57:49 -0800160 const bool doLog = property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800161 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800162 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
163 MemoryHeapBase::READ_ONLY);
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700164 (void) pthread_once(&sMediaLogOnce, sMediaLogInit);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800165 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700166
Wei Jia3f273d12015-11-24 09:06:49 -0800167 // reset battery stats.
168 // if the audio service has crashed, battery stats could be left
169 // in bad state, reset the state upon service start.
170 BatteryNotifier::getInstance().noteResetAudio();
171
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700172 mDevicesFactoryHal = DevicesFactoryHalInterface::create();
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700173 mEffectsFactoryHal = EffectsFactoryHalInterface::create();
174
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -0800175 mMediaLogNotifier->run("MediaLogNotifier");
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700176}
177
178void AudioFlinger::onFirstRef()
179{
Eric Laurent93575202011-01-18 18:39:02 -0800180 Mutex::Autolock _l(mLock);
181
Dima Zavin799a70e2011-04-18 16:57:27 -0700182 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800183 char val_str[PROPERTY_VALUE_MAX] = { 0 };
184 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
185 uint32_t int_val;
186 if (1 == sscanf(val_str, "%u", &int_val)) {
187 mStandbyTimeInNsecs = milliseconds(int_val);
188 ALOGI("Using %u mSec as standby time.", int_val);
189 } else {
190 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
191 ALOGI("Using default %u mSec as standby time.",
192 (uint32_t)(mStandbyTimeInNsecs / 1000000));
193 }
194 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195
Eric Laurenta4c5a552012-03-29 10:12:40 -0700196 mMode = AUDIO_MODE_NORMAL;
Eric Laurentfc235202016-12-20 18:48:17 -0800197
198 gAudioFlinger = this;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700199}
200
201AudioFlinger::~AudioFlinger()
202{
203 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700204 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700205 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700206 }
207 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700208 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700209 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700210 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700211
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800212 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
213 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700214 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700215 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700216
217 // Tell media.log service about any old writers that still need to be unregistered
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700218 if (sMediaLogService != 0) {
219 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
220 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
221 mUnregisteredWriters.pop();
222 sMediaLogService->unregisterWriter(iMemory);
Glenn Kasten481fb672013-09-30 14:39:28 -0700223 }
224 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700225}
226
Eric Laurentfc235202016-12-20 18:48:17 -0800227//static
Eric Laurent6acd1d42017-01-04 14:23:29 -0800228__attribute__ ((visibility ("default")))
Eric Laurentfc235202016-12-20 18:48:17 -0800229status_t MmapStreamInterface::openMmapStream(MmapStreamInterface::stream_direction_t direction,
230 const audio_attributes_t *attr,
231 audio_config_base_t *config,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700232 const AudioClient& client,
Eric Laurentfc235202016-12-20 18:48:17 -0800233 audio_port_handle_t *deviceId,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800234 audio_session_t *sessionId,
Eric Laurentfc235202016-12-20 18:48:17 -0800235 const sp<MmapStreamCallback>& callback,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700236 sp<MmapStreamInterface>& interface,
237 audio_port_handle_t *handle)
Eric Laurentfc235202016-12-20 18:48:17 -0800238{
239 sp<AudioFlinger> af;
240 {
241 Mutex::Autolock _l(gLock);
242 af = gAudioFlinger.promote();
243 }
244 status_t ret = NO_INIT;
245 if (af != 0) {
246 ret = af->openMmapStream(
Phil Burk4e1af9f2018-01-03 15:54:35 -0800247 direction, attr, config, client, deviceId,
248 sessionId, callback, interface, handle);
Eric Laurentfc235202016-12-20 18:48:17 -0800249 }
250 return ret;
251}
252
Eric Laurent6acd1d42017-01-04 14:23:29 -0800253status_t AudioFlinger::openMmapStream(MmapStreamInterface::stream_direction_t direction,
254 const audio_attributes_t *attr,
255 audio_config_base_t *config,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700256 const AudioClient& client,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800257 audio_port_handle_t *deviceId,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800258 audio_session_t *sessionId,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800259 const sp<MmapStreamCallback>& callback,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700260 sp<MmapStreamInterface>& interface,
261 audio_port_handle_t *handle)
Eric Laurentfc235202016-12-20 18:48:17 -0800262{
263 status_t ret = initCheck();
264 if (ret != NO_ERROR) {
265 return ret;
266 }
Phil Burk4e1af9f2018-01-03 15:54:35 -0800267 audio_session_t actualSessionId = *sessionId;
268 if (actualSessionId == AUDIO_SESSION_ALLOCATE) {
269 actualSessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
270 }
Eric Laurent6acd1d42017-01-04 14:23:29 -0800271 audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700272 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800273 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
274 if (direction == MmapStreamInterface::DIRECTION_OUTPUT) {
275 audio_config_t fullConfig = AUDIO_CONFIG_INITIALIZER;
276 fullConfig.sample_rate = config->sample_rate;
277 fullConfig.channel_mask = config->channel_mask;
278 fullConfig.format = config->format;
279 ret = AudioSystem::getOutputForAttr(attr, &io,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800280 actualSessionId,
Nadav Bar766fb022018-01-07 12:18:03 +0200281 &streamType, client.clientPid, client.clientUid,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800282 &fullConfig,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800283 (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ |
284 AUDIO_OUTPUT_FLAG_DIRECT),
Eric Laurent2ac76942017-06-22 17:17:09 -0700285 deviceId, &portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800286 } else {
287 ret = AudioSystem::getInputForAttr(attr, &io,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800288 actualSessionId,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800289 client.clientPid,
290 client.clientUid,
Eric Laurentfee19762018-01-29 18:44:13 -0800291 client.packageName,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800292 config,
Eric Laurent2ac76942017-06-22 17:17:09 -0700293 AUDIO_INPUT_FLAG_MMAP_NOIRQ, deviceId, &portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800294 }
295 if (ret != NO_ERROR) {
296 return ret;
297 }
298
299 // at this stage, a MmapThread was created when openOutput() or openInput() was called by
300 // audio policy manager and we can retrieve it
301 sp<MmapThread> thread = mMmapThreads.valueFor(io);
302 if (thread != 0) {
303 interface = new MmapThreadHandle(thread);
Phil Burk4e1af9f2018-01-03 15:54:35 -0800304 thread->configure(attr, streamType, actualSessionId, callback, *deviceId, portId);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700305 *handle = portId;
Phil Burk4e1af9f2018-01-03 15:54:35 -0800306 *sessionId = actualSessionId;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800307 } else {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700308 if (direction == MmapStreamInterface::DIRECTION_OUTPUT) {
Phil Burk4e1af9f2018-01-03 15:54:35 -0800309 AudioSystem::releaseOutput(io, streamType, actualSessionId);
Eric Laurentad2e7b92017-09-14 20:06:42 -0700310 } else {
Eric Laurentfee19762018-01-29 18:44:13 -0800311 AudioSystem::releaseInput(portId);
Eric Laurentad2e7b92017-09-14 20:06:42 -0700312 }
Eric Laurent6acd1d42017-01-04 14:23:29 -0800313 ret = NO_INIT;
314 }
315
316 ALOGV("%s done status %d portId %d", __FUNCTION__, ret, portId);
317
Eric Laurentfc235202016-12-20 18:48:17 -0800318 return ret;
319}
320
Eric Laurenta4c5a552012-03-29 10:12:40 -0700321static const char * const audio_interfaces[] = {
322 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
323 AUDIO_HARDWARE_MODULE_ID_A2DP,
324 AUDIO_HARDWARE_MODULE_ID_USB,
325};
Eric Laurenta4c5a552012-03-29 10:12:40 -0700326
Phil Burk062e67a2015-02-11 13:40:50 -0800327AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700328 audio_module_handle_t module,
329 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700330{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700331 // if module is 0, the request comes from an old policy manager and we should load
332 // well known modules
333 if (module == 0) {
334 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
Mikhail Naganovbf493082017-04-17 17:37:12 -0700335 for (size_t i = 0; i < arraysize(audio_interfaces); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700336 loadHwModule_l(audio_interfaces[i]);
337 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700338 // then try to find a module supporting the requested device.
339 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
340 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700341 sp<DeviceHalInterface> dev = audioHwDevice->hwDevice();
342 uint32_t supportedDevices;
343 if (dev->getSupportedDevices(&supportedDevices) == OK &&
344 (supportedDevices & devices) == devices) {
Eric Laurentf1c04f92012-08-28 14:26:53 -0700345 return audioHwDevice;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700346 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700347 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700348 } else {
349 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700350 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
351 if (audioHwDevice != NULL) {
352 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700353 }
354 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700355
Dima Zavin799a70e2011-04-18 16:57:27 -0700356 return NULL;
357}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358
Glenn Kasten0f11b512014-01-31 16:18:54 -0800359void AudioFlinger::dumpClients(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;
364
365 result.append("Clients:\n");
366 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800367 sp<Client> client = mClients.valueAt(i).promote();
368 if (client != 0) {
369 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
370 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371 }
372 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700373
Eric Laurentd1b28d42013-09-18 18:47:13 -0700374 result.append("Notification Clients:\n");
375 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
376 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
377 result.append(buffer);
378 }
379
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700380 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800381 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700382 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
383 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800384 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700385 result.append(buffer);
386 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388}
389
390
Glenn Kasten0f11b512014-01-31 16:18:54 -0800391void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392{
393 const size_t SIZE = 256;
394 char buffer[SIZE];
395 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800396 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397
John Grossman4ff14ba2012-02-08 16:37:41 -0800398 snprintf(buffer, SIZE, "Hardware status: %d\n"
399 "Standby Time mSec: %u\n",
400 hardwareStatus,
401 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402 result.append(buffer);
403 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404}
405
Glenn Kasten0f11b512014-01-31 16:18:54 -0800406void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407{
408 const size_t SIZE = 256;
409 char buffer[SIZE];
410 String8 result;
411 snprintf(buffer, SIZE, "Permission Denial: "
412 "can't dump AudioFlinger from pid=%d, uid=%d\n",
413 IPCThreadState::self()->getCallingPid(),
414 IPCThreadState::self()->getCallingUid());
415 result.append(buffer);
416 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700417}
418
Eric Laurent81784c32012-11-19 14:55:58 -0800419bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420{
421 bool locked = false;
422 for (int i = 0; i < kDumpLockRetries; ++i) {
423 if (mutex.tryLock() == NO_ERROR) {
424 locked = true;
425 break;
426 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800427 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428 }
429 return locked;
430}
431
432status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
433{
Glenn Kasten44deb052012-02-05 18:09:08 -0800434 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 dumpPermissionDenial(fd, args);
436 } else {
437 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800438 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439 if (!hardwareLocked) {
440 String8 result(kHardwareLockedString);
441 write(fd, result.string(), result.size());
442 } else {
443 mHardwareLock.unlock();
444 }
445
Eric Laurent81784c32012-11-19 14:55:58 -0800446 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700447
448 // failed to lock - AudioFlinger is probably deadlocked
449 if (!locked) {
450 String8 result(kDeadlockedString);
451 write(fd, result.string(), result.size());
452 }
453
Eric Laurent021cf962014-05-13 10:18:14 -0700454 bool clientLocked = dumpTryLock(mClientLock);
455 if (!clientLocked) {
456 String8 result(kClientLockedString);
457 write(fd, result.string(), result.size());
458 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700459
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700460 if (mEffectsFactoryHal != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700461 mEffectsFactoryHal->dumpEffects(fd);
462 } else {
463 String8 result(kNoEffectsFactory);
464 write(fd, result.string(), result.size());
465 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700466
Mathias Agopian65ab4712010-07-14 17:59:35 -0700467 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700468 if (clientLocked) {
469 mClientLock.unlock();
470 }
471
Mathias Agopian65ab4712010-07-14 17:59:35 -0700472 dumpInternals(fd, args);
473
474 // dump playback threads
475 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
476 mPlaybackThreads.valueAt(i)->dump(fd, args);
477 }
478
479 // dump record threads
480 for (size_t i = 0; i < mRecordThreads.size(); i++) {
481 mRecordThreads.valueAt(i)->dump(fd, args);
482 }
483
Eric Laurent6acd1d42017-01-04 14:23:29 -0800484 // dump mmap threads
485 for (size_t i = 0; i < mMmapThreads.size(); i++) {
486 mMmapThreads.valueAt(i)->dump(fd, args);
487 }
488
Eric Laurentaaa44472014-09-12 17:41:50 -0700489 // dump orphan effect chains
490 if (mOrphanEffectChains.size() != 0) {
491 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
492 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
493 mOrphanEffectChains.valueAt(i)->dump(fd, args);
494 }
495 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700496 // dump all hardware devs
497 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700498 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
499 dev->dump(fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700500 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700501
Mikhail Naganov201369b2018-05-16 16:52:32 -0700502 mPatchPanel.dump(fd);
503
rago3bd1c872016-09-26 12:58:14 -0700504 BUFLOG_RESET;
505
Glenn Kastend65d73c2012-06-22 17:21:07 -0700506 if (locked) {
507 mLock.unlock();
508 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800509
Andy Hung8946a282018-04-19 20:04:56 -0700510#ifdef TEE_SINK
511 // NBAIO_Tee dump is safe to call outside of AF lock.
512 NBAIO_Tee::dumpAll(fd, "_DUMP");
513#endif
Glenn Kasten9e58b552013-01-18 15:09:48 -0800514 // append a copy of media.log here by forwarding fd to it, but don't attempt
515 // to lookup the service if it's not running, as it will block for a second
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700516 if (sMediaLogServiceAsBinder != 0) {
517 dprintf(fd, "\nmedia.log:\n");
518 Vector<String16> args;
519 sMediaLogServiceAsBinder->dump(fd, args);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800520 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700521
522 // check for optional arguments
Andy Hung07b745e2016-05-23 16:21:07 -0700523 bool dumpMem = false;
Andy Hung35fec5f2016-04-13 14:21:48 -0700524 bool unreachableMemory = false;
525 for (const auto &arg : args) {
Andy Hung07b745e2016-05-23 16:21:07 -0700526 if (arg == String16("-m")) {
527 dumpMem = true;
528 } else if (arg == String16("--unreachable")) {
Andy Hung35fec5f2016-04-13 14:21:48 -0700529 unreachableMemory = true;
530 }
531 }
532
Andy Hung07b745e2016-05-23 16:21:07 -0700533 if (dumpMem) {
534 dprintf(fd, "\nDumping memory:\n");
535 std::string s = dumpMemoryAddresses(100 /* limit */);
536 write(fd, s.c_str(), s.size());
537 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700538 if (unreachableMemory) {
539 dprintf(fd, "\nDumping unreachable memory:\n");
540 // TODO - should limit be an argument parameter?
Andy Hung07b745e2016-05-23 16:21:07 -0700541 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
Andy Hung35fec5f2016-04-13 14:21:48 -0700542 write(fd, s.c_str(), s.size());
543 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700544 }
545 return NO_ERROR;
546}
547
Eric Laurent021cf962014-05-13 10:18:14 -0700548sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800549{
Eric Laurent021cf962014-05-13 10:18:14 -0700550 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800551 // If pid is already in the mClients wp<> map, then use that entry
552 // (for which promote() is always != 0), otherwise create a new entry and Client.
553 sp<Client> client = mClients.valueFor(pid).promote();
554 if (client == 0) {
555 client = new Client(this, pid);
556 mClients.add(pid, client);
557 }
558
559 return client;
560}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561
Glenn Kasten9e58b552013-01-18 15:09:48 -0800562sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
563{
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700564 // If there is no memory allocated for logs, return a dummy writer that does nothing.
565 // Similarly if we can't contact the media.log service, also return a dummy writer.
566 if (mLogMemoryDealer == 0 || sMediaLogService == 0) {
Glenn Kasten9e58b552013-01-18 15:09:48 -0800567 return new NBLog::Writer();
568 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700569 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
570 // If allocation fails, consult the vector of previously unregistered writers
571 // and garbage-collect one or more them until an allocation succeeds
572 if (shared == 0) {
573 Mutex::Autolock _l(mUnregisteredWritersLock);
574 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
575 {
576 // Pick the oldest stale writer to garbage-collect
577 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
578 mUnregisteredWriters.removeAt(0);
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700579 sMediaLogService->unregisterWriter(iMemory);
Glenn Kasten481fb672013-09-30 14:39:28 -0700580 // Now the media.log remote reference to IMemory is gone. When our last local
581 // reference to IMemory also drops to zero at end of this block,
582 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
583 }
584 // Re-attempt the allocation
585 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
586 if (shared != 0) {
587 goto success;
588 }
589 }
590 // Even after garbage-collecting all old writers, there is still not enough memory,
591 // so return a dummy writer
592 return new NBLog::Writer();
593 }
594success:
Glenn Kasten535e1612016-12-05 12:19:36 -0800595 NBLog::Shared *sharedRawPtr = (NBLog::Shared *) shared->pointer();
596 new((void *) sharedRawPtr) NBLog::Shared(); // placement new here, but the corresponding
597 // explicit destructor not needed since it is POD
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700598 sMediaLogService->registerWriter(shared, size, name);
Glenn Kasten535e1612016-12-05 12:19:36 -0800599 return new NBLog::Writer(shared, size);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800600}
601
602void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
603{
Glenn Kasten685ef092013-02-04 08:15:34 -0800604 if (writer == 0) {
605 return;
606 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800607 sp<IMemory> iMemory(writer->getIMemory());
608 if (iMemory == 0) {
609 return;
610 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700611 // Rather than removing the writer immediately, append it to a queue of old writers to
612 // be garbage-collected later. This allows us to continue to view old logs for a while.
613 Mutex::Autolock _l(mUnregisteredWritersLock);
614 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800615}
616
Mathias Agopian65ab4712010-07-14 17:59:35 -0700617// IAudioFlinger interface
618
Eric Laurent21da6472017-11-09 16:29:26 -0800619sp<IAudioTrack> AudioFlinger::createTrack(const CreateTrackInput& input,
620 CreateTrackOutput& output,
621 status_t *status)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622{
623 sp<PlaybackThread::Track> track;
624 sp<TrackHandle> trackHandle;
625 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626 status_t lStatus;
Eric Laurent21da6472017-11-09 16:29:26 -0800627 audio_stream_type_t streamType;
Eric Laurent77881cd2018-02-09 16:01:31 -0800628 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629
Eric Laurent21da6472017-11-09 16:29:26 -0800630 bool updatePid = (input.clientInfo.clientPid == -1);
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700631 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurent21da6472017-11-09 16:29:26 -0800632 uid_t clientUid = input.clientInfo.clientUid;
Andy Hung4ef19fa2018-05-15 19:35:29 -0700633 if (!isAudioServerOrMediaServerUid(callingUid)) {
Eric Laurent21da6472017-11-09 16:29:26 -0800634 ALOGW_IF(clientUid != callingUid,
635 "%s uid %d tried to pass itself off as %d",
636 __FUNCTION__, callingUid, clientUid);
637 clientUid = callingUid;
638 updatePid = true;
639 }
640 pid_t clientPid = input.clientInfo.clientPid;
641 if (updatePid) {
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700642 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurent21da6472017-11-09 16:29:26 -0800643 ALOGW_IF(clientPid != -1 && clientPid != callingPid,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700644 "%s uid %d pid %d tried to pass itself off as pid %d",
Eric Laurent21da6472017-11-09 16:29:26 -0800645 __func__, callingUid, callingPid, clientPid);
646 clientPid = callingPid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700647 }
648
Eric Laurent21da6472017-11-09 16:29:26 -0800649 audio_session_t sessionId = input.sessionId;
650 if (sessionId == AUDIO_SESSION_ALLOCATE) {
651 sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Eric Laurentf14db3c2017-12-08 14:20:36 -0800652 } else if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
653 lStatus = BAD_VALUE;
654 goto Exit;
Eric Laurent21da6472017-11-09 16:29:26 -0800655 }
Eric Laurentf14db3c2017-12-08 14:20:36 -0800656
Eric Laurent21da6472017-11-09 16:29:26 -0800657 output.sessionId = sessionId;
658 output.outputId = AUDIO_IO_HANDLE_NONE;
659 output.selectedDeviceId = input.selectedDeviceId;
660
661 lStatus = AudioSystem::getOutputForAttr(&input.attr, &output.outputId, sessionId, &streamType,
Nadav Bar766fb022018-01-07 12:18:03 +0200662 clientPid, clientUid, &input.config, input.flags,
Eric Laurent21da6472017-11-09 16:29:26 -0800663 &output.selectedDeviceId, &portId);
664
665 if (lStatus != NO_ERROR || output.outputId == AUDIO_IO_HANDLE_NONE) {
666 ALOGE("createTrack() getOutputForAttr() return error %d or invalid output handle", lStatus);
667 goto Exit;
668 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800669 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
670 // but if someone uses binder directly they could bypass that and cause us to crash
671 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000672 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700673 lStatus = BAD_VALUE;
674 goto Exit;
675 }
676
Glenn Kasten53b5d092014-02-05 10:00:23 -0800677 // further channel mask checks are performed by createTrack_l() depending on the thread type
Eric Laurent21da6472017-11-09 16:29:26 -0800678 if (!audio_is_output_channel(input.config.channel_mask)) {
679 ALOGE("createTrack() invalid channel mask %#x", input.config.channel_mask);
Glenn Kasten53b5d092014-02-05 10:00:23 -0800680 lStatus = BAD_VALUE;
681 goto Exit;
682 }
683
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700684 // further format checks are performed by createTrack_l() depending on the thread type
Eric Laurent21da6472017-11-09 16:29:26 -0800685 if (!audio_is_valid_format(input.config.format)) {
686 ALOGE("createTrack() invalid format %#x", input.config.format);
Glenn Kasten663c2242013-09-24 11:52:37 -0700687 lStatus = BAD_VALUE;
688 goto Exit;
689 }
690
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691 {
692 Mutex::Autolock _l(mLock);
Eric Laurent21da6472017-11-09 16:29:26 -0800693 PlaybackThread *thread = checkPlaybackThread_l(output.outputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 if (thread == NULL) {
Eric Laurent21da6472017-11-09 16:29:26 -0800695 ALOGE("no playback thread found for output handle %d", output.outputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696 lStatus = BAD_VALUE;
697 goto Exit;
698 }
699
Eric Laurent21da6472017-11-09 16:29:26 -0800700 client = registerPid(clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701
Glenn Kastene848bd92014-03-13 15:00:32 -0700702 PlaybackThread *effectThread = NULL;
Eric Laurent21da6472017-11-09 16:29:26 -0800703 // check if an effect chain with the same session ID is present on another
704 // output thread and move it here.
705 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
706 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
707 if (mPlaybackThreads.keyAt(i) != output.outputId) {
708 uint32_t sessions = t->hasAudioSession(sessionId);
709 if (sessions & ThreadBase::EFFECT_SESSION) {
710 effectThread = t.get();
711 break;
Eric Laurentde070132010-07-13 04:45:46 -0700712 }
713 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 }
Eric Laurent21da6472017-11-09 16:29:26 -0800715 ALOGV("createTrack() sessionId: %d", sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716
Eric Laurent21da6472017-11-09 16:29:26 -0800717 output.sampleRate = input.config.sample_rate;
718 output.frameCount = input.frameCount;
719 output.notificationFrameCount = input.notificationFrameCount;
720 output.flags = input.flags;
721
Kevin Rocard1f564ac2018-03-29 13:53:10 -0700722 track = thread->createTrack_l(client, streamType, input.attr, &output.sampleRate,
723 input.config.format, input.config.channel_mask,
Eric Laurent21da6472017-11-09 16:29:26 -0800724 &output.frameCount, &output.notificationFrameCount,
725 input.notificationsPerBuffer, input.speed,
726 input.sharedBuffer, sessionId, &output.flags,
727 input.clientInfo.clientTid, clientUid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800728 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700729 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700730
Eric Laurent21da6472017-11-09 16:29:26 -0800731 output.afFrameCount = thread->frameCount();
732 output.afSampleRate = thread->sampleRate();
733 output.afLatencyMs = thread->latency();
734
Eric Laurent39e94f82010-07-28 01:32:47 -0700735 // move effect chain to this output thread if an effect on same session was waiting
736 // for a track to be created
737 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700738 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700739 Mutex::Autolock _dl(thread->mLock);
740 Mutex::Autolock _sl(effectThread->mLock);
Eric Laurent21da6472017-11-09 16:29:26 -0800741 moveEffectChain_l(sessionId, effectThread, thread, true);
Eric Laurent39e94f82010-07-28 01:32:47 -0700742 }
Eric Laurenta011e352012-03-29 15:51:43 -0700743
744 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700745 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurent21da6472017-11-09 16:29:26 -0800746 if (mPendingSyncEvents[i]->triggerSession() == sessionId) {
Eric Laurenta011e352012-03-29 15:51:43 -0700747 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700748 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700749 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700750 } else {
751 mPendingSyncEvents[i]->cancel();
752 }
Eric Laurenta011e352012-03-29 15:51:43 -0700753 mPendingSyncEvents.removeAt(i);
754 i--;
755 }
756 }
757 }
Glenn Kastene198c362013-08-13 09:13:36 -0700758
Eric Laurent21da6472017-11-09 16:29:26 -0800759 setAudioHwSyncForSession_l(thread, sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760 }
Glenn Kastene198c362013-08-13 09:13:36 -0700761
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700762 if (lStatus != NO_ERROR) {
763 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700764 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700765 // Don't hold mClientLock when releasing the reference on the track as the
766 // destructor will acquire it.
767 {
768 Mutex::Autolock _cl(mClientLock);
769 client.clear();
770 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700772 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773 }
774
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700775 // return handle to client
776 trackHandle = new TrackHandle(track);
777
Mathias Agopian65ab4712010-07-14 17:59:35 -0700778Exit:
Eric Laurent21da6472017-11-09 16:29:26 -0800779 if (lStatus != NO_ERROR && output.outputId != AUDIO_IO_HANDLE_NONE) {
780 AudioSystem::releaseOutput(output.outputId, streamType, sessionId);
781 }
Glenn Kasten9156ef32013-08-06 15:39:08 -0700782 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783 return trackHandle;
784}
785
Glenn Kasten2c073da2016-02-26 09:14:08 -0800786uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700787{
788 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800789 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700790 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800791 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792 return 0;
793 }
794 return thread->sampleRate();
795}
796
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800797audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798{
799 Mutex::Autolock _l(mLock);
800 PlaybackThread *thread = checkPlaybackThread_l(output);
801 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000802 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800803 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 }
805 return thread->format();
806}
807
Glenn Kasten2c073da2016-02-26 09:14:08 -0800808size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809{
810 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800811 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800813 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 return 0;
815 }
Glenn Kasten58912562012-04-03 10:45:00 -0700816 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
817 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 return thread->frameCount();
819}
820
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700821size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
822{
823 Mutex::Autolock _l(mLock);
824 ThreadBase *thread = checkThread_l(ioHandle);
825 if (thread == NULL) {
826 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
827 return 0;
828 }
829 return thread->frameCountHAL();
830}
831
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800832uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833{
834 Mutex::Autolock _l(mLock);
835 PlaybackThread *thread = checkPlaybackThread_l(output);
836 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800837 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838 return 0;
839 }
840 return thread->latency();
841}
842
843status_t AudioFlinger::setMasterVolume(float value)
844{
Eric Laurenta1884f92011-08-23 08:25:03 -0700845 status_t ret = initCheck();
846 if (ret != NO_ERROR) {
847 return ret;
848 }
849
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850 // check calling permissions
851 if (!settingsAllowed()) {
852 return PERMISSION_DENIED;
853 }
854
Eric Laurenta4c5a552012-03-29 10:12:40 -0700855 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700856 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700857
John Grossmanee578c02012-07-23 17:05:46 -0700858 // Set master volume in the HALs which support it.
859 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
860 AutoMutex lock(mHardwareLock);
861 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800862
John Grossmanee578c02012-07-23 17:05:46 -0700863 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
864 if (dev->canSetMasterVolume()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700865 dev->hwDevice()->setMasterVolume(value);
Eric Laurent93575202011-01-18 18:39:02 -0800866 }
John Grossmanee578c02012-07-23 17:05:46 -0700867 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700868 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869
John Grossmanee578c02012-07-23 17:05:46 -0700870 // Now set the master volume in each playback thread. Playback threads
871 // assigned to HALs which do not have master volume support will apply
872 // master volume during the mix operation. Threads with HALs which do
873 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700874 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
875 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
876 continue;
877 }
John Grossmanee578c02012-07-23 17:05:46 -0700878 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700879 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880
881 return NO_ERROR;
882}
883
Glenn Kastenf78aee72012-01-04 11:00:47 -0800884status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700885{
Eric Laurenta1884f92011-08-23 08:25:03 -0700886 status_t ret = initCheck();
887 if (ret != NO_ERROR) {
888 return ret;
889 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890
891 // check calling permissions
892 if (!settingsAllowed()) {
893 return PERMISSION_DENIED;
894 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800895 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000896 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897 return BAD_VALUE;
898 }
899
900 { // scope for the lock
901 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700902 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700903 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700904 ret = dev->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905 mHardwareStatus = AUDIO_HW_IDLE;
906 }
907
908 if (NO_ERROR == ret) {
909 Mutex::Autolock _l(mLock);
910 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800911 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700912 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913 }
914
915 return ret;
916}
917
918status_t AudioFlinger::setMicMute(bool state)
919{
Eric Laurenta1884f92011-08-23 08:25:03 -0700920 status_t ret = initCheck();
921 if (ret != NO_ERROR) {
922 return ret;
923 }
924
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925 // check calling permissions
926 if (!settingsAllowed()) {
927 return PERMISSION_DENIED;
928 }
929
930 AutoMutex lock(mHardwareLock);
931 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700932 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700933 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
934 status_t result = dev->setMicMute(state);
Eric Laurent2f035f52014-09-14 12:11:52 -0700935 if (result != NO_ERROR) {
936 ret = result;
937 }
938 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939 mHardwareStatus = AUDIO_HW_IDLE;
940 return ret;
941}
942
943bool AudioFlinger::getMicMute() const
944{
Eric Laurenta1884f92011-08-23 08:25:03 -0700945 status_t ret = initCheck();
946 if (ret != NO_ERROR) {
947 return false;
948 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800949 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700950 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800951 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800953 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700954 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
955 status_t result = dev->getMicMute(&state);
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800956 if (result == NO_ERROR) {
957 mute = mute && state;
958 }
959 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800961
962 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963}
964
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800965void AudioFlinger::setRecordSilenced(uid_t uid, bool silenced)
966{
967 ALOGV("AudioFlinger::setRecordSilenced(uid:%d, silenced:%d)", uid, silenced);
968
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800969 AutoMutex lock(mLock);
970 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent331679c2018-04-16 17:03:16 -0700971 mRecordThreads[i]->setRecordSilenced(uid, silenced);
972 }
973 for (size_t i = 0; i < mMmapThreads.size(); i++) {
974 mMmapThreads[i]->setRecordSilenced(uid, silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800975 }
976}
977
Mathias Agopian65ab4712010-07-14 17:59:35 -0700978status_t AudioFlinger::setMasterMute(bool muted)
979{
John Grossmand8f178d2012-07-20 14:51:35 -0700980 status_t ret = initCheck();
981 if (ret != NO_ERROR) {
982 return ret;
983 }
984
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 // check calling permissions
986 if (!settingsAllowed()) {
987 return PERMISSION_DENIED;
988 }
989
John Grossmanee578c02012-07-23 17:05:46 -0700990 Mutex::Autolock _l(mLock);
991 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700992
John Grossmanee578c02012-07-23 17:05:46 -0700993 // Set master mute in the HALs which support it.
994 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
995 AutoMutex lock(mHardwareLock);
996 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700997
John Grossmanee578c02012-07-23 17:05:46 -0700998 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
999 if (dev->canSetMasterMute()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001000 dev->hwDevice()->setMasterMute(muted);
John Grossmand8f178d2012-07-20 14:51:35 -07001001 }
John Grossmanee578c02012-07-23 17:05:46 -07001002 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -07001003 }
1004
John Grossmanee578c02012-07-23 17:05:46 -07001005 // Now set the master mute in each playback thread. Playback threads
1006 // assigned to HALs which do not have master mute support will apply master
1007 // mute during the mix operation. Threads with HALs which do support master
1008 // mute will simply ignore the setting.
Eric Laurent6acd1d42017-01-04 14:23:29 -08001009 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1010 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1011 volumeInterfaces[i]->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -07001012 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001013
1014 return NO_ERROR;
1015}
1016
1017float AudioFlinger::masterVolume() const
1018{
Glenn Kasten98067102011-12-13 11:47:54 -08001019 Mutex::Autolock _l(mLock);
1020 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021}
1022
1023bool AudioFlinger::masterMute() const
1024{
Glenn Kasten98067102011-12-13 11:47:54 -08001025 Mutex::Autolock _l(mLock);
1026 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001027}
1028
John Grossman4ff14ba2012-02-08 16:37:41 -08001029float AudioFlinger::masterVolume_l() const
1030{
John Grossman4ff14ba2012-02-08 16:37:41 -08001031 return mMasterVolume;
1032}
1033
John Grossmand8f178d2012-07-20 14:51:35 -07001034bool AudioFlinger::masterMute_l() const
1035{
John Grossmanee578c02012-07-23 17:05:46 -07001036 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -07001037}
1038
Eric Laurent223fd5c2014-11-11 13:43:36 -08001039status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
1040{
1041 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001042 ALOGW("checkStreamType() invalid stream %d", stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001043 return BAD_VALUE;
1044 }
Andy Hung4ef19fa2018-05-15 19:35:29 -07001045 const uid_t callerUid = IPCThreadState::self()->getCallingUid();
1046 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && !isAudioServerUid(callerUid)) {
1047 ALOGW("checkStreamType() uid %d cannot use internal stream type %d", callerUid, stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001048 return PERMISSION_DENIED;
1049 }
1050
1051 return NO_ERROR;
1052}
1053
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001054status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
1055 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001056{
1057 // check calling permissions
1058 if (!settingsAllowed()) {
1059 return PERMISSION_DENIED;
1060 }
1061
Eric Laurent223fd5c2014-11-11 13:43:36 -08001062 status_t status = checkStreamType(stream);
1063 if (status != NO_ERROR) {
1064 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065 }
Eric Laurent98e38192018-02-15 18:31:53 -08001066 if (output == AUDIO_IO_HANDLE_NONE) {
1067 return BAD_VALUE;
1068 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001069 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001070
1071 AutoMutex lock(mLock);
Eric Laurent98e38192018-02-15 18:31:53 -08001072 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1073 if (volumeInterface == NULL) {
1074 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001075 }
Eric Laurent98e38192018-02-15 18:31:53 -08001076 volumeInterface->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077
1078 return NO_ERROR;
1079}
1080
Glenn Kastenfff6d712012-01-12 16:38:12 -08001081status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001082{
1083 // check calling permissions
1084 if (!settingsAllowed()) {
1085 return PERMISSION_DENIED;
1086 }
1087
Eric Laurent223fd5c2014-11-11 13:43:36 -08001088 status_t status = checkStreamType(stream);
1089 if (status != NO_ERROR) {
1090 return status;
1091 }
1092 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1093
1094 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001095 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001096 return BAD_VALUE;
1097 }
1098
Eric Laurent93575202011-01-18 18:39:02 -08001099 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001100 mStreamTypes[stream].mute = muted;
Eric Laurent6acd1d42017-01-04 14:23:29 -08001101 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1102 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1103 volumeInterfaces[i]->setStreamMute(stream, muted);
1104 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001105
1106 return NO_ERROR;
1107}
1108
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001109float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001111 status_t status = checkStreamType(stream);
1112 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001113 return 0.0f;
1114 }
Eric Laurent98e38192018-02-15 18:31:53 -08001115 if (output == AUDIO_IO_HANDLE_NONE) {
1116 return 0.0f;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117 }
1118
Eric Laurent98e38192018-02-15 18:31:53 -08001119 AutoMutex lock(mLock);
1120 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1121 if (volumeInterface == NULL) {
1122 return 0.0f;
1123 }
1124
1125 return volumeInterface->streamVolume(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001126}
1127
Glenn Kastenfff6d712012-01-12 16:38:12 -08001128bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001130 status_t status = checkStreamType(stream);
1131 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001132 return true;
1133 }
1134
Glenn Kasten6637baa2012-01-09 09:40:36 -08001135 AutoMutex lock(mLock);
1136 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137}
1138
Eric Laurent054d9d32015-04-24 08:48:48 -07001139
1140void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1141{
1142 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1143 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1144 }
1145}
1146
Mikhail Naganovb261ef52018-07-16 13:34:38 -07001147// forwardAudioHwSyncToDownstreamPatches_l() must be called with AudioFlinger::mLock held
1148void AudioFlinger::forwardParametersToDownstreamPatches_l(
1149 audio_io_handle_t upStream, const String8& keyValuePairs,
1150 std::function<bool(const sp<PlaybackThread>&)> useThread)
1151{
1152 std::vector<PatchPanel::SoftwarePatch> swPatches;
1153 if (mPatchPanel.getDownstreamSoftwarePatches(upStream, &swPatches) != OK) return;
1154 ALOGV_IF(!swPatches.empty(), "%s found %zu downstream patches for stream ID %d",
1155 __func__, swPatches.size(), upStream);
1156 for (const auto& swPatch : swPatches) {
1157 sp<PlaybackThread> downStream = checkPlaybackThread_l(swPatch.getPlaybackThreadHandle());
1158 if (downStream != NULL && (useThread == nullptr || useThread(downStream))) {
1159 downStream->setParameters(keyValuePairs);
1160 }
1161 }
1162}
1163
Eric Laurentf1047e82018-04-16 19:18:20 -07001164// Filter reserved keys from setParameters() before forwarding to audio HAL or acting upon.
1165// Some keys are used for audio routing and audio path configuration and should be reserved for use
1166// by audio policy and audio flinger for functional, privacy and security reasons.
1167void AudioFlinger::filterReservedParameters(String8& keyValuePairs, uid_t callingUid)
1168{
1169 static const String8 kReservedParameters[] = {
1170 String8(AudioParameter::keyRouting),
1171 String8(AudioParameter::keySamplingRate),
1172 String8(AudioParameter::keyFormat),
1173 String8(AudioParameter::keyChannels),
1174 String8(AudioParameter::keyFrameCount),
1175 String8(AudioParameter::keyInputSource),
1176 String8(AudioParameter::keyMonoOutput),
1177 String8(AudioParameter::keyStreamConnect),
1178 String8(AudioParameter::keyStreamDisconnect),
1179 String8(AudioParameter::keyStreamSupportedFormats),
1180 String8(AudioParameter::keyStreamSupportedChannels),
1181 String8(AudioParameter::keyStreamSupportedSamplingRates),
1182 };
1183
Andy Hung4ef19fa2018-05-15 19:35:29 -07001184 if (isAudioServerUid(callingUid)) {
1185 return; // no need to filter if audioserver.
Eric Laurentf1047e82018-04-16 19:18:20 -07001186 }
1187
1188 AudioParameter param = AudioParameter(keyValuePairs);
1189 String8 value;
1190 for (auto& key : kReservedParameters) {
1191 if (param.get(key, value) == NO_ERROR) {
1192 ALOGW("%s: filtering key %s value %s from uid %d",
1193 __func__, key.string(), value.string(), callingUid);
1194 param.remove(key);
1195 }
1196 }
1197 keyValuePairs = param.toString();
1198}
1199
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001200status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201{
Eric Laurentf1047e82018-04-16 19:18:20 -07001202 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d calling uid %d",
1203 ioHandle, keyValuePairs.string(),
1204 IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid());
Eric Laurent81784c32012-11-19 14:55:58 -08001205
Mathias Agopian65ab4712010-07-14 17:59:35 -07001206 // check calling permissions
1207 if (!settingsAllowed()) {
1208 return PERMISSION_DENIED;
1209 }
1210
Eric Laurentf1047e82018-04-16 19:18:20 -07001211 String8 filteredKeyValuePairs = keyValuePairs;
1212 filterReservedParameters(filteredKeyValuePairs, IPCThreadState::self()->getCallingUid());
1213
1214 ALOGV("%s: filtered keyvalue %s", __func__, filteredKeyValuePairs.string());
1215
Glenn Kasten142f5192014-03-25 17:44:59 -07001216 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1217 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001218 Mutex::Autolock _l(mLock);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001219 // result will remain NO_INIT if no audio device is present
1220 status_t final_result = NO_INIT;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001221 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001222 AutoMutex lock(mHardwareLock);
1223 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1224 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001225 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
Eric Laurentf1047e82018-04-16 19:18:20 -07001226 status_t result = dev->setParameters(filteredKeyValuePairs);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001227 // return success if at least one audio device accepts the parameters as not all
1228 // HALs are requested to support all parameters. If no audio device supports the
1229 // requested parameters, the last error is reported.
1230 if (final_result != NO_ERROR) {
1231 final_result = result;
1232 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001233 }
1234 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001235 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001236 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
Eric Laurentf1047e82018-04-16 19:18:20 -07001237 AudioParameter param = AudioParameter(filteredKeyValuePairs);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001238 String8 value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001239 if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) {
1240 bool btNrecIsOff = (value == AudioParameter::valueOff);
Eric Laurentd8365c52017-07-16 15:27:05 -07001241 if (mBtNrecIsOff.exchange(btNrecIsOff) != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001242 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentd8365c52017-07-16 15:27:05 -07001243 mRecordThreads.valueAt(i)->checkBtNrec();
Eric Laurent59bd0da2011-08-01 09:52:20 -07001244 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001245 }
1246 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001247 String8 screenState;
1248 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
Mikhail Naganov00260b52016-10-13 12:54:24 -07001249 bool isOff = (screenState == AudioParameter::valueOff);
Eric Laurent81784c32012-11-19 14:55:58 -08001250 if (isOff != (AudioFlinger::mScreenState & 1)) {
1251 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001252 }
1253 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001254 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255 }
1256
1257 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1258 // and the thread is exited once the lock is released
1259 sp<ThreadBase> thread;
1260 {
1261 Mutex::Autolock _l(mLock);
1262 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001263 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001264 thread = checkRecordThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001265 if (thread == 0) {
1266 thread = checkMmapThread_l(ioHandle);
1267 }
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001268 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001269 // indicate output device change to all input threads for pre processing
Eric Laurentf1047e82018-04-16 19:18:20 -07001270 AudioParameter param = AudioParameter(filteredKeyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001271 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001272 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1273 (value != 0)) {
Eric Laurentf1047e82018-04-16 19:18:20 -07001274 broacastParametersToRecordThreads_l(filteredKeyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001275 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276 }
1277 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001278 if (thread != 0) {
Mikhail Naganovb261ef52018-07-16 13:34:38 -07001279 status_t result = thread->setParameters(filteredKeyValuePairs);
1280 forwardParametersToDownstreamPatches_l(thread->id(), filteredKeyValuePairs);
1281 return result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001282 }
1283 return BAD_VALUE;
1284}
1285
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001286String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001287{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001288 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1289 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001290
Eric Laurenta4c5a552012-03-29 10:12:40 -07001291 Mutex::Autolock _l(mLock);
1292
Glenn Kasten142f5192014-03-25 17:44:59 -07001293 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001294 String8 out_s8;
1295
Dima Zavin799a70e2011-04-18 16:57:27 -07001296 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001297 String8 s;
1298 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001299 {
1300 AutoMutex lock(mHardwareLock);
1301 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001302 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1303 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001304 mHardwareStatus = AUDIO_HW_IDLE;
1305 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001306 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001307 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001308 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001309 }
1310
Eric Laurent6acd1d42017-01-04 14:23:29 -08001311 ThreadBase *thread = (ThreadBase *)checkPlaybackThread_l(ioHandle);
1312 if (thread == NULL) {
1313 thread = (ThreadBase *)checkRecordThread_l(ioHandle);
1314 if (thread == NULL) {
1315 thread = (ThreadBase *)checkMmapThread_l(ioHandle);
1316 if (thread == NULL) {
Mikhail Naganovaa165e52017-07-12 10:39:16 -07001317 return String8("");
Eric Laurent6acd1d42017-01-04 14:23:29 -08001318 }
1319 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001321 return thread->getParameters(keys);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001322}
1323
Glenn Kastendd8104c2012-07-02 12:42:44 -07001324size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1325 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001326{
Eric Laurenta1884f92011-08-23 08:25:03 -07001327 status_t ret = initCheck();
1328 if (ret != NO_ERROR) {
1329 return 0;
1330 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001331 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001332 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001333 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001334 return 0;
1335 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001336
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001337 AutoMutex lock(mHardwareLock);
1338 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001339 audio_config_t config, proposed;
1340 memset(&proposed, 0, sizeof(proposed));
1341 proposed.sample_rate = sampleRate;
1342 proposed.channel_mask = channelMask;
1343 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001344
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001345 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001346 size_t frames;
1347 for (;;) {
1348 // Note: config is currently a const parameter for get_input_buffer_size()
1349 // but we use a copy from proposed in case config changes from the call.
1350 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001351 status_t result = dev->getInputBufferSize(&config, &frames);
1352 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001353 break; // hal success, config is the result
1354 }
1355 // change one parameter of the configuration each iteration to a more "common" value
1356 // to see if the device will support it.
1357 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1358 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1359 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1360 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1361 } else {
1362 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1363 "format %#x, channelMask 0x%X",
1364 sampleRate, format, channelMask);
1365 break; // retries failed, break out of loop with frames == 0.
1366 }
1367 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001368 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001369 if (frames > 0 && config.sample_rate != sampleRate) {
1370 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1371 }
1372 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001373}
1374
Glenn Kasten5f972c02014-01-13 09:59:31 -08001375uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001376{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001377 Mutex::Autolock _l(mLock);
1378
1379 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1380 if (recordThread != NULL) {
1381 return recordThread->getInputFramesLost();
1382 }
1383 return 0;
1384}
1385
1386status_t AudioFlinger::setVoiceVolume(float value)
1387{
Eric Laurenta1884f92011-08-23 08:25:03 -07001388 status_t ret = initCheck();
1389 if (ret != NO_ERROR) {
1390 return ret;
1391 }
1392
Mathias Agopian65ab4712010-07-14 17:59:35 -07001393 // check calling permissions
1394 if (!settingsAllowed()) {
1395 return PERMISSION_DENIED;
1396 }
1397
1398 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001399 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001400 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001401 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402 mHardwareStatus = AUDIO_HW_IDLE;
1403
1404 return ret;
1405}
1406
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001407status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001408 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001410 Mutex::Autolock _l(mLock);
1411
1412 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1413 if (playbackThread != NULL) {
1414 return playbackThread->getRenderPosition(halFrames, dspFrames);
1415 }
1416
1417 return BAD_VALUE;
1418}
1419
1420void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1421{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001422 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001423 if (client == 0) {
1424 return;
1425 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001426 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001427 {
1428 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001429 if (mNotificationClients.indexOfKey(pid) < 0) {
1430 sp<NotificationClient> notificationClient = new NotificationClient(this,
1431 client,
1432 pid);
1433 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001434
Eric Laurent021cf962014-05-13 10:18:14 -07001435 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001436
Marco Nelissen06b46062014-11-14 07:58:25 -08001437 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001438 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001439 }
1440 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001441
Eric Laurent021cf962014-05-13 10:18:14 -07001442 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001443 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001444 // the config change is always sent from playback or record threads to avoid deadlock
1445 // with AudioSystem::gLock
1446 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001447 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_REGISTERED, pid);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001448 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001450 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001451 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_REGISTERED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001452 }
1453}
1454
1455void AudioFlinger::removeNotificationClient(pid_t pid)
1456{
1457 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001458 {
1459 Mutex::Autolock _cl(mClientLock);
1460 mNotificationClients.removeItem(pid);
1461 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001462
Steve Block3856b092011-10-20 11:56:00 +01001463 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001464 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001465 bool removed = false;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001466 for (size_t i = 0; i < num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001467 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001468 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001469 if (ref->mPid == pid) {
1470 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001471 mAudioSessionRefs.removeAt(i);
1472 delete ref;
1473 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001474 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001475 } else {
1476 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001477 }
1478 }
1479 if (removed) {
1480 purgeStaleEffects_l();
1481 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482}
1483
Eric Laurent73e26b62015-04-27 16:55:58 -07001484void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001485 const sp<AudioIoDescriptor>& ioDesc,
1486 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487{
Eric Laurent021cf962014-05-13 10:18:14 -07001488 Mutex::Autolock _l(mClientLock);
1489 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001491 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1492 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1493 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001494 }
1495}
1496
Eric Laurent021cf962014-05-13 10:18:14 -07001497// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498void AudioFlinger::removeClient_l(pid_t pid)
1499{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001500 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001501 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001502 mClients.removeItem(pid);
1503}
1504
Eric Laurent717e1282012-06-29 16:36:52 -07001505// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001506sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1507 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001508{
1509 sp<PlaybackThread> thread;
1510
1511 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1512 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1513 ALOG_ASSERT(thread == 0);
1514 thread = mPlaybackThreads.valueAt(i);
1515 }
1516 }
1517
1518 return thread;
1519}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522
1523// ----------------------------------------------------------------------------
1524
1525AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1526 : RefBase(),
1527 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001528 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529{
Andy Hung6f248bb2018-01-23 14:04:37 -08001530 mMemoryDealer = new MemoryDealer(
1531 audioFlinger->getClientSharedHeapSize(),
1532 (std::string("AudioFlinger::Client(") + std::to_string(pid) + ")").c_str());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533}
1534
Eric Laurent021cf962014-05-13 10:18:14 -07001535// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001536AudioFlinger::Client::~Client()
1537{
1538 mAudioFlinger->removeClient_l(mPid);
1539}
1540
Glenn Kasten435dbe62012-01-30 10:15:48 -08001541sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542{
1543 return mMemoryDealer;
1544}
1545
1546// ----------------------------------------------------------------------------
1547
1548AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1549 const sp<IAudioFlingerClient>& client,
1550 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001551 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001552{
1553}
1554
1555AudioFlinger::NotificationClient::~NotificationClient()
1556{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001557}
1558
Glenn Kasten0f11b512014-01-31 16:18:54 -08001559void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001560{
1561 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001562 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563}
1564
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001565// ----------------------------------------------------------------------------
1566AudioFlinger::MediaLogNotifier::MediaLogNotifier()
1567 : mPendingRequests(false) {}
1568
1569
1570void AudioFlinger::MediaLogNotifier::requestMerge() {
1571 AutoMutex _l(mMutex);
1572 mPendingRequests = true;
1573 mCond.signal();
1574}
1575
1576bool AudioFlinger::MediaLogNotifier::threadLoop() {
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001577 // Should already have been checked, but just in case
1578 if (sMediaLogService == 0) {
1579 return false;
1580 }
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001581 // Wait until there are pending requests
1582 {
1583 AutoMutex _l(mMutex);
1584 mPendingRequests = false; // to ignore past requests
1585 while (!mPendingRequests) {
1586 mCond.wait(mMutex);
1587 // TODO may also need an exitPending check
1588 }
1589 mPendingRequests = false;
1590 }
1591 // Execute the actual MediaLogService binder call and ignore extra requests for a while
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001592 sMediaLogService->requestMergeWakeup();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001593 usleep(kPostTriggerSleepPeriod);
1594 return true;
1595}
1596
1597void AudioFlinger::requestLogMerge() {
1598 mMediaLogNotifier->requestMerge();
1599}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600
1601// ----------------------------------------------------------------------------
1602
Eric Laurentf14db3c2017-12-08 14:20:36 -08001603sp<media::IAudioRecord> AudioFlinger::createRecord(const CreateRecordInput& input,
1604 CreateRecordOutput& output,
1605 status_t *status)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001606{
1607 sp<RecordThread::RecordTrack> recordTrack;
1608 sp<RecordHandle> recordHandle;
1609 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001610 status_t lStatus;
Eric Laurentf14db3c2017-12-08 14:20:36 -08001611 audio_session_t sessionId = input.sessionId;
Eric Laurent77881cd2018-02-09 16:01:31 -08001612 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001613
Eric Laurentf14db3c2017-12-08 14:20:36 -08001614 output.cblk.clear();
1615 output.buffers.clear();
Eric Laurent896c9672017-12-08 14:08:45 -08001616 output.inputId = AUDIO_IO_HANDLE_NONE;
Glenn Kastend776ac62014-05-07 09:16:09 -07001617
Eric Laurentf14db3c2017-12-08 14:20:36 -08001618 bool updatePid = (input.clientInfo.clientPid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001619 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurentf14db3c2017-12-08 14:20:36 -08001620 uid_t clientUid = input.clientInfo.clientUid;
Andy Hung4ef19fa2018-05-15 19:35:29 -07001621 if (!isAudioServerOrMediaServerUid(callingUid)) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001622 ALOGW_IF(clientUid != callingUid,
1623 "%s uid %d tried to pass itself off as %d",
1624 __FUNCTION__, callingUid, clientUid);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001625 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001626 updatePid = true;
1627 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001628 pid_t clientPid = input.clientInfo.clientPid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001629 if (updatePid) {
1630 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurentf14db3c2017-12-08 14:20:36 -08001631 ALOGW_IF(clientPid != -1 && clientPid != callingPid,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001632 "%s uid %d pid %d tried to pass itself off as pid %d",
Eric Laurentf14db3c2017-12-08 14:20:36 -08001633 __func__, callingUid, callingPid, clientPid);
1634 clientPid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001635 }
1636
Andy Hung6770c6f2015-04-07 13:43:36 -07001637 // we don't yet support anything other than linear PCM
Eric Laurentf14db3c2017-12-08 14:20:36 -08001638 if (!audio_is_valid_format(input.config.format) || !audio_is_linear_pcm(input.config.format)) {
1639 ALOGE("createRecord() invalid format %#x", input.config.format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001640 lStatus = BAD_VALUE;
1641 goto Exit;
1642 }
1643
Glenn Kasten53b5d092014-02-05 10:00:23 -08001644 // further channel mask checks are performed by createRecordTrack_l()
Eric Laurentf14db3c2017-12-08 14:20:36 -08001645 if (!audio_is_input_channel(input.config.channel_mask)) {
1646 ALOGE("createRecord() invalid channel mask %#x", input.config.channel_mask);
Glenn Kasten53b5d092014-02-05 10:00:23 -08001647 lStatus = BAD_VALUE;
1648 goto Exit;
1649 }
1650
Eric Laurentf14db3c2017-12-08 14:20:36 -08001651 if (sessionId == AUDIO_SESSION_ALLOCATE) {
1652 sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
1653 } else if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1654 lStatus = BAD_VALUE;
1655 goto Exit;
1656 }
1657
1658 output.sessionId = sessionId;
Eric Laurentf14db3c2017-12-08 14:20:36 -08001659 output.selectedDeviceId = input.selectedDeviceId;
1660 output.flags = input.flags;
1661
1662 client = registerPid(clientPid);
1663
1664 // Not a conventional loop, but a retry loop for at most two iterations total.
1665 // Try first maybe with FAST flag then try again without FAST flag if that fails.
1666 // Exits loop via break on no error of got exit on error
1667 // The sp<> references will be dropped when re-entering scope.
1668 // The lack of indentation is deliberate, to reduce code churn and ease merges.
1669 for (;;) {
Eric Laurent896c9672017-12-08 14:08:45 -08001670 // release previously opened input if retrying.
1671 if (output.inputId != AUDIO_IO_HANDLE_NONE) {
1672 recordTrack.clear();
Eric Laurentfee19762018-01-29 18:44:13 -08001673 AudioSystem::releaseInput(portId);
Eric Laurent896c9672017-12-08 14:08:45 -08001674 output.inputId = AUDIO_IO_HANDLE_NONE;
Yung Ti Su5fac9c62018-05-15 15:07:43 +08001675 output.selectedDeviceId = input.selectedDeviceId;
Eric Laurent77881cd2018-02-09 16:01:31 -08001676 portId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent896c9672017-12-08 14:08:45 -08001677 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001678 lStatus = AudioSystem::getInputForAttr(&input.attr, &output.inputId,
1679 sessionId,
1680 // FIXME compare to AudioTrack
1681 clientPid,
1682 clientUid,
Eric Laurentfee19762018-01-29 18:44:13 -08001683 input.opPackageName,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001684 &input.config,
1685 output.flags, &output.selectedDeviceId, &portId);
1686
Glenn Kasten05997e22014-03-13 15:08:33 -07001687 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001688 Mutex::Autolock _l(mLock);
Eric Laurentf14db3c2017-12-08 14:20:36 -08001689 RecordThread *thread = checkRecordThread_l(output.inputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001690 if (thread == NULL) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001691 ALOGE("createRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001692 lStatus = BAD_VALUE;
1693 goto Exit;
1694 }
1695
Eric Laurentf14db3c2017-12-08 14:20:36 -08001696 ALOGV("createRecord() lSessionId: %d input %d", sessionId, output.inputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001697
Eric Laurentf14db3c2017-12-08 14:20:36 -08001698 output.sampleRate = input.config.sample_rate;
1699 output.frameCount = input.frameCount;
1700 output.notificationFrameCount = input.notificationFrameCount;
Glenn Kasten570f6332014-03-13 15:01:06 -07001701
Kevin Rocard1f564ac2018-03-29 13:53:10 -07001702 recordTrack = thread->createRecordTrack_l(client, input.attr, &output.sampleRate,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001703 input.config.format, input.config.channel_mask,
1704 &output.frameCount, sessionId,
1705 &output.notificationFrameCount,
1706 clientUid, &output.flags,
1707 input.clientInfo.clientTid,
1708 &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001709 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001710
Eric Laurentf14db3c2017-12-08 14:20:36 -08001711 // lStatus == BAD_TYPE means FAST flag was rejected: request a new input from
1712 // audio policy manager without FAST constraint
1713 if (lStatus == BAD_TYPE) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001714 continue;
Eric Laurent1b928682014-10-02 19:41:47 -07001715 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001716
1717 if (lStatus != NO_ERROR) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001718 goto Exit;
1719 }
1720
1721 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1722 // session and move it to this thread.
1723 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
1724 if (chain != 0) {
1725 Mutex::Autolock _l(thread->mLock);
1726 thread->addEffectChain_l(chain);
1727 }
1728 break;
1729 }
1730 // End of retry loop.
1731 // The lack of indentation is deliberate, to reduce code churn and ease merges.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001732 }
Glenn Kastene198c362013-08-13 09:13:36 -07001733
Eric Laurentf14db3c2017-12-08 14:20:36 -08001734 output.cblk = recordTrack->getCblk();
1735 output.buffers = recordTrack->getBuffers();
1736
1737 // return handle to client
1738 recordHandle = new RecordHandle(recordTrack);
1739
1740Exit:
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001741 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001742 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001743 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001744 // Don't hold mClientLock when releasing the reference on the track as the
1745 // destructor will acquire it.
1746 {
1747 Mutex::Autolock _cl(mClientLock);
1748 client.clear();
1749 }
Eric Laurent896c9672017-12-08 14:08:45 -08001750 recordTrack.clear();
1751 if (output.inputId != AUDIO_IO_HANDLE_NONE) {
Eric Laurentfee19762018-01-29 18:44:13 -08001752 AudioSystem::releaseInput(portId);
Eric Laurent896c9672017-12-08 14:08:45 -08001753 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001754 }
1755
Glenn Kasten9156ef32013-08-06 15:39:08 -07001756 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001757 return recordHandle;
1758}
1759
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001760
1761
Mathias Agopian65ab4712010-07-14 17:59:35 -07001762// ----------------------------------------------------------------------------
1763
Eric Laurenta4c5a552012-03-29 10:12:40 -07001764audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1765{
Eric Laurent44622db2014-08-01 19:00:33 -07001766 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001767 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001768 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001769 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001770 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001771 }
1772 Mutex::Autolock _l(mLock);
1773 return loadHwModule_l(name);
1774}
1775
1776// loadHwModule_l() must be called with AudioFlinger::mLock held
1777audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1778{
1779 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1780 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1781 ALOGW("loadHwModule() module %s already loaded", name);
1782 return mAudioHwDevs.keyAt(i);
1783 }
1784 }
1785
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001786 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001787
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001788 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001789 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001790 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001791 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001792 }
1793
1794 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001795 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07001796 mHardwareStatus = AUDIO_HW_IDLE;
1797 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001798 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001799 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001800 }
1801
John Grossmanee578c02012-07-23 17:05:46 -07001802 // Check and cache this HAL's level of support for master mute and master
1803 // volume. If this is the first HAL opened, and it supports the get
1804 // methods, use the initial values provided by the HAL as the current
1805 // master mute and volume settings.
1806
1807 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1808 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001809 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001810
1811 if (0 == mAudioHwDevs.size()) {
1812 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001813 float mv;
1814 if (OK == dev->getMasterVolume(&mv)) {
1815 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07001816 }
1817
1818 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001819 bool mm;
1820 if (OK == dev->getMasterMute(&mm)) {
1821 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07001822 }
1823 }
1824
Eric Laurenta4c5a552012-03-29 10:12:40 -07001825 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001826 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07001827 flags = static_cast<AudioHwDevice::Flags>(flags |
1828 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1829 }
1830
1831 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001832 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07001833 flags = static_cast<AudioHwDevice::Flags>(flags |
1834 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1835 }
1836
Eric Laurenta4c5a552012-03-29 10:12:40 -07001837 mHardwareStatus = AUDIO_HW_IDLE;
1838 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -07001839 if (strcmp(name, AUDIO_HAL_SERVICE_NAME_MSD) == 0) {
1840 // An MSD module is inserted before hardware modules in order to mix encoded streams.
1841 flags = static_cast<AudioHwDevice::Flags>(flags | AudioHwDevice::AHWD_IS_INSERT);
1842 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001843
Glenn Kastena13cde92016-03-28 15:26:02 -07001844 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001845 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001846
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001847 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001848
1849 return handle;
1850
1851}
1852
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001853// ----------------------------------------------------------------------------
1854
Glenn Kasten3b16c762012-11-14 08:44:39 -08001855uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001856{
1857 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001858 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001859 return thread != NULL ? thread->sampleRate() : 0;
1860}
1861
Glenn Kastene33054e2012-11-14 12:54:39 -08001862size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001863{
1864 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001865 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001866 return thread != NULL ? thread->frameCountHAL() : 0;
1867}
1868
1869// ----------------------------------------------------------------------------
1870
Andy Hung6f248bb2018-01-23 14:04:37 -08001871status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001872{
1873 uid_t uid = IPCThreadState::self()->getCallingUid();
Andy Hung4ef19fa2018-05-15 19:35:29 -07001874 if (!isAudioServerOrSystemServerUid(uid)) {
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001875 return PERMISSION_DENIED;
1876 }
1877 Mutex::Autolock _l(mLock);
1878 if (mIsDeviceTypeKnown) {
1879 return INVALID_OPERATION;
1880 }
1881 mIsLowRamDevice = isLowRamDevice;
Andy Hung6f248bb2018-01-23 14:04:37 -08001882 mTotalMemory = totalMemory;
1883 // mIsLowRamDevice and mTotalMemory are obtained through ActivityManager;
1884 // see ActivityManager.isLowRamDevice() and ActivityManager.getMemoryInfo().
1885 // mIsLowRamDevice generally represent devices with less than 1GB of memory,
1886 // though actual setting is determined through device configuration.
1887 constexpr int64_t GB = 1024 * 1024 * 1024;
1888 mClientSharedHeapSize =
1889 isLowRamDevice ? kMinimumClientSharedHeapSizeBytes
1890 : mTotalMemory < 2 * GB ? 4 * kMinimumClientSharedHeapSizeBytes
1891 : mTotalMemory < 3 * GB ? 8 * kMinimumClientSharedHeapSizeBytes
1892 : mTotalMemory < 4 * GB ? 16 * kMinimumClientSharedHeapSizeBytes
1893 : 32 * kMinimumClientSharedHeapSizeBytes;
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001894 mIsDeviceTypeKnown = true;
Andy Hung6f248bb2018-01-23 14:04:37 -08001895
1896 // TODO: Cache the client shared heap size in a persistent property.
1897 // It's possible that a native process or Java service or app accesses audioserver
1898 // after it is registered by system server, but before AudioService updates
1899 // the memory info. This would occur immediately after boot or an audioserver
1900 // crash and restore. Before update from AudioService, the client would get the
1901 // minimum heap size.
1902
1903 ALOGD("isLowRamDevice:%s totalMemory:%lld mClientSharedHeapSize:%zu",
1904 (isLowRamDevice ? "true" : "false"),
1905 (long long)mTotalMemory,
1906 mClientSharedHeapSize.load());
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001907 return NO_ERROR;
1908}
1909
Andy Hung6f248bb2018-01-23 14:04:37 -08001910size_t AudioFlinger::getClientSharedHeapSize() const
1911{
1912 size_t heapSizeInBytes = property_get_int32("ro.af.client_heap_size_kbyte", 0) * 1024;
1913 if (heapSizeInBytes != 0) { // read-only property overrides all.
1914 return heapSizeInBytes;
1915 }
1916 return mClientSharedHeapSize;
1917}
1918
Mikhail Naganovdea53042018-04-26 13:10:21 -07001919status_t AudioFlinger::setAudioPortConfig(const struct audio_port_config *config)
1920{
1921 ALOGV(__func__);
1922
1923 audio_module_handle_t module;
1924 if (config->type == AUDIO_PORT_TYPE_DEVICE) {
1925 module = config->ext.device.hw_module;
1926 } else {
1927 module = config->ext.mix.hw_module;
1928 }
1929
1930 Mutex::Autolock _l(mLock);
1931 ssize_t index = mAudioHwDevs.indexOfKey(module);
1932 if (index < 0) {
1933 ALOGW("%s() bad hw module %d", __func__, module);
1934 return BAD_VALUE;
1935 }
1936
1937 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(index);
1938 return audioHwDevice->hwDevice()->setAudioPortConfig(config);
1939}
1940
Eric Laurent93c3d412014-08-01 14:48:35 -07001941audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1942{
1943 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001944
1945 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1946 if (index >= 0) {
1947 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1948 mHwAvSyncIds.valueAt(index), sessionId);
1949 return mHwAvSyncIds.valueAt(index);
1950 }
1951
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001952 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07001953 if (dev == NULL) {
1954 return AUDIO_HW_SYNC_INVALID;
1955 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001956 String8 reply;
1957 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001958 if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001959 param = AudioParameter(reply);
1960 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001961
1962 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001963 if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001964 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1965 return AUDIO_HW_SYNC_INVALID;
1966 }
1967
1968 // allow only one session for a given HW A/V sync ID.
1969 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1970 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1971 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1972 value, mHwAvSyncIds.keyAt(i));
1973 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001974 break;
1975 }
1976 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001977
1978 mHwAvSyncIds.add(sessionId, value);
1979
1980 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1981 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1982 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07001983 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001984 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001985 param.addInt(String8(AudioParameter::keyStreamHwAvSync), value);
Mikhail Naganovb261ef52018-07-16 13:34:38 -07001986 String8 keyValuePairs = param.toString();
1987 thread->setParameters(keyValuePairs);
1988 forwardParametersToDownstreamPatches_l(thread->id(), keyValuePairs,
1989 [](const sp<PlaybackThread>& thread) { return thread->usesHwAvSync(); });
Eric Laurentfa90e842014-10-17 18:12:31 -07001990 break;
1991 }
1992 }
1993
1994 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1995 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001996}
1997
Eric Laurent72e3f392015-05-20 14:43:50 -07001998status_t AudioFlinger::systemReady()
1999{
2000 Mutex::Autolock _l(mLock);
2001 ALOGI("%s", __FUNCTION__);
2002 if (mSystemReady) {
2003 ALOGW("%s called twice", __FUNCTION__);
2004 return NO_ERROR;
2005 }
2006 mSystemReady = true;
2007 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2008 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
2009 thread->systemReady();
2010 }
2011 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2012 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
2013 thread->systemReady();
2014 }
2015 return NO_ERROR;
2016}
2017
jiabin46a76fa2018-01-05 10:18:21 -08002018status_t AudioFlinger::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
2019{
jiabin9ff780e2018-03-19 18:19:52 -07002020 AutoMutex lock(mHardwareLock);
2021 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
2022 status_t status = dev->getMicrophones(microphones);
2023 return status;
jiabin46a76fa2018-01-05 10:18:21 -08002024}
2025
Eric Laurentfa90e842014-10-17 18:12:31 -07002026// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
2027void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
2028{
2029 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
2030 if (index >= 0) {
2031 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
2032 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
2033 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07002034 param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId);
Mikhail Naganovb261ef52018-07-16 13:34:38 -07002035 String8 keyValuePairs = param.toString();
2036 thread->setParameters(keyValuePairs);
2037 forwardParametersToDownstreamPatches_l(thread->id(), keyValuePairs,
2038 [](const sp<PlaybackThread>& thread) { return thread->usesHwAvSync(); });
Eric Laurentfa90e842014-10-17 18:12:31 -07002039 }
2040}
2041
2042
Glenn Kasten4182c4e2013-07-15 14:45:07 -07002043// ----------------------------------------------------------------------------
2044
Eric Laurent83b88082014-06-20 18:31:16 -07002045
Eric Laurent6acd1d42017-01-04 14:23:29 -08002046sp<AudioFlinger::ThreadBase> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002047 audio_io_handle_t *output,
2048 audio_config_t *config,
2049 audio_devices_t devices,
2050 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07002051 audio_output_flags_t flags)
2052{
Eric Laurentcf2c0212014-07-25 16:20:43 -07002053 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07002054 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002055 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07002056 }
2057
Eric Laurentcf2c0212014-07-25 16:20:43 -07002058 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002059 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
2060 } else {
2061 // Audio Policy does not currently request a specific output handle.
2062 // If this is ever needed, see openInput_l() for example code.
2063 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
2064 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002065 }
Eric Laurent83b88082014-06-20 18:31:16 -07002066
2067 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
2068
Eric Laurent83b88082014-06-20 18:31:16 -07002069 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07002070 // This if statement allows overriding the audio policy settings
2071 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
2072 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
2073 // Check only for Normal Mixing mode
2074 if (kEnableExtendedPrecision) {
2075 // Specify format (uncomment one below to choose)
2076 //config->format = AUDIO_FORMAT_PCM_FLOAT;
2077 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
2078 //config->format = AUDIO_FORMAT_PCM_32_BIT;
2079 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002080 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07002081 }
2082 if (kEnableExtendedChannels) {
2083 // Specify channel mask (uncomment one below to choose)
2084 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
2085 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
2086 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
2087 }
Eric Laurent83b88082014-06-20 18:31:16 -07002088 }
2089
Phil Burk062e67a2015-02-11 13:40:50 -08002090 AudioStreamOut *outputStream = NULL;
2091 status_t status = outHwDev->openOutputStream(
2092 &outputStream,
2093 *output,
2094 devices,
2095 flags,
2096 config,
2097 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07002098
2099 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07002100
Phil Burk062e67a2015-02-11 13:40:50 -08002101 if (status == NO_ERROR) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002102 if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
2103 sp<MmapPlaybackThread> thread =
2104 new MmapPlaybackThread(this, *output, outHwDev, outputStream,
2105 devices, AUDIO_DEVICE_NONE, mSystemReady);
2106 mMmapThreads.add(*output, thread);
2107 ALOGV("openOutput_l() created mmap playback thread: ID %d thread %p",
2108 *output, thread.get());
2109 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07002110 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002111 sp<PlaybackThread> thread;
2112 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2113 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
2114 ALOGV("openOutput_l() created offload output: ID %d thread %p",
2115 *output, thread.get());
2116 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
2117 || !isValidPcmSinkFormat(config->format)
2118 || !isValidPcmSinkChannelMask(config->channel_mask)) {
2119 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
2120 ALOGV("openOutput_l() created direct output: ID %d thread %p",
2121 *output, thread.get());
2122 } else {
2123 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
2124 ALOGV("openOutput_l() created mixer output: ID %d thread %p",
2125 *output, thread.get());
2126 }
2127 mPlaybackThreads.add(*output, thread);
Mikhail Naganovadca70f2018-07-09 12:49:25 -07002128 mPatchPanel.notifyStreamOpened(outHwDev, *output);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002129 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07002130 }
Eric Laurent83b88082014-06-20 18:31:16 -07002131 }
2132
2133 return 0;
2134}
2135
Eric Laurentcf2c0212014-07-25 16:20:43 -07002136status_t AudioFlinger::openOutput(audio_module_handle_t module,
2137 audio_io_handle_t *output,
2138 audio_config_t *config,
2139 audio_devices_t *devices,
2140 const String8& address,
2141 uint32_t *latencyMs,
2142 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002143{
Glenn Kasten49f36ba2017-12-06 13:02:02 -08002144 ALOGI("openOutput() this %p, module %d Device %#x, SamplingRate %d, Format %#08x, "
2145 "Channels %#x, flags %#x",
Eric Laurent6acd1d42017-01-04 14:23:29 -08002146 this, module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002147 (devices != NULL) ? *devices : 0,
2148 config->sample_rate,
2149 config->format,
2150 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002151 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002152
Yunlian Jiang32ba9862017-01-31 15:55:00 -08002153 if (devices == NULL || *devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002154 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002155 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002156
Mathias Agopian65ab4712010-07-14 17:59:35 -07002157 Mutex::Autolock _l(mLock);
2158
Eric Laurent6acd1d42017-01-04 14:23:29 -08002159 sp<ThreadBase> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002160 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002161 if ((flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) {
2162 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
2163 *latencyMs = playbackThread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002164
Eric Laurent6acd1d42017-01-04 14:23:29 -08002165 // notify client processes of the new output creation
2166 playbackThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002167
Eric Laurent6acd1d42017-01-04 14:23:29 -08002168 // the first primary output opened designates the primary hw device
2169 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -08002170 ALOGI("Using module %d as the primary audio interface", module);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002171 mPrimaryHardwareDev = playbackThread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002172
Eric Laurent6acd1d42017-01-04 14:23:29 -08002173 AutoMutex lock(mHardwareLock);
2174 mHardwareStatus = AUDIO_HW_SET_MODE;
2175 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
2176 mHardwareStatus = AUDIO_HW_IDLE;
2177 }
2178 } else {
2179 MmapThread *mmapThread = (MmapThread *)thread.get();
2180 mmapThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002181 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002182 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002183 }
2184
Eric Laurentcf2c0212014-07-25 16:20:43 -07002185 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002186}
2187
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002188audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
2189 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002190{
2191 Mutex::Autolock _l(mLock);
2192 MixerThread *thread1 = checkMixerThread_l(output1);
2193 MixerThread *thread2 = checkMixerThread_l(output2);
2194
2195 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002196 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
2197 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07002198 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002199 }
2200
Glenn Kasteneeecb982016-02-26 10:44:04 -08002201 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07002202 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002203 thread->addOutputTrack(thread2);
2204 mPlaybackThreads.add(id, thread);
2205 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002206 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002207 return id;
2208}
2209
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002210status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002211{
Glenn Kastend96c5722012-04-25 13:44:49 -07002212 return closeOutput_nonvirtual(output);
2213}
2214
2215status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
2216{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002217 // keep strong reference on the playback thread so that
2218 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002219 sp<PlaybackThread> playbackThread;
2220 sp<MmapPlaybackThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002221 {
2222 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002223 playbackThread = checkPlaybackThread_l(output);
2224 if (playbackThread != NULL) {
2225 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002226
Eric Laurent6acd1d42017-01-04 14:23:29 -08002227 if (playbackThread->type() == ThreadBase::MIXER) {
2228 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2229 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
2230 DuplicatingThread *dupThread =
2231 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
2232 dupThread->removeOutputTrack((MixerThread *)playbackThread.get());
2233 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002234 }
2235 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002236
2237
Eric Laurent6acd1d42017-01-04 14:23:29 -08002238 mPlaybackThreads.removeItem(output);
2239 // save all effects to the default thread
2240 if (mPlaybackThreads.size()) {
2241 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
2242 if (dstThread != NULL) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002243 // audioflinger lock is held so order of thread lock acquisition doesn't matter
Eric Laurent6acd1d42017-01-04 14:23:29 -08002244 Mutex::Autolock _dl(dstThread->mLock);
2245 Mutex::Autolock _sl(playbackThread->mLock);
2246 Vector< sp<EffectChain> > effectChains = playbackThread->getEffectChains_l();
2247 for (size_t i = 0; i < effectChains.size(); i ++) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002248 moveEffectChain_l(effectChains[i]->sessionId(), playbackThread.get(),
2249 dstThread, true);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002250 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002251 }
2252 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002253 } else {
2254 mmapThread = (MmapPlaybackThread *)checkMmapThread_l(output);
2255 if (mmapThread == 0) {
2256 return BAD_VALUE;
2257 }
2258 mMmapThreads.removeItem(output);
Phil Burk7f6b40d2017-02-09 13:18:38 -08002259 ALOGD("closing mmapThread %p", mmapThread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002260 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002261 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2262 ioDesc->mIoHandle = output;
2263 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mikhail Naganovadca70f2018-07-09 12:49:25 -07002264 mPatchPanel.notifyStreamClosed(output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002265 }
Glenn Kastenb28686f2012-01-06 08:39:38 -08002266 // The thread entity (active unit of execution) is no longer running here,
2267 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002268
Eric Laurent6acd1d42017-01-04 14:23:29 -08002269 if (playbackThread != 0) {
2270 playbackThread->exit();
2271 if (!playbackThread->isDuplicating()) {
2272 closeOutputFinish(playbackThread);
2273 }
2274 } else if (mmapThread != 0) {
Phil Burk7f6b40d2017-02-09 13:18:38 -08002275 ALOGD("mmapThread exit()");
Eric Laurent6acd1d42017-01-04 14:23:29 -08002276 mmapThread->exit();
2277 AudioStreamOut *out = mmapThread->clearOutput();
2278 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2279 // from now on thread->mOutput is NULL
2280 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002281 }
2282 return NO_ERROR;
2283}
2284
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002285void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002286{
2287 AudioStreamOut *out = thread->clearOutput();
2288 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2289 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07002290 delete out;
2291}
2292
Mikhail Naganov444ecc32018-05-01 17:40:05 -07002293void AudioFlinger::closeThreadInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002294{
2295 mPlaybackThreads.removeItem(thread->mId);
2296 thread->exit();
2297 closeOutputFinish(thread);
2298}
2299
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002300status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002301{
2302 Mutex::Autolock _l(mLock);
2303 PlaybackThread *thread = checkPlaybackThread_l(output);
2304
2305 if (thread == NULL) {
2306 return BAD_VALUE;
2307 }
2308
Steve Block3856b092011-10-20 11:56:00 +01002309 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002310 thread->suspend();
2311
2312 return NO_ERROR;
2313}
2314
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002315status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002316{
2317 Mutex::Autolock _l(mLock);
2318 PlaybackThread *thread = checkPlaybackThread_l(output);
2319
2320 if (thread == NULL) {
2321 return BAD_VALUE;
2322 }
2323
Steve Block3856b092011-10-20 11:56:00 +01002324 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002325
2326 thread->restore();
2327
2328 return NO_ERROR;
2329}
2330
Eric Laurentcf2c0212014-07-25 16:20:43 -07002331status_t AudioFlinger::openInput(audio_module_handle_t module,
2332 audio_io_handle_t *input,
2333 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002334 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002335 const String8& address,
2336 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002337 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002338{
Eric Laurent83b88082014-06-20 18:31:16 -07002339 Mutex::Autolock _l(mLock);
2340
Glenn Kastene7d66712015-03-05 13:46:52 -08002341 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002342 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002343 }
2344
Eric Laurent6acd1d42017-01-04 14:23:29 -08002345 sp<ThreadBase> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002346
2347 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002348 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002349 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002350 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002351 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002352 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002353}
Dima Zavin799a70e2011-04-18 16:57:27 -07002354
Eric Laurent6acd1d42017-01-04 14:23:29 -08002355sp<AudioFlinger::ThreadBase> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002356 audio_io_handle_t *input,
2357 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002358 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002359 const String8& address,
2360 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002361 audio_input_flags_t flags)
2362{
Glenn Kastene7d66712015-03-05 13:46:52 -08002363 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002364 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002365 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002366 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002367 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002368
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07002369 // Some flags are specific to framework and must not leak to the HAL.
2370 flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FRAMEWORK_FLAGS);
2371
Glenn Kasteneeecb982016-02-26 10:44:04 -08002372 // Audio Policy can request a specific handle for hardware hotword.
2373 // The goal here is not to re-open an already opened input.
2374 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002375 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002376 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2377 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2378 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2379 return 0;
2380 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2381 // This should not happen in a transient state with current design.
2382 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2383 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002384 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002385
Eric Laurentcf2c0212014-07-25 16:20:43 -07002386 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002387 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002388 sp<StreamInHalInterface> inStream;
2389 status_t status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002390 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten49f36ba2017-12-06 13:02:02 -08002391 ALOGV("openInput_l() openInputStream returned input %p, devices %#x, SamplingRate %d"
2392 ", Format %#x, Channels %#x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002393 inStream.get(),
Mikhail Naganovf558e022016-11-14 17:45:17 -08002394 devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002395 halconfig.sample_rate,
2396 halconfig.format,
2397 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002398 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002399 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002400
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002401 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002402 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002403 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002404 audio_is_linear_pcm(config->format) &&
2405 audio_is_linear_pcm(halconfig.format) &&
2406 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002407 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2408 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002409 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002410 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002411 inStream.clear();
2412 status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002413 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002414 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002415 }
2416
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002417 if (status == NO_ERROR && inStream != 0) {
Eric Laurent05067782016-06-01 18:27:28 -07002418 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002419 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) {
2420 sp<MmapCaptureThread> thread =
2421 new MmapCaptureThread(this, *input,
2422 inHwDev, inputStream,
2423 primaryOutputDevice_l(), devices, mSystemReady);
2424 mMmapThreads.add(*input, thread);
Glenn Kastend3bb6452016-12-05 18:14:37 -08002425 ALOGV("openInput_l() created mmap capture thread: ID %d thread %p", *input,
2426 thread.get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002427 return thread;
2428 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002429 // Start record thread
2430 // RecordThread requires both input and output device indication to forward to audio
2431 // pre processing modules
2432 sp<RecordThread> thread = new RecordThread(this,
2433 inputStream,
2434 *input,
2435 primaryOutputDevice_l(),
2436 devices,
2437 mSystemReady
Eric Laurent6acd1d42017-01-04 14:23:29 -08002438 );
2439 mRecordThreads.add(*input, thread);
2440 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
2441 return thread;
2442 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002443 }
2444
Eric Laurentcf2c0212014-07-25 16:20:43 -07002445 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002446 return 0;
2447}
2448
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002449status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002450{
Glenn Kastend96c5722012-04-25 13:44:49 -07002451 return closeInput_nonvirtual(input);
2452}
2453
2454status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2455{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002456 // keep strong reference on the record thread so that
2457 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002458 sp<RecordThread> recordThread;
2459 sp<MmapCaptureThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002460 {
2461 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002462 recordThread = checkRecordThread_l(input);
2463 if (recordThread != 0) {
2464 ALOGV("closeInput() %d", input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002465
Eric Laurent6acd1d42017-01-04 14:23:29 -08002466 // If we still have effect chains, it means that a client still holds a handle
2467 // on at least one effect. We must either move the chain to an existing thread with the
2468 // same session ID or put it aside in case a new record thread is opened for a
2469 // new capture on the same session
2470 sp<EffectChain> chain;
2471 {
2472 Mutex::Autolock _sl(recordThread->mLock);
2473 Vector< sp<EffectChain> > effectChains = recordThread->getEffectChains_l();
2474 // Note: maximum one chain per record thread
2475 if (effectChains.size() != 0) {
2476 chain = effectChains[0];
Eric Laurent1b928682014-10-02 19:41:47 -07002477 }
2478 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002479 if (chain != 0) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002480 // first check if a record thread is already opened with a client on same session.
Eric Laurent6acd1d42017-01-04 14:23:29 -08002481 // This should only happen in case of overlap between one thread tear down and the
2482 // creation of its replacement
2483 size_t i;
2484 for (i = 0; i < mRecordThreads.size(); i++) {
2485 sp<RecordThread> t = mRecordThreads.valueAt(i);
2486 if (t == recordThread) {
2487 continue;
2488 }
2489 if (t->hasAudioSession(chain->sessionId()) != 0) {
2490 Mutex::Autolock _l(t->mLock);
2491 ALOGV("closeInput() found thread %d for effect session %d",
2492 t->id(), chain->sessionId());
2493 t->addEffectChain_l(chain);
2494 break;
2495 }
2496 }
Glenn Kastend3bb6452016-12-05 18:14:37 -08002497 // put the chain aside if we could not find a record thread with the same session id
Eric Laurent6acd1d42017-01-04 14:23:29 -08002498 if (i == mRecordThreads.size()) {
2499 putOrphanEffectChain_l(chain);
2500 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002501 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002502 mRecordThreads.removeItem(input);
2503 } else {
2504 mmapThread = (MmapCaptureThread *)checkMmapThread_l(input);
2505 if (mmapThread == 0) {
2506 return BAD_VALUE;
2507 }
2508 mMmapThreads.removeItem(input);
Eric Laurentaaa44472014-09-12 17:41:50 -07002509 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002510 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2511 ioDesc->mIoHandle = input;
2512 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002513 }
Eric Laurent83b88082014-06-20 18:31:16 -07002514 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2515 // we have a different lock for notification client
Eric Laurent6acd1d42017-01-04 14:23:29 -08002516 if (recordThread != 0) {
2517 closeInputFinish(recordThread);
2518 } else if (mmapThread != 0) {
2519 mmapThread->exit();
2520 AudioStreamIn *in = mmapThread->clearInput();
2521 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
2522 // from now on thread->mInput is NULL
2523 delete in;
2524 }
Eric Laurent83b88082014-06-20 18:31:16 -07002525 return NO_ERROR;
2526}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002527
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002528void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002529{
2530 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002531 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002532 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002533 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002534 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002535}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002536
Mikhail Naganov444ecc32018-05-01 17:40:05 -07002537void AudioFlinger::closeThreadInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002538{
2539 mRecordThreads.removeItem(thread->mId);
2540 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002541}
2542
Glenn Kastend2304db2014-02-03 07:40:31 -08002543status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002544{
2545 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002546 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002547
2548 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2549 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002550 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002551 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002552 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2553 mMmapThreads[i]->invalidateTracks(stream);
2554 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002555 return NO_ERROR;
2556}
2557
2558
Glenn Kasteneeecb982016-02-26 10:44:04 -08002559audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002560{
Glenn Kasten9d003132016-04-06 14:38:09 -07002561 // This is a binder API, so a malicious client could pass in a bad parameter.
2562 // Check for that before calling the internal API nextUniqueId().
2563 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2564 ALOGE("newAudioUniqueId invalid use %d", use);
2565 return AUDIO_UNIQUE_ID_ALLOCATE;
2566 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002567 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002568}
2569
Glenn Kastend848eb42016-03-08 13:42:11 -08002570void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002571{
2572 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002573 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002574 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
Andy Hung4ef19fa2018-05-15 19:35:29 -07002575 const uid_t callerUid = IPCThreadState::self()->getCallingUid();
2576 if (pid != -1 && isAudioServerUid(callerUid)) { // check must match releaseAudioSessionId()
Marco Nelissend457c972014-02-11 08:47:07 -08002577 caller = pid;
2578 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002579
Eric Laurent021cf962014-05-13 10:18:14 -07002580 {
2581 Mutex::Autolock _cl(mClientLock);
2582 // Ignore requests received from processes not known as notification client. The request
2583 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2584 // called from a different pid leaving a stale session reference. Also we don't know how
2585 // to clear this reference if the client process dies.
2586 if (mNotificationClients.indexOfKey(caller) < 0) {
2587 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2588 return;
2589 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002590 }
2591
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002592 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002593 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002594 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002595 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2596 ref->mCnt++;
2597 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002598 return;
2599 }
2600 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002601 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2602 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002603}
2604
Glenn Kastend848eb42016-03-08 13:42:11 -08002605void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002606{
2607 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002608 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002609 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
Andy Hung4ef19fa2018-05-15 19:35:29 -07002610 const uid_t callerUid = IPCThreadState::self()->getCallingUid();
2611 if (pid != -1 && isAudioServerUid(callerUid)) { // check must match acquireAudioSessionId()
Marco Nelissend457c972014-02-11 08:47:07 -08002612 caller = pid;
2613 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002614 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002615 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002616 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002617 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2618 ref->mCnt--;
2619 ALOGV(" decremented refcount to %d", ref->mCnt);
2620 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002621 mAudioSessionRefs.removeAt(i);
2622 delete ref;
2623 purgeStaleEffects_l();
2624 }
2625 return;
2626 }
2627 }
Andy Hung4ef19fa2018-05-15 19:35:29 -07002628 // If the caller is audioserver it is likely that the session being released was acquired
Eric Laurentd1b28d42013-09-18 18:47:13 -07002629 // on behalf of a process not in notification clients and we ignore the warning.
Andy Hung4ef19fa2018-05-15 19:35:29 -07002630 ALOGW_IF(!isAudioServerUid(callerUid),
2631 "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002632}
2633
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002634bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
2635{
2636 size_t num = mAudioSessionRefs.size();
2637 for (size_t i = 0; i < num; i++) {
2638 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2639 if (ref->mSessionid == audioSession) {
2640 return true;
2641 }
2642 }
2643 return false;
2644}
2645
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002646void AudioFlinger::purgeStaleEffects_l() {
2647
Steve Block3856b092011-10-20 11:56:00 +01002648 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002649
2650 Vector< sp<EffectChain> > chains;
2651
2652 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2653 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
Mateusz Kaplon2a6e1b02017-03-30 16:50:50 +02002654 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002655 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2656 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002657 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2658 chains.push(ec);
2659 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002660 }
2661 }
2662 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2663 sp<RecordThread> t = mRecordThreads.valueAt(i);
Mateusz Kaplon2a6e1b02017-03-30 16:50:50 +02002664 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002665 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2666 sp<EffectChain> ec = t->mEffectChains[j];
2667 chains.push(ec);
2668 }
2669 }
2670
2671 for (size_t i = 0; i < chains.size(); i++) {
2672 sp<EffectChain> ec = chains[i];
2673 int sessionid = ec->sessionId();
2674 sp<ThreadBase> t = ec->mThread.promote();
2675 if (t == 0) {
2676 continue;
2677 }
2678 size_t numsessionrefs = mAudioSessionRefs.size();
2679 bool found = false;
2680 for (size_t k = 0; k < numsessionrefs; k++) {
2681 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002682 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002683 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002684 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002685 found = true;
2686 break;
2687 }
2688 }
2689 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002690 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002691 // remove all effects from the chain
2692 while (ec->mEffects.size()) {
2693 sp<EffectModule> effect = ec->mEffects[0];
2694 effect->unPin();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07002695 t->removeEffect_l(effect, /*release*/ true);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002696 if (effect->purgeHandles()) {
2697 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002698 }
2699 AudioSystem::unregisterEffect(effect->id());
2700 }
2701 }
2702 }
2703 return;
2704}
2705
Glenn Kasteneeecb982016-02-26 10:44:04 -08002706// checkThread_l() must be called with AudioFlinger::mLock held
2707AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2708{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002709 ThreadBase *thread = checkMmapThread_l(ioHandle);
2710 if (thread == 0) {
2711 switch (audio_unique_id_get_use(ioHandle)) {
2712 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2713 thread = checkPlaybackThread_l(ioHandle);
2714 break;
2715 case AUDIO_UNIQUE_ID_USE_INPUT:
2716 thread = checkRecordThread_l(ioHandle);
2717 break;
2718 default:
2719 break;
2720 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002721 }
2722 return thread;
2723}
2724
Mathias Agopian65ab4712010-07-14 17:59:35 -07002725// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002726AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002727{
Glenn Kastena1117922012-01-26 10:53:32 -08002728 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002729}
2730
2731// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002732AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002733{
2734 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002735 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002736}
2737
2738// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002739AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002740{
Glenn Kastena1117922012-01-26 10:53:32 -08002741 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002742}
2743
Eric Laurent6acd1d42017-01-04 14:23:29 -08002744// checkMmapThread_l() must be called with AudioFlinger::mLock held
2745AudioFlinger::MmapThread *AudioFlinger::checkMmapThread_l(audio_io_handle_t io) const
2746{
2747 return mMmapThreads.valueFor(io).get();
2748}
2749
2750
2751// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
2752AudioFlinger::VolumeInterface *AudioFlinger::getVolumeInterface_l(audio_io_handle_t output) const
2753{
Eric Laurent7459b902017-04-19 18:10:41 -07002754 VolumeInterface *volumeInterface = mPlaybackThreads.valueFor(output).get();
Eric Laurent6acd1d42017-01-04 14:23:29 -08002755 if (volumeInterface == nullptr) {
2756 MmapThread *mmapThread = mMmapThreads.valueFor(output).get();
2757 if (mmapThread != nullptr) {
2758 if (mmapThread->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07002759 MmapPlaybackThread *mmapPlaybackThread =
2760 static_cast<MmapPlaybackThread *>(mmapThread);
2761 volumeInterface = mmapPlaybackThread;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002762 }
2763 }
2764 }
2765 return volumeInterface;
2766}
2767
2768Vector <AudioFlinger::VolumeInterface *> AudioFlinger::getAllVolumeInterfaces_l() const
2769{
2770 Vector <VolumeInterface *> volumeInterfaces;
2771 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7459b902017-04-19 18:10:41 -07002772 volumeInterfaces.add(mPlaybackThreads.valueAt(i).get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002773 }
2774 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2775 if (mMmapThreads.valueAt(i)->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07002776 MmapPlaybackThread *mmapPlaybackThread =
2777 static_cast<MmapPlaybackThread *>(mMmapThreads.valueAt(i).get());
2778 volumeInterfaces.add(mmapPlaybackThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002779 }
2780 }
2781 return volumeInterfaces;
2782}
2783
Glenn Kasteneeecb982016-02-26 10:44:04 -08002784audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002785{
Glenn Kasten9d003132016-04-06 14:38:09 -07002786 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002787 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002788 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2789 for (int retry = 0; retry < maxRetries; retry++) {
2790 // The cast allows wraparound from max positive to min negative instead of abort
2791 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2792 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2793 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2794 // allow wrap by skipping 0 and -1 for session ids
2795 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2796 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2797 return (audio_unique_id_t) (base | use);
2798 }
2799 }
2800 // We have no way of recovering from wraparound
2801 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2802 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002803}
2804
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002805AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002806{
2807 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2808 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002809 if(thread->isDuplicating()) {
2810 continue;
2811 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002812 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002813 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002814 return thread;
2815 }
2816 }
2817 return NULL;
2818}
2819
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002820audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002821{
2822 PlaybackThread *thread = primaryPlaybackThread_l();
2823
2824 if (thread == NULL) {
2825 return 0;
2826 }
2827
Eric Laurentf1c04f92012-08-28 14:26:53 -07002828 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002829}
2830
Glenn Kastena7335632016-06-09 17:09:53 -07002831AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
2832{
2833 size_t minFrameCount = 0;
2834 PlaybackThread *minThread = NULL;
2835 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2836 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2837 if (!thread->isDuplicating()) {
2838 size_t frameCount = thread->frameCountHAL();
2839 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
2840 (frameCount == minFrameCount && thread->hasFastMixer() &&
2841 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
2842 minFrameCount = frameCount;
2843 minThread = thread;
2844 }
2845 }
2846 }
2847 return minThread;
2848}
2849
Eric Laurenta011e352012-03-29 15:51:43 -07002850sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002851 audio_session_t triggerSession,
2852 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002853 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002854 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002855{
2856 Mutex::Autolock _l(mLock);
2857
2858 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2859 status_t playStatus = NAME_NOT_FOUND;
2860 status_t recStatus = NAME_NOT_FOUND;
2861 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2862 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2863 if (playStatus == NO_ERROR) {
2864 return event;
2865 }
2866 }
2867 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2868 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2869 if (recStatus == NO_ERROR) {
2870 return event;
2871 }
2872 }
2873 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2874 mPendingSyncEvents.add(event);
2875 } else {
2876 ALOGV("createSyncEvent() invalid event %d", event->type());
2877 event.clear();
2878 }
2879 return event;
2880}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002881
Mathias Agopian65ab4712010-07-14 17:59:35 -07002882// ----------------------------------------------------------------------------
2883// Effect management
2884// ----------------------------------------------------------------------------
2885
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002886sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
2887 return mEffectsFactoryHal;
2888}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002889
Glenn Kastenf587ba52012-01-26 16:25:10 -08002890status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002891{
2892 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002893 if (mEffectsFactoryHal.get()) {
2894 return mEffectsFactoryHal->queryNumberEffects(numEffects);
2895 } else {
2896 return -ENODEV;
2897 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002898}
2899
Glenn Kastenf587ba52012-01-26 16:25:10 -08002900status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002901{
2902 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002903 if (mEffectsFactoryHal.get()) {
2904 return mEffectsFactoryHal->getDescriptor(index, descriptor);
2905 } else {
2906 return -ENODEV;
2907 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002908}
2909
Glenn Kasten5e92a782012-01-30 07:40:52 -08002910status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002911 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002912{
2913 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002914 if (mEffectsFactoryHal.get()) {
2915 return mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
2916 } else {
2917 return -ENODEV;
2918 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002919}
2920
2921
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002922sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002923 effect_descriptor_t *pDesc,
2924 const sp<IEffectClient>& effectClient,
2925 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002926 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002927 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002928 const String16& opPackageName,
Eric Laurentb6436272016-12-07 19:24:50 -08002929 pid_t pid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002930 status_t *status,
2931 int *id,
2932 int *enabled)
2933{
2934 status_t lStatus = NO_ERROR;
2935 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002936 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002937
Eric Laurentb6436272016-12-07 19:24:50 -08002938 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Andy Hung4ef19fa2018-05-15 19:35:29 -07002939 if (pid == -1 || !isAudioServerOrMediaServerUid(callingUid)) {
Eric Laurentb6436272016-12-07 19:24:50 -08002940 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
2941 ALOGW_IF(pid != -1 && pid != callingPid,
2942 "%s uid %d pid %d tried to pass itself off as pid %d",
2943 __func__, callingUid, callingPid, pid);
2944 pid = callingPid;
2945 }
2946
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002947 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
2948 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002949
2950 if (pDesc == NULL) {
2951 lStatus = BAD_VALUE;
2952 goto Exit;
2953 }
2954
Eric Laurent84e9a102010-09-23 16:10:16 -07002955 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002956 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002957 lStatus = PERMISSION_DENIED;
2958 goto Exit;
2959 }
2960
Dima Zavinfce7a472011-04-19 22:30:36 -07002961 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Andy Hung4ef19fa2018-05-15 19:35:29 -07002962 // that can only be created by audio policy manager
2963 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && !isAudioServerUid(callingUid)) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002964 lStatus = PERMISSION_DENIED;
2965 goto Exit;
2966 }
2967
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002968 if (mEffectsFactoryHal == 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002969 lStatus = NO_INIT;
2970 goto Exit;
2971 }
2972
Mathias Agopian65ab4712010-07-14 17:59:35 -07002973 {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002974 if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002975 // if uuid is specified, request effect descriptor
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002976 lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002977 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002978 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002979 goto Exit;
2980 }
2981 } else {
2982 // if uuid is not specified, look for an available implementation
2983 // of the required type in effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002984 if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002985 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002986 lStatus = BAD_VALUE;
2987 goto Exit;
2988 }
2989 uint32_t numEffects = 0;
2990 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002991 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002992 bool found = false;
2993
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002994 lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002995 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002996 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002997 goto Exit;
2998 }
2999 for (uint32_t i = 0; i < numEffects; i++) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003000 lStatus = mEffectsFactoryHal->getDescriptor(i, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003001 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003002 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003003 continue;
3004 }
3005 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
3006 // If matching type found save effect descriptor. If the session is
3007 // 0 and the effect is not auxiliary, continue enumeration in case
3008 // an auxiliary version of this effect type is available
3009 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08003010 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07003011 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07003012 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
3013 break;
3014 }
3015 }
3016 }
3017 if (!found) {
3018 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00003019 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07003020 goto Exit;
3021 }
3022 // For same effect type, chose auxiliary version over insert version if
3023 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07003024 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07003025 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08003026 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003027 }
3028 }
3029
3030 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07003031 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07003032 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
3033 lStatus = INVALID_OPERATION;
3034 goto Exit;
3035 }
3036
Eric Laurent59255e42011-07-27 19:49:51 -07003037 // check recording permission for visualizer
3038 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Svet Ganov6e641372018-03-02 09:21:30 -08003039 // TODO: Do we need to start/stop op - i.e. is there recording being performed?
Marco Nelissendcb346b2015-09-09 10:47:29 -07003040 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07003041 lStatus = PERMISSION_DENIED;
3042 goto Exit;
3043 }
3044
Mathias Agopian65ab4712010-07-14 17:59:35 -07003045 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08003046 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07003047 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07003048 // if the output returned by getOutputForEffect() is removed before we lock the
3049 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
3050 // and we will exit safely
3051 io = AudioSystem::getOutputForEffect(&desc);
3052 ALOGV("createEffect got output %d", io);
3053 }
3054
3055 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003056
3057 // If output is not specified try to find a matching audio session ID in one of the
3058 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07003059 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
3060 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003061 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07003062 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07003063 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
3064 // output must be specified by AudioPolicyManager when using session
3065 // AUDIO_SESSION_OUTPUT_STAGE
3066 lStatus = BAD_VALUE;
3067 goto Exit;
3068 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07003069 // look for the thread where the specified audio session is present
3070 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3071 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
3072 io = mPlaybackThreads.keyAt(i);
3073 break;
3074 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003075 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08003076 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07003077 for (size_t i = 0; i < mRecordThreads.size(); i++) {
3078 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
3079 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07003080 break;
3081 }
3082 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003083 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08003084 if (io == AUDIO_IO_HANDLE_NONE) {
3085 for (size_t i = 0; i < mMmapThreads.size(); i++) {
3086 if (mMmapThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
3087 io = mMmapThreads.keyAt(i);
3088 break;
3089 }
3090 }
3091 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003092 // If no output thread contains the requested session ID, default to
3093 // first output. The effect chain will be moved to the correct output
3094 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07003095 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003096 io = mPlaybackThreads.keyAt(0);
3097 }
Steve Block3856b092011-10-20 11:56:00 +01003098 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003099 }
3100 ThreadBase *thread = checkRecordThread_l(io);
3101 if (thread == NULL) {
3102 thread = checkPlaybackThread_l(io);
3103 if (thread == NULL) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08003104 thread = checkMmapThread_l(io);
3105 if (thread == NULL) {
3106 ALOGE("createEffect() unknown output thread");
3107 lStatus = BAD_VALUE;
3108 goto Exit;
3109 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003110 }
Eric Laurentaaa44472014-09-12 17:41:50 -07003111 } else {
3112 // Check if one effect chain was awaiting for an effect to be created on this
3113 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08003114 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07003115 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07003116 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07003117 thread->addEffectChain_l(chain);
3118 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003119 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003120
Eric Laurent021cf962014-05-13 10:18:14 -07003121 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003122
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003123 // create effect on selected output thread
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003124 bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003125 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003126 &desc, enabled, &lStatus, pinned);
haobo101735295ff7b0d2017-03-17 17:44:03 +08003127 if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003128 // remove local strong reference to Client with mClientLock held
3129 Mutex::Autolock _cl(mClientLock);
3130 client.clear();
haobo101735295ff7b0d2017-03-17 17:44:03 +08003131 } else {
3132 // handle must be valid here, but check again to be safe.
3133 if (handle.get() != nullptr && id != nullptr) *id = handle->id();
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003134 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003135 }
3136
haobo101735295ff7b0d2017-03-17 17:44:03 +08003137 if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
3138 // handle must be cleared outside lock.
3139 handle.clear();
3140 }
3141
Mathias Agopian65ab4712010-07-14 17:59:35 -07003142Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07003143 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003144 return handle;
3145}
3146
Glenn Kastend848eb42016-03-08 13:42:11 -08003147status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003148 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07003149{
Steve Block3856b092011-10-20 11:56:00 +01003150 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07003151 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003152 Mutex::Autolock _l(mLock);
3153 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003154 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003155 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003156 }
Eric Laurentde070132010-07-13 04:45:46 -07003157 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
3158 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003159 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003160 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003161 }
Eric Laurentde070132010-07-13 04:45:46 -07003162 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
3163 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003164 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003165 return BAD_VALUE;
3166 }
3167
3168 Mutex::Autolock _dl(dstThread->mLock);
3169 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003170 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003171}
3172
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003173// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08003174status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07003175 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07003176 AudioFlinger::PlaybackThread *dstThread,
3177 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07003178{
Steve Block3856b092011-10-20 11:56:00 +01003179 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003180 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07003181
Eric Laurent59255e42011-07-27 19:49:51 -07003182 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003183 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003184 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003185 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07003186 return INVALID_OPERATION;
3187 }
3188
Eric Laurent4c415062016-06-17 16:14:16 -07003189 // Check whether the destination thread and all effects in the chain are compatible
3190 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07003191 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07003192 " destination thread %p is not compatible with effects in the chain",
3193 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07003194 return INVALID_OPERATION;
3195 }
3196
Eric Laurent39e94f82010-07-28 01:32:47 -07003197 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07003198 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07003199 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07003200 // removed.
3201 srcThread->removeEffectChain_l(chain);
3202
3203 // transfer all effects one by one so that new effect chain is created on new thread with
3204 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07003205 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003206 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07003207 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003208 Vector< sp<EffectModule> > removed;
3209 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07003210 while (effect != 0) {
3211 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003212 removed.add(effect);
3213 status = dstThread->addEffect_l(effect);
3214 if (status != NO_ERROR) {
3215 break;
3216 }
Eric Laurentec35a142011-10-05 17:42:25 -07003217 // removeEffect_l() has stopped the effect if it was active so it must be restarted
3218 if (effect->state() == EffectModule::ACTIVE ||
3219 effect->state() == EffectModule::STOPPING) {
3220 effect->start();
3221 }
Eric Laurent39e94f82010-07-28 01:32:47 -07003222 // if the move request is not received from audio policy manager, the effect must be
3223 // re-registered with the new strategy and output
3224 if (dstChain == 0) {
3225 dstChain = effect->chain().promote();
3226 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003227 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003228 status = NO_INIT;
3229 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07003230 }
3231 strategy = dstChain->strategy();
3232 }
3233 if (reRegister) {
3234 AudioSystem::unregisterEffect(effect->id());
3235 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07003236 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07003237 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07003238 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07003239 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003240 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07003241 }
Eric Laurentde070132010-07-13 04:45:46 -07003242 effect = chain->getEffectFromId_l(0);
3243 }
3244
Eric Laurent5baf2af2013-09-12 17:37:00 -07003245 if (status != NO_ERROR) {
3246 for (size_t i = 0; i < removed.size(); i++) {
3247 srcThread->addEffect_l(removed[i]);
3248 if (dstChain != 0 && reRegister) {
3249 AudioSystem::unregisterEffect(removed[i]->id());
3250 AudioSystem::registerEffect(&removed[i]->desc(),
3251 srcThread->id(),
3252 strategy,
3253 sessionId,
3254 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003255 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003256 }
3257 }
3258 }
3259
3260 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003261}
3262
Eric Laurent5baf2af2013-09-12 17:37:00 -07003263bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07003264{
3265 if (mGlobalEffectEnableTime != 0 &&
3266 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
3267 return true;
3268 }
3269
3270 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3271 sp<EffectChain> ec =
3272 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003273 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07003274 return true;
3275 }
3276 }
3277 return false;
3278}
3279
Eric Laurent5baf2af2013-09-12 17:37:00 -07003280void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07003281{
3282 Mutex::Autolock _l(mLock);
3283
3284 mGlobalEffectEnableTime = systemTime();
3285
3286 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3287 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
3288 if (t->mType == ThreadBase::OFFLOAD) {
3289 t->invalidateTracks(AUDIO_STREAM_MUSIC);
3290 }
3291 }
3292
3293}
3294
Eric Laurentaaa44472014-09-12 17:41:50 -07003295status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
3296{
Eric Laurentd8365c52017-07-16 15:27:05 -07003297 // clear possible suspended state before parking the chain so that it starts in default state
3298 // when attached to a new record thread
3299 chain->setEffectSuspended_l(FX_IID_AEC, false);
3300 chain->setEffectSuspended_l(FX_IID_NS, false);
3301
Glenn Kastend848eb42016-03-08 13:42:11 -08003302 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003303 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003304 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003305 if (index >= 0) {
3306 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
3307 return ALREADY_EXISTS;
3308 }
3309 mOrphanEffectChains.add(session, chain);
3310 return NO_ERROR;
3311}
3312
3313sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
3314{
3315 sp<EffectChain> chain;
3316 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003317 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003318 if (index >= 0) {
3319 chain = mOrphanEffectChains.valueAt(index);
3320 mOrphanEffectChains.removeItemsAt(index);
3321 }
3322 return chain;
3323}
3324
3325bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
3326{
3327 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08003328 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003329 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003330 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003331 if (index >= 0) {
3332 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003333 if (chain->removeEffect_l(effect, true) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003334 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003335 mOrphanEffectChains.removeItemsAt(index);
3336 }
3337 return true;
3338 }
3339 return false;
3340}
3341
3342
Mathias Agopian65ab4712010-07-14 17:59:35 -07003343// ----------------------------------------------------------------------------
3344
3345status_t AudioFlinger::onTransact(
3346 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3347{
3348 return BnAudioFlinger::onTransact(code, data, reply, flags);
3349}
3350
Glenn Kasten63238ef2015-03-02 15:50:29 -08003351} // namespace android