blob: 0237aee0f3ec01bbc76949e35614c91fe1938d59 [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"
Eric Tan7b651152018-07-13 10:17:19 -070023#include <algorithm> // std::any_of
Glenn Kastenda6ef132013-01-10 12:31:01 -080024#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <math.h>
26#include <signal.h>
Eric Tan7b651152018-07-13 10:17:19 -070027#include <string>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028#include <sys/time.h>
29#include <sys/resource.h>
30
Gloria Wang9ee159b2011-02-24 14:51:45 -080031#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070032#include <binder/IServiceManager.h>
33#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070034#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <binder/Parcel.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070036#include <media/audiohal/DeviceHalInterface.h>
37#include <media/audiohal/DevicesFactoryHalInterface.h>
38#include <media/audiohal/EffectsFactoryHalInterface.h>
Mikhail Naganov00260b52016-10-13 12:54:24 -070039#include <media/AudioParameter.h>
Mikhail Naganov913d06c2016-11-01 12:49:22 -070040#include <media/TypeConverter.h>
Andy Hung35fec5f2016-04-13 14:21:48 -070041#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042#include <utils/String16.h>
43#include <utils/threads.h>
44
Steven Morelandf0c02ce2018-02-23 14:53:55 -080045#include <cutils/atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070046#include <cutils/properties.h>
47
Dima Zavin64760242011-05-11 14:15:23 -070048#include <system/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
Mathias Agopian65ab4712010-07-14 17:59:35 -070050#include "AudioFlinger.h"
Andy Hung8946a282018-04-19 20:04:56 -070051#include "NBAIO_Tee.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Andy Hung6770c6f2015-04-07 13:43:36 -070053#include <media/AudioResamplerPublic.h>
54
Mikhail Naganov9fe94012016-10-14 14:57:40 -070055#include <system/audio_effects/effect_visualizer.h>
56#include <system/audio_effects/effect_ns.h>
57#include <system/audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070058
Glenn Kasten3b21c502011-12-15 09:52:39 -080059#include <audio_utils/primitives.h>
60
Eric Laurentfeb0db62011-07-22 09:04:31 -070061#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080062
Glenn Kasten9e58b552013-01-18 15:09:48 -080063#include <media/IMediaLogService.h>
Andy Hung07b745e2016-05-23 16:21:07 -070064#include <media/MemoryLeakTrackUtil.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080065#include <media/nbaio/Pipe.h>
66#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070067#include <media/AudioParameter.h>
Wei Jia3f273d12015-11-24 09:06:49 -080068#include <mediautils/BatteryNotifier.h>
Andy Hungab7ef302018-05-15 19:35:29 -070069#include <mediautils/ServiceUtilities.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070070#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080071
rago3bd1c872016-09-26 12:58:14 -070072//#define BUFLOG_NDEBUG 0
73#include <BufLog.h>
74
Nicolas Rouletfe1e1442017-01-30 12:02:03 -080075#include "TypedLogger.h"
76
Mathias Agopian65ab4712010-07-14 17:59:35 -070077// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070078
John Grossman1c345192012-03-27 14:00:17 -070079// Note: the following macro is used for extremely verbose logging message. In
80// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
81// 0; but one side effect of this is to turn all LOGV's as well. Some messages
82// are so verbose that we want to suppress them even when we have ALOG_ASSERT
83// turned on. Do not uncomment the #def below unless you really know what you
84// are doing and want to see all of the extremely verbose messages.
85//#define VERY_VERY_VERBOSE_LOGGING
86#ifdef VERY_VERY_VERBOSE_LOGGING
87#define ALOGVV ALOGV
88#else
89#define ALOGVV(a...) do { } while(0)
90#endif
Eric Laurentde070132010-07-13 04:45:46 -070091
Mathias Agopian65ab4712010-07-14 17:59:35 -070092namespace android {
93
Glenn Kastenec1d6b52011-12-12 09:04:45 -080094static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
95static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070096static const char kClientLockedString[] = "Client lock is taken\n";
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070097static const char kNoEffectsFactory[] = "Effects Factory is absent\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070098
Glenn Kasten58912562012-04-03 10:45:00 -070099
John Grossman4ff14ba2012-02-08 16:37:41 -0800100nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -0800101
Eric Laurent81784c32012-11-19 14:55:58 -0800102uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -0700103
Eric Laurent813e2a72013-08-31 12:59:48 -0700104// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
105// we define a minimum time during which a global effect is considered enabled.
106static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
107
Eric Laurentfc235202016-12-20 18:48:17 -0800108Mutex gLock;
109wp<AudioFlinger> gAudioFlinger;
110
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700111// Keep a strong reference to media.log service around forever.
112// The service is within our parent process so it can never die in a way that we could observe.
113// These two variables are const after initialization.
114static sp<IBinder> sMediaLogServiceAsBinder;
115static sp<IMediaLogService> sMediaLogService;
116
117static pthread_once_t sMediaLogOnce = PTHREAD_ONCE_INIT;
118
119static void sMediaLogInit()
120{
121 sMediaLogServiceAsBinder = defaultServiceManager()->getService(String16("media.log"));
122 if (sMediaLogServiceAsBinder != 0) {
123 sMediaLogService = interface_cast<IMediaLogService>(sMediaLogServiceAsBinder);
124 }
125}
126
Mathias Agopian65ab4712010-07-14 17:59:35 -0700127// ----------------------------------------------------------------------------
128
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700129std::string formatToString(audio_format_t format) {
130 std::string result;
131 FormatConverter::toString(format, result);
132 return result;
Marco Nelissenb2208842014-02-07 14:00:50 -0800133}
134
Mathias Agopian65ab4712010-07-14 17:59:35 -0700135// ----------------------------------------------------------------------------
136
137AudioFlinger::AudioFlinger()
138 : BnAudioFlinger(),
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -0800139 mMediaLogNotifier(new AudioFlinger::MediaLogNotifier()),
John Grossman4ff14ba2012-02-08 16:37:41 -0800140 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800141 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700142 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800143 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800144 mMasterMute(false),
Glenn Kastend2e67e12016-04-11 08:26:37 -0700145 // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),
John Grossman4ff14ba2012-02-08 16:37:41 -0800146 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700147 mBtNrecIsOff(false),
148 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700149 mIsDeviceTypeKnown(false),
Andy Hung6f248bb2018-01-23 14:04:37 -0800150 mTotalMemory(0),
151 mClientSharedHeapSize(kMinimumClientSharedHeapSizeBytes),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700152 mGlobalEffectEnableTime(0),
Mikhail Naganovdea53042018-04-26 13:10:21 -0700153 mPatchPanel(this),
Eric Laurent72e3f392015-05-20 14:43:50 -0700154 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155{
Glenn Kastend2e67e12016-04-11 08:26:37 -0700156 // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum
157 for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) {
158 // zero ID has a special meaning, so unavailable
159 mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX;
160 }
161
Glenn Kastenae0cff12016-02-24 13:57:49 -0800162 const bool doLog = property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800163 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800164 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
165 MemoryHeapBase::READ_ONLY);
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700166 (void) pthread_once(&sMediaLogOnce, sMediaLogInit);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800167 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700168
Wei Jia3f273d12015-11-24 09:06:49 -0800169 // reset battery stats.
170 // if the audio service has crashed, battery stats could be left
171 // in bad state, reset the state upon service start.
172 BatteryNotifier::getInstance().noteResetAudio();
173
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700174 mDevicesFactoryHal = DevicesFactoryHalInterface::create();
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700175 mEffectsFactoryHal = EffectsFactoryHalInterface::create();
176
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -0800177 mMediaLogNotifier->run("MediaLogNotifier");
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700178}
179
180void AudioFlinger::onFirstRef()
181{
Eric Laurent93575202011-01-18 18:39:02 -0800182 Mutex::Autolock _l(mLock);
183
Dima Zavin799a70e2011-04-18 16:57:27 -0700184 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800185 char val_str[PROPERTY_VALUE_MAX] = { 0 };
186 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
187 uint32_t int_val;
188 if (1 == sscanf(val_str, "%u", &int_val)) {
189 mStandbyTimeInNsecs = milliseconds(int_val);
190 ALOGI("Using %u mSec as standby time.", int_val);
191 } else {
192 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
193 ALOGI("Using default %u mSec as standby time.",
194 (uint32_t)(mStandbyTimeInNsecs / 1000000));
195 }
196 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700197
Eric Laurenta4c5a552012-03-29 10:12:40 -0700198 mMode = AUDIO_MODE_NORMAL;
Eric Laurentfc235202016-12-20 18:48:17 -0800199
200 gAudioFlinger = this;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700201}
202
203AudioFlinger::~AudioFlinger()
204{
205 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700206 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700207 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208 }
209 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700210 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700211 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700213
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800214 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
215 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700216 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700218
219 // Tell media.log service about any old writers that still need to be unregistered
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700220 if (sMediaLogService != 0) {
221 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
222 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
223 mUnregisteredWriters.pop();
224 sMediaLogService->unregisterWriter(iMemory);
Glenn Kasten481fb672013-09-30 14:39:28 -0700225 }
226 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700227}
228
Eric Laurentfc235202016-12-20 18:48:17 -0800229//static
Eric Laurent6acd1d42017-01-04 14:23:29 -0800230__attribute__ ((visibility ("default")))
Eric Laurentfc235202016-12-20 18:48:17 -0800231status_t MmapStreamInterface::openMmapStream(MmapStreamInterface::stream_direction_t direction,
232 const audio_attributes_t *attr,
233 audio_config_base_t *config,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700234 const AudioClient& client,
Eric Laurentfc235202016-12-20 18:48:17 -0800235 audio_port_handle_t *deviceId,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800236 audio_session_t *sessionId,
Eric Laurentfc235202016-12-20 18:48:17 -0800237 const sp<MmapStreamCallback>& callback,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700238 sp<MmapStreamInterface>& interface,
239 audio_port_handle_t *handle)
Eric Laurentfc235202016-12-20 18:48:17 -0800240{
241 sp<AudioFlinger> af;
242 {
243 Mutex::Autolock _l(gLock);
244 af = gAudioFlinger.promote();
245 }
246 status_t ret = NO_INIT;
247 if (af != 0) {
248 ret = af->openMmapStream(
Phil Burk4e1af9f2018-01-03 15:54:35 -0800249 direction, attr, config, client, deviceId,
250 sessionId, callback, interface, handle);
Eric Laurentfc235202016-12-20 18:48:17 -0800251 }
252 return ret;
253}
254
Eric Laurent6acd1d42017-01-04 14:23:29 -0800255status_t AudioFlinger::openMmapStream(MmapStreamInterface::stream_direction_t direction,
256 const audio_attributes_t *attr,
257 audio_config_base_t *config,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700258 const AudioClient& client,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800259 audio_port_handle_t *deviceId,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800260 audio_session_t *sessionId,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800261 const sp<MmapStreamCallback>& callback,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700262 sp<MmapStreamInterface>& interface,
263 audio_port_handle_t *handle)
Eric Laurentfc235202016-12-20 18:48:17 -0800264{
265 status_t ret = initCheck();
266 if (ret != NO_ERROR) {
267 return ret;
268 }
Phil Burk4e1af9f2018-01-03 15:54:35 -0800269 audio_session_t actualSessionId = *sessionId;
270 if (actualSessionId == AUDIO_SESSION_ALLOCATE) {
271 actualSessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
272 }
Eric Laurent6acd1d42017-01-04 14:23:29 -0800273 audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700274 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800275 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
276 if (direction == MmapStreamInterface::DIRECTION_OUTPUT) {
277 audio_config_t fullConfig = AUDIO_CONFIG_INITIALIZER;
278 fullConfig.sample_rate = config->sample_rate;
279 fullConfig.channel_mask = config->channel_mask;
280 fullConfig.format = config->format;
281 ret = AudioSystem::getOutputForAttr(attr, &io,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800282 actualSessionId,
Nadav Bar766fb022018-01-07 12:18:03 +0200283 &streamType, client.clientPid, client.clientUid,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800284 &fullConfig,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800285 (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ |
286 AUDIO_OUTPUT_FLAG_DIRECT),
Eric Laurent2ac76942017-06-22 17:17:09 -0700287 deviceId, &portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800288 } else {
289 ret = AudioSystem::getInputForAttr(attr, &io,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800290 actualSessionId,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800291 client.clientPid,
292 client.clientUid,
Eric Laurentfee19762018-01-29 18:44:13 -0800293 client.packageName,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800294 config,
Eric Laurent2ac76942017-06-22 17:17:09 -0700295 AUDIO_INPUT_FLAG_MMAP_NOIRQ, deviceId, &portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800296 }
297 if (ret != NO_ERROR) {
298 return ret;
299 }
300
301 // at this stage, a MmapThread was created when openOutput() or openInput() was called by
302 // audio policy manager and we can retrieve it
303 sp<MmapThread> thread = mMmapThreads.valueFor(io);
304 if (thread != 0) {
305 interface = new MmapThreadHandle(thread);
Phil Burk4e1af9f2018-01-03 15:54:35 -0800306 thread->configure(attr, streamType, actualSessionId, callback, *deviceId, portId);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700307 *handle = portId;
Phil Burk4e1af9f2018-01-03 15:54:35 -0800308 *sessionId = actualSessionId;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800309 } else {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700310 if (direction == MmapStreamInterface::DIRECTION_OUTPUT) {
Phil Burk4e1af9f2018-01-03 15:54:35 -0800311 AudioSystem::releaseOutput(io, streamType, actualSessionId);
Eric Laurentad2e7b92017-09-14 20:06:42 -0700312 } else {
Eric Laurentfee19762018-01-29 18:44:13 -0800313 AudioSystem::releaseInput(portId);
Eric Laurentad2e7b92017-09-14 20:06:42 -0700314 }
Eric Laurent6acd1d42017-01-04 14:23:29 -0800315 ret = NO_INIT;
316 }
317
318 ALOGV("%s done status %d portId %d", __FUNCTION__, ret, portId);
319
Eric Laurentfc235202016-12-20 18:48:17 -0800320 return ret;
321}
322
Eric Laurenta4c5a552012-03-29 10:12:40 -0700323static const char * const audio_interfaces[] = {
324 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
325 AUDIO_HARDWARE_MODULE_ID_A2DP,
326 AUDIO_HARDWARE_MODULE_ID_USB,
327};
Eric Laurenta4c5a552012-03-29 10:12:40 -0700328
Phil Burk062e67a2015-02-11 13:40:50 -0800329AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700330 audio_module_handle_t module,
331 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700332{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700333 // if module is 0, the request comes from an old policy manager and we should load
334 // well known modules
335 if (module == 0) {
336 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
Mikhail Naganovbf493082017-04-17 17:37:12 -0700337 for (size_t i = 0; i < arraysize(audio_interfaces); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700338 loadHwModule_l(audio_interfaces[i]);
339 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700340 // then try to find a module supporting the requested device.
341 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
342 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700343 sp<DeviceHalInterface> dev = audioHwDevice->hwDevice();
344 uint32_t supportedDevices;
345 if (dev->getSupportedDevices(&supportedDevices) == OK &&
346 (supportedDevices & devices) == devices) {
Eric Laurentf1c04f92012-08-28 14:26:53 -0700347 return audioHwDevice;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700348 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700349 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700350 } else {
351 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700352 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
353 if (audioHwDevice != NULL) {
354 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700355 }
356 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700357
Dima Zavin799a70e2011-04-18 16:57:27 -0700358 return NULL;
359}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360
Glenn Kasten0f11b512014-01-31 16:18:54 -0800361void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362{
363 const size_t SIZE = 256;
364 char buffer[SIZE];
365 String8 result;
366
367 result.append("Clients:\n");
368 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800369 sp<Client> client = mClients.valueAt(i).promote();
370 if (client != 0) {
371 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
372 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 }
374 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700375
Eric Laurentd1b28d42013-09-18 18:47:13 -0700376 result.append("Notification Clients:\n");
377 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
378 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
379 result.append(buffer);
380 }
381
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700382 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800383 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700384 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
385 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800386 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700387 result.append(buffer);
388 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390}
391
392
Glenn Kasten0f11b512014-01-31 16:18:54 -0800393void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394{
395 const size_t SIZE = 256;
396 char buffer[SIZE];
397 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800398 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700399
John Grossman4ff14ba2012-02-08 16:37:41 -0800400 snprintf(buffer, SIZE, "Hardware status: %d\n"
401 "Standby Time mSec: %u\n",
402 hardwareStatus,
403 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404 result.append(buffer);
405 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700406}
407
Glenn Kasten0f11b512014-01-31 16:18:54 -0800408void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409{
410 const size_t SIZE = 256;
411 char buffer[SIZE];
412 String8 result;
413 snprintf(buffer, SIZE, "Permission Denial: "
414 "can't dump AudioFlinger from pid=%d, uid=%d\n",
415 IPCThreadState::self()->getCallingPid(),
416 IPCThreadState::self()->getCallingUid());
417 result.append(buffer);
418 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700419}
420
Eric Laurent81784c32012-11-19 14:55:58 -0800421bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422{
423 bool locked = false;
424 for (int i = 0; i < kDumpLockRetries; ++i) {
425 if (mutex.tryLock() == NO_ERROR) {
426 locked = true;
427 break;
428 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800429 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430 }
431 return locked;
432}
433
434status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
435{
Glenn Kasten44deb052012-02-05 18:09:08 -0800436 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700437 dumpPermissionDenial(fd, args);
438 } else {
Eric Tan7b651152018-07-13 10:17:19 -0700439 // XXX This is sort of hacky for now.
440 const bool formatJson = std::any_of(args.begin(), args.end(),
441 [](const String16 &arg) { return arg == String16("--json"); });
442 if (formatJson) {
443 // XXX consider buffering if the string happens to be too long.
444 dprintf(fd, "%s", getJsonString().c_str());
445 return NO_ERROR;
446 }
447
Mathias Agopian65ab4712010-07-14 17:59:35 -0700448 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800449 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700450 if (!hardwareLocked) {
451 String8 result(kHardwareLockedString);
452 write(fd, result.string(), result.size());
453 } else {
454 mHardwareLock.unlock();
455 }
456
Eric Tan7b651152018-07-13 10:17:19 -0700457 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700458
459 // failed to lock - AudioFlinger is probably deadlocked
460 if (!locked) {
461 String8 result(kDeadlockedString);
462 write(fd, result.string(), result.size());
463 }
464
Eric Laurent021cf962014-05-13 10:18:14 -0700465 bool clientLocked = dumpTryLock(mClientLock);
466 if (!clientLocked) {
467 String8 result(kClientLockedString);
468 write(fd, result.string(), result.size());
469 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700470
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700471 if (mEffectsFactoryHal != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700472 mEffectsFactoryHal->dumpEffects(fd);
473 } else {
474 String8 result(kNoEffectsFactory);
475 write(fd, result.string(), result.size());
476 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700477
Mathias Agopian65ab4712010-07-14 17:59:35 -0700478 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700479 if (clientLocked) {
480 mClientLock.unlock();
481 }
482
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483 dumpInternals(fd, args);
484
485 // dump playback threads
486 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
487 mPlaybackThreads.valueAt(i)->dump(fd, args);
488 }
489
490 // dump record threads
491 for (size_t i = 0; i < mRecordThreads.size(); i++) {
492 mRecordThreads.valueAt(i)->dump(fd, args);
493 }
494
Eric Laurent6acd1d42017-01-04 14:23:29 -0800495 // dump mmap threads
496 for (size_t i = 0; i < mMmapThreads.size(); i++) {
497 mMmapThreads.valueAt(i)->dump(fd, args);
498 }
499
Eric Laurentaaa44472014-09-12 17:41:50 -0700500 // dump orphan effect chains
501 if (mOrphanEffectChains.size() != 0) {
502 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
503 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
504 mOrphanEffectChains.valueAt(i)->dump(fd, args);
505 }
506 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700507 // dump all hardware devs
508 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700509 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
510 dev->dump(fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700512
Mikhail Naganov201369b2018-05-16 16:52:32 -0700513 mPatchPanel.dump(fd);
514
rago3bd1c872016-09-26 12:58:14 -0700515 BUFLOG_RESET;
516
Glenn Kastend65d73c2012-06-22 17:21:07 -0700517 if (locked) {
518 mLock.unlock();
519 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800520
Andy Hung8946a282018-04-19 20:04:56 -0700521#ifdef TEE_SINK
522 // NBAIO_Tee dump is safe to call outside of AF lock.
523 NBAIO_Tee::dumpAll(fd, "_DUMP");
524#endif
Glenn Kasten9e58b552013-01-18 15:09:48 -0800525 // append a copy of media.log here by forwarding fd to it, but don't attempt
526 // to lookup the service if it's not running, as it will block for a second
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700527 if (sMediaLogServiceAsBinder != 0) {
528 dprintf(fd, "\nmedia.log:\n");
529 Vector<String16> args;
530 sMediaLogServiceAsBinder->dump(fd, args);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800531 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700532
533 // check for optional arguments
Andy Hung07b745e2016-05-23 16:21:07 -0700534 bool dumpMem = false;
Andy Hung35fec5f2016-04-13 14:21:48 -0700535 bool unreachableMemory = false;
536 for (const auto &arg : args) {
Andy Hung07b745e2016-05-23 16:21:07 -0700537 if (arg == String16("-m")) {
538 dumpMem = true;
539 } else if (arg == String16("--unreachable")) {
Andy Hung35fec5f2016-04-13 14:21:48 -0700540 unreachableMemory = true;
541 }
542 }
543
Andy Hung07b745e2016-05-23 16:21:07 -0700544 if (dumpMem) {
545 dprintf(fd, "\nDumping memory:\n");
546 std::string s = dumpMemoryAddresses(100 /* limit */);
547 write(fd, s.c_str(), s.size());
548 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700549 if (unreachableMemory) {
550 dprintf(fd, "\nDumping unreachable memory:\n");
551 // TODO - should limit be an argument parameter?
Andy Hung07b745e2016-05-23 16:21:07 -0700552 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
Andy Hung35fec5f2016-04-13 14:21:48 -0700553 write(fd, s.c_str(), s.size());
554 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700555 }
556 return NO_ERROR;
557}
558
Eric Tan7b651152018-07-13 10:17:19 -0700559std::string AudioFlinger::getJsonString()
560{
561 std::string jsonStr = "{\n";
562 const bool locked = dumpTryLock(mLock);
563
564 // failed to lock - AudioFlinger is probably deadlocked
565 if (!locked) {
566 jsonStr += " \"deadlock_message\": ";
567 jsonStr += kDeadlockedString;
568 jsonStr += ",\n";
569 }
570 // FIXME risky to access data structures without a lock held?
571
572 jsonStr += " \"Playback_Threads\": [\n";
573 // dump playback threads
574 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
575 if (i != 0) {
576 jsonStr += ",\n";
577 }
578 jsonStr += mPlaybackThreads.valueAt(i)->getJsonString();
579 }
580 jsonStr += "\n ]\n}\n";
581
582 if (locked) {
583 mLock.unlock();
584 }
585
586 return jsonStr;
587}
588
Eric Laurent021cf962014-05-13 10:18:14 -0700589sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800590{
Eric Laurent021cf962014-05-13 10:18:14 -0700591 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800592 // If pid is already in the mClients wp<> map, then use that entry
593 // (for which promote() is always != 0), otherwise create a new entry and Client.
594 sp<Client> client = mClients.valueFor(pid).promote();
595 if (client == 0) {
596 client = new Client(this, pid);
597 mClients.add(pid, client);
598 }
599
600 return client;
601}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602
Glenn Kasten9e58b552013-01-18 15:09:48 -0800603sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
604{
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700605 // If there is no memory allocated for logs, return a dummy writer that does nothing.
606 // Similarly if we can't contact the media.log service, also return a dummy writer.
607 if (mLogMemoryDealer == 0 || sMediaLogService == 0) {
Glenn Kasten9e58b552013-01-18 15:09:48 -0800608 return new NBLog::Writer();
609 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700610 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
611 // If allocation fails, consult the vector of previously unregistered writers
612 // and garbage-collect one or more them until an allocation succeeds
613 if (shared == 0) {
614 Mutex::Autolock _l(mUnregisteredWritersLock);
615 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
616 {
617 // Pick the oldest stale writer to garbage-collect
618 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
619 mUnregisteredWriters.removeAt(0);
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700620 sMediaLogService->unregisterWriter(iMemory);
Glenn Kasten481fb672013-09-30 14:39:28 -0700621 // Now the media.log remote reference to IMemory is gone. When our last local
622 // reference to IMemory also drops to zero at end of this block,
623 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
624 }
625 // Re-attempt the allocation
626 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
627 if (shared != 0) {
628 goto success;
629 }
630 }
631 // Even after garbage-collecting all old writers, there is still not enough memory,
632 // so return a dummy writer
633 return new NBLog::Writer();
634 }
635success:
Glenn Kasten535e1612016-12-05 12:19:36 -0800636 NBLog::Shared *sharedRawPtr = (NBLog::Shared *) shared->pointer();
637 new((void *) sharedRawPtr) NBLog::Shared(); // placement new here, but the corresponding
638 // explicit destructor not needed since it is POD
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700639 sMediaLogService->registerWriter(shared, size, name);
Glenn Kasten535e1612016-12-05 12:19:36 -0800640 return new NBLog::Writer(shared, size);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800641}
642
643void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
644{
Glenn Kasten685ef092013-02-04 08:15:34 -0800645 if (writer == 0) {
646 return;
647 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800648 sp<IMemory> iMemory(writer->getIMemory());
649 if (iMemory == 0) {
650 return;
651 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700652 // Rather than removing the writer immediately, append it to a queue of old writers to
653 // be garbage-collected later. This allows us to continue to view old logs for a while.
654 Mutex::Autolock _l(mUnregisteredWritersLock);
655 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800656}
657
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658// IAudioFlinger interface
659
Eric Laurent21da6472017-11-09 16:29:26 -0800660sp<IAudioTrack> AudioFlinger::createTrack(const CreateTrackInput& input,
661 CreateTrackOutput& output,
662 status_t *status)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663{
664 sp<PlaybackThread::Track> track;
665 sp<TrackHandle> trackHandle;
666 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667 status_t lStatus;
Eric Laurent21da6472017-11-09 16:29:26 -0800668 audio_stream_type_t streamType;
Eric Laurent77881cd2018-02-09 16:01:31 -0800669 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670
Eric Laurent21da6472017-11-09 16:29:26 -0800671 bool updatePid = (input.clientInfo.clientPid == -1);
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700672 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurent21da6472017-11-09 16:29:26 -0800673 uid_t clientUid = input.clientInfo.clientUid;
Andy Hung4ef19fa2018-05-15 19:35:29 -0700674 if (!isAudioServerOrMediaServerUid(callingUid)) {
Eric Laurent21da6472017-11-09 16:29:26 -0800675 ALOGW_IF(clientUid != callingUid,
676 "%s uid %d tried to pass itself off as %d",
677 __FUNCTION__, callingUid, clientUid);
678 clientUid = callingUid;
679 updatePid = true;
680 }
681 pid_t clientPid = input.clientInfo.clientPid;
682 if (updatePid) {
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700683 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurent21da6472017-11-09 16:29:26 -0800684 ALOGW_IF(clientPid != -1 && clientPid != callingPid,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700685 "%s uid %d pid %d tried to pass itself off as pid %d",
Eric Laurent21da6472017-11-09 16:29:26 -0800686 __func__, callingUid, callingPid, clientPid);
687 clientPid = callingPid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700688 }
689
Eric Laurent21da6472017-11-09 16:29:26 -0800690 audio_session_t sessionId = input.sessionId;
691 if (sessionId == AUDIO_SESSION_ALLOCATE) {
692 sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Eric Laurentf14db3c2017-12-08 14:20:36 -0800693 } else if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
694 lStatus = BAD_VALUE;
695 goto Exit;
Eric Laurent21da6472017-11-09 16:29:26 -0800696 }
Eric Laurentf14db3c2017-12-08 14:20:36 -0800697
Eric Laurent21da6472017-11-09 16:29:26 -0800698 output.sessionId = sessionId;
699 output.outputId = AUDIO_IO_HANDLE_NONE;
700 output.selectedDeviceId = input.selectedDeviceId;
701
702 lStatus = AudioSystem::getOutputForAttr(&input.attr, &output.outputId, sessionId, &streamType,
Nadav Bar766fb022018-01-07 12:18:03 +0200703 clientPid, clientUid, &input.config, input.flags,
Eric Laurent21da6472017-11-09 16:29:26 -0800704 &output.selectedDeviceId, &portId);
705
706 if (lStatus != NO_ERROR || output.outputId == AUDIO_IO_HANDLE_NONE) {
707 ALOGE("createTrack() getOutputForAttr() return error %d or invalid output handle", lStatus);
708 goto Exit;
709 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800710 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
711 // but if someone uses binder directly they could bypass that and cause us to crash
712 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000713 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 lStatus = BAD_VALUE;
715 goto Exit;
716 }
717
Glenn Kasten53b5d092014-02-05 10:00:23 -0800718 // further channel mask checks are performed by createTrack_l() depending on the thread type
Eric Laurent21da6472017-11-09 16:29:26 -0800719 if (!audio_is_output_channel(input.config.channel_mask)) {
720 ALOGE("createTrack() invalid channel mask %#x", input.config.channel_mask);
Glenn Kasten53b5d092014-02-05 10:00:23 -0800721 lStatus = BAD_VALUE;
722 goto Exit;
723 }
724
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700725 // further format checks are performed by createTrack_l() depending on the thread type
Eric Laurent21da6472017-11-09 16:29:26 -0800726 if (!audio_is_valid_format(input.config.format)) {
727 ALOGE("createTrack() invalid format %#x", input.config.format);
Glenn Kasten663c2242013-09-24 11:52:37 -0700728 lStatus = BAD_VALUE;
729 goto Exit;
730 }
731
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732 {
733 Mutex::Autolock _l(mLock);
Eric Laurent21da6472017-11-09 16:29:26 -0800734 PlaybackThread *thread = checkPlaybackThread_l(output.outputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 if (thread == NULL) {
Eric Laurent21da6472017-11-09 16:29:26 -0800736 ALOGE("no playback thread found for output handle %d", output.outputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700737 lStatus = BAD_VALUE;
738 goto Exit;
739 }
740
Eric Laurent21da6472017-11-09 16:29:26 -0800741 client = registerPid(clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742
Glenn Kastene848bd92014-03-13 15:00:32 -0700743 PlaybackThread *effectThread = NULL;
Eric Laurent21da6472017-11-09 16:29:26 -0800744 // check if an effect chain with the same session ID is present on another
745 // output thread and move it here.
746 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
747 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
748 if (mPlaybackThreads.keyAt(i) != output.outputId) {
749 uint32_t sessions = t->hasAudioSession(sessionId);
750 if (sessions & ThreadBase::EFFECT_SESSION) {
751 effectThread = t.get();
752 break;
Eric Laurentde070132010-07-13 04:45:46 -0700753 }
754 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755 }
Eric Laurent21da6472017-11-09 16:29:26 -0800756 ALOGV("createTrack() sessionId: %d", sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757
Eric Laurent21da6472017-11-09 16:29:26 -0800758 output.sampleRate = input.config.sample_rate;
759 output.frameCount = input.frameCount;
760 output.notificationFrameCount = input.notificationFrameCount;
761 output.flags = input.flags;
762
Kevin Rocard1f564ac2018-03-29 13:53:10 -0700763 track = thread->createTrack_l(client, streamType, input.attr, &output.sampleRate,
764 input.config.format, input.config.channel_mask,
Eric Laurent21da6472017-11-09 16:29:26 -0800765 &output.frameCount, &output.notificationFrameCount,
766 input.notificationsPerBuffer, input.speed,
767 input.sharedBuffer, sessionId, &output.flags,
768 input.clientInfo.clientTid, clientUid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800769 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700770 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700771
Eric Laurent21da6472017-11-09 16:29:26 -0800772 output.afFrameCount = thread->frameCount();
773 output.afSampleRate = thread->sampleRate();
774 output.afLatencyMs = thread->latency();
775
Eric Laurent39e94f82010-07-28 01:32:47 -0700776 // move effect chain to this output thread if an effect on same session was waiting
777 // for a track to be created
778 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700779 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700780 Mutex::Autolock _dl(thread->mLock);
781 Mutex::Autolock _sl(effectThread->mLock);
Eric Laurent21da6472017-11-09 16:29:26 -0800782 moveEffectChain_l(sessionId, effectThread, thread, true);
Eric Laurent39e94f82010-07-28 01:32:47 -0700783 }
Eric Laurenta011e352012-03-29 15:51:43 -0700784
785 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700786 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurent21da6472017-11-09 16:29:26 -0800787 if (mPendingSyncEvents[i]->triggerSession() == sessionId) {
Eric Laurenta011e352012-03-29 15:51:43 -0700788 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700789 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700790 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700791 } else {
792 mPendingSyncEvents[i]->cancel();
793 }
Eric Laurenta011e352012-03-29 15:51:43 -0700794 mPendingSyncEvents.removeAt(i);
795 i--;
796 }
797 }
798 }
Glenn Kastene198c362013-08-13 09:13:36 -0700799
Eric Laurent21da6472017-11-09 16:29:26 -0800800 setAudioHwSyncForSession_l(thread, sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801 }
Glenn Kastene198c362013-08-13 09:13:36 -0700802
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700803 if (lStatus != NO_ERROR) {
804 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700805 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700806 // Don't hold mClientLock when releasing the reference on the track as the
807 // destructor will acquire it.
808 {
809 Mutex::Autolock _cl(mClientLock);
810 client.clear();
811 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700813 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 }
815
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700816 // return handle to client
817 trackHandle = new TrackHandle(track);
818
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819Exit:
Eric Laurent21da6472017-11-09 16:29:26 -0800820 if (lStatus != NO_ERROR && output.outputId != AUDIO_IO_HANDLE_NONE) {
821 AudioSystem::releaseOutput(output.outputId, streamType, sessionId);
822 }
Glenn Kasten9156ef32013-08-06 15:39:08 -0700823 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824 return trackHandle;
825}
826
Glenn Kasten2c073da2016-02-26 09:14:08 -0800827uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828{
829 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800830 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800832 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 return 0;
834 }
835 return thread->sampleRate();
836}
837
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800838audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839{
840 Mutex::Autolock _l(mLock);
841 PlaybackThread *thread = checkPlaybackThread_l(output);
842 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000843 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800844 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845 }
846 return thread->format();
847}
848
Glenn Kasten2c073da2016-02-26 09:14:08 -0800849size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850{
851 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800852 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800854 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 return 0;
856 }
Glenn Kasten58912562012-04-03 10:45:00 -0700857 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
858 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 return thread->frameCount();
860}
861
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700862size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
863{
864 Mutex::Autolock _l(mLock);
865 ThreadBase *thread = checkThread_l(ioHandle);
866 if (thread == NULL) {
867 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
868 return 0;
869 }
870 return thread->frameCountHAL();
871}
872
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800873uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700874{
875 Mutex::Autolock _l(mLock);
876 PlaybackThread *thread = checkPlaybackThread_l(output);
877 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800878 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879 return 0;
880 }
881 return thread->latency();
882}
883
884status_t AudioFlinger::setMasterVolume(float value)
885{
Eric Laurenta1884f92011-08-23 08:25:03 -0700886 status_t ret = initCheck();
887 if (ret != NO_ERROR) {
888 return ret;
889 }
890
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891 // check calling permissions
892 if (!settingsAllowed()) {
893 return PERMISSION_DENIED;
894 }
895
Eric Laurenta4c5a552012-03-29 10:12:40 -0700896 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700897 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700898
John Grossmanee578c02012-07-23 17:05:46 -0700899 // Set master volume in the HALs which support it.
900 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
901 AutoMutex lock(mHardwareLock);
902 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800903
John Grossmanee578c02012-07-23 17:05:46 -0700904 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
905 if (dev->canSetMasterVolume()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700906 dev->hwDevice()->setMasterVolume(value);
Eric Laurent93575202011-01-18 18:39:02 -0800907 }
John Grossmanee578c02012-07-23 17:05:46 -0700908 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910
John Grossmanee578c02012-07-23 17:05:46 -0700911 // Now set the master volume in each playback thread. Playback threads
912 // assigned to HALs which do not have master volume support will apply
913 // master volume during the mix operation. Threads with HALs which do
914 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700915 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
916 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
917 continue;
918 }
John Grossmanee578c02012-07-23 17:05:46 -0700919 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700920 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921
922 return NO_ERROR;
923}
924
Glenn Kastenf78aee72012-01-04 11:00:47 -0800925status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926{
Eric Laurenta1884f92011-08-23 08:25:03 -0700927 status_t ret = initCheck();
928 if (ret != NO_ERROR) {
929 return ret;
930 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931
932 // check calling permissions
933 if (!settingsAllowed()) {
934 return PERMISSION_DENIED;
935 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800936 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000937 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938 return BAD_VALUE;
939 }
940
941 { // scope for the lock
942 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700943 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700945 ret = dev->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 mHardwareStatus = AUDIO_HW_IDLE;
947 }
948
949 if (NO_ERROR == ret) {
950 Mutex::Autolock _l(mLock);
951 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800952 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700953 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954 }
955
956 return ret;
957}
958
959status_t AudioFlinger::setMicMute(bool state)
960{
Eric Laurenta1884f92011-08-23 08:25:03 -0700961 status_t ret = initCheck();
962 if (ret != NO_ERROR) {
963 return ret;
964 }
965
Mathias Agopian65ab4712010-07-14 17:59:35 -0700966 // check calling permissions
967 if (!settingsAllowed()) {
968 return PERMISSION_DENIED;
969 }
970
971 AutoMutex lock(mHardwareLock);
972 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700973 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700974 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
975 status_t result = dev->setMicMute(state);
Eric Laurent2f035f52014-09-14 12:11:52 -0700976 if (result != NO_ERROR) {
977 ret = result;
978 }
979 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700980 mHardwareStatus = AUDIO_HW_IDLE;
981 return ret;
982}
983
984bool AudioFlinger::getMicMute() const
985{
Eric Laurenta1884f92011-08-23 08:25:03 -0700986 status_t ret = initCheck();
987 if (ret != NO_ERROR) {
988 return false;
989 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800990 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700991 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800992 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800994 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700995 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
996 status_t result = dev->getMicMute(&state);
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800997 if (result == NO_ERROR) {
998 mute = mute && state;
999 }
1000 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001001 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -08001002
1003 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004}
1005
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001006void AudioFlinger::setRecordSilenced(uid_t uid, bool silenced)
1007{
1008 ALOGV("AudioFlinger::setRecordSilenced(uid:%d, silenced:%d)", uid, silenced);
1009
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001010 AutoMutex lock(mLock);
1011 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent331679c2018-04-16 17:03:16 -07001012 mRecordThreads[i]->setRecordSilenced(uid, silenced);
1013 }
1014 for (size_t i = 0; i < mMmapThreads.size(); i++) {
1015 mMmapThreads[i]->setRecordSilenced(uid, silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001016 }
1017}
1018
Mathias Agopian65ab4712010-07-14 17:59:35 -07001019status_t AudioFlinger::setMasterMute(bool muted)
1020{
John Grossmand8f178d2012-07-20 14:51:35 -07001021 status_t ret = initCheck();
1022 if (ret != NO_ERROR) {
1023 return ret;
1024 }
1025
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 // check calling permissions
1027 if (!settingsAllowed()) {
1028 return PERMISSION_DENIED;
1029 }
1030
John Grossmanee578c02012-07-23 17:05:46 -07001031 Mutex::Autolock _l(mLock);
1032 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -07001033
John Grossmanee578c02012-07-23 17:05:46 -07001034 // Set master mute in the HALs which support it.
1035 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1036 AutoMutex lock(mHardwareLock);
1037 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -07001038
John Grossmanee578c02012-07-23 17:05:46 -07001039 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1040 if (dev->canSetMasterMute()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001041 dev->hwDevice()->setMasterMute(muted);
John Grossmand8f178d2012-07-20 14:51:35 -07001042 }
John Grossmanee578c02012-07-23 17:05:46 -07001043 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -07001044 }
1045
John Grossmanee578c02012-07-23 17:05:46 -07001046 // Now set the master mute in each playback thread. Playback threads
1047 // assigned to HALs which do not have master mute support will apply master
1048 // mute during the mix operation. Threads with HALs which do support master
1049 // mute will simply ignore the setting.
Eric Laurent6acd1d42017-01-04 14:23:29 -08001050 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1051 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1052 volumeInterfaces[i]->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -07001053 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054
1055 return NO_ERROR;
1056}
1057
1058float AudioFlinger::masterVolume() const
1059{
Glenn Kasten98067102011-12-13 11:47:54 -08001060 Mutex::Autolock _l(mLock);
1061 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062}
1063
1064bool AudioFlinger::masterMute() const
1065{
Glenn Kasten98067102011-12-13 11:47:54 -08001066 Mutex::Autolock _l(mLock);
1067 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068}
1069
John Grossman4ff14ba2012-02-08 16:37:41 -08001070float AudioFlinger::masterVolume_l() const
1071{
John Grossman4ff14ba2012-02-08 16:37:41 -08001072 return mMasterVolume;
1073}
1074
John Grossmand8f178d2012-07-20 14:51:35 -07001075bool AudioFlinger::masterMute_l() const
1076{
John Grossmanee578c02012-07-23 17:05:46 -07001077 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -07001078}
1079
Eric Laurent223fd5c2014-11-11 13:43:36 -08001080status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
1081{
1082 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001083 ALOGW("checkStreamType() invalid stream %d", stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001084 return BAD_VALUE;
1085 }
Andy Hung4ef19fa2018-05-15 19:35:29 -07001086 const uid_t callerUid = IPCThreadState::self()->getCallingUid();
1087 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && !isAudioServerUid(callerUid)) {
1088 ALOGW("checkStreamType() uid %d cannot use internal stream type %d", callerUid, stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001089 return PERMISSION_DENIED;
1090 }
1091
1092 return NO_ERROR;
1093}
1094
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001095status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
1096 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001097{
1098 // check calling permissions
1099 if (!settingsAllowed()) {
1100 return PERMISSION_DENIED;
1101 }
1102
Eric Laurent223fd5c2014-11-11 13:43:36 -08001103 status_t status = checkStreamType(stream);
1104 if (status != NO_ERROR) {
1105 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106 }
Eric Laurent98e38192018-02-15 18:31:53 -08001107 if (output == AUDIO_IO_HANDLE_NONE) {
1108 return BAD_VALUE;
1109 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001110 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001111
1112 AutoMutex lock(mLock);
Eric Laurent98e38192018-02-15 18:31:53 -08001113 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1114 if (volumeInterface == NULL) {
1115 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001116 }
Eric Laurent98e38192018-02-15 18:31:53 -08001117 volumeInterface->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001118
1119 return NO_ERROR;
1120}
1121
Glenn Kastenfff6d712012-01-12 16:38:12 -08001122status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123{
1124 // check calling permissions
1125 if (!settingsAllowed()) {
1126 return PERMISSION_DENIED;
1127 }
1128
Eric Laurent223fd5c2014-11-11 13:43:36 -08001129 status_t status = checkStreamType(stream);
1130 if (status != NO_ERROR) {
1131 return status;
1132 }
1133 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1134
1135 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001136 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137 return BAD_VALUE;
1138 }
1139
Eric Laurent93575202011-01-18 18:39:02 -08001140 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141 mStreamTypes[stream].mute = muted;
Eric Laurent6acd1d42017-01-04 14:23:29 -08001142 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1143 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1144 volumeInterfaces[i]->setStreamMute(stream, muted);
1145 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146
1147 return NO_ERROR;
1148}
1149
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001150float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001152 status_t status = checkStreamType(stream);
1153 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154 return 0.0f;
1155 }
Eric Laurent98e38192018-02-15 18:31:53 -08001156 if (output == AUDIO_IO_HANDLE_NONE) {
1157 return 0.0f;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158 }
1159
Eric Laurent98e38192018-02-15 18:31:53 -08001160 AutoMutex lock(mLock);
1161 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1162 if (volumeInterface == NULL) {
1163 return 0.0f;
1164 }
1165
1166 return volumeInterface->streamVolume(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167}
1168
Glenn Kastenfff6d712012-01-12 16:38:12 -08001169bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001171 status_t status = checkStreamType(stream);
1172 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001173 return true;
1174 }
1175
Glenn Kasten6637baa2012-01-09 09:40:36 -08001176 AutoMutex lock(mLock);
1177 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178}
1179
Eric Laurent054d9d32015-04-24 08:48:48 -07001180
1181void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1182{
1183 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1184 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1185 }
1186}
1187
Eric Laurentf1047e82018-04-16 19:18:20 -07001188// Filter reserved keys from setParameters() before forwarding to audio HAL or acting upon.
1189// Some keys are used for audio routing and audio path configuration and should be reserved for use
1190// by audio policy and audio flinger for functional, privacy and security reasons.
1191void AudioFlinger::filterReservedParameters(String8& keyValuePairs, uid_t callingUid)
1192{
1193 static const String8 kReservedParameters[] = {
1194 String8(AudioParameter::keyRouting),
1195 String8(AudioParameter::keySamplingRate),
1196 String8(AudioParameter::keyFormat),
1197 String8(AudioParameter::keyChannels),
1198 String8(AudioParameter::keyFrameCount),
1199 String8(AudioParameter::keyInputSource),
1200 String8(AudioParameter::keyMonoOutput),
1201 String8(AudioParameter::keyStreamConnect),
1202 String8(AudioParameter::keyStreamDisconnect),
1203 String8(AudioParameter::keyStreamSupportedFormats),
1204 String8(AudioParameter::keyStreamSupportedChannels),
1205 String8(AudioParameter::keyStreamSupportedSamplingRates),
1206 };
1207
Andy Hung4ef19fa2018-05-15 19:35:29 -07001208 if (isAudioServerUid(callingUid)) {
1209 return; // no need to filter if audioserver.
Eric Laurentf1047e82018-04-16 19:18:20 -07001210 }
1211
1212 AudioParameter param = AudioParameter(keyValuePairs);
1213 String8 value;
1214 for (auto& key : kReservedParameters) {
1215 if (param.get(key, value) == NO_ERROR) {
1216 ALOGW("%s: filtering key %s value %s from uid %d",
1217 __func__, key.string(), value.string(), callingUid);
1218 param.remove(key);
1219 }
1220 }
1221 keyValuePairs = param.toString();
1222}
1223
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001224status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001225{
Eric Laurentf1047e82018-04-16 19:18:20 -07001226 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d calling uid %d",
1227 ioHandle, keyValuePairs.string(),
1228 IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid());
Eric Laurent81784c32012-11-19 14:55:58 -08001229
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230 // check calling permissions
1231 if (!settingsAllowed()) {
1232 return PERMISSION_DENIED;
1233 }
1234
Eric Laurentf1047e82018-04-16 19:18:20 -07001235 String8 filteredKeyValuePairs = keyValuePairs;
1236 filterReservedParameters(filteredKeyValuePairs, IPCThreadState::self()->getCallingUid());
1237
1238 ALOGV("%s: filtered keyvalue %s", __func__, filteredKeyValuePairs.string());
1239
Glenn Kasten142f5192014-03-25 17:44:59 -07001240 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1241 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001242 Mutex::Autolock _l(mLock);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001243 // result will remain NO_INIT if no audio device is present
1244 status_t final_result = NO_INIT;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001245 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001246 AutoMutex lock(mHardwareLock);
1247 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1248 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001249 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
Eric Laurentf1047e82018-04-16 19:18:20 -07001250 status_t result = dev->setParameters(filteredKeyValuePairs);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001251 // return success if at least one audio device accepts the parameters as not all
1252 // HALs are requested to support all parameters. If no audio device supports the
1253 // requested parameters, the last error is reported.
1254 if (final_result != NO_ERROR) {
1255 final_result = result;
1256 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001257 }
1258 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001259 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001260 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
Eric Laurentf1047e82018-04-16 19:18:20 -07001261 AudioParameter param = AudioParameter(filteredKeyValuePairs);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001262 String8 value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001263 if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) {
1264 bool btNrecIsOff = (value == AudioParameter::valueOff);
Eric Laurentd8365c52017-07-16 15:27:05 -07001265 if (mBtNrecIsOff.exchange(btNrecIsOff) != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001266 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentd8365c52017-07-16 15:27:05 -07001267 mRecordThreads.valueAt(i)->checkBtNrec();
Eric Laurent59bd0da2011-08-01 09:52:20 -07001268 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001269 }
1270 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001271 String8 screenState;
1272 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
Mikhail Naganov00260b52016-10-13 12:54:24 -07001273 bool isOff = (screenState == AudioParameter::valueOff);
Eric Laurent81784c32012-11-19 14:55:58 -08001274 if (isOff != (AudioFlinger::mScreenState & 1)) {
1275 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001276 }
1277 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001278 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001279 }
1280
1281 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1282 // and the thread is exited once the lock is released
1283 sp<ThreadBase> thread;
1284 {
1285 Mutex::Autolock _l(mLock);
1286 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001287 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 thread = checkRecordThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001289 if (thread == 0) {
1290 thread = checkMmapThread_l(ioHandle);
1291 }
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001292 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001293 // indicate output device change to all input threads for pre processing
Eric Laurentf1047e82018-04-16 19:18:20 -07001294 AudioParameter param = AudioParameter(filteredKeyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001295 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001296 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1297 (value != 0)) {
Eric Laurentf1047e82018-04-16 19:18:20 -07001298 broacastParametersToRecordThreads_l(filteredKeyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001299 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300 }
1301 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001302 if (thread != 0) {
Eric Laurentf1047e82018-04-16 19:18:20 -07001303 return thread->setParameters(filteredKeyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001304 }
1305 return BAD_VALUE;
1306}
1307
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001308String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001309{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001310 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1311 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312
Eric Laurenta4c5a552012-03-29 10:12:40 -07001313 Mutex::Autolock _l(mLock);
1314
Glenn Kasten142f5192014-03-25 17:44:59 -07001315 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001316 String8 out_s8;
1317
Dima Zavin799a70e2011-04-18 16:57:27 -07001318 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001319 String8 s;
1320 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001321 {
1322 AutoMutex lock(mHardwareLock);
1323 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001324 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1325 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001326 mHardwareStatus = AUDIO_HW_IDLE;
1327 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001328 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001329 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001330 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331 }
1332
Eric Laurent6acd1d42017-01-04 14:23:29 -08001333 ThreadBase *thread = (ThreadBase *)checkPlaybackThread_l(ioHandle);
1334 if (thread == NULL) {
1335 thread = (ThreadBase *)checkRecordThread_l(ioHandle);
1336 if (thread == NULL) {
1337 thread = (ThreadBase *)checkMmapThread_l(ioHandle);
1338 if (thread == NULL) {
Mikhail Naganovaa165e52017-07-12 10:39:16 -07001339 return String8("");
Eric Laurent6acd1d42017-01-04 14:23:29 -08001340 }
1341 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001342 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001343 return thread->getParameters(keys);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001344}
1345
Glenn Kastendd8104c2012-07-02 12:42:44 -07001346size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1347 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348{
Eric Laurenta1884f92011-08-23 08:25:03 -07001349 status_t ret = initCheck();
1350 if (ret != NO_ERROR) {
1351 return 0;
1352 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001353 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001354 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001355 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001356 return 0;
1357 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001358
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001359 AutoMutex lock(mHardwareLock);
1360 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001361 audio_config_t config, proposed;
1362 memset(&proposed, 0, sizeof(proposed));
1363 proposed.sample_rate = sampleRate;
1364 proposed.channel_mask = channelMask;
1365 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001366
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001367 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001368 size_t frames;
1369 for (;;) {
1370 // Note: config is currently a const parameter for get_input_buffer_size()
1371 // but we use a copy from proposed in case config changes from the call.
1372 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001373 status_t result = dev->getInputBufferSize(&config, &frames);
1374 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001375 break; // hal success, config is the result
1376 }
1377 // change one parameter of the configuration each iteration to a more "common" value
1378 // to see if the device will support it.
1379 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1380 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1381 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1382 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1383 } else {
1384 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1385 "format %#x, channelMask 0x%X",
1386 sampleRate, format, channelMask);
1387 break; // retries failed, break out of loop with frames == 0.
1388 }
1389 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001390 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001391 if (frames > 0 && config.sample_rate != sampleRate) {
1392 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1393 }
1394 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001395}
1396
Glenn Kasten5f972c02014-01-13 09:59:31 -08001397uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001398{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399 Mutex::Autolock _l(mLock);
1400
1401 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1402 if (recordThread != NULL) {
1403 return recordThread->getInputFramesLost();
1404 }
1405 return 0;
1406}
1407
1408status_t AudioFlinger::setVoiceVolume(float value)
1409{
Eric Laurenta1884f92011-08-23 08:25:03 -07001410 status_t ret = initCheck();
1411 if (ret != NO_ERROR) {
1412 return ret;
1413 }
1414
Mathias Agopian65ab4712010-07-14 17:59:35 -07001415 // check calling permissions
1416 if (!settingsAllowed()) {
1417 return PERMISSION_DENIED;
1418 }
1419
1420 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001421 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001422 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001423 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424 mHardwareStatus = AUDIO_HW_IDLE;
1425
1426 return ret;
1427}
1428
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001429status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001430 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001431{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432 Mutex::Autolock _l(mLock);
1433
1434 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1435 if (playbackThread != NULL) {
1436 return playbackThread->getRenderPosition(halFrames, dspFrames);
1437 }
1438
1439 return BAD_VALUE;
1440}
1441
1442void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1443{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001445 if (client == 0) {
1446 return;
1447 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001448 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001449 {
1450 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001451 if (mNotificationClients.indexOfKey(pid) < 0) {
1452 sp<NotificationClient> notificationClient = new NotificationClient(this,
1453 client,
1454 pid);
1455 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456
Eric Laurent021cf962014-05-13 10:18:14 -07001457 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001458
Marco Nelissen06b46062014-11-14 07:58:25 -08001459 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001460 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001461 }
1462 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001463
Eric Laurent021cf962014-05-13 10:18:14 -07001464 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001465 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001466 // the config change is always sent from playback or record threads to avoid deadlock
1467 // with AudioSystem::gLock
1468 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001469 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_REGISTERED, pid);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001470 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001472 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001473 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_REGISTERED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474 }
1475}
1476
1477void AudioFlinger::removeNotificationClient(pid_t pid)
1478{
1479 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001480 {
1481 Mutex::Autolock _cl(mClientLock);
1482 mNotificationClients.removeItem(pid);
1483 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001484
Steve Block3856b092011-10-20 11:56:00 +01001485 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001486 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001487 bool removed = false;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001488 for (size_t i = 0; i < num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001489 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001490 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001491 if (ref->mPid == pid) {
1492 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001493 mAudioSessionRefs.removeAt(i);
1494 delete ref;
1495 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001496 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001497 } else {
1498 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001499 }
1500 }
1501 if (removed) {
1502 purgeStaleEffects_l();
1503 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001504}
1505
Eric Laurent73e26b62015-04-27 16:55:58 -07001506void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001507 const sp<AudioIoDescriptor>& ioDesc,
1508 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001509{
Eric Laurent021cf962014-05-13 10:18:14 -07001510 Mutex::Autolock _l(mClientLock);
1511 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001513 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1514 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1515 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516 }
1517}
1518
Eric Laurent021cf962014-05-13 10:18:14 -07001519// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520void AudioFlinger::removeClient_l(pid_t pid)
1521{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001522 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001523 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001524 mClients.removeItem(pid);
1525}
1526
Eric Laurent717e1282012-06-29 16:36:52 -07001527// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001528sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1529 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001530{
1531 sp<PlaybackThread> thread;
1532
1533 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1534 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1535 ALOG_ASSERT(thread == 0);
1536 thread = mPlaybackThreads.valueAt(i);
1537 }
1538 }
1539
1540 return thread;
1541}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543
Mathias Agopian65ab4712010-07-14 17:59:35 -07001544
1545// ----------------------------------------------------------------------------
1546
1547AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1548 : RefBase(),
1549 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001550 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551{
Andy Hung6f248bb2018-01-23 14:04:37 -08001552 mMemoryDealer = new MemoryDealer(
1553 audioFlinger->getClientSharedHeapSize(),
1554 (std::string("AudioFlinger::Client(") + std::to_string(pid) + ")").c_str());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001555}
1556
Eric Laurent021cf962014-05-13 10:18:14 -07001557// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001558AudioFlinger::Client::~Client()
1559{
1560 mAudioFlinger->removeClient_l(mPid);
1561}
1562
Glenn Kasten435dbe62012-01-30 10:15:48 -08001563sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001564{
1565 return mMemoryDealer;
1566}
1567
1568// ----------------------------------------------------------------------------
1569
1570AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1571 const sp<IAudioFlingerClient>& client,
1572 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001573 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001574{
1575}
1576
1577AudioFlinger::NotificationClient::~NotificationClient()
1578{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001579}
1580
Glenn Kasten0f11b512014-01-31 16:18:54 -08001581void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001582{
1583 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001584 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001585}
1586
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001587// ----------------------------------------------------------------------------
1588AudioFlinger::MediaLogNotifier::MediaLogNotifier()
1589 : mPendingRequests(false) {}
1590
1591
1592void AudioFlinger::MediaLogNotifier::requestMerge() {
1593 AutoMutex _l(mMutex);
1594 mPendingRequests = true;
1595 mCond.signal();
1596}
1597
1598bool AudioFlinger::MediaLogNotifier::threadLoop() {
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001599 // Should already have been checked, but just in case
1600 if (sMediaLogService == 0) {
1601 return false;
1602 }
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001603 // Wait until there are pending requests
1604 {
1605 AutoMutex _l(mMutex);
1606 mPendingRequests = false; // to ignore past requests
1607 while (!mPendingRequests) {
1608 mCond.wait(mMutex);
1609 // TODO may also need an exitPending check
1610 }
1611 mPendingRequests = false;
1612 }
1613 // Execute the actual MediaLogService binder call and ignore extra requests for a while
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001614 sMediaLogService->requestMergeWakeup();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001615 usleep(kPostTriggerSleepPeriod);
1616 return true;
1617}
1618
1619void AudioFlinger::requestLogMerge() {
1620 mMediaLogNotifier->requestMerge();
1621}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001622
1623// ----------------------------------------------------------------------------
1624
Eric Laurentf14db3c2017-12-08 14:20:36 -08001625sp<media::IAudioRecord> AudioFlinger::createRecord(const CreateRecordInput& input,
1626 CreateRecordOutput& output,
1627 status_t *status)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001628{
1629 sp<RecordThread::RecordTrack> recordTrack;
1630 sp<RecordHandle> recordHandle;
1631 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001632 status_t lStatus;
Eric Laurentf14db3c2017-12-08 14:20:36 -08001633 audio_session_t sessionId = input.sessionId;
Eric Laurent77881cd2018-02-09 16:01:31 -08001634 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001635
Eric Laurentf14db3c2017-12-08 14:20:36 -08001636 output.cblk.clear();
1637 output.buffers.clear();
Eric Laurent896c9672017-12-08 14:08:45 -08001638 output.inputId = AUDIO_IO_HANDLE_NONE;
Glenn Kastend776ac62014-05-07 09:16:09 -07001639
Eric Laurentf14db3c2017-12-08 14:20:36 -08001640 bool updatePid = (input.clientInfo.clientPid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001641 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurentf14db3c2017-12-08 14:20:36 -08001642 uid_t clientUid = input.clientInfo.clientUid;
Andy Hung4ef19fa2018-05-15 19:35:29 -07001643 if (!isAudioServerOrMediaServerUid(callingUid)) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001644 ALOGW_IF(clientUid != callingUid,
1645 "%s uid %d tried to pass itself off as %d",
1646 __FUNCTION__, callingUid, clientUid);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001647 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001648 updatePid = true;
1649 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001650 pid_t clientPid = input.clientInfo.clientPid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001651 if (updatePid) {
1652 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurentf14db3c2017-12-08 14:20:36 -08001653 ALOGW_IF(clientPid != -1 && clientPid != callingPid,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001654 "%s uid %d pid %d tried to pass itself off as pid %d",
Eric Laurentf14db3c2017-12-08 14:20:36 -08001655 __func__, callingUid, callingPid, clientPid);
1656 clientPid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001657 }
1658
Andy Hung6770c6f2015-04-07 13:43:36 -07001659 // we don't yet support anything other than linear PCM
Eric Laurentf14db3c2017-12-08 14:20:36 -08001660 if (!audio_is_valid_format(input.config.format) || !audio_is_linear_pcm(input.config.format)) {
1661 ALOGE("createRecord() invalid format %#x", input.config.format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001662 lStatus = BAD_VALUE;
1663 goto Exit;
1664 }
1665
Glenn Kasten53b5d092014-02-05 10:00:23 -08001666 // further channel mask checks are performed by createRecordTrack_l()
Eric Laurentf14db3c2017-12-08 14:20:36 -08001667 if (!audio_is_input_channel(input.config.channel_mask)) {
1668 ALOGE("createRecord() invalid channel mask %#x", input.config.channel_mask);
Glenn Kasten53b5d092014-02-05 10:00:23 -08001669 lStatus = BAD_VALUE;
1670 goto Exit;
1671 }
1672
Eric Laurentf14db3c2017-12-08 14:20:36 -08001673 if (sessionId == AUDIO_SESSION_ALLOCATE) {
1674 sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
1675 } else if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1676 lStatus = BAD_VALUE;
1677 goto Exit;
1678 }
1679
1680 output.sessionId = sessionId;
Eric Laurentf14db3c2017-12-08 14:20:36 -08001681 output.selectedDeviceId = input.selectedDeviceId;
1682 output.flags = input.flags;
1683
1684 client = registerPid(clientPid);
1685
1686 // Not a conventional loop, but a retry loop for at most two iterations total.
1687 // Try first maybe with FAST flag then try again without FAST flag if that fails.
1688 // Exits loop via break on no error of got exit on error
1689 // The sp<> references will be dropped when re-entering scope.
1690 // The lack of indentation is deliberate, to reduce code churn and ease merges.
1691 for (;;) {
Eric Laurent896c9672017-12-08 14:08:45 -08001692 // release previously opened input if retrying.
1693 if (output.inputId != AUDIO_IO_HANDLE_NONE) {
1694 recordTrack.clear();
Eric Laurentfee19762018-01-29 18:44:13 -08001695 AudioSystem::releaseInput(portId);
Eric Laurent896c9672017-12-08 14:08:45 -08001696 output.inputId = AUDIO_IO_HANDLE_NONE;
Yung Ti Su5fac9c62018-05-15 15:07:43 +08001697 output.selectedDeviceId = input.selectedDeviceId;
Eric Laurent77881cd2018-02-09 16:01:31 -08001698 portId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent896c9672017-12-08 14:08:45 -08001699 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001700 lStatus = AudioSystem::getInputForAttr(&input.attr, &output.inputId,
1701 sessionId,
1702 // FIXME compare to AudioTrack
1703 clientPid,
1704 clientUid,
Eric Laurentfee19762018-01-29 18:44:13 -08001705 input.opPackageName,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001706 &input.config,
1707 output.flags, &output.selectedDeviceId, &portId);
1708
Glenn Kasten05997e22014-03-13 15:08:33 -07001709 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001710 Mutex::Autolock _l(mLock);
Eric Laurentf14db3c2017-12-08 14:20:36 -08001711 RecordThread *thread = checkRecordThread_l(output.inputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001712 if (thread == NULL) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001713 ALOGE("createRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001714 lStatus = BAD_VALUE;
1715 goto Exit;
1716 }
1717
Eric Laurentf14db3c2017-12-08 14:20:36 -08001718 ALOGV("createRecord() lSessionId: %d input %d", sessionId, output.inputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001719
Eric Laurentf14db3c2017-12-08 14:20:36 -08001720 output.sampleRate = input.config.sample_rate;
1721 output.frameCount = input.frameCount;
1722 output.notificationFrameCount = input.notificationFrameCount;
Glenn Kasten570f6332014-03-13 15:01:06 -07001723
Kevin Rocard1f564ac2018-03-29 13:53:10 -07001724 recordTrack = thread->createRecordTrack_l(client, input.attr, &output.sampleRate,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001725 input.config.format, input.config.channel_mask,
1726 &output.frameCount, sessionId,
1727 &output.notificationFrameCount,
1728 clientUid, &output.flags,
1729 input.clientInfo.clientTid,
1730 &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001731 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001732
Eric Laurentf14db3c2017-12-08 14:20:36 -08001733 // lStatus == BAD_TYPE means FAST flag was rejected: request a new input from
1734 // audio policy manager without FAST constraint
1735 if (lStatus == BAD_TYPE) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001736 continue;
Eric Laurent1b928682014-10-02 19:41:47 -07001737 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001738
1739 if (lStatus != NO_ERROR) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001740 goto Exit;
1741 }
1742
1743 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1744 // session and move it to this thread.
1745 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
1746 if (chain != 0) {
1747 Mutex::Autolock _l(thread->mLock);
1748 thread->addEffectChain_l(chain);
1749 }
1750 break;
1751 }
1752 // End of retry loop.
1753 // The lack of indentation is deliberate, to reduce code churn and ease merges.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001754 }
Glenn Kastene198c362013-08-13 09:13:36 -07001755
Eric Laurentf14db3c2017-12-08 14:20:36 -08001756 output.cblk = recordTrack->getCblk();
1757 output.buffers = recordTrack->getBuffers();
1758
1759 // return handle to client
1760 recordHandle = new RecordHandle(recordTrack);
1761
1762Exit:
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001763 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001764 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001765 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001766 // Don't hold mClientLock when releasing the reference on the track as the
1767 // destructor will acquire it.
1768 {
1769 Mutex::Autolock _cl(mClientLock);
1770 client.clear();
1771 }
Eric Laurent896c9672017-12-08 14:08:45 -08001772 recordTrack.clear();
1773 if (output.inputId != AUDIO_IO_HANDLE_NONE) {
Eric Laurentfee19762018-01-29 18:44:13 -08001774 AudioSystem::releaseInput(portId);
Eric Laurent896c9672017-12-08 14:08:45 -08001775 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001776 }
1777
Glenn Kasten9156ef32013-08-06 15:39:08 -07001778 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001779 return recordHandle;
1780}
1781
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001782
1783
Mathias Agopian65ab4712010-07-14 17:59:35 -07001784// ----------------------------------------------------------------------------
1785
Eric Laurenta4c5a552012-03-29 10:12:40 -07001786audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1787{
Eric Laurent44622db2014-08-01 19:00:33 -07001788 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001789 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001790 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001791 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001792 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001793 }
1794 Mutex::Autolock _l(mLock);
1795 return loadHwModule_l(name);
1796}
1797
1798// loadHwModule_l() must be called with AudioFlinger::mLock held
1799audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1800{
1801 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1802 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1803 ALOGW("loadHwModule() module %s already loaded", name);
1804 return mAudioHwDevs.keyAt(i);
1805 }
1806 }
1807
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001808 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001809
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001810 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001811 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001812 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001813 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001814 }
1815
1816 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001817 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07001818 mHardwareStatus = AUDIO_HW_IDLE;
1819 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001820 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001821 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001822 }
1823
John Grossmanee578c02012-07-23 17:05:46 -07001824 // Check and cache this HAL's level of support for master mute and master
1825 // volume. If this is the first HAL opened, and it supports the get
1826 // methods, use the initial values provided by the HAL as the current
1827 // master mute and volume settings.
1828
1829 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1830 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001831 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001832
1833 if (0 == mAudioHwDevs.size()) {
1834 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001835 float mv;
1836 if (OK == dev->getMasterVolume(&mv)) {
1837 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07001838 }
1839
1840 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001841 bool mm;
1842 if (OK == dev->getMasterMute(&mm)) {
1843 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07001844 }
1845 }
1846
Eric Laurenta4c5a552012-03-29 10:12:40 -07001847 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001848 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07001849 flags = static_cast<AudioHwDevice::Flags>(flags |
1850 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1851 }
1852
1853 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001854 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07001855 flags = static_cast<AudioHwDevice::Flags>(flags |
1856 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1857 }
1858
Eric Laurenta4c5a552012-03-29 10:12:40 -07001859 mHardwareStatus = AUDIO_HW_IDLE;
1860 }
1861
Glenn Kastena13cde92016-03-28 15:26:02 -07001862 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001863 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001864
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001865 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001866
1867 return handle;
1868
1869}
1870
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001871// ----------------------------------------------------------------------------
1872
Glenn Kasten3b16c762012-11-14 08:44:39 -08001873uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001874{
1875 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001876 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001877 return thread != NULL ? thread->sampleRate() : 0;
1878}
1879
Glenn Kastene33054e2012-11-14 12:54:39 -08001880size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001881{
1882 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001883 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001884 return thread != NULL ? thread->frameCountHAL() : 0;
1885}
1886
1887// ----------------------------------------------------------------------------
1888
Andy Hung6f248bb2018-01-23 14:04:37 -08001889status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001890{
1891 uid_t uid = IPCThreadState::self()->getCallingUid();
Andy Hung4ef19fa2018-05-15 19:35:29 -07001892 if (!isAudioServerOrSystemServerUid(uid)) {
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001893 return PERMISSION_DENIED;
1894 }
1895 Mutex::Autolock _l(mLock);
1896 if (mIsDeviceTypeKnown) {
1897 return INVALID_OPERATION;
1898 }
1899 mIsLowRamDevice = isLowRamDevice;
Andy Hung6f248bb2018-01-23 14:04:37 -08001900 mTotalMemory = totalMemory;
1901 // mIsLowRamDevice and mTotalMemory are obtained through ActivityManager;
1902 // see ActivityManager.isLowRamDevice() and ActivityManager.getMemoryInfo().
1903 // mIsLowRamDevice generally represent devices with less than 1GB of memory,
1904 // though actual setting is determined through device configuration.
1905 constexpr int64_t GB = 1024 * 1024 * 1024;
1906 mClientSharedHeapSize =
1907 isLowRamDevice ? kMinimumClientSharedHeapSizeBytes
1908 : mTotalMemory < 2 * GB ? 4 * kMinimumClientSharedHeapSizeBytes
1909 : mTotalMemory < 3 * GB ? 8 * kMinimumClientSharedHeapSizeBytes
1910 : mTotalMemory < 4 * GB ? 16 * kMinimumClientSharedHeapSizeBytes
1911 : 32 * kMinimumClientSharedHeapSizeBytes;
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001912 mIsDeviceTypeKnown = true;
Andy Hung6f248bb2018-01-23 14:04:37 -08001913
1914 // TODO: Cache the client shared heap size in a persistent property.
1915 // It's possible that a native process or Java service or app accesses audioserver
1916 // after it is registered by system server, but before AudioService updates
1917 // the memory info. This would occur immediately after boot or an audioserver
1918 // crash and restore. Before update from AudioService, the client would get the
1919 // minimum heap size.
1920
1921 ALOGD("isLowRamDevice:%s totalMemory:%lld mClientSharedHeapSize:%zu",
1922 (isLowRamDevice ? "true" : "false"),
1923 (long long)mTotalMemory,
1924 mClientSharedHeapSize.load());
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001925 return NO_ERROR;
1926}
1927
Andy Hung6f248bb2018-01-23 14:04:37 -08001928size_t AudioFlinger::getClientSharedHeapSize() const
1929{
1930 size_t heapSizeInBytes = property_get_int32("ro.af.client_heap_size_kbyte", 0) * 1024;
1931 if (heapSizeInBytes != 0) { // read-only property overrides all.
1932 return heapSizeInBytes;
1933 }
1934 return mClientSharedHeapSize;
1935}
1936
Mikhail Naganovdea53042018-04-26 13:10:21 -07001937status_t AudioFlinger::setAudioPortConfig(const struct audio_port_config *config)
1938{
1939 ALOGV(__func__);
1940
1941 audio_module_handle_t module;
1942 if (config->type == AUDIO_PORT_TYPE_DEVICE) {
1943 module = config->ext.device.hw_module;
1944 } else {
1945 module = config->ext.mix.hw_module;
1946 }
1947
1948 Mutex::Autolock _l(mLock);
1949 ssize_t index = mAudioHwDevs.indexOfKey(module);
1950 if (index < 0) {
1951 ALOGW("%s() bad hw module %d", __func__, module);
1952 return BAD_VALUE;
1953 }
1954
1955 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(index);
1956 return audioHwDevice->hwDevice()->setAudioPortConfig(config);
1957}
1958
Eric Laurent93c3d412014-08-01 14:48:35 -07001959audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1960{
1961 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001962
1963 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1964 if (index >= 0) {
1965 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1966 mHwAvSyncIds.valueAt(index), sessionId);
1967 return mHwAvSyncIds.valueAt(index);
1968 }
1969
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001970 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07001971 if (dev == NULL) {
1972 return AUDIO_HW_SYNC_INVALID;
1973 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001974 String8 reply;
1975 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001976 if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001977 param = AudioParameter(reply);
1978 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001979
1980 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001981 if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001982 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1983 return AUDIO_HW_SYNC_INVALID;
1984 }
1985
1986 // allow only one session for a given HW A/V sync ID.
1987 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1988 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1989 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1990 value, mHwAvSyncIds.keyAt(i));
1991 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001992 break;
1993 }
1994 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001995
1996 mHwAvSyncIds.add(sessionId, value);
1997
1998 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1999 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
2000 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07002001 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07002002 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07002003 param.addInt(String8(AudioParameter::keyStreamHwAvSync), value);
Eric Laurentfa90e842014-10-17 18:12:31 -07002004 thread->setParameters(param.toString());
2005 break;
2006 }
2007 }
2008
2009 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
2010 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07002011}
2012
Eric Laurent72e3f392015-05-20 14:43:50 -07002013status_t AudioFlinger::systemReady()
2014{
2015 Mutex::Autolock _l(mLock);
2016 ALOGI("%s", __FUNCTION__);
2017 if (mSystemReady) {
2018 ALOGW("%s called twice", __FUNCTION__);
2019 return NO_ERROR;
2020 }
2021 mSystemReady = true;
2022 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2023 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
2024 thread->systemReady();
2025 }
2026 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2027 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
2028 thread->systemReady();
2029 }
2030 return NO_ERROR;
2031}
2032
jiabin46a76fa2018-01-05 10:18:21 -08002033status_t AudioFlinger::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
2034{
jiabin9ff780e2018-03-19 18:19:52 -07002035 AutoMutex lock(mHardwareLock);
2036 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
2037 status_t status = dev->getMicrophones(microphones);
2038 return status;
jiabin46a76fa2018-01-05 10:18:21 -08002039}
2040
Eric Laurentfa90e842014-10-17 18:12:31 -07002041// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
2042void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
2043{
2044 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
2045 if (index >= 0) {
2046 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
2047 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
2048 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07002049 param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId);
Eric Laurentfa90e842014-10-17 18:12:31 -07002050 thread->setParameters(param.toString());
2051 }
2052}
2053
2054
Glenn Kasten4182c4e2013-07-15 14:45:07 -07002055// ----------------------------------------------------------------------------
2056
Eric Laurent83b88082014-06-20 18:31:16 -07002057
Eric Laurent6acd1d42017-01-04 14:23:29 -08002058sp<AudioFlinger::ThreadBase> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002059 audio_io_handle_t *output,
2060 audio_config_t *config,
2061 audio_devices_t devices,
2062 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07002063 audio_output_flags_t flags)
2064{
Eric Laurentcf2c0212014-07-25 16:20:43 -07002065 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07002066 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002067 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07002068 }
2069
Eric Laurentcf2c0212014-07-25 16:20:43 -07002070 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002071 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
2072 } else {
2073 // Audio Policy does not currently request a specific output handle.
2074 // If this is ever needed, see openInput_l() for example code.
2075 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
2076 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002077 }
Eric Laurent83b88082014-06-20 18:31:16 -07002078
2079 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
2080
Eric Laurent83b88082014-06-20 18:31:16 -07002081 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07002082 // This if statement allows overriding the audio policy settings
2083 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
2084 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
2085 // Check only for Normal Mixing mode
2086 if (kEnableExtendedPrecision) {
2087 // Specify format (uncomment one below to choose)
2088 //config->format = AUDIO_FORMAT_PCM_FLOAT;
2089 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
2090 //config->format = AUDIO_FORMAT_PCM_32_BIT;
2091 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002092 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07002093 }
2094 if (kEnableExtendedChannels) {
2095 // Specify channel mask (uncomment one below to choose)
2096 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
2097 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
2098 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
2099 }
Eric Laurent83b88082014-06-20 18:31:16 -07002100 }
2101
Phil Burk062e67a2015-02-11 13:40:50 -08002102 AudioStreamOut *outputStream = NULL;
2103 status_t status = outHwDev->openOutputStream(
2104 &outputStream,
2105 *output,
2106 devices,
2107 flags,
2108 config,
2109 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07002110
2111 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07002112
Phil Burk062e67a2015-02-11 13:40:50 -08002113 if (status == NO_ERROR) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002114 if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
2115 sp<MmapPlaybackThread> thread =
2116 new MmapPlaybackThread(this, *output, outHwDev, outputStream,
2117 devices, AUDIO_DEVICE_NONE, mSystemReady);
2118 mMmapThreads.add(*output, thread);
2119 ALOGV("openOutput_l() created mmap playback thread: ID %d thread %p",
2120 *output, thread.get());
2121 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07002122 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002123 sp<PlaybackThread> thread;
2124 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2125 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
2126 ALOGV("openOutput_l() created offload output: ID %d thread %p",
2127 *output, thread.get());
2128 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
2129 || !isValidPcmSinkFormat(config->format)
2130 || !isValidPcmSinkChannelMask(config->channel_mask)) {
2131 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
2132 ALOGV("openOutput_l() created direct output: ID %d thread %p",
2133 *output, thread.get());
2134 } else {
2135 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
2136 ALOGV("openOutput_l() created mixer output: ID %d thread %p",
2137 *output, thread.get());
2138 }
2139 mPlaybackThreads.add(*output, thread);
2140 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07002141 }
Eric Laurent83b88082014-06-20 18:31:16 -07002142 }
2143
2144 return 0;
2145}
2146
Eric Laurentcf2c0212014-07-25 16:20:43 -07002147status_t AudioFlinger::openOutput(audio_module_handle_t module,
2148 audio_io_handle_t *output,
2149 audio_config_t *config,
2150 audio_devices_t *devices,
2151 const String8& address,
2152 uint32_t *latencyMs,
2153 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002154{
Glenn Kasten49f36ba2017-12-06 13:02:02 -08002155 ALOGI("openOutput() this %p, module %d Device %#x, SamplingRate %d, Format %#08x, "
2156 "Channels %#x, flags %#x",
Eric Laurent6acd1d42017-01-04 14:23:29 -08002157 this, module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002158 (devices != NULL) ? *devices : 0,
2159 config->sample_rate,
2160 config->format,
2161 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002162 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002163
Yunlian Jiang32ba9862017-01-31 15:55:00 -08002164 if (devices == NULL || *devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002165 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002166 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002167
Mathias Agopian65ab4712010-07-14 17:59:35 -07002168 Mutex::Autolock _l(mLock);
2169
Eric Laurent6acd1d42017-01-04 14:23:29 -08002170 sp<ThreadBase> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002171 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002172 if ((flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) {
2173 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
2174 *latencyMs = playbackThread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002175
Eric Laurent6acd1d42017-01-04 14:23:29 -08002176 // notify client processes of the new output creation
2177 playbackThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002178
Eric Laurent6acd1d42017-01-04 14:23:29 -08002179 // the first primary output opened designates the primary hw device
2180 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -08002181 ALOGI("Using module %d as the primary audio interface", module);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002182 mPrimaryHardwareDev = playbackThread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002183
Eric Laurent6acd1d42017-01-04 14:23:29 -08002184 AutoMutex lock(mHardwareLock);
2185 mHardwareStatus = AUDIO_HW_SET_MODE;
2186 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
2187 mHardwareStatus = AUDIO_HW_IDLE;
2188 }
2189 } else {
2190 MmapThread *mmapThread = (MmapThread *)thread.get();
2191 mmapThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002192 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002193 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002194 }
2195
Eric Laurentcf2c0212014-07-25 16:20:43 -07002196 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002197}
2198
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002199audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
2200 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002201{
2202 Mutex::Autolock _l(mLock);
2203 MixerThread *thread1 = checkMixerThread_l(output1);
2204 MixerThread *thread2 = checkMixerThread_l(output2);
2205
2206 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002207 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
2208 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07002209 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002210 }
2211
Glenn Kasteneeecb982016-02-26 10:44:04 -08002212 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07002213 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002214 thread->addOutputTrack(thread2);
2215 mPlaybackThreads.add(id, thread);
2216 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002217 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218 return id;
2219}
2220
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002221status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002222{
Glenn Kastend96c5722012-04-25 13:44:49 -07002223 return closeOutput_nonvirtual(output);
2224}
2225
2226status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
2227{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002228 // keep strong reference on the playback thread so that
2229 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002230 sp<PlaybackThread> playbackThread;
2231 sp<MmapPlaybackThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002232 {
2233 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002234 playbackThread = checkPlaybackThread_l(output);
2235 if (playbackThread != NULL) {
2236 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237
Eric Laurent6acd1d42017-01-04 14:23:29 -08002238 if (playbackThread->type() == ThreadBase::MIXER) {
2239 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2240 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
2241 DuplicatingThread *dupThread =
2242 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
2243 dupThread->removeOutputTrack((MixerThread *)playbackThread.get());
2244 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002245 }
2246 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002247
2248
Eric Laurent6acd1d42017-01-04 14:23:29 -08002249 mPlaybackThreads.removeItem(output);
2250 // save all effects to the default thread
2251 if (mPlaybackThreads.size()) {
2252 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
2253 if (dstThread != NULL) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002254 // audioflinger lock is held so order of thread lock acquisition doesn't matter
Eric Laurent6acd1d42017-01-04 14:23:29 -08002255 Mutex::Autolock _dl(dstThread->mLock);
2256 Mutex::Autolock _sl(playbackThread->mLock);
2257 Vector< sp<EffectChain> > effectChains = playbackThread->getEffectChains_l();
2258 for (size_t i = 0; i < effectChains.size(); i ++) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002259 moveEffectChain_l(effectChains[i]->sessionId(), playbackThread.get(),
2260 dstThread, true);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002261 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002262 }
2263 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002264 } else {
2265 mmapThread = (MmapPlaybackThread *)checkMmapThread_l(output);
2266 if (mmapThread == 0) {
2267 return BAD_VALUE;
2268 }
2269 mMmapThreads.removeItem(output);
Phil Burk7f6b40d2017-02-09 13:18:38 -08002270 ALOGD("closing mmapThread %p", mmapThread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002271 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002272 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2273 ioDesc->mIoHandle = output;
2274 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002275 }
Glenn Kastenb28686f2012-01-06 08:39:38 -08002276 // The thread entity (active unit of execution) is no longer running here,
2277 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002278
Eric Laurent6acd1d42017-01-04 14:23:29 -08002279 if (playbackThread != 0) {
2280 playbackThread->exit();
2281 if (!playbackThread->isDuplicating()) {
2282 closeOutputFinish(playbackThread);
2283 }
2284 } else if (mmapThread != 0) {
Phil Burk7f6b40d2017-02-09 13:18:38 -08002285 ALOGD("mmapThread exit()");
Eric Laurent6acd1d42017-01-04 14:23:29 -08002286 mmapThread->exit();
2287 AudioStreamOut *out = mmapThread->clearOutput();
2288 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2289 // from now on thread->mOutput is NULL
2290 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002291 }
2292 return NO_ERROR;
2293}
2294
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002295void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002296{
2297 AudioStreamOut *out = thread->clearOutput();
2298 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2299 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07002300 delete out;
2301}
2302
Mikhail Naganov444ecc32018-05-01 17:40:05 -07002303void AudioFlinger::closeThreadInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002304{
2305 mPlaybackThreads.removeItem(thread->mId);
2306 thread->exit();
2307 closeOutputFinish(thread);
2308}
2309
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002310status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002311{
2312 Mutex::Autolock _l(mLock);
2313 PlaybackThread *thread = checkPlaybackThread_l(output);
2314
2315 if (thread == NULL) {
2316 return BAD_VALUE;
2317 }
2318
Steve Block3856b092011-10-20 11:56:00 +01002319 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002320 thread->suspend();
2321
2322 return NO_ERROR;
2323}
2324
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002325status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002326{
2327 Mutex::Autolock _l(mLock);
2328 PlaybackThread *thread = checkPlaybackThread_l(output);
2329
2330 if (thread == NULL) {
2331 return BAD_VALUE;
2332 }
2333
Steve Block3856b092011-10-20 11:56:00 +01002334 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002335
2336 thread->restore();
2337
2338 return NO_ERROR;
2339}
2340
Eric Laurentcf2c0212014-07-25 16:20:43 -07002341status_t AudioFlinger::openInput(audio_module_handle_t module,
2342 audio_io_handle_t *input,
2343 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002344 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002345 const String8& address,
2346 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002347 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002348{
Eric Laurent83b88082014-06-20 18:31:16 -07002349 Mutex::Autolock _l(mLock);
2350
Glenn Kastene7d66712015-03-05 13:46:52 -08002351 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002352 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002353 }
2354
Eric Laurent6acd1d42017-01-04 14:23:29 -08002355 sp<ThreadBase> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002356
2357 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002358 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002359 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002360 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002361 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002362 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002363}
Dima Zavin799a70e2011-04-18 16:57:27 -07002364
Eric Laurent6acd1d42017-01-04 14:23:29 -08002365sp<AudioFlinger::ThreadBase> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002366 audio_io_handle_t *input,
2367 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002368 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002369 const String8& address,
2370 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002371 audio_input_flags_t flags)
2372{
Glenn Kastene7d66712015-03-05 13:46:52 -08002373 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002374 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002375 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002376 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002377 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002378
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07002379 // Some flags are specific to framework and must not leak to the HAL.
2380 flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FRAMEWORK_FLAGS);
2381
Glenn Kasteneeecb982016-02-26 10:44:04 -08002382 // Audio Policy can request a specific handle for hardware hotword.
2383 // The goal here is not to re-open an already opened input.
2384 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002385 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002386 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2387 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2388 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2389 return 0;
2390 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2391 // This should not happen in a transient state with current design.
2392 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2393 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002394 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002395
Eric Laurentcf2c0212014-07-25 16:20:43 -07002396 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002397 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002398 sp<StreamInHalInterface> inStream;
2399 status_t status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002400 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten49f36ba2017-12-06 13:02:02 -08002401 ALOGV("openInput_l() openInputStream returned input %p, devices %#x, SamplingRate %d"
2402 ", Format %#x, Channels %#x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002403 inStream.get(),
Mikhail Naganovf558e022016-11-14 17:45:17 -08002404 devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002405 halconfig.sample_rate,
2406 halconfig.format,
2407 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002408 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002409 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002410
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002411 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002412 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002413 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002414 audio_is_linear_pcm(config->format) &&
2415 audio_is_linear_pcm(halconfig.format) &&
2416 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002417 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2418 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002419 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002420 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002421 inStream.clear();
2422 status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002423 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002424 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002425 }
2426
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002427 if (status == NO_ERROR && inStream != 0) {
Eric Laurent05067782016-06-01 18:27:28 -07002428 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002429 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) {
2430 sp<MmapCaptureThread> thread =
2431 new MmapCaptureThread(this, *input,
2432 inHwDev, inputStream,
2433 primaryOutputDevice_l(), devices, mSystemReady);
2434 mMmapThreads.add(*input, thread);
Glenn Kastend3bb6452016-12-05 18:14:37 -08002435 ALOGV("openInput_l() created mmap capture thread: ID %d thread %p", *input,
2436 thread.get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002437 return thread;
2438 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002439 // Start record thread
2440 // RecordThread requires both input and output device indication to forward to audio
2441 // pre processing modules
2442 sp<RecordThread> thread = new RecordThread(this,
2443 inputStream,
2444 *input,
2445 primaryOutputDevice_l(),
2446 devices,
2447 mSystemReady
Eric Laurent6acd1d42017-01-04 14:23:29 -08002448 );
2449 mRecordThreads.add(*input, thread);
2450 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
2451 return thread;
2452 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002453 }
2454
Eric Laurentcf2c0212014-07-25 16:20:43 -07002455 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002456 return 0;
2457}
2458
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002459status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002460{
Glenn Kastend96c5722012-04-25 13:44:49 -07002461 return closeInput_nonvirtual(input);
2462}
2463
2464status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2465{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002466 // keep strong reference on the record thread so that
2467 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002468 sp<RecordThread> recordThread;
2469 sp<MmapCaptureThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002470 {
2471 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002472 recordThread = checkRecordThread_l(input);
2473 if (recordThread != 0) {
2474 ALOGV("closeInput() %d", input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002475
Eric Laurent6acd1d42017-01-04 14:23:29 -08002476 // If we still have effect chains, it means that a client still holds a handle
2477 // on at least one effect. We must either move the chain to an existing thread with the
2478 // same session ID or put it aside in case a new record thread is opened for a
2479 // new capture on the same session
2480 sp<EffectChain> chain;
2481 {
2482 Mutex::Autolock _sl(recordThread->mLock);
2483 Vector< sp<EffectChain> > effectChains = recordThread->getEffectChains_l();
2484 // Note: maximum one chain per record thread
2485 if (effectChains.size() != 0) {
2486 chain = effectChains[0];
Eric Laurent1b928682014-10-02 19:41:47 -07002487 }
2488 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002489 if (chain != 0) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002490 // first check if a record thread is already opened with a client on same session.
Eric Laurent6acd1d42017-01-04 14:23:29 -08002491 // This should only happen in case of overlap between one thread tear down and the
2492 // creation of its replacement
2493 size_t i;
2494 for (i = 0; i < mRecordThreads.size(); i++) {
2495 sp<RecordThread> t = mRecordThreads.valueAt(i);
2496 if (t == recordThread) {
2497 continue;
2498 }
2499 if (t->hasAudioSession(chain->sessionId()) != 0) {
2500 Mutex::Autolock _l(t->mLock);
2501 ALOGV("closeInput() found thread %d for effect session %d",
2502 t->id(), chain->sessionId());
2503 t->addEffectChain_l(chain);
2504 break;
2505 }
2506 }
Glenn Kastend3bb6452016-12-05 18:14:37 -08002507 // put the chain aside if we could not find a record thread with the same session id
Eric Laurent6acd1d42017-01-04 14:23:29 -08002508 if (i == mRecordThreads.size()) {
2509 putOrphanEffectChain_l(chain);
2510 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002511 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002512 mRecordThreads.removeItem(input);
2513 } else {
2514 mmapThread = (MmapCaptureThread *)checkMmapThread_l(input);
2515 if (mmapThread == 0) {
2516 return BAD_VALUE;
2517 }
2518 mMmapThreads.removeItem(input);
Eric Laurentaaa44472014-09-12 17:41:50 -07002519 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002520 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2521 ioDesc->mIoHandle = input;
2522 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002523 }
Eric Laurent83b88082014-06-20 18:31:16 -07002524 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2525 // we have a different lock for notification client
Eric Laurent6acd1d42017-01-04 14:23:29 -08002526 if (recordThread != 0) {
2527 closeInputFinish(recordThread);
2528 } else if (mmapThread != 0) {
2529 mmapThread->exit();
2530 AudioStreamIn *in = mmapThread->clearInput();
2531 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
2532 // from now on thread->mInput is NULL
2533 delete in;
2534 }
Eric Laurent83b88082014-06-20 18:31:16 -07002535 return NO_ERROR;
2536}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002537
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002538void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002539{
2540 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002541 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002542 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002543 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002544 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002545}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002546
Mikhail Naganov444ecc32018-05-01 17:40:05 -07002547void AudioFlinger::closeThreadInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002548{
2549 mRecordThreads.removeItem(thread->mId);
2550 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002551}
2552
Glenn Kastend2304db2014-02-03 07:40:31 -08002553status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002554{
2555 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002556 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002557
2558 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2559 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002560 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002561 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002562 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2563 mMmapThreads[i]->invalidateTracks(stream);
2564 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002565 return NO_ERROR;
2566}
2567
2568
Glenn Kasteneeecb982016-02-26 10:44:04 -08002569audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002570{
Glenn Kasten9d003132016-04-06 14:38:09 -07002571 // This is a binder API, so a malicious client could pass in a bad parameter.
2572 // Check for that before calling the internal API nextUniqueId().
2573 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2574 ALOGE("newAudioUniqueId invalid use %d", use);
2575 return AUDIO_UNIQUE_ID_ALLOCATE;
2576 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002577 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002578}
2579
Glenn Kastend848eb42016-03-08 13:42:11 -08002580void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002581{
2582 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002583 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002584 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
Andy Hung4ef19fa2018-05-15 19:35:29 -07002585 const uid_t callerUid = IPCThreadState::self()->getCallingUid();
2586 if (pid != -1 && isAudioServerUid(callerUid)) { // check must match releaseAudioSessionId()
Marco Nelissend457c972014-02-11 08:47:07 -08002587 caller = pid;
2588 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002589
Eric Laurent021cf962014-05-13 10:18:14 -07002590 {
2591 Mutex::Autolock _cl(mClientLock);
2592 // Ignore requests received from processes not known as notification client. The request
2593 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2594 // called from a different pid leaving a stale session reference. Also we don't know how
2595 // to clear this reference if the client process dies.
2596 if (mNotificationClients.indexOfKey(caller) < 0) {
2597 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2598 return;
2599 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002600 }
2601
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002602 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002603 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002604 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002605 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2606 ref->mCnt++;
2607 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002608 return;
2609 }
2610 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002611 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2612 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002613}
2614
Glenn Kastend848eb42016-03-08 13:42:11 -08002615void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002616{
2617 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002618 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002619 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
Andy Hung4ef19fa2018-05-15 19:35:29 -07002620 const uid_t callerUid = IPCThreadState::self()->getCallingUid();
2621 if (pid != -1 && isAudioServerUid(callerUid)) { // check must match acquireAudioSessionId()
Marco Nelissend457c972014-02-11 08:47:07 -08002622 caller = pid;
2623 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002624 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002625 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002626 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002627 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2628 ref->mCnt--;
2629 ALOGV(" decremented refcount to %d", ref->mCnt);
2630 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002631 mAudioSessionRefs.removeAt(i);
2632 delete ref;
2633 purgeStaleEffects_l();
2634 }
2635 return;
2636 }
2637 }
Andy Hung4ef19fa2018-05-15 19:35:29 -07002638 // If the caller is audioserver it is likely that the session being released was acquired
Eric Laurentd1b28d42013-09-18 18:47:13 -07002639 // on behalf of a process not in notification clients and we ignore the warning.
Andy Hung4ef19fa2018-05-15 19:35:29 -07002640 ALOGW_IF(!isAudioServerUid(callerUid),
2641 "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002642}
2643
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002644bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
2645{
2646 size_t num = mAudioSessionRefs.size();
2647 for (size_t i = 0; i < num; i++) {
2648 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2649 if (ref->mSessionid == audioSession) {
2650 return true;
2651 }
2652 }
2653 return false;
2654}
2655
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002656void AudioFlinger::purgeStaleEffects_l() {
2657
Steve Block3856b092011-10-20 11:56:00 +01002658 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002659
2660 Vector< sp<EffectChain> > chains;
2661
2662 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2663 sp<PlaybackThread> t = mPlaybackThreads.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];
Marco Nelissen0270b182011-08-12 14:14:39 -07002667 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2668 chains.push(ec);
2669 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002670 }
2671 }
2672 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2673 sp<RecordThread> t = mRecordThreads.valueAt(i);
Mateusz Kaplon2a6e1b02017-03-30 16:50:50 +02002674 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002675 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2676 sp<EffectChain> ec = t->mEffectChains[j];
2677 chains.push(ec);
2678 }
2679 }
2680
2681 for (size_t i = 0; i < chains.size(); i++) {
2682 sp<EffectChain> ec = chains[i];
2683 int sessionid = ec->sessionId();
2684 sp<ThreadBase> t = ec->mThread.promote();
2685 if (t == 0) {
2686 continue;
2687 }
2688 size_t numsessionrefs = mAudioSessionRefs.size();
2689 bool found = false;
2690 for (size_t k = 0; k < numsessionrefs; k++) {
2691 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002692 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002693 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002694 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002695 found = true;
2696 break;
2697 }
2698 }
2699 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002700 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002701 // remove all effects from the chain
2702 while (ec->mEffects.size()) {
2703 sp<EffectModule> effect = ec->mEffects[0];
2704 effect->unPin();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07002705 t->removeEffect_l(effect, /*release*/ true);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002706 if (effect->purgeHandles()) {
2707 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002708 }
2709 AudioSystem::unregisterEffect(effect->id());
2710 }
2711 }
2712 }
2713 return;
2714}
2715
Glenn Kasteneeecb982016-02-26 10:44:04 -08002716// checkThread_l() must be called with AudioFlinger::mLock held
2717AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2718{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002719 ThreadBase *thread = checkMmapThread_l(ioHandle);
2720 if (thread == 0) {
2721 switch (audio_unique_id_get_use(ioHandle)) {
2722 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2723 thread = checkPlaybackThread_l(ioHandle);
2724 break;
2725 case AUDIO_UNIQUE_ID_USE_INPUT:
2726 thread = checkRecordThread_l(ioHandle);
2727 break;
2728 default:
2729 break;
2730 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002731 }
2732 return thread;
2733}
2734
Mathias Agopian65ab4712010-07-14 17:59:35 -07002735// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002736AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002737{
Glenn Kastena1117922012-01-26 10:53:32 -08002738 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002739}
2740
2741// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002742AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002743{
2744 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002745 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002746}
2747
2748// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002749AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002750{
Glenn Kastena1117922012-01-26 10:53:32 -08002751 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002752}
2753
Eric Laurent6acd1d42017-01-04 14:23:29 -08002754// checkMmapThread_l() must be called with AudioFlinger::mLock held
2755AudioFlinger::MmapThread *AudioFlinger::checkMmapThread_l(audio_io_handle_t io) const
2756{
2757 return mMmapThreads.valueFor(io).get();
2758}
2759
2760
2761// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
2762AudioFlinger::VolumeInterface *AudioFlinger::getVolumeInterface_l(audio_io_handle_t output) const
2763{
Eric Laurent7459b902017-04-19 18:10:41 -07002764 VolumeInterface *volumeInterface = mPlaybackThreads.valueFor(output).get();
Eric Laurent6acd1d42017-01-04 14:23:29 -08002765 if (volumeInterface == nullptr) {
2766 MmapThread *mmapThread = mMmapThreads.valueFor(output).get();
2767 if (mmapThread != nullptr) {
2768 if (mmapThread->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07002769 MmapPlaybackThread *mmapPlaybackThread =
2770 static_cast<MmapPlaybackThread *>(mmapThread);
2771 volumeInterface = mmapPlaybackThread;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002772 }
2773 }
2774 }
2775 return volumeInterface;
2776}
2777
2778Vector <AudioFlinger::VolumeInterface *> AudioFlinger::getAllVolumeInterfaces_l() const
2779{
2780 Vector <VolumeInterface *> volumeInterfaces;
2781 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7459b902017-04-19 18:10:41 -07002782 volumeInterfaces.add(mPlaybackThreads.valueAt(i).get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002783 }
2784 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2785 if (mMmapThreads.valueAt(i)->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07002786 MmapPlaybackThread *mmapPlaybackThread =
2787 static_cast<MmapPlaybackThread *>(mMmapThreads.valueAt(i).get());
2788 volumeInterfaces.add(mmapPlaybackThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002789 }
2790 }
2791 return volumeInterfaces;
2792}
2793
Glenn Kasteneeecb982016-02-26 10:44:04 -08002794audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002795{
Glenn Kasten9d003132016-04-06 14:38:09 -07002796 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002797 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002798 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2799 for (int retry = 0; retry < maxRetries; retry++) {
2800 // The cast allows wraparound from max positive to min negative instead of abort
2801 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2802 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2803 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2804 // allow wrap by skipping 0 and -1 for session ids
2805 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2806 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2807 return (audio_unique_id_t) (base | use);
2808 }
2809 }
2810 // We have no way of recovering from wraparound
2811 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2812 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002813}
2814
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002815AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002816{
2817 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2818 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002819 if(thread->isDuplicating()) {
2820 continue;
2821 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002822 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002823 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002824 return thread;
2825 }
2826 }
2827 return NULL;
2828}
2829
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002830audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002831{
2832 PlaybackThread *thread = primaryPlaybackThread_l();
2833
2834 if (thread == NULL) {
2835 return 0;
2836 }
2837
Eric Laurentf1c04f92012-08-28 14:26:53 -07002838 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002839}
2840
Glenn Kastena7335632016-06-09 17:09:53 -07002841AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
2842{
2843 size_t minFrameCount = 0;
2844 PlaybackThread *minThread = NULL;
2845 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2846 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2847 if (!thread->isDuplicating()) {
2848 size_t frameCount = thread->frameCountHAL();
2849 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
2850 (frameCount == minFrameCount && thread->hasFastMixer() &&
2851 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
2852 minFrameCount = frameCount;
2853 minThread = thread;
2854 }
2855 }
2856 }
2857 return minThread;
2858}
2859
Eric Laurenta011e352012-03-29 15:51:43 -07002860sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002861 audio_session_t triggerSession,
2862 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002863 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002864 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002865{
2866 Mutex::Autolock _l(mLock);
2867
2868 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2869 status_t playStatus = NAME_NOT_FOUND;
2870 status_t recStatus = NAME_NOT_FOUND;
2871 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2872 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2873 if (playStatus == NO_ERROR) {
2874 return event;
2875 }
2876 }
2877 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2878 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2879 if (recStatus == NO_ERROR) {
2880 return event;
2881 }
2882 }
2883 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2884 mPendingSyncEvents.add(event);
2885 } else {
2886 ALOGV("createSyncEvent() invalid event %d", event->type());
2887 event.clear();
2888 }
2889 return event;
2890}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002891
Mathias Agopian65ab4712010-07-14 17:59:35 -07002892// ----------------------------------------------------------------------------
2893// Effect management
2894// ----------------------------------------------------------------------------
2895
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002896sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
2897 return mEffectsFactoryHal;
2898}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002899
Glenn Kastenf587ba52012-01-26 16:25:10 -08002900status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) 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->queryNumberEffects(numEffects);
2905 } else {
2906 return -ENODEV;
2907 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002908}
2909
Glenn Kastenf587ba52012-01-26 16:25:10 -08002910status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002911{
2912 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002913 if (mEffectsFactoryHal.get()) {
2914 return mEffectsFactoryHal->getDescriptor(index, descriptor);
2915 } else {
2916 return -ENODEV;
2917 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002918}
2919
Glenn Kasten5e92a782012-01-30 07:40:52 -08002920status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002921 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002922{
2923 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002924 if (mEffectsFactoryHal.get()) {
2925 return mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
2926 } else {
2927 return -ENODEV;
2928 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002929}
2930
2931
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002932sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002933 effect_descriptor_t *pDesc,
2934 const sp<IEffectClient>& effectClient,
2935 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002936 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002937 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002938 const String16& opPackageName,
Eric Laurentb6436272016-12-07 19:24:50 -08002939 pid_t pid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002940 status_t *status,
2941 int *id,
2942 int *enabled)
2943{
2944 status_t lStatus = NO_ERROR;
2945 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002946 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002947
Eric Laurentb6436272016-12-07 19:24:50 -08002948 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Andy Hung4ef19fa2018-05-15 19:35:29 -07002949 if (pid == -1 || !isAudioServerOrMediaServerUid(callingUid)) {
Eric Laurentb6436272016-12-07 19:24:50 -08002950 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
2951 ALOGW_IF(pid != -1 && pid != callingPid,
2952 "%s uid %d pid %d tried to pass itself off as pid %d",
2953 __func__, callingUid, callingPid, pid);
2954 pid = callingPid;
2955 }
2956
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002957 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
2958 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002959
2960 if (pDesc == NULL) {
2961 lStatus = BAD_VALUE;
2962 goto Exit;
2963 }
2964
Eric Laurent84e9a102010-09-23 16:10:16 -07002965 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002966 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002967 lStatus = PERMISSION_DENIED;
2968 goto Exit;
2969 }
2970
Dima Zavinfce7a472011-04-19 22:30:36 -07002971 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Andy Hung4ef19fa2018-05-15 19:35:29 -07002972 // that can only be created by audio policy manager
2973 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && !isAudioServerUid(callingUid)) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002974 lStatus = PERMISSION_DENIED;
2975 goto Exit;
2976 }
2977
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002978 if (mEffectsFactoryHal == 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002979 lStatus = NO_INIT;
2980 goto Exit;
2981 }
2982
Mathias Agopian65ab4712010-07-14 17:59:35 -07002983 {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002984 if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002985 // if uuid is specified, request effect descriptor
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002986 lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002987 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002988 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002989 goto Exit;
2990 }
2991 } else {
2992 // if uuid is not specified, look for an available implementation
2993 // of the required type in effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002994 if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002995 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002996 lStatus = BAD_VALUE;
2997 goto Exit;
2998 }
2999 uint32_t numEffects = 0;
3000 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003001 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07003002 bool found = false;
3003
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003004 lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003005 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003006 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003007 goto Exit;
3008 }
3009 for (uint32_t i = 0; i < numEffects; i++) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003010 lStatus = mEffectsFactoryHal->getDescriptor(i, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003011 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003012 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003013 continue;
3014 }
3015 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
3016 // If matching type found save effect descriptor. If the session is
3017 // 0 and the effect is not auxiliary, continue enumeration in case
3018 // an auxiliary version of this effect type is available
3019 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08003020 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07003021 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07003022 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
3023 break;
3024 }
3025 }
3026 }
3027 if (!found) {
3028 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00003029 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07003030 goto Exit;
3031 }
3032 // For same effect type, chose auxiliary version over insert version if
3033 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07003034 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07003035 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08003036 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003037 }
3038 }
3039
3040 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07003041 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07003042 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
3043 lStatus = INVALID_OPERATION;
3044 goto Exit;
3045 }
3046
Eric Laurent59255e42011-07-27 19:49:51 -07003047 // check recording permission for visualizer
3048 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Svet Ganov6e641372018-03-02 09:21:30 -08003049 // TODO: Do we need to start/stop op - i.e. is there recording being performed?
Marco Nelissendcb346b2015-09-09 10:47:29 -07003050 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07003051 lStatus = PERMISSION_DENIED;
3052 goto Exit;
3053 }
3054
Mathias Agopian65ab4712010-07-14 17:59:35 -07003055 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08003056 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07003057 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07003058 // if the output returned by getOutputForEffect() is removed before we lock the
3059 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
3060 // and we will exit safely
3061 io = AudioSystem::getOutputForEffect(&desc);
3062 ALOGV("createEffect got output %d", io);
3063 }
3064
3065 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003066
3067 // If output is not specified try to find a matching audio session ID in one of the
3068 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07003069 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
3070 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003071 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07003072 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07003073 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
3074 // output must be specified by AudioPolicyManager when using session
3075 // AUDIO_SESSION_OUTPUT_STAGE
3076 lStatus = BAD_VALUE;
3077 goto Exit;
3078 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07003079 // look for the thread where the specified audio session is present
3080 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3081 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
3082 io = mPlaybackThreads.keyAt(i);
3083 break;
3084 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003085 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08003086 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07003087 for (size_t i = 0; i < mRecordThreads.size(); i++) {
3088 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
3089 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07003090 break;
3091 }
3092 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003093 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08003094 if (io == AUDIO_IO_HANDLE_NONE) {
3095 for (size_t i = 0; i < mMmapThreads.size(); i++) {
3096 if (mMmapThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
3097 io = mMmapThreads.keyAt(i);
3098 break;
3099 }
3100 }
3101 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003102 // If no output thread contains the requested session ID, default to
3103 // first output. The effect chain will be moved to the correct output
3104 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07003105 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003106 io = mPlaybackThreads.keyAt(0);
3107 }
Steve Block3856b092011-10-20 11:56:00 +01003108 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003109 }
3110 ThreadBase *thread = checkRecordThread_l(io);
3111 if (thread == NULL) {
3112 thread = checkPlaybackThread_l(io);
3113 if (thread == NULL) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08003114 thread = checkMmapThread_l(io);
3115 if (thread == NULL) {
3116 ALOGE("createEffect() unknown output thread");
3117 lStatus = BAD_VALUE;
3118 goto Exit;
3119 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003120 }
Eric Laurentaaa44472014-09-12 17:41:50 -07003121 } else {
3122 // Check if one effect chain was awaiting for an effect to be created on this
3123 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08003124 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07003125 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07003126 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07003127 thread->addEffectChain_l(chain);
3128 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003129 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003130
Eric Laurent021cf962014-05-13 10:18:14 -07003131 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003132
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003133 // create effect on selected output thread
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003134 bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003135 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003136 &desc, enabled, &lStatus, pinned);
haobo101735295ff7b0d2017-03-17 17:44:03 +08003137 if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003138 // remove local strong reference to Client with mClientLock held
3139 Mutex::Autolock _cl(mClientLock);
3140 client.clear();
haobo101735295ff7b0d2017-03-17 17:44:03 +08003141 } else {
3142 // handle must be valid here, but check again to be safe.
3143 if (handle.get() != nullptr && id != nullptr) *id = handle->id();
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003144 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003145 }
3146
haobo101735295ff7b0d2017-03-17 17:44:03 +08003147 if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
3148 // handle must be cleared outside lock.
3149 handle.clear();
3150 }
3151
Mathias Agopian65ab4712010-07-14 17:59:35 -07003152Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07003153 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003154 return handle;
3155}
3156
Glenn Kastend848eb42016-03-08 13:42:11 -08003157status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003158 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07003159{
Steve Block3856b092011-10-20 11:56:00 +01003160 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07003161 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003162 Mutex::Autolock _l(mLock);
3163 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003164 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003165 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003166 }
Eric Laurentde070132010-07-13 04:45:46 -07003167 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
3168 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003169 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003170 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003171 }
Eric Laurentde070132010-07-13 04:45:46 -07003172 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
3173 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003174 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003175 return BAD_VALUE;
3176 }
3177
3178 Mutex::Autolock _dl(dstThread->mLock);
3179 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003180 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003181}
3182
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003183// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08003184status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07003185 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07003186 AudioFlinger::PlaybackThread *dstThread,
3187 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07003188{
Steve Block3856b092011-10-20 11:56:00 +01003189 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003190 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07003191
Eric Laurent59255e42011-07-27 19:49:51 -07003192 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003193 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003194 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003195 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07003196 return INVALID_OPERATION;
3197 }
3198
Eric Laurent4c415062016-06-17 16:14:16 -07003199 // Check whether the destination thread and all effects in the chain are compatible
3200 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07003201 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07003202 " destination thread %p is not compatible with effects in the chain",
3203 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07003204 return INVALID_OPERATION;
3205 }
3206
Eric Laurent39e94f82010-07-28 01:32:47 -07003207 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07003208 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07003209 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07003210 // removed.
3211 srcThread->removeEffectChain_l(chain);
3212
3213 // transfer all effects one by one so that new effect chain is created on new thread with
3214 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07003215 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003216 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07003217 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003218 Vector< sp<EffectModule> > removed;
3219 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07003220 while (effect != 0) {
3221 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003222 removed.add(effect);
3223 status = dstThread->addEffect_l(effect);
3224 if (status != NO_ERROR) {
3225 break;
3226 }
Eric Laurentec35a142011-10-05 17:42:25 -07003227 // removeEffect_l() has stopped the effect if it was active so it must be restarted
3228 if (effect->state() == EffectModule::ACTIVE ||
3229 effect->state() == EffectModule::STOPPING) {
3230 effect->start();
3231 }
Eric Laurent39e94f82010-07-28 01:32:47 -07003232 // if the move request is not received from audio policy manager, the effect must be
3233 // re-registered with the new strategy and output
3234 if (dstChain == 0) {
3235 dstChain = effect->chain().promote();
3236 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003237 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003238 status = NO_INIT;
3239 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07003240 }
3241 strategy = dstChain->strategy();
3242 }
3243 if (reRegister) {
3244 AudioSystem::unregisterEffect(effect->id());
3245 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07003246 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07003247 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07003248 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07003249 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003250 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07003251 }
Eric Laurentde070132010-07-13 04:45:46 -07003252 effect = chain->getEffectFromId_l(0);
3253 }
3254
Eric Laurent5baf2af2013-09-12 17:37:00 -07003255 if (status != NO_ERROR) {
3256 for (size_t i = 0; i < removed.size(); i++) {
3257 srcThread->addEffect_l(removed[i]);
3258 if (dstChain != 0 && reRegister) {
3259 AudioSystem::unregisterEffect(removed[i]->id());
3260 AudioSystem::registerEffect(&removed[i]->desc(),
3261 srcThread->id(),
3262 strategy,
3263 sessionId,
3264 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003265 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003266 }
3267 }
3268 }
3269
3270 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003271}
3272
Eric Laurent5baf2af2013-09-12 17:37:00 -07003273bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07003274{
3275 if (mGlobalEffectEnableTime != 0 &&
3276 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
3277 return true;
3278 }
3279
3280 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3281 sp<EffectChain> ec =
3282 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003283 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07003284 return true;
3285 }
3286 }
3287 return false;
3288}
3289
Eric Laurent5baf2af2013-09-12 17:37:00 -07003290void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07003291{
3292 Mutex::Autolock _l(mLock);
3293
3294 mGlobalEffectEnableTime = systemTime();
3295
3296 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3297 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
3298 if (t->mType == ThreadBase::OFFLOAD) {
3299 t->invalidateTracks(AUDIO_STREAM_MUSIC);
3300 }
3301 }
3302
3303}
3304
Eric Laurentaaa44472014-09-12 17:41:50 -07003305status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
3306{
Eric Laurentd8365c52017-07-16 15:27:05 -07003307 // clear possible suspended state before parking the chain so that it starts in default state
3308 // when attached to a new record thread
3309 chain->setEffectSuspended_l(FX_IID_AEC, false);
3310 chain->setEffectSuspended_l(FX_IID_NS, false);
3311
Glenn Kastend848eb42016-03-08 13:42:11 -08003312 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003313 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003314 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003315 if (index >= 0) {
3316 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
3317 return ALREADY_EXISTS;
3318 }
3319 mOrphanEffectChains.add(session, chain);
3320 return NO_ERROR;
3321}
3322
3323sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
3324{
3325 sp<EffectChain> chain;
3326 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003327 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003328 if (index >= 0) {
3329 chain = mOrphanEffectChains.valueAt(index);
3330 mOrphanEffectChains.removeItemsAt(index);
3331 }
3332 return chain;
3333}
3334
3335bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
3336{
3337 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08003338 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003339 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003340 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003341 if (index >= 0) {
3342 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003343 if (chain->removeEffect_l(effect, true) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003344 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003345 mOrphanEffectChains.removeItemsAt(index);
3346 }
3347 return true;
3348 }
3349 return false;
3350}
3351
3352
Mathias Agopian65ab4712010-07-14 17:59:35 -07003353// ----------------------------------------------------------------------------
3354
3355status_t AudioFlinger::onTransact(
3356 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3357{
3358 return BnAudioFlinger::onTransact(code, data, reply, flags);
3359}
3360
Glenn Kasten63238ef2015-03-02 15:50:29 -08003361} // namespace android