blob: e722995aa3aa4bd6ec09fa9580c53c6d2732a1b5 [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
jiabin8cb192f2019-09-23 10:34:20 -070022// Define AUDIO_ARRAYS_STATIC_CHECK to check all audio arrays are correct
23#define AUDIO_ARRAYS_STATIC_CHECK 1
24
Glenn Kasten153b9fe2013-07-15 11:23:36 -070025#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080026#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070027#include <math.h>
28#include <signal.h>
Eric Tan7b651152018-07-13 10:17:19 -070029#include <string>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <sys/time.h>
31#include <sys/resource.h>
32
jiabin57303cc2018-12-18 15:45:57 -080033#include <android/os/IExternalVibratorService.h>
Gloria Wang9ee159b2011-02-24 14:51:45 -080034#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <binder/IServiceManager.h>
36#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070037#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <binder/Parcel.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070039#include <media/audiohal/DeviceHalInterface.h>
40#include <media/audiohal/DevicesFactoryHalInterface.h>
41#include <media/audiohal/EffectsFactoryHalInterface.h>
Mikhail Naganov00260b52016-10-13 12:54:24 -070042#include <media/AudioParameter.h>
Mikhail Naganov913d06c2016-11-01 12:49:22 -070043#include <media/TypeConverter.h>
Andy Hung35fec5f2016-04-13 14:21:48 -070044#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045#include <utils/String16.h>
46#include <utils/threads.h>
47
Steven Morelandf0c02ce2018-02-23 14:53:55 -080048#include <cutils/atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070049#include <cutils/properties.h>
50
Dima Zavin64760242011-05-11 14:15:23 -070051#include <system/audio.h>
Mikhail Naganov2996f672019-04-18 12:29:59 -070052#include <audiomanager/AudioManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Mathias Agopian65ab4712010-07-14 17:59:35 -070054#include "AudioFlinger.h"
Andy Hung8946a282018-04-19 20:04:56 -070055#include "NBAIO_Tee.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
Andy Hung6770c6f2015-04-07 13:43:36 -070057#include <media/AudioResamplerPublic.h>
58
Mikhail Naganov9fe94012016-10-14 14:57:40 -070059#include <system/audio_effects/effect_visualizer.h>
60#include <system/audio_effects/effect_ns.h>
61#include <system/audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070062
Glenn Kasten3b21c502011-12-15 09:52:39 -080063#include <audio_utils/primitives.h>
64
Eric Laurentfeb0db62011-07-22 09:04:31 -070065#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080066
Glenn Kasten9e58b552013-01-18 15:09:48 -080067#include <media/IMediaLogService.h>
Andy Hung07b745e2016-05-23 16:21:07 -070068#include <media/MemoryLeakTrackUtil.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080069#include <media/nbaio/Pipe.h>
70#include <media/nbaio/PipeReader.h>
Wei Jia3f273d12015-11-24 09:06:49 -080071#include <mediautils/BatteryNotifier.h>
Andy Hungab7ef302018-05-15 19:35:29 -070072#include <mediautils/ServiceUtilities.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070073#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080074
rago3bd1c872016-09-26 12:58:14 -070075//#define BUFLOG_NDEBUG 0
76#include <BufLog.h>
77
Nicolas Rouletfe1e1442017-01-30 12:02:03 -080078#include "TypedLogger.h"
79
Mathias Agopian65ab4712010-07-14 17:59:35 -070080// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070081
John Grossman1c345192012-03-27 14:00:17 -070082// Note: the following macro is used for extremely verbose logging message. In
83// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
84// 0; but one side effect of this is to turn all LOGV's as well. Some messages
85// are so verbose that we want to suppress them even when we have ALOG_ASSERT
86// turned on. Do not uncomment the #def below unless you really know what you
87// are doing and want to see all of the extremely verbose messages.
88//#define VERY_VERY_VERBOSE_LOGGING
89#ifdef VERY_VERY_VERBOSE_LOGGING
90#define ALOGVV ALOGV
91#else
92#define ALOGVV(a...) do { } while(0)
93#endif
Eric Laurentde070132010-07-13 04:45:46 -070094
Mathias Agopian65ab4712010-07-14 17:59:35 -070095namespace android {
96
Glenn Kastenec1d6b52011-12-12 09:04:45 -080097static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
98static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070099static const char kClientLockedString[] = "Client lock is taken\n";
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700100static const char kNoEffectsFactory[] = "Effects Factory is absent\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -0700101
Glenn Kasten58912562012-04-03 10:45:00 -0700102
John Grossman4ff14ba2012-02-08 16:37:41 -0800103nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -0800104
Eric Laurent81784c32012-11-19 14:55:58 -0800105uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -0700106
Eric Laurent813e2a72013-08-31 12:59:48 -0700107// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
108// we define a minimum time during which a global effect is considered enabled.
109static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
110
Eric Laurentfc235202016-12-20 18:48:17 -0800111Mutex gLock;
112wp<AudioFlinger> gAudioFlinger;
113
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700114// Keep a strong reference to media.log service around forever.
115// The service is within our parent process so it can never die in a way that we could observe.
116// These two variables are const after initialization.
117static sp<IBinder> sMediaLogServiceAsBinder;
118static sp<IMediaLogService> sMediaLogService;
119
120static pthread_once_t sMediaLogOnce = PTHREAD_ONCE_INIT;
121
122static void sMediaLogInit()
123{
124 sMediaLogServiceAsBinder = defaultServiceManager()->getService(String16("media.log"));
125 if (sMediaLogServiceAsBinder != 0) {
126 sMediaLogService = interface_cast<IMediaLogService>(sMediaLogServiceAsBinder);
127 }
128}
129
jiabin57303cc2018-12-18 15:45:57 -0800130// Keep a strong reference to external vibrator service
131static sp<os::IExternalVibratorService> sExternalVibratorService;
132
133static sp<os::IExternalVibratorService> getExternalVibratorService() {
134 if (sExternalVibratorService == 0) {
135 sp <IBinder> binder = defaultServiceManager()->getService(
136 String16("external_vibrator_service"));
137 if (binder != 0) {
138 sExternalVibratorService =
139 interface_cast<os::IExternalVibratorService>(binder);
140 }
141 }
142 return sExternalVibratorService;
143}
144
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145// ----------------------------------------------------------------------------
146
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700147std::string formatToString(audio_format_t format) {
148 std::string result;
149 FormatConverter::toString(format, result);
150 return result;
Marco Nelissenb2208842014-02-07 14:00:50 -0800151}
152
Mathias Agopian65ab4712010-07-14 17:59:35 -0700153// ----------------------------------------------------------------------------
154
155AudioFlinger::AudioFlinger()
156 : BnAudioFlinger(),
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -0800157 mMediaLogNotifier(new AudioFlinger::MediaLogNotifier()),
John Grossman4ff14ba2012-02-08 16:37:41 -0800158 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800159 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700160 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800161 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800162 mMasterMute(false),
Glenn Kastend2e67e12016-04-11 08:26:37 -0700163 // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),
John Grossman4ff14ba2012-02-08 16:37:41 -0800164 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700165 mBtNrecIsOff(false),
166 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700167 mIsDeviceTypeKnown(false),
Andy Hung6f248bb2018-01-23 14:04:37 -0800168 mTotalMemory(0),
169 mClientSharedHeapSize(kMinimumClientSharedHeapSizeBytes),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700170 mGlobalEffectEnableTime(0),
Mikhail Naganovdea53042018-04-26 13:10:21 -0700171 mPatchPanel(this),
Eric Laurent72e3f392015-05-20 14:43:50 -0700172 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700173{
Glenn Kastend2e67e12016-04-11 08:26:37 -0700174 // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum
175 for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) {
176 // zero ID has a special meaning, so unavailable
177 mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX;
178 }
179
Glenn Kastenae0cff12016-02-24 13:57:49 -0800180 const bool doLog = property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800181 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800182 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
183 MemoryHeapBase::READ_ONLY);
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700184 (void) pthread_once(&sMediaLogOnce, sMediaLogInit);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800185 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700186
Wei Jia3f273d12015-11-24 09:06:49 -0800187 // reset battery stats.
188 // if the audio service has crashed, battery stats could be left
189 // in bad state, reset the state upon service start.
190 BatteryNotifier::getInstance().noteResetAudio();
191
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700192 mDevicesFactoryHal = DevicesFactoryHalInterface::create();
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700193 mEffectsFactoryHal = EffectsFactoryHalInterface::create();
194
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -0800195 mMediaLogNotifier->run("MediaLogNotifier");
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700196}
197
198void AudioFlinger::onFirstRef()
199{
Eric Laurent93575202011-01-18 18:39:02 -0800200 Mutex::Autolock _l(mLock);
201
Dima Zavin799a70e2011-04-18 16:57:27 -0700202 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800203 char val_str[PROPERTY_VALUE_MAX] = { 0 };
204 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
205 uint32_t int_val;
206 if (1 == sscanf(val_str, "%u", &int_val)) {
207 mStandbyTimeInNsecs = milliseconds(int_val);
208 ALOGI("Using %u mSec as standby time.", int_val);
209 } else {
210 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
211 ALOGI("Using default %u mSec as standby time.",
212 (uint32_t)(mStandbyTimeInNsecs / 1000000));
213 }
214 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700215
Eric Laurenta4c5a552012-03-29 10:12:40 -0700216 mMode = AUDIO_MODE_NORMAL;
Eric Laurentfc235202016-12-20 18:48:17 -0800217
218 gAudioFlinger = this;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700219}
220
221AudioFlinger::~AudioFlinger()
222{
223 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700224 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700225 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226 }
227 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700228 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700229 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230 }
Andy Hungb4946b62019-03-14 11:19:28 -0700231 while (!mMmapThreads.isEmpty()) {
232 const audio_io_handle_t io = mMmapThreads.keyAt(0);
233 if (mMmapThreads.valueAt(0)->isOutput()) {
234 closeOutput_nonvirtual(io); // removes entry from mMmapThreads
235 } else {
236 closeInput_nonvirtual(io); // removes entry from mMmapThreads
237 }
238 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700239
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800240 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
241 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700242 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700244
245 // Tell media.log service about any old writers that still need to be unregistered
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700246 if (sMediaLogService != 0) {
247 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
248 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
249 mUnregisteredWriters.pop();
250 sMediaLogService->unregisterWriter(iMemory);
Glenn Kasten481fb672013-09-30 14:39:28 -0700251 }
252 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253}
254
Eric Laurentfc235202016-12-20 18:48:17 -0800255//static
Eric Laurent6acd1d42017-01-04 14:23:29 -0800256__attribute__ ((visibility ("default")))
Eric Laurentfc235202016-12-20 18:48:17 -0800257status_t MmapStreamInterface::openMmapStream(MmapStreamInterface::stream_direction_t direction,
258 const audio_attributes_t *attr,
259 audio_config_base_t *config,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700260 const AudioClient& client,
Eric Laurentfc235202016-12-20 18:48:17 -0800261 audio_port_handle_t *deviceId,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800262 audio_session_t *sessionId,
Eric Laurentfc235202016-12-20 18:48:17 -0800263 const sp<MmapStreamCallback>& callback,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700264 sp<MmapStreamInterface>& interface,
265 audio_port_handle_t *handle)
Eric Laurentfc235202016-12-20 18:48:17 -0800266{
267 sp<AudioFlinger> af;
268 {
269 Mutex::Autolock _l(gLock);
270 af = gAudioFlinger.promote();
271 }
272 status_t ret = NO_INIT;
273 if (af != 0) {
274 ret = af->openMmapStream(
Phil Burk4e1af9f2018-01-03 15:54:35 -0800275 direction, attr, config, client, deviceId,
276 sessionId, callback, interface, handle);
Eric Laurentfc235202016-12-20 18:48:17 -0800277 }
278 return ret;
279}
280
Eric Laurent6acd1d42017-01-04 14:23:29 -0800281status_t AudioFlinger::openMmapStream(MmapStreamInterface::stream_direction_t direction,
282 const audio_attributes_t *attr,
283 audio_config_base_t *config,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700284 const AudioClient& client,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800285 audio_port_handle_t *deviceId,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800286 audio_session_t *sessionId,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800287 const sp<MmapStreamCallback>& callback,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700288 sp<MmapStreamInterface>& interface,
289 audio_port_handle_t *handle)
Eric Laurentfc235202016-12-20 18:48:17 -0800290{
291 status_t ret = initCheck();
292 if (ret != NO_ERROR) {
293 return ret;
294 }
Phil Burk4e1af9f2018-01-03 15:54:35 -0800295 audio_session_t actualSessionId = *sessionId;
296 if (actualSessionId == AUDIO_SESSION_ALLOCATE) {
297 actualSessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
298 }
Eric Laurent6acd1d42017-01-04 14:23:29 -0800299 audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700300 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800301 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent42984412019-05-09 17:57:03 -0700302 audio_attributes_t localAttr = *attr;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800303 if (direction == MmapStreamInterface::DIRECTION_OUTPUT) {
304 audio_config_t fullConfig = AUDIO_CONFIG_INITIALIZER;
305 fullConfig.sample_rate = config->sample_rate;
306 fullConfig.channel_mask = config->channel_mask;
307 fullConfig.format = config->format;
Kevin Rocard153f92d2018-12-18 18:33:28 -0800308 std::vector<audio_io_handle_t> secondaryOutputs;
Eric Laurent42984412019-05-09 17:57:03 -0700309
310 ret = AudioSystem::getOutputForAttr(&localAttr, &io,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800311 actualSessionId,
Nadav Bar766fb022018-01-07 12:18:03 +0200312 &streamType, client.clientPid, client.clientUid,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800313 &fullConfig,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800314 (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ |
315 AUDIO_OUTPUT_FLAG_DIRECT),
Kevin Rocard153f92d2018-12-18 18:33:28 -0800316 deviceId, &portId, &secondaryOutputs);
317 ALOGW_IF(!secondaryOutputs.empty(),
318 "%s does not support secondary outputs, ignoring them", __func__);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800319 } else {
Eric Laurent42984412019-05-09 17:57:03 -0700320 ret = AudioSystem::getInputForAttr(&localAttr, &io,
Mikhail Naganov2996f672019-04-18 12:29:59 -0700321 RECORD_RIID_INVALID,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800322 actualSessionId,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800323 client.clientPid,
324 client.clientUid,
Eric Laurentfee19762018-01-29 18:44:13 -0800325 client.packageName,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800326 config,
Eric Laurent2ac76942017-06-22 17:17:09 -0700327 AUDIO_INPUT_FLAG_MMAP_NOIRQ, deviceId, &portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800328 }
329 if (ret != NO_ERROR) {
330 return ret;
331 }
332
333 // at this stage, a MmapThread was created when openOutput() or openInput() was called by
334 // audio policy manager and we can retrieve it
335 sp<MmapThread> thread = mMmapThreads.valueFor(io);
336 if (thread != 0) {
337 interface = new MmapThreadHandle(thread);
Eric Laurent42984412019-05-09 17:57:03 -0700338 thread->configure(&localAttr, streamType, actualSessionId, callback, *deviceId, portId);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700339 *handle = portId;
Phil Burk4e1af9f2018-01-03 15:54:35 -0800340 *sessionId = actualSessionId;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800341 } else {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700342 if (direction == MmapStreamInterface::DIRECTION_OUTPUT) {
Eric Laurentd7fe0862018-07-14 16:48:01 -0700343 AudioSystem::releaseOutput(portId);
Eric Laurentad2e7b92017-09-14 20:06:42 -0700344 } else {
Eric Laurentfee19762018-01-29 18:44:13 -0800345 AudioSystem::releaseInput(portId);
Eric Laurentad2e7b92017-09-14 20:06:42 -0700346 }
Eric Laurent6acd1d42017-01-04 14:23:29 -0800347 ret = NO_INIT;
348 }
349
350 ALOGV("%s done status %d portId %d", __FUNCTION__, ret, portId);
351
Eric Laurentfc235202016-12-20 18:48:17 -0800352 return ret;
353}
354
jiabin57303cc2018-12-18 15:45:57 -0800355/* static */
356int AudioFlinger::onExternalVibrationStart(const sp<os::ExternalVibration>& externalVibration) {
357 sp<os::IExternalVibratorService> evs = getExternalVibratorService();
358 if (evs != 0) {
359 int32_t ret;
360 binder::Status status = evs->onExternalVibrationStart(*externalVibration, &ret);
361 if (status.isOk()) {
362 return ret;
363 }
364 }
jiabinbf6b0ec2019-02-12 12:30:12 -0800365 return AudioMixer::HAPTIC_SCALE_MUTE;
jiabin57303cc2018-12-18 15:45:57 -0800366}
367
368/* static */
369void AudioFlinger::onExternalVibrationStop(const sp<os::ExternalVibration>& externalVibration) {
370 sp<os::IExternalVibratorService> evs = getExternalVibratorService();
371 if (evs != 0) {
372 evs->onExternalVibrationStop(*externalVibration);
373 }
374}
375
Eric Laurenta4c5a552012-03-29 10:12:40 -0700376static const char * const audio_interfaces[] = {
377 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
378 AUDIO_HARDWARE_MODULE_ID_A2DP,
379 AUDIO_HARDWARE_MODULE_ID_USB,
380};
Eric Laurenta4c5a552012-03-29 10:12:40 -0700381
Phil Burk062e67a2015-02-11 13:40:50 -0800382AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700383 audio_module_handle_t module,
jiabinc0106832019-10-24 14:58:31 -0700384 audio_devices_t deviceType)
Dima Zavin799a70e2011-04-18 16:57:27 -0700385{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700386 // if module is 0, the request comes from an old policy manager and we should load
387 // well known modules
388 if (module == 0) {
389 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
Mikhail Naganovbf493082017-04-17 17:37:12 -0700390 for (size_t i = 0; i < arraysize(audio_interfaces); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700391 loadHwModule_l(audio_interfaces[i]);
392 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700393 // then try to find a module supporting the requested device.
394 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
395 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700396 sp<DeviceHalInterface> dev = audioHwDevice->hwDevice();
397 uint32_t supportedDevices;
398 if (dev->getSupportedDevices(&supportedDevices) == OK &&
jiabinc0106832019-10-24 14:58:31 -0700399 (supportedDevices & deviceType) == deviceType) {
Eric Laurentf1c04f92012-08-28 14:26:53 -0700400 return audioHwDevice;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700401 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700402 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700403 } else {
404 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700405 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
406 if (audioHwDevice != NULL) {
407 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700408 }
409 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700410
Dima Zavin799a70e2011-04-18 16:57:27 -0700411 return NULL;
412}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413
Glenn Kasten0f11b512014-01-31 16:18:54 -0800414void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700415{
416 const size_t SIZE = 256;
417 char buffer[SIZE];
418 String8 result;
419
420 result.append("Clients:\n");
421 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800422 sp<Client> client = mClients.valueAt(i).promote();
423 if (client != 0) {
424 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
425 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426 }
427 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700428
Eric Laurentd1b28d42013-09-18 18:47:13 -0700429 result.append("Notification Clients:\n");
430 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
431 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
432 result.append(buffer);
433 }
434
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700435 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800436 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700437 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
438 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800439 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700440 result.append(buffer);
441 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443}
444
445
Glenn Kasten0f11b512014-01-31 16:18:54 -0800446void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700447{
448 const size_t SIZE = 256;
449 char buffer[SIZE];
450 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800451 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452
John Grossman4ff14ba2012-02-08 16:37:41 -0800453 snprintf(buffer, SIZE, "Hardware status: %d\n"
454 "Standby Time mSec: %u\n",
455 hardwareStatus,
456 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700457 result.append(buffer);
458 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700459}
460
Glenn Kasten0f11b512014-01-31 16:18:54 -0800461void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700462{
463 const size_t SIZE = 256;
464 char buffer[SIZE];
465 String8 result;
466 snprintf(buffer, SIZE, "Permission Denial: "
467 "can't dump AudioFlinger from pid=%d, uid=%d\n",
468 IPCThreadState::self()->getCallingPid(),
469 IPCThreadState::self()->getCallingUid());
470 result.append(buffer);
471 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700472}
473
Eric Laurent81784c32012-11-19 14:55:58 -0800474bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475{
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700476 status_t err = mutex.timedLock(kDumpLockTimeoutNs);
477 return err == NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700478}
479
480status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
481{
Glenn Kasten44deb052012-02-05 18:09:08 -0800482 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483 dumpPermissionDenial(fd, args);
484 } else {
485 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800486 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700487 if (!hardwareLocked) {
488 String8 result(kHardwareLockedString);
489 write(fd, result.string(), result.size());
490 } else {
491 mHardwareLock.unlock();
492 }
493
Eric Tan7b651152018-07-13 10:17:19 -0700494 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700495
496 // failed to lock - AudioFlinger is probably deadlocked
497 if (!locked) {
498 String8 result(kDeadlockedString);
499 write(fd, result.string(), result.size());
500 }
501
Eric Laurent021cf962014-05-13 10:18:14 -0700502 bool clientLocked = dumpTryLock(mClientLock);
503 if (!clientLocked) {
504 String8 result(kClientLockedString);
505 write(fd, result.string(), result.size());
506 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700507
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700508 if (mEffectsFactoryHal != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700509 mEffectsFactoryHal->dumpEffects(fd);
510 } else {
511 String8 result(kNoEffectsFactory);
512 write(fd, result.string(), result.size());
513 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700514
Mathias Agopian65ab4712010-07-14 17:59:35 -0700515 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700516 if (clientLocked) {
517 mClientLock.unlock();
518 }
519
Mathias Agopian65ab4712010-07-14 17:59:35 -0700520 dumpInternals(fd, args);
521
522 // dump playback threads
523 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
524 mPlaybackThreads.valueAt(i)->dump(fd, args);
525 }
526
527 // dump record threads
528 for (size_t i = 0; i < mRecordThreads.size(); i++) {
529 mRecordThreads.valueAt(i)->dump(fd, args);
530 }
531
Eric Laurent6acd1d42017-01-04 14:23:29 -0800532 // dump mmap threads
533 for (size_t i = 0; i < mMmapThreads.size(); i++) {
534 mMmapThreads.valueAt(i)->dump(fd, args);
535 }
536
Eric Laurentaaa44472014-09-12 17:41:50 -0700537 // dump orphan effect chains
538 if (mOrphanEffectChains.size() != 0) {
539 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
540 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
541 mOrphanEffectChains.valueAt(i)->dump(fd, args);
542 }
543 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700544 // dump all hardware devs
545 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700546 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
547 dev->dump(fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700549
Mikhail Naganov201369b2018-05-16 16:52:32 -0700550 mPatchPanel.dump(fd);
551
Kevin Rocarda0a5d2a2018-08-06 15:03:18 -0700552 // dump external setParameters
553 auto dumpLogger = [fd](SimpleLog& logger, const char* name) {
554 dprintf(fd, "\n%s setParameters:\n", name);
555 logger.dump(fd, " " /* prefix */);
556 };
557 dumpLogger(mRejectedSetParameterLog, "Rejected");
558 dumpLogger(mAppSetParameterLog, "App");
559 dumpLogger(mSystemSetParameterLog, "System");
560
Andy Hunga8115dc2018-08-24 15:51:59 -0700561 // dump historical threads in the last 10 seconds
562 const std::string threadLog = mThreadLog.dumpToString(
563 "Historical Thread Log ", 0 /* lines */,
564 audio_utils_get_real_time_ns() - 10 * 60 * NANOS_PER_SECOND);
565 write(fd, threadLog.c_str(), threadLog.size());
566
rago3bd1c872016-09-26 12:58:14 -0700567 BUFLOG_RESET;
568
Glenn Kastend65d73c2012-06-22 17:21:07 -0700569 if (locked) {
570 mLock.unlock();
571 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800572
Andy Hung8946a282018-04-19 20:04:56 -0700573#ifdef TEE_SINK
574 // NBAIO_Tee dump is safe to call outside of AF lock.
575 NBAIO_Tee::dumpAll(fd, "_DUMP");
576#endif
Glenn Kasten9e58b552013-01-18 15:09:48 -0800577 // append a copy of media.log here by forwarding fd to it, but don't attempt
578 // to lookup the service if it's not running, as it will block for a second
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700579 if (sMediaLogServiceAsBinder != 0) {
580 dprintf(fd, "\nmedia.log:\n");
581 Vector<String16> args;
582 sMediaLogServiceAsBinder->dump(fd, args);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800583 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700584
585 // check for optional arguments
Andy Hung07b745e2016-05-23 16:21:07 -0700586 bool dumpMem = false;
Andy Hung35fec5f2016-04-13 14:21:48 -0700587 bool unreachableMemory = false;
588 for (const auto &arg : args) {
Andy Hung07b745e2016-05-23 16:21:07 -0700589 if (arg == String16("-m")) {
590 dumpMem = true;
591 } else if (arg == String16("--unreachable")) {
Andy Hung35fec5f2016-04-13 14:21:48 -0700592 unreachableMemory = true;
593 }
594 }
595
Andy Hung07b745e2016-05-23 16:21:07 -0700596 if (dumpMem) {
597 dprintf(fd, "\nDumping memory:\n");
598 std::string s = dumpMemoryAddresses(100 /* limit */);
599 write(fd, s.c_str(), s.size());
600 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700601 if (unreachableMemory) {
602 dprintf(fd, "\nDumping unreachable memory:\n");
603 // TODO - should limit be an argument parameter?
Andy Hung07b745e2016-05-23 16:21:07 -0700604 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
Andy Hung35fec5f2016-04-13 14:21:48 -0700605 write(fd, s.c_str(), s.size());
606 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607 }
608 return NO_ERROR;
609}
610
Eric Laurent021cf962014-05-13 10:18:14 -0700611sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800612{
Eric Laurent021cf962014-05-13 10:18:14 -0700613 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800614 // If pid is already in the mClients wp<> map, then use that entry
615 // (for which promote() is always != 0), otherwise create a new entry and Client.
616 sp<Client> client = mClients.valueFor(pid).promote();
617 if (client == 0) {
618 client = new Client(this, pid);
619 mClients.add(pid, client);
620 }
621
622 return client;
623}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700624
Glenn Kasten9e58b552013-01-18 15:09:48 -0800625sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
626{
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700627 // If there is no memory allocated for logs, return a dummy writer that does nothing.
628 // Similarly if we can't contact the media.log service, also return a dummy writer.
629 if (mLogMemoryDealer == 0 || sMediaLogService == 0) {
Glenn Kasten9e58b552013-01-18 15:09:48 -0800630 return new NBLog::Writer();
631 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700632 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
633 // If allocation fails, consult the vector of previously unregistered writers
634 // and garbage-collect one or more them until an allocation succeeds
635 if (shared == 0) {
636 Mutex::Autolock _l(mUnregisteredWritersLock);
637 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
638 {
639 // Pick the oldest stale writer to garbage-collect
640 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
641 mUnregisteredWriters.removeAt(0);
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700642 sMediaLogService->unregisterWriter(iMemory);
Glenn Kasten481fb672013-09-30 14:39:28 -0700643 // Now the media.log remote reference to IMemory is gone. When our last local
644 // reference to IMemory also drops to zero at end of this block,
645 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
646 }
647 // Re-attempt the allocation
648 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
649 if (shared != 0) {
650 goto success;
651 }
652 }
653 // Even after garbage-collecting all old writers, there is still not enough memory,
654 // so return a dummy writer
655 return new NBLog::Writer();
656 }
657success:
Glenn Kasten535e1612016-12-05 12:19:36 -0800658 NBLog::Shared *sharedRawPtr = (NBLog::Shared *) shared->pointer();
659 new((void *) sharedRawPtr) NBLog::Shared(); // placement new here, but the corresponding
660 // explicit destructor not needed since it is POD
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700661 sMediaLogService->registerWriter(shared, size, name);
Glenn Kasten535e1612016-12-05 12:19:36 -0800662 return new NBLog::Writer(shared, size);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800663}
664
665void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
666{
Glenn Kasten685ef092013-02-04 08:15:34 -0800667 if (writer == 0) {
668 return;
669 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800670 sp<IMemory> iMemory(writer->getIMemory());
671 if (iMemory == 0) {
672 return;
673 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700674 // Rather than removing the writer immediately, append it to a queue of old writers to
675 // be garbage-collected later. This allows us to continue to view old logs for a while.
676 Mutex::Autolock _l(mUnregisteredWritersLock);
677 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800678}
679
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680// IAudioFlinger interface
681
Eric Laurent21da6472017-11-09 16:29:26 -0800682sp<IAudioTrack> AudioFlinger::createTrack(const CreateTrackInput& input,
683 CreateTrackOutput& output,
684 status_t *status)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685{
686 sp<PlaybackThread::Track> track;
687 sp<TrackHandle> trackHandle;
688 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689 status_t lStatus;
Eric Laurent21da6472017-11-09 16:29:26 -0800690 audio_stream_type_t streamType;
Eric Laurent77881cd2018-02-09 16:01:31 -0800691 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
Kevin Rocard153f92d2018-12-18 18:33:28 -0800692 std::vector<audio_io_handle_t> secondaryOutputs;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700693
Eric Laurent21da6472017-11-09 16:29:26 -0800694 bool updatePid = (input.clientInfo.clientPid == -1);
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700695 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurent21da6472017-11-09 16:29:26 -0800696 uid_t clientUid = input.clientInfo.clientUid;
Eric Laurent6c796322019-04-09 14:13:17 -0700697 audio_io_handle_t effectThreadId = AUDIO_IO_HANDLE_NONE;
698 std::vector<int> effectIds;
Eric Laurent42984412019-05-09 17:57:03 -0700699 audio_attributes_t localAttr = input.attr;
Eric Laurent6c796322019-04-09 14:13:17 -0700700
Andy Hung4ef19fa2018-05-15 19:35:29 -0700701 if (!isAudioServerOrMediaServerUid(callingUid)) {
Eric Laurent21da6472017-11-09 16:29:26 -0800702 ALOGW_IF(clientUid != callingUid,
703 "%s uid %d tried to pass itself off as %d",
704 __FUNCTION__, callingUid, clientUid);
705 clientUid = callingUid;
706 updatePid = true;
707 }
708 pid_t clientPid = input.clientInfo.clientPid;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700709 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurent21da6472017-11-09 16:29:26 -0800710 if (updatePid) {
Eric Laurent21da6472017-11-09 16:29:26 -0800711 ALOGW_IF(clientPid != -1 && clientPid != callingPid,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700712 "%s uid %d pid %d tried to pass itself off as pid %d",
Eric Laurent21da6472017-11-09 16:29:26 -0800713 __func__, callingUid, callingPid, clientPid);
714 clientPid = callingPid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700715 }
716
Eric Laurent21da6472017-11-09 16:29:26 -0800717 audio_session_t sessionId = input.sessionId;
718 if (sessionId == AUDIO_SESSION_ALLOCATE) {
719 sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Eric Laurentf14db3c2017-12-08 14:20:36 -0800720 } else if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
721 lStatus = BAD_VALUE;
722 goto Exit;
Eric Laurent21da6472017-11-09 16:29:26 -0800723 }
Eric Laurentf14db3c2017-12-08 14:20:36 -0800724
Eric Laurent21da6472017-11-09 16:29:26 -0800725 output.sessionId = sessionId;
726 output.outputId = AUDIO_IO_HANDLE_NONE;
727 output.selectedDeviceId = input.selectedDeviceId;
Eric Laurent42984412019-05-09 17:57:03 -0700728 lStatus = AudioSystem::getOutputForAttr(&localAttr, &output.outputId, sessionId, &streamType,
Nadav Bar766fb022018-01-07 12:18:03 +0200729 clientPid, clientUid, &input.config, input.flags,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800730 &output.selectedDeviceId, &portId, &secondaryOutputs);
Eric Laurent21da6472017-11-09 16:29:26 -0800731
732 if (lStatus != NO_ERROR || output.outputId == AUDIO_IO_HANDLE_NONE) {
733 ALOGE("createTrack() getOutputForAttr() return error %d or invalid output handle", lStatus);
734 goto Exit;
735 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800736 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
737 // but if someone uses binder directly they could bypass that and cause us to crash
738 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000739 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740 lStatus = BAD_VALUE;
741 goto Exit;
742 }
743
Glenn Kasten53b5d092014-02-05 10:00:23 -0800744 // further channel mask checks are performed by createTrack_l() depending on the thread type
Eric Laurent21da6472017-11-09 16:29:26 -0800745 if (!audio_is_output_channel(input.config.channel_mask)) {
746 ALOGE("createTrack() invalid channel mask %#x", input.config.channel_mask);
Glenn Kasten53b5d092014-02-05 10:00:23 -0800747 lStatus = BAD_VALUE;
748 goto Exit;
749 }
750
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700751 // further format checks are performed by createTrack_l() depending on the thread type
Eric Laurent21da6472017-11-09 16:29:26 -0800752 if (!audio_is_valid_format(input.config.format)) {
753 ALOGE("createTrack() invalid format %#x", input.config.format);
Glenn Kasten663c2242013-09-24 11:52:37 -0700754 lStatus = BAD_VALUE;
755 goto Exit;
756 }
757
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758 {
759 Mutex::Autolock _l(mLock);
Eric Laurent21da6472017-11-09 16:29:26 -0800760 PlaybackThread *thread = checkPlaybackThread_l(output.outputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761 if (thread == NULL) {
Eric Laurent21da6472017-11-09 16:29:26 -0800762 ALOGE("no playback thread found for output handle %d", output.outputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763 lStatus = BAD_VALUE;
764 goto Exit;
765 }
766
Eric Laurent21da6472017-11-09 16:29:26 -0800767 client = registerPid(clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768
Glenn Kastene848bd92014-03-13 15:00:32 -0700769 PlaybackThread *effectThread = NULL;
Eric Laurent21da6472017-11-09 16:29:26 -0800770 // check if an effect chain with the same session ID is present on another
771 // output thread and move it here.
772 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
773 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
774 if (mPlaybackThreads.keyAt(i) != output.outputId) {
775 uint32_t sessions = t->hasAudioSession(sessionId);
776 if (sessions & ThreadBase::EFFECT_SESSION) {
777 effectThread = t.get();
778 break;
Eric Laurentde070132010-07-13 04:45:46 -0700779 }
780 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700781 }
Eric Laurent21da6472017-11-09 16:29:26 -0800782 ALOGV("createTrack() sessionId: %d", sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783
Eric Laurent21da6472017-11-09 16:29:26 -0800784 output.sampleRate = input.config.sample_rate;
785 output.frameCount = input.frameCount;
786 output.notificationFrameCount = input.notificationFrameCount;
787 output.flags = input.flags;
788
Eric Laurent42984412019-05-09 17:57:03 -0700789 track = thread->createTrack_l(client, streamType, localAttr, &output.sampleRate,
Kevin Rocard1f564ac2018-03-29 13:53:10 -0700790 input.config.format, input.config.channel_mask,
Eric Laurent21da6472017-11-09 16:29:26 -0800791 &output.frameCount, &output.notificationFrameCount,
792 input.notificationsPerBuffer, input.speed,
793 input.sharedBuffer, sessionId, &output.flags,
Eric Laurent09f1ed22019-04-24 17:45:17 -0700794 callingPid, input.clientInfo.clientTid, clientUid,
795 &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800796 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700797 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700798
Eric Laurent21da6472017-11-09 16:29:26 -0800799 output.afFrameCount = thread->frameCount();
800 output.afSampleRate = thread->sampleRate();
801 output.afLatencyMs = thread->latency();
Eric Laurent973db022018-11-20 14:54:31 -0800802 output.portId = portId;
Eric Laurent21da6472017-11-09 16:29:26 -0800803
Kevin Rocard153f92d2018-12-18 18:33:28 -0800804 if (lStatus == NO_ERROR) {
805 // Connect secondary outputs. Failure on a secondary output must not imped the primary
806 // Any secondary output setup failure will lead to a desync between the AP and AF until
807 // the track is destroyed.
808 TeePatches teePatches;
809 for (audio_io_handle_t secondaryOutput : secondaryOutputs) {
810 PlaybackThread *secondaryThread = checkPlaybackThread_l(secondaryOutput);
811 if (secondaryThread == NULL) {
812 ALOGE("no playback thread found for secondary output %d", output.outputId);
813 continue;
814 }
815
Kevin Rocard20a44f82019-09-18 11:24:52 +0100816 size_t sourceFrameCount = thread->frameCount() * output.sampleRate
817 / thread->sampleRate();
818 size_t sinkFrameCount = secondaryThread->frameCount() * output.sampleRate
819 / secondaryThread->sampleRate();
820 // If the secondary output has just been opened, the first secondaryThread write
821 // will not block as it will fill the empty startup buffer of the HAL,
822 // so a second sink buffer needs to be ready for the immediate next blocking write.
823 // Additionally, have a margin of one main thread buffer as the scheduling jitter
824 // can reorder the writes (eg if thread A&B have the same write intervale,
825 // the scheduler could schedule AB...BA)
826 size_t frameCountToBeReady = 2 * sinkFrameCount + sourceFrameCount;
827 // Total secondary output buffer must be at least as the read frames plus
828 // the margin of a few buffers on both sides in case the
829 // threads scheduling has some jitter.
830 // That value should not impact latency as the secondary track is started before
831 // its buffer is full, see frameCountToBeReady.
832 size_t frameCount = frameCountToBeReady + 2 * (sourceFrameCount + sinkFrameCount);
833 // The frameCount should also not be smaller than the secondary thread min frame
834 // count
835 size_t minFrameCount = AudioSystem::calculateMinFrameCount(
836 [&] { Mutex::Autolock _l(secondaryThread->mLock);
837 return secondaryThread->latency_l(); }(),
838 secondaryThread->mNormalFrameCount,
839 secondaryThread->mSampleRate,
840 output.sampleRate,
841 input.speed);
842 frameCount = std::max(frameCount, minFrameCount);
Kevin Rocard153f92d2018-12-18 18:33:28 -0800843
844 using namespace std::chrono_literals;
845 auto inChannelMask = audio_channel_mask_out_to_in(input.config.channel_mask);
846 sp patchRecord = new RecordThread::PatchRecord(nullptr /* thread */,
847 output.sampleRate,
848 inChannelMask,
849 input.config.format,
850 frameCount,
851 NULL /* buffer */,
852 (size_t)0 /* bufferSize */,
853 AUDIO_INPUT_FLAG_DIRECT,
854 0ns /* timeout */);
855 status_t status = patchRecord->initCheck();
856 if (status != NO_ERROR) {
857 ALOGE("Secondary output patchRecord init failed: %d", status);
858 continue;
859 }
Andy Hungae22b482019-05-09 15:38:55 -0700860
861 // TODO: We could check compatibility of the secondaryThread with the PatchTrack
862 // for fast usage: thread has fast mixer, sample rate matches, etc.;
863 // for now, we exclude fast tracks by removing the Fast flag.
864 const audio_output_flags_t outputFlags =
865 (audio_output_flags_t)(output.flags & ~AUDIO_OUTPUT_FLAG_FAST);
Kevin Rocard153f92d2018-12-18 18:33:28 -0800866 sp patchTrack = new PlaybackThread::PatchTrack(secondaryThread,
867 streamType,
868 output.sampleRate,
869 input.config.channel_mask,
870 input.config.format,
871 frameCount,
872 patchRecord->buffer(),
873 patchRecord->bufferSize(),
Andy Hungae22b482019-05-09 15:38:55 -0700874 outputFlags,
Kevin Rocard20a44f82019-09-18 11:24:52 +0100875 0ns /* timeout */,
876 frameCountToBeReady);
Kevin Rocard153f92d2018-12-18 18:33:28 -0800877 status = patchTrack->initCheck();
878 if (status != NO_ERROR) {
879 ALOGE("Secondary output patchTrack init failed: %d", status);
880 continue;
881 }
882 teePatches.push_back({patchRecord, patchTrack});
883 secondaryThread->addPatchTrack(patchTrack);
Andy Hungabfab202019-03-07 19:45:54 -0800884 // In case the downstream patchTrack on the secondaryThread temporarily outlives
885 // our created track, ensure the corresponding patchRecord is still alive.
886 patchTrack->setPeerProxy(patchRecord, true /* holdReference */);
887 patchRecord->setPeerProxy(patchTrack, false /* holdReference */);
Kevin Rocard153f92d2018-12-18 18:33:28 -0800888 }
889 track->setTeePatches(std::move(teePatches));
890 }
891
Eric Laurent39e94f82010-07-28 01:32:47 -0700892 // move effect chain to this output thread if an effect on same session was waiting
893 // for a track to be created
894 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700895 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700896 Mutex::Autolock _dl(thread->mLock);
897 Mutex::Autolock _sl(effectThread->mLock);
Eric Laurent6c796322019-04-09 14:13:17 -0700898 if (moveEffectChain_l(sessionId, effectThread, thread) == NO_ERROR) {
899 effectThreadId = thread->id();
900 effectIds = thread->getEffectIds_l(sessionId);
901 }
Eric Laurent39e94f82010-07-28 01:32:47 -0700902 }
Eric Laurenta011e352012-03-29 15:51:43 -0700903
904 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700905 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurent21da6472017-11-09 16:29:26 -0800906 if (mPendingSyncEvents[i]->triggerSession() == sessionId) {
Eric Laurenta011e352012-03-29 15:51:43 -0700907 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700908 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700909 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700910 } else {
911 mPendingSyncEvents[i]->cancel();
912 }
Eric Laurenta011e352012-03-29 15:51:43 -0700913 mPendingSyncEvents.removeAt(i);
914 i--;
915 }
916 }
917 }
Glenn Kastene198c362013-08-13 09:13:36 -0700918
Eric Laurent21da6472017-11-09 16:29:26 -0800919 setAudioHwSyncForSession_l(thread, sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920 }
Glenn Kastene198c362013-08-13 09:13:36 -0700921
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700922 if (lStatus != NO_ERROR) {
923 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700924 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700925 // Don't hold mClientLock when releasing the reference on the track as the
926 // destructor will acquire it.
927 {
928 Mutex::Autolock _cl(mClientLock);
929 client.clear();
930 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700932 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933 }
934
Eric Laurent6c796322019-04-09 14:13:17 -0700935 // effectThreadId is not NONE if an effect chain corresponding to the track session
936 // was found on another thread and must be moved on this thread
937 if (effectThreadId != AUDIO_IO_HANDLE_NONE) {
938 AudioSystem::moveEffectsToIo(effectIds, effectThreadId);
939 }
940
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700941 // return handle to client
942 trackHandle = new TrackHandle(track);
943
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944Exit:
Eric Laurent21da6472017-11-09 16:29:26 -0800945 if (lStatus != NO_ERROR && output.outputId != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd7fe0862018-07-14 16:48:01 -0700946 AudioSystem::releaseOutput(portId);
Eric Laurent21da6472017-11-09 16:29:26 -0800947 }
Glenn Kasten9156ef32013-08-06 15:39:08 -0700948 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949 return trackHandle;
950}
951
Glenn Kasten2c073da2016-02-26 09:14:08 -0800952uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953{
954 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800955 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800957 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 return 0;
959 }
960 return thread->sampleRate();
961}
962
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800963audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964{
965 Mutex::Autolock _l(mLock);
966 PlaybackThread *thread = checkPlaybackThread_l(output);
967 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000968 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800969 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970 }
971 return thread->format();
972}
973
Glenn Kasten2c073da2016-02-26 09:14:08 -0800974size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975{
976 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800977 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700978 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800979 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700980 return 0;
981 }
Glenn Kasten58912562012-04-03 10:45:00 -0700982 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
983 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984 return thread->frameCount();
985}
986
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700987size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
988{
989 Mutex::Autolock _l(mLock);
990 ThreadBase *thread = checkThread_l(ioHandle);
991 if (thread == NULL) {
992 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
993 return 0;
994 }
995 return thread->frameCountHAL();
996}
997
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800998uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999{
1000 Mutex::Autolock _l(mLock);
1001 PlaybackThread *thread = checkPlaybackThread_l(output);
1002 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -08001003 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004 return 0;
1005 }
1006 return thread->latency();
1007}
1008
1009status_t AudioFlinger::setMasterVolume(float value)
1010{
Eric Laurenta1884f92011-08-23 08:25:03 -07001011 status_t ret = initCheck();
1012 if (ret != NO_ERROR) {
1013 return ret;
1014 }
1015
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016 // check calling permissions
1017 if (!settingsAllowed()) {
1018 return PERMISSION_DENIED;
1019 }
1020
Eric Laurenta4c5a552012-03-29 10:12:40 -07001021 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -07001022 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001023
John Grossmanee578c02012-07-23 17:05:46 -07001024 // Set master volume in the HALs which support it.
1025 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1026 AutoMutex lock(mHardwareLock);
1027 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -08001028
John Grossmanee578c02012-07-23 17:05:46 -07001029 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
1030 if (dev->canSetMasterVolume()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001031 dev->hwDevice()->setMasterVolume(value);
Eric Laurent93575202011-01-18 18:39:02 -08001032 }
John Grossmanee578c02012-07-23 17:05:46 -07001033 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001035
John Grossmanee578c02012-07-23 17:05:46 -07001036 // Now set the master volume in each playback thread. Playback threads
1037 // assigned to HALs which do not have master volume support will apply
1038 // master volume during the mix operation. Threads with HALs which do
1039 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -07001040 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1041 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
1042 continue;
1043 }
John Grossmanee578c02012-07-23 17:05:46 -07001044 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -07001045 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046
1047 return NO_ERROR;
1048}
1049
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001050status_t AudioFlinger::setMasterBalance(float balance)
1051{
1052 status_t ret = initCheck();
1053 if (ret != NO_ERROR) {
1054 return ret;
1055 }
1056
1057 // check calling permissions
1058 if (!settingsAllowed()) {
1059 return PERMISSION_DENIED;
1060 }
1061
1062 // check range
1063 if (isnan(balance) || fabs(balance) > 1.f) {
1064 return BAD_VALUE;
1065 }
1066
1067 Mutex::Autolock _l(mLock);
1068
1069 // short cut.
1070 if (mMasterBalance == balance) return NO_ERROR;
1071
1072 mMasterBalance = balance;
1073
1074 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1075 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
1076 continue;
1077 }
1078 mPlaybackThreads.valueAt(i)->setMasterBalance(balance);
1079 }
1080
1081 return NO_ERROR;
1082}
1083
Glenn Kastenf78aee72012-01-04 11:00:47 -08001084status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001085{
Eric Laurenta1884f92011-08-23 08:25:03 -07001086 status_t ret = initCheck();
1087 if (ret != NO_ERROR) {
1088 return ret;
1089 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090
1091 // check calling permissions
1092 if (!settingsAllowed()) {
1093 return PERMISSION_DENIED;
1094 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -08001095 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001096 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001097 return BAD_VALUE;
1098 }
1099
1100 { // scope for the lock
1101 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001102 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001103 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001104 ret = dev->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001105 mHardwareStatus = AUDIO_HW_IDLE;
1106 }
1107
1108 if (NO_ERROR == ret) {
1109 Mutex::Autolock _l(mLock);
1110 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001111 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001112 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001113 }
1114
1115 return ret;
1116}
1117
1118status_t AudioFlinger::setMicMute(bool state)
1119{
Eric Laurenta1884f92011-08-23 08:25:03 -07001120 status_t ret = initCheck();
1121 if (ret != NO_ERROR) {
1122 return ret;
1123 }
1124
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125 // check calling permissions
1126 if (!settingsAllowed()) {
1127 return PERMISSION_DENIED;
1128 }
1129
1130 AutoMutex lock(mHardwareLock);
1131 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -07001132 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001133 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1134 status_t result = dev->setMicMute(state);
Eric Laurent2f035f52014-09-14 12:11:52 -07001135 if (result != NO_ERROR) {
1136 ret = result;
1137 }
1138 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139 mHardwareStatus = AUDIO_HW_IDLE;
1140 return ret;
1141}
1142
1143bool AudioFlinger::getMicMute() const
1144{
Eric Laurenta1884f92011-08-23 08:25:03 -07001145 status_t ret = initCheck();
1146 if (ret != NO_ERROR) {
1147 return false;
1148 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -08001149 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -07001150 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001151 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -08001153 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001154 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1155 status_t result = dev->getMicMute(&state);
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -08001156 if (result == NO_ERROR) {
1157 mute = mute && state;
1158 }
1159 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001160 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -08001161
1162 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163}
1164
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001165void AudioFlinger::setRecordSilenced(uid_t uid, bool silenced)
1166{
1167 ALOGV("AudioFlinger::setRecordSilenced(uid:%d, silenced:%d)", uid, silenced);
1168
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001169 AutoMutex lock(mLock);
1170 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent331679c2018-04-16 17:03:16 -07001171 mRecordThreads[i]->setRecordSilenced(uid, silenced);
1172 }
1173 for (size_t i = 0; i < mMmapThreads.size(); i++) {
1174 mMmapThreads[i]->setRecordSilenced(uid, silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001175 }
1176}
1177
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178status_t AudioFlinger::setMasterMute(bool muted)
1179{
John Grossmand8f178d2012-07-20 14:51:35 -07001180 status_t ret = initCheck();
1181 if (ret != NO_ERROR) {
1182 return ret;
1183 }
1184
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185 // check calling permissions
1186 if (!settingsAllowed()) {
1187 return PERMISSION_DENIED;
1188 }
1189
John Grossmanee578c02012-07-23 17:05:46 -07001190 Mutex::Autolock _l(mLock);
1191 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -07001192
John Grossmanee578c02012-07-23 17:05:46 -07001193 // Set master mute in the HALs which support it.
1194 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1195 AutoMutex lock(mHardwareLock);
1196 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -07001197
John Grossmanee578c02012-07-23 17:05:46 -07001198 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1199 if (dev->canSetMasterMute()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001200 dev->hwDevice()->setMasterMute(muted);
John Grossmand8f178d2012-07-20 14:51:35 -07001201 }
John Grossmanee578c02012-07-23 17:05:46 -07001202 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -07001203 }
1204
John Grossmanee578c02012-07-23 17:05:46 -07001205 // Now set the master mute in each playback thread. Playback threads
1206 // assigned to HALs which do not have master mute support will apply master
1207 // mute during the mix operation. Threads with HALs which do support master
1208 // mute will simply ignore the setting.
Eric Laurent6acd1d42017-01-04 14:23:29 -08001209 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1210 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1211 volumeInterfaces[i]->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -07001212 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001213
1214 return NO_ERROR;
1215}
1216
1217float AudioFlinger::masterVolume() const
1218{
Glenn Kasten98067102011-12-13 11:47:54 -08001219 Mutex::Autolock _l(mLock);
1220 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001221}
1222
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001223status_t AudioFlinger::getMasterBalance(float *balance) const
1224{
1225 Mutex::Autolock _l(mLock);
1226 *balance = getMasterBalance_l();
1227 return NO_ERROR; // if called through binder, may return a transactional error
1228}
1229
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230bool AudioFlinger::masterMute() const
1231{
Glenn Kasten98067102011-12-13 11:47:54 -08001232 Mutex::Autolock _l(mLock);
1233 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001234}
1235
John Grossman4ff14ba2012-02-08 16:37:41 -08001236float AudioFlinger::masterVolume_l() const
1237{
John Grossman4ff14ba2012-02-08 16:37:41 -08001238 return mMasterVolume;
1239}
1240
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001241float AudioFlinger::getMasterBalance_l() const
1242{
1243 return mMasterBalance;
1244}
1245
John Grossmand8f178d2012-07-20 14:51:35 -07001246bool AudioFlinger::masterMute_l() const
1247{
John Grossmanee578c02012-07-23 17:05:46 -07001248 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -07001249}
1250
Eric Laurent223fd5c2014-11-11 13:43:36 -08001251status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
1252{
1253 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001254 ALOGW("checkStreamType() invalid stream %d", stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001255 return BAD_VALUE;
1256 }
Andy Hung4ef19fa2018-05-15 19:35:29 -07001257 const uid_t callerUid = IPCThreadState::self()->getCallingUid();
1258 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && !isAudioServerUid(callerUid)) {
1259 ALOGW("checkStreamType() uid %d cannot use internal stream type %d", callerUid, stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001260 return PERMISSION_DENIED;
1261 }
1262
1263 return NO_ERROR;
1264}
1265
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001266status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
1267 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001268{
1269 // check calling permissions
1270 if (!settingsAllowed()) {
1271 return PERMISSION_DENIED;
1272 }
1273
Eric Laurent223fd5c2014-11-11 13:43:36 -08001274 status_t status = checkStreamType(stream);
1275 if (status != NO_ERROR) {
1276 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277 }
Eric Laurent98e38192018-02-15 18:31:53 -08001278 if (output == AUDIO_IO_HANDLE_NONE) {
1279 return BAD_VALUE;
1280 }
François Gaffie9e1533c2019-04-11 16:07:36 +02001281 LOG_ALWAYS_FATAL_IF(stream == AUDIO_STREAM_PATCH && value != 1.0f,
1282 "AUDIO_STREAM_PATCH must have full scale volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001283
1284 AutoMutex lock(mLock);
Eric Laurent98e38192018-02-15 18:31:53 -08001285 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1286 if (volumeInterface == NULL) {
1287 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 }
Eric Laurent98e38192018-02-15 18:31:53 -08001289 volumeInterface->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001290
1291 return NO_ERROR;
1292}
1293
Glenn Kastenfff6d712012-01-12 16:38:12 -08001294status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001295{
1296 // check calling permissions
1297 if (!settingsAllowed()) {
1298 return PERMISSION_DENIED;
1299 }
1300
Eric Laurent223fd5c2014-11-11 13:43:36 -08001301 status_t status = checkStreamType(stream);
1302 if (status != NO_ERROR) {
1303 return status;
1304 }
1305 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1306
1307 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001308 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001309 return BAD_VALUE;
1310 }
1311
Eric Laurent93575202011-01-18 18:39:02 -08001312 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001313 mStreamTypes[stream].mute = muted;
Eric Laurent6acd1d42017-01-04 14:23:29 -08001314 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1315 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1316 volumeInterfaces[i]->setStreamMute(stream, muted);
1317 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001318
1319 return NO_ERROR;
1320}
1321
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001322float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001324 status_t status = checkStreamType(stream);
1325 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001326 return 0.0f;
1327 }
Eric Laurent98e38192018-02-15 18:31:53 -08001328 if (output == AUDIO_IO_HANDLE_NONE) {
1329 return 0.0f;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001330 }
1331
Eric Laurent98e38192018-02-15 18:31:53 -08001332 AutoMutex lock(mLock);
1333 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1334 if (volumeInterface == NULL) {
1335 return 0.0f;
1336 }
1337
1338 return volumeInterface->streamVolume(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001339}
1340
Glenn Kastenfff6d712012-01-12 16:38:12 -08001341bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001342{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001343 status_t status = checkStreamType(stream);
1344 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001345 return true;
1346 }
1347
Glenn Kasten6637baa2012-01-09 09:40:36 -08001348 AutoMutex lock(mLock);
1349 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350}
1351
Eric Laurent054d9d32015-04-24 08:48:48 -07001352
1353void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1354{
1355 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1356 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1357 }
1358}
1359
jiabin10d86fd2019-10-31 17:20:42 -07001360void AudioFlinger::updateOutDevicesForRecordThreads_l(const DeviceDescriptorBaseVector& devices)
1361{
1362 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1363 mRecordThreads.valueAt(i)->updateOutDevices(devices);
1364 }
1365}
1366
Mikhail Naganovb261ef52018-07-16 13:34:38 -07001367// forwardAudioHwSyncToDownstreamPatches_l() must be called with AudioFlinger::mLock held
1368void AudioFlinger::forwardParametersToDownstreamPatches_l(
1369 audio_io_handle_t upStream, const String8& keyValuePairs,
1370 std::function<bool(const sp<PlaybackThread>&)> useThread)
1371{
1372 std::vector<PatchPanel::SoftwarePatch> swPatches;
1373 if (mPatchPanel.getDownstreamSoftwarePatches(upStream, &swPatches) != OK) return;
1374 ALOGV_IF(!swPatches.empty(), "%s found %zu downstream patches for stream ID %d",
1375 __func__, swPatches.size(), upStream);
1376 for (const auto& swPatch : swPatches) {
1377 sp<PlaybackThread> downStream = checkPlaybackThread_l(swPatch.getPlaybackThreadHandle());
1378 if (downStream != NULL && (useThread == nullptr || useThread(downStream))) {
1379 downStream->setParameters(keyValuePairs);
1380 }
1381 }
1382}
1383
Eric Laurentf1047e82018-04-16 19:18:20 -07001384// Filter reserved keys from setParameters() before forwarding to audio HAL or acting upon.
1385// Some keys are used for audio routing and audio path configuration and should be reserved for use
1386// by audio policy and audio flinger for functional, privacy and security reasons.
1387void AudioFlinger::filterReservedParameters(String8& keyValuePairs, uid_t callingUid)
1388{
1389 static const String8 kReservedParameters[] = {
1390 String8(AudioParameter::keyRouting),
1391 String8(AudioParameter::keySamplingRate),
1392 String8(AudioParameter::keyFormat),
1393 String8(AudioParameter::keyChannels),
1394 String8(AudioParameter::keyFrameCount),
1395 String8(AudioParameter::keyInputSource),
1396 String8(AudioParameter::keyMonoOutput),
Mikhail Naganovbb3b1602019-07-08 15:28:43 -07001397 String8(AudioParameter::keyDeviceConnect),
1398 String8(AudioParameter::keyDeviceDisconnect),
Eric Laurentf1047e82018-04-16 19:18:20 -07001399 String8(AudioParameter::keyStreamSupportedFormats),
1400 String8(AudioParameter::keyStreamSupportedChannels),
1401 String8(AudioParameter::keyStreamSupportedSamplingRates),
1402 };
1403
Andy Hung4ef19fa2018-05-15 19:35:29 -07001404 if (isAudioServerUid(callingUid)) {
1405 return; // no need to filter if audioserver.
Eric Laurentf1047e82018-04-16 19:18:20 -07001406 }
1407
1408 AudioParameter param = AudioParameter(keyValuePairs);
1409 String8 value;
Kevin Rocarda0a5d2a2018-08-06 15:03:18 -07001410 AudioParameter rejectedParam;
Eric Laurentf1047e82018-04-16 19:18:20 -07001411 for (auto& key : kReservedParameters) {
1412 if (param.get(key, value) == NO_ERROR) {
Kevin Rocarda0a5d2a2018-08-06 15:03:18 -07001413 rejectedParam.add(key, value);
Eric Laurentf1047e82018-04-16 19:18:20 -07001414 param.remove(key);
1415 }
1416 }
Kevin Rocarda0a5d2a2018-08-06 15:03:18 -07001417 logFilteredParameters(param.size() + rejectedParam.size(), keyValuePairs,
1418 rejectedParam.size(), rejectedParam.toString(), callingUid);
Eric Laurentf1047e82018-04-16 19:18:20 -07001419 keyValuePairs = param.toString();
1420}
1421
Kevin Rocarda0a5d2a2018-08-06 15:03:18 -07001422void AudioFlinger::logFilteredParameters(size_t originalKVPSize, const String8& originalKVPs,
1423 size_t rejectedKVPSize, const String8& rejectedKVPs,
1424 uid_t callingUid) {
1425 auto prefix = String8::format("UID %5d", callingUid);
1426 auto suffix = String8::format("%zu KVP received: %s", originalKVPSize, originalKVPs.c_str());
1427 if (rejectedKVPSize != 0) {
1428 auto error = String8::format("%zu KVP rejected: %s", rejectedKVPSize, rejectedKVPs.c_str());
1429 ALOGW("%s: %s, %s, %s", __func__, prefix.c_str(), error.c_str(), suffix.c_str());
1430 mRejectedSetParameterLog.log("%s, %s, %s", prefix.c_str(), error.c_str(), suffix.c_str());
1431 } else {
1432 auto& logger = (isServiceUid(callingUid) ? mSystemSetParameterLog : mAppSetParameterLog);
1433 logger.log("%s, %s", prefix.c_str(), suffix.c_str());
1434 }
1435}
1436
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001437status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001438{
Eric Laurentf1047e82018-04-16 19:18:20 -07001439 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d calling uid %d",
1440 ioHandle, keyValuePairs.string(),
1441 IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid());
Eric Laurent81784c32012-11-19 14:55:58 -08001442
Mathias Agopian65ab4712010-07-14 17:59:35 -07001443 // check calling permissions
1444 if (!settingsAllowed()) {
1445 return PERMISSION_DENIED;
1446 }
1447
Eric Laurentf1047e82018-04-16 19:18:20 -07001448 String8 filteredKeyValuePairs = keyValuePairs;
1449 filterReservedParameters(filteredKeyValuePairs, IPCThreadState::self()->getCallingUid());
1450
1451 ALOGV("%s: filtered keyvalue %s", __func__, filteredKeyValuePairs.string());
1452
Glenn Kasten142f5192014-03-25 17:44:59 -07001453 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1454 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001455 Mutex::Autolock _l(mLock);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001456 // result will remain NO_INIT if no audio device is present
1457 status_t final_result = NO_INIT;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001458 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001459 AutoMutex lock(mHardwareLock);
1460 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1461 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001462 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
Eric Laurentf1047e82018-04-16 19:18:20 -07001463 status_t result = dev->setParameters(filteredKeyValuePairs);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001464 // return success if at least one audio device accepts the parameters as not all
1465 // HALs are requested to support all parameters. If no audio device supports the
1466 // requested parameters, the last error is reported.
1467 if (final_result != NO_ERROR) {
1468 final_result = result;
1469 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001470 }
1471 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001472 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001473 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
Eric Laurentf1047e82018-04-16 19:18:20 -07001474 AudioParameter param = AudioParameter(filteredKeyValuePairs);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001475 String8 value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001476 if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) {
1477 bool btNrecIsOff = (value == AudioParameter::valueOff);
Eric Laurentd8365c52017-07-16 15:27:05 -07001478 if (mBtNrecIsOff.exchange(btNrecIsOff) != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001479 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentd8365c52017-07-16 15:27:05 -07001480 mRecordThreads.valueAt(i)->checkBtNrec();
Eric Laurent59bd0da2011-08-01 09:52:20 -07001481 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001482 }
1483 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001484 String8 screenState;
1485 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
Mikhail Naganov00260b52016-10-13 12:54:24 -07001486 bool isOff = (screenState == AudioParameter::valueOff);
Eric Laurent81784c32012-11-19 14:55:58 -08001487 if (isOff != (AudioFlinger::mScreenState & 1)) {
1488 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001489 }
1490 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001491 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492 }
1493
1494 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1495 // and the thread is exited once the lock is released
1496 sp<ThreadBase> thread;
1497 {
1498 Mutex::Autolock _l(mLock);
1499 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001500 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001501 thread = checkRecordThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001502 if (thread == 0) {
1503 thread = checkMmapThread_l(ioHandle);
1504 }
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001505 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001506 // indicate output device change to all input threads for pre processing
Eric Laurentf1047e82018-04-16 19:18:20 -07001507 AudioParameter param = AudioParameter(filteredKeyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001508 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001509 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1510 (value != 0)) {
Eric Laurentf1047e82018-04-16 19:18:20 -07001511 broacastParametersToRecordThreads_l(filteredKeyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001512 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001513 }
1514 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001515 if (thread != 0) {
Mikhail Naganovb261ef52018-07-16 13:34:38 -07001516 status_t result = thread->setParameters(filteredKeyValuePairs);
1517 forwardParametersToDownstreamPatches_l(thread->id(), filteredKeyValuePairs);
1518 return result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519 }
1520 return BAD_VALUE;
1521}
1522
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001523String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001524{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001525 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1526 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527
Eric Laurenta4c5a552012-03-29 10:12:40 -07001528 Mutex::Autolock _l(mLock);
1529
Glenn Kasten142f5192014-03-25 17:44:59 -07001530 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001531 String8 out_s8;
1532
Dima Zavin799a70e2011-04-18 16:57:27 -07001533 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001534 String8 s;
1535 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001536 {
1537 AutoMutex lock(mHardwareLock);
1538 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001539 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1540 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001541 mHardwareStatus = AUDIO_HW_IDLE;
1542 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001543 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001544 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001545 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001546 }
1547
Eric Laurent6acd1d42017-01-04 14:23:29 -08001548 ThreadBase *thread = (ThreadBase *)checkPlaybackThread_l(ioHandle);
1549 if (thread == NULL) {
1550 thread = (ThreadBase *)checkRecordThread_l(ioHandle);
1551 if (thread == NULL) {
1552 thread = (ThreadBase *)checkMmapThread_l(ioHandle);
1553 if (thread == NULL) {
Mikhail Naganovaa165e52017-07-12 10:39:16 -07001554 return String8("");
Eric Laurent6acd1d42017-01-04 14:23:29 -08001555 }
1556 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001557 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001558 return thread->getParameters(keys);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001559}
1560
Glenn Kastendd8104c2012-07-02 12:42:44 -07001561size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1562 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563{
Eric Laurenta1884f92011-08-23 08:25:03 -07001564 status_t ret = initCheck();
1565 if (ret != NO_ERROR) {
1566 return 0;
1567 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001568 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001569 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001570 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001571 return 0;
1572 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001573
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001574 AutoMutex lock(mHardwareLock);
1575 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001576 audio_config_t config, proposed;
1577 memset(&proposed, 0, sizeof(proposed));
1578 proposed.sample_rate = sampleRate;
1579 proposed.channel_mask = channelMask;
1580 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001581
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001582 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Tao Wu1eb82df2019-11-05 10:20:18 -08001583 size_t frames = 0;
Andy Hung6770c6f2015-04-07 13:43:36 -07001584 for (;;) {
1585 // Note: config is currently a const parameter for get_input_buffer_size()
1586 // but we use a copy from proposed in case config changes from the call.
1587 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001588 status_t result = dev->getInputBufferSize(&config, &frames);
1589 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001590 break; // hal success, config is the result
1591 }
1592 // change one parameter of the configuration each iteration to a more "common" value
1593 // to see if the device will support it.
1594 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1595 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1596 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1597 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1598 } else {
1599 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1600 "format %#x, channelMask 0x%X",
1601 sampleRate, format, channelMask);
1602 break; // retries failed, break out of loop with frames == 0.
1603 }
1604 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001605 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001606 if (frames > 0 && config.sample_rate != sampleRate) {
1607 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1608 }
1609 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001610}
1611
Glenn Kasten5f972c02014-01-13 09:59:31 -08001612uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001613{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614 Mutex::Autolock _l(mLock);
1615
1616 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1617 if (recordThread != NULL) {
1618 return recordThread->getInputFramesLost();
1619 }
1620 return 0;
1621}
1622
1623status_t AudioFlinger::setVoiceVolume(float value)
1624{
Eric Laurenta1884f92011-08-23 08:25:03 -07001625 status_t ret = initCheck();
1626 if (ret != NO_ERROR) {
1627 return ret;
1628 }
1629
Mathias Agopian65ab4712010-07-14 17:59:35 -07001630 // check calling permissions
1631 if (!settingsAllowed()) {
1632 return PERMISSION_DENIED;
1633 }
1634
1635 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001636 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001637 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001638 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001639 mHardwareStatus = AUDIO_HW_IDLE;
1640
1641 return ret;
1642}
1643
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001644status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001645 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001646{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001647 Mutex::Autolock _l(mLock);
1648
1649 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1650 if (playbackThread != NULL) {
1651 return playbackThread->getRenderPosition(halFrames, dspFrames);
1652 }
1653
1654 return BAD_VALUE;
1655}
1656
1657void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1658{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001659 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001660 if (client == 0) {
1661 return;
1662 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001663 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001664 {
1665 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001666 if (mNotificationClients.indexOfKey(pid) < 0) {
1667 sp<NotificationClient> notificationClient = new NotificationClient(this,
1668 client,
1669 pid);
1670 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001671
Eric Laurent021cf962014-05-13 10:18:14 -07001672 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001673
Marco Nelissen06b46062014-11-14 07:58:25 -08001674 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001675 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001676 }
1677 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001678
Eric Laurent021cf962014-05-13 10:18:14 -07001679 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001680 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001681 // the config change is always sent from playback or record threads to avoid deadlock
1682 // with AudioSystem::gLock
1683 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001684 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_REGISTERED, pid);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001685 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001686
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001687 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001688 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_REGISTERED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001689 }
1690}
1691
1692void AudioFlinger::removeNotificationClient(pid_t pid)
1693{
Eric Laurent6c796322019-04-09 14:13:17 -07001694 std::vector< sp<AudioFlinger::EffectModule> > removedEffects;
Eric Laurent021cf962014-05-13 10:18:14 -07001695 {
Eric Laurent6c796322019-04-09 14:13:17 -07001696 Mutex::Autolock _l(mLock);
1697 {
1698 Mutex::Autolock _cl(mClientLock);
1699 mNotificationClients.removeItem(pid);
1700 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001701
Eric Laurent6c796322019-04-09 14:13:17 -07001702 ALOGV("%d died, releasing its sessions", pid);
1703 size_t num = mAudioSessionRefs.size();
1704 bool removed = false;
1705 for (size_t i = 0; i < num; ) {
1706 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
1707 ALOGV(" pid %d @ %zu", ref->mPid, i);
1708 if (ref->mPid == pid) {
1709 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
1710 mAudioSessionRefs.removeAt(i);
1711 delete ref;
1712 removed = true;
1713 num--;
1714 } else {
1715 i++;
1716 }
1717 }
1718 if (removed) {
1719 removedEffects = purgeStaleEffects_l();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001720 }
1721 }
Eric Laurent6c796322019-04-09 14:13:17 -07001722 for (auto& effect : removedEffects) {
1723 effect->updatePolicyState();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001724 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001725}
1726
Eric Laurent73e26b62015-04-27 16:55:58 -07001727void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001728 const sp<AudioIoDescriptor>& ioDesc,
1729 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001730{
Eric Laurent021cf962014-05-13 10:18:14 -07001731 Mutex::Autolock _l(mClientLock);
1732 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001733 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001734 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1735 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1736 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001737 }
1738}
1739
Eric Laurent021cf962014-05-13 10:18:14 -07001740// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001741void AudioFlinger::removeClient_l(pid_t pid)
1742{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001743 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001744 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001745 mClients.removeItem(pid);
1746}
1747
Eric Laurent717e1282012-06-29 16:36:52 -07001748// getEffectThread_l() must be called with AudioFlinger::mLock held
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001749sp<AudioFlinger::ThreadBase> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1750 int effectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001751{
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001752 sp<ThreadBase> thread;
Eric Laurent717e1282012-06-29 16:36:52 -07001753
1754 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001755 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, effectId) != 0) {
Eric Laurent717e1282012-06-29 16:36:52 -07001756 ALOG_ASSERT(thread == 0);
1757 thread = mPlaybackThreads.valueAt(i);
1758 }
1759 }
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001760 if (thread != nullptr) {
1761 return thread;
1762 }
1763 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1764 if (mRecordThreads.valueAt(i)->getEffect(sessionId, effectId) != 0) {
1765 ALOG_ASSERT(thread == 0);
1766 thread = mRecordThreads.valueAt(i);
1767 }
1768 }
1769 if (thread != nullptr) {
1770 return thread;
1771 }
1772 for (size_t i = 0; i < mMmapThreads.size(); i++) {
1773 if (mMmapThreads.valueAt(i)->getEffect(sessionId, effectId) != 0) {
1774 ALOG_ASSERT(thread == 0);
1775 thread = mMmapThreads.valueAt(i);
1776 }
1777 }
Eric Laurent717e1282012-06-29 16:36:52 -07001778 return thread;
1779}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001780
Mathias Agopian65ab4712010-07-14 17:59:35 -07001781
Mathias Agopian65ab4712010-07-14 17:59:35 -07001782
1783// ----------------------------------------------------------------------------
1784
1785AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1786 : RefBase(),
1787 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001788 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001789{
Andy Hung6f248bb2018-01-23 14:04:37 -08001790 mMemoryDealer = new MemoryDealer(
1791 audioFlinger->getClientSharedHeapSize(),
1792 (std::string("AudioFlinger::Client(") + std::to_string(pid) + ")").c_str());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001793}
1794
Eric Laurent021cf962014-05-13 10:18:14 -07001795// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001796AudioFlinger::Client::~Client()
1797{
1798 mAudioFlinger->removeClient_l(mPid);
1799}
1800
Glenn Kasten435dbe62012-01-30 10:15:48 -08001801sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001802{
1803 return mMemoryDealer;
1804}
1805
1806// ----------------------------------------------------------------------------
1807
1808AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1809 const sp<IAudioFlingerClient>& client,
1810 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001811 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001812{
1813}
1814
1815AudioFlinger::NotificationClient::~NotificationClient()
1816{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001817}
1818
Glenn Kasten0f11b512014-01-31 16:18:54 -08001819void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001820{
1821 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001822 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001823}
1824
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001825// ----------------------------------------------------------------------------
1826AudioFlinger::MediaLogNotifier::MediaLogNotifier()
1827 : mPendingRequests(false) {}
1828
1829
1830void AudioFlinger::MediaLogNotifier::requestMerge() {
1831 AutoMutex _l(mMutex);
1832 mPendingRequests = true;
1833 mCond.signal();
1834}
1835
1836bool AudioFlinger::MediaLogNotifier::threadLoop() {
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001837 // Should already have been checked, but just in case
1838 if (sMediaLogService == 0) {
1839 return false;
1840 }
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001841 // Wait until there are pending requests
1842 {
1843 AutoMutex _l(mMutex);
1844 mPendingRequests = false; // to ignore past requests
1845 while (!mPendingRequests) {
1846 mCond.wait(mMutex);
1847 // TODO may also need an exitPending check
1848 }
1849 mPendingRequests = false;
1850 }
1851 // Execute the actual MediaLogService binder call and ignore extra requests for a while
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001852 sMediaLogService->requestMergeWakeup();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001853 usleep(kPostTriggerSleepPeriod);
1854 return true;
1855}
1856
1857void AudioFlinger::requestLogMerge() {
1858 mMediaLogNotifier->requestMerge();
1859}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001860
1861// ----------------------------------------------------------------------------
1862
Eric Laurentf14db3c2017-12-08 14:20:36 -08001863sp<media::IAudioRecord> AudioFlinger::createRecord(const CreateRecordInput& input,
1864 CreateRecordOutput& output,
1865 status_t *status)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001866{
1867 sp<RecordThread::RecordTrack> recordTrack;
1868 sp<RecordHandle> recordHandle;
1869 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001870 status_t lStatus;
Eric Laurentf14db3c2017-12-08 14:20:36 -08001871 audio_session_t sessionId = input.sessionId;
Eric Laurent77881cd2018-02-09 16:01:31 -08001872 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001873
Eric Laurentf14db3c2017-12-08 14:20:36 -08001874 output.cblk.clear();
1875 output.buffers.clear();
Eric Laurent896c9672017-12-08 14:08:45 -08001876 output.inputId = AUDIO_IO_HANDLE_NONE;
Glenn Kastend776ac62014-05-07 09:16:09 -07001877
Eric Laurentf14db3c2017-12-08 14:20:36 -08001878 bool updatePid = (input.clientInfo.clientPid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001879 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurentf14db3c2017-12-08 14:20:36 -08001880 uid_t clientUid = input.clientInfo.clientUid;
Andy Hung4ef19fa2018-05-15 19:35:29 -07001881 if (!isAudioServerOrMediaServerUid(callingUid)) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001882 ALOGW_IF(clientUid != callingUid,
1883 "%s uid %d tried to pass itself off as %d",
1884 __FUNCTION__, callingUid, clientUid);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001885 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001886 updatePid = true;
1887 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001888 pid_t clientPid = input.clientInfo.clientPid;
Eric Laurent09f1ed22019-04-24 17:45:17 -07001889 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001890 if (updatePid) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001891 ALOGW_IF(clientPid != -1 && clientPid != callingPid,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001892 "%s uid %d pid %d tried to pass itself off as pid %d",
Eric Laurentf14db3c2017-12-08 14:20:36 -08001893 __func__, callingUid, callingPid, clientPid);
1894 clientPid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001895 }
1896
Andy Hung6770c6f2015-04-07 13:43:36 -07001897 // we don't yet support anything other than linear PCM
Eric Laurentf14db3c2017-12-08 14:20:36 -08001898 if (!audio_is_valid_format(input.config.format) || !audio_is_linear_pcm(input.config.format)) {
1899 ALOGE("createRecord() invalid format %#x", input.config.format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001900 lStatus = BAD_VALUE;
1901 goto Exit;
1902 }
1903
Glenn Kasten53b5d092014-02-05 10:00:23 -08001904 // further channel mask checks are performed by createRecordTrack_l()
Eric Laurentf14db3c2017-12-08 14:20:36 -08001905 if (!audio_is_input_channel(input.config.channel_mask)) {
1906 ALOGE("createRecord() invalid channel mask %#x", input.config.channel_mask);
Glenn Kasten53b5d092014-02-05 10:00:23 -08001907 lStatus = BAD_VALUE;
1908 goto Exit;
1909 }
1910
Eric Laurentf14db3c2017-12-08 14:20:36 -08001911 if (sessionId == AUDIO_SESSION_ALLOCATE) {
1912 sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
1913 } else if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1914 lStatus = BAD_VALUE;
1915 goto Exit;
1916 }
1917
1918 output.sessionId = sessionId;
Eric Laurentf14db3c2017-12-08 14:20:36 -08001919 output.selectedDeviceId = input.selectedDeviceId;
1920 output.flags = input.flags;
1921
1922 client = registerPid(clientPid);
1923
1924 // Not a conventional loop, but a retry loop for at most two iterations total.
1925 // Try first maybe with FAST flag then try again without FAST flag if that fails.
1926 // Exits loop via break on no error of got exit on error
1927 // The sp<> references will be dropped when re-entering scope.
1928 // The lack of indentation is deliberate, to reduce code churn and ease merges.
1929 for (;;) {
Eric Laurent896c9672017-12-08 14:08:45 -08001930 // release previously opened input if retrying.
1931 if (output.inputId != AUDIO_IO_HANDLE_NONE) {
1932 recordTrack.clear();
Eric Laurentfee19762018-01-29 18:44:13 -08001933 AudioSystem::releaseInput(portId);
Eric Laurent896c9672017-12-08 14:08:45 -08001934 output.inputId = AUDIO_IO_HANDLE_NONE;
Yung Ti Su5fac9c62018-05-15 15:07:43 +08001935 output.selectedDeviceId = input.selectedDeviceId;
Eric Laurent77881cd2018-02-09 16:01:31 -08001936 portId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent896c9672017-12-08 14:08:45 -08001937 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001938 lStatus = AudioSystem::getInputForAttr(&input.attr, &output.inputId,
Mikhail Naganov2996f672019-04-18 12:29:59 -07001939 input.riid,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001940 sessionId,
1941 // FIXME compare to AudioTrack
1942 clientPid,
1943 clientUid,
Eric Laurentfee19762018-01-29 18:44:13 -08001944 input.opPackageName,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001945 &input.config,
1946 output.flags, &output.selectedDeviceId, &portId);
jiabinc1de2df2019-05-07 14:26:40 -07001947 if (lStatus != NO_ERROR) {
1948 ALOGE("createRecord() getInputForAttr return error %d", lStatus);
1949 goto Exit;
1950 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001951
Glenn Kasten05997e22014-03-13 15:08:33 -07001952 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001953 Mutex::Autolock _l(mLock);
Eric Laurentf14db3c2017-12-08 14:20:36 -08001954 RecordThread *thread = checkRecordThread_l(output.inputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955 if (thread == NULL) {
jiabinc1de2df2019-05-07 14:26:40 -07001956 ALOGE("createRecord() checkRecordThread_l failed, input handle %d", output.inputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957 lStatus = BAD_VALUE;
1958 goto Exit;
1959 }
1960
Eric Laurentf14db3c2017-12-08 14:20:36 -08001961 ALOGV("createRecord() lSessionId: %d input %d", sessionId, output.inputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001962
Eric Laurentf14db3c2017-12-08 14:20:36 -08001963 output.sampleRate = input.config.sample_rate;
1964 output.frameCount = input.frameCount;
1965 output.notificationFrameCount = input.notificationFrameCount;
Glenn Kasten570f6332014-03-13 15:01:06 -07001966
Kevin Rocard1f564ac2018-03-29 13:53:10 -07001967 recordTrack = thread->createRecordTrack_l(client, input.attr, &output.sampleRate,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001968 input.config.format, input.config.channel_mask,
1969 &output.frameCount, sessionId,
1970 &output.notificationFrameCount,
Eric Laurent09f1ed22019-04-24 17:45:17 -07001971 callingPid, clientUid, &output.flags,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001972 input.clientInfo.clientTid,
Jean-Michel Trivib0de5692019-08-20 15:42:04 -07001973 &lStatus, portId,
1974 input.opPackageName);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001975 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001976
Eric Laurentf14db3c2017-12-08 14:20:36 -08001977 // lStatus == BAD_TYPE means FAST flag was rejected: request a new input from
1978 // audio policy manager without FAST constraint
1979 if (lStatus == BAD_TYPE) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001980 continue;
Eric Laurent1b928682014-10-02 19:41:47 -07001981 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001982
1983 if (lStatus != NO_ERROR) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001984 goto Exit;
1985 }
1986
1987 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1988 // session and move it to this thread.
1989 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
1990 if (chain != 0) {
1991 Mutex::Autolock _l(thread->mLock);
1992 thread->addEffectChain_l(chain);
1993 }
1994 break;
1995 }
1996 // End of retry loop.
1997 // The lack of indentation is deliberate, to reduce code churn and ease merges.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001998 }
Glenn Kastene198c362013-08-13 09:13:36 -07001999
Eric Laurentf14db3c2017-12-08 14:20:36 -08002000 output.cblk = recordTrack->getCblk();
2001 output.buffers = recordTrack->getBuffers();
Eric Laurent973db022018-11-20 14:54:31 -08002002 output.portId = portId;
Eric Laurentf14db3c2017-12-08 14:20:36 -08002003
2004 // return handle to client
2005 recordHandle = new RecordHandle(recordTrack);
2006
2007Exit:
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002008 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002009 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07002010 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002011 // Don't hold mClientLock when releasing the reference on the track as the
2012 // destructor will acquire it.
2013 {
2014 Mutex::Autolock _cl(mClientLock);
2015 client.clear();
2016 }
Eric Laurent896c9672017-12-08 14:08:45 -08002017 recordTrack.clear();
2018 if (output.inputId != AUDIO_IO_HANDLE_NONE) {
Eric Laurentfee19762018-01-29 18:44:13 -08002019 AudioSystem::releaseInput(portId);
Eric Laurent896c9672017-12-08 14:08:45 -08002020 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002021 }
2022
Glenn Kasten9156ef32013-08-06 15:39:08 -07002023 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024 return recordHandle;
2025}
2026
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002027
2028
Mathias Agopian65ab4712010-07-14 17:59:35 -07002029// ----------------------------------------------------------------------------
2030
Eric Laurenta4c5a552012-03-29 10:12:40 -07002031audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
2032{
Eric Laurent44622db2014-08-01 19:00:33 -07002033 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07002034 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07002035 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07002036 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07002037 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002038 }
2039 Mutex::Autolock _l(mLock);
2040 return loadHwModule_l(name);
2041}
2042
2043// loadHwModule_l() must be called with AudioFlinger::mLock held
2044audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
2045{
2046 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
2047 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
2048 ALOGW("loadHwModule() module %s already loaded", name);
2049 return mAudioHwDevs.keyAt(i);
2050 }
2051 }
2052
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002053 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002054
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002055 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002056 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002057 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07002058 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002059 }
2060
2061 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002062 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07002063 mHardwareStatus = AUDIO_HW_IDLE;
2064 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002065 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07002066 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002067 }
2068
John Grossmanee578c02012-07-23 17:05:46 -07002069 // Check and cache this HAL's level of support for master mute and master
2070 // volume. If this is the first HAL opened, and it supports the get
2071 // methods, use the initial values provided by the HAL as the current
2072 // master mute and volume settings.
2073
2074 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
2075 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07002076 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07002077
2078 if (0 == mAudioHwDevs.size()) {
2079 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002080 float mv;
2081 if (OK == dev->getMasterVolume(&mv)) {
2082 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07002083 }
2084
2085 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002086 bool mm;
2087 if (OK == dev->getMasterMute(&mm)) {
2088 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07002089 }
2090 }
2091
Eric Laurenta4c5a552012-03-29 10:12:40 -07002092 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002093 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07002094 flags = static_cast<AudioHwDevice::Flags>(flags |
2095 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
2096 }
2097
2098 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002099 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07002100 flags = static_cast<AudioHwDevice::Flags>(flags |
2101 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
2102 }
2103
Eric Laurenta4c5a552012-03-29 10:12:40 -07002104 mHardwareStatus = AUDIO_HW_IDLE;
2105 }
Mikhail Naganov97373b02018-07-23 13:27:24 -07002106 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_MSD) == 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -07002107 // An MSD module is inserted before hardware modules in order to mix encoded streams.
2108 flags = static_cast<AudioHwDevice::Flags>(flags | AudioHwDevice::AHWD_IS_INSERT);
2109 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07002110
Glenn Kastena13cde92016-03-28 15:26:02 -07002111 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07002112 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07002113
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002114 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002115
2116 return handle;
2117
2118}
2119
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07002120// ----------------------------------------------------------------------------
2121
Glenn Kasten3b16c762012-11-14 08:44:39 -08002122uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07002123{
2124 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07002125 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07002126 return thread != NULL ? thread->sampleRate() : 0;
2127}
2128
Glenn Kastene33054e2012-11-14 12:54:39 -08002129size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07002130{
2131 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07002132 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07002133 return thread != NULL ? thread->frameCountHAL() : 0;
2134}
2135
2136// ----------------------------------------------------------------------------
2137
Andy Hung6f248bb2018-01-23 14:04:37 -08002138status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07002139{
2140 uid_t uid = IPCThreadState::self()->getCallingUid();
Andy Hung4ef19fa2018-05-15 19:35:29 -07002141 if (!isAudioServerOrSystemServerUid(uid)) {
Glenn Kasten4182c4e2013-07-15 14:45:07 -07002142 return PERMISSION_DENIED;
2143 }
2144 Mutex::Autolock _l(mLock);
2145 if (mIsDeviceTypeKnown) {
2146 return INVALID_OPERATION;
2147 }
2148 mIsLowRamDevice = isLowRamDevice;
Andy Hung6f248bb2018-01-23 14:04:37 -08002149 mTotalMemory = totalMemory;
2150 // mIsLowRamDevice and mTotalMemory are obtained through ActivityManager;
2151 // see ActivityManager.isLowRamDevice() and ActivityManager.getMemoryInfo().
2152 // mIsLowRamDevice generally represent devices with less than 1GB of memory,
2153 // though actual setting is determined through device configuration.
2154 constexpr int64_t GB = 1024 * 1024 * 1024;
2155 mClientSharedHeapSize =
2156 isLowRamDevice ? kMinimumClientSharedHeapSizeBytes
2157 : mTotalMemory < 2 * GB ? 4 * kMinimumClientSharedHeapSizeBytes
2158 : mTotalMemory < 3 * GB ? 8 * kMinimumClientSharedHeapSizeBytes
2159 : mTotalMemory < 4 * GB ? 16 * kMinimumClientSharedHeapSizeBytes
2160 : 32 * kMinimumClientSharedHeapSizeBytes;
Glenn Kasten4182c4e2013-07-15 14:45:07 -07002161 mIsDeviceTypeKnown = true;
Andy Hung6f248bb2018-01-23 14:04:37 -08002162
2163 // TODO: Cache the client shared heap size in a persistent property.
2164 // It's possible that a native process or Java service or app accesses audioserver
2165 // after it is registered by system server, but before AudioService updates
2166 // the memory info. This would occur immediately after boot or an audioserver
2167 // crash and restore. Before update from AudioService, the client would get the
2168 // minimum heap size.
2169
2170 ALOGD("isLowRamDevice:%s totalMemory:%lld mClientSharedHeapSize:%zu",
2171 (isLowRamDevice ? "true" : "false"),
2172 (long long)mTotalMemory,
2173 mClientSharedHeapSize.load());
Glenn Kasten4182c4e2013-07-15 14:45:07 -07002174 return NO_ERROR;
2175}
2176
Andy Hung6f248bb2018-01-23 14:04:37 -08002177size_t AudioFlinger::getClientSharedHeapSize() const
2178{
2179 size_t heapSizeInBytes = property_get_int32("ro.af.client_heap_size_kbyte", 0) * 1024;
2180 if (heapSizeInBytes != 0) { // read-only property overrides all.
2181 return heapSizeInBytes;
2182 }
2183 return mClientSharedHeapSize;
2184}
2185
Mikhail Naganovdea53042018-04-26 13:10:21 -07002186status_t AudioFlinger::setAudioPortConfig(const struct audio_port_config *config)
2187{
2188 ALOGV(__func__);
2189
2190 audio_module_handle_t module;
2191 if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2192 module = config->ext.device.hw_module;
2193 } else {
2194 module = config->ext.mix.hw_module;
2195 }
2196
2197 Mutex::Autolock _l(mLock);
2198 ssize_t index = mAudioHwDevs.indexOfKey(module);
2199 if (index < 0) {
2200 ALOGW("%s() bad hw module %d", __func__, module);
2201 return BAD_VALUE;
2202 }
2203
2204 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(index);
2205 return audioHwDevice->hwDevice()->setAudioPortConfig(config);
2206}
2207
Eric Laurent93c3d412014-08-01 14:48:35 -07002208audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
2209{
2210 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07002211
2212 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
2213 if (index >= 0) {
2214 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
2215 mHwAvSyncIds.valueAt(index), sessionId);
2216 return mHwAvSyncIds.valueAt(index);
2217 }
2218
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002219 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07002220 if (dev == NULL) {
2221 return AUDIO_HW_SYNC_INVALID;
2222 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002223 String8 reply;
2224 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07002225 if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002226 param = AudioParameter(reply);
2227 }
Eric Laurentfa90e842014-10-17 18:12:31 -07002228
2229 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07002230 if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) {
Eric Laurentfa90e842014-10-17 18:12:31 -07002231 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
2232 return AUDIO_HW_SYNC_INVALID;
2233 }
2234
2235 // allow only one session for a given HW A/V sync ID.
2236 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
2237 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
2238 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
2239 value, mHwAvSyncIds.keyAt(i));
2240 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07002241 break;
2242 }
2243 }
Eric Laurentfa90e842014-10-17 18:12:31 -07002244
2245 mHwAvSyncIds.add(sessionId, value);
2246
2247 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2248 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
2249 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07002250 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07002251 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07002252 param.addInt(String8(AudioParameter::keyStreamHwAvSync), value);
Mikhail Naganovb261ef52018-07-16 13:34:38 -07002253 String8 keyValuePairs = param.toString();
2254 thread->setParameters(keyValuePairs);
2255 forwardParametersToDownstreamPatches_l(thread->id(), keyValuePairs,
2256 [](const sp<PlaybackThread>& thread) { return thread->usesHwAvSync(); });
Eric Laurentfa90e842014-10-17 18:12:31 -07002257 break;
2258 }
2259 }
2260
2261 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
2262 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07002263}
2264
Eric Laurent72e3f392015-05-20 14:43:50 -07002265status_t AudioFlinger::systemReady()
2266{
2267 Mutex::Autolock _l(mLock);
2268 ALOGI("%s", __FUNCTION__);
2269 if (mSystemReady) {
2270 ALOGW("%s called twice", __FUNCTION__);
2271 return NO_ERROR;
2272 }
2273 mSystemReady = true;
2274 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2275 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
2276 thread->systemReady();
2277 }
2278 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2279 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
2280 thread->systemReady();
2281 }
2282 return NO_ERROR;
2283}
2284
jiabin46a76fa2018-01-05 10:18:21 -08002285status_t AudioFlinger::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
2286{
jiabin9ff780e2018-03-19 18:19:52 -07002287 AutoMutex lock(mHardwareLock);
2288 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
2289 status_t status = dev->getMicrophones(microphones);
2290 return status;
jiabin46a76fa2018-01-05 10:18:21 -08002291}
2292
Eric Laurentfa90e842014-10-17 18:12:31 -07002293// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
2294void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
2295{
2296 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
2297 if (index >= 0) {
2298 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
2299 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
2300 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07002301 param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId);
Mikhail Naganovb261ef52018-07-16 13:34:38 -07002302 String8 keyValuePairs = param.toString();
2303 thread->setParameters(keyValuePairs);
2304 forwardParametersToDownstreamPatches_l(thread->id(), keyValuePairs,
2305 [](const sp<PlaybackThread>& thread) { return thread->usesHwAvSync(); });
Eric Laurentfa90e842014-10-17 18:12:31 -07002306 }
2307}
2308
2309
Glenn Kasten4182c4e2013-07-15 14:45:07 -07002310// ----------------------------------------------------------------------------
2311
Eric Laurent83b88082014-06-20 18:31:16 -07002312
Eric Laurent6acd1d42017-01-04 14:23:29 -08002313sp<AudioFlinger::ThreadBase> AudioFlinger::openOutput_l(audio_module_handle_t module,
jiabinc0106832019-10-24 14:58:31 -07002314 audio_io_handle_t *output,
2315 audio_config_t *config,
2316 audio_devices_t deviceType,
2317 const String8& address,
2318 audio_output_flags_t flags)
Eric Laurent83b88082014-06-20 18:31:16 -07002319{
jiabinc0106832019-10-24 14:58:31 -07002320 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, deviceType);
Eric Laurent83b88082014-06-20 18:31:16 -07002321 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002322 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07002323 }
2324
Eric Laurentcf2c0212014-07-25 16:20:43 -07002325 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002326 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
2327 } else {
2328 // Audio Policy does not currently request a specific output handle.
2329 // If this is ever needed, see openInput_l() for example code.
2330 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
2331 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002332 }
Eric Laurent83b88082014-06-20 18:31:16 -07002333
2334 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
2335
Eric Laurent83b88082014-06-20 18:31:16 -07002336 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07002337 // This if statement allows overriding the audio policy settings
2338 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
2339 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
2340 // Check only for Normal Mixing mode
2341 if (kEnableExtendedPrecision) {
2342 // Specify format (uncomment one below to choose)
2343 //config->format = AUDIO_FORMAT_PCM_FLOAT;
2344 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
2345 //config->format = AUDIO_FORMAT_PCM_32_BIT;
2346 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002347 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07002348 }
2349 if (kEnableExtendedChannels) {
2350 // Specify channel mask (uncomment one below to choose)
2351 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
2352 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
2353 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
2354 }
Eric Laurent83b88082014-06-20 18:31:16 -07002355 }
2356
Phil Burk062e67a2015-02-11 13:40:50 -08002357 AudioStreamOut *outputStream = NULL;
2358 status_t status = outHwDev->openOutputStream(
2359 &outputStream,
2360 *output,
jiabinc0106832019-10-24 14:58:31 -07002361 deviceType,
Phil Burk062e67a2015-02-11 13:40:50 -08002362 flags,
2363 config,
2364 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07002365
2366 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07002367
Phil Burk062e67a2015-02-11 13:40:50 -08002368 if (status == NO_ERROR) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002369 if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
2370 sp<MmapPlaybackThread> thread =
jiabin10d86fd2019-10-31 17:20:42 -07002371 new MmapPlaybackThread(this, *output, outHwDev, outputStream, mSystemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002372 mMmapThreads.add(*output, thread);
2373 ALOGV("openOutput_l() created mmap playback thread: ID %d thread %p",
2374 *output, thread.get());
2375 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07002376 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002377 sp<PlaybackThread> thread;
2378 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
jiabin10d86fd2019-10-31 17:20:42 -07002379 thread = new OffloadThread(this, outputStream, *output, mSystemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002380 ALOGV("openOutput_l() created offload output: ID %d thread %p",
2381 *output, thread.get());
2382 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
2383 || !isValidPcmSinkFormat(config->format)
2384 || !isValidPcmSinkChannelMask(config->channel_mask)) {
jiabin10d86fd2019-10-31 17:20:42 -07002385 thread = new DirectOutputThread(this, outputStream, *output, mSystemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002386 ALOGV("openOutput_l() created direct output: ID %d thread %p",
2387 *output, thread.get());
2388 } else {
jiabin10d86fd2019-10-31 17:20:42 -07002389 thread = new MixerThread(this, outputStream, *output, mSystemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002390 ALOGV("openOutput_l() created mixer output: ID %d thread %p",
2391 *output, thread.get());
2392 }
2393 mPlaybackThreads.add(*output, thread);
Mikhail Naganovadca70f2018-07-09 12:49:25 -07002394 mPatchPanel.notifyStreamOpened(outHwDev, *output);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002395 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07002396 }
Eric Laurent83b88082014-06-20 18:31:16 -07002397 }
2398
2399 return 0;
2400}
2401
Eric Laurentcf2c0212014-07-25 16:20:43 -07002402status_t AudioFlinger::openOutput(audio_module_handle_t module,
2403 audio_io_handle_t *output,
2404 audio_config_t *config,
jiabinc0106832019-10-24 14:58:31 -07002405 const sp<DeviceDescriptorBase>& device,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002406 uint32_t *latencyMs,
2407 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002408{
jiabinc0106832019-10-24 14:58:31 -07002409 ALOGI("openOutput() this %p, module %d Device %s, SamplingRate %d, Format %#08x, "
Glenn Kasten49f36ba2017-12-06 13:02:02 -08002410 "Channels %#x, flags %#x",
Eric Laurent6acd1d42017-01-04 14:23:29 -08002411 this, module,
jiabinc0106832019-10-24 14:58:31 -07002412 device->toString().c_str(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07002413 config->sample_rate,
2414 config->format,
2415 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002416 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002417
jiabinc0106832019-10-24 14:58:31 -07002418 audio_devices_t deviceType = device->type();
2419 const String8 address = String8(device->address().c_str());
2420
2421 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002422 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002423 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002424
Mathias Agopian65ab4712010-07-14 17:59:35 -07002425 Mutex::Autolock _l(mLock);
2426
jiabinc0106832019-10-24 14:58:31 -07002427 sp<ThreadBase> thread = openOutput_l(module, output, config, deviceType, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002428 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002429 if ((flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) {
2430 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
2431 *latencyMs = playbackThread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002432
Eric Laurent6acd1d42017-01-04 14:23:29 -08002433 // notify client processes of the new output creation
2434 playbackThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002435
Eric Laurent6acd1d42017-01-04 14:23:29 -08002436 // the first primary output opened designates the primary hw device
2437 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -08002438 ALOGI("Using module %d as the primary audio interface", module);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002439 mPrimaryHardwareDev = playbackThread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002440
Eric Laurent6acd1d42017-01-04 14:23:29 -08002441 AutoMutex lock(mHardwareLock);
2442 mHardwareStatus = AUDIO_HW_SET_MODE;
2443 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
2444 mHardwareStatus = AUDIO_HW_IDLE;
2445 }
2446 } else {
2447 MmapThread *mmapThread = (MmapThread *)thread.get();
2448 mmapThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002449 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002450 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002451 }
2452
Eric Laurentcf2c0212014-07-25 16:20:43 -07002453 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002454}
2455
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002456audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
2457 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002458{
2459 Mutex::Autolock _l(mLock);
2460 MixerThread *thread1 = checkMixerThread_l(output1);
2461 MixerThread *thread2 = checkMixerThread_l(output2);
2462
2463 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002464 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
2465 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07002466 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002467 }
2468
Glenn Kasteneeecb982016-02-26 10:44:04 -08002469 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07002470 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002471 thread->addOutputTrack(thread2);
2472 mPlaybackThreads.add(id, thread);
2473 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002474 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002475 return id;
2476}
2477
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002478status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002479{
Glenn Kastend96c5722012-04-25 13:44:49 -07002480 return closeOutput_nonvirtual(output);
2481}
2482
2483status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
2484{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002485 // keep strong reference on the playback thread so that
2486 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002487 sp<PlaybackThread> playbackThread;
2488 sp<MmapPlaybackThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002489 {
2490 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002491 playbackThread = checkPlaybackThread_l(output);
2492 if (playbackThread != NULL) {
2493 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002494
Andy Hungdc099c22018-09-18 13:46:39 -07002495 dumpToThreadLog_l(playbackThread);
Andy Hunga8115dc2018-08-24 15:51:59 -07002496
Eric Laurent6acd1d42017-01-04 14:23:29 -08002497 if (playbackThread->type() == ThreadBase::MIXER) {
2498 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2499 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
2500 DuplicatingThread *dupThread =
2501 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
2502 dupThread->removeOutputTrack((MixerThread *)playbackThread.get());
2503 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002504 }
2505 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002506
2507
Eric Laurent6acd1d42017-01-04 14:23:29 -08002508 mPlaybackThreads.removeItem(output);
2509 // save all effects to the default thread
2510 if (mPlaybackThreads.size()) {
2511 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
2512 if (dstThread != NULL) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002513 // audioflinger lock is held so order of thread lock acquisition doesn't matter
Eric Laurent6acd1d42017-01-04 14:23:29 -08002514 Mutex::Autolock _dl(dstThread->mLock);
2515 Mutex::Autolock _sl(playbackThread->mLock);
2516 Vector< sp<EffectChain> > effectChains = playbackThread->getEffectChains_l();
2517 for (size_t i = 0; i < effectChains.size(); i ++) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002518 moveEffectChain_l(effectChains[i]->sessionId(), playbackThread.get(),
Eric Laurent6c796322019-04-09 14:13:17 -07002519 dstThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002520 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002521 }
2522 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002523 } else {
2524 mmapThread = (MmapPlaybackThread *)checkMmapThread_l(output);
2525 if (mmapThread == 0) {
2526 return BAD_VALUE;
2527 }
Andy Hungdc099c22018-09-18 13:46:39 -07002528 dumpToThreadLog_l(mmapThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002529 mMmapThreads.removeItem(output);
Phil Burk7f6b40d2017-02-09 13:18:38 -08002530 ALOGD("closing mmapThread %p", mmapThread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002531 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002532 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2533 ioDesc->mIoHandle = output;
2534 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mikhail Naganovadca70f2018-07-09 12:49:25 -07002535 mPatchPanel.notifyStreamClosed(output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002536 }
Glenn Kastenb28686f2012-01-06 08:39:38 -08002537 // The thread entity (active unit of execution) is no longer running here,
2538 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002539
Eric Laurent6acd1d42017-01-04 14:23:29 -08002540 if (playbackThread != 0) {
2541 playbackThread->exit();
2542 if (!playbackThread->isDuplicating()) {
2543 closeOutputFinish(playbackThread);
2544 }
2545 } else if (mmapThread != 0) {
Phil Burk7f6b40d2017-02-09 13:18:38 -08002546 ALOGD("mmapThread exit()");
Eric Laurent6acd1d42017-01-04 14:23:29 -08002547 mmapThread->exit();
2548 AudioStreamOut *out = mmapThread->clearOutput();
2549 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2550 // from now on thread->mOutput is NULL
2551 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002552 }
2553 return NO_ERROR;
2554}
2555
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002556void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002557{
2558 AudioStreamOut *out = thread->clearOutput();
2559 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2560 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07002561 delete out;
2562}
2563
Mikhail Naganov444ecc32018-05-01 17:40:05 -07002564void AudioFlinger::closeThreadInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002565{
2566 mPlaybackThreads.removeItem(thread->mId);
2567 thread->exit();
2568 closeOutputFinish(thread);
2569}
2570
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002571status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002572{
2573 Mutex::Autolock _l(mLock);
2574 PlaybackThread *thread = checkPlaybackThread_l(output);
2575
2576 if (thread == NULL) {
2577 return BAD_VALUE;
2578 }
2579
Steve Block3856b092011-10-20 11:56:00 +01002580 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002581 thread->suspend();
2582
2583 return NO_ERROR;
2584}
2585
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002586status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002587{
2588 Mutex::Autolock _l(mLock);
2589 PlaybackThread *thread = checkPlaybackThread_l(output);
2590
2591 if (thread == NULL) {
2592 return BAD_VALUE;
2593 }
2594
Steve Block3856b092011-10-20 11:56:00 +01002595 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002596
2597 thread->restore();
2598
2599 return NO_ERROR;
2600}
2601
Eric Laurentcf2c0212014-07-25 16:20:43 -07002602status_t AudioFlinger::openInput(audio_module_handle_t module,
2603 audio_io_handle_t *input,
2604 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002605 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002606 const String8& address,
2607 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002608 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002609{
Eric Laurent83b88082014-06-20 18:31:16 -07002610 Mutex::Autolock _l(mLock);
2611
Glenn Kastene7d66712015-03-05 13:46:52 -08002612 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002613 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002614 }
2615
Mikhail Naganovb4e037e2019-01-14 15:56:33 -08002616 sp<ThreadBase> thread = openInput_l(
2617 module, input, config, *devices, address, source, flags, AUDIO_DEVICE_NONE, String8{});
Eric Laurent83b88082014-06-20 18:31:16 -07002618
2619 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002620 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002621 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002622 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002623 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002624 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002625}
Dima Zavin799a70e2011-04-18 16:57:27 -07002626
Eric Laurent6acd1d42017-01-04 14:23:29 -08002627sp<AudioFlinger::ThreadBase> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002628 audio_io_handle_t *input,
2629 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002630 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002631 const String8& address,
2632 audio_source_t source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -08002633 audio_input_flags_t flags,
2634 audio_devices_t outputDevice,
2635 const String8& outputDeviceAddress)
Eric Laurent83b88082014-06-20 18:31:16 -07002636{
Glenn Kastene7d66712015-03-05 13:46:52 -08002637 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002638 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002639 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002640 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002641 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002642
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07002643 // Some flags are specific to framework and must not leak to the HAL.
2644 flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FRAMEWORK_FLAGS);
2645
Glenn Kasteneeecb982016-02-26 10:44:04 -08002646 // Audio Policy can request a specific handle for hardware hotword.
2647 // The goal here is not to re-open an already opened input.
2648 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002649 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002650 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2651 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2652 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2653 return 0;
2654 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2655 // This should not happen in a transient state with current design.
2656 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2657 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002658 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002659
Eric Laurentcf2c0212014-07-25 16:20:43 -07002660 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002661 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002662 sp<StreamInHalInterface> inStream;
2663 status_t status = inHwHal->openInputStream(
Mikhail Naganovb4e037e2019-01-14 15:56:33 -08002664 *input, devices, &halconfig, flags, address.string(), source,
2665 outputDevice, outputDeviceAddress, &inStream);
Glenn Kasten49f36ba2017-12-06 13:02:02 -08002666 ALOGV("openInput_l() openInputStream returned input %p, devices %#x, SamplingRate %d"
2667 ", Format %#x, Channels %#x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002668 inStream.get(),
Mikhail Naganovf558e022016-11-14 17:45:17 -08002669 devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002670 halconfig.sample_rate,
2671 halconfig.format,
2672 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002673 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002674 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002675
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002676 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002677 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002678 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002679 audio_is_linear_pcm(config->format) &&
2680 audio_is_linear_pcm(halconfig.format) &&
2681 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002682 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2683 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002684 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002685 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002686 inStream.clear();
2687 status = inHwHal->openInputStream(
Mikhail Naganovb4e037e2019-01-14 15:56:33 -08002688 *input, devices, &halconfig, flags, address.string(), source,
2689 outputDevice, outputDeviceAddress, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002690 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002691 }
2692
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002693 if (status == NO_ERROR && inStream != 0) {
Eric Laurent05067782016-06-01 18:27:28 -07002694 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002695 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) {
2696 sp<MmapCaptureThread> thread =
jiabin10d86fd2019-10-31 17:20:42 -07002697 new MmapCaptureThread(this, *input, inHwDev, inputStream, mSystemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002698 mMmapThreads.add(*input, thread);
Glenn Kastend3bb6452016-12-05 18:14:37 -08002699 ALOGV("openInput_l() created mmap capture thread: ID %d thread %p", *input,
2700 thread.get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002701 return thread;
2702 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002703 // Start record thread
2704 // RecordThread requires both input and output device indication to forward to audio
2705 // pre processing modules
jiabin10d86fd2019-10-31 17:20:42 -07002706 sp<RecordThread> thread = new RecordThread(this, inputStream, *input, mSystemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002707 mRecordThreads.add(*input, thread);
2708 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
2709 return thread;
2710 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002711 }
2712
Eric Laurentcf2c0212014-07-25 16:20:43 -07002713 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002714 return 0;
2715}
2716
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002717status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002718{
Glenn Kastend96c5722012-04-25 13:44:49 -07002719 return closeInput_nonvirtual(input);
2720}
2721
2722status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2723{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002724 // keep strong reference on the record thread so that
2725 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002726 sp<RecordThread> recordThread;
2727 sp<MmapCaptureThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002728 {
2729 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002730 recordThread = checkRecordThread_l(input);
2731 if (recordThread != 0) {
2732 ALOGV("closeInput() %d", input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002733
Andy Hungdc099c22018-09-18 13:46:39 -07002734 dumpToThreadLog_l(recordThread);
Andy Hung0264e7d2018-09-17 19:30:46 -07002735
Eric Laurent6acd1d42017-01-04 14:23:29 -08002736 // If we still have effect chains, it means that a client still holds a handle
2737 // on at least one effect. We must either move the chain to an existing thread with the
2738 // same session ID or put it aside in case a new record thread is opened for a
2739 // new capture on the same session
2740 sp<EffectChain> chain;
2741 {
2742 Mutex::Autolock _sl(recordThread->mLock);
2743 Vector< sp<EffectChain> > effectChains = recordThread->getEffectChains_l();
2744 // Note: maximum one chain per record thread
2745 if (effectChains.size() != 0) {
2746 chain = effectChains[0];
Eric Laurent1b928682014-10-02 19:41:47 -07002747 }
2748 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002749 if (chain != 0) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002750 // first check if a record thread is already opened with a client on same session.
Eric Laurent6acd1d42017-01-04 14:23:29 -08002751 // This should only happen in case of overlap between one thread tear down and the
2752 // creation of its replacement
2753 size_t i;
2754 for (i = 0; i < mRecordThreads.size(); i++) {
2755 sp<RecordThread> t = mRecordThreads.valueAt(i);
2756 if (t == recordThread) {
2757 continue;
2758 }
2759 if (t->hasAudioSession(chain->sessionId()) != 0) {
2760 Mutex::Autolock _l(t->mLock);
2761 ALOGV("closeInput() found thread %d for effect session %d",
2762 t->id(), chain->sessionId());
2763 t->addEffectChain_l(chain);
2764 break;
2765 }
2766 }
Glenn Kastend3bb6452016-12-05 18:14:37 -08002767 // put the chain aside if we could not find a record thread with the same session id
Eric Laurent6acd1d42017-01-04 14:23:29 -08002768 if (i == mRecordThreads.size()) {
2769 putOrphanEffectChain_l(chain);
2770 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002771 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002772 mRecordThreads.removeItem(input);
2773 } else {
2774 mmapThread = (MmapCaptureThread *)checkMmapThread_l(input);
2775 if (mmapThread == 0) {
2776 return BAD_VALUE;
2777 }
Andy Hungdc099c22018-09-18 13:46:39 -07002778 dumpToThreadLog_l(mmapThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002779 mMmapThreads.removeItem(input);
Eric Laurentaaa44472014-09-12 17:41:50 -07002780 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002781 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2782 ioDesc->mIoHandle = input;
2783 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002784 }
Eric Laurent83b88082014-06-20 18:31:16 -07002785 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2786 // we have a different lock for notification client
Eric Laurent6acd1d42017-01-04 14:23:29 -08002787 if (recordThread != 0) {
2788 closeInputFinish(recordThread);
2789 } else if (mmapThread != 0) {
2790 mmapThread->exit();
2791 AudioStreamIn *in = mmapThread->clearInput();
2792 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
2793 // from now on thread->mInput is NULL
2794 delete in;
2795 }
Eric Laurent83b88082014-06-20 18:31:16 -07002796 return NO_ERROR;
2797}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002798
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002799void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002800{
2801 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002802 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002803 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002804 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002805 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002806}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002807
Mikhail Naganov444ecc32018-05-01 17:40:05 -07002808void AudioFlinger::closeThreadInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002809{
2810 mRecordThreads.removeItem(thread->mId);
2811 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002812}
2813
Glenn Kastend2304db2014-02-03 07:40:31 -08002814status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002815{
2816 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002817 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002818
2819 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2820 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002821 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002822 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002823 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2824 mMmapThreads[i]->invalidateTracks(stream);
2825 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002826 return NO_ERROR;
2827}
2828
2829
Glenn Kasteneeecb982016-02-26 10:44:04 -08002830audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002831{
Glenn Kasten9d003132016-04-06 14:38:09 -07002832 // This is a binder API, so a malicious client could pass in a bad parameter.
2833 // Check for that before calling the internal API nextUniqueId().
2834 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2835 ALOGE("newAudioUniqueId invalid use %d", use);
2836 return AUDIO_UNIQUE_ID_ALLOCATE;
2837 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002838 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002839}
2840
Glenn Kastend848eb42016-03-08 13:42:11 -08002841void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002842{
2843 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002844 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002845 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
Andy Hung4ef19fa2018-05-15 19:35:29 -07002846 const uid_t callerUid = IPCThreadState::self()->getCallingUid();
2847 if (pid != -1 && isAudioServerUid(callerUid)) { // check must match releaseAudioSessionId()
Marco Nelissend457c972014-02-11 08:47:07 -08002848 caller = pid;
2849 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002850
Eric Laurent021cf962014-05-13 10:18:14 -07002851 {
2852 Mutex::Autolock _cl(mClientLock);
2853 // Ignore requests received from processes not known as notification client. The request
2854 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2855 // called from a different pid leaving a stale session reference. Also we don't know how
2856 // to clear this reference if the client process dies.
2857 if (mNotificationClients.indexOfKey(caller) < 0) {
2858 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2859 return;
2860 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002861 }
2862
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002863 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002864 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002865 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002866 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2867 ref->mCnt++;
2868 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002869 return;
2870 }
2871 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002872 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2873 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002874}
2875
Glenn Kastend848eb42016-03-08 13:42:11 -08002876void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002877{
Eric Laurent6c796322019-04-09 14:13:17 -07002878 std::vector< sp<EffectModule> > removedEffects;
2879 {
2880 Mutex::Autolock _l(mLock);
2881 pid_t caller = IPCThreadState::self()->getCallingPid();
2882 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2883 const uid_t callerUid = IPCThreadState::self()->getCallingUid();
2884 if (pid != -1 && isAudioServerUid(callerUid)) { // check must match acquireAudioSessionId()
2885 caller = pid;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002886 }
Eric Laurent6c796322019-04-09 14:13:17 -07002887 size_t num = mAudioSessionRefs.size();
2888 for (size_t i = 0; i < num; i++) {
2889 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2890 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2891 ref->mCnt--;
2892 ALOGV(" decremented refcount to %d", ref->mCnt);
2893 if (ref->mCnt == 0) {
2894 mAudioSessionRefs.removeAt(i);
2895 delete ref;
2896 std::vector< sp<EffectModule> > effects = purgeStaleEffects_l();
2897 removedEffects.insert(removedEffects.end(), effects.begin(), effects.end());
2898 }
2899 goto Exit;
2900 }
2901 }
2902 // If the caller is audioserver it is likely that the session being released was acquired
2903 // on behalf of a process not in notification clients and we ignore the warning.
2904 ALOGW_IF(!isAudioServerUid(callerUid),
2905 "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002906 }
Eric Laurent6c796322019-04-09 14:13:17 -07002907
2908Exit:
2909 for (auto& effect : removedEffects) {
2910 effect->updatePolicyState();
2911 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002912}
2913
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002914bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
2915{
2916 size_t num = mAudioSessionRefs.size();
2917 for (size_t i = 0; i < num; i++) {
2918 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2919 if (ref->mSessionid == audioSession) {
2920 return true;
2921 }
2922 }
2923 return false;
2924}
2925
Eric Laurent6c796322019-04-09 14:13:17 -07002926std::vector<sp<AudioFlinger::EffectModule>> AudioFlinger::purgeStaleEffects_l() {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002927
Steve Block3856b092011-10-20 11:56:00 +01002928 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002929
2930 Vector< sp<EffectChain> > chains;
Eric Laurent6c796322019-04-09 14:13:17 -07002931 std::vector< sp<EffectModule> > removedEffects;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002932
2933 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2934 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
Mateusz Kaplon2a6e1b02017-03-30 16:50:50 +02002935 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002936 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2937 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002938 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2939 chains.push(ec);
2940 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002941 }
2942 }
Eric Laurent6c796322019-04-09 14:13:17 -07002943
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002944 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2945 sp<RecordThread> t = mRecordThreads.valueAt(i);
Mateusz Kaplon2a6e1b02017-03-30 16:50:50 +02002946 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002947 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2948 sp<EffectChain> ec = t->mEffectChains[j];
2949 chains.push(ec);
2950 }
2951 }
2952
Eric Laurent6c796322019-04-09 14:13:17 -07002953 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2954 sp<MmapThread> t = mMmapThreads.valueAt(i);
2955 Mutex::Autolock _l(t->mLock);
2956 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2957 sp<EffectChain> ec = t->mEffectChains[j];
2958 chains.push(ec);
2959 }
2960 }
2961
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002962 for (size_t i = 0; i < chains.size(); i++) {
2963 sp<EffectChain> ec = chains[i];
2964 int sessionid = ec->sessionId();
2965 sp<ThreadBase> t = ec->mThread.promote();
2966 if (t == 0) {
2967 continue;
2968 }
2969 size_t numsessionrefs = mAudioSessionRefs.size();
2970 bool found = false;
2971 for (size_t k = 0; k < numsessionrefs; k++) {
2972 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002973 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002974 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002975 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002976 found = true;
2977 break;
2978 }
2979 }
2980 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002981 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002982 // remove all effects from the chain
2983 while (ec->mEffects.size()) {
2984 sp<EffectModule> effect = ec->mEffects[0];
2985 effect->unPin();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07002986 t->removeEffect_l(effect, /*release*/ true);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002987 if (effect->purgeHandles()) {
2988 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002989 }
Eric Laurent6c796322019-04-09 14:13:17 -07002990 removedEffects.push_back(effect);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002991 }
2992 }
2993 }
Eric Laurent6c796322019-04-09 14:13:17 -07002994 return removedEffects;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002995}
2996
Andy Hungdc099c22018-09-18 13:46:39 -07002997// dumpToThreadLog_l() must be called with AudioFlinger::mLock held
2998void AudioFlinger::dumpToThreadLog_l(const sp<ThreadBase> &thread)
2999{
3000 audio_utils::FdToString fdToString;
3001 const int fd = fdToString.fd();
3002 if (fd >= 0) {
3003 thread->dump(fd, {} /* args */);
3004 mThreadLog.logs(-1 /* time */, fdToString.getStringAndClose());
3005 }
3006}
3007
Glenn Kasteneeecb982016-02-26 10:44:04 -08003008// checkThread_l() must be called with AudioFlinger::mLock held
3009AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
3010{
Eric Laurent6acd1d42017-01-04 14:23:29 -08003011 ThreadBase *thread = checkMmapThread_l(ioHandle);
3012 if (thread == 0) {
3013 switch (audio_unique_id_get_use(ioHandle)) {
3014 case AUDIO_UNIQUE_ID_USE_OUTPUT:
3015 thread = checkPlaybackThread_l(ioHandle);
3016 break;
3017 case AUDIO_UNIQUE_ID_USE_INPUT:
3018 thread = checkRecordThread_l(ioHandle);
3019 break;
3020 default:
3021 break;
3022 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08003023 }
3024 return thread;
3025}
3026
Mathias Agopian65ab4712010-07-14 17:59:35 -07003027// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003028AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003029{
Glenn Kastena1117922012-01-26 10:53:32 -08003030 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07003031}
3032
3033// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003034AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003035{
3036 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08003037 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003038}
3039
3040// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003041AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003042{
Glenn Kastena1117922012-01-26 10:53:32 -08003043 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07003044}
3045
Eric Laurent6acd1d42017-01-04 14:23:29 -08003046// checkMmapThread_l() must be called with AudioFlinger::mLock held
3047AudioFlinger::MmapThread *AudioFlinger::checkMmapThread_l(audio_io_handle_t io) const
3048{
3049 return mMmapThreads.valueFor(io).get();
3050}
3051
3052
3053// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
3054AudioFlinger::VolumeInterface *AudioFlinger::getVolumeInterface_l(audio_io_handle_t output) const
3055{
Eric Laurent7459b902017-04-19 18:10:41 -07003056 VolumeInterface *volumeInterface = mPlaybackThreads.valueFor(output).get();
Eric Laurent6acd1d42017-01-04 14:23:29 -08003057 if (volumeInterface == nullptr) {
3058 MmapThread *mmapThread = mMmapThreads.valueFor(output).get();
3059 if (mmapThread != nullptr) {
3060 if (mmapThread->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07003061 MmapPlaybackThread *mmapPlaybackThread =
3062 static_cast<MmapPlaybackThread *>(mmapThread);
3063 volumeInterface = mmapPlaybackThread;
Eric Laurent6acd1d42017-01-04 14:23:29 -08003064 }
3065 }
3066 }
3067 return volumeInterface;
3068}
3069
3070Vector <AudioFlinger::VolumeInterface *> AudioFlinger::getAllVolumeInterfaces_l() const
3071{
3072 Vector <VolumeInterface *> volumeInterfaces;
3073 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7459b902017-04-19 18:10:41 -07003074 volumeInterfaces.add(mPlaybackThreads.valueAt(i).get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08003075 }
3076 for (size_t i = 0; i < mMmapThreads.size(); i++) {
3077 if (mMmapThreads.valueAt(i)->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07003078 MmapPlaybackThread *mmapPlaybackThread =
3079 static_cast<MmapPlaybackThread *>(mMmapThreads.valueAt(i).get());
3080 volumeInterfaces.add(mmapPlaybackThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08003081 }
3082 }
3083 return volumeInterfaces;
3084}
3085
Glenn Kasteneeecb982016-02-26 10:44:04 -08003086audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003087{
Glenn Kasten9d003132016-04-06 14:38:09 -07003088 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08003089 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07003090 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
3091 for (int retry = 0; retry < maxRetries; retry++) {
3092 // The cast allows wraparound from max positive to min negative instead of abort
3093 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
3094 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
3095 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
3096 // allow wrap by skipping 0 and -1 for session ids
3097 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
3098 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
3099 return (audio_unique_id_t) (base | use);
3100 }
3101 }
3102 // We have no way of recovering from wraparound
3103 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
3104 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07003105}
3106
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08003107AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003108{
3109 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3110 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07003111 if(thread->isDuplicating()) {
3112 continue;
3113 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07003114 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07003115 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003116 return thread;
3117 }
3118 }
3119 return NULL;
3120}
3121
jiabin10d86fd2019-10-31 17:20:42 -07003122DeviceTypeSet AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003123{
3124 PlaybackThread *thread = primaryPlaybackThread_l();
3125
3126 if (thread == NULL) {
jiabin10d86fd2019-10-31 17:20:42 -07003127 return DeviceTypeSet();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003128 }
3129
jiabin10d86fd2019-10-31 17:20:42 -07003130 return thread->outDeviceTypes();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003131}
3132
Glenn Kastena7335632016-06-09 17:09:53 -07003133AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
3134{
3135 size_t minFrameCount = 0;
3136 PlaybackThread *minThread = NULL;
3137 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3138 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
3139 if (!thread->isDuplicating()) {
3140 size_t frameCount = thread->frameCountHAL();
3141 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
3142 (frameCount == minFrameCount && thread->hasFastMixer() &&
3143 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
3144 minFrameCount = frameCount;
3145 minThread = thread;
3146 }
3147 }
3148 }
3149 return minThread;
3150}
3151
Eric Laurenta011e352012-03-29 15:51:43 -07003152sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08003153 audio_session_t triggerSession,
3154 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07003155 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003156 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07003157{
3158 Mutex::Autolock _l(mLock);
3159
3160 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
3161 status_t playStatus = NAME_NOT_FOUND;
3162 status_t recStatus = NAME_NOT_FOUND;
3163 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3164 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
3165 if (playStatus == NO_ERROR) {
3166 return event;
3167 }
3168 }
3169 for (size_t i = 0; i < mRecordThreads.size(); i++) {
3170 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
3171 if (recStatus == NO_ERROR) {
3172 return event;
3173 }
3174 }
3175 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
3176 mPendingSyncEvents.add(event);
3177 } else {
3178 ALOGV("createSyncEvent() invalid event %d", event->type());
3179 event.clear();
3180 }
3181 return event;
3182}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003183
Mathias Agopian65ab4712010-07-14 17:59:35 -07003184// ----------------------------------------------------------------------------
3185// Effect management
3186// ----------------------------------------------------------------------------
3187
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003188sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
3189 return mEffectsFactoryHal;
3190}
Mathias Agopian65ab4712010-07-14 17:59:35 -07003191
Glenn Kastenf587ba52012-01-26 16:25:10 -08003192status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003193{
3194 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003195 if (mEffectsFactoryHal.get()) {
3196 return mEffectsFactoryHal->queryNumberEffects(numEffects);
3197 } else {
3198 return -ENODEV;
3199 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003200}
3201
Glenn Kastenf587ba52012-01-26 16:25:10 -08003202status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003203{
3204 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003205 if (mEffectsFactoryHal.get()) {
3206 return mEffectsFactoryHal->getDescriptor(index, descriptor);
3207 } else {
3208 return -ENODEV;
3209 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003210}
3211
Glenn Kasten5e92a782012-01-30 07:40:52 -08003212status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -07003213 const effect_uuid_t *pTypeUuid,
3214 uint32_t preferredTypeFlag,
3215 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003216{
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -07003217 if (pUuid == NULL || pTypeUuid == NULL || descriptor == NULL) {
3218 return BAD_VALUE;
3219 }
3220
Mathias Agopian65ab4712010-07-14 17:59:35 -07003221 Mutex::Autolock _l(mLock);
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -07003222
3223 if (!mEffectsFactoryHal.get()) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003224 return -ENODEV;
3225 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003226
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -07003227 status_t status = NO_ERROR;
3228 if (!EffectsFactoryHalInterface::isNullUuid(pUuid)) {
3229 // If uuid is specified, request effect descriptor from that.
3230 status = mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
3231 } else if (!EffectsFactoryHalInterface::isNullUuid(pTypeUuid)) {
3232 // If uuid is not specified, look for an available implementation
3233 // of the required type instead.
3234
3235 // Use a temporary descriptor to avoid modifying |descriptor| in the failure case.
3236 effect_descriptor_t desc;
3237 desc.flags = 0; // prevent compiler warning
3238
3239 uint32_t numEffects = 0;
3240 status = mEffectsFactoryHal->queryNumberEffects(&numEffects);
3241 if (status < 0) {
3242 ALOGW("getEffectDescriptor() error %d from FactoryHal queryNumberEffects", status);
3243 return status;
3244 }
3245
3246 bool found = false;
3247 for (uint32_t i = 0; i < numEffects; i++) {
3248 status = mEffectsFactoryHal->getDescriptor(i, &desc);
3249 if (status < 0) {
3250 ALOGW("getEffectDescriptor() error %d from FactoryHal getDescriptor", status);
3251 continue;
3252 }
3253 if (memcmp(&desc.type, pTypeUuid, sizeof(effect_uuid_t)) == 0) {
3254 // If matching type found save effect descriptor.
3255 found = true;
3256 *descriptor = desc;
3257
3258 // If there's no preferred flag or this descriptor matches the preferred
3259 // flag, success! If this descriptor doesn't match the preferred
3260 // flag, continue enumeration in case a better matching version of this
3261 // effect type is available. Note that this means if no effect with a
3262 // correct flag is found, the descriptor returned will correspond to the
3263 // last effect that at least had a matching type uuid (if any).
3264 if (preferredTypeFlag == EFFECT_FLAG_TYPE_MASK ||
3265 (desc.flags & EFFECT_FLAG_TYPE_MASK) == preferredTypeFlag) {
3266 break;
3267 }
3268 }
3269 }
3270
3271 if (!found) {
3272 status = NAME_NOT_FOUND;
3273 ALOGW("getEffectDescriptor(): Effect not found by type.");
3274 }
3275 } else {
3276 status = BAD_VALUE;
3277 ALOGE("getEffectDescriptor(): Either uuid or type uuid must be non-null UUIDs.");
3278 }
3279 return status;
3280}
Mathias Agopian65ab4712010-07-14 17:59:35 -07003281
Glenn Kasten8d6cc842012-02-03 11:06:53 -08003282sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07003283 effect_descriptor_t *pDesc,
3284 const sp<IEffectClient>& effectClient,
3285 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003286 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08003287 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07003288 const String16& opPackageName,
Eric Laurentb6436272016-12-07 19:24:50 -08003289 pid_t pid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003290 status_t *status,
3291 int *id,
3292 int *enabled)
3293{
3294 status_t lStatus = NO_ERROR;
3295 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003296 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003297
Eric Laurentb6436272016-12-07 19:24:50 -08003298 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Andy Hung4ef19fa2018-05-15 19:35:29 -07003299 if (pid == -1 || !isAudioServerOrMediaServerUid(callingUid)) {
Eric Laurentb6436272016-12-07 19:24:50 -08003300 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
3301 ALOGW_IF(pid != -1 && pid != callingPid,
3302 "%s uid %d pid %d tried to pass itself off as pid %d",
3303 __func__, callingUid, callingPid, pid);
3304 pid = callingPid;
3305 }
3306
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003307 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
3308 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003309
3310 if (pDesc == NULL) {
3311 lStatus = BAD_VALUE;
3312 goto Exit;
3313 }
3314
Mikhail Naganov1dc98672016-08-18 17:50:29 -07003315 if (mEffectsFactoryHal == 0) {
Andy Hung6096dcd2019-03-13 13:20:30 -07003316 ALOGE("%s: no effects factory hal", __func__);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003317 lStatus = NO_INIT;
3318 goto Exit;
3319 }
3320
Andy Hung6096dcd2019-03-13 13:20:30 -07003321 // check audio settings permission for global effects
3322 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
3323 if (!settingsAllowed()) {
3324 ALOGE("%s: no permission for AUDIO_SESSION_OUTPUT_MIX", __func__);
3325 lStatus = PERMISSION_DENIED;
3326 goto Exit;
3327 }
3328 } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
3329 if (!isAudioServerUid(callingUid)) {
3330 ALOGE("%s: only APM can create using AUDIO_SESSION_OUTPUT_STAGE", __func__);
3331 lStatus = PERMISSION_DENIED;
3332 goto Exit;
3333 }
3334
3335 if (io == AUDIO_IO_HANDLE_NONE) {
3336 ALOGE("%s: APM must specify output when using AUDIO_SESSION_OUTPUT_STAGE", __func__);
3337 lStatus = BAD_VALUE;
3338 goto Exit;
3339 }
3340 } else {
3341 // general sessionId.
3342
3343 if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
3344 ALOGE("%s: invalid sessionId %d", __func__, sessionId);
3345 lStatus = BAD_VALUE;
3346 goto Exit;
3347 }
3348
3349 // TODO: should we check if the callingUid (limited to pid) is in mAudioSessionRefs
3350 // to prevent creating an effect when one doesn't actually have track with that session?
3351 }
3352
Mathias Agopian65ab4712010-07-14 17:59:35 -07003353 {
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -07003354 // Get the full effect descriptor from the uuid/type.
3355 // If the session is the output mix, prefer an auxiliary effect,
3356 // otherwise no preference.
3357 uint32_t preferredType = (sessionId == AUDIO_SESSION_OUTPUT_MIX ?
3358 EFFECT_FLAG_TYPE_AUXILIARY : EFFECT_FLAG_TYPE_MASK);
3359 lStatus = getEffectDescriptor(&pDesc->uuid, &pDesc->type, preferredType, &desc);
3360 if (lStatus < 0) {
3361 ALOGW("createEffect() error %d from getEffectDescriptor", lStatus);
3362 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003363 }
3364
3365 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07003366 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07003367 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
3368 lStatus = INVALID_OPERATION;
3369 goto Exit;
3370 }
3371
Eric Laurent59255e42011-07-27 19:49:51 -07003372 // check recording permission for visualizer
3373 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Svet Ganov6e641372018-03-02 09:21:30 -08003374 // TODO: Do we need to start/stop op - i.e. is there recording being performed?
Marco Nelissendcb346b2015-09-09 10:47:29 -07003375 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07003376 lStatus = PERMISSION_DENIED;
3377 goto Exit;
3378 }
3379
Mathias Agopian65ab4712010-07-14 17:59:35 -07003380 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08003381 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07003382 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07003383 // if the output returned by getOutputForEffect() is removed before we lock the
3384 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
3385 // and we will exit safely
3386 io = AudioSystem::getOutputForEffect(&desc);
3387 ALOGV("createEffect got output %d", io);
3388 }
3389
3390 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003391
3392 // If output is not specified try to find a matching audio session ID in one of the
3393 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07003394 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
3395 // because of code checking output when entering the function.
Andy Hung444bb552019-03-14 17:06:39 -07003396 // Note: io is never AUDIO_IO_HANDLE_NONE when creating an effect on an input by APM.
3397 // An AudioEffect created from the Java API will have io as AUDIO_IO_HANDLE_NONE.
Glenn Kasten142f5192014-03-25 17:44:59 -07003398 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07003399 // look for the thread where the specified audio session is present
Andy Hunge778c422019-03-14 15:04:30 -07003400 io = findIoHandleBySessionId_l(sessionId, mPlaybackThreads);
3401 if (io == AUDIO_IO_HANDLE_NONE) {
3402 io = findIoHandleBySessionId_l(sessionId, mRecordThreads);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003403 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08003404 if (io == AUDIO_IO_HANDLE_NONE) {
Andy Hunge778c422019-03-14 15:04:30 -07003405 io = findIoHandleBySessionId_l(sessionId, mMmapThreads);
Eric Laurent6acd1d42017-01-04 14:23:29 -08003406 }
Andy Hung444bb552019-03-14 17:06:39 -07003407
3408 // If you wish to create a Record preprocessing AudioEffect in Java,
3409 // you MUST create an AudioRecord first and keep it alive so it is picked up above.
3410 // Otherwise it will fail when created on a Playback thread by legacy
3411 // handling below. Ditto with Mmap, the associated Mmap track must be created
3412 // before creating the AudioEffect or the io handle must be specified.
3413 //
3414 // Detect if the effect is created after an AudioRecord is destroyed.
3415 if (getOrphanEffectChain_l(sessionId).get() != nullptr) {
3416 ALOGE("%s: effect %s with no specified io handle is denied because the AudioRecord"
3417 " for session %d no longer exists",
3418 __func__, desc.name, sessionId);
3419 lStatus = PERMISSION_DENIED;
3420 goto Exit;
3421 }
3422
3423 // Legacy handling of creating an effect on an expired or made-up
3424 // session id. We think that it is a Playback effect.
3425 //
Eric Laurent84e9a102010-09-23 16:10:16 -07003426 // If no output thread contains the requested session ID, default to
3427 // first output. The effect chain will be moved to the correct output
3428 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07003429 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003430 io = mPlaybackThreads.keyAt(0);
3431 }
Steve Block3856b092011-10-20 11:56:00 +01003432 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Andy Hung1631f062019-03-12 19:39:03 -07003433 } else if (checkPlaybackThread_l(io) != nullptr) {
3434 // allow only one effect chain per sessionId on mPlaybackThreads.
3435 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3436 const audio_io_handle_t checkIo = mPlaybackThreads.keyAt(i);
3437 if (io == checkIo) continue;
3438 const uint32_t sessionType =
3439 mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId);
3440 if ((sessionType & ThreadBase::EFFECT_SESSION) != 0) {
3441 ALOGE("%s: effect %s io %d denied because session %d effect exists on io %d",
3442 __func__, desc.name, (int)io, (int)sessionId, (int)checkIo);
3443 android_errorWriteLog(0x534e4554, "123237974");
3444 lStatus = BAD_VALUE;
3445 goto Exit;
3446 }
3447 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003448 }
3449 ThreadBase *thread = checkRecordThread_l(io);
3450 if (thread == NULL) {
3451 thread = checkPlaybackThread_l(io);
3452 if (thread == NULL) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08003453 thread = checkMmapThread_l(io);
3454 if (thread == NULL) {
3455 ALOGE("createEffect() unknown output thread");
3456 lStatus = BAD_VALUE;
3457 goto Exit;
3458 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003459 }
Eric Laurentaaa44472014-09-12 17:41:50 -07003460 } else {
3461 // Check if one effect chain was awaiting for an effect to be created on this
3462 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08003463 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07003464 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07003465 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07003466 thread->addEffectChain_l(chain);
3467 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003468 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003469
Eric Laurent021cf962014-05-13 10:18:14 -07003470 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003471
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003472 // create effect on selected output thread
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003473 bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003474 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003475 &desc, enabled, &lStatus, pinned);
haobo101735295ff7b0d2017-03-17 17:44:03 +08003476 if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003477 // remove local strong reference to Client with mClientLock held
3478 Mutex::Autolock _cl(mClientLock);
3479 client.clear();
haobo101735295ff7b0d2017-03-17 17:44:03 +08003480 } else {
3481 // handle must be valid here, but check again to be safe.
3482 if (handle.get() != nullptr && id != nullptr) *id = handle->id();
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003483 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003484 }
3485
Eric Laurent6c796322019-04-09 14:13:17 -07003486 if (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS) {
3487 // Check CPU and memory usage
3488 sp<EffectModule> effect = handle->effect().promote();
3489 if (effect != nullptr) {
3490 status_t rStatus = effect->updatePolicyState();
3491 if (rStatus != NO_ERROR) {
3492 lStatus = rStatus;
3493 }
3494 }
3495 } else {
haobo101735295ff7b0d2017-03-17 17:44:03 +08003496 handle.clear();
3497 }
3498
Mathias Agopian65ab4712010-07-14 17:59:35 -07003499Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07003500 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003501 return handle;
3502}
3503
Glenn Kastend848eb42016-03-08 13:42:11 -08003504status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003505 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07003506{
Steve Block3856b092011-10-20 11:56:00 +01003507 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07003508 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003509 Mutex::Autolock _l(mLock);
3510 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003511 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003512 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003513 }
Eric Laurentde070132010-07-13 04:45:46 -07003514 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
3515 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003516 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003517 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003518 }
Eric Laurentde070132010-07-13 04:45:46 -07003519 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
3520 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003521 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003522 return BAD_VALUE;
3523 }
3524
3525 Mutex::Autolock _dl(dstThread->mLock);
3526 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent6c796322019-04-09 14:13:17 -07003527 return moveEffectChain_l(sessionId, srcThread, dstThread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003528}
3529
Eric Laurentb20cf7d2019-04-05 19:37:34 -07003530
3531void AudioFlinger::setEffectSuspended(int effectId,
3532 audio_session_t sessionId,
3533 bool suspended)
3534{
3535 Mutex::Autolock _l(mLock);
3536
3537 sp<ThreadBase> thread = getEffectThread_l(sessionId, effectId);
3538 if (thread == nullptr) {
3539 return;
3540 }
3541 Mutex::Autolock _sl(thread->mLock);
3542 sp<EffectModule> effect = thread->getEffect_l(sessionId, effectId);
3543 thread->setEffectSuspended_l(&effect->desc().type, suspended, sessionId);
3544}
3545
3546
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003547// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08003548status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07003549 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent6c796322019-04-09 14:13:17 -07003550 AudioFlinger::PlaybackThread *dstThread)
Eric Laurentde070132010-07-13 04:45:46 -07003551{
Steve Block3856b092011-10-20 11:56:00 +01003552 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003553 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07003554
Eric Laurent59255e42011-07-27 19:49:51 -07003555 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003556 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003557 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003558 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07003559 return INVALID_OPERATION;
3560 }
3561
Eric Laurent4c415062016-06-17 16:14:16 -07003562 // Check whether the destination thread and all effects in the chain are compatible
3563 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07003564 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07003565 " destination thread %p is not compatible with effects in the chain",
3566 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07003567 return INVALID_OPERATION;
3568 }
3569
Eric Laurent39e94f82010-07-28 01:32:47 -07003570 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07003571 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07003572 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07003573 // removed.
3574 srcThread->removeEffectChain_l(chain);
3575
3576 // transfer all effects one by one so that new effect chain is created on new thread with
3577 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07003578 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003579 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07003580 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003581 Vector< sp<EffectModule> > removed;
3582 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07003583 while (effect != 0) {
3584 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003585 removed.add(effect);
3586 status = dstThread->addEffect_l(effect);
3587 if (status != NO_ERROR) {
3588 break;
3589 }
Eric Laurentec35a142011-10-05 17:42:25 -07003590 // removeEffect_l() has stopped the effect if it was active so it must be restarted
3591 if (effect->state() == EffectModule::ACTIVE ||
3592 effect->state() == EffectModule::STOPPING) {
3593 effect->start();
3594 }
Eric Laurent39e94f82010-07-28 01:32:47 -07003595 // if the move request is not received from audio policy manager, the effect must be
3596 // re-registered with the new strategy and output
3597 if (dstChain == 0) {
3598 dstChain = effect->chain().promote();
3599 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003600 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003601 status = NO_INIT;
3602 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07003603 }
3604 strategy = dstChain->strategy();
3605 }
Eric Laurentde070132010-07-13 04:45:46 -07003606 effect = chain->getEffectFromId_l(0);
3607 }
3608
Eric Laurent5baf2af2013-09-12 17:37:00 -07003609 if (status != NO_ERROR) {
3610 for (size_t i = 0; i < removed.size(); i++) {
3611 srcThread->addEffect_l(removed[i]);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003612 }
3613 }
3614
3615 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003616}
3617
Eric Laurent6c796322019-04-09 14:13:17 -07003618status_t AudioFlinger::moveAuxEffectToIo(int EffectId,
3619 const sp<PlaybackThread>& dstThread,
3620 sp<PlaybackThread> *srcThread)
3621{
3622 status_t status = NO_ERROR;
3623 Mutex::Autolock _l(mLock);
Eric Laurentb20cf7d2019-04-05 19:37:34 -07003624 sp<PlaybackThread> thread =
3625 static_cast<PlaybackThread *>(getEffectThread_l(AUDIO_SESSION_OUTPUT_MIX, EffectId).get());
Eric Laurent6c796322019-04-09 14:13:17 -07003626
3627 if (EffectId != 0 && thread != 0 && dstThread != thread.get()) {
3628 Mutex::Autolock _dl(dstThread->mLock);
3629 Mutex::Autolock _sl(thread->mLock);
3630 sp<EffectChain> srcChain = thread->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
3631 sp<EffectChain> dstChain;
3632 if (srcChain == 0) {
3633 return INVALID_OPERATION;
3634 }
3635
3636 sp<EffectModule> effect = srcChain->getEffectFromId_l(EffectId);
3637 if (effect == 0) {
3638 return INVALID_OPERATION;
3639 }
3640 thread->removeEffect_l(effect);
3641 status = dstThread->addEffect_l(effect);
3642 if (status != NO_ERROR) {
3643 thread->addEffect_l(effect);
3644 status = INVALID_OPERATION;
3645 goto Exit;
3646 }
3647
3648 dstChain = effect->chain().promote();
3649 if (dstChain == 0) {
3650 thread->addEffect_l(effect);
3651 status = INVALID_OPERATION;
3652 }
3653
3654Exit:
3655 // removeEffect_l() has stopped the effect if it was active so it must be restarted
3656 if (effect->state() == EffectModule::ACTIVE ||
3657 effect->state() == EffectModule::STOPPING) {
3658 effect->start();
3659 }
3660 }
3661
3662 if (status == NO_ERROR && srcThread != nullptr) {
3663 *srcThread = thread;
3664 }
3665 return status;
3666}
3667
Eric Laurent5baf2af2013-09-12 17:37:00 -07003668bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07003669{
3670 if (mGlobalEffectEnableTime != 0 &&
3671 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
3672 return true;
3673 }
3674
3675 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3676 sp<EffectChain> ec =
3677 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003678 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07003679 return true;
3680 }
3681 }
3682 return false;
3683}
3684
Eric Laurent5baf2af2013-09-12 17:37:00 -07003685void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07003686{
3687 Mutex::Autolock _l(mLock);
3688
3689 mGlobalEffectEnableTime = systemTime();
3690
3691 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3692 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
3693 if (t->mType == ThreadBase::OFFLOAD) {
3694 t->invalidateTracks(AUDIO_STREAM_MUSIC);
3695 }
3696 }
3697
3698}
3699
Eric Laurentaaa44472014-09-12 17:41:50 -07003700status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
3701{
Eric Laurentd8365c52017-07-16 15:27:05 -07003702 // clear possible suspended state before parking the chain so that it starts in default state
3703 // when attached to a new record thread
3704 chain->setEffectSuspended_l(FX_IID_AEC, false);
3705 chain->setEffectSuspended_l(FX_IID_NS, false);
3706
Glenn Kastend848eb42016-03-08 13:42:11 -08003707 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003708 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003709 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003710 if (index >= 0) {
3711 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
3712 return ALREADY_EXISTS;
3713 }
3714 mOrphanEffectChains.add(session, chain);
3715 return NO_ERROR;
3716}
3717
3718sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
3719{
3720 sp<EffectChain> chain;
3721 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003722 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003723 if (index >= 0) {
3724 chain = mOrphanEffectChains.valueAt(index);
3725 mOrphanEffectChains.removeItemsAt(index);
3726 }
3727 return chain;
3728}
3729
3730bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
3731{
3732 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08003733 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003734 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003735 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003736 if (index >= 0) {
3737 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003738 if (chain->removeEffect_l(effect, true) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003739 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003740 mOrphanEffectChains.removeItemsAt(index);
3741 }
3742 return true;
3743 }
3744 return false;
3745}
3746
3747
Mathias Agopian65ab4712010-07-14 17:59:35 -07003748// ----------------------------------------------------------------------------
3749
3750status_t AudioFlinger::onTransact(
3751 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3752{
3753 return BnAudioFlinger::onTransact(code, data, reply, flags);
3754}
3755
Glenn Kasten63238ef2015-03-02 15:50:29 -08003756} // namespace android