blob: 2a7194b8b638e8493dd1194320826e819678eb6b [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080023#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <math.h>
25#include <signal.h>
26#include <sys/time.h>
27#include <sys/resource.h>
28
Gloria Wang9ee159b2011-02-24 14:51:45 -080029#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <binder/IServiceManager.h>
31#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070032#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <binder/Parcel.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070034#include <media/audiohal/DeviceHalInterface.h>
35#include <media/audiohal/DevicesFactoryHalInterface.h>
36#include <media/audiohal/EffectsFactoryHalInterface.h>
Mikhail Naganov00260b52016-10-13 12:54:24 -070037#include <media/AudioParameter.h>
Mikhail Naganov913d06c2016-11-01 12:49:22 -070038#include <media/TypeConverter.h>
Andy Hung35fec5f2016-04-13 14:21:48 -070039#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070040#include <utils/String16.h>
41#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070042#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
Dima Zavinfce7a472011-04-19 22:30:36 -070044#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045#include <cutils/properties.h>
46
Dima Zavin64760242011-05-11 14:15:23 -070047#include <system/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
Mathias Agopian65ab4712010-07-14 17:59:35 -070049#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080050#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
Andy Hung6770c6f2015-04-07 13:43:36 -070052#include <media/AudioResamplerPublic.h>
53
Mikhail Naganov9fe94012016-10-14 14:57:40 -070054#include <system/audio_effects/effect_visualizer.h>
55#include <system/audio_effects/effect_ns.h>
56#include <system/audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070057
Glenn Kasten3b21c502011-12-15 09:52:39 -080058#include <audio_utils/primitives.h>
59
Eric Laurentfeb0db62011-07-22 09:04:31 -070060#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080061
Glenn Kasten9e58b552013-01-18 15:09:48 -080062#include <media/IMediaLogService.h>
Andy Hung07b745e2016-05-23 16:21:07 -070063#include <media/MemoryLeakTrackUtil.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080064#include <media/nbaio/Pipe.h>
65#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070066#include <media/AudioParameter.h>
Wei Jia3f273d12015-11-24 09:06:49 -080067#include <mediautils/BatteryNotifier.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070068#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080069
rago3bd1c872016-09-26 12:58:14 -070070//#define BUFLOG_NDEBUG 0
71#include <BufLog.h>
72
Nicolas Rouletfe1e1442017-01-30 12:02:03 -080073#include "TypedLogger.h"
74
Mathias Agopian65ab4712010-07-14 17:59:35 -070075// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070076
John Grossman1c345192012-03-27 14:00:17 -070077// Note: the following macro is used for extremely verbose logging message. In
78// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
79// 0; but one side effect of this is to turn all LOGV's as well. Some messages
80// are so verbose that we want to suppress them even when we have ALOG_ASSERT
81// turned on. Do not uncomment the #def below unless you really know what you
82// are doing and want to see all of the extremely verbose messages.
83//#define VERY_VERY_VERBOSE_LOGGING
84#ifdef VERY_VERY_VERBOSE_LOGGING
85#define ALOGVV ALOGV
86#else
87#define ALOGVV(a...) do { } while(0)
88#endif
Eric Laurentde070132010-07-13 04:45:46 -070089
Mathias Agopian65ab4712010-07-14 17:59:35 -070090namespace android {
91
Glenn Kastenec1d6b52011-12-12 09:04:45 -080092static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
93static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070094static const char kClientLockedString[] = "Client lock is taken\n";
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070095static const char kNoEffectsFactory[] = "Effects Factory is absent\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070096
Glenn Kasten58912562012-04-03 10:45:00 -070097
John Grossman4ff14ba2012-02-08 16:37:41 -080098nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080099
Eric Laurent81784c32012-11-19 14:55:58 -0800100uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -0700101
Eric Laurentfc235202016-12-20 18:48:17 -0800102
Glenn Kasten46909e72013-02-26 09:20:22 -0800103#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800104bool AudioFlinger::mTeeSinkInputEnabled = false;
105bool AudioFlinger::mTeeSinkOutputEnabled = false;
106bool AudioFlinger::mTeeSinkTrackEnabled = false;
107
108size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
109size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
110size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800111#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800112
Eric Laurent813e2a72013-08-31 12:59:48 -0700113// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
114// we define a minimum time during which a global effect is considered enabled.
115static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
116
Eric Laurentfc235202016-12-20 18:48:17 -0800117Mutex gLock;
118wp<AudioFlinger> gAudioFlinger;
119
Mathias Agopian65ab4712010-07-14 17:59:35 -0700120// ----------------------------------------------------------------------------
121
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700122std::string formatToString(audio_format_t format) {
123 std::string result;
124 FormatConverter::toString(format, result);
125 return result;
Marco Nelissenb2208842014-02-07 14:00:50 -0800126}
127
Mathias Agopian65ab4712010-07-14 17:59:35 -0700128// ----------------------------------------------------------------------------
129
130AudioFlinger::AudioFlinger()
131 : BnAudioFlinger(),
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -0800132 mMediaLogNotifier(new AudioFlinger::MediaLogNotifier()),
John Grossman4ff14ba2012-02-08 16:37:41 -0800133 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800134 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700135 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800136 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800137 mMasterMute(false),
Glenn Kastend2e67e12016-04-11 08:26:37 -0700138 // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),
John Grossman4ff14ba2012-02-08 16:37:41 -0800139 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700140 mBtNrecIsOff(false),
141 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700142 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700143 mGlobalEffectEnableTime(0),
Eric Laurent72e3f392015-05-20 14:43:50 -0700144 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145{
Glenn Kastend2e67e12016-04-11 08:26:37 -0700146 // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum
147 for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) {
148 // zero ID has a special meaning, so unavailable
149 mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX;
150 }
151
Glenn Kasten949a9262013-04-16 12:35:20 -0700152 getpid_cached = getpid();
Glenn Kastenae0cff12016-02-24 13:57:49 -0800153 const bool doLog = property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800154 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800155 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
156 MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800157 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700158
Wei Jia3f273d12015-11-24 09:06:49 -0800159 // reset battery stats.
160 // if the audio service has crashed, battery stats could be left
161 // in bad state, reset the state upon service start.
162 BatteryNotifier::getInstance().noteResetAudio();
163
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700164 mDevicesFactoryHal = DevicesFactoryHalInterface::create();
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700165 mEffectsFactoryHal = EffectsFactoryHalInterface::create();
166
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -0800167 mMediaLogNotifier->run("MediaLogNotifier");
168
Glenn Kasten46909e72013-02-26 09:20:22 -0800169#ifdef TEE_SINK
Glenn Kasten9a003992016-02-23 15:24:34 -0800170 char value[PROPERTY_VALUE_MAX];
Glenn Kastenda6ef132013-01-10 12:31:01 -0800171 (void) property_get("ro.debuggable", value, "0");
172 int debuggable = atoi(value);
173 int teeEnabled = 0;
174 if (debuggable) {
175 (void) property_get("af.tee", value, "0");
176 teeEnabled = atoi(value);
177 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800178 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700179 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800180 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700181 }
182 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800183 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700184 }
185 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800186 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700187 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800188#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700189}
190
191void AudioFlinger::onFirstRef()
192{
Eric Laurent93575202011-01-18 18:39:02 -0800193 Mutex::Autolock _l(mLock);
194
Dima Zavin799a70e2011-04-18 16:57:27 -0700195 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800196 char val_str[PROPERTY_VALUE_MAX] = { 0 };
197 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
198 uint32_t int_val;
199 if (1 == sscanf(val_str, "%u", &int_val)) {
200 mStandbyTimeInNsecs = milliseconds(int_val);
201 ALOGI("Using %u mSec as standby time.", int_val);
202 } else {
203 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
204 ALOGI("Using default %u mSec as standby time.",
205 (uint32_t)(mStandbyTimeInNsecs / 1000000));
206 }
207 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208
Eric Laurent1c333e22014-05-20 10:48:17 -0700209 mPatchPanel = new PatchPanel(this);
Andy Hungdeb03352016-08-29 14:40:14 -0700210
Eric Laurenta4c5a552012-03-29 10:12:40 -0700211 mMode = AUDIO_MODE_NORMAL;
Eric Laurentfc235202016-12-20 18:48:17 -0800212
213 gAudioFlinger = this;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214}
215
216AudioFlinger::~AudioFlinger()
217{
218 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700219 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700220 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700221 }
222 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700223 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700224 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700225 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700226
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800227 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
228 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700229 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700231
232 // Tell media.log service about any old writers that still need to be unregistered
Andy Hungce717682015-12-22 13:40:32 -0800233 if (mLogMemoryDealer != 0) {
234 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
235 if (binder != 0) {
236 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
237 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
238 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
239 mUnregisteredWriters.pop();
240 mediaLogService->unregisterWriter(iMemory);
241 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700242 }
243 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244}
245
Eric Laurentfc235202016-12-20 18:48:17 -0800246//static
Eric Laurent6acd1d42017-01-04 14:23:29 -0800247__attribute__ ((visibility ("default")))
Eric Laurentfc235202016-12-20 18:48:17 -0800248status_t MmapStreamInterface::openMmapStream(MmapStreamInterface::stream_direction_t direction,
249 const audio_attributes_t *attr,
250 audio_config_base_t *config,
251 const MmapStreamInterface::Client& client,
252 audio_port_handle_t *deviceId,
253 const sp<MmapStreamCallback>& callback,
254 sp<MmapStreamInterface>& interface)
255{
256 sp<AudioFlinger> af;
257 {
258 Mutex::Autolock _l(gLock);
259 af = gAudioFlinger.promote();
260 }
261 status_t ret = NO_INIT;
262 if (af != 0) {
263 ret = af->openMmapStream(
264 direction, attr, config, client, deviceId, callback, interface);
265 }
266 return ret;
267}
268
Eric Laurent6acd1d42017-01-04 14:23:29 -0800269status_t AudioFlinger::openMmapStream(MmapStreamInterface::stream_direction_t direction,
270 const audio_attributes_t *attr,
271 audio_config_base_t *config,
272 const MmapStreamInterface::Client& client,
273 audio_port_handle_t *deviceId,
274 const sp<MmapStreamCallback>& callback,
275 sp<MmapStreamInterface>& interface)
Eric Laurentfc235202016-12-20 18:48:17 -0800276{
277 status_t ret = initCheck();
278 if (ret != NO_ERROR) {
279 return ret;
280 }
281
Eric Laurent6acd1d42017-01-04 14:23:29 -0800282 audio_session_t sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
283 audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT;
284 audio_io_handle_t io;
285 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
286 if (direction == MmapStreamInterface::DIRECTION_OUTPUT) {
287 audio_config_t fullConfig = AUDIO_CONFIG_INITIALIZER;
288 fullConfig.sample_rate = config->sample_rate;
289 fullConfig.channel_mask = config->channel_mask;
290 fullConfig.format = config->format;
291 ret = AudioSystem::getOutputForAttr(attr, &io,
292 sessionId,
293 &streamType, client.clientUid,
294 &fullConfig,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800295 (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ |
296 AUDIO_OUTPUT_FLAG_DIRECT),
Eric Laurent6acd1d42017-01-04 14:23:29 -0800297 *deviceId, &portId);
298 } else {
299 ret = AudioSystem::getInputForAttr(attr, &io,
300 sessionId,
301 client.clientPid,
302 client.clientUid,
303 config,
304 AUDIO_INPUT_FLAG_MMAP_NOIRQ, *deviceId, &portId);
305 }
306 if (ret != NO_ERROR) {
307 return ret;
308 }
309
310 // at this stage, a MmapThread was created when openOutput() or openInput() was called by
311 // audio policy manager and we can retrieve it
312 sp<MmapThread> thread = mMmapThreads.valueFor(io);
313 if (thread != 0) {
314 interface = new MmapThreadHandle(thread);
315 thread->configure(attr, streamType, sessionId, callback, portId);
316 } else {
317 ret = NO_INIT;
318 }
319
320 ALOGV("%s done status %d portId %d", __FUNCTION__, ret, portId);
321
Eric Laurentfc235202016-12-20 18:48:17 -0800322 return ret;
323}
324
Eric Laurenta4c5a552012-03-29 10:12:40 -0700325static const char * const audio_interfaces[] = {
326 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
327 AUDIO_HARDWARE_MODULE_ID_A2DP,
328 AUDIO_HARDWARE_MODULE_ID_USB,
329};
330#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
331
Phil Burk062e67a2015-02-11 13:40:50 -0800332AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700333 audio_module_handle_t module,
334 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700335{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700336 // if module is 0, the request comes from an old policy manager and we should load
337 // well known modules
338 if (module == 0) {
339 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
340 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
341 loadHwModule_l(audio_interfaces[i]);
342 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700343 // then try to find a module supporting the requested device.
344 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
345 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700346 sp<DeviceHalInterface> dev = audioHwDevice->hwDevice();
347 uint32_t supportedDevices;
348 if (dev->getSupportedDevices(&supportedDevices) == OK &&
349 (supportedDevices & devices) == devices) {
Eric Laurentf1c04f92012-08-28 14:26:53 -0700350 return audioHwDevice;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700351 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700352 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700353 } else {
354 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700355 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
356 if (audioHwDevice != NULL) {
357 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700358 }
359 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700360
Dima Zavin799a70e2011-04-18 16:57:27 -0700361 return NULL;
362}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363
Glenn Kasten0f11b512014-01-31 16:18:54 -0800364void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365{
366 const size_t SIZE = 256;
367 char buffer[SIZE];
368 String8 result;
369
370 result.append("Clients:\n");
371 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800372 sp<Client> client = mClients.valueAt(i).promote();
373 if (client != 0) {
374 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
375 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376 }
377 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700378
Eric Laurentd1b28d42013-09-18 18:47:13 -0700379 result.append("Notification Clients:\n");
380 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
381 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
382 result.append(buffer);
383 }
384
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700385 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800386 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700387 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
388 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800389 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700390 result.append(buffer);
391 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393}
394
395
Glenn Kasten0f11b512014-01-31 16:18:54 -0800396void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397{
398 const size_t SIZE = 256;
399 char buffer[SIZE];
400 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800401 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402
John Grossman4ff14ba2012-02-08 16:37:41 -0800403 snprintf(buffer, SIZE, "Hardware status: %d\n"
404 "Standby Time mSec: %u\n",
405 hardwareStatus,
406 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407 result.append(buffer);
408 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409}
410
Glenn Kasten0f11b512014-01-31 16:18:54 -0800411void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412{
413 const size_t SIZE = 256;
414 char buffer[SIZE];
415 String8 result;
416 snprintf(buffer, SIZE, "Permission Denial: "
417 "can't dump AudioFlinger from pid=%d, uid=%d\n",
418 IPCThreadState::self()->getCallingPid(),
419 IPCThreadState::self()->getCallingUid());
420 result.append(buffer);
421 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422}
423
Eric Laurent81784c32012-11-19 14:55:58 -0800424bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425{
426 bool locked = false;
427 for (int i = 0; i < kDumpLockRetries; ++i) {
428 if (mutex.tryLock() == NO_ERROR) {
429 locked = true;
430 break;
431 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800432 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700433 }
434 return locked;
435}
436
437status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
438{
Glenn Kasten44deb052012-02-05 18:09:08 -0800439 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440 dumpPermissionDenial(fd, args);
441 } else {
442 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800443 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700444 if (!hardwareLocked) {
445 String8 result(kHardwareLockedString);
446 write(fd, result.string(), result.size());
447 } else {
448 mHardwareLock.unlock();
449 }
450
Eric Laurent81784c32012-11-19 14:55:58 -0800451 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452
453 // failed to lock - AudioFlinger is probably deadlocked
454 if (!locked) {
455 String8 result(kDeadlockedString);
456 write(fd, result.string(), result.size());
457 }
458
Eric Laurent021cf962014-05-13 10:18:14 -0700459 bool clientLocked = dumpTryLock(mClientLock);
460 if (!clientLocked) {
461 String8 result(kClientLockedString);
462 write(fd, result.string(), result.size());
463 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700464
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700465 if (mEffectsFactoryHal != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700466 mEffectsFactoryHal->dumpEffects(fd);
467 } else {
468 String8 result(kNoEffectsFactory);
469 write(fd, result.string(), result.size());
470 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700471
Mathias Agopian65ab4712010-07-14 17:59:35 -0700472 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700473 if (clientLocked) {
474 mClientLock.unlock();
475 }
476
Mathias Agopian65ab4712010-07-14 17:59:35 -0700477 dumpInternals(fd, args);
478
479 // dump playback threads
480 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
481 mPlaybackThreads.valueAt(i)->dump(fd, args);
482 }
483
484 // dump record threads
485 for (size_t i = 0; i < mRecordThreads.size(); i++) {
486 mRecordThreads.valueAt(i)->dump(fd, args);
487 }
488
Eric Laurent6acd1d42017-01-04 14:23:29 -0800489 // dump mmap threads
490 for (size_t i = 0; i < mMmapThreads.size(); i++) {
491 mMmapThreads.valueAt(i)->dump(fd, args);
492 }
493
Eric Laurentaaa44472014-09-12 17:41:50 -0700494 // dump orphan effect chains
495 if (mOrphanEffectChains.size() != 0) {
496 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
497 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
498 mOrphanEffectChains.valueAt(i)->dump(fd, args);
499 }
500 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700501 // dump all hardware devs
502 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700503 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
504 dev->dump(fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700506
Glenn Kasten46909e72013-02-26 09:20:22 -0800507#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700508 // dump the serially shared record tee sink
509 if (mRecordTeeSource != 0) {
510 dumpTee(fd, mRecordTeeSource);
511 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800512#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700513
rago3bd1c872016-09-26 12:58:14 -0700514 BUFLOG_RESET;
515
Glenn Kastend65d73c2012-06-22 17:21:07 -0700516 if (locked) {
517 mLock.unlock();
518 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800519
520 // append a copy of media.log here by forwarding fd to it, but don't attempt
521 // to lookup the service if it's not running, as it will block for a second
522 if (mLogMemoryDealer != 0) {
523 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
524 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700525 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800526 Vector<String16> args;
527 binder->dump(fd, args);
528 }
529 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700530
531 // check for optional arguments
Andy Hung07b745e2016-05-23 16:21:07 -0700532 bool dumpMem = false;
Andy Hung35fec5f2016-04-13 14:21:48 -0700533 bool unreachableMemory = false;
534 for (const auto &arg : args) {
Andy Hung07b745e2016-05-23 16:21:07 -0700535 if (arg == String16("-m")) {
536 dumpMem = true;
537 } else if (arg == String16("--unreachable")) {
Andy Hung35fec5f2016-04-13 14:21:48 -0700538 unreachableMemory = true;
539 }
540 }
541
Andy Hung07b745e2016-05-23 16:21:07 -0700542 if (dumpMem) {
543 dprintf(fd, "\nDumping memory:\n");
544 std::string s = dumpMemoryAddresses(100 /* limit */);
545 write(fd, s.c_str(), s.size());
546 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700547 if (unreachableMemory) {
548 dprintf(fd, "\nDumping unreachable memory:\n");
549 // TODO - should limit be an argument parameter?
Andy Hung07b745e2016-05-23 16:21:07 -0700550 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
Andy Hung35fec5f2016-04-13 14:21:48 -0700551 write(fd, s.c_str(), s.size());
552 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 }
554 return NO_ERROR;
555}
556
Eric Laurent021cf962014-05-13 10:18:14 -0700557sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800558{
Eric Laurent021cf962014-05-13 10:18:14 -0700559 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800560 // If pid is already in the mClients wp<> map, then use that entry
561 // (for which promote() is always != 0), otherwise create a new entry and Client.
562 sp<Client> client = mClients.valueFor(pid).promote();
563 if (client == 0) {
564 client = new Client(this, pid);
565 mClients.add(pid, client);
566 }
567
568 return client;
569}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700570
Glenn Kasten9e58b552013-01-18 15:09:48 -0800571sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
572{
Glenn Kasten481fb672013-09-30 14:39:28 -0700573 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800574 if (mLogMemoryDealer == 0) {
575 return new NBLog::Writer();
576 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800577 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700578 // Similarly if we can't contact the media.log service, also return a dummy writer
579 if (binder == 0) {
580 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800581 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700582 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
583 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
584 // If allocation fails, consult the vector of previously unregistered writers
585 // and garbage-collect one or more them until an allocation succeeds
586 if (shared == 0) {
587 Mutex::Autolock _l(mUnregisteredWritersLock);
588 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
589 {
590 // Pick the oldest stale writer to garbage-collect
591 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
592 mUnregisteredWriters.removeAt(0);
593 mediaLogService->unregisterWriter(iMemory);
594 // Now the media.log remote reference to IMemory is gone. When our last local
595 // reference to IMemory also drops to zero at end of this block,
596 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
597 }
598 // Re-attempt the allocation
599 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
600 if (shared != 0) {
601 goto success;
602 }
603 }
604 // Even after garbage-collecting all old writers, there is still not enough memory,
605 // so return a dummy writer
606 return new NBLog::Writer();
607 }
608success:
Glenn Kasten535e1612016-12-05 12:19:36 -0800609 NBLog::Shared *sharedRawPtr = (NBLog::Shared *) shared->pointer();
610 new((void *) sharedRawPtr) NBLog::Shared(); // placement new here, but the corresponding
611 // explicit destructor not needed since it is POD
Glenn Kasten481fb672013-09-30 14:39:28 -0700612 mediaLogService->registerWriter(shared, size, name);
Glenn Kasten535e1612016-12-05 12:19:36 -0800613 return new NBLog::Writer(shared, size);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800614}
615
616void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
617{
Glenn Kasten685ef092013-02-04 08:15:34 -0800618 if (writer == 0) {
619 return;
620 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800621 sp<IMemory> iMemory(writer->getIMemory());
622 if (iMemory == 0) {
623 return;
624 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700625 // Rather than removing the writer immediately, append it to a queue of old writers to
626 // be garbage-collected later. This allows us to continue to view old logs for a while.
627 Mutex::Autolock _l(mUnregisteredWritersLock);
628 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800629}
630
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631// IAudioFlinger interface
632
633
634sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800635 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800637 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700638 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800639 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -0700640 audio_output_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800642 audio_io_handle_t output,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700643 pid_t pid,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800644 pid_t tid,
Glenn Kastend848eb42016-03-08 13:42:11 -0800645 audio_session_t *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800646 int clientUid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800647 status_t *status,
648 audio_port_handle_t portId)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649{
650 sp<PlaybackThread::Track> track;
651 sp<TrackHandle> trackHandle;
652 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -0800654 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700656 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
657 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
658 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
659 ALOGW_IF(pid != -1 && pid != callingPid,
660 "%s uid %d pid %d tried to pass itself off as pid %d",
661 __func__, callingUid, callingPid, pid);
662 pid = callingPid;
663 }
664
Glenn Kasten263709e2012-01-06 08:40:01 -0800665 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
666 // but if someone uses binder directly they could bypass that and cause us to crash
667 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000668 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 lStatus = BAD_VALUE;
670 goto Exit;
671 }
672
Glenn Kasten53b5d092014-02-05 10:00:23 -0800673 // further sample rate checks are performed by createTrack_l() depending on the thread type
674 if (sampleRate == 0) {
675 ALOGE("createTrack() invalid sample rate %u", sampleRate);
676 lStatus = BAD_VALUE;
677 goto Exit;
678 }
679
680 // further channel mask checks are performed by createTrack_l() depending on the thread type
681 if (!audio_is_output_channel(channelMask)) {
682 ALOGE("createTrack() invalid channel mask %#x", channelMask);
683 lStatus = BAD_VALUE;
684 goto Exit;
685 }
686
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700687 // further format checks are performed by createTrack_l() depending on the thread type
688 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800689 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700690 lStatus = BAD_VALUE;
691 goto Exit;
692 }
693
Glenn Kasten663c2242013-09-24 11:52:37 -0700694 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
695 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
696 lStatus = BAD_VALUE;
697 goto Exit;
698 }
699
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700 {
701 Mutex::Autolock _l(mLock);
702 PlaybackThread *thread = checkPlaybackThread_l(output);
703 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800704 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705 lStatus = BAD_VALUE;
706 goto Exit;
707 }
708
Eric Laurent021cf962014-05-13 10:18:14 -0700709 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710
Glenn Kastene848bd92014-03-13 15:00:32 -0700711 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700712 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800713 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
714 ALOGE("createTrack() invalid session ID %d", *sessionId);
715 lStatus = BAD_VALUE;
716 goto Exit;
717 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700718 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700719 // check if an effect chain with the same session ID is present on another
720 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700721 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700722 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
723 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700724 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent4c415062016-06-17 16:14:16 -0700725 if (sessions & ThreadBase::EFFECT_SESSION) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700726 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700727 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700728 }
Eric Laurentde070132010-07-13 04:45:46 -0700729 }
730 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700731 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700732 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800733 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734 if (sessionId != NULL) {
735 *sessionId = lSessionId;
736 }
737 }
Steve Block3856b092011-10-20 11:56:00 +0100738 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700739
740 track = thread->createTrack_l(client, streamType, sampleRate, format,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800741 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid,
742 clientUid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800743 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700744 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700745
746 // move effect chain to this output thread if an effect on same session was waiting
747 // for a track to be created
748 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700749 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700750 Mutex::Autolock _dl(thread->mLock);
751 Mutex::Autolock _sl(effectThread->mLock);
752 moveEffectChain_l(lSessionId, effectThread, thread, true);
753 }
Eric Laurenta011e352012-03-29 15:51:43 -0700754
755 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700756 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700757 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
758 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700759 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700760 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700761 } else {
762 mPendingSyncEvents[i]->cancel();
763 }
Eric Laurenta011e352012-03-29 15:51:43 -0700764 mPendingSyncEvents.removeAt(i);
765 i--;
766 }
767 }
768 }
Glenn Kastene198c362013-08-13 09:13:36 -0700769
Glenn Kastend848eb42016-03-08 13:42:11 -0800770 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 }
Glenn Kastene198c362013-08-13 09:13:36 -0700772
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700773 if (lStatus != NO_ERROR) {
774 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700775 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700776 // Don't hold mClientLock when releasing the reference on the track as the
777 // destructor will acquire it.
778 {
779 Mutex::Autolock _cl(mClientLock);
780 client.clear();
781 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700783 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784 }
785
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700786 // return handle to client
787 trackHandle = new TrackHandle(track);
788
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700790 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791 return trackHandle;
792}
793
Glenn Kasten2c073da2016-02-26 09:14:08 -0800794uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795{
796 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800797 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800799 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 return 0;
801 }
802 return thread->sampleRate();
803}
804
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800805audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700806{
807 Mutex::Autolock _l(mLock);
808 PlaybackThread *thread = checkPlaybackThread_l(output);
809 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000810 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800811 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 }
813 return thread->format();
814}
815
Glenn Kasten2c073da2016-02-26 09:14:08 -0800816size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817{
818 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800819 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800821 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700822 return 0;
823 }
Glenn Kasten58912562012-04-03 10:45:00 -0700824 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
825 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826 return thread->frameCount();
827}
828
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700829size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
830{
831 Mutex::Autolock _l(mLock);
832 ThreadBase *thread = checkThread_l(ioHandle);
833 if (thread == NULL) {
834 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
835 return 0;
836 }
837 return thread->frameCountHAL();
838}
839
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800840uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841{
842 Mutex::Autolock _l(mLock);
843 PlaybackThread *thread = checkPlaybackThread_l(output);
844 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800845 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 return 0;
847 }
848 return thread->latency();
849}
850
851status_t AudioFlinger::setMasterVolume(float value)
852{
Eric Laurenta1884f92011-08-23 08:25:03 -0700853 status_t ret = initCheck();
854 if (ret != NO_ERROR) {
855 return ret;
856 }
857
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 // check calling permissions
859 if (!settingsAllowed()) {
860 return PERMISSION_DENIED;
861 }
862
Eric Laurenta4c5a552012-03-29 10:12:40 -0700863 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700864 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700865
John Grossmanee578c02012-07-23 17:05:46 -0700866 // Set master volume in the HALs which support it.
867 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
868 AutoMutex lock(mHardwareLock);
869 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800870
John Grossmanee578c02012-07-23 17:05:46 -0700871 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
872 if (dev->canSetMasterVolume()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700873 dev->hwDevice()->setMasterVolume(value);
Eric Laurent93575202011-01-18 18:39:02 -0800874 }
John Grossmanee578c02012-07-23 17:05:46 -0700875 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700877
John Grossmanee578c02012-07-23 17:05:46 -0700878 // Now set the master volume in each playback thread. Playback threads
879 // assigned to HALs which do not have master volume support will apply
880 // master volume during the mix operation. Threads with HALs which do
881 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700882 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
883 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
884 continue;
885 }
John Grossmanee578c02012-07-23 17:05:46 -0700886 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700887 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888
889 return NO_ERROR;
890}
891
Glenn Kastenf78aee72012-01-04 11:00:47 -0800892status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700893{
Eric Laurenta1884f92011-08-23 08:25:03 -0700894 status_t ret = initCheck();
895 if (ret != NO_ERROR) {
896 return ret;
897 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898
899 // check calling permissions
900 if (!settingsAllowed()) {
901 return PERMISSION_DENIED;
902 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800903 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000904 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905 return BAD_VALUE;
906 }
907
908 { // scope for the lock
909 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700910 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700912 ret = dev->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913 mHardwareStatus = AUDIO_HW_IDLE;
914 }
915
916 if (NO_ERROR == ret) {
917 Mutex::Autolock _l(mLock);
918 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800919 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700920 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921 }
922
923 return ret;
924}
925
926status_t AudioFlinger::setMicMute(bool state)
927{
Eric Laurenta1884f92011-08-23 08:25:03 -0700928 status_t ret = initCheck();
929 if (ret != NO_ERROR) {
930 return ret;
931 }
932
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933 // check calling permissions
934 if (!settingsAllowed()) {
935 return PERMISSION_DENIED;
936 }
937
938 AutoMutex lock(mHardwareLock);
939 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700940 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700941 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
942 status_t result = dev->setMicMute(state);
Eric Laurent2f035f52014-09-14 12:11:52 -0700943 if (result != NO_ERROR) {
944 ret = result;
945 }
946 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 mHardwareStatus = AUDIO_HW_IDLE;
948 return ret;
949}
950
951bool AudioFlinger::getMicMute() const
952{
Eric Laurenta1884f92011-08-23 08:25:03 -0700953 status_t ret = initCheck();
954 if (ret != NO_ERROR) {
955 return false;
956 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800957 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700958 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800959 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800961 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700962 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
963 status_t result = dev->getMicMute(&state);
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800964 if (result == NO_ERROR) {
965 mute = mute && state;
966 }
967 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800969
970 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971}
972
973status_t AudioFlinger::setMasterMute(bool muted)
974{
John Grossmand8f178d2012-07-20 14:51:35 -0700975 status_t ret = initCheck();
976 if (ret != NO_ERROR) {
977 return ret;
978 }
979
Mathias Agopian65ab4712010-07-14 17:59:35 -0700980 // check calling permissions
981 if (!settingsAllowed()) {
982 return PERMISSION_DENIED;
983 }
984
John Grossmanee578c02012-07-23 17:05:46 -0700985 Mutex::Autolock _l(mLock);
986 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700987
John Grossmanee578c02012-07-23 17:05:46 -0700988 // Set master mute in the HALs which support it.
989 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
990 AutoMutex lock(mHardwareLock);
991 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700992
John Grossmanee578c02012-07-23 17:05:46 -0700993 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
994 if (dev->canSetMasterMute()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700995 dev->hwDevice()->setMasterMute(muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700996 }
John Grossmanee578c02012-07-23 17:05:46 -0700997 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700998 }
999
John Grossmanee578c02012-07-23 17:05:46 -07001000 // Now set the master mute in each playback thread. Playback threads
1001 // assigned to HALs which do not have master mute support will apply master
1002 // mute during the mix operation. Threads with HALs which do support master
1003 // mute will simply ignore the setting.
Eric Laurent6acd1d42017-01-04 14:23:29 -08001004 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1005 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1006 volumeInterfaces[i]->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -07001007 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008
1009 return NO_ERROR;
1010}
1011
1012float AudioFlinger::masterVolume() const
1013{
Glenn Kasten98067102011-12-13 11:47:54 -08001014 Mutex::Autolock _l(mLock);
1015 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016}
1017
1018bool AudioFlinger::masterMute() const
1019{
Glenn Kasten98067102011-12-13 11:47:54 -08001020 Mutex::Autolock _l(mLock);
1021 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022}
1023
John Grossman4ff14ba2012-02-08 16:37:41 -08001024float AudioFlinger::masterVolume_l() const
1025{
John Grossman4ff14ba2012-02-08 16:37:41 -08001026 return mMasterVolume;
1027}
1028
John Grossmand8f178d2012-07-20 14:51:35 -07001029bool AudioFlinger::masterMute_l() const
1030{
John Grossmanee578c02012-07-23 17:05:46 -07001031 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -07001032}
1033
Eric Laurent223fd5c2014-11-11 13:43:36 -08001034status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
1035{
1036 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001037 ALOGW("checkStreamType() invalid stream %d", stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001038 return BAD_VALUE;
1039 }
1040 pid_t caller = IPCThreadState::self()->getCallingPid();
1041 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001042 ALOGW("checkStreamType() pid %d cannot use internal stream type %d", caller, stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001043 return PERMISSION_DENIED;
1044 }
1045
1046 return NO_ERROR;
1047}
1048
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001049status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
1050 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051{
1052 // check calling permissions
1053 if (!settingsAllowed()) {
1054 return PERMISSION_DENIED;
1055 }
1056
Eric Laurent223fd5c2014-11-11 13:43:36 -08001057 status_t status = checkStreamType(stream);
1058 if (status != NO_ERROR) {
1059 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001061 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062
1063 AutoMutex lock(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001064 Vector<VolumeInterface *> volumeInterfaces;
Glenn Kasten142f5192014-03-25 17:44:59 -07001065 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001066 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1067 if (volumeInterface == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068 return BAD_VALUE;
1069 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001070 volumeInterfaces.add(volumeInterface);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071 }
1072
1073 mStreamTypes[stream].volume = value;
1074
Eric Laurent6acd1d42017-01-04 14:23:29 -08001075 if (volumeInterfaces.size() == 0) {
1076 volumeInterfaces = getAllVolumeInterfaces_l();
1077 }
1078 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1079 volumeInterfaces[i]->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001080 }
1081
1082 return NO_ERROR;
1083}
1084
Glenn Kastenfff6d712012-01-12 16:38:12 -08001085status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001086{
1087 // check calling permissions
1088 if (!settingsAllowed()) {
1089 return PERMISSION_DENIED;
1090 }
1091
Eric Laurent223fd5c2014-11-11 13:43:36 -08001092 status_t status = checkStreamType(stream);
1093 if (status != NO_ERROR) {
1094 return status;
1095 }
1096 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1097
1098 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001099 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001100 return BAD_VALUE;
1101 }
1102
Eric Laurent93575202011-01-18 18:39:02 -08001103 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104 mStreamTypes[stream].mute = muted;
Eric Laurent6acd1d42017-01-04 14:23:29 -08001105 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1106 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1107 volumeInterfaces[i]->setStreamMute(stream, muted);
1108 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001109
1110 return NO_ERROR;
1111}
1112
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001113float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001114{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001115 status_t status = checkStreamType(stream);
1116 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117 return 0.0f;
1118 }
1119
1120 AutoMutex lock(mLock);
1121 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001122 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001123 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1124 if (volumeInterface != NULL) {
1125 volume = volumeInterface->streamVolume(stream);
1126 } else {
1127 volume = 0.0f;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001130 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001131 }
1132
1133 return volume;
1134}
1135
Glenn Kastenfff6d712012-01-12 16:38:12 -08001136bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001138 status_t status = checkStreamType(stream);
1139 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001140 return true;
1141 }
1142
Glenn Kasten6637baa2012-01-09 09:40:36 -08001143 AutoMutex lock(mLock);
1144 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001145}
1146
Eric Laurent054d9d32015-04-24 08:48:48 -07001147
1148void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1149{
1150 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1151 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1152 }
1153}
1154
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001155status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001156{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001157 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1158 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001159
Mathias Agopian65ab4712010-07-14 17:59:35 -07001160 // check calling permissions
1161 if (!settingsAllowed()) {
1162 return PERMISSION_DENIED;
1163 }
1164
Glenn Kasten142f5192014-03-25 17:44:59 -07001165 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1166 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001167 Mutex::Autolock _l(mLock);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001168 // result will remain NO_INIT if no audio device is present
1169 status_t final_result = NO_INIT;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001170 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001171 AutoMutex lock(mHardwareLock);
1172 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1173 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001174 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1175 status_t result = dev->setParameters(keyValuePairs);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001176 // return success if at least one audio device accepts the parameters as not all
1177 // HALs are requested to support all parameters. If no audio device supports the
1178 // requested parameters, the last error is reported.
1179 if (final_result != NO_ERROR) {
1180 final_result = result;
1181 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001182 }
1183 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001184 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001185 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1186 AudioParameter param = AudioParameter(keyValuePairs);
1187 String8 value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001188 if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) {
1189 bool btNrecIsOff = (value == AudioParameter::valueOff);
Eric Laurentbee53372011-08-29 12:42:48 -07001190 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001191 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1192 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001193 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001194 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1195 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001196 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001197 // suspend effects associated with those session IDs
1198 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001199 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001200 thread->setEffectSuspended(FX_IID_AEC,
1201 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001202 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001203 thread->setEffectSuspended(FX_IID_NS,
1204 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001205 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001206 }
1207 }
Eric Laurentbee53372011-08-29 12:42:48 -07001208 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001209 }
1210 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001211 String8 screenState;
1212 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
Mikhail Naganov00260b52016-10-13 12:54:24 -07001213 bool isOff = (screenState == AudioParameter::valueOff);
Eric Laurent81784c32012-11-19 14:55:58 -08001214 if (isOff != (AudioFlinger::mScreenState & 1)) {
1215 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001216 }
1217 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001218 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219 }
1220
1221 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1222 // and the thread is exited once the lock is released
1223 sp<ThreadBase> thread;
1224 {
1225 Mutex::Autolock _l(mLock);
1226 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001227 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001228 thread = checkRecordThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001229 if (thread == 0) {
1230 thread = checkMmapThread_l(ioHandle);
1231 }
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001232 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001233 // indicate output device change to all input threads for pre processing
1234 AudioParameter param = AudioParameter(keyValuePairs);
1235 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001236 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1237 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001238 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001239 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001240 }
1241 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001242 if (thread != 0) {
1243 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 }
1245 return BAD_VALUE;
1246}
1247
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001248String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001249{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001250 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1251 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001252
Eric Laurenta4c5a552012-03-29 10:12:40 -07001253 Mutex::Autolock _l(mLock);
1254
Glenn Kasten142f5192014-03-25 17:44:59 -07001255 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001256 String8 out_s8;
1257
Dima Zavin799a70e2011-04-18 16:57:27 -07001258 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001259 String8 s;
1260 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001261 {
1262 AutoMutex lock(mHardwareLock);
1263 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001264 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1265 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001266 mHardwareStatus = AUDIO_HW_IDLE;
1267 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001268 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001269 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001270 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001271 }
1272
Eric Laurent6acd1d42017-01-04 14:23:29 -08001273 ThreadBase *thread = (ThreadBase *)checkPlaybackThread_l(ioHandle);
1274 if (thread == NULL) {
1275 thread = (ThreadBase *)checkRecordThread_l(ioHandle);
1276 if (thread == NULL) {
1277 thread = (ThreadBase *)checkMmapThread_l(ioHandle);
1278 if (thread == NULL) {
1279 String8("");
1280 }
1281 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001282 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001283 return thread->getParameters(keys);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001284}
1285
Glenn Kastendd8104c2012-07-02 12:42:44 -07001286size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1287 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288{
Eric Laurenta1884f92011-08-23 08:25:03 -07001289 status_t ret = initCheck();
1290 if (ret != NO_ERROR) {
1291 return 0;
1292 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001293 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001294 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001295 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001296 return 0;
1297 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001298
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001299 AutoMutex lock(mHardwareLock);
1300 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001301 audio_config_t config, proposed;
1302 memset(&proposed, 0, sizeof(proposed));
1303 proposed.sample_rate = sampleRate;
1304 proposed.channel_mask = channelMask;
1305 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001306
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001307 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001308 size_t frames;
1309 for (;;) {
1310 // Note: config is currently a const parameter for get_input_buffer_size()
1311 // but we use a copy from proposed in case config changes from the call.
1312 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001313 status_t result = dev->getInputBufferSize(&config, &frames);
1314 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001315 break; // hal success, config is the result
1316 }
1317 // change one parameter of the configuration each iteration to a more "common" value
1318 // to see if the device will support it.
1319 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1320 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1321 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1322 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1323 } else {
1324 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1325 "format %#x, channelMask 0x%X",
1326 sampleRate, format, channelMask);
1327 break; // retries failed, break out of loop with frames == 0.
1328 }
1329 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001330 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001331 if (frames > 0 && config.sample_rate != sampleRate) {
1332 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1333 }
1334 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001335}
1336
Glenn Kasten5f972c02014-01-13 09:59:31 -08001337uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001338{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001339 Mutex::Autolock _l(mLock);
1340
1341 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1342 if (recordThread != NULL) {
1343 return recordThread->getInputFramesLost();
1344 }
1345 return 0;
1346}
1347
1348status_t AudioFlinger::setVoiceVolume(float value)
1349{
Eric Laurenta1884f92011-08-23 08:25:03 -07001350 status_t ret = initCheck();
1351 if (ret != NO_ERROR) {
1352 return ret;
1353 }
1354
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355 // check calling permissions
1356 if (!settingsAllowed()) {
1357 return PERMISSION_DENIED;
1358 }
1359
1360 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001361 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001362 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001363 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001364 mHardwareStatus = AUDIO_HW_IDLE;
1365
1366 return ret;
1367}
1368
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001369status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001370 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001371{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372 Mutex::Autolock _l(mLock);
1373
1374 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1375 if (playbackThread != NULL) {
1376 return playbackThread->getRenderPosition(halFrames, dspFrames);
1377 }
1378
1379 return BAD_VALUE;
1380}
1381
1382void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1383{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001384 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001385 if (client == 0) {
1386 return;
1387 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001388 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001389 {
1390 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001391 if (mNotificationClients.indexOfKey(pid) < 0) {
1392 sp<NotificationClient> notificationClient = new NotificationClient(this,
1393 client,
1394 pid);
1395 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001396
Eric Laurent021cf962014-05-13 10:18:14 -07001397 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001398
Marco Nelissen06b46062014-11-14 07:58:25 -08001399 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001400 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001401 }
1402 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403
Eric Laurent021cf962014-05-13 10:18:14 -07001404 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001405 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001406 // the config change is always sent from playback or record threads to avoid deadlock
1407 // with AudioSystem::gLock
1408 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1409 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1410 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001411
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001412 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1413 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414 }
1415}
1416
1417void AudioFlinger::removeNotificationClient(pid_t pid)
1418{
1419 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001420 {
1421 Mutex::Autolock _cl(mClientLock);
1422 mNotificationClients.removeItem(pid);
1423 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001424
Steve Block3856b092011-10-20 11:56:00 +01001425 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001426 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001427 bool removed = false;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001428 for (size_t i = 0; i < num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001429 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001430 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001431 if (ref->mPid == pid) {
1432 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001433 mAudioSessionRefs.removeAt(i);
1434 delete ref;
1435 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001436 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001437 } else {
1438 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001439 }
1440 }
1441 if (removed) {
1442 purgeStaleEffects_l();
1443 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444}
1445
Eric Laurent73e26b62015-04-27 16:55:58 -07001446void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001447 const sp<AudioIoDescriptor>& ioDesc,
1448 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449{
Eric Laurent021cf962014-05-13 10:18:14 -07001450 Mutex::Autolock _l(mClientLock);
1451 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001452 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001453 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1454 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1455 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456 }
1457}
1458
Eric Laurent021cf962014-05-13 10:18:14 -07001459// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001460void AudioFlinger::removeClient_l(pid_t pid)
1461{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001462 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001463 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001464 mClients.removeItem(pid);
1465}
1466
Eric Laurent717e1282012-06-29 16:36:52 -07001467// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001468sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1469 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001470{
1471 sp<PlaybackThread> thread;
1472
1473 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1474 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1475 ALOG_ASSERT(thread == 0);
1476 thread = mPlaybackThreads.valueAt(i);
1477 }
1478 }
1479
1480 return thread;
1481}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482
Mathias Agopian65ab4712010-07-14 17:59:35 -07001483
Mathias Agopian65ab4712010-07-14 17:59:35 -07001484
1485// ----------------------------------------------------------------------------
1486
1487AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1488 : RefBase(),
1489 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001490 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001491{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301492 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1493 heapSize *= 1024;
1494 if (!heapSize) {
1495 heapSize = kClientSharedHeapSizeBytes;
1496 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1497 // invalidated tracks
1498 if (!audioFlinger->isLowRamDevice()) {
1499 heapSize *= kClientSharedHeapSizeMultiplier;
1500 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001501 }
1502 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001503}
1504
Eric Laurent021cf962014-05-13 10:18:14 -07001505// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001506AudioFlinger::Client::~Client()
1507{
1508 mAudioFlinger->removeClient_l(mPid);
1509}
1510
Glenn Kasten435dbe62012-01-30 10:15:48 -08001511sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512{
1513 return mMemoryDealer;
1514}
1515
1516// ----------------------------------------------------------------------------
1517
1518AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1519 const sp<IAudioFlingerClient>& client,
1520 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001521 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522{
1523}
1524
1525AudioFlinger::NotificationClient::~NotificationClient()
1526{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527}
1528
Glenn Kasten0f11b512014-01-31 16:18:54 -08001529void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001530{
1531 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001532 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533}
1534
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001535// ----------------------------------------------------------------------------
1536AudioFlinger::MediaLogNotifier::MediaLogNotifier()
1537 : mPendingRequests(false) {}
1538
1539
1540void AudioFlinger::MediaLogNotifier::requestMerge() {
1541 AutoMutex _l(mMutex);
1542 mPendingRequests = true;
1543 mCond.signal();
1544}
1545
1546bool AudioFlinger::MediaLogNotifier::threadLoop() {
1547 // Wait until there are pending requests
1548 {
1549 AutoMutex _l(mMutex);
1550 mPendingRequests = false; // to ignore past requests
1551 while (!mPendingRequests) {
1552 mCond.wait(mMutex);
1553 // TODO may also need an exitPending check
1554 }
1555 mPendingRequests = false;
1556 }
1557 // Execute the actual MediaLogService binder call and ignore extra requests for a while
1558 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
1559 if (binder != 0) {
1560 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
1561 mediaLogService->requestMergeWakeup();
1562 }
1563 usleep(kPostTriggerSleepPeriod);
1564 return true;
1565}
1566
1567void AudioFlinger::requestLogMerge() {
1568 mMediaLogNotifier->requestMerge();
1569}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001570
1571// ----------------------------------------------------------------------------
1572
1573sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001574 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001575 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001576 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001577 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001578 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001579 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -07001580 audio_input_flags_t *flags,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001581 pid_t pid,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001582 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001583 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001584 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001585 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001586 sp<IMemory>& cblk,
1587 sp<IMemory>& buffers,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001588 status_t *status,
1589 audio_port_handle_t portId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001590{
1591 sp<RecordThread::RecordTrack> recordTrack;
1592 sp<RecordHandle> recordHandle;
1593 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001595 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001596
Glenn Kastend776ac62014-05-07 09:16:09 -07001597 cblk.clear();
1598 buffers.clear();
1599
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001600 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001601 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1602 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001603 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001604 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1605 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001606 updatePid = true;
1607 }
1608
1609 if (updatePid) {
1610 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
1611 ALOGW_IF(pid != -1 && pid != callingPid,
1612 "%s uid %d pid %d tried to pass itself off as pid %d",
1613 __func__, callingUid, callingPid, pid);
1614 pid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001615 }
1616
Mathias Agopian65ab4712010-07-14 17:59:35 -07001617 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001618 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001619 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001620 lStatus = PERMISSION_DENIED;
1621 goto Exit;
1622 }
1623
Glenn Kasten53b5d092014-02-05 10:00:23 -08001624 // further sample rate checks are performed by createRecordTrack_l()
1625 if (sampleRate == 0) {
1626 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1627 lStatus = BAD_VALUE;
1628 goto Exit;
1629 }
1630
Andy Hung6770c6f2015-04-07 13:43:36 -07001631 // we don't yet support anything other than linear PCM
1632 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001633 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001634 lStatus = BAD_VALUE;
1635 goto Exit;
1636 }
1637
Glenn Kasten53b5d092014-02-05 10:00:23 -08001638 // further channel mask checks are performed by createRecordTrack_l()
1639 if (!audio_is_input_channel(channelMask)) {
1640 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1641 lStatus = BAD_VALUE;
1642 goto Exit;
1643 }
1644
Glenn Kasten05997e22014-03-13 15:08:33 -07001645 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001646 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001647 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001649 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001650 lStatus = BAD_VALUE;
1651 goto Exit;
1652 }
1653
Eric Laurent021cf962014-05-13 10:18:14 -07001654 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001655
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001656 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001657 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1658 lStatus = BAD_VALUE;
1659 goto Exit;
1660 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001661 lSessionId = *sessionId;
1662 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001663 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001664 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001665 if (sessionId != NULL) {
1666 *sessionId = lSessionId;
1667 }
1668 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001669 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001670
Glenn Kasten1879fff2012-07-11 15:36:59 -07001671 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001672 frameCount, lSessionId, notificationFrames,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001673 clientUid, flags, tid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001674 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001675
1676 if (lStatus == NO_ERROR) {
1677 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1678 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001679 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001680 if (chain != 0) {
1681 Mutex::Autolock _l(thread->mLock);
1682 thread->addEffectChain_l(chain);
1683 }
1684 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001685 }
Glenn Kastene198c362013-08-13 09:13:36 -07001686
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001687 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001688 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001689 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001690 // Don't hold mClientLock when releasing the reference on the track as the
1691 // destructor will acquire it.
1692 {
1693 Mutex::Autolock _cl(mClientLock);
1694 client.clear();
1695 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001696 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001697 goto Exit;
1698 }
1699
Glenn Kastend776ac62014-05-07 09:16:09 -07001700 cblk = recordTrack->getCblk();
1701 buffers = recordTrack->getBuffers();
1702
Glenn Kasten2fc14732013-08-05 14:58:14 -07001703 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001704 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001705
1706Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001707 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001708 return recordHandle;
1709}
1710
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001711
1712
Mathias Agopian65ab4712010-07-14 17:59:35 -07001713// ----------------------------------------------------------------------------
1714
Eric Laurenta4c5a552012-03-29 10:12:40 -07001715audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1716{
Eric Laurent44622db2014-08-01 19:00:33 -07001717 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001718 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001719 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001720 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001721 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001722 }
1723 Mutex::Autolock _l(mLock);
1724 return loadHwModule_l(name);
1725}
1726
1727// loadHwModule_l() must be called with AudioFlinger::mLock held
1728audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1729{
1730 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1731 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1732 ALOGW("loadHwModule() module %s already loaded", name);
1733 return mAudioHwDevs.keyAt(i);
1734 }
1735 }
1736
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001737 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001738
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001739 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001740 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001741 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001742 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001743 }
1744
1745 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001746 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07001747 mHardwareStatus = AUDIO_HW_IDLE;
1748 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001749 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001750 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001751 }
1752
John Grossmanee578c02012-07-23 17:05:46 -07001753 // Check and cache this HAL's level of support for master mute and master
1754 // volume. If this is the first HAL opened, and it supports the get
1755 // methods, use the initial values provided by the HAL as the current
1756 // master mute and volume settings.
1757
1758 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1759 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001760 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001761
1762 if (0 == mAudioHwDevs.size()) {
1763 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001764 float mv;
1765 if (OK == dev->getMasterVolume(&mv)) {
1766 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07001767 }
1768
1769 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001770 bool mm;
1771 if (OK == dev->getMasterMute(&mm)) {
1772 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07001773 }
1774 }
1775
Eric Laurenta4c5a552012-03-29 10:12:40 -07001776 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001777 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07001778 flags = static_cast<AudioHwDevice::Flags>(flags |
1779 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1780 }
1781
1782 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001783 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07001784 flags = static_cast<AudioHwDevice::Flags>(flags |
1785 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1786 }
1787
Eric Laurenta4c5a552012-03-29 10:12:40 -07001788 mHardwareStatus = AUDIO_HW_IDLE;
1789 }
1790
Glenn Kastena13cde92016-03-28 15:26:02 -07001791 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001792 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001793
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001794 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001795
1796 return handle;
1797
1798}
1799
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001800// ----------------------------------------------------------------------------
1801
Glenn Kasten3b16c762012-11-14 08:44:39 -08001802uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001803{
1804 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001805 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001806 return thread != NULL ? thread->sampleRate() : 0;
1807}
1808
Glenn Kastene33054e2012-11-14 12:54:39 -08001809size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001810{
1811 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001812 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001813 return thread != NULL ? thread->frameCountHAL() : 0;
1814}
1815
1816// ----------------------------------------------------------------------------
1817
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001818status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1819{
1820 uid_t uid = IPCThreadState::self()->getCallingUid();
1821 if (uid != AID_SYSTEM) {
1822 return PERMISSION_DENIED;
1823 }
1824 Mutex::Autolock _l(mLock);
1825 if (mIsDeviceTypeKnown) {
1826 return INVALID_OPERATION;
1827 }
1828 mIsLowRamDevice = isLowRamDevice;
1829 mIsDeviceTypeKnown = true;
1830 return NO_ERROR;
1831}
1832
Eric Laurent93c3d412014-08-01 14:48:35 -07001833audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1834{
1835 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001836
1837 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1838 if (index >= 0) {
1839 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1840 mHwAvSyncIds.valueAt(index), sessionId);
1841 return mHwAvSyncIds.valueAt(index);
1842 }
1843
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001844 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07001845 if (dev == NULL) {
1846 return AUDIO_HW_SYNC_INVALID;
1847 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001848 String8 reply;
1849 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001850 if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001851 param = AudioParameter(reply);
1852 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001853
1854 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001855 if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001856 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1857 return AUDIO_HW_SYNC_INVALID;
1858 }
1859
1860 // allow only one session for a given HW A/V sync ID.
1861 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1862 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1863 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1864 value, mHwAvSyncIds.keyAt(i));
1865 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001866 break;
1867 }
1868 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001869
1870 mHwAvSyncIds.add(sessionId, value);
1871
1872 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1873 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1874 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07001875 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001876 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001877 param.addInt(String8(AudioParameter::keyStreamHwAvSync), value);
Eric Laurentfa90e842014-10-17 18:12:31 -07001878 thread->setParameters(param.toString());
1879 break;
1880 }
1881 }
1882
1883 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1884 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001885}
1886
Eric Laurent72e3f392015-05-20 14:43:50 -07001887status_t AudioFlinger::systemReady()
1888{
1889 Mutex::Autolock _l(mLock);
1890 ALOGI("%s", __FUNCTION__);
1891 if (mSystemReady) {
1892 ALOGW("%s called twice", __FUNCTION__);
1893 return NO_ERROR;
1894 }
1895 mSystemReady = true;
1896 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1897 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1898 thread->systemReady();
1899 }
1900 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1901 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1902 thread->systemReady();
1903 }
1904 return NO_ERROR;
1905}
1906
Eric Laurentfa90e842014-10-17 18:12:31 -07001907// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1908void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1909{
1910 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1911 if (index >= 0) {
1912 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1913 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1914 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001915 param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId);
Eric Laurentfa90e842014-10-17 18:12:31 -07001916 thread->setParameters(param.toString());
1917 }
1918}
1919
1920
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001921// ----------------------------------------------------------------------------
1922
Eric Laurent83b88082014-06-20 18:31:16 -07001923
Eric Laurent6acd1d42017-01-04 14:23:29 -08001924sp<AudioFlinger::ThreadBase> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001925 audio_io_handle_t *output,
1926 audio_config_t *config,
1927 audio_devices_t devices,
1928 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001929 audio_output_flags_t flags)
1930{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001931 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001932 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001933 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001934 }
1935
Eric Laurentcf2c0212014-07-25 16:20:43 -07001936 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001937 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1938 } else {
1939 // Audio Policy does not currently request a specific output handle.
1940 // If this is ever needed, see openInput_l() for example code.
1941 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1942 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001943 }
Eric Laurent83b88082014-06-20 18:31:16 -07001944
1945 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1946
Eric Laurent83b88082014-06-20 18:31:16 -07001947 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001948 // This if statement allows overriding the audio policy settings
1949 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1950 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1951 // Check only for Normal Mixing mode
1952 if (kEnableExtendedPrecision) {
1953 // Specify format (uncomment one below to choose)
1954 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1955 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1956 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1957 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001958 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001959 }
1960 if (kEnableExtendedChannels) {
1961 // Specify channel mask (uncomment one below to choose)
1962 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1963 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1964 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1965 }
Eric Laurent83b88082014-06-20 18:31:16 -07001966 }
1967
Phil Burk062e67a2015-02-11 13:40:50 -08001968 AudioStreamOut *outputStream = NULL;
1969 status_t status = outHwDev->openOutputStream(
1970 &outputStream,
1971 *output,
1972 devices,
1973 flags,
1974 config,
1975 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001976
1977 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001978
Phil Burk062e67a2015-02-11 13:40:50 -08001979 if (status == NO_ERROR) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001980 if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
1981 sp<MmapPlaybackThread> thread =
1982 new MmapPlaybackThread(this, *output, outHwDev, outputStream,
1983 devices, AUDIO_DEVICE_NONE, mSystemReady);
1984 mMmapThreads.add(*output, thread);
1985 ALOGV("openOutput_l() created mmap playback thread: ID %d thread %p",
1986 *output, thread.get());
1987 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07001988 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001989 sp<PlaybackThread> thread;
1990 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1991 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
1992 ALOGV("openOutput_l() created offload output: ID %d thread %p",
1993 *output, thread.get());
1994 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1995 || !isValidPcmSinkFormat(config->format)
1996 || !isValidPcmSinkChannelMask(config->channel_mask)) {
1997 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
1998 ALOGV("openOutput_l() created direct output: ID %d thread %p",
1999 *output, thread.get());
2000 } else {
2001 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
2002 ALOGV("openOutput_l() created mixer output: ID %d thread %p",
2003 *output, thread.get());
2004 }
2005 mPlaybackThreads.add(*output, thread);
2006 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07002007 }
Eric Laurent83b88082014-06-20 18:31:16 -07002008 }
2009
2010 return 0;
2011}
2012
Eric Laurentcf2c0212014-07-25 16:20:43 -07002013status_t AudioFlinger::openOutput(audio_module_handle_t module,
2014 audio_io_handle_t *output,
2015 audio_config_t *config,
2016 audio_devices_t *devices,
2017 const String8& address,
2018 uint32_t *latencyMs,
2019 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002020{
Glenn Kastend3bb6452016-12-05 18:14:37 -08002021 ALOGI("openOutput() this %p, module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, "
2022 "flags %x",
Eric Laurent6acd1d42017-01-04 14:23:29 -08002023 this, module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002024 (devices != NULL) ? *devices : 0,
2025 config->sample_rate,
2026 config->format,
2027 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002028 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002029
Yunlian Jiang32ba9862017-01-31 15:55:00 -08002030 if (devices == NULL || *devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002031 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002032 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002033
Mathias Agopian65ab4712010-07-14 17:59:35 -07002034 Mutex::Autolock _l(mLock);
2035
Eric Laurent6acd1d42017-01-04 14:23:29 -08002036 sp<ThreadBase> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002037 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002038 if ((flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) {
2039 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
2040 *latencyMs = playbackThread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002041
Eric Laurent6acd1d42017-01-04 14:23:29 -08002042 // notify client processes of the new output creation
2043 playbackThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002044
Eric Laurent6acd1d42017-01-04 14:23:29 -08002045 // the first primary output opened designates the primary hw device
2046 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2047 ALOGI("Using module %d has the primary audio interface", module);
2048 mPrimaryHardwareDev = playbackThread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002049
Eric Laurent6acd1d42017-01-04 14:23:29 -08002050 AutoMutex lock(mHardwareLock);
2051 mHardwareStatus = AUDIO_HW_SET_MODE;
2052 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
2053 mHardwareStatus = AUDIO_HW_IDLE;
2054 }
2055 } else {
2056 MmapThread *mmapThread = (MmapThread *)thread.get();
2057 mmapThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002058 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002059 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002060 }
2061
Eric Laurentcf2c0212014-07-25 16:20:43 -07002062 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002063}
2064
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002065audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
2066 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002067{
2068 Mutex::Autolock _l(mLock);
2069 MixerThread *thread1 = checkMixerThread_l(output1);
2070 MixerThread *thread2 = checkMixerThread_l(output2);
2071
2072 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002073 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
2074 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07002075 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002076 }
2077
Glenn Kasteneeecb982016-02-26 10:44:04 -08002078 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07002079 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002080 thread->addOutputTrack(thread2);
2081 mPlaybackThreads.add(id, thread);
2082 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002083 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002084 return id;
2085}
2086
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002087status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002088{
Glenn Kastend96c5722012-04-25 13:44:49 -07002089 return closeOutput_nonvirtual(output);
2090}
2091
2092status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
2093{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002094 // keep strong reference on the playback thread so that
2095 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002096 sp<PlaybackThread> playbackThread;
2097 sp<MmapPlaybackThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002098 {
2099 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002100 playbackThread = checkPlaybackThread_l(output);
2101 if (playbackThread != NULL) {
2102 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002103
Eric Laurent6acd1d42017-01-04 14:23:29 -08002104 if (playbackThread->type() == ThreadBase::MIXER) {
2105 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2106 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
2107 DuplicatingThread *dupThread =
2108 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
2109 dupThread->removeOutputTrack((MixerThread *)playbackThread.get());
2110 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002111 }
2112 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002113
2114
Eric Laurent6acd1d42017-01-04 14:23:29 -08002115 mPlaybackThreads.removeItem(output);
2116 // save all effects to the default thread
2117 if (mPlaybackThreads.size()) {
2118 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
2119 if (dstThread != NULL) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002120 // audioflinger lock is held so order of thread lock acquisition doesn't matter
Eric Laurent6acd1d42017-01-04 14:23:29 -08002121 Mutex::Autolock _dl(dstThread->mLock);
2122 Mutex::Autolock _sl(playbackThread->mLock);
2123 Vector< sp<EffectChain> > effectChains = playbackThread->getEffectChains_l();
2124 for (size_t i = 0; i < effectChains.size(); i ++) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002125 moveEffectChain_l(effectChains[i]->sessionId(), playbackThread.get(),
2126 dstThread, true);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002127 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002128 }
2129 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002130 } else {
2131 mmapThread = (MmapPlaybackThread *)checkMmapThread_l(output);
2132 if (mmapThread == 0) {
2133 return BAD_VALUE;
2134 }
2135 mMmapThreads.removeItem(output);
Phil Burkc0c70e32017-02-09 13:18:38 -08002136 ALOGD("closing mmapThread %p", mmapThread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002137 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002138 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2139 ioDesc->mIoHandle = output;
2140 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002141 }
Glenn Kastenb28686f2012-01-06 08:39:38 -08002142 // The thread entity (active unit of execution) is no longer running here,
2143 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002144
Eric Laurent6acd1d42017-01-04 14:23:29 -08002145 if (playbackThread != 0) {
2146 playbackThread->exit();
2147 if (!playbackThread->isDuplicating()) {
2148 closeOutputFinish(playbackThread);
2149 }
2150 } else if (mmapThread != 0) {
Phil Burkc0c70e32017-02-09 13:18:38 -08002151 ALOGD("mmapThread exit()");
Eric Laurent6acd1d42017-01-04 14:23:29 -08002152 mmapThread->exit();
2153 AudioStreamOut *out = mmapThread->clearOutput();
2154 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2155 // from now on thread->mOutput is NULL
2156 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002157 }
2158 return NO_ERROR;
2159}
2160
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002161void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002162{
2163 AudioStreamOut *out = thread->clearOutput();
2164 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2165 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07002166 delete out;
2167}
2168
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002169void AudioFlinger::closeOutputInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002170{
2171 mPlaybackThreads.removeItem(thread->mId);
2172 thread->exit();
2173 closeOutputFinish(thread);
2174}
2175
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002176status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002177{
2178 Mutex::Autolock _l(mLock);
2179 PlaybackThread *thread = checkPlaybackThread_l(output);
2180
2181 if (thread == NULL) {
2182 return BAD_VALUE;
2183 }
2184
Steve Block3856b092011-10-20 11:56:00 +01002185 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002186 thread->suspend();
2187
2188 return NO_ERROR;
2189}
2190
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002191status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192{
2193 Mutex::Autolock _l(mLock);
2194 PlaybackThread *thread = checkPlaybackThread_l(output);
2195
2196 if (thread == NULL) {
2197 return BAD_VALUE;
2198 }
2199
Steve Block3856b092011-10-20 11:56:00 +01002200 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002201
2202 thread->restore();
2203
2204 return NO_ERROR;
2205}
2206
Eric Laurentcf2c0212014-07-25 16:20:43 -07002207status_t AudioFlinger::openInput(audio_module_handle_t module,
2208 audio_io_handle_t *input,
2209 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002210 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002211 const String8& address,
2212 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002213 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002214{
Eric Laurent83b88082014-06-20 18:31:16 -07002215 Mutex::Autolock _l(mLock);
2216
Glenn Kastene7d66712015-03-05 13:46:52 -08002217 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002218 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002219 }
2220
Eric Laurent6acd1d42017-01-04 14:23:29 -08002221 sp<ThreadBase> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002222
2223 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002224 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002225 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002226 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002227 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002228 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002229}
Dima Zavin799a70e2011-04-18 16:57:27 -07002230
Eric Laurent6acd1d42017-01-04 14:23:29 -08002231sp<AudioFlinger::ThreadBase> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002232 audio_io_handle_t *input,
2233 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002234 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002235 const String8& address,
2236 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002237 audio_input_flags_t flags)
2238{
Glenn Kastene7d66712015-03-05 13:46:52 -08002239 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002240 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002241 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002242 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002243 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002244
Glenn Kasteneeecb982016-02-26 10:44:04 -08002245 // Audio Policy can request a specific handle for hardware hotword.
2246 // The goal here is not to re-open an already opened input.
2247 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002248 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002249 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2250 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2251 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2252 return 0;
2253 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2254 // This should not happen in a transient state with current design.
2255 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2256 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002257 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002258
Eric Laurentcf2c0212014-07-25 16:20:43 -07002259 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002260 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002261 sp<StreamInHalInterface> inStream;
2262 status_t status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002263 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Mikhail Naganovf558e022016-11-14 17:45:17 -08002264 ALOGV("openInput_l() openInputStream returned input %p, devices %x, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002265 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002266 inStream.get(),
Mikhail Naganovf558e022016-11-14 17:45:17 -08002267 devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002268 halconfig.sample_rate,
2269 halconfig.format,
2270 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002271 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002272 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002273
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002274 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002275 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002276 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002277 audio_is_linear_pcm(config->format) &&
2278 audio_is_linear_pcm(halconfig.format) &&
2279 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002280 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2281 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002282 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002283 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002284 inStream.clear();
2285 status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002286 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002287 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002288 }
2289
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002290 if (status == NO_ERROR && inStream != 0) {
Eric Laurent05067782016-06-01 18:27:28 -07002291 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002292 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) {
2293 sp<MmapCaptureThread> thread =
2294 new MmapCaptureThread(this, *input,
2295 inHwDev, inputStream,
2296 primaryOutputDevice_l(), devices, mSystemReady);
2297 mMmapThreads.add(*input, thread);
Glenn Kastend3bb6452016-12-05 18:14:37 -08002298 ALOGV("openInput_l() created mmap capture thread: ID %d thread %p", *input,
2299 thread.get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002300 return thread;
2301 } else {
Glenn Kasten46909e72013-02-26 09:20:22 -08002302#ifdef TEE_SINK
Eric Laurent6acd1d42017-01-04 14:23:29 -08002303 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2304 // or (re-)create if current Pipe is idle and does not match the new format
2305 sp<NBAIO_Sink> teeSink;
2306 enum {
2307 TEE_SINK_NO, // don't copy input
2308 TEE_SINK_NEW, // copy input using a new pipe
2309 TEE_SINK_OLD, // copy input using an existing pipe
2310 } kind;
2311 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2312 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
2313 if (!mTeeSinkInputEnabled) {
2314 kind = TEE_SINK_NO;
2315 } else if (!Format_isValid(format)) {
2316 kind = TEE_SINK_NO;
2317 } else if (mRecordTeeSink == 0) {
2318 kind = TEE_SINK_NEW;
2319 } else if (mRecordTeeSink->getStrongCount() != 1) {
2320 kind = TEE_SINK_NO;
2321 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
2322 kind = TEE_SINK_OLD;
2323 } else {
2324 kind = TEE_SINK_NEW;
2325 }
2326 switch (kind) {
2327 case TEE_SINK_NEW: {
2328 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
2329 size_t numCounterOffers = 0;
2330 const NBAIO_Format offers[1] = {format};
2331 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2332 ALOG_ASSERT(index == 0);
2333 PipeReader *pipeReader = new PipeReader(*pipe);
2334 numCounterOffers = 0;
2335 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2336 ALOG_ASSERT(index == 0);
2337 mRecordTeeSink = pipe;
2338 mRecordTeeSource = pipeReader;
2339 teeSink = pipe;
2340 }
2341 break;
2342 case TEE_SINK_OLD:
2343 teeSink = mRecordTeeSink;
2344 break;
2345 case TEE_SINK_NO:
2346 default:
2347 break;
2348 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002349#endif
Eric Laurent6acd1d42017-01-04 14:23:29 -08002350
2351 // Start record thread
2352 // RecordThread requires both input and output device indication to forward to audio
2353 // pre processing modules
2354 sp<RecordThread> thread = new RecordThread(this,
2355 inputStream,
2356 *input,
2357 primaryOutputDevice_l(),
2358 devices,
2359 mSystemReady
2360#ifdef TEE_SINK
2361 , teeSink
2362#endif
2363 );
2364 mRecordThreads.add(*input, thread);
2365 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
2366 return thread;
2367 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002368 }
2369
Eric Laurentcf2c0212014-07-25 16:20:43 -07002370 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002371 return 0;
2372}
2373
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002374status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002375{
Glenn Kastend96c5722012-04-25 13:44:49 -07002376 return closeInput_nonvirtual(input);
2377}
2378
2379status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2380{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002381 // keep strong reference on the record thread so that
2382 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002383 sp<RecordThread> recordThread;
2384 sp<MmapCaptureThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002385 {
2386 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002387 recordThread = checkRecordThread_l(input);
2388 if (recordThread != 0) {
2389 ALOGV("closeInput() %d", input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002390
Eric Laurent6acd1d42017-01-04 14:23:29 -08002391 // If we still have effect chains, it means that a client still holds a handle
2392 // on at least one effect. We must either move the chain to an existing thread with the
2393 // same session ID or put it aside in case a new record thread is opened for a
2394 // new capture on the same session
2395 sp<EffectChain> chain;
2396 {
2397 Mutex::Autolock _sl(recordThread->mLock);
2398 Vector< sp<EffectChain> > effectChains = recordThread->getEffectChains_l();
2399 // Note: maximum one chain per record thread
2400 if (effectChains.size() != 0) {
2401 chain = effectChains[0];
Eric Laurent1b928682014-10-02 19:41:47 -07002402 }
2403 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002404 if (chain != 0) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002405 // first check if a record thread is already opened with a client on same session.
Eric Laurent6acd1d42017-01-04 14:23:29 -08002406 // This should only happen in case of overlap between one thread tear down and the
2407 // creation of its replacement
2408 size_t i;
2409 for (i = 0; i < mRecordThreads.size(); i++) {
2410 sp<RecordThread> t = mRecordThreads.valueAt(i);
2411 if (t == recordThread) {
2412 continue;
2413 }
2414 if (t->hasAudioSession(chain->sessionId()) != 0) {
2415 Mutex::Autolock _l(t->mLock);
2416 ALOGV("closeInput() found thread %d for effect session %d",
2417 t->id(), chain->sessionId());
2418 t->addEffectChain_l(chain);
2419 break;
2420 }
2421 }
Glenn Kastend3bb6452016-12-05 18:14:37 -08002422 // put the chain aside if we could not find a record thread with the same session id
Eric Laurent6acd1d42017-01-04 14:23:29 -08002423 if (i == mRecordThreads.size()) {
2424 putOrphanEffectChain_l(chain);
2425 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002426 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002427 mRecordThreads.removeItem(input);
2428 } else {
2429 mmapThread = (MmapCaptureThread *)checkMmapThread_l(input);
2430 if (mmapThread == 0) {
2431 return BAD_VALUE;
2432 }
2433 mMmapThreads.removeItem(input);
Eric Laurentaaa44472014-09-12 17:41:50 -07002434 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002435 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2436 ioDesc->mIoHandle = input;
2437 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002438 }
Eric Laurent83b88082014-06-20 18:31:16 -07002439 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2440 // we have a different lock for notification client
Eric Laurent6acd1d42017-01-04 14:23:29 -08002441 if (recordThread != 0) {
2442 closeInputFinish(recordThread);
2443 } else if (mmapThread != 0) {
2444 mmapThread->exit();
2445 AudioStreamIn *in = mmapThread->clearInput();
2446 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
2447 // from now on thread->mInput is NULL
2448 delete in;
2449 }
Eric Laurent83b88082014-06-20 18:31:16 -07002450 return NO_ERROR;
2451}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002452
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002453void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002454{
2455 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002456 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002457 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002458 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002459 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002460}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002461
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002462void AudioFlinger::closeInputInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002463{
2464 mRecordThreads.removeItem(thread->mId);
2465 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002466}
2467
Glenn Kastend2304db2014-02-03 07:40:31 -08002468status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002469{
2470 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002471 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002472
2473 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2474 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002475 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002476 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002477 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2478 mMmapThreads[i]->invalidateTracks(stream);
2479 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002480 return NO_ERROR;
2481}
2482
2483
Glenn Kasteneeecb982016-02-26 10:44:04 -08002484audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002485{
Glenn Kasten9d003132016-04-06 14:38:09 -07002486 // This is a binder API, so a malicious client could pass in a bad parameter.
2487 // Check for that before calling the internal API nextUniqueId().
2488 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2489 ALOGE("newAudioUniqueId invalid use %d", use);
2490 return AUDIO_UNIQUE_ID_ALLOCATE;
2491 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002492 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002493}
2494
Glenn Kastend848eb42016-03-08 13:42:11 -08002495void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002496{
2497 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002498 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002499 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2500 if (pid != -1 && (caller == getpid_cached)) {
2501 caller = pid;
2502 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002503
Eric Laurent021cf962014-05-13 10:18:14 -07002504 {
2505 Mutex::Autolock _cl(mClientLock);
2506 // Ignore requests received from processes not known as notification client. The request
2507 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2508 // called from a different pid leaving a stale session reference. Also we don't know how
2509 // to clear this reference if the client process dies.
2510 if (mNotificationClients.indexOfKey(caller) < 0) {
2511 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2512 return;
2513 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002514 }
2515
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002516 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002517 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002518 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002519 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2520 ref->mCnt++;
2521 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002522 return;
2523 }
2524 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002525 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2526 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002527}
2528
Glenn Kastend848eb42016-03-08 13:42:11 -08002529void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002530{
2531 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002532 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002533 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2534 if (pid != -1 && (caller == getpid_cached)) {
2535 caller = pid;
2536 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002537 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002538 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002539 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002540 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2541 ref->mCnt--;
2542 ALOGV(" decremented refcount to %d", ref->mCnt);
2543 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002544 mAudioSessionRefs.removeAt(i);
2545 delete ref;
2546 purgeStaleEffects_l();
2547 }
2548 return;
2549 }
2550 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002551 // If the caller is mediaserver it is likely that the session being released was acquired
2552 // on behalf of a process not in notification clients and we ignore the warning.
2553 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002554}
2555
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002556bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
2557{
2558 size_t num = mAudioSessionRefs.size();
2559 for (size_t i = 0; i < num; i++) {
2560 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2561 if (ref->mSessionid == audioSession) {
2562 return true;
2563 }
2564 }
2565 return false;
2566}
2567
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002568void AudioFlinger::purgeStaleEffects_l() {
2569
Steve Block3856b092011-10-20 11:56:00 +01002570 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002571
2572 Vector< sp<EffectChain> > chains;
2573
2574 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2575 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2576 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2577 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002578 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2579 chains.push(ec);
2580 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002581 }
2582 }
2583 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2584 sp<RecordThread> t = mRecordThreads.valueAt(i);
2585 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2586 sp<EffectChain> ec = t->mEffectChains[j];
2587 chains.push(ec);
2588 }
2589 }
2590
2591 for (size_t i = 0; i < chains.size(); i++) {
2592 sp<EffectChain> ec = chains[i];
2593 int sessionid = ec->sessionId();
2594 sp<ThreadBase> t = ec->mThread.promote();
2595 if (t == 0) {
2596 continue;
2597 }
2598 size_t numsessionrefs = mAudioSessionRefs.size();
2599 bool found = false;
2600 for (size_t k = 0; k < numsessionrefs; k++) {
2601 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002602 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002603 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002604 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002605 found = true;
2606 break;
2607 }
2608 }
2609 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002610 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002611 // remove all effects from the chain
2612 while (ec->mEffects.size()) {
2613 sp<EffectModule> effect = ec->mEffects[0];
2614 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002615 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002616 if (effect->purgeHandles()) {
2617 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002618 }
2619 AudioSystem::unregisterEffect(effect->id());
2620 }
2621 }
2622 }
2623 return;
2624}
2625
Glenn Kasteneeecb982016-02-26 10:44:04 -08002626// checkThread_l() must be called with AudioFlinger::mLock held
2627AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2628{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002629 ThreadBase *thread = checkMmapThread_l(ioHandle);
2630 if (thread == 0) {
2631 switch (audio_unique_id_get_use(ioHandle)) {
2632 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2633 thread = checkPlaybackThread_l(ioHandle);
2634 break;
2635 case AUDIO_UNIQUE_ID_USE_INPUT:
2636 thread = checkRecordThread_l(ioHandle);
2637 break;
2638 default:
2639 break;
2640 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002641 }
2642 return thread;
2643}
2644
Mathias Agopian65ab4712010-07-14 17:59:35 -07002645// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002646AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002647{
Glenn Kastena1117922012-01-26 10:53:32 -08002648 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002649}
2650
2651// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002652AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002653{
2654 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002655 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002656}
2657
2658// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002659AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002660{
Glenn Kastena1117922012-01-26 10:53:32 -08002661 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002662}
2663
Eric Laurent6acd1d42017-01-04 14:23:29 -08002664// checkMmapThread_l() must be called with AudioFlinger::mLock held
2665AudioFlinger::MmapThread *AudioFlinger::checkMmapThread_l(audio_io_handle_t io) const
2666{
2667 return mMmapThreads.valueFor(io).get();
2668}
2669
2670
2671// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
2672AudioFlinger::VolumeInterface *AudioFlinger::getVolumeInterface_l(audio_io_handle_t output) const
2673{
2674 VolumeInterface *volumeInterface = (VolumeInterface *)mPlaybackThreads.valueFor(output).get();
2675 if (volumeInterface == nullptr) {
2676 MmapThread *mmapThread = mMmapThreads.valueFor(output).get();
2677 if (mmapThread != nullptr) {
2678 if (mmapThread->isOutput()) {
2679 volumeInterface = (VolumeInterface *)mmapThread;
2680 }
2681 }
2682 }
2683 return volumeInterface;
2684}
2685
2686Vector <AudioFlinger::VolumeInterface *> AudioFlinger::getAllVolumeInterfaces_l() const
2687{
2688 Vector <VolumeInterface *> volumeInterfaces;
2689 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2690 volumeInterfaces.add((VolumeInterface *)mPlaybackThreads.valueAt(i).get());
2691 }
2692 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2693 if (mMmapThreads.valueAt(i)->isOutput()) {
2694 volumeInterfaces.add((VolumeInterface *)mMmapThreads.valueAt(i).get());
2695 }
2696 }
2697 return volumeInterfaces;
2698}
2699
Glenn Kasteneeecb982016-02-26 10:44:04 -08002700audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002701{
Glenn Kasten9d003132016-04-06 14:38:09 -07002702 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002703 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002704 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2705 for (int retry = 0; retry < maxRetries; retry++) {
2706 // The cast allows wraparound from max positive to min negative instead of abort
2707 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2708 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2709 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2710 // allow wrap by skipping 0 and -1 for session ids
2711 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2712 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2713 return (audio_unique_id_t) (base | use);
2714 }
2715 }
2716 // We have no way of recovering from wraparound
2717 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2718 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002719}
2720
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002721AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002722{
2723 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2724 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002725 if(thread->isDuplicating()) {
2726 continue;
2727 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002728 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002729 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002730 return thread;
2731 }
2732 }
2733 return NULL;
2734}
2735
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002736audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002737{
2738 PlaybackThread *thread = primaryPlaybackThread_l();
2739
2740 if (thread == NULL) {
2741 return 0;
2742 }
2743
Eric Laurentf1c04f92012-08-28 14:26:53 -07002744 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002745}
2746
Glenn Kastena7335632016-06-09 17:09:53 -07002747AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
2748{
2749 size_t minFrameCount = 0;
2750 PlaybackThread *minThread = NULL;
2751 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2752 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2753 if (!thread->isDuplicating()) {
2754 size_t frameCount = thread->frameCountHAL();
2755 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
2756 (frameCount == minFrameCount && thread->hasFastMixer() &&
2757 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
2758 minFrameCount = frameCount;
2759 minThread = thread;
2760 }
2761 }
2762 }
2763 return minThread;
2764}
2765
Eric Laurenta011e352012-03-29 15:51:43 -07002766sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002767 audio_session_t triggerSession,
2768 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002769 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002770 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002771{
2772 Mutex::Autolock _l(mLock);
2773
2774 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2775 status_t playStatus = NAME_NOT_FOUND;
2776 status_t recStatus = NAME_NOT_FOUND;
2777 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2778 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2779 if (playStatus == NO_ERROR) {
2780 return event;
2781 }
2782 }
2783 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2784 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2785 if (recStatus == NO_ERROR) {
2786 return event;
2787 }
2788 }
2789 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2790 mPendingSyncEvents.add(event);
2791 } else {
2792 ALOGV("createSyncEvent() invalid event %d", event->type());
2793 event.clear();
2794 }
2795 return event;
2796}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002797
Mathias Agopian65ab4712010-07-14 17:59:35 -07002798// ----------------------------------------------------------------------------
2799// Effect management
2800// ----------------------------------------------------------------------------
2801
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002802sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
2803 return mEffectsFactoryHal;
2804}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002805
Glenn Kastenf587ba52012-01-26 16:25:10 -08002806status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002807{
2808 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002809 if (mEffectsFactoryHal.get()) {
2810 return mEffectsFactoryHal->queryNumberEffects(numEffects);
2811 } else {
2812 return -ENODEV;
2813 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002814}
2815
Glenn Kastenf587ba52012-01-26 16:25:10 -08002816status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002817{
2818 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002819 if (mEffectsFactoryHal.get()) {
2820 return mEffectsFactoryHal->getDescriptor(index, descriptor);
2821 } else {
2822 return -ENODEV;
2823 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002824}
2825
Glenn Kasten5e92a782012-01-30 07:40:52 -08002826status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002827 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002828{
2829 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002830 if (mEffectsFactoryHal.get()) {
2831 return mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
2832 } else {
2833 return -ENODEV;
2834 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002835}
2836
2837
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002838sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002839 effect_descriptor_t *pDesc,
2840 const sp<IEffectClient>& effectClient,
2841 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002842 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002843 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002844 const String16& opPackageName,
Eric Laurentb6436272016-12-07 19:24:50 -08002845 pid_t pid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002846 status_t *status,
2847 int *id,
2848 int *enabled)
2849{
2850 status_t lStatus = NO_ERROR;
2851 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002852 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002853
Eric Laurentb6436272016-12-07 19:24:50 -08002854 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
2855 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
2856 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
2857 ALOGW_IF(pid != -1 && pid != callingPid,
2858 "%s uid %d pid %d tried to pass itself off as pid %d",
2859 __func__, callingUid, callingPid, pid);
2860 pid = callingPid;
2861 }
2862
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002863 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
2864 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002865
2866 if (pDesc == NULL) {
2867 lStatus = BAD_VALUE;
2868 goto Exit;
2869 }
2870
Eric Laurent84e9a102010-09-23 16:10:16 -07002871 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002872 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002873 lStatus = PERMISSION_DENIED;
2874 goto Exit;
2875 }
2876
Dima Zavinfce7a472011-04-19 22:30:36 -07002877 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002878 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002879 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002880 lStatus = PERMISSION_DENIED;
2881 goto Exit;
2882 }
2883
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002884 if (mEffectsFactoryHal == 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002885 lStatus = NO_INIT;
2886 goto Exit;
2887 }
2888
Mathias Agopian65ab4712010-07-14 17:59:35 -07002889 {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002890 if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002891 // if uuid is specified, request effect descriptor
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002892 lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002893 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002894 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002895 goto Exit;
2896 }
2897 } else {
2898 // if uuid is not specified, look for an available implementation
2899 // of the required type in effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002900 if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002901 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002902 lStatus = BAD_VALUE;
2903 goto Exit;
2904 }
2905 uint32_t numEffects = 0;
2906 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002907 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002908 bool found = false;
2909
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002910 lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002911 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002912 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002913 goto Exit;
2914 }
2915 for (uint32_t i = 0; i < numEffects; i++) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002916 lStatus = mEffectsFactoryHal->getDescriptor(i, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002917 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002918 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002919 continue;
2920 }
2921 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2922 // If matching type found save effect descriptor. If the session is
2923 // 0 and the effect is not auxiliary, continue enumeration in case
2924 // an auxiliary version of this effect type is available
2925 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002926 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002927 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002928 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2929 break;
2930 }
2931 }
2932 }
2933 if (!found) {
2934 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002935 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002936 goto Exit;
2937 }
2938 // For same effect type, chose auxiliary version over insert version if
2939 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002940 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002941 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002942 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002943 }
2944 }
2945
2946 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002947 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002948 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2949 lStatus = INVALID_OPERATION;
2950 goto Exit;
2951 }
2952
Eric Laurent59255e42011-07-27 19:49:51 -07002953 // check recording permission for visualizer
2954 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002955 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002956 lStatus = PERMISSION_DENIED;
2957 goto Exit;
2958 }
2959
Mathias Agopian65ab4712010-07-14 17:59:35 -07002960 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002961 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002962 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002963 // if the output returned by getOutputForEffect() is removed before we lock the
2964 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2965 // and we will exit safely
2966 io = AudioSystem::getOutputForEffect(&desc);
2967 ALOGV("createEffect got output %d", io);
2968 }
2969
2970 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002971
2972 // If output is not specified try to find a matching audio session ID in one of the
2973 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002974 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2975 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002976 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002977 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002978 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2979 // output must be specified by AudioPolicyManager when using session
2980 // AUDIO_SESSION_OUTPUT_STAGE
2981 lStatus = BAD_VALUE;
2982 goto Exit;
2983 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002984 // look for the thread where the specified audio session is present
2985 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2986 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2987 io = mPlaybackThreads.keyAt(i);
2988 break;
2989 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002990 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002991 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002992 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2993 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2994 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002995 break;
2996 }
2997 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002998 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002999 if (io == AUDIO_IO_HANDLE_NONE) {
3000 for (size_t i = 0; i < mMmapThreads.size(); i++) {
3001 if (mMmapThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
3002 io = mMmapThreads.keyAt(i);
3003 break;
3004 }
3005 }
3006 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003007 // If no output thread contains the requested session ID, default to
3008 // first output. The effect chain will be moved to the correct output
3009 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07003010 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003011 io = mPlaybackThreads.keyAt(0);
3012 }
Steve Block3856b092011-10-20 11:56:00 +01003013 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003014 }
3015 ThreadBase *thread = checkRecordThread_l(io);
3016 if (thread == NULL) {
3017 thread = checkPlaybackThread_l(io);
3018 if (thread == NULL) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08003019 thread = checkMmapThread_l(io);
3020 if (thread == NULL) {
3021 ALOGE("createEffect() unknown output thread");
3022 lStatus = BAD_VALUE;
3023 goto Exit;
3024 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003025 }
Eric Laurentaaa44472014-09-12 17:41:50 -07003026 } else {
3027 // Check if one effect chain was awaiting for an effect to be created on this
3028 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08003029 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07003030 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07003031 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07003032 thread->addEffectChain_l(chain);
3033 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003034 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003035
Eric Laurent021cf962014-05-13 10:18:14 -07003036 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003037
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003038 // create effect on selected output thread
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003039 bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003040 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003041 &desc, enabled, &lStatus, pinned);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003042 if (handle != 0 && id != NULL) {
3043 *id = handle->id();
3044 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003045 if (handle == 0) {
3046 // remove local strong reference to Client with mClientLock held
3047 Mutex::Autolock _cl(mClientLock);
3048 client.clear();
3049 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003050 }
3051
3052Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07003053 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003054 return handle;
3055}
3056
Glenn Kastend848eb42016-03-08 13:42:11 -08003057status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003058 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07003059{
Steve Block3856b092011-10-20 11:56:00 +01003060 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07003061 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003062 Mutex::Autolock _l(mLock);
3063 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003064 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003065 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003066 }
Eric Laurentde070132010-07-13 04:45:46 -07003067 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
3068 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003069 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003070 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003071 }
Eric Laurentde070132010-07-13 04:45:46 -07003072 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
3073 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003074 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003075 return BAD_VALUE;
3076 }
3077
3078 Mutex::Autolock _dl(dstThread->mLock);
3079 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003080 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003081}
3082
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003083// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08003084status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07003085 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07003086 AudioFlinger::PlaybackThread *dstThread,
3087 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07003088{
Steve Block3856b092011-10-20 11:56:00 +01003089 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003090 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07003091
Eric Laurent59255e42011-07-27 19:49:51 -07003092 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003093 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003094 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003095 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07003096 return INVALID_OPERATION;
3097 }
3098
Eric Laurent4c415062016-06-17 16:14:16 -07003099 // Check whether the destination thread and all effects in the chain are compatible
3100 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07003101 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07003102 " destination thread %p is not compatible with effects in the chain",
3103 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07003104 return INVALID_OPERATION;
3105 }
3106
Eric Laurent39e94f82010-07-28 01:32:47 -07003107 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07003108 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07003109 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07003110 // removed.
3111 srcThread->removeEffectChain_l(chain);
3112
3113 // transfer all effects one by one so that new effect chain is created on new thread with
3114 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07003115 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003116 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07003117 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003118 Vector< sp<EffectModule> > removed;
3119 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07003120 while (effect != 0) {
3121 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003122 removed.add(effect);
3123 status = dstThread->addEffect_l(effect);
3124 if (status != NO_ERROR) {
3125 break;
3126 }
Eric Laurentec35a142011-10-05 17:42:25 -07003127 // removeEffect_l() has stopped the effect if it was active so it must be restarted
3128 if (effect->state() == EffectModule::ACTIVE ||
3129 effect->state() == EffectModule::STOPPING) {
3130 effect->start();
3131 }
Eric Laurent39e94f82010-07-28 01:32:47 -07003132 // if the move request is not received from audio policy manager, the effect must be
3133 // re-registered with the new strategy and output
3134 if (dstChain == 0) {
3135 dstChain = effect->chain().promote();
3136 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003137 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003138 status = NO_INIT;
3139 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07003140 }
3141 strategy = dstChain->strategy();
3142 }
3143 if (reRegister) {
3144 AudioSystem::unregisterEffect(effect->id());
3145 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07003146 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07003147 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07003148 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07003149 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003150 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07003151 }
Eric Laurentde070132010-07-13 04:45:46 -07003152 effect = chain->getEffectFromId_l(0);
3153 }
3154
Eric Laurent5baf2af2013-09-12 17:37:00 -07003155 if (status != NO_ERROR) {
3156 for (size_t i = 0; i < removed.size(); i++) {
3157 srcThread->addEffect_l(removed[i]);
3158 if (dstChain != 0 && reRegister) {
3159 AudioSystem::unregisterEffect(removed[i]->id());
3160 AudioSystem::registerEffect(&removed[i]->desc(),
3161 srcThread->id(),
3162 strategy,
3163 sessionId,
3164 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003165 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003166 }
3167 }
3168 }
3169
3170 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003171}
3172
Eric Laurent5baf2af2013-09-12 17:37:00 -07003173bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07003174{
3175 if (mGlobalEffectEnableTime != 0 &&
3176 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
3177 return true;
3178 }
3179
3180 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3181 sp<EffectChain> ec =
3182 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003183 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07003184 return true;
3185 }
3186 }
3187 return false;
3188}
3189
Eric Laurent5baf2af2013-09-12 17:37:00 -07003190void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07003191{
3192 Mutex::Autolock _l(mLock);
3193
3194 mGlobalEffectEnableTime = systemTime();
3195
3196 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3197 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
3198 if (t->mType == ThreadBase::OFFLOAD) {
3199 t->invalidateTracks(AUDIO_STREAM_MUSIC);
3200 }
3201 }
3202
3203}
3204
Eric Laurentaaa44472014-09-12 17:41:50 -07003205status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
3206{
Glenn Kastend848eb42016-03-08 13:42:11 -08003207 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003208 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003209 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003210 if (index >= 0) {
3211 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
3212 return ALREADY_EXISTS;
3213 }
3214 mOrphanEffectChains.add(session, chain);
3215 return NO_ERROR;
3216}
3217
3218sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
3219{
3220 sp<EffectChain> chain;
3221 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003222 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003223 if (index >= 0) {
3224 chain = mOrphanEffectChains.valueAt(index);
3225 mOrphanEffectChains.removeItemsAt(index);
3226 }
3227 return chain;
3228}
3229
3230bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
3231{
3232 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08003233 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003234 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003235 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003236 if (index >= 0) {
3237 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003238 if (chain->removeEffect_l(effect, true) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003239 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003240 mOrphanEffectChains.removeItemsAt(index);
3241 }
3242 return true;
3243 }
3244 return false;
3245}
3246
3247
Glenn Kastenda6ef132013-01-10 12:31:01 -08003248struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003249#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
3250 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003251};
3252
3253int comparEntry(const void *p1, const void *p2)
3254{
Glenn Kasten2f55e762015-03-05 16:05:08 -08003255 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003256}
3257
Glenn Kasten46909e72013-02-26 09:20:22 -08003258#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08003259void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003260{
Eric Laurent81784c32012-11-19 14:55:58 -08003261 NBAIO_Source *teeSource = source.get();
3262 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003263 // .wav rotation
3264 // There is a benign race condition if 2 threads call this simultaneously.
3265 // They would both traverse the directory, but the result would simply be
3266 // failures at unlink() which are ignored. It's also unlikely since
3267 // normally dumpsys is only done by bugreport or from the command line.
3268 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08003269 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08003270 size_t teePathLen = strlen(teePath);
3271 DIR *dir = opendir(teePath);
3272 teePath[teePathLen++] = '/';
3273 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003274#define TEE_MAX_SORT 20 // number of entries to sort
3275#define TEE_MAX_KEEP 10 // number of entries to keep
3276 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003277 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08003278 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003279 struct dirent de;
3280 struct dirent *result = NULL;
3281 int rc = readdir_r(dir, &de, &result);
3282 if (rc != 0) {
3283 ALOGW("readdir_r failed %d", rc);
3284 break;
3285 }
3286 if (result == NULL) {
3287 break;
3288 }
3289 if (result != &de) {
3290 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
3291 break;
3292 }
3293 // ignore non .wav file entries
3294 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003295 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08003296 strcmp(&de.d_name[nameLen - 4], ".wav")) {
3297 continue;
3298 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08003299 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003300 }
3301 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003302 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003303 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003304 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
3305 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003306 (void) unlink(teePath);
3307 }
3308 }
3309 } else {
3310 if (fd >= 0) {
Glenn Kastenfbd87e82016-07-18 15:45:56 -07003311 dprintf(fd, "unable to rotate tees in %.*s: %s\n", (int) teePathLen, teePath,
Glenn Kasten9a003992016-02-23 15:24:34 -08003312 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003313 }
3314 }
Eric Laurent81784c32012-11-19 14:55:58 -08003315 char teeTime[16];
3316 struct timeval tv;
3317 gettimeofday(&tv, NULL);
3318 struct tm tm;
3319 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003320 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
3321 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
3322 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3323 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003324 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003325 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003326 char wavHeader[44];
3327 memcpy(wavHeader,
3328 "RIFF\0\0\0\0WAVEfmt \20\0\0\0\1\0\2\0\104\254\0\0\0\0\0\0\4\0\20\0data\0\0\0\0",
3329 sizeof(wavHeader));
3330 NBAIO_Format format = teeSource->format();
3331 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003332 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003333 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003334 wavHeader[22] = channelCount; // number of channels
3335 wavHeader[24] = sampleRate; // sample rate
3336 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003337 wavHeader[32] = frameSize; // block alignment
3338 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003339 write(teeFd, wavHeader, sizeof(wavHeader));
3340 size_t total = 0;
3341 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003342#define TEE_SINK_READ 1024 // frames per I/O operation
3343 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003344 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003345 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003346 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003347 bool wasFirstRead = firstRead;
3348 firstRead = false;
3349 if (actual <= 0) {
3350 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3351 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003352 }
Eric Laurent81784c32012-11-19 14:55:58 -08003353 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003354 }
Eric Laurent81784c32012-11-19 14:55:58 -08003355 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003356 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003357 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003358 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003359 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003360 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003361 uint32_t temp = 44 + total * frameSize - 8;
3362 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003363 write(teeFd, &temp, sizeof(temp));
3364 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003365 temp = total * frameSize;
3366 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003367 write(teeFd, &temp, sizeof(temp));
3368 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003369 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003370 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003371 }
Eric Laurent59255e42011-07-27 19:49:51 -07003372 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003373 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003374 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003375 }
Eric Laurent59255e42011-07-27 19:49:51 -07003376 }
3377 }
3378}
Glenn Kasten46909e72013-02-26 09:20:22 -08003379#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003380
Mathias Agopian65ab4712010-07-14 17:59:35 -07003381// ----------------------------------------------------------------------------
3382
3383status_t AudioFlinger::onTransact(
3384 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3385{
3386 return BnAudioFlinger::onTransact(code, data, reply, flags);
3387}
3388
Glenn Kasten63238ef2015-03-02 15:50:29 -08003389} // namespace android