blob: d850aa9307826250012039672d976f37f9e71d5c [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
44#include <cutils/properties.h>
45
Dima Zavin64760242011-05-11 14:15:23 -070046#include <system/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080049#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
Andy Hung6770c6f2015-04-07 13:43:36 -070051#include <media/AudioResamplerPublic.h>
52
Mikhail Naganov9fe94012016-10-14 14:57:40 -070053#include <system/audio_effects/effect_visualizer.h>
54#include <system/audio_effects/effect_ns.h>
55#include <system/audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
Glenn Kasten3b21c502011-12-15 09:52:39 -080057#include <audio_utils/primitives.h>
58
Eric Laurentfeb0db62011-07-22 09:04:31 -070059#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080060
Glenn Kasten9e58b552013-01-18 15:09:48 -080061#include <media/IMediaLogService.h>
Andy Hung07b745e2016-05-23 16:21:07 -070062#include <media/MemoryLeakTrackUtil.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080063#include <media/nbaio/Pipe.h>
64#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070065#include <media/AudioParameter.h>
Wei Jia3f273d12015-11-24 09:06:49 -080066#include <mediautils/BatteryNotifier.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070067#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080068
rago3bd1c872016-09-26 12:58:14 -070069//#define BUFLOG_NDEBUG 0
70#include <BufLog.h>
71
Nicolas Rouletfe1e1442017-01-30 12:02:03 -080072#include "TypedLogger.h"
73
Mathias Agopian65ab4712010-07-14 17:59:35 -070074// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070075
John Grossman1c345192012-03-27 14:00:17 -070076// Note: the following macro is used for extremely verbose logging message. In
77// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
78// 0; but one side effect of this is to turn all LOGV's as well. Some messages
79// are so verbose that we want to suppress them even when we have ALOG_ASSERT
80// turned on. Do not uncomment the #def below unless you really know what you
81// are doing and want to see all of the extremely verbose messages.
82//#define VERY_VERY_VERBOSE_LOGGING
83#ifdef VERY_VERY_VERBOSE_LOGGING
84#define ALOGVV ALOGV
85#else
86#define ALOGVV(a...) do { } while(0)
87#endif
Eric Laurentde070132010-07-13 04:45:46 -070088
Mathias Agopian65ab4712010-07-14 17:59:35 -070089namespace android {
90
Glenn Kastenec1d6b52011-12-12 09:04:45 -080091static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
92static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070093static const char kClientLockedString[] = "Client lock is taken\n";
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070094static const char kNoEffectsFactory[] = "Effects Factory is absent\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070095
Glenn Kasten58912562012-04-03 10:45:00 -070096
John Grossman4ff14ba2012-02-08 16:37:41 -080097nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080098
Eric Laurent81784c32012-11-19 14:55:58 -080099uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -0700100
Eric Laurentfc235202016-12-20 18:48:17 -0800101
Glenn Kasten46909e72013-02-26 09:20:22 -0800102#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800103bool AudioFlinger::mTeeSinkInputEnabled = false;
104bool AudioFlinger::mTeeSinkOutputEnabled = false;
105bool AudioFlinger::mTeeSinkTrackEnabled = false;
106
107size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
108size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
109size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800110#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800111
Eric Laurent813e2a72013-08-31 12:59:48 -0700112// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
113// we define a minimum time during which a global effect is considered enabled.
114static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
115
Eric Laurentfc235202016-12-20 18:48:17 -0800116Mutex gLock;
117wp<AudioFlinger> gAudioFlinger;
118
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700119// Keep a strong reference to media.log service around forever.
120// The service is within our parent process so it can never die in a way that we could observe.
121// These two variables are const after initialization.
122static sp<IBinder> sMediaLogServiceAsBinder;
123static sp<IMediaLogService> sMediaLogService;
124
125static pthread_once_t sMediaLogOnce = PTHREAD_ONCE_INIT;
126
127static void sMediaLogInit()
128{
129 sMediaLogServiceAsBinder = defaultServiceManager()->getService(String16("media.log"));
130 if (sMediaLogServiceAsBinder != 0) {
131 sMediaLogService = interface_cast<IMediaLogService>(sMediaLogServiceAsBinder);
132 }
133}
134
Mathias Agopian65ab4712010-07-14 17:59:35 -0700135// ----------------------------------------------------------------------------
136
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700137std::string formatToString(audio_format_t format) {
138 std::string result;
139 FormatConverter::toString(format, result);
140 return result;
Marco Nelissenb2208842014-02-07 14:00:50 -0800141}
142
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143// ----------------------------------------------------------------------------
144
145AudioFlinger::AudioFlinger()
146 : BnAudioFlinger(),
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -0800147 mMediaLogNotifier(new AudioFlinger::MediaLogNotifier()),
John Grossman4ff14ba2012-02-08 16:37:41 -0800148 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800149 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700150 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800151 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800152 mMasterMute(false),
Glenn Kastend2e67e12016-04-11 08:26:37 -0700153 // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),
John Grossman4ff14ba2012-02-08 16:37:41 -0800154 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700155 mBtNrecIsOff(false),
156 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700157 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700158 mGlobalEffectEnableTime(0),
Eric Laurent72e3f392015-05-20 14:43:50 -0700159 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160{
Glenn Kastend2e67e12016-04-11 08:26:37 -0700161 // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum
162 for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) {
163 // zero ID has a special meaning, so unavailable
164 mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX;
165 }
166
Glenn Kasten949a9262013-04-16 12:35:20 -0700167 getpid_cached = getpid();
Glenn Kastenae0cff12016-02-24 13:57:49 -0800168 const bool doLog = property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800169 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800170 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
171 MemoryHeapBase::READ_ONLY);
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700172 (void) pthread_once(&sMediaLogOnce, sMediaLogInit);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800173 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700174
Wei Jia3f273d12015-11-24 09:06:49 -0800175 // reset battery stats.
176 // if the audio service has crashed, battery stats could be left
177 // in bad state, reset the state upon service start.
178 BatteryNotifier::getInstance().noteResetAudio();
179
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700180 mDevicesFactoryHal = DevicesFactoryHalInterface::create();
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700181 mEffectsFactoryHal = EffectsFactoryHalInterface::create();
182
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -0800183 mMediaLogNotifier->run("MediaLogNotifier");
184
Glenn Kasten46909e72013-02-26 09:20:22 -0800185#ifdef TEE_SINK
Glenn Kasten9a003992016-02-23 15:24:34 -0800186 char value[PROPERTY_VALUE_MAX];
Glenn Kastenda6ef132013-01-10 12:31:01 -0800187 (void) property_get("ro.debuggable", value, "0");
188 int debuggable = atoi(value);
189 int teeEnabled = 0;
190 if (debuggable) {
191 (void) property_get("af.tee", value, "0");
192 teeEnabled = atoi(value);
193 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800194 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700195 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800196 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700197 }
198 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800199 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700200 }
201 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800202 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700203 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800204#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700205}
206
207void AudioFlinger::onFirstRef()
208{
Eric Laurent93575202011-01-18 18:39:02 -0800209 Mutex::Autolock _l(mLock);
210
Dima Zavin799a70e2011-04-18 16:57:27 -0700211 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800212 char val_str[PROPERTY_VALUE_MAX] = { 0 };
213 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
214 uint32_t int_val;
215 if (1 == sscanf(val_str, "%u", &int_val)) {
216 mStandbyTimeInNsecs = milliseconds(int_val);
217 ALOGI("Using %u mSec as standby time.", int_val);
218 } else {
219 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
220 ALOGI("Using default %u mSec as standby time.",
221 (uint32_t)(mStandbyTimeInNsecs / 1000000));
222 }
223 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700224
Eric Laurent1c333e22014-05-20 10:48:17 -0700225 mPatchPanel = new PatchPanel(this);
Andy Hungdeb03352016-08-29 14:40:14 -0700226
Eric Laurenta4c5a552012-03-29 10:12:40 -0700227 mMode = AUDIO_MODE_NORMAL;
Eric Laurentfc235202016-12-20 18:48:17 -0800228
229 gAudioFlinger = this;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230}
231
232AudioFlinger::~AudioFlinger()
233{
234 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700235 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700236 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700237 }
238 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700239 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700240 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700241 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700242
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800243 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
244 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700245 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700247
248 // Tell media.log service about any old writers that still need to be unregistered
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700249 if (sMediaLogService != 0) {
250 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
251 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
252 mUnregisteredWriters.pop();
253 sMediaLogService->unregisterWriter(iMemory);
Glenn Kasten481fb672013-09-30 14:39:28 -0700254 }
255 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256}
257
Eric Laurentfc235202016-12-20 18:48:17 -0800258//static
Eric Laurent6acd1d42017-01-04 14:23:29 -0800259__attribute__ ((visibility ("default")))
Eric Laurentfc235202016-12-20 18:48:17 -0800260status_t MmapStreamInterface::openMmapStream(MmapStreamInterface::stream_direction_t direction,
261 const audio_attributes_t *attr,
262 audio_config_base_t *config,
263 const MmapStreamInterface::Client& client,
264 audio_port_handle_t *deviceId,
265 const sp<MmapStreamCallback>& callback,
266 sp<MmapStreamInterface>& interface)
267{
268 sp<AudioFlinger> af;
269 {
270 Mutex::Autolock _l(gLock);
271 af = gAudioFlinger.promote();
272 }
273 status_t ret = NO_INIT;
274 if (af != 0) {
275 ret = af->openMmapStream(
276 direction, attr, config, client, deviceId, callback, interface);
277 }
278 return ret;
279}
280
Eric Laurent6acd1d42017-01-04 14:23:29 -0800281status_t AudioFlinger::openMmapStream(MmapStreamInterface::stream_direction_t direction,
282 const audio_attributes_t *attr,
283 audio_config_base_t *config,
284 const MmapStreamInterface::Client& client,
285 audio_port_handle_t *deviceId,
286 const sp<MmapStreamCallback>& callback,
287 sp<MmapStreamInterface>& interface)
Eric Laurentfc235202016-12-20 18:48:17 -0800288{
289 status_t ret = initCheck();
290 if (ret != NO_ERROR) {
291 return ret;
292 }
293
Eric Laurent6acd1d42017-01-04 14:23:29 -0800294 audio_session_t sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
295 audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT;
296 audio_io_handle_t io;
297 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
298 if (direction == MmapStreamInterface::DIRECTION_OUTPUT) {
299 audio_config_t fullConfig = AUDIO_CONFIG_INITIALIZER;
300 fullConfig.sample_rate = config->sample_rate;
301 fullConfig.channel_mask = config->channel_mask;
302 fullConfig.format = config->format;
303 ret = AudioSystem::getOutputForAttr(attr, &io,
304 sessionId,
305 &streamType, client.clientUid,
306 &fullConfig,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800307 (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ |
308 AUDIO_OUTPUT_FLAG_DIRECT),
Eric Laurent9ae8c592017-06-22 17:17:09 -0700309 deviceId, &portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800310 } else {
311 ret = AudioSystem::getInputForAttr(attr, &io,
312 sessionId,
313 client.clientPid,
314 client.clientUid,
315 config,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700316 AUDIO_INPUT_FLAG_MMAP_NOIRQ, deviceId, &portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800317 }
318 if (ret != NO_ERROR) {
319 return ret;
320 }
321
322 // at this stage, a MmapThread was created when openOutput() or openInput() was called by
323 // audio policy manager and we can retrieve it
324 sp<MmapThread> thread = mMmapThreads.valueFor(io);
325 if (thread != 0) {
326 interface = new MmapThreadHandle(thread);
327 thread->configure(attr, streamType, sessionId, callback, portId);
328 } else {
329 ret = NO_INIT;
330 }
331
332 ALOGV("%s done status %d portId %d", __FUNCTION__, ret, portId);
333
Eric Laurentfc235202016-12-20 18:48:17 -0800334 return ret;
335}
336
Eric Laurenta4c5a552012-03-29 10:12:40 -0700337static const char * const audio_interfaces[] = {
338 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
339 AUDIO_HARDWARE_MODULE_ID_A2DP,
340 AUDIO_HARDWARE_MODULE_ID_USB,
341};
Eric Laurenta4c5a552012-03-29 10:12:40 -0700342
Phil Burk062e67a2015-02-11 13:40:50 -0800343AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700344 audio_module_handle_t module,
345 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700346{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700347 // if module is 0, the request comes from an old policy manager and we should load
348 // well known modules
349 if (module == 0) {
350 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
Mikhail Naganovbf493082017-04-17 17:37:12 -0700351 for (size_t i = 0; i < arraysize(audio_interfaces); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700352 loadHwModule_l(audio_interfaces[i]);
353 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700354 // then try to find a module supporting the requested device.
355 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
356 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700357 sp<DeviceHalInterface> dev = audioHwDevice->hwDevice();
358 uint32_t supportedDevices;
359 if (dev->getSupportedDevices(&supportedDevices) == OK &&
360 (supportedDevices & devices) == devices) {
Eric Laurentf1c04f92012-08-28 14:26:53 -0700361 return audioHwDevice;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700362 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700363 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700364 } else {
365 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700366 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
367 if (audioHwDevice != NULL) {
368 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700369 }
370 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700371
Dima Zavin799a70e2011-04-18 16:57:27 -0700372 return NULL;
373}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374
Glenn Kasten0f11b512014-01-31 16:18:54 -0800375void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376{
377 const size_t SIZE = 256;
378 char buffer[SIZE];
379 String8 result;
380
381 result.append("Clients:\n");
382 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800383 sp<Client> client = mClients.valueAt(i).promote();
384 if (client != 0) {
385 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
386 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387 }
388 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700389
Eric Laurentd1b28d42013-09-18 18:47:13 -0700390 result.append("Notification Clients:\n");
391 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
392 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
393 result.append(buffer);
394 }
395
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700396 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800397 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700398 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
399 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800400 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700401 result.append(buffer);
402 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404}
405
406
Glenn Kasten0f11b512014-01-31 16:18:54 -0800407void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408{
409 const size_t SIZE = 256;
410 char buffer[SIZE];
411 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800412 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413
John Grossman4ff14ba2012-02-08 16:37:41 -0800414 snprintf(buffer, SIZE, "Hardware status: %d\n"
415 "Standby Time mSec: %u\n",
416 hardwareStatus,
417 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418 result.append(buffer);
419 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420}
421
Glenn Kasten0f11b512014-01-31 16:18:54 -0800422void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700423{
424 const size_t SIZE = 256;
425 char buffer[SIZE];
426 String8 result;
427 snprintf(buffer, SIZE, "Permission Denial: "
428 "can't dump AudioFlinger from pid=%d, uid=%d\n",
429 IPCThreadState::self()->getCallingPid(),
430 IPCThreadState::self()->getCallingUid());
431 result.append(buffer);
432 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700433}
434
Eric Laurent81784c32012-11-19 14:55:58 -0800435bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436{
437 bool locked = false;
438 for (int i = 0; i < kDumpLockRetries; ++i) {
439 if (mutex.tryLock() == NO_ERROR) {
440 locked = true;
441 break;
442 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800443 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700444 }
445 return locked;
446}
447
448status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
449{
Glenn Kasten44deb052012-02-05 18:09:08 -0800450 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700451 dumpPermissionDenial(fd, args);
452 } else {
453 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800454 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700455 if (!hardwareLocked) {
456 String8 result(kHardwareLockedString);
457 write(fd, result.string(), result.size());
458 } else {
459 mHardwareLock.unlock();
460 }
461
Eric Laurent81784c32012-11-19 14:55:58 -0800462 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463
464 // failed to lock - AudioFlinger is probably deadlocked
465 if (!locked) {
466 String8 result(kDeadlockedString);
467 write(fd, result.string(), result.size());
468 }
469
Eric Laurent021cf962014-05-13 10:18:14 -0700470 bool clientLocked = dumpTryLock(mClientLock);
471 if (!clientLocked) {
472 String8 result(kClientLockedString);
473 write(fd, result.string(), result.size());
474 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700475
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700476 if (mEffectsFactoryHal != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700477 mEffectsFactoryHal->dumpEffects(fd);
478 } else {
479 String8 result(kNoEffectsFactory);
480 write(fd, result.string(), result.size());
481 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700482
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700484 if (clientLocked) {
485 mClientLock.unlock();
486 }
487
Mathias Agopian65ab4712010-07-14 17:59:35 -0700488 dumpInternals(fd, args);
489
490 // dump playback threads
491 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
492 mPlaybackThreads.valueAt(i)->dump(fd, args);
493 }
494
495 // dump record threads
496 for (size_t i = 0; i < mRecordThreads.size(); i++) {
497 mRecordThreads.valueAt(i)->dump(fd, args);
498 }
499
Eric Laurent6acd1d42017-01-04 14:23:29 -0800500 // dump mmap threads
501 for (size_t i = 0; i < mMmapThreads.size(); i++) {
502 mMmapThreads.valueAt(i)->dump(fd, args);
503 }
504
Eric Laurentaaa44472014-09-12 17:41:50 -0700505 // dump orphan effect chains
506 if (mOrphanEffectChains.size() != 0) {
507 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
508 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
509 mOrphanEffectChains.valueAt(i)->dump(fd, args);
510 }
511 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700512 // dump all hardware devs
513 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700514 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
515 dev->dump(fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700516 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700517
Glenn Kasten46909e72013-02-26 09:20:22 -0800518#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700519 // dump the serially shared record tee sink
520 if (mRecordTeeSource != 0) {
Glenn Kasten5b2191a2016-08-19 11:44:47 -0700521 dumpTee(fd, mRecordTeeSource, AUDIO_IO_HANDLE_NONE, 'C');
Glenn Kastend06785b2012-09-30 12:29:28 -0700522 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800523#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700524
rago3bd1c872016-09-26 12:58:14 -0700525 BUFLOG_RESET;
526
Glenn Kastend65d73c2012-06-22 17:21:07 -0700527 if (locked) {
528 mLock.unlock();
529 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800530
531 // append a copy of media.log here by forwarding fd to it, but don't attempt
532 // to lookup the service if it's not running, as it will block for a second
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700533 if (sMediaLogServiceAsBinder != 0) {
534 dprintf(fd, "\nmedia.log:\n");
535 Vector<String16> args;
536 sMediaLogServiceAsBinder->dump(fd, args);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800537 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700538
539 // check for optional arguments
Andy Hung07b745e2016-05-23 16:21:07 -0700540 bool dumpMem = false;
Andy Hung35fec5f2016-04-13 14:21:48 -0700541 bool unreachableMemory = false;
542 for (const auto &arg : args) {
Andy Hung07b745e2016-05-23 16:21:07 -0700543 if (arg == String16("-m")) {
544 dumpMem = true;
545 } else if (arg == String16("--unreachable")) {
Andy Hung35fec5f2016-04-13 14:21:48 -0700546 unreachableMemory = true;
547 }
548 }
549
Andy Hung07b745e2016-05-23 16:21:07 -0700550 if (dumpMem) {
551 dprintf(fd, "\nDumping memory:\n");
552 std::string s = dumpMemoryAddresses(100 /* limit */);
553 write(fd, s.c_str(), s.size());
554 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700555 if (unreachableMemory) {
556 dprintf(fd, "\nDumping unreachable memory:\n");
557 // TODO - should limit be an argument parameter?
Andy Hung07b745e2016-05-23 16:21:07 -0700558 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
Andy Hung35fec5f2016-04-13 14:21:48 -0700559 write(fd, s.c_str(), s.size());
560 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561 }
562 return NO_ERROR;
563}
564
Eric Laurent021cf962014-05-13 10:18:14 -0700565sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800566{
Eric Laurent021cf962014-05-13 10:18:14 -0700567 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800568 // If pid is already in the mClients wp<> map, then use that entry
569 // (for which promote() is always != 0), otherwise create a new entry and Client.
570 sp<Client> client = mClients.valueFor(pid).promote();
571 if (client == 0) {
572 client = new Client(this, pid);
573 mClients.add(pid, client);
574 }
575
576 return client;
577}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578
Glenn Kasten9e58b552013-01-18 15:09:48 -0800579sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
580{
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700581 // If there is no memory allocated for logs, return a dummy writer that does nothing.
582 // Similarly if we can't contact the media.log service, also return a dummy writer.
583 if (mLogMemoryDealer == 0 || sMediaLogService == 0) {
Glenn Kasten9e58b552013-01-18 15:09:48 -0800584 return new NBLog::Writer();
585 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700586 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
587 // If allocation fails, consult the vector of previously unregistered writers
588 // and garbage-collect one or more them until an allocation succeeds
589 if (shared == 0) {
590 Mutex::Autolock _l(mUnregisteredWritersLock);
591 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
592 {
593 // Pick the oldest stale writer to garbage-collect
594 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
595 mUnregisteredWriters.removeAt(0);
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700596 sMediaLogService->unregisterWriter(iMemory);
Glenn Kasten481fb672013-09-30 14:39:28 -0700597 // Now the media.log remote reference to IMemory is gone. When our last local
598 // reference to IMemory also drops to zero at end of this block,
599 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
600 }
601 // Re-attempt the allocation
602 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
603 if (shared != 0) {
604 goto success;
605 }
606 }
607 // Even after garbage-collecting all old writers, there is still not enough memory,
608 // so return a dummy writer
609 return new NBLog::Writer();
610 }
611success:
Glenn Kasten535e1612016-12-05 12:19:36 -0800612 NBLog::Shared *sharedRawPtr = (NBLog::Shared *) shared->pointer();
613 new((void *) sharedRawPtr) NBLog::Shared(); // placement new here, but the corresponding
614 // explicit destructor not needed since it is POD
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700615 sMediaLogService->registerWriter(shared, size, name);
Glenn Kasten535e1612016-12-05 12:19:36 -0800616 return new NBLog::Writer(shared, size);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800617}
618
619void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
620{
Glenn Kasten685ef092013-02-04 08:15:34 -0800621 if (writer == 0) {
622 return;
623 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800624 sp<IMemory> iMemory(writer->getIMemory());
625 if (iMemory == 0) {
626 return;
627 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700628 // Rather than removing the writer immediately, append it to a queue of old writers to
629 // be garbage-collected later. This allows us to continue to view old logs for a while.
630 Mutex::Autolock _l(mUnregisteredWritersLock);
631 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800632}
633
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634// IAudioFlinger interface
635
636
637sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800638 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800640 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700641 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800642 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -0700643 audio_output_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800645 audio_io_handle_t output,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700646 pid_t pid,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800647 pid_t tid,
Glenn Kastend848eb42016-03-08 13:42:11 -0800648 audio_session_t *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800649 int clientUid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800650 status_t *status,
651 audio_port_handle_t portId)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652{
653 sp<PlaybackThread::Track> track;
654 sp<TrackHandle> trackHandle;
655 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -0800657 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700659 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
660 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
661 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
662 ALOGW_IF(pid != -1 && pid != callingPid,
663 "%s uid %d pid %d tried to pass itself off as pid %d",
664 __func__, callingUid, callingPid, pid);
665 pid = callingPid;
666 }
667
Glenn Kasten263709e2012-01-06 08:40:01 -0800668 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
669 // but if someone uses binder directly they could bypass that and cause us to crash
670 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000671 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 lStatus = BAD_VALUE;
673 goto Exit;
674 }
675
Glenn Kasten53b5d092014-02-05 10:00:23 -0800676 // further sample rate checks are performed by createTrack_l() depending on the thread type
677 if (sampleRate == 0) {
678 ALOGE("createTrack() invalid sample rate %u", sampleRate);
679 lStatus = BAD_VALUE;
680 goto Exit;
681 }
682
683 // further channel mask checks are performed by createTrack_l() depending on the thread type
684 if (!audio_is_output_channel(channelMask)) {
685 ALOGE("createTrack() invalid channel mask %#x", channelMask);
686 lStatus = BAD_VALUE;
687 goto Exit;
688 }
689
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700690 // further format checks are performed by createTrack_l() depending on the thread type
691 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800692 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700693 lStatus = BAD_VALUE;
694 goto Exit;
695 }
696
Glenn Kasten663c2242013-09-24 11:52:37 -0700697 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
698 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
699 lStatus = BAD_VALUE;
700 goto Exit;
701 }
702
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703 {
704 Mutex::Autolock _l(mLock);
705 PlaybackThread *thread = checkPlaybackThread_l(output);
706 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800707 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 lStatus = BAD_VALUE;
709 goto Exit;
710 }
711
Eric Laurent021cf962014-05-13 10:18:14 -0700712 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713
Glenn Kastene848bd92014-03-13 15:00:32 -0700714 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700715 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800716 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
717 ALOGE("createTrack() invalid session ID %d", *sessionId);
718 lStatus = BAD_VALUE;
719 goto Exit;
720 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700721 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700722 // check if an effect chain with the same session ID is present on another
723 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700724 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700725 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
726 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700727 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent4c415062016-06-17 16:14:16 -0700728 if (sessions & ThreadBase::EFFECT_SESSION) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700729 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700730 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700731 }
Eric Laurentde070132010-07-13 04:45:46 -0700732 }
733 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700735 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800736 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700737 if (sessionId != NULL) {
738 *sessionId = lSessionId;
739 }
740 }
Steve Block3856b092011-10-20 11:56:00 +0100741 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742
743 track = thread->createTrack_l(client, streamType, sampleRate, format,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800744 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid,
745 clientUid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800746 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700747 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700748
749 // move effect chain to this output thread if an effect on same session was waiting
750 // for a track to be created
751 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700752 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700753 Mutex::Autolock _dl(thread->mLock);
754 Mutex::Autolock _sl(effectThread->mLock);
755 moveEffectChain_l(lSessionId, effectThread, thread, true);
756 }
Eric Laurenta011e352012-03-29 15:51:43 -0700757
758 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700759 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700760 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
761 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700762 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700763 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700764 } else {
765 mPendingSyncEvents[i]->cancel();
766 }
Eric Laurenta011e352012-03-29 15:51:43 -0700767 mPendingSyncEvents.removeAt(i);
768 i--;
769 }
770 }
771 }
Glenn Kastene198c362013-08-13 09:13:36 -0700772
Glenn Kastend848eb42016-03-08 13:42:11 -0800773 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700774 }
Glenn Kastene198c362013-08-13 09:13:36 -0700775
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700776 if (lStatus != NO_ERROR) {
777 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700778 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700779 // Don't hold mClientLock when releasing the reference on the track as the
780 // destructor will acquire it.
781 {
782 Mutex::Autolock _cl(mClientLock);
783 client.clear();
784 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700785 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700786 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700787 }
788
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700789 // return handle to client
790 trackHandle = new TrackHandle(track);
791
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700793 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794 return trackHandle;
795}
796
Glenn Kasten2c073da2016-02-26 09:14:08 -0800797uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798{
799 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800800 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800802 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700803 return 0;
804 }
805 return thread->sampleRate();
806}
807
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800808audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809{
810 Mutex::Autolock _l(mLock);
811 PlaybackThread *thread = checkPlaybackThread_l(output);
812 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000813 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800814 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815 }
816 return thread->format();
817}
818
Glenn Kasten2c073da2016-02-26 09:14:08 -0800819size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820{
821 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800822 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800824 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825 return 0;
826 }
Glenn Kasten58912562012-04-03 10:45:00 -0700827 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
828 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700829 return thread->frameCount();
830}
831
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700832size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
833{
834 Mutex::Autolock _l(mLock);
835 ThreadBase *thread = checkThread_l(ioHandle);
836 if (thread == NULL) {
837 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
838 return 0;
839 }
840 return thread->frameCountHAL();
841}
842
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800843uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844{
845 Mutex::Autolock _l(mLock);
846 PlaybackThread *thread = checkPlaybackThread_l(output);
847 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800848 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849 return 0;
850 }
851 return thread->latency();
852}
853
854status_t AudioFlinger::setMasterVolume(float value)
855{
Eric Laurenta1884f92011-08-23 08:25:03 -0700856 status_t ret = initCheck();
857 if (ret != NO_ERROR) {
858 return ret;
859 }
860
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 // check calling permissions
862 if (!settingsAllowed()) {
863 return PERMISSION_DENIED;
864 }
865
Eric Laurenta4c5a552012-03-29 10:12:40 -0700866 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700867 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700868
John Grossmanee578c02012-07-23 17:05:46 -0700869 // Set master volume in the HALs which support it.
870 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
871 AutoMutex lock(mHardwareLock);
872 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800873
John Grossmanee578c02012-07-23 17:05:46 -0700874 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
875 if (dev->canSetMasterVolume()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700876 dev->hwDevice()->setMasterVolume(value);
Eric Laurent93575202011-01-18 18:39:02 -0800877 }
John Grossmanee578c02012-07-23 17:05:46 -0700878 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880
John Grossmanee578c02012-07-23 17:05:46 -0700881 // Now set the master volume in each playback thread. Playback threads
882 // assigned to HALs which do not have master volume support will apply
883 // master volume during the mix operation. Threads with HALs which do
884 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700885 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
886 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
887 continue;
888 }
John Grossmanee578c02012-07-23 17:05:46 -0700889 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700890 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891
892 return NO_ERROR;
893}
894
Glenn Kastenf78aee72012-01-04 11:00:47 -0800895status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700896{
Eric Laurenta1884f92011-08-23 08:25:03 -0700897 status_t ret = initCheck();
898 if (ret != NO_ERROR) {
899 return ret;
900 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700901
902 // check calling permissions
903 if (!settingsAllowed()) {
904 return PERMISSION_DENIED;
905 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800906 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000907 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700908 return BAD_VALUE;
909 }
910
911 { // scope for the lock
912 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700913 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700915 ret = dev->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700916 mHardwareStatus = AUDIO_HW_IDLE;
917 }
918
919 if (NO_ERROR == ret) {
920 Mutex::Autolock _l(mLock);
921 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800922 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700923 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 }
925
926 return ret;
927}
928
929status_t AudioFlinger::setMicMute(bool state)
930{
Eric Laurenta1884f92011-08-23 08:25:03 -0700931 status_t ret = initCheck();
932 if (ret != NO_ERROR) {
933 return ret;
934 }
935
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936 // check calling permissions
937 if (!settingsAllowed()) {
938 return PERMISSION_DENIED;
939 }
940
941 AutoMutex lock(mHardwareLock);
942 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700943 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700944 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
945 status_t result = dev->setMicMute(state);
Eric Laurent2f035f52014-09-14 12:11:52 -0700946 if (result != NO_ERROR) {
947 ret = result;
948 }
949 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700950 mHardwareStatus = AUDIO_HW_IDLE;
951 return ret;
952}
953
954bool AudioFlinger::getMicMute() const
955{
Eric Laurenta1884f92011-08-23 08:25:03 -0700956 status_t ret = initCheck();
957 if (ret != NO_ERROR) {
958 return false;
959 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800960 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700961 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800962 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800964 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700965 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
966 status_t result = dev->getMicMute(&state);
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800967 if (result == NO_ERROR) {
968 mute = mute && state;
969 }
970 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800972
973 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700974}
975
976status_t AudioFlinger::setMasterMute(bool muted)
977{
John Grossmand8f178d2012-07-20 14:51:35 -0700978 status_t ret = initCheck();
979 if (ret != NO_ERROR) {
980 return ret;
981 }
982
Mathias Agopian65ab4712010-07-14 17:59:35 -0700983 // check calling permissions
984 if (!settingsAllowed()) {
985 return PERMISSION_DENIED;
986 }
987
John Grossmanee578c02012-07-23 17:05:46 -0700988 Mutex::Autolock _l(mLock);
989 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700990
John Grossmanee578c02012-07-23 17:05:46 -0700991 // Set master mute in the HALs which support it.
992 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
993 AutoMutex lock(mHardwareLock);
994 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700995
John Grossmanee578c02012-07-23 17:05:46 -0700996 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
997 if (dev->canSetMasterMute()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700998 dev->hwDevice()->setMasterMute(muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700999 }
John Grossmanee578c02012-07-23 17:05:46 -07001000 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -07001001 }
1002
John Grossmanee578c02012-07-23 17:05:46 -07001003 // Now set the master mute in each playback thread. Playback threads
1004 // assigned to HALs which do not have master mute support will apply master
1005 // mute during the mix operation. Threads with HALs which do support master
1006 // mute will simply ignore the setting.
Eric Laurent6acd1d42017-01-04 14:23:29 -08001007 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1008 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1009 volumeInterfaces[i]->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -07001010 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001011
1012 return NO_ERROR;
1013}
1014
1015float AudioFlinger::masterVolume() const
1016{
Glenn Kasten98067102011-12-13 11:47:54 -08001017 Mutex::Autolock _l(mLock);
1018 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001019}
1020
1021bool AudioFlinger::masterMute() const
1022{
Glenn Kasten98067102011-12-13 11:47:54 -08001023 Mutex::Autolock _l(mLock);
1024 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001025}
1026
John Grossman4ff14ba2012-02-08 16:37:41 -08001027float AudioFlinger::masterVolume_l() const
1028{
John Grossman4ff14ba2012-02-08 16:37:41 -08001029 return mMasterVolume;
1030}
1031
John Grossmand8f178d2012-07-20 14:51:35 -07001032bool AudioFlinger::masterMute_l() const
1033{
John Grossmanee578c02012-07-23 17:05:46 -07001034 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -07001035}
1036
Eric Laurent223fd5c2014-11-11 13:43:36 -08001037status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
1038{
1039 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001040 ALOGW("checkStreamType() invalid stream %d", stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001041 return BAD_VALUE;
1042 }
1043 pid_t caller = IPCThreadState::self()->getCallingPid();
1044 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001045 ALOGW("checkStreamType() pid %d cannot use internal stream type %d", caller, stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001046 return PERMISSION_DENIED;
1047 }
1048
1049 return NO_ERROR;
1050}
1051
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001052status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
1053 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054{
1055 // check calling permissions
1056 if (!settingsAllowed()) {
1057 return PERMISSION_DENIED;
1058 }
1059
Eric Laurent223fd5c2014-11-11 13:43:36 -08001060 status_t status = checkStreamType(stream);
1061 if (status != NO_ERROR) {
1062 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001064 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065
1066 AutoMutex lock(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001067 Vector<VolumeInterface *> volumeInterfaces;
Glenn Kasten142f5192014-03-25 17:44:59 -07001068 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001069 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1070 if (volumeInterface == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071 return BAD_VALUE;
1072 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001073 volumeInterfaces.add(volumeInterface);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074 }
1075
1076 mStreamTypes[stream].volume = value;
1077
Eric Laurent6acd1d42017-01-04 14:23:29 -08001078 if (volumeInterfaces.size() == 0) {
1079 volumeInterfaces = getAllVolumeInterfaces_l();
1080 }
1081 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1082 volumeInterfaces[i]->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001083 }
1084
1085 return NO_ERROR;
1086}
1087
Glenn Kastenfff6d712012-01-12 16:38:12 -08001088status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089{
1090 // check calling permissions
1091 if (!settingsAllowed()) {
1092 return PERMISSION_DENIED;
1093 }
1094
Eric Laurent223fd5c2014-11-11 13:43:36 -08001095 status_t status = checkStreamType(stream);
1096 if (status != NO_ERROR) {
1097 return status;
1098 }
1099 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1100
1101 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001102 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001103 return BAD_VALUE;
1104 }
1105
Eric Laurent93575202011-01-18 18:39:02 -08001106 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001107 mStreamTypes[stream].mute = muted;
Eric Laurent6acd1d42017-01-04 14:23:29 -08001108 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1109 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1110 volumeInterfaces[i]->setStreamMute(stream, muted);
1111 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112
1113 return NO_ERROR;
1114}
1115
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001116float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001118 status_t status = checkStreamType(stream);
1119 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001120 return 0.0f;
1121 }
1122
1123 AutoMutex lock(mLock);
1124 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001125 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001126 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1127 if (volumeInterface != NULL) {
1128 volume = volumeInterface->streamVolume(stream);
1129 } else {
1130 volume = 0.0f;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001131 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001132 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001133 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134 }
1135
1136 return volume;
1137}
1138
Glenn Kastenfff6d712012-01-12 16:38:12 -08001139bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001140{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001141 status_t status = checkStreamType(stream);
1142 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143 return true;
1144 }
1145
Glenn Kasten6637baa2012-01-09 09:40:36 -08001146 AutoMutex lock(mLock);
1147 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148}
1149
Eric Laurent054d9d32015-04-24 08:48:48 -07001150
1151void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1152{
1153 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1154 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1155 }
1156}
1157
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001158status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001159{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001160 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1161 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001162
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163 // check calling permissions
1164 if (!settingsAllowed()) {
1165 return PERMISSION_DENIED;
1166 }
1167
Glenn Kasten142f5192014-03-25 17:44:59 -07001168 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1169 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001170 Mutex::Autolock _l(mLock);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001171 // result will remain NO_INIT if no audio device is present
1172 status_t final_result = NO_INIT;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001173 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001174 AutoMutex lock(mHardwareLock);
1175 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1176 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001177 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1178 status_t result = dev->setParameters(keyValuePairs);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001179 // return success if at least one audio device accepts the parameters as not all
1180 // HALs are requested to support all parameters. If no audio device supports the
1181 // requested parameters, the last error is reported.
1182 if (final_result != NO_ERROR) {
1183 final_result = result;
1184 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001185 }
1186 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001187 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001188 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1189 AudioParameter param = AudioParameter(keyValuePairs);
1190 String8 value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001191 if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) {
1192 bool btNrecIsOff = (value == AudioParameter::valueOff);
Eric Laurentbee53372011-08-29 12:42:48 -07001193 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001194 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1195 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001196 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001197 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1198 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001199 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001200 // suspend effects associated with those session IDs
1201 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001202 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001203 thread->setEffectSuspended(FX_IID_AEC,
1204 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001205 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001206 thread->setEffectSuspended(FX_IID_NS,
1207 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001208 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001209 }
1210 }
Eric Laurentbee53372011-08-29 12:42:48 -07001211 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001212 }
1213 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001214 String8 screenState;
1215 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
Mikhail Naganov00260b52016-10-13 12:54:24 -07001216 bool isOff = (screenState == AudioParameter::valueOff);
Eric Laurent81784c32012-11-19 14:55:58 -08001217 if (isOff != (AudioFlinger::mScreenState & 1)) {
1218 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001219 }
1220 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001221 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222 }
1223
1224 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1225 // and the thread is exited once the lock is released
1226 sp<ThreadBase> thread;
1227 {
1228 Mutex::Autolock _l(mLock);
1229 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001230 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231 thread = checkRecordThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001232 if (thread == 0) {
1233 thread = checkMmapThread_l(ioHandle);
1234 }
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001235 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001236 // indicate output device change to all input threads for pre processing
1237 AudioParameter param = AudioParameter(keyValuePairs);
1238 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001239 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1240 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001241 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001242 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001243 }
1244 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001245 if (thread != 0) {
1246 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001247 }
1248 return BAD_VALUE;
1249}
1250
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001251String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001252{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001253 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1254 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255
Eric Laurenta4c5a552012-03-29 10:12:40 -07001256 Mutex::Autolock _l(mLock);
1257
Glenn Kasten142f5192014-03-25 17:44:59 -07001258 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001259 String8 out_s8;
1260
Dima Zavin799a70e2011-04-18 16:57:27 -07001261 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001262 String8 s;
1263 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001264 {
1265 AutoMutex lock(mHardwareLock);
1266 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001267 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1268 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001269 mHardwareStatus = AUDIO_HW_IDLE;
1270 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001271 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001272 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001273 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001274 }
1275
Eric Laurent6acd1d42017-01-04 14:23:29 -08001276 ThreadBase *thread = (ThreadBase *)checkPlaybackThread_l(ioHandle);
1277 if (thread == NULL) {
1278 thread = (ThreadBase *)checkRecordThread_l(ioHandle);
1279 if (thread == NULL) {
1280 thread = (ThreadBase *)checkMmapThread_l(ioHandle);
1281 if (thread == NULL) {
1282 String8("");
1283 }
1284 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001285 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001286 return thread->getParameters(keys);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001287}
1288
Glenn Kastendd8104c2012-07-02 12:42:44 -07001289size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1290 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001291{
Eric Laurenta1884f92011-08-23 08:25:03 -07001292 status_t ret = initCheck();
1293 if (ret != NO_ERROR) {
1294 return 0;
1295 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001296 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001297 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001298 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001299 return 0;
1300 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001301
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001302 AutoMutex lock(mHardwareLock);
1303 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001304 audio_config_t config, proposed;
1305 memset(&proposed, 0, sizeof(proposed));
1306 proposed.sample_rate = sampleRate;
1307 proposed.channel_mask = channelMask;
1308 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001309
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001310 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001311 size_t frames;
1312 for (;;) {
1313 // Note: config is currently a const parameter for get_input_buffer_size()
1314 // but we use a copy from proposed in case config changes from the call.
1315 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001316 status_t result = dev->getInputBufferSize(&config, &frames);
1317 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001318 break; // hal success, config is the result
1319 }
1320 // change one parameter of the configuration each iteration to a more "common" value
1321 // to see if the device will support it.
1322 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1323 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1324 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1325 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1326 } else {
1327 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1328 "format %#x, channelMask 0x%X",
1329 sampleRate, format, channelMask);
1330 break; // retries failed, break out of loop with frames == 0.
1331 }
1332 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001333 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001334 if (frames > 0 && config.sample_rate != sampleRate) {
1335 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1336 }
1337 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001338}
1339
Glenn Kasten5f972c02014-01-13 09:59:31 -08001340uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001341{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001342 Mutex::Autolock _l(mLock);
1343
1344 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1345 if (recordThread != NULL) {
1346 return recordThread->getInputFramesLost();
1347 }
1348 return 0;
1349}
1350
1351status_t AudioFlinger::setVoiceVolume(float value)
1352{
Eric Laurenta1884f92011-08-23 08:25:03 -07001353 status_t ret = initCheck();
1354 if (ret != NO_ERROR) {
1355 return ret;
1356 }
1357
Mathias Agopian65ab4712010-07-14 17:59:35 -07001358 // check calling permissions
1359 if (!settingsAllowed()) {
1360 return PERMISSION_DENIED;
1361 }
1362
1363 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001364 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001365 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001366 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001367 mHardwareStatus = AUDIO_HW_IDLE;
1368
1369 return ret;
1370}
1371
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001372status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001373 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001374{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001375 Mutex::Autolock _l(mLock);
1376
1377 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1378 if (playbackThread != NULL) {
1379 return playbackThread->getRenderPosition(halFrames, dspFrames);
1380 }
1381
1382 return BAD_VALUE;
1383}
1384
1385void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1386{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001387 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001388 if (client == 0) {
1389 return;
1390 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001391 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001392 {
1393 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001394 if (mNotificationClients.indexOfKey(pid) < 0) {
1395 sp<NotificationClient> notificationClient = new NotificationClient(this,
1396 client,
1397 pid);
1398 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399
Eric Laurent021cf962014-05-13 10:18:14 -07001400 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401
Marco Nelissen06b46062014-11-14 07:58:25 -08001402 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001403 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001404 }
1405 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406
Eric Laurent021cf962014-05-13 10:18:14 -07001407 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001408 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001409 // the config change is always sent from playback or record threads to avoid deadlock
1410 // with AudioSystem::gLock
1411 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1412 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1413 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001415 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1416 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001417 }
1418}
1419
1420void AudioFlinger::removeNotificationClient(pid_t pid)
1421{
1422 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001423 {
1424 Mutex::Autolock _cl(mClientLock);
1425 mNotificationClients.removeItem(pid);
1426 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001427
Steve Block3856b092011-10-20 11:56:00 +01001428 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001429 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001430 bool removed = false;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001431 for (size_t i = 0; i < num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001432 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001433 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001434 if (ref->mPid == pid) {
1435 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001436 mAudioSessionRefs.removeAt(i);
1437 delete ref;
1438 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001439 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001440 } else {
1441 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001442 }
1443 }
1444 if (removed) {
1445 purgeStaleEffects_l();
1446 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447}
1448
Eric Laurent73e26b62015-04-27 16:55:58 -07001449void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001450 const sp<AudioIoDescriptor>& ioDesc,
1451 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001452{
Eric Laurent021cf962014-05-13 10:18:14 -07001453 Mutex::Autolock _l(mClientLock);
1454 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001455 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001456 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1457 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1458 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001459 }
1460}
1461
Eric Laurent021cf962014-05-13 10:18:14 -07001462// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001463void AudioFlinger::removeClient_l(pid_t pid)
1464{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001465 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001466 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001467 mClients.removeItem(pid);
1468}
1469
Eric Laurent717e1282012-06-29 16:36:52 -07001470// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001471sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1472 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001473{
1474 sp<PlaybackThread> thread;
1475
1476 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1477 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1478 ALOG_ASSERT(thread == 0);
1479 thread = mPlaybackThreads.valueAt(i);
1480 }
1481 }
1482
1483 return thread;
1484}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485
Mathias Agopian65ab4712010-07-14 17:59:35 -07001486
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487
1488// ----------------------------------------------------------------------------
1489
1490AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1491 : RefBase(),
1492 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001493 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001494{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301495 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1496 heapSize *= 1024;
1497 if (!heapSize) {
1498 heapSize = kClientSharedHeapSizeBytes;
1499 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1500 // invalidated tracks
1501 if (!audioFlinger->isLowRamDevice()) {
1502 heapSize *= kClientSharedHeapSizeMultiplier;
1503 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001504 }
1505 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001506}
1507
Eric Laurent021cf962014-05-13 10:18:14 -07001508// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001509AudioFlinger::Client::~Client()
1510{
1511 mAudioFlinger->removeClient_l(mPid);
1512}
1513
Glenn Kasten435dbe62012-01-30 10:15:48 -08001514sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001515{
1516 return mMemoryDealer;
1517}
1518
1519// ----------------------------------------------------------------------------
1520
1521AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1522 const sp<IAudioFlingerClient>& client,
1523 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001524 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525{
1526}
1527
1528AudioFlinger::NotificationClient::~NotificationClient()
1529{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001530}
1531
Glenn Kasten0f11b512014-01-31 16:18:54 -08001532void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533{
1534 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001535 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001536}
1537
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001538// ----------------------------------------------------------------------------
1539AudioFlinger::MediaLogNotifier::MediaLogNotifier()
1540 : mPendingRequests(false) {}
1541
1542
1543void AudioFlinger::MediaLogNotifier::requestMerge() {
1544 AutoMutex _l(mMutex);
1545 mPendingRequests = true;
1546 mCond.signal();
1547}
1548
1549bool AudioFlinger::MediaLogNotifier::threadLoop() {
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001550 // Should already have been checked, but just in case
1551 if (sMediaLogService == 0) {
1552 return false;
1553 }
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001554 // Wait until there are pending requests
1555 {
1556 AutoMutex _l(mMutex);
1557 mPendingRequests = false; // to ignore past requests
1558 while (!mPendingRequests) {
1559 mCond.wait(mMutex);
1560 // TODO may also need an exitPending check
1561 }
1562 mPendingRequests = false;
1563 }
1564 // Execute the actual MediaLogService binder call and ignore extra requests for a while
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001565 sMediaLogService->requestMergeWakeup();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001566 usleep(kPostTriggerSleepPeriod);
1567 return true;
1568}
1569
1570void AudioFlinger::requestLogMerge() {
1571 mMediaLogNotifier->requestMerge();
1572}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001573
1574// ----------------------------------------------------------------------------
1575
1576sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001577 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001578 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001579 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001580 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001581 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001582 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -07001583 audio_input_flags_t *flags,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001584 pid_t pid,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001585 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001586 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001587 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001588 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001589 sp<IMemory>& cblk,
1590 sp<IMemory>& buffers,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001591 status_t *status,
1592 audio_port_handle_t portId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001593{
1594 sp<RecordThread::RecordTrack> recordTrack;
1595 sp<RecordHandle> recordHandle;
1596 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001597 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001598 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001599
Glenn Kastend776ac62014-05-07 09:16:09 -07001600 cblk.clear();
1601 buffers.clear();
1602
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001603 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001604 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1605 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001606 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001607 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1608 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001609 updatePid = true;
1610 }
1611
1612 if (updatePid) {
1613 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
1614 ALOGW_IF(pid != -1 && pid != callingPid,
1615 "%s uid %d pid %d tried to pass itself off as pid %d",
1616 __func__, callingUid, callingPid, pid);
1617 pid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001618 }
1619
Mathias Agopian65ab4712010-07-14 17:59:35 -07001620 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001621 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001622 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001623 lStatus = PERMISSION_DENIED;
1624 goto Exit;
1625 }
1626
Glenn Kasten53b5d092014-02-05 10:00:23 -08001627 // further sample rate checks are performed by createRecordTrack_l()
1628 if (sampleRate == 0) {
1629 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1630 lStatus = BAD_VALUE;
1631 goto Exit;
1632 }
1633
Andy Hung6770c6f2015-04-07 13:43:36 -07001634 // we don't yet support anything other than linear PCM
1635 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001636 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001637 lStatus = BAD_VALUE;
1638 goto Exit;
1639 }
1640
Glenn Kasten53b5d092014-02-05 10:00:23 -08001641 // further channel mask checks are performed by createRecordTrack_l()
1642 if (!audio_is_input_channel(channelMask)) {
1643 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1644 lStatus = BAD_VALUE;
1645 goto Exit;
1646 }
1647
Glenn Kasten05997e22014-03-13 15:08:33 -07001648 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001649 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001650 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001651 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001652 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001653 lStatus = BAD_VALUE;
1654 goto Exit;
1655 }
1656
Eric Laurent021cf962014-05-13 10:18:14 -07001657 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001658
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001659 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001660 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1661 lStatus = BAD_VALUE;
1662 goto Exit;
1663 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001664 lSessionId = *sessionId;
1665 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001666 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001667 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001668 if (sessionId != NULL) {
1669 *sessionId = lSessionId;
1670 }
1671 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001672 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001673
Glenn Kasten1879fff2012-07-11 15:36:59 -07001674 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001675 frameCount, lSessionId, notificationFrames,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001676 clientUid, flags, tid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001677 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001678
1679 if (lStatus == NO_ERROR) {
1680 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1681 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001682 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001683 if (chain != 0) {
1684 Mutex::Autolock _l(thread->mLock);
1685 thread->addEffectChain_l(chain);
1686 }
1687 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001688 }
Glenn Kastene198c362013-08-13 09:13:36 -07001689
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001690 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001691 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001692 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001693 // Don't hold mClientLock when releasing the reference on the track as the
1694 // destructor will acquire it.
1695 {
1696 Mutex::Autolock _cl(mClientLock);
1697 client.clear();
1698 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001699 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001700 goto Exit;
1701 }
1702
Glenn Kastend776ac62014-05-07 09:16:09 -07001703 cblk = recordTrack->getCblk();
1704 buffers = recordTrack->getBuffers();
1705
Glenn Kasten2fc14732013-08-05 14:58:14 -07001706 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001707 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001708
1709Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001710 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001711 return recordHandle;
1712}
1713
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001714
1715
Mathias Agopian65ab4712010-07-14 17:59:35 -07001716// ----------------------------------------------------------------------------
1717
Eric Laurenta4c5a552012-03-29 10:12:40 -07001718audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1719{
Eric Laurent44622db2014-08-01 19:00:33 -07001720 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001721 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001722 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001723 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001724 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001725 }
1726 Mutex::Autolock _l(mLock);
1727 return loadHwModule_l(name);
1728}
1729
1730// loadHwModule_l() must be called with AudioFlinger::mLock held
1731audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1732{
1733 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1734 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1735 ALOGW("loadHwModule() module %s already loaded", name);
1736 return mAudioHwDevs.keyAt(i);
1737 }
1738 }
1739
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001740 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001741
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001742 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001743 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001744 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001745 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001746 }
1747
1748 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001749 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07001750 mHardwareStatus = AUDIO_HW_IDLE;
1751 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001752 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001753 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001754 }
1755
John Grossmanee578c02012-07-23 17:05:46 -07001756 // Check and cache this HAL's level of support for master mute and master
1757 // volume. If this is the first HAL opened, and it supports the get
1758 // methods, use the initial values provided by the HAL as the current
1759 // master mute and volume settings.
1760
1761 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1762 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001763 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001764
1765 if (0 == mAudioHwDevs.size()) {
1766 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001767 float mv;
1768 if (OK == dev->getMasterVolume(&mv)) {
1769 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07001770 }
1771
1772 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001773 bool mm;
1774 if (OK == dev->getMasterMute(&mm)) {
1775 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07001776 }
1777 }
1778
Eric Laurenta4c5a552012-03-29 10:12:40 -07001779 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001780 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07001781 flags = static_cast<AudioHwDevice::Flags>(flags |
1782 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1783 }
1784
1785 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001786 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07001787 flags = static_cast<AudioHwDevice::Flags>(flags |
1788 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1789 }
1790
Eric Laurenta4c5a552012-03-29 10:12:40 -07001791 mHardwareStatus = AUDIO_HW_IDLE;
1792 }
1793
Glenn Kastena13cde92016-03-28 15:26:02 -07001794 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001795 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001796
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001797 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001798
1799 return handle;
1800
1801}
1802
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001803// ----------------------------------------------------------------------------
1804
Glenn Kasten3b16c762012-11-14 08:44:39 -08001805uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001806{
1807 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001808 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001809 return thread != NULL ? thread->sampleRate() : 0;
1810}
1811
Glenn Kastene33054e2012-11-14 12:54:39 -08001812size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001813{
1814 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001815 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001816 return thread != NULL ? thread->frameCountHAL() : 0;
1817}
1818
1819// ----------------------------------------------------------------------------
1820
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001821status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1822{
1823 uid_t uid = IPCThreadState::self()->getCallingUid();
1824 if (uid != AID_SYSTEM) {
1825 return PERMISSION_DENIED;
1826 }
1827 Mutex::Autolock _l(mLock);
1828 if (mIsDeviceTypeKnown) {
1829 return INVALID_OPERATION;
1830 }
1831 mIsLowRamDevice = isLowRamDevice;
1832 mIsDeviceTypeKnown = true;
1833 return NO_ERROR;
1834}
1835
Eric Laurent93c3d412014-08-01 14:48:35 -07001836audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1837{
1838 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001839
1840 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1841 if (index >= 0) {
1842 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1843 mHwAvSyncIds.valueAt(index), sessionId);
1844 return mHwAvSyncIds.valueAt(index);
1845 }
1846
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001847 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07001848 if (dev == NULL) {
1849 return AUDIO_HW_SYNC_INVALID;
1850 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001851 String8 reply;
1852 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001853 if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001854 param = AudioParameter(reply);
1855 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001856
1857 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001858 if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001859 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1860 return AUDIO_HW_SYNC_INVALID;
1861 }
1862
1863 // allow only one session for a given HW A/V sync ID.
1864 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1865 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1866 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1867 value, mHwAvSyncIds.keyAt(i));
1868 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001869 break;
1870 }
1871 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001872
1873 mHwAvSyncIds.add(sessionId, value);
1874
1875 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1876 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1877 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07001878 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001879 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001880 param.addInt(String8(AudioParameter::keyStreamHwAvSync), value);
Eric Laurentfa90e842014-10-17 18:12:31 -07001881 thread->setParameters(param.toString());
1882 break;
1883 }
1884 }
1885
1886 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1887 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001888}
1889
Eric Laurent72e3f392015-05-20 14:43:50 -07001890status_t AudioFlinger::systemReady()
1891{
1892 Mutex::Autolock _l(mLock);
1893 ALOGI("%s", __FUNCTION__);
1894 if (mSystemReady) {
1895 ALOGW("%s called twice", __FUNCTION__);
1896 return NO_ERROR;
1897 }
1898 mSystemReady = true;
1899 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1900 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1901 thread->systemReady();
1902 }
1903 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1904 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1905 thread->systemReady();
1906 }
1907 return NO_ERROR;
1908}
1909
Eric Laurentfa90e842014-10-17 18:12:31 -07001910// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1911void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1912{
1913 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1914 if (index >= 0) {
1915 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1916 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1917 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001918 param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId);
Eric Laurentfa90e842014-10-17 18:12:31 -07001919 thread->setParameters(param.toString());
1920 }
1921}
1922
1923
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001924// ----------------------------------------------------------------------------
1925
Eric Laurent83b88082014-06-20 18:31:16 -07001926
Eric Laurent6acd1d42017-01-04 14:23:29 -08001927sp<AudioFlinger::ThreadBase> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001928 audio_io_handle_t *output,
1929 audio_config_t *config,
1930 audio_devices_t devices,
1931 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001932 audio_output_flags_t flags)
1933{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001934 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001935 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001936 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001937 }
1938
Eric Laurentcf2c0212014-07-25 16:20:43 -07001939 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001940 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1941 } else {
1942 // Audio Policy does not currently request a specific output handle.
1943 // If this is ever needed, see openInput_l() for example code.
1944 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1945 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001946 }
Eric Laurent83b88082014-06-20 18:31:16 -07001947
1948 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1949
Eric Laurent83b88082014-06-20 18:31:16 -07001950 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001951 // This if statement allows overriding the audio policy settings
1952 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1953 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1954 // Check only for Normal Mixing mode
1955 if (kEnableExtendedPrecision) {
1956 // Specify format (uncomment one below to choose)
1957 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1958 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1959 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1960 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001961 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001962 }
1963 if (kEnableExtendedChannels) {
1964 // Specify channel mask (uncomment one below to choose)
1965 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1966 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1967 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1968 }
Eric Laurent83b88082014-06-20 18:31:16 -07001969 }
1970
Phil Burk062e67a2015-02-11 13:40:50 -08001971 AudioStreamOut *outputStream = NULL;
1972 status_t status = outHwDev->openOutputStream(
1973 &outputStream,
1974 *output,
1975 devices,
1976 flags,
1977 config,
1978 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001979
1980 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001981
Phil Burk062e67a2015-02-11 13:40:50 -08001982 if (status == NO_ERROR) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001983 if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
1984 sp<MmapPlaybackThread> thread =
1985 new MmapPlaybackThread(this, *output, outHwDev, outputStream,
1986 devices, AUDIO_DEVICE_NONE, mSystemReady);
1987 mMmapThreads.add(*output, thread);
1988 ALOGV("openOutput_l() created mmap playback thread: ID %d thread %p",
1989 *output, thread.get());
1990 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07001991 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001992 sp<PlaybackThread> thread;
1993 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1994 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
1995 ALOGV("openOutput_l() created offload output: ID %d thread %p",
1996 *output, thread.get());
1997 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1998 || !isValidPcmSinkFormat(config->format)
1999 || !isValidPcmSinkChannelMask(config->channel_mask)) {
2000 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
2001 ALOGV("openOutput_l() created direct output: ID %d thread %p",
2002 *output, thread.get());
2003 } else {
2004 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
2005 ALOGV("openOutput_l() created mixer output: ID %d thread %p",
2006 *output, thread.get());
2007 }
2008 mPlaybackThreads.add(*output, thread);
2009 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07002010 }
Eric Laurent83b88082014-06-20 18:31:16 -07002011 }
2012
2013 return 0;
2014}
2015
Eric Laurentcf2c0212014-07-25 16:20:43 -07002016status_t AudioFlinger::openOutput(audio_module_handle_t module,
2017 audio_io_handle_t *output,
2018 audio_config_t *config,
2019 audio_devices_t *devices,
2020 const String8& address,
2021 uint32_t *latencyMs,
2022 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002023{
Glenn Kastend3bb6452016-12-05 18:14:37 -08002024 ALOGI("openOutput() this %p, module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, "
2025 "flags %x",
Eric Laurent6acd1d42017-01-04 14:23:29 -08002026 this, module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002027 (devices != NULL) ? *devices : 0,
2028 config->sample_rate,
2029 config->format,
2030 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002031 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002032
Yunlian Jiang32ba9862017-01-31 15:55:00 -08002033 if (devices == NULL || *devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002034 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002035 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002036
Mathias Agopian65ab4712010-07-14 17:59:35 -07002037 Mutex::Autolock _l(mLock);
2038
Eric Laurent6acd1d42017-01-04 14:23:29 -08002039 sp<ThreadBase> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002040 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002041 if ((flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) {
2042 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
2043 *latencyMs = playbackThread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002044
Eric Laurent6acd1d42017-01-04 14:23:29 -08002045 // notify client processes of the new output creation
2046 playbackThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002047
Eric Laurent6acd1d42017-01-04 14:23:29 -08002048 // the first primary output opened designates the primary hw device
2049 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -08002050 ALOGI("Using module %d as the primary audio interface", module);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002051 mPrimaryHardwareDev = playbackThread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002052
Eric Laurent6acd1d42017-01-04 14:23:29 -08002053 AutoMutex lock(mHardwareLock);
2054 mHardwareStatus = AUDIO_HW_SET_MODE;
2055 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
2056 mHardwareStatus = AUDIO_HW_IDLE;
2057 }
2058 } else {
2059 MmapThread *mmapThread = (MmapThread *)thread.get();
2060 mmapThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002061 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002062 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002063 }
2064
Eric Laurentcf2c0212014-07-25 16:20:43 -07002065 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002066}
2067
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002068audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
2069 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002070{
2071 Mutex::Autolock _l(mLock);
2072 MixerThread *thread1 = checkMixerThread_l(output1);
2073 MixerThread *thread2 = checkMixerThread_l(output2);
2074
2075 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002076 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
2077 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07002078 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002079 }
2080
Glenn Kasteneeecb982016-02-26 10:44:04 -08002081 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07002082 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002083 thread->addOutputTrack(thread2);
2084 mPlaybackThreads.add(id, thread);
2085 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002086 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002087 return id;
2088}
2089
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002090status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002091{
Glenn Kastend96c5722012-04-25 13:44:49 -07002092 return closeOutput_nonvirtual(output);
2093}
2094
2095status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
2096{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002097 // keep strong reference on the playback thread so that
2098 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002099 sp<PlaybackThread> playbackThread;
2100 sp<MmapPlaybackThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002101 {
2102 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002103 playbackThread = checkPlaybackThread_l(output);
2104 if (playbackThread != NULL) {
2105 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002106
Eric Laurent6acd1d42017-01-04 14:23:29 -08002107 if (playbackThread->type() == ThreadBase::MIXER) {
2108 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2109 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
2110 DuplicatingThread *dupThread =
2111 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
2112 dupThread->removeOutputTrack((MixerThread *)playbackThread.get());
2113 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002114 }
2115 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002116
2117
Eric Laurent6acd1d42017-01-04 14:23:29 -08002118 mPlaybackThreads.removeItem(output);
2119 // save all effects to the default thread
2120 if (mPlaybackThreads.size()) {
2121 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
2122 if (dstThread != NULL) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002123 // audioflinger lock is held so order of thread lock acquisition doesn't matter
Eric Laurent6acd1d42017-01-04 14:23:29 -08002124 Mutex::Autolock _dl(dstThread->mLock);
2125 Mutex::Autolock _sl(playbackThread->mLock);
2126 Vector< sp<EffectChain> > effectChains = playbackThread->getEffectChains_l();
2127 for (size_t i = 0; i < effectChains.size(); i ++) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002128 moveEffectChain_l(effectChains[i]->sessionId(), playbackThread.get(),
2129 dstThread, true);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002130 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002131 }
2132 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002133 } else {
2134 mmapThread = (MmapPlaybackThread *)checkMmapThread_l(output);
2135 if (mmapThread == 0) {
2136 return BAD_VALUE;
2137 }
2138 mMmapThreads.removeItem(output);
Phil Burk7f6b40d2017-02-09 13:18:38 -08002139 ALOGD("closing mmapThread %p", mmapThread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002140 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002141 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2142 ioDesc->mIoHandle = output;
2143 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002144 }
Glenn Kastenb28686f2012-01-06 08:39:38 -08002145 // The thread entity (active unit of execution) is no longer running here,
2146 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002147
Eric Laurent6acd1d42017-01-04 14:23:29 -08002148 if (playbackThread != 0) {
2149 playbackThread->exit();
2150 if (!playbackThread->isDuplicating()) {
2151 closeOutputFinish(playbackThread);
2152 }
2153 } else if (mmapThread != 0) {
Phil Burk7f6b40d2017-02-09 13:18:38 -08002154 ALOGD("mmapThread exit()");
Eric Laurent6acd1d42017-01-04 14:23:29 -08002155 mmapThread->exit();
2156 AudioStreamOut *out = mmapThread->clearOutput();
2157 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2158 // from now on thread->mOutput is NULL
2159 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002160 }
2161 return NO_ERROR;
2162}
2163
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002164void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002165{
2166 AudioStreamOut *out = thread->clearOutput();
2167 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2168 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07002169 delete out;
2170}
2171
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002172void AudioFlinger::closeOutputInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002173{
2174 mPlaybackThreads.removeItem(thread->mId);
2175 thread->exit();
2176 closeOutputFinish(thread);
2177}
2178
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002179status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002180{
2181 Mutex::Autolock _l(mLock);
2182 PlaybackThread *thread = checkPlaybackThread_l(output);
2183
2184 if (thread == NULL) {
2185 return BAD_VALUE;
2186 }
2187
Steve Block3856b092011-10-20 11:56:00 +01002188 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002189 thread->suspend();
2190
2191 return NO_ERROR;
2192}
2193
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002194status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002195{
2196 Mutex::Autolock _l(mLock);
2197 PlaybackThread *thread = checkPlaybackThread_l(output);
2198
2199 if (thread == NULL) {
2200 return BAD_VALUE;
2201 }
2202
Steve Block3856b092011-10-20 11:56:00 +01002203 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002204
2205 thread->restore();
2206
2207 return NO_ERROR;
2208}
2209
Eric Laurentcf2c0212014-07-25 16:20:43 -07002210status_t AudioFlinger::openInput(audio_module_handle_t module,
2211 audio_io_handle_t *input,
2212 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002213 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002214 const String8& address,
2215 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002216 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002217{
Eric Laurent83b88082014-06-20 18:31:16 -07002218 Mutex::Autolock _l(mLock);
2219
Glenn Kastene7d66712015-03-05 13:46:52 -08002220 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002221 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002222 }
2223
Eric Laurent6acd1d42017-01-04 14:23:29 -08002224 sp<ThreadBase> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002225
2226 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002227 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002228 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002229 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002230 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002231 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002232}
Dima Zavin799a70e2011-04-18 16:57:27 -07002233
Eric Laurent6acd1d42017-01-04 14:23:29 -08002234sp<AudioFlinger::ThreadBase> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002235 audio_io_handle_t *input,
2236 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002237 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002238 const String8& address,
2239 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002240 audio_input_flags_t flags)
2241{
Glenn Kastene7d66712015-03-05 13:46:52 -08002242 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002243 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002244 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002245 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002246 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002247
Glenn Kasteneeecb982016-02-26 10:44:04 -08002248 // Audio Policy can request a specific handle for hardware hotword.
2249 // The goal here is not to re-open an already opened input.
2250 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002251 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002252 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2253 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2254 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2255 return 0;
2256 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2257 // This should not happen in a transient state with current design.
2258 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2259 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002260 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002261
Eric Laurentcf2c0212014-07-25 16:20:43 -07002262 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002263 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002264 sp<StreamInHalInterface> inStream;
2265 status_t status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002266 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Mikhail Naganovf558e022016-11-14 17:45:17 -08002267 ALOGV("openInput_l() openInputStream returned input %p, devices %x, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002268 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002269 inStream.get(),
Mikhail Naganovf558e022016-11-14 17:45:17 -08002270 devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002271 halconfig.sample_rate,
2272 halconfig.format,
2273 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002274 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002275 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002276
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002277 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002278 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002279 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002280 audio_is_linear_pcm(config->format) &&
2281 audio_is_linear_pcm(halconfig.format) &&
2282 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002283 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2284 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002285 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002286 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002287 inStream.clear();
2288 status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002289 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002290 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002291 }
2292
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002293 if (status == NO_ERROR && inStream != 0) {
Eric Laurent05067782016-06-01 18:27:28 -07002294 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002295 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) {
2296 sp<MmapCaptureThread> thread =
2297 new MmapCaptureThread(this, *input,
2298 inHwDev, inputStream,
2299 primaryOutputDevice_l(), devices, mSystemReady);
2300 mMmapThreads.add(*input, thread);
Glenn Kastend3bb6452016-12-05 18:14:37 -08002301 ALOGV("openInput_l() created mmap capture thread: ID %d thread %p", *input,
2302 thread.get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002303 return thread;
2304 } else {
Glenn Kasten46909e72013-02-26 09:20:22 -08002305#ifdef TEE_SINK
Eric Laurent6acd1d42017-01-04 14:23:29 -08002306 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2307 // or (re-)create if current Pipe is idle and does not match the new format
2308 sp<NBAIO_Sink> teeSink;
2309 enum {
2310 TEE_SINK_NO, // don't copy input
2311 TEE_SINK_NEW, // copy input using a new pipe
2312 TEE_SINK_OLD, // copy input using an existing pipe
2313 } kind;
2314 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2315 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
2316 if (!mTeeSinkInputEnabled) {
2317 kind = TEE_SINK_NO;
2318 } else if (!Format_isValid(format)) {
2319 kind = TEE_SINK_NO;
2320 } else if (mRecordTeeSink == 0) {
2321 kind = TEE_SINK_NEW;
2322 } else if (mRecordTeeSink->getStrongCount() != 1) {
2323 kind = TEE_SINK_NO;
2324 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
2325 kind = TEE_SINK_OLD;
2326 } else {
2327 kind = TEE_SINK_NEW;
2328 }
2329 switch (kind) {
2330 case TEE_SINK_NEW: {
2331 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
2332 size_t numCounterOffers = 0;
2333 const NBAIO_Format offers[1] = {format};
2334 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2335 ALOG_ASSERT(index == 0);
2336 PipeReader *pipeReader = new PipeReader(*pipe);
2337 numCounterOffers = 0;
2338 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2339 ALOG_ASSERT(index == 0);
2340 mRecordTeeSink = pipe;
2341 mRecordTeeSource = pipeReader;
2342 teeSink = pipe;
2343 }
2344 break;
2345 case TEE_SINK_OLD:
2346 teeSink = mRecordTeeSink;
2347 break;
2348 case TEE_SINK_NO:
2349 default:
2350 break;
2351 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002352#endif
Eric Laurent6acd1d42017-01-04 14:23:29 -08002353
2354 // Start record thread
2355 // RecordThread requires both input and output device indication to forward to audio
2356 // pre processing modules
2357 sp<RecordThread> thread = new RecordThread(this,
2358 inputStream,
2359 *input,
2360 primaryOutputDevice_l(),
2361 devices,
2362 mSystemReady
2363#ifdef TEE_SINK
2364 , teeSink
2365#endif
2366 );
2367 mRecordThreads.add(*input, thread);
2368 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
2369 return thread;
2370 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002371 }
2372
Eric Laurentcf2c0212014-07-25 16:20:43 -07002373 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002374 return 0;
2375}
2376
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002377status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002378{
Glenn Kastend96c5722012-04-25 13:44:49 -07002379 return closeInput_nonvirtual(input);
2380}
2381
2382status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2383{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002384 // keep strong reference on the record thread so that
2385 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002386 sp<RecordThread> recordThread;
2387 sp<MmapCaptureThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002388 {
2389 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002390 recordThread = checkRecordThread_l(input);
2391 if (recordThread != 0) {
2392 ALOGV("closeInput() %d", input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002393
Eric Laurent6acd1d42017-01-04 14:23:29 -08002394 // If we still have effect chains, it means that a client still holds a handle
2395 // on at least one effect. We must either move the chain to an existing thread with the
2396 // same session ID or put it aside in case a new record thread is opened for a
2397 // new capture on the same session
2398 sp<EffectChain> chain;
2399 {
2400 Mutex::Autolock _sl(recordThread->mLock);
2401 Vector< sp<EffectChain> > effectChains = recordThread->getEffectChains_l();
2402 // Note: maximum one chain per record thread
2403 if (effectChains.size() != 0) {
2404 chain = effectChains[0];
Eric Laurent1b928682014-10-02 19:41:47 -07002405 }
2406 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002407 if (chain != 0) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002408 // first check if a record thread is already opened with a client on same session.
Eric Laurent6acd1d42017-01-04 14:23:29 -08002409 // This should only happen in case of overlap between one thread tear down and the
2410 // creation of its replacement
2411 size_t i;
2412 for (i = 0; i < mRecordThreads.size(); i++) {
2413 sp<RecordThread> t = mRecordThreads.valueAt(i);
2414 if (t == recordThread) {
2415 continue;
2416 }
2417 if (t->hasAudioSession(chain->sessionId()) != 0) {
2418 Mutex::Autolock _l(t->mLock);
2419 ALOGV("closeInput() found thread %d for effect session %d",
2420 t->id(), chain->sessionId());
2421 t->addEffectChain_l(chain);
2422 break;
2423 }
2424 }
Glenn Kastend3bb6452016-12-05 18:14:37 -08002425 // put the chain aside if we could not find a record thread with the same session id
Eric Laurent6acd1d42017-01-04 14:23:29 -08002426 if (i == mRecordThreads.size()) {
2427 putOrphanEffectChain_l(chain);
2428 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002429 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002430 mRecordThreads.removeItem(input);
2431 } else {
2432 mmapThread = (MmapCaptureThread *)checkMmapThread_l(input);
2433 if (mmapThread == 0) {
2434 return BAD_VALUE;
2435 }
2436 mMmapThreads.removeItem(input);
Eric Laurentaaa44472014-09-12 17:41:50 -07002437 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002438 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2439 ioDesc->mIoHandle = input;
2440 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002441 }
Eric Laurent83b88082014-06-20 18:31:16 -07002442 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2443 // we have a different lock for notification client
Eric Laurent6acd1d42017-01-04 14:23:29 -08002444 if (recordThread != 0) {
2445 closeInputFinish(recordThread);
2446 } else if (mmapThread != 0) {
2447 mmapThread->exit();
2448 AudioStreamIn *in = mmapThread->clearInput();
2449 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
2450 // from now on thread->mInput is NULL
2451 delete in;
2452 }
Eric Laurent83b88082014-06-20 18:31:16 -07002453 return NO_ERROR;
2454}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002455
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002456void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002457{
2458 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002459 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002460 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002461 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002462 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002463}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002464
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002465void AudioFlinger::closeInputInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002466{
2467 mRecordThreads.removeItem(thread->mId);
2468 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002469}
2470
Glenn Kastend2304db2014-02-03 07:40:31 -08002471status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002472{
2473 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002474 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002475
2476 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2477 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002478 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002479 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002480 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2481 mMmapThreads[i]->invalidateTracks(stream);
2482 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002483 return NO_ERROR;
2484}
2485
2486
Glenn Kasteneeecb982016-02-26 10:44:04 -08002487audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002488{
Glenn Kasten9d003132016-04-06 14:38:09 -07002489 // This is a binder API, so a malicious client could pass in a bad parameter.
2490 // Check for that before calling the internal API nextUniqueId().
2491 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2492 ALOGE("newAudioUniqueId invalid use %d", use);
2493 return AUDIO_UNIQUE_ID_ALLOCATE;
2494 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002495 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002496}
2497
Glenn Kastend848eb42016-03-08 13:42:11 -08002498void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002499{
2500 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002501 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002502 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2503 if (pid != -1 && (caller == getpid_cached)) {
2504 caller = pid;
2505 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002506
Eric Laurent021cf962014-05-13 10:18:14 -07002507 {
2508 Mutex::Autolock _cl(mClientLock);
2509 // Ignore requests received from processes not known as notification client. The request
2510 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2511 // called from a different pid leaving a stale session reference. Also we don't know how
2512 // to clear this reference if the client process dies.
2513 if (mNotificationClients.indexOfKey(caller) < 0) {
2514 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2515 return;
2516 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002517 }
2518
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002519 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002520 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002521 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002522 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2523 ref->mCnt++;
2524 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002525 return;
2526 }
2527 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002528 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2529 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002530}
2531
Glenn Kastend848eb42016-03-08 13:42:11 -08002532void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002533{
2534 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002535 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002536 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2537 if (pid != -1 && (caller == getpid_cached)) {
2538 caller = pid;
2539 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002540 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002541 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002542 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002543 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2544 ref->mCnt--;
2545 ALOGV(" decremented refcount to %d", ref->mCnt);
2546 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002547 mAudioSessionRefs.removeAt(i);
2548 delete ref;
2549 purgeStaleEffects_l();
2550 }
2551 return;
2552 }
2553 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002554 // If the caller is mediaserver it is likely that the session being released was acquired
2555 // on behalf of a process not in notification clients and we ignore the warning.
2556 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002557}
2558
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002559bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
2560{
2561 size_t num = mAudioSessionRefs.size();
2562 for (size_t i = 0; i < num; i++) {
2563 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2564 if (ref->mSessionid == audioSession) {
2565 return true;
2566 }
2567 }
2568 return false;
2569}
2570
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002571void AudioFlinger::purgeStaleEffects_l() {
2572
Steve Block3856b092011-10-20 11:56:00 +01002573 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002574
2575 Vector< sp<EffectChain> > chains;
2576
2577 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2578 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2579 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2580 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002581 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2582 chains.push(ec);
2583 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002584 }
2585 }
2586 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2587 sp<RecordThread> t = mRecordThreads.valueAt(i);
2588 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2589 sp<EffectChain> ec = t->mEffectChains[j];
2590 chains.push(ec);
2591 }
2592 }
2593
2594 for (size_t i = 0; i < chains.size(); i++) {
2595 sp<EffectChain> ec = chains[i];
2596 int sessionid = ec->sessionId();
2597 sp<ThreadBase> t = ec->mThread.promote();
2598 if (t == 0) {
2599 continue;
2600 }
2601 size_t numsessionrefs = mAudioSessionRefs.size();
2602 bool found = false;
2603 for (size_t k = 0; k < numsessionrefs; k++) {
2604 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002605 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002606 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002607 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002608 found = true;
2609 break;
2610 }
2611 }
2612 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002613 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002614 // remove all effects from the chain
2615 while (ec->mEffects.size()) {
2616 sp<EffectModule> effect = ec->mEffects[0];
2617 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002618 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002619 if (effect->purgeHandles()) {
2620 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002621 }
2622 AudioSystem::unregisterEffect(effect->id());
2623 }
2624 }
2625 }
2626 return;
2627}
2628
Glenn Kasteneeecb982016-02-26 10:44:04 -08002629// checkThread_l() must be called with AudioFlinger::mLock held
2630AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2631{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002632 ThreadBase *thread = checkMmapThread_l(ioHandle);
2633 if (thread == 0) {
2634 switch (audio_unique_id_get_use(ioHandle)) {
2635 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2636 thread = checkPlaybackThread_l(ioHandle);
2637 break;
2638 case AUDIO_UNIQUE_ID_USE_INPUT:
2639 thread = checkRecordThread_l(ioHandle);
2640 break;
2641 default:
2642 break;
2643 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002644 }
2645 return thread;
2646}
2647
Mathias Agopian65ab4712010-07-14 17:59:35 -07002648// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002649AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002650{
Glenn Kastena1117922012-01-26 10:53:32 -08002651 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002652}
2653
2654// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002655AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002656{
2657 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002658 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002659}
2660
2661// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002662AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002663{
Glenn Kastena1117922012-01-26 10:53:32 -08002664 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002665}
2666
Eric Laurent6acd1d42017-01-04 14:23:29 -08002667// checkMmapThread_l() must be called with AudioFlinger::mLock held
2668AudioFlinger::MmapThread *AudioFlinger::checkMmapThread_l(audio_io_handle_t io) const
2669{
2670 return mMmapThreads.valueFor(io).get();
2671}
2672
2673
2674// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
2675AudioFlinger::VolumeInterface *AudioFlinger::getVolumeInterface_l(audio_io_handle_t output) const
2676{
Eric Laurent7459b902017-04-19 18:10:41 -07002677 VolumeInterface *volumeInterface = mPlaybackThreads.valueFor(output).get();
Eric Laurent6acd1d42017-01-04 14:23:29 -08002678 if (volumeInterface == nullptr) {
2679 MmapThread *mmapThread = mMmapThreads.valueFor(output).get();
2680 if (mmapThread != nullptr) {
2681 if (mmapThread->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07002682 MmapPlaybackThread *mmapPlaybackThread =
2683 static_cast<MmapPlaybackThread *>(mmapThread);
2684 volumeInterface = mmapPlaybackThread;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002685 }
2686 }
2687 }
2688 return volumeInterface;
2689}
2690
2691Vector <AudioFlinger::VolumeInterface *> AudioFlinger::getAllVolumeInterfaces_l() const
2692{
2693 Vector <VolumeInterface *> volumeInterfaces;
2694 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7459b902017-04-19 18:10:41 -07002695 volumeInterfaces.add(mPlaybackThreads.valueAt(i).get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002696 }
2697 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2698 if (mMmapThreads.valueAt(i)->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07002699 MmapPlaybackThread *mmapPlaybackThread =
2700 static_cast<MmapPlaybackThread *>(mMmapThreads.valueAt(i).get());
2701 volumeInterfaces.add(mmapPlaybackThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002702 }
2703 }
2704 return volumeInterfaces;
2705}
2706
Glenn Kasteneeecb982016-02-26 10:44:04 -08002707audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002708{
Glenn Kasten9d003132016-04-06 14:38:09 -07002709 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002710 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002711 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2712 for (int retry = 0; retry < maxRetries; retry++) {
2713 // The cast allows wraparound from max positive to min negative instead of abort
2714 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2715 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2716 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2717 // allow wrap by skipping 0 and -1 for session ids
2718 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2719 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2720 return (audio_unique_id_t) (base | use);
2721 }
2722 }
2723 // We have no way of recovering from wraparound
2724 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2725 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002726}
2727
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002728AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002729{
2730 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2731 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002732 if(thread->isDuplicating()) {
2733 continue;
2734 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002735 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002736 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002737 return thread;
2738 }
2739 }
2740 return NULL;
2741}
2742
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002743audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002744{
2745 PlaybackThread *thread = primaryPlaybackThread_l();
2746
2747 if (thread == NULL) {
2748 return 0;
2749 }
2750
Eric Laurentf1c04f92012-08-28 14:26:53 -07002751 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002752}
2753
Glenn Kastena7335632016-06-09 17:09:53 -07002754AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
2755{
2756 size_t minFrameCount = 0;
2757 PlaybackThread *minThread = NULL;
2758 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2759 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2760 if (!thread->isDuplicating()) {
2761 size_t frameCount = thread->frameCountHAL();
2762 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
2763 (frameCount == minFrameCount && thread->hasFastMixer() &&
2764 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
2765 minFrameCount = frameCount;
2766 minThread = thread;
2767 }
2768 }
2769 }
2770 return minThread;
2771}
2772
Eric Laurenta011e352012-03-29 15:51:43 -07002773sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002774 audio_session_t triggerSession,
2775 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002776 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002777 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002778{
2779 Mutex::Autolock _l(mLock);
2780
2781 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2782 status_t playStatus = NAME_NOT_FOUND;
2783 status_t recStatus = NAME_NOT_FOUND;
2784 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2785 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2786 if (playStatus == NO_ERROR) {
2787 return event;
2788 }
2789 }
2790 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2791 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2792 if (recStatus == NO_ERROR) {
2793 return event;
2794 }
2795 }
2796 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2797 mPendingSyncEvents.add(event);
2798 } else {
2799 ALOGV("createSyncEvent() invalid event %d", event->type());
2800 event.clear();
2801 }
2802 return event;
2803}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002804
Mathias Agopian65ab4712010-07-14 17:59:35 -07002805// ----------------------------------------------------------------------------
2806// Effect management
2807// ----------------------------------------------------------------------------
2808
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002809sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
2810 return mEffectsFactoryHal;
2811}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002812
Glenn Kastenf587ba52012-01-26 16:25:10 -08002813status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002814{
2815 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002816 if (mEffectsFactoryHal.get()) {
2817 return mEffectsFactoryHal->queryNumberEffects(numEffects);
2818 } else {
2819 return -ENODEV;
2820 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002821}
2822
Glenn Kastenf587ba52012-01-26 16:25:10 -08002823status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002824{
2825 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002826 if (mEffectsFactoryHal.get()) {
2827 return mEffectsFactoryHal->getDescriptor(index, descriptor);
2828 } else {
2829 return -ENODEV;
2830 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002831}
2832
Glenn Kasten5e92a782012-01-30 07:40:52 -08002833status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002834 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002835{
2836 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002837 if (mEffectsFactoryHal.get()) {
2838 return mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
2839 } else {
2840 return -ENODEV;
2841 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002842}
2843
2844
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002845sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002846 effect_descriptor_t *pDesc,
2847 const sp<IEffectClient>& effectClient,
2848 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002849 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002850 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002851 const String16& opPackageName,
Eric Laurentb6436272016-12-07 19:24:50 -08002852 pid_t pid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002853 status_t *status,
2854 int *id,
2855 int *enabled)
2856{
2857 status_t lStatus = NO_ERROR;
2858 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002859 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002860
Eric Laurentb6436272016-12-07 19:24:50 -08002861 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
2862 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
2863 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
2864 ALOGW_IF(pid != -1 && pid != callingPid,
2865 "%s uid %d pid %d tried to pass itself off as pid %d",
2866 __func__, callingUid, callingPid, pid);
2867 pid = callingPid;
2868 }
2869
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002870 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
2871 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002872
2873 if (pDesc == NULL) {
2874 lStatus = BAD_VALUE;
2875 goto Exit;
2876 }
2877
Eric Laurent84e9a102010-09-23 16:10:16 -07002878 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002879 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002880 lStatus = PERMISSION_DENIED;
2881 goto Exit;
2882 }
2883
Dima Zavinfce7a472011-04-19 22:30:36 -07002884 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002885 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002886 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002887 lStatus = PERMISSION_DENIED;
2888 goto Exit;
2889 }
2890
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002891 if (mEffectsFactoryHal == 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002892 lStatus = NO_INIT;
2893 goto Exit;
2894 }
2895
Mathias Agopian65ab4712010-07-14 17:59:35 -07002896 {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002897 if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002898 // if uuid is specified, request effect descriptor
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002899 lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002900 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002901 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002902 goto Exit;
2903 }
2904 } else {
2905 // if uuid is not specified, look for an available implementation
2906 // of the required type in effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002907 if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002908 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002909 lStatus = BAD_VALUE;
2910 goto Exit;
2911 }
2912 uint32_t numEffects = 0;
2913 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002914 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002915 bool found = false;
2916
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002917 lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002918 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002919 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002920 goto Exit;
2921 }
2922 for (uint32_t i = 0; i < numEffects; i++) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002923 lStatus = mEffectsFactoryHal->getDescriptor(i, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002924 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002925 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002926 continue;
2927 }
2928 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2929 // If matching type found save effect descriptor. If the session is
2930 // 0 and the effect is not auxiliary, continue enumeration in case
2931 // an auxiliary version of this effect type is available
2932 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002933 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002934 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002935 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2936 break;
2937 }
2938 }
2939 }
2940 if (!found) {
2941 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002942 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002943 goto Exit;
2944 }
2945 // For same effect type, chose auxiliary version over insert version if
2946 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002947 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002948 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002949 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002950 }
2951 }
2952
2953 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002954 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002955 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2956 lStatus = INVALID_OPERATION;
2957 goto Exit;
2958 }
2959
Eric Laurent59255e42011-07-27 19:49:51 -07002960 // check recording permission for visualizer
2961 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002962 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002963 lStatus = PERMISSION_DENIED;
2964 goto Exit;
2965 }
2966
Mathias Agopian65ab4712010-07-14 17:59:35 -07002967 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002968 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002969 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002970 // if the output returned by getOutputForEffect() is removed before we lock the
2971 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2972 // and we will exit safely
2973 io = AudioSystem::getOutputForEffect(&desc);
2974 ALOGV("createEffect got output %d", io);
2975 }
2976
2977 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002978
2979 // If output is not specified try to find a matching audio session ID in one of the
2980 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002981 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2982 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002983 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002984 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002985 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2986 // output must be specified by AudioPolicyManager when using session
2987 // AUDIO_SESSION_OUTPUT_STAGE
2988 lStatus = BAD_VALUE;
2989 goto Exit;
2990 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002991 // look for the thread where the specified audio session is present
2992 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2993 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2994 io = mPlaybackThreads.keyAt(i);
2995 break;
2996 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002997 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002998 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002999 for (size_t i = 0; i < mRecordThreads.size(); i++) {
3000 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
3001 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07003002 break;
3003 }
3004 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003005 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08003006 if (io == AUDIO_IO_HANDLE_NONE) {
3007 for (size_t i = 0; i < mMmapThreads.size(); i++) {
3008 if (mMmapThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
3009 io = mMmapThreads.keyAt(i);
3010 break;
3011 }
3012 }
3013 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003014 // If no output thread contains the requested session ID, default to
3015 // first output. The effect chain will be moved to the correct output
3016 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07003017 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003018 io = mPlaybackThreads.keyAt(0);
3019 }
Steve Block3856b092011-10-20 11:56:00 +01003020 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003021 }
3022 ThreadBase *thread = checkRecordThread_l(io);
3023 if (thread == NULL) {
3024 thread = checkPlaybackThread_l(io);
3025 if (thread == NULL) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08003026 thread = checkMmapThread_l(io);
3027 if (thread == NULL) {
3028 ALOGE("createEffect() unknown output thread");
3029 lStatus = BAD_VALUE;
3030 goto Exit;
3031 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003032 }
Eric Laurentaaa44472014-09-12 17:41:50 -07003033 } else {
3034 // Check if one effect chain was awaiting for an effect to be created on this
3035 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08003036 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07003037 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07003038 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07003039 thread->addEffectChain_l(chain);
3040 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003041 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003042
Eric Laurent021cf962014-05-13 10:18:14 -07003043 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003044
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003045 // create effect on selected output thread
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003046 bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003047 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003048 &desc, enabled, &lStatus, pinned);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003049 if (handle != 0 && id != NULL) {
3050 *id = handle->id();
3051 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003052 if (handle == 0) {
3053 // remove local strong reference to Client with mClientLock held
3054 Mutex::Autolock _cl(mClientLock);
3055 client.clear();
3056 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003057 }
3058
3059Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07003060 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003061 return handle;
3062}
3063
Glenn Kastend848eb42016-03-08 13:42:11 -08003064status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003065 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07003066{
Steve Block3856b092011-10-20 11:56:00 +01003067 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07003068 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003069 Mutex::Autolock _l(mLock);
3070 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003071 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003072 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003073 }
Eric Laurentde070132010-07-13 04:45:46 -07003074 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
3075 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003076 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003077 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003078 }
Eric Laurentde070132010-07-13 04:45:46 -07003079 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
3080 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003081 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003082 return BAD_VALUE;
3083 }
3084
3085 Mutex::Autolock _dl(dstThread->mLock);
3086 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003087 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003088}
3089
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003090// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08003091status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07003092 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07003093 AudioFlinger::PlaybackThread *dstThread,
3094 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07003095{
Steve Block3856b092011-10-20 11:56:00 +01003096 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003097 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07003098
Eric Laurent59255e42011-07-27 19:49:51 -07003099 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003100 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003101 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003102 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07003103 return INVALID_OPERATION;
3104 }
3105
Eric Laurent4c415062016-06-17 16:14:16 -07003106 // Check whether the destination thread and all effects in the chain are compatible
3107 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07003108 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07003109 " destination thread %p is not compatible with effects in the chain",
3110 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07003111 return INVALID_OPERATION;
3112 }
3113
Eric Laurent39e94f82010-07-28 01:32:47 -07003114 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07003115 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07003116 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07003117 // removed.
3118 srcThread->removeEffectChain_l(chain);
3119
3120 // transfer all effects one by one so that new effect chain is created on new thread with
3121 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07003122 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003123 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07003124 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003125 Vector< sp<EffectModule> > removed;
3126 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07003127 while (effect != 0) {
3128 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003129 removed.add(effect);
3130 status = dstThread->addEffect_l(effect);
3131 if (status != NO_ERROR) {
3132 break;
3133 }
Eric Laurentec35a142011-10-05 17:42:25 -07003134 // removeEffect_l() has stopped the effect if it was active so it must be restarted
3135 if (effect->state() == EffectModule::ACTIVE ||
3136 effect->state() == EffectModule::STOPPING) {
3137 effect->start();
3138 }
Eric Laurent39e94f82010-07-28 01:32:47 -07003139 // if the move request is not received from audio policy manager, the effect must be
3140 // re-registered with the new strategy and output
3141 if (dstChain == 0) {
3142 dstChain = effect->chain().promote();
3143 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003144 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003145 status = NO_INIT;
3146 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07003147 }
3148 strategy = dstChain->strategy();
3149 }
3150 if (reRegister) {
3151 AudioSystem::unregisterEffect(effect->id());
3152 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07003153 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07003154 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07003155 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07003156 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003157 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07003158 }
Eric Laurentde070132010-07-13 04:45:46 -07003159 effect = chain->getEffectFromId_l(0);
3160 }
3161
Eric Laurent5baf2af2013-09-12 17:37:00 -07003162 if (status != NO_ERROR) {
3163 for (size_t i = 0; i < removed.size(); i++) {
3164 srcThread->addEffect_l(removed[i]);
3165 if (dstChain != 0 && reRegister) {
3166 AudioSystem::unregisterEffect(removed[i]->id());
3167 AudioSystem::registerEffect(&removed[i]->desc(),
3168 srcThread->id(),
3169 strategy,
3170 sessionId,
3171 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003172 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003173 }
3174 }
3175 }
3176
3177 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003178}
3179
Eric Laurent5baf2af2013-09-12 17:37:00 -07003180bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07003181{
3182 if (mGlobalEffectEnableTime != 0 &&
3183 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
3184 return true;
3185 }
3186
3187 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3188 sp<EffectChain> ec =
3189 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003190 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07003191 return true;
3192 }
3193 }
3194 return false;
3195}
3196
Eric Laurent5baf2af2013-09-12 17:37:00 -07003197void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07003198{
3199 Mutex::Autolock _l(mLock);
3200
3201 mGlobalEffectEnableTime = systemTime();
3202
3203 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3204 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
3205 if (t->mType == ThreadBase::OFFLOAD) {
3206 t->invalidateTracks(AUDIO_STREAM_MUSIC);
3207 }
3208 }
3209
3210}
3211
Eric Laurentaaa44472014-09-12 17:41:50 -07003212status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
3213{
Glenn Kastend848eb42016-03-08 13:42:11 -08003214 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003215 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003216 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003217 if (index >= 0) {
3218 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
3219 return ALREADY_EXISTS;
3220 }
3221 mOrphanEffectChains.add(session, chain);
3222 return NO_ERROR;
3223}
3224
3225sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
3226{
3227 sp<EffectChain> chain;
3228 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003229 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003230 if (index >= 0) {
3231 chain = mOrphanEffectChains.valueAt(index);
3232 mOrphanEffectChains.removeItemsAt(index);
3233 }
3234 return chain;
3235}
3236
3237bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
3238{
3239 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08003240 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003241 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003242 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003243 if (index >= 0) {
3244 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003245 if (chain->removeEffect_l(effect, true) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003246 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003247 mOrphanEffectChains.removeItemsAt(index);
3248 }
3249 return true;
3250 }
3251 return false;
3252}
3253
3254
Glenn Kastenda6ef132013-01-10 12:31:01 -08003255struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003256#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
3257 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003258};
3259
3260int comparEntry(const void *p1, const void *p2)
3261{
Glenn Kasten2f55e762015-03-05 16:05:08 -08003262 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003263}
3264
Glenn Kasten46909e72013-02-26 09:20:22 -08003265#ifdef TEE_SINK
Glenn Kasten5b2191a2016-08-19 11:44:47 -07003266void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id, char suffix)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003267{
Eric Laurent81784c32012-11-19 14:55:58 -08003268 NBAIO_Source *teeSource = source.get();
3269 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003270 // .wav rotation
3271 // There is a benign race condition if 2 threads call this simultaneously.
3272 // They would both traverse the directory, but the result would simply be
3273 // failures at unlink() which are ignored. It's also unlikely since
3274 // normally dumpsys is only done by bugreport or from the command line.
3275 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08003276 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08003277 size_t teePathLen = strlen(teePath);
3278 DIR *dir = opendir(teePath);
3279 teePath[teePathLen++] = '/';
3280 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003281#define TEE_MAX_SORT 20 // number of entries to sort
3282#define TEE_MAX_KEEP 10 // number of entries to keep
3283 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003284 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08003285 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003286 struct dirent de;
3287 struct dirent *result = NULL;
3288 int rc = readdir_r(dir, &de, &result);
3289 if (rc != 0) {
3290 ALOGW("readdir_r failed %d", rc);
3291 break;
3292 }
3293 if (result == NULL) {
3294 break;
3295 }
3296 if (result != &de) {
3297 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
3298 break;
3299 }
3300 // ignore non .wav file entries
3301 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003302 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08003303 strcmp(&de.d_name[nameLen - 4], ".wav")) {
3304 continue;
3305 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08003306 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003307 }
3308 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003309 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003310 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003311 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
3312 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003313 (void) unlink(teePath);
3314 }
3315 }
3316 } else {
3317 if (fd >= 0) {
Glenn Kastenfbd87e82016-07-18 15:45:56 -07003318 dprintf(fd, "unable to rotate tees in %.*s: %s\n", (int) teePathLen, teePath,
Glenn Kasten9a003992016-02-23 15:24:34 -08003319 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003320 }
3321 }
Eric Laurent81784c32012-11-19 14:55:58 -08003322 char teeTime[16];
3323 struct timeval tv;
3324 gettimeofday(&tv, NULL);
3325 struct tm tm;
3326 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003327 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
Glenn Kasten5b2191a2016-08-19 11:44:47 -07003328 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d_%c.wav", teeTime, id,
3329 suffix);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003330 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3331 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003332 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003333 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003334 char wavHeader[44];
3335 memcpy(wavHeader,
3336 "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",
3337 sizeof(wavHeader));
3338 NBAIO_Format format = teeSource->format();
3339 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003340 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003341 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003342 wavHeader[22] = channelCount; // number of channels
3343 wavHeader[24] = sampleRate; // sample rate
3344 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003345 wavHeader[32] = frameSize; // block alignment
3346 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003347 write(teeFd, wavHeader, sizeof(wavHeader));
3348 size_t total = 0;
3349 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003350#define TEE_SINK_READ 1024 // frames per I/O operation
3351 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003352 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003353 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003354 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003355 bool wasFirstRead = firstRead;
3356 firstRead = false;
3357 if (actual <= 0) {
3358 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3359 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003360 }
Eric Laurent81784c32012-11-19 14:55:58 -08003361 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003362 }
Eric Laurent81784c32012-11-19 14:55:58 -08003363 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003364 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003365 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003366 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003367 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003368 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003369 uint32_t temp = 44 + total * frameSize - 8;
3370 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003371 write(teeFd, &temp, sizeof(temp));
3372 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003373 temp = total * frameSize;
3374 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003375 write(teeFd, &temp, sizeof(temp));
3376 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003377 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003378 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003379 }
Eric Laurent59255e42011-07-27 19:49:51 -07003380 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003381 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003382 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003383 }
Eric Laurent59255e42011-07-27 19:49:51 -07003384 }
3385 }
3386}
Glenn Kasten46909e72013-02-26 09:20:22 -08003387#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003388
Mathias Agopian65ab4712010-07-14 17:59:35 -07003389// ----------------------------------------------------------------------------
3390
3391status_t AudioFlinger::onTransact(
3392 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3393{
3394 return BnAudioFlinger::onTransact(code, data, reply, flags);
3395}
3396
Glenn Kasten63238ef2015-03-02 15:50:29 -08003397} // namespace android