blob: eba0b20613e487782ace3087cb4aefeaf24fe8c7 [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
jiabina392a552019-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,
jiabin43810402019-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 &&
jiabin43810402019-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:
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700658 NBLog::Shared *sharedRawPtr = (NBLog::Shared *) shared->unsecurePointer();
Glenn Kasten535e1612016-12-05 12:19:36 -0800659 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 Rocard01c7d9e2019-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 Rocard01c7d9e2019-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
Eric Laurent5ada82e2019-08-29 17:53:54 -07001165void AudioFlinger::setRecordSilenced(audio_port_handle_t portId, bool silenced)
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001166{
Eric Laurent5ada82e2019-08-29 17:53:54 -07001167 ALOGV("AudioFlinger::setRecordSilenced(portId:%d, silenced:%d)", portId, silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001168
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001169 AutoMutex lock(mLock);
1170 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent5ada82e2019-08-29 17:53:54 -07001171 mRecordThreads[i]->setRecordSilenced(portId, silenced);
Eric Laurent331679c2018-04-16 17:03:16 -07001172 }
1173 for (size_t i = 0; i < mMmapThreads.size(); i++) {
Eric Laurent5ada82e2019-08-29 17:53:54 -07001174 mMmapThreads[i]->setRecordSilenced(portId, 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
Mikhail Naganovb261ef52018-07-16 13:34:38 -07001360// forwardAudioHwSyncToDownstreamPatches_l() must be called with AudioFlinger::mLock held
1361void AudioFlinger::forwardParametersToDownstreamPatches_l(
1362 audio_io_handle_t upStream, const String8& keyValuePairs,
1363 std::function<bool(const sp<PlaybackThread>&)> useThread)
1364{
1365 std::vector<PatchPanel::SoftwarePatch> swPatches;
1366 if (mPatchPanel.getDownstreamSoftwarePatches(upStream, &swPatches) != OK) return;
1367 ALOGV_IF(!swPatches.empty(), "%s found %zu downstream patches for stream ID %d",
1368 __func__, swPatches.size(), upStream);
1369 for (const auto& swPatch : swPatches) {
1370 sp<PlaybackThread> downStream = checkPlaybackThread_l(swPatch.getPlaybackThreadHandle());
1371 if (downStream != NULL && (useThread == nullptr || useThread(downStream))) {
1372 downStream->setParameters(keyValuePairs);
1373 }
1374 }
1375}
1376
Eric Laurentf1047e82018-04-16 19:18:20 -07001377// Filter reserved keys from setParameters() before forwarding to audio HAL or acting upon.
1378// Some keys are used for audio routing and audio path configuration and should be reserved for use
1379// by audio policy and audio flinger for functional, privacy and security reasons.
1380void AudioFlinger::filterReservedParameters(String8& keyValuePairs, uid_t callingUid)
1381{
1382 static const String8 kReservedParameters[] = {
1383 String8(AudioParameter::keyRouting),
1384 String8(AudioParameter::keySamplingRate),
1385 String8(AudioParameter::keyFormat),
1386 String8(AudioParameter::keyChannels),
1387 String8(AudioParameter::keyFrameCount),
1388 String8(AudioParameter::keyInputSource),
1389 String8(AudioParameter::keyMonoOutput),
Mikhail Naganovf23fcf62019-07-08 15:28:43 -07001390 String8(AudioParameter::keyDeviceConnect),
1391 String8(AudioParameter::keyDeviceDisconnect),
Eric Laurentf1047e82018-04-16 19:18:20 -07001392 String8(AudioParameter::keyStreamSupportedFormats),
1393 String8(AudioParameter::keyStreamSupportedChannels),
1394 String8(AudioParameter::keyStreamSupportedSamplingRates),
1395 };
1396
Andy Hung4ef19fa2018-05-15 19:35:29 -07001397 if (isAudioServerUid(callingUid)) {
1398 return; // no need to filter if audioserver.
Eric Laurentf1047e82018-04-16 19:18:20 -07001399 }
1400
1401 AudioParameter param = AudioParameter(keyValuePairs);
1402 String8 value;
Kevin Rocarda0a5d2a2018-08-06 15:03:18 -07001403 AudioParameter rejectedParam;
Eric Laurentf1047e82018-04-16 19:18:20 -07001404 for (auto& key : kReservedParameters) {
1405 if (param.get(key, value) == NO_ERROR) {
Kevin Rocarda0a5d2a2018-08-06 15:03:18 -07001406 rejectedParam.add(key, value);
Eric Laurentf1047e82018-04-16 19:18:20 -07001407 param.remove(key);
1408 }
1409 }
Kevin Rocarda0a5d2a2018-08-06 15:03:18 -07001410 logFilteredParameters(param.size() + rejectedParam.size(), keyValuePairs,
1411 rejectedParam.size(), rejectedParam.toString(), callingUid);
Eric Laurentf1047e82018-04-16 19:18:20 -07001412 keyValuePairs = param.toString();
1413}
1414
Kevin Rocarda0a5d2a2018-08-06 15:03:18 -07001415void AudioFlinger::logFilteredParameters(size_t originalKVPSize, const String8& originalKVPs,
1416 size_t rejectedKVPSize, const String8& rejectedKVPs,
1417 uid_t callingUid) {
1418 auto prefix = String8::format("UID %5d", callingUid);
1419 auto suffix = String8::format("%zu KVP received: %s", originalKVPSize, originalKVPs.c_str());
1420 if (rejectedKVPSize != 0) {
1421 auto error = String8::format("%zu KVP rejected: %s", rejectedKVPSize, rejectedKVPs.c_str());
1422 ALOGW("%s: %s, %s, %s", __func__, prefix.c_str(), error.c_str(), suffix.c_str());
1423 mRejectedSetParameterLog.log("%s, %s, %s", prefix.c_str(), error.c_str(), suffix.c_str());
1424 } else {
1425 auto& logger = (isServiceUid(callingUid) ? mSystemSetParameterLog : mAppSetParameterLog);
1426 logger.log("%s, %s", prefix.c_str(), suffix.c_str());
1427 }
1428}
1429
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001430status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001431{
Eric Laurentf1047e82018-04-16 19:18:20 -07001432 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d calling uid %d",
1433 ioHandle, keyValuePairs.string(),
1434 IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid());
Eric Laurent81784c32012-11-19 14:55:58 -08001435
Mathias Agopian65ab4712010-07-14 17:59:35 -07001436 // check calling permissions
1437 if (!settingsAllowed()) {
1438 return PERMISSION_DENIED;
1439 }
1440
Eric Laurentf1047e82018-04-16 19:18:20 -07001441 String8 filteredKeyValuePairs = keyValuePairs;
1442 filterReservedParameters(filteredKeyValuePairs, IPCThreadState::self()->getCallingUid());
1443
1444 ALOGV("%s: filtered keyvalue %s", __func__, filteredKeyValuePairs.string());
1445
Glenn Kasten142f5192014-03-25 17:44:59 -07001446 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1447 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001448 Mutex::Autolock _l(mLock);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001449 // result will remain NO_INIT if no audio device is present
1450 status_t final_result = NO_INIT;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001451 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001452 AutoMutex lock(mHardwareLock);
1453 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1454 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001455 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
Eric Laurentf1047e82018-04-16 19:18:20 -07001456 status_t result = dev->setParameters(filteredKeyValuePairs);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001457 // return success if at least one audio device accepts the parameters as not all
1458 // HALs are requested to support all parameters. If no audio device supports the
1459 // requested parameters, the last error is reported.
1460 if (final_result != NO_ERROR) {
1461 final_result = result;
1462 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001463 }
1464 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001465 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001466 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
Eric Laurentf1047e82018-04-16 19:18:20 -07001467 AudioParameter param = AudioParameter(filteredKeyValuePairs);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001468 String8 value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001469 if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) {
1470 bool btNrecIsOff = (value == AudioParameter::valueOff);
Eric Laurentd8365c52017-07-16 15:27:05 -07001471 if (mBtNrecIsOff.exchange(btNrecIsOff) != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001472 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentd8365c52017-07-16 15:27:05 -07001473 mRecordThreads.valueAt(i)->checkBtNrec();
Eric Laurent59bd0da2011-08-01 09:52:20 -07001474 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001475 }
1476 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001477 String8 screenState;
1478 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
Mikhail Naganov00260b52016-10-13 12:54:24 -07001479 bool isOff = (screenState == AudioParameter::valueOff);
Eric Laurent81784c32012-11-19 14:55:58 -08001480 if (isOff != (AudioFlinger::mScreenState & 1)) {
1481 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001482 }
1483 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001484 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485 }
1486
1487 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1488 // and the thread is exited once the lock is released
1489 sp<ThreadBase> thread;
1490 {
1491 Mutex::Autolock _l(mLock);
1492 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001493 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001494 thread = checkRecordThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001495 if (thread == 0) {
1496 thread = checkMmapThread_l(ioHandle);
1497 }
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001498 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001499 // indicate output device change to all input threads for pre processing
Eric Laurentf1047e82018-04-16 19:18:20 -07001500 AudioParameter param = AudioParameter(filteredKeyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001501 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001502 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1503 (value != 0)) {
Eric Laurentf1047e82018-04-16 19:18:20 -07001504 broacastParametersToRecordThreads_l(filteredKeyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001505 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001506 }
1507 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001508 if (thread != 0) {
Mikhail Naganovb261ef52018-07-16 13:34:38 -07001509 status_t result = thread->setParameters(filteredKeyValuePairs);
1510 forwardParametersToDownstreamPatches_l(thread->id(), filteredKeyValuePairs);
1511 return result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512 }
1513 return BAD_VALUE;
1514}
1515
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001516String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001517{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001518 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1519 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520
Eric Laurenta4c5a552012-03-29 10:12:40 -07001521 Mutex::Autolock _l(mLock);
1522
Glenn Kasten142f5192014-03-25 17:44:59 -07001523 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001524 String8 out_s8;
1525
Dima Zavin799a70e2011-04-18 16:57:27 -07001526 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001527 String8 s;
1528 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001529 {
1530 AutoMutex lock(mHardwareLock);
1531 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001532 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1533 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001534 mHardwareStatus = AUDIO_HW_IDLE;
1535 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001536 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001537 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001538 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001539 }
1540
Eric Laurent6acd1d42017-01-04 14:23:29 -08001541 ThreadBase *thread = (ThreadBase *)checkPlaybackThread_l(ioHandle);
1542 if (thread == NULL) {
1543 thread = (ThreadBase *)checkRecordThread_l(ioHandle);
1544 if (thread == NULL) {
1545 thread = (ThreadBase *)checkMmapThread_l(ioHandle);
1546 if (thread == NULL) {
Mikhail Naganovaa165e52017-07-12 10:39:16 -07001547 return String8("");
Eric Laurent6acd1d42017-01-04 14:23:29 -08001548 }
1549 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001550 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001551 return thread->getParameters(keys);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001552}
1553
Glenn Kastendd8104c2012-07-02 12:42:44 -07001554size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1555 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556{
Eric Laurenta1884f92011-08-23 08:25:03 -07001557 status_t ret = initCheck();
1558 if (ret != NO_ERROR) {
1559 return 0;
1560 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001561 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001562 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001563 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001564 return 0;
1565 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001566
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001567 AutoMutex lock(mHardwareLock);
1568 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001569 audio_config_t config, proposed;
1570 memset(&proposed, 0, sizeof(proposed));
1571 proposed.sample_rate = sampleRate;
1572 proposed.channel_mask = channelMask;
1573 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001574
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001575 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001576 size_t frames;
1577 for (;;) {
1578 // Note: config is currently a const parameter for get_input_buffer_size()
1579 // but we use a copy from proposed in case config changes from the call.
1580 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001581 status_t result = dev->getInputBufferSize(&config, &frames);
1582 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001583 break; // hal success, config is the result
1584 }
1585 // change one parameter of the configuration each iteration to a more "common" value
1586 // to see if the device will support it.
1587 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1588 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1589 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1590 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1591 } else {
1592 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1593 "format %#x, channelMask 0x%X",
1594 sampleRate, format, channelMask);
1595 break; // retries failed, break out of loop with frames == 0.
1596 }
1597 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001598 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001599 if (frames > 0 && config.sample_rate != sampleRate) {
1600 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1601 }
1602 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001603}
1604
Glenn Kasten5f972c02014-01-13 09:59:31 -08001605uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001606{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001607 Mutex::Autolock _l(mLock);
1608
1609 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1610 if (recordThread != NULL) {
1611 return recordThread->getInputFramesLost();
1612 }
1613 return 0;
1614}
1615
1616status_t AudioFlinger::setVoiceVolume(float value)
1617{
Eric Laurenta1884f92011-08-23 08:25:03 -07001618 status_t ret = initCheck();
1619 if (ret != NO_ERROR) {
1620 return ret;
1621 }
1622
Mathias Agopian65ab4712010-07-14 17:59:35 -07001623 // check calling permissions
1624 if (!settingsAllowed()) {
1625 return PERMISSION_DENIED;
1626 }
1627
1628 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001629 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001630 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001631 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001632 mHardwareStatus = AUDIO_HW_IDLE;
1633
1634 return ret;
1635}
1636
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001637status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001638 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001639{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001640 Mutex::Autolock _l(mLock);
1641
1642 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1643 if (playbackThread != NULL) {
1644 return playbackThread->getRenderPosition(halFrames, dspFrames);
1645 }
1646
1647 return BAD_VALUE;
1648}
1649
1650void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1651{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001652 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001653 if (client == 0) {
1654 return;
1655 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001656 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001657 {
1658 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001659 if (mNotificationClients.indexOfKey(pid) < 0) {
1660 sp<NotificationClient> notificationClient = new NotificationClient(this,
1661 client,
1662 pid);
1663 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001664
Eric Laurent021cf962014-05-13 10:18:14 -07001665 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001666
Marco Nelissen06b46062014-11-14 07:58:25 -08001667 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001668 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001669 }
1670 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001671
Eric Laurent021cf962014-05-13 10:18:14 -07001672 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001673 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001674 // the config change is always sent from playback or record threads to avoid deadlock
1675 // with AudioSystem::gLock
1676 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001677 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_REGISTERED, pid);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001678 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001679
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001680 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001681 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_REGISTERED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001682 }
1683}
1684
1685void AudioFlinger::removeNotificationClient(pid_t pid)
1686{
Eric Laurent6c796322019-04-09 14:13:17 -07001687 std::vector< sp<AudioFlinger::EffectModule> > removedEffects;
Eric Laurent021cf962014-05-13 10:18:14 -07001688 {
Eric Laurent6c796322019-04-09 14:13:17 -07001689 Mutex::Autolock _l(mLock);
1690 {
1691 Mutex::Autolock _cl(mClientLock);
1692 mNotificationClients.removeItem(pid);
1693 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001694
Eric Laurent6c796322019-04-09 14:13:17 -07001695 ALOGV("%d died, releasing its sessions", pid);
1696 size_t num = mAudioSessionRefs.size();
1697 bool removed = false;
1698 for (size_t i = 0; i < num; ) {
1699 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
1700 ALOGV(" pid %d @ %zu", ref->mPid, i);
1701 if (ref->mPid == pid) {
1702 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
1703 mAudioSessionRefs.removeAt(i);
1704 delete ref;
1705 removed = true;
1706 num--;
1707 } else {
1708 i++;
1709 }
1710 }
1711 if (removed) {
1712 removedEffects = purgeStaleEffects_l();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001713 }
1714 }
Eric Laurent6c796322019-04-09 14:13:17 -07001715 for (auto& effect : removedEffects) {
1716 effect->updatePolicyState();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001717 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001718}
1719
Eric Laurent73e26b62015-04-27 16:55:58 -07001720void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001721 const sp<AudioIoDescriptor>& ioDesc,
1722 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001723{
Eric Laurent021cf962014-05-13 10:18:14 -07001724 Mutex::Autolock _l(mClientLock);
1725 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001726 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001727 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1728 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1729 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001730 }
1731}
1732
Eric Laurent021cf962014-05-13 10:18:14 -07001733// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001734void AudioFlinger::removeClient_l(pid_t pid)
1735{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001736 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001737 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001738 mClients.removeItem(pid);
1739}
1740
Eric Laurent717e1282012-06-29 16:36:52 -07001741// getEffectThread_l() must be called with AudioFlinger::mLock held
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001742sp<AudioFlinger::ThreadBase> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1743 int effectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001744{
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001745 sp<ThreadBase> thread;
Eric Laurent717e1282012-06-29 16:36:52 -07001746
1747 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001748 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, effectId) != 0) {
Eric Laurent717e1282012-06-29 16:36:52 -07001749 ALOG_ASSERT(thread == 0);
1750 thread = mPlaybackThreads.valueAt(i);
1751 }
1752 }
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001753 if (thread != nullptr) {
1754 return thread;
1755 }
1756 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1757 if (mRecordThreads.valueAt(i)->getEffect(sessionId, effectId) != 0) {
1758 ALOG_ASSERT(thread == 0);
1759 thread = mRecordThreads.valueAt(i);
1760 }
1761 }
1762 if (thread != nullptr) {
1763 return thread;
1764 }
1765 for (size_t i = 0; i < mMmapThreads.size(); i++) {
1766 if (mMmapThreads.valueAt(i)->getEffect(sessionId, effectId) != 0) {
1767 ALOG_ASSERT(thread == 0);
1768 thread = mMmapThreads.valueAt(i);
1769 }
1770 }
Eric Laurent717e1282012-06-29 16:36:52 -07001771 return thread;
1772}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001773
Mathias Agopian65ab4712010-07-14 17:59:35 -07001774
Mathias Agopian65ab4712010-07-14 17:59:35 -07001775
1776// ----------------------------------------------------------------------------
1777
1778AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1779 : RefBase(),
1780 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001781 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001782{
Andy Hung6f248bb2018-01-23 14:04:37 -08001783 mMemoryDealer = new MemoryDealer(
1784 audioFlinger->getClientSharedHeapSize(),
1785 (std::string("AudioFlinger::Client(") + std::to_string(pid) + ")").c_str());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001786}
1787
Eric Laurent021cf962014-05-13 10:18:14 -07001788// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001789AudioFlinger::Client::~Client()
1790{
1791 mAudioFlinger->removeClient_l(mPid);
1792}
1793
Glenn Kasten435dbe62012-01-30 10:15:48 -08001794sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001795{
1796 return mMemoryDealer;
1797}
1798
1799// ----------------------------------------------------------------------------
1800
1801AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1802 const sp<IAudioFlingerClient>& client,
1803 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001804 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001805{
1806}
1807
1808AudioFlinger::NotificationClient::~NotificationClient()
1809{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001810}
1811
Glenn Kasten0f11b512014-01-31 16:18:54 -08001812void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001813{
1814 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001815 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001816}
1817
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001818// ----------------------------------------------------------------------------
1819AudioFlinger::MediaLogNotifier::MediaLogNotifier()
1820 : mPendingRequests(false) {}
1821
1822
1823void AudioFlinger::MediaLogNotifier::requestMerge() {
1824 AutoMutex _l(mMutex);
1825 mPendingRequests = true;
1826 mCond.signal();
1827}
1828
1829bool AudioFlinger::MediaLogNotifier::threadLoop() {
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001830 // Should already have been checked, but just in case
1831 if (sMediaLogService == 0) {
1832 return false;
1833 }
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001834 // Wait until there are pending requests
1835 {
1836 AutoMutex _l(mMutex);
1837 mPendingRequests = false; // to ignore past requests
1838 while (!mPendingRequests) {
1839 mCond.wait(mMutex);
1840 // TODO may also need an exitPending check
1841 }
1842 mPendingRequests = false;
1843 }
1844 // Execute the actual MediaLogService binder call and ignore extra requests for a while
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001845 sMediaLogService->requestMergeWakeup();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001846 usleep(kPostTriggerSleepPeriod);
1847 return true;
1848}
1849
1850void AudioFlinger::requestLogMerge() {
1851 mMediaLogNotifier->requestMerge();
1852}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001853
1854// ----------------------------------------------------------------------------
1855
Eric Laurentf14db3c2017-12-08 14:20:36 -08001856sp<media::IAudioRecord> AudioFlinger::createRecord(const CreateRecordInput& input,
1857 CreateRecordOutput& output,
1858 status_t *status)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001859{
1860 sp<RecordThread::RecordTrack> recordTrack;
1861 sp<RecordHandle> recordHandle;
1862 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001863 status_t lStatus;
Eric Laurentf14db3c2017-12-08 14:20:36 -08001864 audio_session_t sessionId = input.sessionId;
Eric Laurent77881cd2018-02-09 16:01:31 -08001865 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001866
Eric Laurentf14db3c2017-12-08 14:20:36 -08001867 output.cblk.clear();
1868 output.buffers.clear();
Eric Laurent896c9672017-12-08 14:08:45 -08001869 output.inputId = AUDIO_IO_HANDLE_NONE;
Glenn Kastend776ac62014-05-07 09:16:09 -07001870
Eric Laurentf14db3c2017-12-08 14:20:36 -08001871 bool updatePid = (input.clientInfo.clientPid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001872 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurentf14db3c2017-12-08 14:20:36 -08001873 uid_t clientUid = input.clientInfo.clientUid;
Andy Hung4ef19fa2018-05-15 19:35:29 -07001874 if (!isAudioServerOrMediaServerUid(callingUid)) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001875 ALOGW_IF(clientUid != callingUid,
1876 "%s uid %d tried to pass itself off as %d",
1877 __FUNCTION__, callingUid, clientUid);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001878 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001879 updatePid = true;
1880 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001881 pid_t clientPid = input.clientInfo.clientPid;
Eric Laurent09f1ed22019-04-24 17:45:17 -07001882 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001883 if (updatePid) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001884 ALOGW_IF(clientPid != -1 && clientPid != callingPid,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001885 "%s uid %d pid %d tried to pass itself off as pid %d",
Eric Laurentf14db3c2017-12-08 14:20:36 -08001886 __func__, callingUid, callingPid, clientPid);
1887 clientPid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001888 }
1889
Andy Hung6770c6f2015-04-07 13:43:36 -07001890 // we don't yet support anything other than linear PCM
Eric Laurentf14db3c2017-12-08 14:20:36 -08001891 if (!audio_is_valid_format(input.config.format) || !audio_is_linear_pcm(input.config.format)) {
1892 ALOGE("createRecord() invalid format %#x", input.config.format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001893 lStatus = BAD_VALUE;
1894 goto Exit;
1895 }
1896
Glenn Kasten53b5d092014-02-05 10:00:23 -08001897 // further channel mask checks are performed by createRecordTrack_l()
Eric Laurentf14db3c2017-12-08 14:20:36 -08001898 if (!audio_is_input_channel(input.config.channel_mask)) {
1899 ALOGE("createRecord() invalid channel mask %#x", input.config.channel_mask);
Glenn Kasten53b5d092014-02-05 10:00:23 -08001900 lStatus = BAD_VALUE;
1901 goto Exit;
1902 }
1903
Eric Laurentf14db3c2017-12-08 14:20:36 -08001904 if (sessionId == AUDIO_SESSION_ALLOCATE) {
1905 sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
1906 } else if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1907 lStatus = BAD_VALUE;
1908 goto Exit;
1909 }
1910
1911 output.sessionId = sessionId;
Eric Laurentf14db3c2017-12-08 14:20:36 -08001912 output.selectedDeviceId = input.selectedDeviceId;
1913 output.flags = input.flags;
1914
1915 client = registerPid(clientPid);
1916
1917 // Not a conventional loop, but a retry loop for at most two iterations total.
1918 // Try first maybe with FAST flag then try again without FAST flag if that fails.
1919 // Exits loop via break on no error of got exit on error
1920 // The sp<> references will be dropped when re-entering scope.
1921 // The lack of indentation is deliberate, to reduce code churn and ease merges.
1922 for (;;) {
Eric Laurent896c9672017-12-08 14:08:45 -08001923 // release previously opened input if retrying.
1924 if (output.inputId != AUDIO_IO_HANDLE_NONE) {
1925 recordTrack.clear();
Eric Laurentfee19762018-01-29 18:44:13 -08001926 AudioSystem::releaseInput(portId);
Eric Laurent896c9672017-12-08 14:08:45 -08001927 output.inputId = AUDIO_IO_HANDLE_NONE;
Yung Ti Su5fac9c62018-05-15 15:07:43 +08001928 output.selectedDeviceId = input.selectedDeviceId;
Eric Laurent77881cd2018-02-09 16:01:31 -08001929 portId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent896c9672017-12-08 14:08:45 -08001930 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001931 lStatus = AudioSystem::getInputForAttr(&input.attr, &output.inputId,
Mikhail Naganov2996f672019-04-18 12:29:59 -07001932 input.riid,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001933 sessionId,
1934 // FIXME compare to AudioTrack
1935 clientPid,
1936 clientUid,
Eric Laurentfee19762018-01-29 18:44:13 -08001937 input.opPackageName,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001938 &input.config,
1939 output.flags, &output.selectedDeviceId, &portId);
jiabinc1de2df2019-05-07 14:26:40 -07001940 if (lStatus != NO_ERROR) {
1941 ALOGE("createRecord() getInputForAttr return error %d", lStatus);
1942 goto Exit;
1943 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001944
Glenn Kasten05997e22014-03-13 15:08:33 -07001945 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001946 Mutex::Autolock _l(mLock);
Eric Laurentf14db3c2017-12-08 14:20:36 -08001947 RecordThread *thread = checkRecordThread_l(output.inputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948 if (thread == NULL) {
jiabinc1de2df2019-05-07 14:26:40 -07001949 ALOGE("createRecord() checkRecordThread_l failed, input handle %d", output.inputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950 lStatus = BAD_VALUE;
1951 goto Exit;
1952 }
1953
Eric Laurentf14db3c2017-12-08 14:20:36 -08001954 ALOGV("createRecord() lSessionId: %d input %d", sessionId, output.inputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955
Eric Laurentf14db3c2017-12-08 14:20:36 -08001956 output.sampleRate = input.config.sample_rate;
1957 output.frameCount = input.frameCount;
1958 output.notificationFrameCount = input.notificationFrameCount;
Glenn Kasten570f6332014-03-13 15:01:06 -07001959
Kevin Rocard1f564ac2018-03-29 13:53:10 -07001960 recordTrack = thread->createRecordTrack_l(client, input.attr, &output.sampleRate,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001961 input.config.format, input.config.channel_mask,
1962 &output.frameCount, sessionId,
1963 &output.notificationFrameCount,
Eric Laurent09f1ed22019-04-24 17:45:17 -07001964 callingPid, clientUid, &output.flags,
Eric Laurentf14db3c2017-12-08 14:20:36 -08001965 input.clientInfo.clientTid,
Jean-Michel Trividdf87ef2019-08-20 15:42:04 -07001966 &lStatus, portId,
1967 input.opPackageName);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001968 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001969
Eric Laurentf14db3c2017-12-08 14:20:36 -08001970 // lStatus == BAD_TYPE means FAST flag was rejected: request a new input from
1971 // audio policy manager without FAST constraint
1972 if (lStatus == BAD_TYPE) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001973 continue;
Eric Laurent1b928682014-10-02 19:41:47 -07001974 }
Eric Laurentf14db3c2017-12-08 14:20:36 -08001975
1976 if (lStatus != NO_ERROR) {
Eric Laurentf14db3c2017-12-08 14:20:36 -08001977 goto Exit;
1978 }
1979
1980 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1981 // session and move it to this thread.
1982 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
1983 if (chain != 0) {
1984 Mutex::Autolock _l(thread->mLock);
1985 thread->addEffectChain_l(chain);
1986 }
1987 break;
1988 }
1989 // End of retry loop.
1990 // The lack of indentation is deliberate, to reduce code churn and ease merges.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001991 }
Glenn Kastene198c362013-08-13 09:13:36 -07001992
Eric Laurentf14db3c2017-12-08 14:20:36 -08001993 output.cblk = recordTrack->getCblk();
1994 output.buffers = recordTrack->getBuffers();
Eric Laurent973db022018-11-20 14:54:31 -08001995 output.portId = portId;
Eric Laurentf14db3c2017-12-08 14:20:36 -08001996
1997 // return handle to client
1998 recordHandle = new RecordHandle(recordTrack);
1999
2000Exit:
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002001 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002002 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07002003 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002004 // Don't hold mClientLock when releasing the reference on the track as the
2005 // destructor will acquire it.
2006 {
2007 Mutex::Autolock _cl(mClientLock);
2008 client.clear();
2009 }
Eric Laurent896c9672017-12-08 14:08:45 -08002010 recordTrack.clear();
2011 if (output.inputId != AUDIO_IO_HANDLE_NONE) {
Eric Laurentfee19762018-01-29 18:44:13 -08002012 AudioSystem::releaseInput(portId);
Eric Laurent896c9672017-12-08 14:08:45 -08002013 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002014 }
2015
Glenn Kasten9156ef32013-08-06 15:39:08 -07002016 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002017 return recordHandle;
2018}
2019
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002020
2021
Mathias Agopian65ab4712010-07-14 17:59:35 -07002022// ----------------------------------------------------------------------------
2023
Eric Laurenta4c5a552012-03-29 10:12:40 -07002024audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
2025{
Eric Laurent44622db2014-08-01 19:00:33 -07002026 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07002027 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07002028 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07002029 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07002030 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002031 }
2032 Mutex::Autolock _l(mLock);
2033 return loadHwModule_l(name);
2034}
2035
2036// loadHwModule_l() must be called with AudioFlinger::mLock held
2037audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
2038{
2039 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
2040 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
2041 ALOGW("loadHwModule() module %s already loaded", name);
2042 return mAudioHwDevs.keyAt(i);
2043 }
2044 }
2045
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002046 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002047
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002048 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002049 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002050 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07002051 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002052 }
2053
2054 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002055 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07002056 mHardwareStatus = AUDIO_HW_IDLE;
2057 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002058 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07002059 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002060 }
2061
John Grossmanee578c02012-07-23 17:05:46 -07002062 // Check and cache this HAL's level of support for master mute and master
2063 // volume. If this is the first HAL opened, and it supports the get
2064 // methods, use the initial values provided by the HAL as the current
2065 // master mute and volume settings.
2066
2067 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
2068 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07002069 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07002070
2071 if (0 == mAudioHwDevs.size()) {
2072 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002073 float mv;
2074 if (OK == dev->getMasterVolume(&mv)) {
2075 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07002076 }
2077
2078 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002079 bool mm;
2080 if (OK == dev->getMasterMute(&mm)) {
2081 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07002082 }
2083 }
2084
Eric Laurenta4c5a552012-03-29 10:12:40 -07002085 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002086 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07002087 flags = static_cast<AudioHwDevice::Flags>(flags |
2088 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
2089 }
2090
2091 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002092 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07002093 flags = static_cast<AudioHwDevice::Flags>(flags |
2094 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
2095 }
2096
Eric Laurenta4c5a552012-03-29 10:12:40 -07002097 mHardwareStatus = AUDIO_HW_IDLE;
2098 }
Mikhail Naganov97373b02018-07-23 13:27:24 -07002099 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_MSD) == 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -07002100 // An MSD module is inserted before hardware modules in order to mix encoded streams.
2101 flags = static_cast<AudioHwDevice::Flags>(flags | AudioHwDevice::AHWD_IS_INSERT);
2102 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07002103
Glenn Kastena13cde92016-03-28 15:26:02 -07002104 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07002105 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07002106
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002107 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002108
2109 return handle;
2110
2111}
2112
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07002113// ----------------------------------------------------------------------------
2114
Glenn Kasten3b16c762012-11-14 08:44:39 -08002115uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07002116{
2117 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07002118 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07002119 return thread != NULL ? thread->sampleRate() : 0;
2120}
2121
Glenn Kastene33054e2012-11-14 12:54:39 -08002122size_t AudioFlinger::getPrimaryOutputFrameCount()
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->frameCountHAL() : 0;
2127}
2128
2129// ----------------------------------------------------------------------------
2130
Andy Hung6f248bb2018-01-23 14:04:37 -08002131status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07002132{
2133 uid_t uid = IPCThreadState::self()->getCallingUid();
Andy Hung4ef19fa2018-05-15 19:35:29 -07002134 if (!isAudioServerOrSystemServerUid(uid)) {
Glenn Kasten4182c4e2013-07-15 14:45:07 -07002135 return PERMISSION_DENIED;
2136 }
2137 Mutex::Autolock _l(mLock);
2138 if (mIsDeviceTypeKnown) {
2139 return INVALID_OPERATION;
2140 }
2141 mIsLowRamDevice = isLowRamDevice;
Andy Hung6f248bb2018-01-23 14:04:37 -08002142 mTotalMemory = totalMemory;
2143 // mIsLowRamDevice and mTotalMemory are obtained through ActivityManager;
2144 // see ActivityManager.isLowRamDevice() and ActivityManager.getMemoryInfo().
2145 // mIsLowRamDevice generally represent devices with less than 1GB of memory,
2146 // though actual setting is determined through device configuration.
2147 constexpr int64_t GB = 1024 * 1024 * 1024;
2148 mClientSharedHeapSize =
2149 isLowRamDevice ? kMinimumClientSharedHeapSizeBytes
2150 : mTotalMemory < 2 * GB ? 4 * kMinimumClientSharedHeapSizeBytes
2151 : mTotalMemory < 3 * GB ? 8 * kMinimumClientSharedHeapSizeBytes
2152 : mTotalMemory < 4 * GB ? 16 * kMinimumClientSharedHeapSizeBytes
2153 : 32 * kMinimumClientSharedHeapSizeBytes;
Glenn Kasten4182c4e2013-07-15 14:45:07 -07002154 mIsDeviceTypeKnown = true;
Andy Hung6f248bb2018-01-23 14:04:37 -08002155
2156 // TODO: Cache the client shared heap size in a persistent property.
2157 // It's possible that a native process or Java service or app accesses audioserver
2158 // after it is registered by system server, but before AudioService updates
2159 // the memory info. This would occur immediately after boot or an audioserver
2160 // crash and restore. Before update from AudioService, the client would get the
2161 // minimum heap size.
2162
2163 ALOGD("isLowRamDevice:%s totalMemory:%lld mClientSharedHeapSize:%zu",
2164 (isLowRamDevice ? "true" : "false"),
2165 (long long)mTotalMemory,
2166 mClientSharedHeapSize.load());
Glenn Kasten4182c4e2013-07-15 14:45:07 -07002167 return NO_ERROR;
2168}
2169
Andy Hung6f248bb2018-01-23 14:04:37 -08002170size_t AudioFlinger::getClientSharedHeapSize() const
2171{
2172 size_t heapSizeInBytes = property_get_int32("ro.af.client_heap_size_kbyte", 0) * 1024;
2173 if (heapSizeInBytes != 0) { // read-only property overrides all.
2174 return heapSizeInBytes;
2175 }
2176 return mClientSharedHeapSize;
2177}
2178
Mikhail Naganovdea53042018-04-26 13:10:21 -07002179status_t AudioFlinger::setAudioPortConfig(const struct audio_port_config *config)
2180{
2181 ALOGV(__func__);
2182
2183 audio_module_handle_t module;
2184 if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2185 module = config->ext.device.hw_module;
2186 } else {
2187 module = config->ext.mix.hw_module;
2188 }
2189
2190 Mutex::Autolock _l(mLock);
2191 ssize_t index = mAudioHwDevs.indexOfKey(module);
2192 if (index < 0) {
2193 ALOGW("%s() bad hw module %d", __func__, module);
2194 return BAD_VALUE;
2195 }
2196
2197 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(index);
2198 return audioHwDevice->hwDevice()->setAudioPortConfig(config);
2199}
2200
Eric Laurent93c3d412014-08-01 14:48:35 -07002201audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
2202{
2203 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07002204
2205 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
2206 if (index >= 0) {
2207 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
2208 mHwAvSyncIds.valueAt(index), sessionId);
2209 return mHwAvSyncIds.valueAt(index);
2210 }
2211
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002212 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07002213 if (dev == NULL) {
2214 return AUDIO_HW_SYNC_INVALID;
2215 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002216 String8 reply;
2217 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07002218 if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002219 param = AudioParameter(reply);
2220 }
Eric Laurentfa90e842014-10-17 18:12:31 -07002221
2222 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07002223 if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) {
Eric Laurentfa90e842014-10-17 18:12:31 -07002224 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
2225 return AUDIO_HW_SYNC_INVALID;
2226 }
2227
2228 // allow only one session for a given HW A/V sync ID.
2229 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
2230 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
2231 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
2232 value, mHwAvSyncIds.keyAt(i));
2233 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07002234 break;
2235 }
2236 }
Eric Laurentfa90e842014-10-17 18:12:31 -07002237
2238 mHwAvSyncIds.add(sessionId, value);
2239
2240 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2241 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
2242 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07002243 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07002244 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07002245 param.addInt(String8(AudioParameter::keyStreamHwAvSync), value);
Mikhail Naganovb261ef52018-07-16 13:34:38 -07002246 String8 keyValuePairs = param.toString();
2247 thread->setParameters(keyValuePairs);
2248 forwardParametersToDownstreamPatches_l(thread->id(), keyValuePairs,
2249 [](const sp<PlaybackThread>& thread) { return thread->usesHwAvSync(); });
Eric Laurentfa90e842014-10-17 18:12:31 -07002250 break;
2251 }
2252 }
2253
2254 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
2255 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07002256}
2257
Eric Laurent72e3f392015-05-20 14:43:50 -07002258status_t AudioFlinger::systemReady()
2259{
2260 Mutex::Autolock _l(mLock);
2261 ALOGI("%s", __FUNCTION__);
2262 if (mSystemReady) {
2263 ALOGW("%s called twice", __FUNCTION__);
2264 return NO_ERROR;
2265 }
2266 mSystemReady = true;
2267 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2268 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
2269 thread->systemReady();
2270 }
2271 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2272 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
2273 thread->systemReady();
2274 }
2275 return NO_ERROR;
2276}
2277
jiabin46a76fa2018-01-05 10:18:21 -08002278status_t AudioFlinger::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
2279{
jiabin9ff780e2018-03-19 18:19:52 -07002280 AutoMutex lock(mHardwareLock);
2281 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
2282 status_t status = dev->getMicrophones(microphones);
2283 return status;
jiabin46a76fa2018-01-05 10:18:21 -08002284}
2285
Eric Laurentfa90e842014-10-17 18:12:31 -07002286// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
2287void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
2288{
2289 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
2290 if (index >= 0) {
2291 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
2292 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
2293 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07002294 param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId);
Mikhail Naganovb261ef52018-07-16 13:34:38 -07002295 String8 keyValuePairs = param.toString();
2296 thread->setParameters(keyValuePairs);
2297 forwardParametersToDownstreamPatches_l(thread->id(), keyValuePairs,
2298 [](const sp<PlaybackThread>& thread) { return thread->usesHwAvSync(); });
Eric Laurentfa90e842014-10-17 18:12:31 -07002299 }
2300}
2301
2302
Glenn Kasten4182c4e2013-07-15 14:45:07 -07002303// ----------------------------------------------------------------------------
2304
Eric Laurent83b88082014-06-20 18:31:16 -07002305
Eric Laurent6acd1d42017-01-04 14:23:29 -08002306sp<AudioFlinger::ThreadBase> AudioFlinger::openOutput_l(audio_module_handle_t module,
jiabin43810402019-10-24 14:58:31 -07002307 audio_io_handle_t *output,
2308 audio_config_t *config,
2309 audio_devices_t deviceType,
2310 const String8& address,
2311 audio_output_flags_t flags)
Eric Laurent83b88082014-06-20 18:31:16 -07002312{
jiabin43810402019-10-24 14:58:31 -07002313 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, deviceType);
Eric Laurent83b88082014-06-20 18:31:16 -07002314 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002315 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07002316 }
2317
Eric Laurentcf2c0212014-07-25 16:20:43 -07002318 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002319 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
2320 } else {
2321 // Audio Policy does not currently request a specific output handle.
2322 // If this is ever needed, see openInput_l() for example code.
2323 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
2324 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002325 }
Eric Laurent83b88082014-06-20 18:31:16 -07002326
2327 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
2328
Eric Laurent83b88082014-06-20 18:31:16 -07002329 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07002330 // This if statement allows overriding the audio policy settings
2331 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
2332 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
2333 // Check only for Normal Mixing mode
2334 if (kEnableExtendedPrecision) {
2335 // Specify format (uncomment one below to choose)
2336 //config->format = AUDIO_FORMAT_PCM_FLOAT;
2337 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
2338 //config->format = AUDIO_FORMAT_PCM_32_BIT;
2339 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002340 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07002341 }
2342 if (kEnableExtendedChannels) {
2343 // Specify channel mask (uncomment one below to choose)
2344 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
2345 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
2346 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
2347 }
Eric Laurent83b88082014-06-20 18:31:16 -07002348 }
2349
Phil Burk062e67a2015-02-11 13:40:50 -08002350 AudioStreamOut *outputStream = NULL;
2351 status_t status = outHwDev->openOutputStream(
2352 &outputStream,
2353 *output,
jiabin43810402019-10-24 14:58:31 -07002354 deviceType,
Phil Burk062e67a2015-02-11 13:40:50 -08002355 flags,
2356 config,
2357 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07002358
2359 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07002360
Phil Burk062e67a2015-02-11 13:40:50 -08002361 if (status == NO_ERROR) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002362 if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
2363 sp<MmapPlaybackThread> thread =
2364 new MmapPlaybackThread(this, *output, outHwDev, outputStream,
jiabin43810402019-10-24 14:58:31 -07002365 deviceType, AUDIO_DEVICE_NONE, mSystemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002366 mMmapThreads.add(*output, thread);
2367 ALOGV("openOutput_l() created mmap playback thread: ID %d thread %p",
2368 *output, thread.get());
2369 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07002370 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002371 sp<PlaybackThread> thread;
2372 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
jiabin43810402019-10-24 14:58:31 -07002373 thread = new OffloadThread(this, outputStream, *output, deviceType, mSystemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002374 ALOGV("openOutput_l() created offload output: ID %d thread %p",
2375 *output, thread.get());
2376 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
2377 || !isValidPcmSinkFormat(config->format)
2378 || !isValidPcmSinkChannelMask(config->channel_mask)) {
jiabin43810402019-10-24 14:58:31 -07002379 thread = new DirectOutputThread(
2380 this, outputStream, *output, deviceType, mSystemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002381 ALOGV("openOutput_l() created direct output: ID %d thread %p",
2382 *output, thread.get());
2383 } else {
jiabin43810402019-10-24 14:58:31 -07002384 thread = new MixerThread(this, outputStream, *output, deviceType, mSystemReady);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002385 ALOGV("openOutput_l() created mixer output: ID %d thread %p",
2386 *output, thread.get());
2387 }
2388 mPlaybackThreads.add(*output, thread);
Mikhail Naganovadca70f2018-07-09 12:49:25 -07002389 mPatchPanel.notifyStreamOpened(outHwDev, *output);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002390 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07002391 }
Eric Laurent83b88082014-06-20 18:31:16 -07002392 }
2393
2394 return 0;
2395}
2396
Eric Laurentcf2c0212014-07-25 16:20:43 -07002397status_t AudioFlinger::openOutput(audio_module_handle_t module,
2398 audio_io_handle_t *output,
2399 audio_config_t *config,
jiabin43810402019-10-24 14:58:31 -07002400 const sp<DeviceDescriptorBase>& device,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002401 uint32_t *latencyMs,
2402 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002403{
jiabin43810402019-10-24 14:58:31 -07002404 ALOGI("openOutput() this %p, module %d Device %s, SamplingRate %d, Format %#08x, "
Glenn Kasten49f36ba2017-12-06 13:02:02 -08002405 "Channels %#x, flags %#x",
Eric Laurent6acd1d42017-01-04 14:23:29 -08002406 this, module,
jiabin43810402019-10-24 14:58:31 -07002407 device->toString().c_str(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07002408 config->sample_rate,
2409 config->format,
2410 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002411 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002412
jiabin43810402019-10-24 14:58:31 -07002413 audio_devices_t deviceType = device->type();
2414 const String8 address = String8(device->address().c_str());
2415
2416 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002417 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002418 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002419
Mathias Agopian65ab4712010-07-14 17:59:35 -07002420 Mutex::Autolock _l(mLock);
2421
jiabin43810402019-10-24 14:58:31 -07002422 sp<ThreadBase> thread = openOutput_l(module, output, config, deviceType, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002423 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002424 if ((flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) {
2425 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
2426 *latencyMs = playbackThread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002427
Eric Laurent6acd1d42017-01-04 14:23:29 -08002428 // notify client processes of the new output creation
2429 playbackThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002430
Eric Laurent6acd1d42017-01-04 14:23:29 -08002431 // the first primary output opened designates the primary hw device
2432 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -08002433 ALOGI("Using module %d as the primary audio interface", module);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002434 mPrimaryHardwareDev = playbackThread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002435
Eric Laurent6acd1d42017-01-04 14:23:29 -08002436 AutoMutex lock(mHardwareLock);
2437 mHardwareStatus = AUDIO_HW_SET_MODE;
2438 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
2439 mHardwareStatus = AUDIO_HW_IDLE;
2440 }
2441 } else {
2442 MmapThread *mmapThread = (MmapThread *)thread.get();
2443 mmapThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002444 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002445 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002446 }
2447
Eric Laurentcf2c0212014-07-25 16:20:43 -07002448 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002449}
2450
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002451audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
2452 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002453{
2454 Mutex::Autolock _l(mLock);
2455 MixerThread *thread1 = checkMixerThread_l(output1);
2456 MixerThread *thread2 = checkMixerThread_l(output2);
2457
2458 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002459 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
2460 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07002461 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002462 }
2463
Glenn Kasteneeecb982016-02-26 10:44:04 -08002464 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07002465 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002466 thread->addOutputTrack(thread2);
2467 mPlaybackThreads.add(id, thread);
2468 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002469 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002470 return id;
2471}
2472
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002473status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002474{
Glenn Kastend96c5722012-04-25 13:44:49 -07002475 return closeOutput_nonvirtual(output);
2476}
2477
2478status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
2479{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002480 // keep strong reference on the playback thread so that
2481 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002482 sp<PlaybackThread> playbackThread;
2483 sp<MmapPlaybackThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002484 {
2485 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002486 playbackThread = checkPlaybackThread_l(output);
2487 if (playbackThread != NULL) {
2488 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002489
Andy Hungdc099c22018-09-18 13:46:39 -07002490 dumpToThreadLog_l(playbackThread);
Andy Hunga8115dc2018-08-24 15:51:59 -07002491
Eric Laurent6acd1d42017-01-04 14:23:29 -08002492 if (playbackThread->type() == ThreadBase::MIXER) {
2493 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2494 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
2495 DuplicatingThread *dupThread =
2496 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
2497 dupThread->removeOutputTrack((MixerThread *)playbackThread.get());
2498 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002499 }
2500 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002501
2502
Eric Laurent6acd1d42017-01-04 14:23:29 -08002503 mPlaybackThreads.removeItem(output);
2504 // save all effects to the default thread
2505 if (mPlaybackThreads.size()) {
2506 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
2507 if (dstThread != NULL) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002508 // audioflinger lock is held so order of thread lock acquisition doesn't matter
Eric Laurent6acd1d42017-01-04 14:23:29 -08002509 Mutex::Autolock _dl(dstThread->mLock);
2510 Mutex::Autolock _sl(playbackThread->mLock);
2511 Vector< sp<EffectChain> > effectChains = playbackThread->getEffectChains_l();
2512 for (size_t i = 0; i < effectChains.size(); i ++) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002513 moveEffectChain_l(effectChains[i]->sessionId(), playbackThread.get(),
Eric Laurent6c796322019-04-09 14:13:17 -07002514 dstThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002515 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002516 }
2517 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002518 } else {
2519 mmapThread = (MmapPlaybackThread *)checkMmapThread_l(output);
2520 if (mmapThread == 0) {
2521 return BAD_VALUE;
2522 }
Andy Hungdc099c22018-09-18 13:46:39 -07002523 dumpToThreadLog_l(mmapThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002524 mMmapThreads.removeItem(output);
Phil Burk7f6b40d2017-02-09 13:18:38 -08002525 ALOGD("closing mmapThread %p", mmapThread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002526 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002527 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2528 ioDesc->mIoHandle = output;
2529 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mikhail Naganovadca70f2018-07-09 12:49:25 -07002530 mPatchPanel.notifyStreamClosed(output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002531 }
Glenn Kastenb28686f2012-01-06 08:39:38 -08002532 // The thread entity (active unit of execution) is no longer running here,
2533 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002534
Eric Laurent6acd1d42017-01-04 14:23:29 -08002535 if (playbackThread != 0) {
2536 playbackThread->exit();
2537 if (!playbackThread->isDuplicating()) {
2538 closeOutputFinish(playbackThread);
2539 }
2540 } else if (mmapThread != 0) {
Phil Burk7f6b40d2017-02-09 13:18:38 -08002541 ALOGD("mmapThread exit()");
Eric Laurent6acd1d42017-01-04 14:23:29 -08002542 mmapThread->exit();
2543 AudioStreamOut *out = mmapThread->clearOutput();
2544 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2545 // from now on thread->mOutput is NULL
2546 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002547 }
2548 return NO_ERROR;
2549}
2550
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002551void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002552{
2553 AudioStreamOut *out = thread->clearOutput();
2554 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2555 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07002556 delete out;
2557}
2558
Mikhail Naganov444ecc32018-05-01 17:40:05 -07002559void AudioFlinger::closeThreadInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002560{
2561 mPlaybackThreads.removeItem(thread->mId);
2562 thread->exit();
2563 closeOutputFinish(thread);
2564}
2565
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002566status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002567{
2568 Mutex::Autolock _l(mLock);
2569 PlaybackThread *thread = checkPlaybackThread_l(output);
2570
2571 if (thread == NULL) {
2572 return BAD_VALUE;
2573 }
2574
Steve Block3856b092011-10-20 11:56:00 +01002575 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002576 thread->suspend();
2577
2578 return NO_ERROR;
2579}
2580
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002581status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002582{
2583 Mutex::Autolock _l(mLock);
2584 PlaybackThread *thread = checkPlaybackThread_l(output);
2585
2586 if (thread == NULL) {
2587 return BAD_VALUE;
2588 }
2589
Steve Block3856b092011-10-20 11:56:00 +01002590 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002591
2592 thread->restore();
2593
2594 return NO_ERROR;
2595}
2596
Eric Laurentcf2c0212014-07-25 16:20:43 -07002597status_t AudioFlinger::openInput(audio_module_handle_t module,
2598 audio_io_handle_t *input,
2599 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002600 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002601 const String8& address,
2602 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002603 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002604{
Eric Laurent83b88082014-06-20 18:31:16 -07002605 Mutex::Autolock _l(mLock);
2606
Glenn Kastene7d66712015-03-05 13:46:52 -08002607 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002608 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002609 }
2610
Mikhail Naganovb4e037e2019-01-14 15:56:33 -08002611 sp<ThreadBase> thread = openInput_l(
2612 module, input, config, *devices, address, source, flags, AUDIO_DEVICE_NONE, String8{});
Eric Laurent83b88082014-06-20 18:31:16 -07002613
2614 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002615 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002616 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002617 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002618 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002619 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002620}
Dima Zavin799a70e2011-04-18 16:57:27 -07002621
Eric Laurent6acd1d42017-01-04 14:23:29 -08002622sp<AudioFlinger::ThreadBase> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002623 audio_io_handle_t *input,
2624 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002625 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002626 const String8& address,
2627 audio_source_t source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -08002628 audio_input_flags_t flags,
2629 audio_devices_t outputDevice,
2630 const String8& outputDeviceAddress)
Eric Laurent83b88082014-06-20 18:31:16 -07002631{
Glenn Kastene7d66712015-03-05 13:46:52 -08002632 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002633 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002634 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002635 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002636 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002637
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07002638 // Some flags are specific to framework and must not leak to the HAL.
2639 flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FRAMEWORK_FLAGS);
2640
Glenn Kasteneeecb982016-02-26 10:44:04 -08002641 // Audio Policy can request a specific handle for hardware hotword.
2642 // The goal here is not to re-open an already opened input.
2643 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002644 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002645 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2646 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2647 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2648 return 0;
2649 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2650 // This should not happen in a transient state with current design.
2651 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2652 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002653 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002654
Eric Laurentcf2c0212014-07-25 16:20:43 -07002655 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002656 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002657 sp<StreamInHalInterface> inStream;
2658 status_t status = inHwHal->openInputStream(
Mikhail Naganovb4e037e2019-01-14 15:56:33 -08002659 *input, devices, &halconfig, flags, address.string(), source,
2660 outputDevice, outputDeviceAddress, &inStream);
Glenn Kasten49f36ba2017-12-06 13:02:02 -08002661 ALOGV("openInput_l() openInputStream returned input %p, devices %#x, SamplingRate %d"
2662 ", Format %#x, Channels %#x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002663 inStream.get(),
Mikhail Naganovf558e022016-11-14 17:45:17 -08002664 devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002665 halconfig.sample_rate,
2666 halconfig.format,
2667 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002668 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002669 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002670
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002671 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002672 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002673 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002674 audio_is_linear_pcm(config->format) &&
2675 audio_is_linear_pcm(halconfig.format) &&
2676 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002677 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2678 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002679 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002680 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002681 inStream.clear();
2682 status = inHwHal->openInputStream(
Mikhail Naganovb4e037e2019-01-14 15:56:33 -08002683 *input, devices, &halconfig, flags, address.string(), source,
2684 outputDevice, outputDeviceAddress, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002685 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002686 }
2687
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002688 if (status == NO_ERROR && inStream != 0) {
Eric Laurent05067782016-06-01 18:27:28 -07002689 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002690 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) {
2691 sp<MmapCaptureThread> thread =
2692 new MmapCaptureThread(this, *input,
2693 inHwDev, inputStream,
2694 primaryOutputDevice_l(), devices, mSystemReady);
2695 mMmapThreads.add(*input, thread);
Glenn Kastend3bb6452016-12-05 18:14:37 -08002696 ALOGV("openInput_l() created mmap capture thread: ID %d thread %p", *input,
2697 thread.get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002698 return thread;
2699 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002700 // Start record thread
2701 // RecordThread requires both input and output device indication to forward to audio
2702 // pre processing modules
2703 sp<RecordThread> thread = new RecordThread(this,
2704 inputStream,
2705 *input,
2706 primaryOutputDevice_l(),
2707 devices,
2708 mSystemReady
Eric Laurent6acd1d42017-01-04 14:23:29 -08002709 );
2710 mRecordThreads.add(*input, thread);
2711 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
2712 return thread;
2713 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002714 }
2715
Eric Laurentcf2c0212014-07-25 16:20:43 -07002716 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002717 return 0;
2718}
2719
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002720status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002721{
Glenn Kastend96c5722012-04-25 13:44:49 -07002722 return closeInput_nonvirtual(input);
2723}
2724
2725status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2726{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002727 // keep strong reference on the record thread so that
2728 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002729 sp<RecordThread> recordThread;
2730 sp<MmapCaptureThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002731 {
2732 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002733 recordThread = checkRecordThread_l(input);
2734 if (recordThread != 0) {
2735 ALOGV("closeInput() %d", input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002736
Andy Hungdc099c22018-09-18 13:46:39 -07002737 dumpToThreadLog_l(recordThread);
Andy Hung0264e7d2018-09-17 19:30:46 -07002738
Eric Laurent6acd1d42017-01-04 14:23:29 -08002739 // If we still have effect chains, it means that a client still holds a handle
2740 // on at least one effect. We must either move the chain to an existing thread with the
2741 // same session ID or put it aside in case a new record thread is opened for a
2742 // new capture on the same session
2743 sp<EffectChain> chain;
2744 {
2745 Mutex::Autolock _sl(recordThread->mLock);
2746 Vector< sp<EffectChain> > effectChains = recordThread->getEffectChains_l();
2747 // Note: maximum one chain per record thread
2748 if (effectChains.size() != 0) {
2749 chain = effectChains[0];
Eric Laurent1b928682014-10-02 19:41:47 -07002750 }
2751 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002752 if (chain != 0) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002753 // first check if a record thread is already opened with a client on same session.
Eric Laurent6acd1d42017-01-04 14:23:29 -08002754 // This should only happen in case of overlap between one thread tear down and the
2755 // creation of its replacement
2756 size_t i;
2757 for (i = 0; i < mRecordThreads.size(); i++) {
2758 sp<RecordThread> t = mRecordThreads.valueAt(i);
2759 if (t == recordThread) {
2760 continue;
2761 }
2762 if (t->hasAudioSession(chain->sessionId()) != 0) {
2763 Mutex::Autolock _l(t->mLock);
2764 ALOGV("closeInput() found thread %d for effect session %d",
2765 t->id(), chain->sessionId());
2766 t->addEffectChain_l(chain);
2767 break;
2768 }
2769 }
Glenn Kastend3bb6452016-12-05 18:14:37 -08002770 // put the chain aside if we could not find a record thread with the same session id
Eric Laurent6acd1d42017-01-04 14:23:29 -08002771 if (i == mRecordThreads.size()) {
2772 putOrphanEffectChain_l(chain);
2773 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002774 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002775 mRecordThreads.removeItem(input);
2776 } else {
2777 mmapThread = (MmapCaptureThread *)checkMmapThread_l(input);
2778 if (mmapThread == 0) {
2779 return BAD_VALUE;
2780 }
Andy Hungdc099c22018-09-18 13:46:39 -07002781 dumpToThreadLog_l(mmapThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002782 mMmapThreads.removeItem(input);
Eric Laurentaaa44472014-09-12 17:41:50 -07002783 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002784 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2785 ioDesc->mIoHandle = input;
2786 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002787 }
Eric Laurent83b88082014-06-20 18:31:16 -07002788 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2789 // we have a different lock for notification client
Eric Laurent6acd1d42017-01-04 14:23:29 -08002790 if (recordThread != 0) {
2791 closeInputFinish(recordThread);
2792 } else if (mmapThread != 0) {
2793 mmapThread->exit();
2794 AudioStreamIn *in = mmapThread->clearInput();
2795 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
2796 // from now on thread->mInput is NULL
2797 delete in;
2798 }
Eric Laurent83b88082014-06-20 18:31:16 -07002799 return NO_ERROR;
2800}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002801
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002802void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002803{
2804 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002805 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002806 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002807 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002808 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002809}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002810
Mikhail Naganov444ecc32018-05-01 17:40:05 -07002811void AudioFlinger::closeThreadInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002812{
2813 mRecordThreads.removeItem(thread->mId);
2814 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002815}
2816
Glenn Kastend2304db2014-02-03 07:40:31 -08002817status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002818{
2819 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002820 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002821
2822 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2823 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002824 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002825 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002826 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2827 mMmapThreads[i]->invalidateTracks(stream);
2828 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002829 return NO_ERROR;
2830}
2831
2832
Glenn Kasteneeecb982016-02-26 10:44:04 -08002833audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002834{
Glenn Kasten9d003132016-04-06 14:38:09 -07002835 // This is a binder API, so a malicious client could pass in a bad parameter.
2836 // Check for that before calling the internal API nextUniqueId().
2837 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2838 ALOGE("newAudioUniqueId invalid use %d", use);
2839 return AUDIO_UNIQUE_ID_ALLOCATE;
2840 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002841 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002842}
2843
Glenn Kastend848eb42016-03-08 13:42:11 -08002844void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002845{
2846 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002847 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002848 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
Andy Hung4ef19fa2018-05-15 19:35:29 -07002849 const uid_t callerUid = IPCThreadState::self()->getCallingUid();
2850 if (pid != -1 && isAudioServerUid(callerUid)) { // check must match releaseAudioSessionId()
Marco Nelissend457c972014-02-11 08:47:07 -08002851 caller = pid;
2852 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002853
Eric Laurent021cf962014-05-13 10:18:14 -07002854 {
2855 Mutex::Autolock _cl(mClientLock);
2856 // Ignore requests received from processes not known as notification client. The request
2857 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2858 // called from a different pid leaving a stale session reference. Also we don't know how
2859 // to clear this reference if the client process dies.
2860 if (mNotificationClients.indexOfKey(caller) < 0) {
2861 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2862 return;
2863 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002864 }
2865
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002866 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002867 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002868 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002869 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2870 ref->mCnt++;
2871 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002872 return;
2873 }
2874 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002875 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2876 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002877}
2878
Glenn Kastend848eb42016-03-08 13:42:11 -08002879void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002880{
Eric Laurent6c796322019-04-09 14:13:17 -07002881 std::vector< sp<EffectModule> > removedEffects;
2882 {
2883 Mutex::Autolock _l(mLock);
2884 pid_t caller = IPCThreadState::self()->getCallingPid();
2885 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2886 const uid_t callerUid = IPCThreadState::self()->getCallingUid();
2887 if (pid != -1 && isAudioServerUid(callerUid)) { // check must match acquireAudioSessionId()
2888 caller = pid;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002889 }
Eric Laurent6c796322019-04-09 14:13:17 -07002890 size_t num = mAudioSessionRefs.size();
2891 for (size_t i = 0; i < num; i++) {
2892 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2893 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2894 ref->mCnt--;
2895 ALOGV(" decremented refcount to %d", ref->mCnt);
2896 if (ref->mCnt == 0) {
2897 mAudioSessionRefs.removeAt(i);
2898 delete ref;
2899 std::vector< sp<EffectModule> > effects = purgeStaleEffects_l();
2900 removedEffects.insert(removedEffects.end(), effects.begin(), effects.end());
2901 }
2902 goto Exit;
2903 }
2904 }
2905 // If the caller is audioserver it is likely that the session being released was acquired
2906 // on behalf of a process not in notification clients and we ignore the warning.
2907 ALOGW_IF(!isAudioServerUid(callerUid),
2908 "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002909 }
Eric Laurent6c796322019-04-09 14:13:17 -07002910
2911Exit:
2912 for (auto& effect : removedEffects) {
2913 effect->updatePolicyState();
2914 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002915}
2916
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002917bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
2918{
2919 size_t num = mAudioSessionRefs.size();
2920 for (size_t i = 0; i < num; i++) {
2921 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2922 if (ref->mSessionid == audioSession) {
2923 return true;
2924 }
2925 }
2926 return false;
2927}
2928
Eric Laurent6c796322019-04-09 14:13:17 -07002929std::vector<sp<AudioFlinger::EffectModule>> AudioFlinger::purgeStaleEffects_l() {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002930
Steve Block3856b092011-10-20 11:56:00 +01002931 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002932
2933 Vector< sp<EffectChain> > chains;
Eric Laurent6c796322019-04-09 14:13:17 -07002934 std::vector< sp<EffectModule> > removedEffects;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002935
2936 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2937 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
Mateusz Kaplon2a6e1b02017-03-30 16:50:50 +02002938 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002939 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2940 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002941 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2942 chains.push(ec);
2943 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002944 }
2945 }
Eric Laurent6c796322019-04-09 14:13:17 -07002946
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002947 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2948 sp<RecordThread> t = mRecordThreads.valueAt(i);
Mateusz Kaplon2a6e1b02017-03-30 16:50:50 +02002949 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002950 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2951 sp<EffectChain> ec = t->mEffectChains[j];
2952 chains.push(ec);
2953 }
2954 }
2955
Eric Laurent6c796322019-04-09 14:13:17 -07002956 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2957 sp<MmapThread> t = mMmapThreads.valueAt(i);
2958 Mutex::Autolock _l(t->mLock);
2959 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2960 sp<EffectChain> ec = t->mEffectChains[j];
2961 chains.push(ec);
2962 }
2963 }
2964
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002965 for (size_t i = 0; i < chains.size(); i++) {
2966 sp<EffectChain> ec = chains[i];
2967 int sessionid = ec->sessionId();
2968 sp<ThreadBase> t = ec->mThread.promote();
2969 if (t == 0) {
2970 continue;
2971 }
2972 size_t numsessionrefs = mAudioSessionRefs.size();
2973 bool found = false;
2974 for (size_t k = 0; k < numsessionrefs; k++) {
2975 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002976 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002977 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002978 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002979 found = true;
2980 break;
2981 }
2982 }
2983 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002984 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002985 // remove all effects from the chain
2986 while (ec->mEffects.size()) {
2987 sp<EffectModule> effect = ec->mEffects[0];
2988 effect->unPin();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07002989 t->removeEffect_l(effect, /*release*/ true);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002990 if (effect->purgeHandles()) {
2991 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002992 }
Eric Laurent6c796322019-04-09 14:13:17 -07002993 removedEffects.push_back(effect);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002994 }
2995 }
2996 }
Eric Laurent6c796322019-04-09 14:13:17 -07002997 return removedEffects;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002998}
2999
Andy Hungdc099c22018-09-18 13:46:39 -07003000// dumpToThreadLog_l() must be called with AudioFlinger::mLock held
3001void AudioFlinger::dumpToThreadLog_l(const sp<ThreadBase> &thread)
3002{
3003 audio_utils::FdToString fdToString;
3004 const int fd = fdToString.fd();
3005 if (fd >= 0) {
3006 thread->dump(fd, {} /* args */);
3007 mThreadLog.logs(-1 /* time */, fdToString.getStringAndClose());
3008 }
3009}
3010
Glenn Kasteneeecb982016-02-26 10:44:04 -08003011// checkThread_l() must be called with AudioFlinger::mLock held
3012AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
3013{
Eric Laurent6acd1d42017-01-04 14:23:29 -08003014 ThreadBase *thread = checkMmapThread_l(ioHandle);
3015 if (thread == 0) {
3016 switch (audio_unique_id_get_use(ioHandle)) {
3017 case AUDIO_UNIQUE_ID_USE_OUTPUT:
3018 thread = checkPlaybackThread_l(ioHandle);
3019 break;
3020 case AUDIO_UNIQUE_ID_USE_INPUT:
3021 thread = checkRecordThread_l(ioHandle);
3022 break;
3023 default:
3024 break;
3025 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08003026 }
3027 return thread;
3028}
3029
Mathias Agopian65ab4712010-07-14 17:59:35 -07003030// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003031AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003032{
Glenn Kastena1117922012-01-26 10:53:32 -08003033 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07003034}
3035
3036// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003037AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003038{
3039 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08003040 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003041}
3042
3043// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003044AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003045{
Glenn Kastena1117922012-01-26 10:53:32 -08003046 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07003047}
3048
Eric Laurent6acd1d42017-01-04 14:23:29 -08003049// checkMmapThread_l() must be called with AudioFlinger::mLock held
3050AudioFlinger::MmapThread *AudioFlinger::checkMmapThread_l(audio_io_handle_t io) const
3051{
3052 return mMmapThreads.valueFor(io).get();
3053}
3054
3055
3056// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
3057AudioFlinger::VolumeInterface *AudioFlinger::getVolumeInterface_l(audio_io_handle_t output) const
3058{
Eric Laurent7459b902017-04-19 18:10:41 -07003059 VolumeInterface *volumeInterface = mPlaybackThreads.valueFor(output).get();
Eric Laurent6acd1d42017-01-04 14:23:29 -08003060 if (volumeInterface == nullptr) {
3061 MmapThread *mmapThread = mMmapThreads.valueFor(output).get();
3062 if (mmapThread != nullptr) {
3063 if (mmapThread->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07003064 MmapPlaybackThread *mmapPlaybackThread =
3065 static_cast<MmapPlaybackThread *>(mmapThread);
3066 volumeInterface = mmapPlaybackThread;
Eric Laurent6acd1d42017-01-04 14:23:29 -08003067 }
3068 }
3069 }
3070 return volumeInterface;
3071}
3072
3073Vector <AudioFlinger::VolumeInterface *> AudioFlinger::getAllVolumeInterfaces_l() const
3074{
3075 Vector <VolumeInterface *> volumeInterfaces;
3076 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7459b902017-04-19 18:10:41 -07003077 volumeInterfaces.add(mPlaybackThreads.valueAt(i).get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08003078 }
3079 for (size_t i = 0; i < mMmapThreads.size(); i++) {
3080 if (mMmapThreads.valueAt(i)->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07003081 MmapPlaybackThread *mmapPlaybackThread =
3082 static_cast<MmapPlaybackThread *>(mMmapThreads.valueAt(i).get());
3083 volumeInterfaces.add(mmapPlaybackThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08003084 }
3085 }
3086 return volumeInterfaces;
3087}
3088
Glenn Kasteneeecb982016-02-26 10:44:04 -08003089audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003090{
Glenn Kasten9d003132016-04-06 14:38:09 -07003091 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08003092 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07003093 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
3094 for (int retry = 0; retry < maxRetries; retry++) {
3095 // The cast allows wraparound from max positive to min negative instead of abort
3096 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
3097 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
3098 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
3099 // allow wrap by skipping 0 and -1 for session ids
3100 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
3101 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
3102 return (audio_unique_id_t) (base | use);
3103 }
3104 }
3105 // We have no way of recovering from wraparound
3106 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
3107 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07003108}
3109
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08003110AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003111{
3112 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3113 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07003114 if(thread->isDuplicating()) {
3115 continue;
3116 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07003117 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07003118 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003119 return thread;
3120 }
3121 }
3122 return NULL;
3123}
3124
Glenn Kastenbb4350d2012-07-03 15:56:38 -07003125audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003126{
3127 PlaybackThread *thread = primaryPlaybackThread_l();
3128
3129 if (thread == NULL) {
3130 return 0;
3131 }
3132
Eric Laurentf1c04f92012-08-28 14:26:53 -07003133 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003134}
3135
Glenn Kastena7335632016-06-09 17:09:53 -07003136AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
3137{
3138 size_t minFrameCount = 0;
3139 PlaybackThread *minThread = NULL;
3140 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3141 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
3142 if (!thread->isDuplicating()) {
3143 size_t frameCount = thread->frameCountHAL();
3144 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
3145 (frameCount == minFrameCount && thread->hasFastMixer() &&
3146 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
3147 minFrameCount = frameCount;
3148 minThread = thread;
3149 }
3150 }
3151 }
3152 return minThread;
3153}
3154
Eric Laurenta011e352012-03-29 15:51:43 -07003155sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08003156 audio_session_t triggerSession,
3157 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07003158 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07003159 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07003160{
3161 Mutex::Autolock _l(mLock);
3162
3163 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
3164 status_t playStatus = NAME_NOT_FOUND;
3165 status_t recStatus = NAME_NOT_FOUND;
3166 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3167 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
3168 if (playStatus == NO_ERROR) {
3169 return event;
3170 }
3171 }
3172 for (size_t i = 0; i < mRecordThreads.size(); i++) {
3173 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
3174 if (recStatus == NO_ERROR) {
3175 return event;
3176 }
3177 }
3178 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
3179 mPendingSyncEvents.add(event);
3180 } else {
3181 ALOGV("createSyncEvent() invalid event %d", event->type());
3182 event.clear();
3183 }
3184 return event;
3185}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003186
Mathias Agopian65ab4712010-07-14 17:59:35 -07003187// ----------------------------------------------------------------------------
3188// Effect management
3189// ----------------------------------------------------------------------------
3190
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003191sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
3192 return mEffectsFactoryHal;
3193}
Mathias Agopian65ab4712010-07-14 17:59:35 -07003194
Glenn Kastenf587ba52012-01-26 16:25:10 -08003195status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003196{
3197 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003198 if (mEffectsFactoryHal.get()) {
3199 return mEffectsFactoryHal->queryNumberEffects(numEffects);
3200 } else {
3201 return -ENODEV;
3202 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003203}
3204
Glenn Kastenf587ba52012-01-26 16:25:10 -08003205status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003206{
3207 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003208 if (mEffectsFactoryHal.get()) {
3209 return mEffectsFactoryHal->getDescriptor(index, descriptor);
3210 } else {
3211 return -ENODEV;
3212 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003213}
3214
Glenn Kasten5e92a782012-01-30 07:40:52 -08003215status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -07003216 const effect_uuid_t *pTypeUuid,
3217 uint32_t preferredTypeFlag,
3218 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07003219{
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -07003220 if (pUuid == NULL || pTypeUuid == NULL || descriptor == NULL) {
3221 return BAD_VALUE;
3222 }
3223
Mathias Agopian65ab4712010-07-14 17:59:35 -07003224 Mutex::Autolock _l(mLock);
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -07003225
3226 if (!mEffectsFactoryHal.get()) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003227 return -ENODEV;
3228 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003229
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -07003230 status_t status = NO_ERROR;
3231 if (!EffectsFactoryHalInterface::isNullUuid(pUuid)) {
3232 // If uuid is specified, request effect descriptor from that.
3233 status = mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
3234 } else if (!EffectsFactoryHalInterface::isNullUuid(pTypeUuid)) {
3235 // If uuid is not specified, look for an available implementation
3236 // of the required type instead.
3237
3238 // Use a temporary descriptor to avoid modifying |descriptor| in the failure case.
3239 effect_descriptor_t desc;
3240 desc.flags = 0; // prevent compiler warning
3241
3242 uint32_t numEffects = 0;
3243 status = mEffectsFactoryHal->queryNumberEffects(&numEffects);
3244 if (status < 0) {
3245 ALOGW("getEffectDescriptor() error %d from FactoryHal queryNumberEffects", status);
3246 return status;
3247 }
3248
3249 bool found = false;
3250 for (uint32_t i = 0; i < numEffects; i++) {
3251 status = mEffectsFactoryHal->getDescriptor(i, &desc);
3252 if (status < 0) {
3253 ALOGW("getEffectDescriptor() error %d from FactoryHal getDescriptor", status);
3254 continue;
3255 }
3256 if (memcmp(&desc.type, pTypeUuid, sizeof(effect_uuid_t)) == 0) {
3257 // If matching type found save effect descriptor.
3258 found = true;
3259 *descriptor = desc;
3260
3261 // If there's no preferred flag or this descriptor matches the preferred
3262 // flag, success! If this descriptor doesn't match the preferred
3263 // flag, continue enumeration in case a better matching version of this
3264 // effect type is available. Note that this means if no effect with a
3265 // correct flag is found, the descriptor returned will correspond to the
3266 // last effect that at least had a matching type uuid (if any).
3267 if (preferredTypeFlag == EFFECT_FLAG_TYPE_MASK ||
3268 (desc.flags & EFFECT_FLAG_TYPE_MASK) == preferredTypeFlag) {
3269 break;
3270 }
3271 }
3272 }
3273
3274 if (!found) {
3275 status = NAME_NOT_FOUND;
3276 ALOGW("getEffectDescriptor(): Effect not found by type.");
3277 }
3278 } else {
3279 status = BAD_VALUE;
3280 ALOGE("getEffectDescriptor(): Either uuid or type uuid must be non-null UUIDs.");
3281 }
3282 return status;
3283}
Mathias Agopian65ab4712010-07-14 17:59:35 -07003284
Glenn Kasten8d6cc842012-02-03 11:06:53 -08003285sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07003286 effect_descriptor_t *pDesc,
3287 const sp<IEffectClient>& effectClient,
3288 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003289 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08003290 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07003291 const String16& opPackageName,
Eric Laurentb6436272016-12-07 19:24:50 -08003292 pid_t pid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07003293 status_t *status,
3294 int *id,
3295 int *enabled)
3296{
3297 status_t lStatus = NO_ERROR;
3298 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003299 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003300
Eric Laurentb6436272016-12-07 19:24:50 -08003301 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Andy Hung4ef19fa2018-05-15 19:35:29 -07003302 if (pid == -1 || !isAudioServerOrMediaServerUid(callingUid)) {
Eric Laurentb6436272016-12-07 19:24:50 -08003303 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
3304 ALOGW_IF(pid != -1 && pid != callingPid,
3305 "%s uid %d pid %d tried to pass itself off as pid %d",
3306 __func__, callingUid, callingPid, pid);
3307 pid = callingPid;
3308 }
3309
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003310 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
3311 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003312
3313 if (pDesc == NULL) {
3314 lStatus = BAD_VALUE;
3315 goto Exit;
3316 }
3317
Mikhail Naganov1dc98672016-08-18 17:50:29 -07003318 if (mEffectsFactoryHal == 0) {
Andy Hung6096dcd2019-03-13 13:20:30 -07003319 ALOGE("%s: no effects factory hal", __func__);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07003320 lStatus = NO_INIT;
3321 goto Exit;
3322 }
3323
Andy Hung6096dcd2019-03-13 13:20:30 -07003324 // check audio settings permission for global effects
3325 if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
3326 if (!settingsAllowed()) {
3327 ALOGE("%s: no permission for AUDIO_SESSION_OUTPUT_MIX", __func__);
3328 lStatus = PERMISSION_DENIED;
3329 goto Exit;
3330 }
3331 } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
3332 if (!isAudioServerUid(callingUid)) {
3333 ALOGE("%s: only APM can create using AUDIO_SESSION_OUTPUT_STAGE", __func__);
3334 lStatus = PERMISSION_DENIED;
3335 goto Exit;
3336 }
3337
3338 if (io == AUDIO_IO_HANDLE_NONE) {
3339 ALOGE("%s: APM must specify output when using AUDIO_SESSION_OUTPUT_STAGE", __func__);
3340 lStatus = BAD_VALUE;
3341 goto Exit;
3342 }
3343 } else {
3344 // general sessionId.
3345
3346 if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
3347 ALOGE("%s: invalid sessionId %d", __func__, sessionId);
3348 lStatus = BAD_VALUE;
3349 goto Exit;
3350 }
3351
3352 // TODO: should we check if the callingUid (limited to pid) is in mAudioSessionRefs
3353 // to prevent creating an effect when one doesn't actually have track with that session?
3354 }
3355
Mathias Agopian65ab4712010-07-14 17:59:35 -07003356 {
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -07003357 // Get the full effect descriptor from the uuid/type.
3358 // If the session is the output mix, prefer an auxiliary effect,
3359 // otherwise no preference.
3360 uint32_t preferredType = (sessionId == AUDIO_SESSION_OUTPUT_MIX ?
3361 EFFECT_FLAG_TYPE_AUXILIARY : EFFECT_FLAG_TYPE_MASK);
3362 lStatus = getEffectDescriptor(&pDesc->uuid, &pDesc->type, preferredType, &desc);
3363 if (lStatus < 0) {
3364 ALOGW("createEffect() error %d from getEffectDescriptor", lStatus);
3365 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003366 }
3367
3368 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07003369 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07003370 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
3371 lStatus = INVALID_OPERATION;
3372 goto Exit;
3373 }
3374
Eric Laurent59255e42011-07-27 19:49:51 -07003375 // check recording permission for visualizer
3376 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Svet Ganov6e641372018-03-02 09:21:30 -08003377 // TODO: Do we need to start/stop op - i.e. is there recording being performed?
Marco Nelissendcb346b2015-09-09 10:47:29 -07003378 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07003379 lStatus = PERMISSION_DENIED;
3380 goto Exit;
3381 }
3382
Mathias Agopian65ab4712010-07-14 17:59:35 -07003383 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08003384 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07003385 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07003386 // if the output returned by getOutputForEffect() is removed before we lock the
3387 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
3388 // and we will exit safely
3389 io = AudioSystem::getOutputForEffect(&desc);
3390 ALOGV("createEffect got output %d", io);
3391 }
3392
3393 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003394
3395 // If output is not specified try to find a matching audio session ID in one of the
3396 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07003397 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
3398 // because of code checking output when entering the function.
Andy Hung444bb552019-03-14 17:06:39 -07003399 // Note: io is never AUDIO_IO_HANDLE_NONE when creating an effect on an input by APM.
3400 // An AudioEffect created from the Java API will have io as AUDIO_IO_HANDLE_NONE.
Glenn Kasten142f5192014-03-25 17:44:59 -07003401 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07003402 // look for the thread where the specified audio session is present
Andy Hunge778c422019-03-14 15:04:30 -07003403 io = findIoHandleBySessionId_l(sessionId, mPlaybackThreads);
3404 if (io == AUDIO_IO_HANDLE_NONE) {
3405 io = findIoHandleBySessionId_l(sessionId, mRecordThreads);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003406 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08003407 if (io == AUDIO_IO_HANDLE_NONE) {
Andy Hunge778c422019-03-14 15:04:30 -07003408 io = findIoHandleBySessionId_l(sessionId, mMmapThreads);
Eric Laurent6acd1d42017-01-04 14:23:29 -08003409 }
Andy Hung444bb552019-03-14 17:06:39 -07003410
3411 // If you wish to create a Record preprocessing AudioEffect in Java,
3412 // you MUST create an AudioRecord first and keep it alive so it is picked up above.
3413 // Otherwise it will fail when created on a Playback thread by legacy
3414 // handling below. Ditto with Mmap, the associated Mmap track must be created
3415 // before creating the AudioEffect or the io handle must be specified.
3416 //
3417 // Detect if the effect is created after an AudioRecord is destroyed.
3418 if (getOrphanEffectChain_l(sessionId).get() != nullptr) {
3419 ALOGE("%s: effect %s with no specified io handle is denied because the AudioRecord"
3420 " for session %d no longer exists",
3421 __func__, desc.name, sessionId);
3422 lStatus = PERMISSION_DENIED;
3423 goto Exit;
3424 }
3425
3426 // Legacy handling of creating an effect on an expired or made-up
3427 // session id. We think that it is a Playback effect.
3428 //
Eric Laurent84e9a102010-09-23 16:10:16 -07003429 // If no output thread contains the requested session ID, default to
3430 // first output. The effect chain will be moved to the correct output
3431 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07003432 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003433 io = mPlaybackThreads.keyAt(0);
3434 }
Steve Block3856b092011-10-20 11:56:00 +01003435 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Andy Hung1631f062019-03-12 19:39:03 -07003436 } else if (checkPlaybackThread_l(io) != nullptr) {
3437 // allow only one effect chain per sessionId on mPlaybackThreads.
3438 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3439 const audio_io_handle_t checkIo = mPlaybackThreads.keyAt(i);
3440 if (io == checkIo) continue;
3441 const uint32_t sessionType =
3442 mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId);
3443 if ((sessionType & ThreadBase::EFFECT_SESSION) != 0) {
3444 ALOGE("%s: effect %s io %d denied because session %d effect exists on io %d",
3445 __func__, desc.name, (int)io, (int)sessionId, (int)checkIo);
3446 android_errorWriteLog(0x534e4554, "123237974");
3447 lStatus = BAD_VALUE;
3448 goto Exit;
3449 }
3450 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003451 }
3452 ThreadBase *thread = checkRecordThread_l(io);
3453 if (thread == NULL) {
3454 thread = checkPlaybackThread_l(io);
3455 if (thread == NULL) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08003456 thread = checkMmapThread_l(io);
3457 if (thread == NULL) {
3458 ALOGE("createEffect() unknown output thread");
3459 lStatus = BAD_VALUE;
3460 goto Exit;
3461 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003462 }
Eric Laurentaaa44472014-09-12 17:41:50 -07003463 } else {
3464 // Check if one effect chain was awaiting for an effect to be created on this
3465 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08003466 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07003467 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07003468 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07003469 thread->addEffectChain_l(chain);
3470 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003471 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003472
Eric Laurent021cf962014-05-13 10:18:14 -07003473 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003474
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003475 // create effect on selected output thread
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003476 bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003477 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003478 &desc, enabled, &lStatus, pinned);
haobo101735295ff7b0d2017-03-17 17:44:03 +08003479 if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003480 // remove local strong reference to Client with mClientLock held
3481 Mutex::Autolock _cl(mClientLock);
3482 client.clear();
haobo101735295ff7b0d2017-03-17 17:44:03 +08003483 } else {
3484 // handle must be valid here, but check again to be safe.
3485 if (handle.get() != nullptr && id != nullptr) *id = handle->id();
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003486 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003487 }
3488
Eric Laurent6c796322019-04-09 14:13:17 -07003489 if (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS) {
3490 // Check CPU and memory usage
3491 sp<EffectModule> effect = handle->effect().promote();
3492 if (effect != nullptr) {
3493 status_t rStatus = effect->updatePolicyState();
3494 if (rStatus != NO_ERROR) {
3495 lStatus = rStatus;
3496 }
3497 }
3498 } else {
haobo101735295ff7b0d2017-03-17 17:44:03 +08003499 handle.clear();
3500 }
3501
Mathias Agopian65ab4712010-07-14 17:59:35 -07003502Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07003503 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003504 return handle;
3505}
3506
Glenn Kastend848eb42016-03-08 13:42:11 -08003507status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003508 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07003509{
Steve Block3856b092011-10-20 11:56:00 +01003510 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07003511 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003512 Mutex::Autolock _l(mLock);
3513 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003514 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003515 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003516 }
Eric Laurentde070132010-07-13 04:45:46 -07003517 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
3518 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003519 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003520 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003521 }
Eric Laurentde070132010-07-13 04:45:46 -07003522 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
3523 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003524 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003525 return BAD_VALUE;
3526 }
3527
3528 Mutex::Autolock _dl(dstThread->mLock);
3529 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent6c796322019-04-09 14:13:17 -07003530 return moveEffectChain_l(sessionId, srcThread, dstThread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003531}
3532
Eric Laurentb20cf7d2019-04-05 19:37:34 -07003533
3534void AudioFlinger::setEffectSuspended(int effectId,
3535 audio_session_t sessionId,
3536 bool suspended)
3537{
3538 Mutex::Autolock _l(mLock);
3539
3540 sp<ThreadBase> thread = getEffectThread_l(sessionId, effectId);
3541 if (thread == nullptr) {
3542 return;
3543 }
3544 Mutex::Autolock _sl(thread->mLock);
3545 sp<EffectModule> effect = thread->getEffect_l(sessionId, effectId);
3546 thread->setEffectSuspended_l(&effect->desc().type, suspended, sessionId);
3547}
3548
3549
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003550// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08003551status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07003552 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent6c796322019-04-09 14:13:17 -07003553 AudioFlinger::PlaybackThread *dstThread)
Eric Laurentde070132010-07-13 04:45:46 -07003554{
Steve Block3856b092011-10-20 11:56:00 +01003555 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003556 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07003557
Eric Laurent59255e42011-07-27 19:49:51 -07003558 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003559 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003560 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003561 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07003562 return INVALID_OPERATION;
3563 }
3564
Eric Laurent4c415062016-06-17 16:14:16 -07003565 // Check whether the destination thread and all effects in the chain are compatible
3566 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07003567 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07003568 " destination thread %p is not compatible with effects in the chain",
3569 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07003570 return INVALID_OPERATION;
3571 }
3572
Eric Laurent39e94f82010-07-28 01:32:47 -07003573 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07003574 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07003575 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07003576 // removed.
3577 srcThread->removeEffectChain_l(chain);
3578
3579 // transfer all effects one by one so that new effect chain is created on new thread with
3580 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07003581 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003582 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07003583 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003584 Vector< sp<EffectModule> > removed;
3585 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07003586 while (effect != 0) {
3587 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003588 removed.add(effect);
3589 status = dstThread->addEffect_l(effect);
3590 if (status != NO_ERROR) {
3591 break;
3592 }
Eric Laurentec35a142011-10-05 17:42:25 -07003593 // removeEffect_l() has stopped the effect if it was active so it must be restarted
3594 if (effect->state() == EffectModule::ACTIVE ||
3595 effect->state() == EffectModule::STOPPING) {
3596 effect->start();
3597 }
Eric Laurent39e94f82010-07-28 01:32:47 -07003598 // if the move request is not received from audio policy manager, the effect must be
3599 // re-registered with the new strategy and output
3600 if (dstChain == 0) {
3601 dstChain = effect->chain().promote();
3602 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003603 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003604 status = NO_INIT;
3605 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07003606 }
3607 strategy = dstChain->strategy();
3608 }
Eric Laurentde070132010-07-13 04:45:46 -07003609 effect = chain->getEffectFromId_l(0);
3610 }
3611
Eric Laurent5baf2af2013-09-12 17:37:00 -07003612 if (status != NO_ERROR) {
3613 for (size_t i = 0; i < removed.size(); i++) {
3614 srcThread->addEffect_l(removed[i]);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003615 }
3616 }
3617
3618 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003619}
3620
Eric Laurent6c796322019-04-09 14:13:17 -07003621status_t AudioFlinger::moveAuxEffectToIo(int EffectId,
3622 const sp<PlaybackThread>& dstThread,
3623 sp<PlaybackThread> *srcThread)
3624{
3625 status_t status = NO_ERROR;
3626 Mutex::Autolock _l(mLock);
Eric Laurentb20cf7d2019-04-05 19:37:34 -07003627 sp<PlaybackThread> thread =
3628 static_cast<PlaybackThread *>(getEffectThread_l(AUDIO_SESSION_OUTPUT_MIX, EffectId).get());
Eric Laurent6c796322019-04-09 14:13:17 -07003629
3630 if (EffectId != 0 && thread != 0 && dstThread != thread.get()) {
3631 Mutex::Autolock _dl(dstThread->mLock);
3632 Mutex::Autolock _sl(thread->mLock);
3633 sp<EffectChain> srcChain = thread->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
3634 sp<EffectChain> dstChain;
3635 if (srcChain == 0) {
3636 return INVALID_OPERATION;
3637 }
3638
3639 sp<EffectModule> effect = srcChain->getEffectFromId_l(EffectId);
3640 if (effect == 0) {
3641 return INVALID_OPERATION;
3642 }
3643 thread->removeEffect_l(effect);
3644 status = dstThread->addEffect_l(effect);
3645 if (status != NO_ERROR) {
3646 thread->addEffect_l(effect);
3647 status = INVALID_OPERATION;
3648 goto Exit;
3649 }
3650
3651 dstChain = effect->chain().promote();
3652 if (dstChain == 0) {
3653 thread->addEffect_l(effect);
3654 status = INVALID_OPERATION;
3655 }
3656
3657Exit:
3658 // removeEffect_l() has stopped the effect if it was active so it must be restarted
3659 if (effect->state() == EffectModule::ACTIVE ||
3660 effect->state() == EffectModule::STOPPING) {
3661 effect->start();
3662 }
3663 }
3664
3665 if (status == NO_ERROR && srcThread != nullptr) {
3666 *srcThread = thread;
3667 }
3668 return status;
3669}
3670
Eric Laurent5baf2af2013-09-12 17:37:00 -07003671bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07003672{
3673 if (mGlobalEffectEnableTime != 0 &&
3674 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
3675 return true;
3676 }
3677
3678 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3679 sp<EffectChain> ec =
3680 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003681 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07003682 return true;
3683 }
3684 }
3685 return false;
3686}
3687
Eric Laurent5baf2af2013-09-12 17:37:00 -07003688void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07003689{
3690 Mutex::Autolock _l(mLock);
3691
3692 mGlobalEffectEnableTime = systemTime();
3693
3694 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3695 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
3696 if (t->mType == ThreadBase::OFFLOAD) {
3697 t->invalidateTracks(AUDIO_STREAM_MUSIC);
3698 }
3699 }
3700
3701}
3702
Eric Laurentaaa44472014-09-12 17:41:50 -07003703status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
3704{
Eric Laurentd8365c52017-07-16 15:27:05 -07003705 // clear possible suspended state before parking the chain so that it starts in default state
3706 // when attached to a new record thread
3707 chain->setEffectSuspended_l(FX_IID_AEC, false);
3708 chain->setEffectSuspended_l(FX_IID_NS, false);
3709
Glenn Kastend848eb42016-03-08 13:42:11 -08003710 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003711 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003712 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003713 if (index >= 0) {
3714 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
3715 return ALREADY_EXISTS;
3716 }
3717 mOrphanEffectChains.add(session, chain);
3718 return NO_ERROR;
3719}
3720
3721sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
3722{
3723 sp<EffectChain> chain;
3724 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003725 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003726 if (index >= 0) {
3727 chain = mOrphanEffectChains.valueAt(index);
3728 mOrphanEffectChains.removeItemsAt(index);
3729 }
3730 return chain;
3731}
3732
3733bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
3734{
3735 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08003736 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003737 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003738 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003739 if (index >= 0) {
3740 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003741 if (chain->removeEffect_l(effect, true) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003742 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003743 mOrphanEffectChains.removeItemsAt(index);
3744 }
3745 return true;
3746 }
3747 return false;
3748}
3749
3750
Mathias Agopian65ab4712010-07-14 17:59:35 -07003751// ----------------------------------------------------------------------------
3752
3753status_t AudioFlinger::onTransact(
3754 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3755{
3756 return BnAudioFlinger::onTransact(code, data, reply, flags);
3757}
3758
Glenn Kasten63238ef2015-03-02 15:50:29 -08003759} // namespace android