blob: 0d6d5ee6abe42eb3475415c0f07a83b73504e19e [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,
Eric Laurenta54f1282017-07-01 19:39:32 -0700263 const AudioClient& client,
Eric Laurentfc235202016-12-20 18:48:17 -0800264 audio_port_handle_t *deviceId,
265 const sp<MmapStreamCallback>& callback,
Eric Laurenta54f1282017-07-01 19:39:32 -0700266 sp<MmapStreamInterface>& interface,
267 audio_port_handle_t *handle)
Eric Laurentfc235202016-12-20 18:48:17 -0800268{
269 sp<AudioFlinger> af;
270 {
271 Mutex::Autolock _l(gLock);
272 af = gAudioFlinger.promote();
273 }
274 status_t ret = NO_INIT;
275 if (af != 0) {
276 ret = af->openMmapStream(
Eric Laurenta54f1282017-07-01 19:39:32 -0700277 direction, attr, config, client, deviceId, callback, interface, handle);
Eric Laurentfc235202016-12-20 18:48:17 -0800278 }
279 return ret;
280}
281
Eric Laurent6acd1d42017-01-04 14:23:29 -0800282status_t AudioFlinger::openMmapStream(MmapStreamInterface::stream_direction_t direction,
283 const audio_attributes_t *attr,
284 audio_config_base_t *config,
Eric Laurenta54f1282017-07-01 19:39:32 -0700285 const AudioClient& client,
Eric Laurent6acd1d42017-01-04 14:23:29 -0800286 audio_port_handle_t *deviceId,
287 const sp<MmapStreamCallback>& callback,
Eric Laurenta54f1282017-07-01 19:39:32 -0700288 sp<MmapStreamInterface>& interface,
289 audio_port_handle_t *handle)
Eric Laurentfc235202016-12-20 18:48:17 -0800290{
291 status_t ret = initCheck();
292 if (ret != NO_ERROR) {
293 return ret;
294 }
295
Eric Laurent6acd1d42017-01-04 14:23:29 -0800296 audio_session_t sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
297 audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT;
Eric Laurenta54f1282017-07-01 19:39:32 -0700298 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800299 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
300 if (direction == MmapStreamInterface::DIRECTION_OUTPUT) {
301 audio_config_t fullConfig = AUDIO_CONFIG_INITIALIZER;
302 fullConfig.sample_rate = config->sample_rate;
303 fullConfig.channel_mask = config->channel_mask;
304 fullConfig.format = config->format;
305 ret = AudioSystem::getOutputForAttr(attr, &io,
306 sessionId,
307 &streamType, client.clientUid,
308 &fullConfig,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800309 (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ |
310 AUDIO_OUTPUT_FLAG_DIRECT),
Eric Laurent9ae8c592017-06-22 17:17:09 -0700311 deviceId, &portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800312 } else {
313 ret = AudioSystem::getInputForAttr(attr, &io,
314 sessionId,
315 client.clientPid,
316 client.clientUid,
317 config,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700318 AUDIO_INPUT_FLAG_MMAP_NOIRQ, deviceId, &portId);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800319 }
320 if (ret != NO_ERROR) {
321 return ret;
322 }
323
324 // at this stage, a MmapThread was created when openOutput() or openInput() was called by
325 // audio policy manager and we can retrieve it
326 sp<MmapThread> thread = mMmapThreads.valueFor(io);
327 if (thread != 0) {
328 interface = new MmapThreadHandle(thread);
329 thread->configure(attr, streamType, sessionId, callback, portId);
Eric Laurenta54f1282017-07-01 19:39:32 -0700330 *handle = portId;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800331 } else {
332 ret = NO_INIT;
333 }
334
335 ALOGV("%s done status %d portId %d", __FUNCTION__, ret, portId);
336
Eric Laurentfc235202016-12-20 18:48:17 -0800337 return ret;
338}
339
Eric Laurenta4c5a552012-03-29 10:12:40 -0700340static const char * const audio_interfaces[] = {
341 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
342 AUDIO_HARDWARE_MODULE_ID_A2DP,
343 AUDIO_HARDWARE_MODULE_ID_USB,
344};
Eric Laurenta4c5a552012-03-29 10:12:40 -0700345
Phil Burk062e67a2015-02-11 13:40:50 -0800346AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700347 audio_module_handle_t module,
348 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700349{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700350 // if module is 0, the request comes from an old policy manager and we should load
351 // well known modules
352 if (module == 0) {
353 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
Mikhail Naganovbf493082017-04-17 17:37:12 -0700354 for (size_t i = 0; i < arraysize(audio_interfaces); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700355 loadHwModule_l(audio_interfaces[i]);
356 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700357 // then try to find a module supporting the requested device.
358 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
359 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700360 sp<DeviceHalInterface> dev = audioHwDevice->hwDevice();
361 uint32_t supportedDevices;
362 if (dev->getSupportedDevices(&supportedDevices) == OK &&
363 (supportedDevices & devices) == devices) {
Eric Laurentf1c04f92012-08-28 14:26:53 -0700364 return audioHwDevice;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700365 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700366 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700367 } else {
368 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700369 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
370 if (audioHwDevice != NULL) {
371 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700372 }
373 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700374
Dima Zavin799a70e2011-04-18 16:57:27 -0700375 return NULL;
376}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377
Glenn Kasten0f11b512014-01-31 16:18:54 -0800378void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379{
380 const size_t SIZE = 256;
381 char buffer[SIZE];
382 String8 result;
383
384 result.append("Clients:\n");
385 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800386 sp<Client> client = mClients.valueAt(i).promote();
387 if (client != 0) {
388 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
389 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390 }
391 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700392
Eric Laurentd1b28d42013-09-18 18:47:13 -0700393 result.append("Notification Clients:\n");
394 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
395 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
396 result.append(buffer);
397 }
398
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700399 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800400 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700401 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
402 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800403 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700404 result.append(buffer);
405 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700406 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407}
408
409
Glenn Kasten0f11b512014-01-31 16:18:54 -0800410void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411{
412 const size_t SIZE = 256;
413 char buffer[SIZE];
414 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800415 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416
John Grossman4ff14ba2012-02-08 16:37:41 -0800417 snprintf(buffer, SIZE, "Hardware status: %d\n"
418 "Standby Time mSec: %u\n",
419 hardwareStatus,
420 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421 result.append(buffer);
422 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700423}
424
Glenn Kasten0f11b512014-01-31 16:18:54 -0800425void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426{
427 const size_t SIZE = 256;
428 char buffer[SIZE];
429 String8 result;
430 snprintf(buffer, SIZE, "Permission Denial: "
431 "can't dump AudioFlinger from pid=%d, uid=%d\n",
432 IPCThreadState::self()->getCallingPid(),
433 IPCThreadState::self()->getCallingUid());
434 result.append(buffer);
435 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436}
437
Eric Laurent81784c32012-11-19 14:55:58 -0800438bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439{
440 bool locked = false;
441 for (int i = 0; i < kDumpLockRetries; ++i) {
442 if (mutex.tryLock() == NO_ERROR) {
443 locked = true;
444 break;
445 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800446 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700447 }
448 return locked;
449}
450
451status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
452{
Glenn Kasten44deb052012-02-05 18:09:08 -0800453 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700454 dumpPermissionDenial(fd, args);
455 } else {
456 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800457 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700458 if (!hardwareLocked) {
459 String8 result(kHardwareLockedString);
460 write(fd, result.string(), result.size());
461 } else {
462 mHardwareLock.unlock();
463 }
464
Eric Laurent81784c32012-11-19 14:55:58 -0800465 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466
467 // failed to lock - AudioFlinger is probably deadlocked
468 if (!locked) {
469 String8 result(kDeadlockedString);
470 write(fd, result.string(), result.size());
471 }
472
Eric Laurent021cf962014-05-13 10:18:14 -0700473 bool clientLocked = dumpTryLock(mClientLock);
474 if (!clientLocked) {
475 String8 result(kClientLockedString);
476 write(fd, result.string(), result.size());
477 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700478
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700479 if (mEffectsFactoryHal != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700480 mEffectsFactoryHal->dumpEffects(fd);
481 } else {
482 String8 result(kNoEffectsFactory);
483 write(fd, result.string(), result.size());
484 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700485
Mathias Agopian65ab4712010-07-14 17:59:35 -0700486 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700487 if (clientLocked) {
488 mClientLock.unlock();
489 }
490
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491 dumpInternals(fd, args);
492
493 // dump playback threads
494 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
495 mPlaybackThreads.valueAt(i)->dump(fd, args);
496 }
497
498 // dump record threads
499 for (size_t i = 0; i < mRecordThreads.size(); i++) {
500 mRecordThreads.valueAt(i)->dump(fd, args);
501 }
502
Eric Laurent6acd1d42017-01-04 14:23:29 -0800503 // dump mmap threads
504 for (size_t i = 0; i < mMmapThreads.size(); i++) {
505 mMmapThreads.valueAt(i)->dump(fd, args);
506 }
507
Eric Laurentaaa44472014-09-12 17:41:50 -0700508 // dump orphan effect chains
509 if (mOrphanEffectChains.size() != 0) {
510 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
511 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
512 mOrphanEffectChains.valueAt(i)->dump(fd, args);
513 }
514 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700515 // dump all hardware devs
516 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700517 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
518 dev->dump(fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700520
Glenn Kasten46909e72013-02-26 09:20:22 -0800521#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700522 // dump the serially shared record tee sink
523 if (mRecordTeeSource != 0) {
Glenn Kasten5b2191a2016-08-19 11:44:47 -0700524 dumpTee(fd, mRecordTeeSource, AUDIO_IO_HANDLE_NONE, 'C');
Glenn Kastend06785b2012-09-30 12:29:28 -0700525 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800526#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700527
rago3bd1c872016-09-26 12:58:14 -0700528 BUFLOG_RESET;
529
Glenn Kastend65d73c2012-06-22 17:21:07 -0700530 if (locked) {
531 mLock.unlock();
532 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800533
534 // append a copy of media.log here by forwarding fd to it, but don't attempt
535 // to lookup the service if it's not running, as it will block for a second
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700536 if (sMediaLogServiceAsBinder != 0) {
537 dprintf(fd, "\nmedia.log:\n");
538 Vector<String16> args;
539 sMediaLogServiceAsBinder->dump(fd, args);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800540 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700541
542 // check for optional arguments
Andy Hung07b745e2016-05-23 16:21:07 -0700543 bool dumpMem = false;
Andy Hung35fec5f2016-04-13 14:21:48 -0700544 bool unreachableMemory = false;
545 for (const auto &arg : args) {
Andy Hung07b745e2016-05-23 16:21:07 -0700546 if (arg == String16("-m")) {
547 dumpMem = true;
548 } else if (arg == String16("--unreachable")) {
Andy Hung35fec5f2016-04-13 14:21:48 -0700549 unreachableMemory = true;
550 }
551 }
552
Andy Hung07b745e2016-05-23 16:21:07 -0700553 if (dumpMem) {
554 dprintf(fd, "\nDumping memory:\n");
555 std::string s = dumpMemoryAddresses(100 /* limit */);
556 write(fd, s.c_str(), s.size());
557 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700558 if (unreachableMemory) {
559 dprintf(fd, "\nDumping unreachable memory:\n");
560 // TODO - should limit be an argument parameter?
Andy Hung07b745e2016-05-23 16:21:07 -0700561 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
Andy Hung35fec5f2016-04-13 14:21:48 -0700562 write(fd, s.c_str(), s.size());
563 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700564 }
565 return NO_ERROR;
566}
567
Eric Laurent021cf962014-05-13 10:18:14 -0700568sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800569{
Eric Laurent021cf962014-05-13 10:18:14 -0700570 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800571 // If pid is already in the mClients wp<> map, then use that entry
572 // (for which promote() is always != 0), otherwise create a new entry and Client.
573 sp<Client> client = mClients.valueFor(pid).promote();
574 if (client == 0) {
575 client = new Client(this, pid);
576 mClients.add(pid, client);
577 }
578
579 return client;
580}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581
Glenn Kasten9e58b552013-01-18 15:09:48 -0800582sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
583{
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700584 // If there is no memory allocated for logs, return a dummy writer that does nothing.
585 // Similarly if we can't contact the media.log service, also return a dummy writer.
586 if (mLogMemoryDealer == 0 || sMediaLogService == 0) {
Glenn Kasten9e58b552013-01-18 15:09:48 -0800587 return new NBLog::Writer();
588 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700589 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
590 // If allocation fails, consult the vector of previously unregistered writers
591 // and garbage-collect one or more them until an allocation succeeds
592 if (shared == 0) {
593 Mutex::Autolock _l(mUnregisteredWritersLock);
594 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
595 {
596 // Pick the oldest stale writer to garbage-collect
597 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
598 mUnregisteredWriters.removeAt(0);
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700599 sMediaLogService->unregisterWriter(iMemory);
Glenn Kasten481fb672013-09-30 14:39:28 -0700600 // Now the media.log remote reference to IMemory is gone. When our last local
601 // reference to IMemory also drops to zero at end of this block,
602 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
603 }
604 // Re-attempt the allocation
605 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
606 if (shared != 0) {
607 goto success;
608 }
609 }
610 // Even after garbage-collecting all old writers, there is still not enough memory,
611 // so return a dummy writer
612 return new NBLog::Writer();
613 }
614success:
Glenn Kasten535e1612016-12-05 12:19:36 -0800615 NBLog::Shared *sharedRawPtr = (NBLog::Shared *) shared->pointer();
616 new((void *) sharedRawPtr) NBLog::Shared(); // placement new here, but the corresponding
617 // explicit destructor not needed since it is POD
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700618 sMediaLogService->registerWriter(shared, size, name);
Glenn Kasten535e1612016-12-05 12:19:36 -0800619 return new NBLog::Writer(shared, size);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800620}
621
622void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
623{
Glenn Kasten685ef092013-02-04 08:15:34 -0800624 if (writer == 0) {
625 return;
626 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800627 sp<IMemory> iMemory(writer->getIMemory());
628 if (iMemory == 0) {
629 return;
630 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700631 // Rather than removing the writer immediately, append it to a queue of old writers to
632 // be garbage-collected later. This allows us to continue to view old logs for a while.
633 Mutex::Autolock _l(mUnregisteredWritersLock);
634 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800635}
636
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637// IAudioFlinger interface
638
639
640sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800641 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800643 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700644 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800645 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -0700646 audio_output_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800648 audio_io_handle_t output,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700649 pid_t pid,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800650 pid_t tid,
Glenn Kastend848eb42016-03-08 13:42:11 -0800651 audio_session_t *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800652 int clientUid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800653 status_t *status,
654 audio_port_handle_t portId)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655{
656 sp<PlaybackThread::Track> track;
657 sp<TrackHandle> trackHandle;
658 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700659 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -0800660 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700661
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700662 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
663 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
664 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
665 ALOGW_IF(pid != -1 && pid != callingPid,
666 "%s uid %d pid %d tried to pass itself off as pid %d",
667 __func__, callingUid, callingPid, pid);
668 pid = callingPid;
669 }
670
Glenn Kasten263709e2012-01-06 08:40:01 -0800671 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
672 // but if someone uses binder directly they could bypass that and cause us to crash
673 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000674 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675 lStatus = BAD_VALUE;
676 goto Exit;
677 }
678
Glenn Kasten53b5d092014-02-05 10:00:23 -0800679 // further sample rate checks are performed by createTrack_l() depending on the thread type
680 if (sampleRate == 0) {
681 ALOGE("createTrack() invalid sample rate %u", sampleRate);
682 lStatus = BAD_VALUE;
683 goto Exit;
684 }
685
686 // further channel mask checks are performed by createTrack_l() depending on the thread type
687 if (!audio_is_output_channel(channelMask)) {
688 ALOGE("createTrack() invalid channel mask %#x", channelMask);
689 lStatus = BAD_VALUE;
690 goto Exit;
691 }
692
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700693 // further format checks are performed by createTrack_l() depending on the thread type
694 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800695 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700696 lStatus = BAD_VALUE;
697 goto Exit;
698 }
699
Glenn Kasten663c2242013-09-24 11:52:37 -0700700 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
701 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
702 lStatus = BAD_VALUE;
703 goto Exit;
704 }
705
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706 {
707 Mutex::Autolock _l(mLock);
708 PlaybackThread *thread = checkPlaybackThread_l(output);
709 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800710 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 lStatus = BAD_VALUE;
712 goto Exit;
713 }
714
Eric Laurent021cf962014-05-13 10:18:14 -0700715 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716
Glenn Kastene848bd92014-03-13 15:00:32 -0700717 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700718 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800719 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
720 ALOGE("createTrack() invalid session ID %d", *sessionId);
721 lStatus = BAD_VALUE;
722 goto Exit;
723 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700724 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700725 // check if an effect chain with the same session ID is present on another
726 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700727 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700728 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
729 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700730 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent4c415062016-06-17 16:14:16 -0700731 if (sessions & ThreadBase::EFFECT_SESSION) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700732 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700733 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700734 }
Eric Laurentde070132010-07-13 04:45:46 -0700735 }
736 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700737 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700738 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800739 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740 if (sessionId != NULL) {
741 *sessionId = lSessionId;
742 }
743 }
Steve Block3856b092011-10-20 11:56:00 +0100744 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700745
746 track = thread->createTrack_l(client, streamType, sampleRate, format,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800747 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid,
748 clientUid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800749 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700750 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700751
752 // move effect chain to this output thread if an effect on same session was waiting
753 // for a track to be created
754 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700755 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700756 Mutex::Autolock _dl(thread->mLock);
757 Mutex::Autolock _sl(effectThread->mLock);
758 moveEffectChain_l(lSessionId, effectThread, thread, true);
759 }
Eric Laurenta011e352012-03-29 15:51:43 -0700760
761 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700762 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700763 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
764 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700765 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700766 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700767 } else {
768 mPendingSyncEvents[i]->cancel();
769 }
Eric Laurenta011e352012-03-29 15:51:43 -0700770 mPendingSyncEvents.removeAt(i);
771 i--;
772 }
773 }
774 }
Glenn Kastene198c362013-08-13 09:13:36 -0700775
Glenn Kastend848eb42016-03-08 13:42:11 -0800776 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777 }
Glenn Kastene198c362013-08-13 09:13:36 -0700778
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700779 if (lStatus != NO_ERROR) {
780 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700781 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700782 // Don't hold mClientLock when releasing the reference on the track as the
783 // destructor will acquire it.
784 {
785 Mutex::Autolock _cl(mClientLock);
786 client.clear();
787 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700788 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700789 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700790 }
791
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700792 // return handle to client
793 trackHandle = new TrackHandle(track);
794
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700796 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797 return trackHandle;
798}
799
Glenn Kasten2c073da2016-02-26 09:14:08 -0800800uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801{
802 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800803 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800805 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700806 return 0;
807 }
808 return thread->sampleRate();
809}
810
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800811audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812{
813 Mutex::Autolock _l(mLock);
814 PlaybackThread *thread = checkPlaybackThread_l(output);
815 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000816 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800817 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 }
819 return thread->format();
820}
821
Glenn Kasten2c073da2016-02-26 09:14:08 -0800822size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823{
824 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800825 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800827 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828 return 0;
829 }
Glenn Kasten58912562012-04-03 10:45:00 -0700830 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
831 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832 return thread->frameCount();
833}
834
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700835size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
836{
837 Mutex::Autolock _l(mLock);
838 ThreadBase *thread = checkThread_l(ioHandle);
839 if (thread == NULL) {
840 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
841 return 0;
842 }
843 return thread->frameCountHAL();
844}
845
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800846uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847{
848 Mutex::Autolock _l(mLock);
849 PlaybackThread *thread = checkPlaybackThread_l(output);
850 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800851 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852 return 0;
853 }
854 return thread->latency();
855}
856
857status_t AudioFlinger::setMasterVolume(float value)
858{
Eric Laurenta1884f92011-08-23 08:25:03 -0700859 status_t ret = initCheck();
860 if (ret != NO_ERROR) {
861 return ret;
862 }
863
Mathias Agopian65ab4712010-07-14 17:59:35 -0700864 // check calling permissions
865 if (!settingsAllowed()) {
866 return PERMISSION_DENIED;
867 }
868
Eric Laurenta4c5a552012-03-29 10:12:40 -0700869 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700870 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700871
John Grossmanee578c02012-07-23 17:05:46 -0700872 // Set master volume in the HALs which support it.
873 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
874 AutoMutex lock(mHardwareLock);
875 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800876
John Grossmanee578c02012-07-23 17:05:46 -0700877 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
878 if (dev->canSetMasterVolume()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700879 dev->hwDevice()->setMasterVolume(value);
Eric Laurent93575202011-01-18 18:39:02 -0800880 }
John Grossmanee578c02012-07-23 17:05:46 -0700881 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700883
John Grossmanee578c02012-07-23 17:05:46 -0700884 // Now set the master volume in each playback thread. Playback threads
885 // assigned to HALs which do not have master volume support will apply
886 // master volume during the mix operation. Threads with HALs which do
887 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700888 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
889 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
890 continue;
891 }
John Grossmanee578c02012-07-23 17:05:46 -0700892 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700893 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700894
895 return NO_ERROR;
896}
897
Glenn Kastenf78aee72012-01-04 11:00:47 -0800898status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899{
Eric Laurenta1884f92011-08-23 08:25:03 -0700900 status_t ret = initCheck();
901 if (ret != NO_ERROR) {
902 return ret;
903 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700904
905 // check calling permissions
906 if (!settingsAllowed()) {
907 return PERMISSION_DENIED;
908 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800909 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000910 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 return BAD_VALUE;
912 }
913
914 { // scope for the lock
915 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700916 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700918 ret = dev->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919 mHardwareStatus = AUDIO_HW_IDLE;
920 }
921
922 if (NO_ERROR == ret) {
923 Mutex::Autolock _l(mLock);
924 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800925 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700926 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927 }
928
929 return ret;
930}
931
932status_t AudioFlinger::setMicMute(bool state)
933{
Eric Laurenta1884f92011-08-23 08:25:03 -0700934 status_t ret = initCheck();
935 if (ret != NO_ERROR) {
936 return ret;
937 }
938
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939 // check calling permissions
940 if (!settingsAllowed()) {
941 return PERMISSION_DENIED;
942 }
943
944 AutoMutex lock(mHardwareLock);
945 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700946 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700947 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
948 status_t result = dev->setMicMute(state);
Eric Laurent2f035f52014-09-14 12:11:52 -0700949 if (result != NO_ERROR) {
950 ret = result;
951 }
952 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953 mHardwareStatus = AUDIO_HW_IDLE;
954 return ret;
955}
956
957bool AudioFlinger::getMicMute() const
958{
Eric Laurenta1884f92011-08-23 08:25:03 -0700959 status_t ret = initCheck();
960 if (ret != NO_ERROR) {
961 return false;
962 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800963 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700964 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800965 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700966 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800967 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700968 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
969 status_t result = dev->getMicMute(&state);
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800970 if (result == NO_ERROR) {
971 mute = mute && state;
972 }
973 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700974 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800975
976 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977}
978
979status_t AudioFlinger::setMasterMute(bool muted)
980{
John Grossmand8f178d2012-07-20 14:51:35 -0700981 status_t ret = initCheck();
982 if (ret != NO_ERROR) {
983 return ret;
984 }
985
Mathias Agopian65ab4712010-07-14 17:59:35 -0700986 // check calling permissions
987 if (!settingsAllowed()) {
988 return PERMISSION_DENIED;
989 }
990
John Grossmanee578c02012-07-23 17:05:46 -0700991 Mutex::Autolock _l(mLock);
992 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700993
John Grossmanee578c02012-07-23 17:05:46 -0700994 // Set master mute in the HALs which support it.
995 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
996 AutoMutex lock(mHardwareLock);
997 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700998
John Grossmanee578c02012-07-23 17:05:46 -0700999 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1000 if (dev->canSetMasterMute()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001001 dev->hwDevice()->setMasterMute(muted);
John Grossmand8f178d2012-07-20 14:51:35 -07001002 }
John Grossmanee578c02012-07-23 17:05:46 -07001003 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -07001004 }
1005
John Grossmanee578c02012-07-23 17:05:46 -07001006 // Now set the master mute in each playback thread. Playback threads
1007 // assigned to HALs which do not have master mute support will apply master
1008 // mute during the mix operation. Threads with HALs which do support master
1009 // mute will simply ignore the setting.
Eric Laurent6acd1d42017-01-04 14:23:29 -08001010 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1011 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1012 volumeInterfaces[i]->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -07001013 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001014
1015 return NO_ERROR;
1016}
1017
1018float AudioFlinger::masterVolume() const
1019{
Glenn Kasten98067102011-12-13 11:47:54 -08001020 Mutex::Autolock _l(mLock);
1021 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022}
1023
1024bool AudioFlinger::masterMute() const
1025{
Glenn Kasten98067102011-12-13 11:47:54 -08001026 Mutex::Autolock _l(mLock);
1027 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028}
1029
John Grossman4ff14ba2012-02-08 16:37:41 -08001030float AudioFlinger::masterVolume_l() const
1031{
John Grossman4ff14ba2012-02-08 16:37:41 -08001032 return mMasterVolume;
1033}
1034
John Grossmand8f178d2012-07-20 14:51:35 -07001035bool AudioFlinger::masterMute_l() const
1036{
John Grossmanee578c02012-07-23 17:05:46 -07001037 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -07001038}
1039
Eric Laurent223fd5c2014-11-11 13:43:36 -08001040status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
1041{
1042 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001043 ALOGW("checkStreamType() invalid stream %d", stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001044 return BAD_VALUE;
1045 }
1046 pid_t caller = IPCThreadState::self()->getCallingPid();
1047 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001048 ALOGW("checkStreamType() pid %d cannot use internal stream type %d", caller, stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001049 return PERMISSION_DENIED;
1050 }
1051
1052 return NO_ERROR;
1053}
1054
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001055status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
1056 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001057{
1058 // check calling permissions
1059 if (!settingsAllowed()) {
1060 return PERMISSION_DENIED;
1061 }
1062
Eric Laurent223fd5c2014-11-11 13:43:36 -08001063 status_t status = checkStreamType(stream);
1064 if (status != NO_ERROR) {
1065 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001066 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001067 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068
1069 AutoMutex lock(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001070 Vector<VolumeInterface *> volumeInterfaces;
Glenn Kasten142f5192014-03-25 17:44:59 -07001071 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001072 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1073 if (volumeInterface == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074 return BAD_VALUE;
1075 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001076 volumeInterfaces.add(volumeInterface);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077 }
1078
1079 mStreamTypes[stream].volume = value;
1080
Eric Laurent6acd1d42017-01-04 14:23:29 -08001081 if (volumeInterfaces.size() == 0) {
1082 volumeInterfaces = getAllVolumeInterfaces_l();
1083 }
1084 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1085 volumeInterfaces[i]->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001086 }
1087
1088 return NO_ERROR;
1089}
1090
Glenn Kastenfff6d712012-01-12 16:38:12 -08001091status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001092{
1093 // check calling permissions
1094 if (!settingsAllowed()) {
1095 return PERMISSION_DENIED;
1096 }
1097
Eric Laurent223fd5c2014-11-11 13:43:36 -08001098 status_t status = checkStreamType(stream);
1099 if (status != NO_ERROR) {
1100 return status;
1101 }
1102 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1103
1104 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001105 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106 return BAD_VALUE;
1107 }
1108
Eric Laurent93575202011-01-18 18:39:02 -08001109 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110 mStreamTypes[stream].mute = muted;
Eric Laurent6acd1d42017-01-04 14:23:29 -08001111 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1112 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1113 volumeInterfaces[i]->setStreamMute(stream, muted);
1114 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115
1116 return NO_ERROR;
1117}
1118
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001119float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001120{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001121 status_t status = checkStreamType(stream);
1122 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123 return 0.0f;
1124 }
1125
1126 AutoMutex lock(mLock);
1127 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001128 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001129 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1130 if (volumeInterface != NULL) {
1131 volume = volumeInterface->streamVolume(stream);
1132 } else {
1133 volume = 0.0f;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001135 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001136 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137 }
1138
1139 return volume;
1140}
1141
Glenn Kastenfff6d712012-01-12 16:38:12 -08001142bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001144 status_t status = checkStreamType(stream);
1145 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146 return true;
1147 }
1148
Glenn Kasten6637baa2012-01-09 09:40:36 -08001149 AutoMutex lock(mLock);
1150 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151}
1152
Eric Laurent054d9d32015-04-24 08:48:48 -07001153
1154void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1155{
1156 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1157 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1158 }
1159}
1160
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001161status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001163 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1164 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001165
Mathias Agopian65ab4712010-07-14 17:59:35 -07001166 // check calling permissions
1167 if (!settingsAllowed()) {
1168 return PERMISSION_DENIED;
1169 }
1170
Glenn Kasten142f5192014-03-25 17:44:59 -07001171 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1172 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001173 Mutex::Autolock _l(mLock);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001174 // result will remain NO_INIT if no audio device is present
1175 status_t final_result = NO_INIT;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001176 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001177 AutoMutex lock(mHardwareLock);
1178 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1179 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001180 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1181 status_t result = dev->setParameters(keyValuePairs);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001182 // return success if at least one audio device accepts the parameters as not all
1183 // HALs are requested to support all parameters. If no audio device supports the
1184 // requested parameters, the last error is reported.
1185 if (final_result != NO_ERROR) {
1186 final_result = result;
1187 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001188 }
1189 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001190 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001191 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1192 AudioParameter param = AudioParameter(keyValuePairs);
1193 String8 value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001194 if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) {
1195 bool btNrecIsOff = (value == AudioParameter::valueOff);
Eric Laurentbee53372011-08-29 12:42:48 -07001196 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001197 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1198 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001199 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001200 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1201 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001202 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001203 // suspend effects associated with those session IDs
1204 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001205 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001206 thread->setEffectSuspended(FX_IID_AEC,
1207 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001208 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001209 thread->setEffectSuspended(FX_IID_NS,
1210 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001211 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001212 }
1213 }
Eric Laurentbee53372011-08-29 12:42:48 -07001214 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001215 }
1216 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001217 String8 screenState;
1218 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
Mikhail Naganov00260b52016-10-13 12:54:24 -07001219 bool isOff = (screenState == AudioParameter::valueOff);
Eric Laurent81784c32012-11-19 14:55:58 -08001220 if (isOff != (AudioFlinger::mScreenState & 1)) {
1221 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001222 }
1223 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001224 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001225 }
1226
1227 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1228 // and the thread is exited once the lock is released
1229 sp<ThreadBase> thread;
1230 {
1231 Mutex::Autolock _l(mLock);
1232 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001233 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001234 thread = checkRecordThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001235 if (thread == 0) {
1236 thread = checkMmapThread_l(ioHandle);
1237 }
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001238 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001239 // indicate output device change to all input threads for pre processing
1240 AudioParameter param = AudioParameter(keyValuePairs);
1241 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001242 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1243 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001244 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001245 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001246 }
1247 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001248 if (thread != 0) {
1249 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001250 }
1251 return BAD_VALUE;
1252}
1253
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001254String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001256 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1257 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001258
Eric Laurenta4c5a552012-03-29 10:12:40 -07001259 Mutex::Autolock _l(mLock);
1260
Glenn Kasten142f5192014-03-25 17:44:59 -07001261 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001262 String8 out_s8;
1263
Dima Zavin799a70e2011-04-18 16:57:27 -07001264 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001265 String8 s;
1266 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001267 {
1268 AutoMutex lock(mHardwareLock);
1269 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001270 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1271 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001272 mHardwareStatus = AUDIO_HW_IDLE;
1273 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001274 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001275 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001276 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277 }
1278
Eric Laurent6acd1d42017-01-04 14:23:29 -08001279 ThreadBase *thread = (ThreadBase *)checkPlaybackThread_l(ioHandle);
1280 if (thread == NULL) {
1281 thread = (ThreadBase *)checkRecordThread_l(ioHandle);
1282 if (thread == NULL) {
1283 thread = (ThreadBase *)checkMmapThread_l(ioHandle);
1284 if (thread == NULL) {
1285 String8("");
1286 }
1287 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001289 return thread->getParameters(keys);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001290}
1291
Glenn Kastendd8104c2012-07-02 12:42:44 -07001292size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1293 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001294{
Eric Laurenta1884f92011-08-23 08:25:03 -07001295 status_t ret = initCheck();
1296 if (ret != NO_ERROR) {
1297 return 0;
1298 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001299 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001300 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001301 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001302 return 0;
1303 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001304
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001305 AutoMutex lock(mHardwareLock);
1306 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001307 audio_config_t config, proposed;
1308 memset(&proposed, 0, sizeof(proposed));
1309 proposed.sample_rate = sampleRate;
1310 proposed.channel_mask = channelMask;
1311 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001312
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001313 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001314 size_t frames;
1315 for (;;) {
1316 // Note: config is currently a const parameter for get_input_buffer_size()
1317 // but we use a copy from proposed in case config changes from the call.
1318 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001319 status_t result = dev->getInputBufferSize(&config, &frames);
1320 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001321 break; // hal success, config is the result
1322 }
1323 // change one parameter of the configuration each iteration to a more "common" value
1324 // to see if the device will support it.
1325 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1326 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1327 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1328 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1329 } else {
1330 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1331 "format %#x, channelMask 0x%X",
1332 sampleRate, format, channelMask);
1333 break; // retries failed, break out of loop with frames == 0.
1334 }
1335 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001336 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001337 if (frames > 0 && config.sample_rate != sampleRate) {
1338 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1339 }
1340 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001341}
1342
Glenn Kasten5f972c02014-01-13 09:59:31 -08001343uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001344{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001345 Mutex::Autolock _l(mLock);
1346
1347 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1348 if (recordThread != NULL) {
1349 return recordThread->getInputFramesLost();
1350 }
1351 return 0;
1352}
1353
1354status_t AudioFlinger::setVoiceVolume(float value)
1355{
Eric Laurenta1884f92011-08-23 08:25:03 -07001356 status_t ret = initCheck();
1357 if (ret != NO_ERROR) {
1358 return ret;
1359 }
1360
Mathias Agopian65ab4712010-07-14 17:59:35 -07001361 // check calling permissions
1362 if (!settingsAllowed()) {
1363 return PERMISSION_DENIED;
1364 }
1365
1366 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001367 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001368 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001369 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001370 mHardwareStatus = AUDIO_HW_IDLE;
1371
1372 return ret;
1373}
1374
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001375status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001376 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001377{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378 Mutex::Autolock _l(mLock);
1379
1380 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1381 if (playbackThread != NULL) {
1382 return playbackThread->getRenderPosition(halFrames, dspFrames);
1383 }
1384
1385 return BAD_VALUE;
1386}
1387
1388void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1389{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001390 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001391 if (client == 0) {
1392 return;
1393 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001394 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001395 {
1396 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001397 if (mNotificationClients.indexOfKey(pid) < 0) {
1398 sp<NotificationClient> notificationClient = new NotificationClient(this,
1399 client,
1400 pid);
1401 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402
Eric Laurent021cf962014-05-13 10:18:14 -07001403 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404
Marco Nelissen06b46062014-11-14 07:58:25 -08001405 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001406 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001407 }
1408 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409
Eric Laurent021cf962014-05-13 10:18:14 -07001410 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001411 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001412 // the config change is always sent from playback or record threads to avoid deadlock
1413 // with AudioSystem::gLock
1414 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1415 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1416 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001417
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001418 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1419 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001420 }
1421}
1422
1423void AudioFlinger::removeNotificationClient(pid_t pid)
1424{
1425 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001426 {
1427 Mutex::Autolock _cl(mClientLock);
1428 mNotificationClients.removeItem(pid);
1429 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001430
Steve Block3856b092011-10-20 11:56:00 +01001431 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001432 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001433 bool removed = false;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001434 for (size_t i = 0; i < num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001435 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001436 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001437 if (ref->mPid == pid) {
1438 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001439 mAudioSessionRefs.removeAt(i);
1440 delete ref;
1441 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001442 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001443 } else {
1444 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001445 }
1446 }
1447 if (removed) {
1448 purgeStaleEffects_l();
1449 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001450}
1451
Eric Laurent73e26b62015-04-27 16:55:58 -07001452void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001453 const sp<AudioIoDescriptor>& ioDesc,
1454 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001455{
Eric Laurent021cf962014-05-13 10:18:14 -07001456 Mutex::Autolock _l(mClientLock);
1457 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001458 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001459 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1460 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1461 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001462 }
1463}
1464
Eric Laurent021cf962014-05-13 10:18:14 -07001465// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001466void AudioFlinger::removeClient_l(pid_t pid)
1467{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001468 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001469 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001470 mClients.removeItem(pid);
1471}
1472
Eric Laurent717e1282012-06-29 16:36:52 -07001473// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001474sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1475 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001476{
1477 sp<PlaybackThread> thread;
1478
1479 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1480 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1481 ALOG_ASSERT(thread == 0);
1482 thread = mPlaybackThreads.valueAt(i);
1483 }
1484 }
1485
1486 return thread;
1487}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001488
Mathias Agopian65ab4712010-07-14 17:59:35 -07001489
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490
1491// ----------------------------------------------------------------------------
1492
1493AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1494 : RefBase(),
1495 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001496 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001497{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301498 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1499 heapSize *= 1024;
1500 if (!heapSize) {
1501 heapSize = kClientSharedHeapSizeBytes;
1502 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1503 // invalidated tracks
1504 if (!audioFlinger->isLowRamDevice()) {
1505 heapSize *= kClientSharedHeapSizeMultiplier;
1506 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001507 }
1508 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001509}
1510
Eric Laurent021cf962014-05-13 10:18:14 -07001511// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512AudioFlinger::Client::~Client()
1513{
1514 mAudioFlinger->removeClient_l(mPid);
1515}
1516
Glenn Kasten435dbe62012-01-30 10:15:48 -08001517sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001518{
1519 return mMemoryDealer;
1520}
1521
1522// ----------------------------------------------------------------------------
1523
1524AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1525 const sp<IAudioFlingerClient>& client,
1526 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001527 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001528{
1529}
1530
1531AudioFlinger::NotificationClient::~NotificationClient()
1532{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533}
1534
Glenn Kasten0f11b512014-01-31 16:18:54 -08001535void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001536{
1537 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001538 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001539}
1540
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001541// ----------------------------------------------------------------------------
1542AudioFlinger::MediaLogNotifier::MediaLogNotifier()
1543 : mPendingRequests(false) {}
1544
1545
1546void AudioFlinger::MediaLogNotifier::requestMerge() {
1547 AutoMutex _l(mMutex);
1548 mPendingRequests = true;
1549 mCond.signal();
1550}
1551
1552bool AudioFlinger::MediaLogNotifier::threadLoop() {
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001553 // Should already have been checked, but just in case
1554 if (sMediaLogService == 0) {
1555 return false;
1556 }
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001557 // Wait until there are pending requests
1558 {
1559 AutoMutex _l(mMutex);
1560 mPendingRequests = false; // to ignore past requests
1561 while (!mPendingRequests) {
1562 mCond.wait(mMutex);
1563 // TODO may also need an exitPending check
1564 }
1565 mPendingRequests = false;
1566 }
1567 // Execute the actual MediaLogService binder call and ignore extra requests for a while
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001568 sMediaLogService->requestMergeWakeup();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001569 usleep(kPostTriggerSleepPeriod);
1570 return true;
1571}
1572
1573void AudioFlinger::requestLogMerge() {
1574 mMediaLogNotifier->requestMerge();
1575}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001576
1577// ----------------------------------------------------------------------------
1578
1579sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001580 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001581 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001582 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001583 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001584 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001585 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -07001586 audio_input_flags_t *flags,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001587 pid_t pid,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001588 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001589 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001590 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001591 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001592 sp<IMemory>& cblk,
1593 sp<IMemory>& buffers,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001594 status_t *status,
1595 audio_port_handle_t portId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001596{
1597 sp<RecordThread::RecordTrack> recordTrack;
1598 sp<RecordHandle> recordHandle;
1599 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001601 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602
Glenn Kastend776ac62014-05-07 09:16:09 -07001603 cblk.clear();
1604 buffers.clear();
1605
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001606 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001607 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1608 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001609 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001610 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1611 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001612 updatePid = true;
1613 }
1614
1615 if (updatePid) {
1616 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
1617 ALOGW_IF(pid != -1 && pid != callingPid,
1618 "%s uid %d pid %d tried to pass itself off as pid %d",
1619 __func__, callingUid, callingPid, pid);
1620 pid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001621 }
1622
Mathias Agopian65ab4712010-07-14 17:59:35 -07001623 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001624 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001625 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001626 lStatus = PERMISSION_DENIED;
1627 goto Exit;
1628 }
1629
Glenn Kasten53b5d092014-02-05 10:00:23 -08001630 // further sample rate checks are performed by createRecordTrack_l()
1631 if (sampleRate == 0) {
1632 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1633 lStatus = BAD_VALUE;
1634 goto Exit;
1635 }
1636
Andy Hung6770c6f2015-04-07 13:43:36 -07001637 // we don't yet support anything other than linear PCM
1638 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001639 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001640 lStatus = BAD_VALUE;
1641 goto Exit;
1642 }
1643
Glenn Kasten53b5d092014-02-05 10:00:23 -08001644 // further channel mask checks are performed by createRecordTrack_l()
1645 if (!audio_is_input_channel(channelMask)) {
1646 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1647 lStatus = BAD_VALUE;
1648 goto Exit;
1649 }
1650
Glenn Kasten05997e22014-03-13 15:08:33 -07001651 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001652 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001653 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001654 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001655 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001656 lStatus = BAD_VALUE;
1657 goto Exit;
1658 }
1659
Eric Laurent021cf962014-05-13 10:18:14 -07001660 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001661
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001662 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001663 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1664 lStatus = BAD_VALUE;
1665 goto Exit;
1666 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001667 lSessionId = *sessionId;
1668 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001669 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001670 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001671 if (sessionId != NULL) {
1672 *sessionId = lSessionId;
1673 }
1674 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001675 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001676
Glenn Kasten1879fff2012-07-11 15:36:59 -07001677 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001678 frameCount, lSessionId, notificationFrames,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001679 clientUid, flags, tid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001680 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001681
1682 if (lStatus == NO_ERROR) {
1683 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1684 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001685 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001686 if (chain != 0) {
1687 Mutex::Autolock _l(thread->mLock);
1688 thread->addEffectChain_l(chain);
1689 }
1690 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001691 }
Glenn Kastene198c362013-08-13 09:13:36 -07001692
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001693 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001694 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001695 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001696 // Don't hold mClientLock when releasing the reference on the track as the
1697 // destructor will acquire it.
1698 {
1699 Mutex::Autolock _cl(mClientLock);
1700 client.clear();
1701 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001702 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001703 goto Exit;
1704 }
1705
Glenn Kastend776ac62014-05-07 09:16:09 -07001706 cblk = recordTrack->getCblk();
1707 buffers = recordTrack->getBuffers();
1708
Glenn Kasten2fc14732013-08-05 14:58:14 -07001709 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001710 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001711
1712Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001713 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001714 return recordHandle;
1715}
1716
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001717
1718
Mathias Agopian65ab4712010-07-14 17:59:35 -07001719// ----------------------------------------------------------------------------
1720
Eric Laurenta4c5a552012-03-29 10:12:40 -07001721audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1722{
Eric Laurent44622db2014-08-01 19:00:33 -07001723 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001724 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001725 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001726 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001727 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001728 }
1729 Mutex::Autolock _l(mLock);
1730 return loadHwModule_l(name);
1731}
1732
1733// loadHwModule_l() must be called with AudioFlinger::mLock held
1734audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1735{
1736 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1737 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1738 ALOGW("loadHwModule() module %s already loaded", name);
1739 return mAudioHwDevs.keyAt(i);
1740 }
1741 }
1742
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001743 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001744
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001745 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001746 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001747 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001748 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001749 }
1750
1751 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001752 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07001753 mHardwareStatus = AUDIO_HW_IDLE;
1754 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001755 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001756 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001757 }
1758
John Grossmanee578c02012-07-23 17:05:46 -07001759 // Check and cache this HAL's level of support for master mute and master
1760 // volume. If this is the first HAL opened, and it supports the get
1761 // methods, use the initial values provided by the HAL as the current
1762 // master mute and volume settings.
1763
1764 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1765 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001766 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001767
1768 if (0 == mAudioHwDevs.size()) {
1769 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001770 float mv;
1771 if (OK == dev->getMasterVolume(&mv)) {
1772 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07001773 }
1774
1775 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001776 bool mm;
1777 if (OK == dev->getMasterMute(&mm)) {
1778 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07001779 }
1780 }
1781
Eric Laurenta4c5a552012-03-29 10:12:40 -07001782 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001783 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07001784 flags = static_cast<AudioHwDevice::Flags>(flags |
1785 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1786 }
1787
1788 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001789 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07001790 flags = static_cast<AudioHwDevice::Flags>(flags |
1791 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1792 }
1793
Eric Laurenta4c5a552012-03-29 10:12:40 -07001794 mHardwareStatus = AUDIO_HW_IDLE;
1795 }
1796
Glenn Kastena13cde92016-03-28 15:26:02 -07001797 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001798 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001799
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001800 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001801
1802 return handle;
1803
1804}
1805
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001806// ----------------------------------------------------------------------------
1807
Glenn Kasten3b16c762012-11-14 08:44:39 -08001808uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001809{
1810 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001811 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001812 return thread != NULL ? thread->sampleRate() : 0;
1813}
1814
Glenn Kastene33054e2012-11-14 12:54:39 -08001815size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001816{
1817 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001818 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001819 return thread != NULL ? thread->frameCountHAL() : 0;
1820}
1821
1822// ----------------------------------------------------------------------------
1823
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001824status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1825{
1826 uid_t uid = IPCThreadState::self()->getCallingUid();
1827 if (uid != AID_SYSTEM) {
1828 return PERMISSION_DENIED;
1829 }
1830 Mutex::Autolock _l(mLock);
1831 if (mIsDeviceTypeKnown) {
1832 return INVALID_OPERATION;
1833 }
1834 mIsLowRamDevice = isLowRamDevice;
1835 mIsDeviceTypeKnown = true;
1836 return NO_ERROR;
1837}
1838
Eric Laurent93c3d412014-08-01 14:48:35 -07001839audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1840{
1841 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001842
1843 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1844 if (index >= 0) {
1845 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1846 mHwAvSyncIds.valueAt(index), sessionId);
1847 return mHwAvSyncIds.valueAt(index);
1848 }
1849
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001850 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07001851 if (dev == NULL) {
1852 return AUDIO_HW_SYNC_INVALID;
1853 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001854 String8 reply;
1855 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001856 if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001857 param = AudioParameter(reply);
1858 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001859
1860 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001861 if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001862 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1863 return AUDIO_HW_SYNC_INVALID;
1864 }
1865
1866 // allow only one session for a given HW A/V sync ID.
1867 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1868 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1869 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1870 value, mHwAvSyncIds.keyAt(i));
1871 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001872 break;
1873 }
1874 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001875
1876 mHwAvSyncIds.add(sessionId, value);
1877
1878 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1879 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1880 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07001881 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001882 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001883 param.addInt(String8(AudioParameter::keyStreamHwAvSync), value);
Eric Laurentfa90e842014-10-17 18:12:31 -07001884 thread->setParameters(param.toString());
1885 break;
1886 }
1887 }
1888
1889 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1890 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001891}
1892
Eric Laurent72e3f392015-05-20 14:43:50 -07001893status_t AudioFlinger::systemReady()
1894{
1895 Mutex::Autolock _l(mLock);
1896 ALOGI("%s", __FUNCTION__);
1897 if (mSystemReady) {
1898 ALOGW("%s called twice", __FUNCTION__);
1899 return NO_ERROR;
1900 }
1901 mSystemReady = true;
1902 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1903 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1904 thread->systemReady();
1905 }
1906 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1907 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1908 thread->systemReady();
1909 }
1910 return NO_ERROR;
1911}
1912
Eric Laurentfa90e842014-10-17 18:12:31 -07001913// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1914void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1915{
1916 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1917 if (index >= 0) {
1918 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1919 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1920 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001921 param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId);
Eric Laurentfa90e842014-10-17 18:12:31 -07001922 thread->setParameters(param.toString());
1923 }
1924}
1925
1926
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001927// ----------------------------------------------------------------------------
1928
Eric Laurent83b88082014-06-20 18:31:16 -07001929
Eric Laurent6acd1d42017-01-04 14:23:29 -08001930sp<AudioFlinger::ThreadBase> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001931 audio_io_handle_t *output,
1932 audio_config_t *config,
1933 audio_devices_t devices,
1934 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001935 audio_output_flags_t flags)
1936{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001937 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001938 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001939 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001940 }
1941
Eric Laurentcf2c0212014-07-25 16:20:43 -07001942 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001943 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1944 } else {
1945 // Audio Policy does not currently request a specific output handle.
1946 // If this is ever needed, see openInput_l() for example code.
1947 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1948 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001949 }
Eric Laurent83b88082014-06-20 18:31:16 -07001950
1951 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1952
Eric Laurent83b88082014-06-20 18:31:16 -07001953 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001954 // This if statement allows overriding the audio policy settings
1955 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1956 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1957 // Check only for Normal Mixing mode
1958 if (kEnableExtendedPrecision) {
1959 // Specify format (uncomment one below to choose)
1960 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1961 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1962 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1963 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001964 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001965 }
1966 if (kEnableExtendedChannels) {
1967 // Specify channel mask (uncomment one below to choose)
1968 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1969 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1970 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1971 }
Eric Laurent83b88082014-06-20 18:31:16 -07001972 }
1973
Phil Burk062e67a2015-02-11 13:40:50 -08001974 AudioStreamOut *outputStream = NULL;
1975 status_t status = outHwDev->openOutputStream(
1976 &outputStream,
1977 *output,
1978 devices,
1979 flags,
1980 config,
1981 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001982
1983 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001984
Phil Burk062e67a2015-02-11 13:40:50 -08001985 if (status == NO_ERROR) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001986 if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
1987 sp<MmapPlaybackThread> thread =
1988 new MmapPlaybackThread(this, *output, outHwDev, outputStream,
1989 devices, AUDIO_DEVICE_NONE, mSystemReady);
1990 mMmapThreads.add(*output, thread);
1991 ALOGV("openOutput_l() created mmap playback thread: ID %d thread %p",
1992 *output, thread.get());
1993 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07001994 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001995 sp<PlaybackThread> thread;
1996 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1997 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
1998 ALOGV("openOutput_l() created offload output: ID %d thread %p",
1999 *output, thread.get());
2000 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
2001 || !isValidPcmSinkFormat(config->format)
2002 || !isValidPcmSinkChannelMask(config->channel_mask)) {
2003 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
2004 ALOGV("openOutput_l() created direct output: ID %d thread %p",
2005 *output, thread.get());
2006 } else {
2007 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
2008 ALOGV("openOutput_l() created mixer output: ID %d thread %p",
2009 *output, thread.get());
2010 }
2011 mPlaybackThreads.add(*output, thread);
2012 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07002013 }
Eric Laurent83b88082014-06-20 18:31:16 -07002014 }
2015
2016 return 0;
2017}
2018
Eric Laurentcf2c0212014-07-25 16:20:43 -07002019status_t AudioFlinger::openOutput(audio_module_handle_t module,
2020 audio_io_handle_t *output,
2021 audio_config_t *config,
2022 audio_devices_t *devices,
2023 const String8& address,
2024 uint32_t *latencyMs,
2025 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002026{
Glenn Kastend3bb6452016-12-05 18:14:37 -08002027 ALOGI("openOutput() this %p, module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, "
2028 "flags %x",
Eric Laurent6acd1d42017-01-04 14:23:29 -08002029 this, module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002030 (devices != NULL) ? *devices : 0,
2031 config->sample_rate,
2032 config->format,
2033 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002034 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002035
Yunlian Jiang32ba9862017-01-31 15:55:00 -08002036 if (devices == NULL || *devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002037 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002038 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002039
Mathias Agopian65ab4712010-07-14 17:59:35 -07002040 Mutex::Autolock _l(mLock);
2041
Eric Laurent6acd1d42017-01-04 14:23:29 -08002042 sp<ThreadBase> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002043 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002044 if ((flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) {
2045 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
2046 *latencyMs = playbackThread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002047
Eric Laurent6acd1d42017-01-04 14:23:29 -08002048 // notify client processes of the new output creation
2049 playbackThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002050
Eric Laurent6acd1d42017-01-04 14:23:29 -08002051 // the first primary output opened designates the primary hw device
2052 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -08002053 ALOGI("Using module %d as the primary audio interface", module);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002054 mPrimaryHardwareDev = playbackThread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002055
Eric Laurent6acd1d42017-01-04 14:23:29 -08002056 AutoMutex lock(mHardwareLock);
2057 mHardwareStatus = AUDIO_HW_SET_MODE;
2058 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
2059 mHardwareStatus = AUDIO_HW_IDLE;
2060 }
2061 } else {
2062 MmapThread *mmapThread = (MmapThread *)thread.get();
2063 mmapThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002064 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002065 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002066 }
2067
Eric Laurentcf2c0212014-07-25 16:20:43 -07002068 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002069}
2070
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002071audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
2072 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002073{
2074 Mutex::Autolock _l(mLock);
2075 MixerThread *thread1 = checkMixerThread_l(output1);
2076 MixerThread *thread2 = checkMixerThread_l(output2);
2077
2078 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002079 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
2080 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07002081 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002082 }
2083
Glenn Kasteneeecb982016-02-26 10:44:04 -08002084 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07002085 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002086 thread->addOutputTrack(thread2);
2087 mPlaybackThreads.add(id, thread);
2088 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002089 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002090 return id;
2091}
2092
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002093status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002094{
Glenn Kastend96c5722012-04-25 13:44:49 -07002095 return closeOutput_nonvirtual(output);
2096}
2097
2098status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
2099{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002100 // keep strong reference on the playback thread so that
2101 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002102 sp<PlaybackThread> playbackThread;
2103 sp<MmapPlaybackThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002104 {
2105 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002106 playbackThread = checkPlaybackThread_l(output);
2107 if (playbackThread != NULL) {
2108 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002109
Eric Laurent6acd1d42017-01-04 14:23:29 -08002110 if (playbackThread->type() == ThreadBase::MIXER) {
2111 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2112 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
2113 DuplicatingThread *dupThread =
2114 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
2115 dupThread->removeOutputTrack((MixerThread *)playbackThread.get());
2116 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002117 }
2118 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002119
2120
Eric Laurent6acd1d42017-01-04 14:23:29 -08002121 mPlaybackThreads.removeItem(output);
2122 // save all effects to the default thread
2123 if (mPlaybackThreads.size()) {
2124 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
2125 if (dstThread != NULL) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002126 // audioflinger lock is held so order of thread lock acquisition doesn't matter
Eric Laurent6acd1d42017-01-04 14:23:29 -08002127 Mutex::Autolock _dl(dstThread->mLock);
2128 Mutex::Autolock _sl(playbackThread->mLock);
2129 Vector< sp<EffectChain> > effectChains = playbackThread->getEffectChains_l();
2130 for (size_t i = 0; i < effectChains.size(); i ++) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002131 moveEffectChain_l(effectChains[i]->sessionId(), playbackThread.get(),
2132 dstThread, true);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002133 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002134 }
2135 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002136 } else {
2137 mmapThread = (MmapPlaybackThread *)checkMmapThread_l(output);
2138 if (mmapThread == 0) {
2139 return BAD_VALUE;
2140 }
2141 mMmapThreads.removeItem(output);
Phil Burk7f6b40d2017-02-09 13:18:38 -08002142 ALOGD("closing mmapThread %p", mmapThread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002143 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002144 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2145 ioDesc->mIoHandle = output;
2146 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002147 }
Glenn Kastenb28686f2012-01-06 08:39:38 -08002148 // The thread entity (active unit of execution) is no longer running here,
2149 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002150
Eric Laurent6acd1d42017-01-04 14:23:29 -08002151 if (playbackThread != 0) {
2152 playbackThread->exit();
2153 if (!playbackThread->isDuplicating()) {
2154 closeOutputFinish(playbackThread);
2155 }
2156 } else if (mmapThread != 0) {
Phil Burk7f6b40d2017-02-09 13:18:38 -08002157 ALOGD("mmapThread exit()");
Eric Laurent6acd1d42017-01-04 14:23:29 -08002158 mmapThread->exit();
2159 AudioStreamOut *out = mmapThread->clearOutput();
2160 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2161 // from now on thread->mOutput is NULL
2162 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002163 }
2164 return NO_ERROR;
2165}
2166
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002167void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002168{
2169 AudioStreamOut *out = thread->clearOutput();
2170 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2171 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07002172 delete out;
2173}
2174
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002175void AudioFlinger::closeOutputInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002176{
2177 mPlaybackThreads.removeItem(thread->mId);
2178 thread->exit();
2179 closeOutputFinish(thread);
2180}
2181
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002182status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002183{
2184 Mutex::Autolock _l(mLock);
2185 PlaybackThread *thread = checkPlaybackThread_l(output);
2186
2187 if (thread == NULL) {
2188 return BAD_VALUE;
2189 }
2190
Steve Block3856b092011-10-20 11:56:00 +01002191 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192 thread->suspend();
2193
2194 return NO_ERROR;
2195}
2196
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002197status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002198{
2199 Mutex::Autolock _l(mLock);
2200 PlaybackThread *thread = checkPlaybackThread_l(output);
2201
2202 if (thread == NULL) {
2203 return BAD_VALUE;
2204 }
2205
Steve Block3856b092011-10-20 11:56:00 +01002206 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002207
2208 thread->restore();
2209
2210 return NO_ERROR;
2211}
2212
Eric Laurentcf2c0212014-07-25 16:20:43 -07002213status_t AudioFlinger::openInput(audio_module_handle_t module,
2214 audio_io_handle_t *input,
2215 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002216 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002217 const String8& address,
2218 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002219 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002220{
Eric Laurent83b88082014-06-20 18:31:16 -07002221 Mutex::Autolock _l(mLock);
2222
Glenn Kastene7d66712015-03-05 13:46:52 -08002223 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002224 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002225 }
2226
Eric Laurent6acd1d42017-01-04 14:23:29 -08002227 sp<ThreadBase> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002228
2229 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002230 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002231 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002232 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002233 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002234 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002235}
Dima Zavin799a70e2011-04-18 16:57:27 -07002236
Eric Laurent6acd1d42017-01-04 14:23:29 -08002237sp<AudioFlinger::ThreadBase> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002238 audio_io_handle_t *input,
2239 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002240 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002241 const String8& address,
2242 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002243 audio_input_flags_t flags)
2244{
Glenn Kastene7d66712015-03-05 13:46:52 -08002245 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002246 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002247 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002248 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002249 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002250
Glenn Kasteneeecb982016-02-26 10:44:04 -08002251 // Audio Policy can request a specific handle for hardware hotword.
2252 // The goal here is not to re-open an already opened input.
2253 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002254 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002255 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2256 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2257 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2258 return 0;
2259 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2260 // This should not happen in a transient state with current design.
2261 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2262 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002263 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002264
Eric Laurentcf2c0212014-07-25 16:20:43 -07002265 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002266 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002267 sp<StreamInHalInterface> inStream;
2268 status_t status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002269 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Mikhail Naganovf558e022016-11-14 17:45:17 -08002270 ALOGV("openInput_l() openInputStream returned input %p, devices %x, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002271 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002272 inStream.get(),
Mikhail Naganovf558e022016-11-14 17:45:17 -08002273 devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002274 halconfig.sample_rate,
2275 halconfig.format,
2276 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002277 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002278 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002279
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002280 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002281 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002282 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002283 audio_is_linear_pcm(config->format) &&
2284 audio_is_linear_pcm(halconfig.format) &&
2285 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002286 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2287 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002288 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002289 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002290 inStream.clear();
2291 status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002292 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002293 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002294 }
2295
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002296 if (status == NO_ERROR && inStream != 0) {
Eric Laurent05067782016-06-01 18:27:28 -07002297 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002298 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) {
2299 sp<MmapCaptureThread> thread =
2300 new MmapCaptureThread(this, *input,
2301 inHwDev, inputStream,
2302 primaryOutputDevice_l(), devices, mSystemReady);
2303 mMmapThreads.add(*input, thread);
Glenn Kastend3bb6452016-12-05 18:14:37 -08002304 ALOGV("openInput_l() created mmap capture thread: ID %d thread %p", *input,
2305 thread.get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002306 return thread;
2307 } else {
Glenn Kasten46909e72013-02-26 09:20:22 -08002308#ifdef TEE_SINK
Eric Laurent6acd1d42017-01-04 14:23:29 -08002309 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2310 // or (re-)create if current Pipe is idle and does not match the new format
2311 sp<NBAIO_Sink> teeSink;
2312 enum {
2313 TEE_SINK_NO, // don't copy input
2314 TEE_SINK_NEW, // copy input using a new pipe
2315 TEE_SINK_OLD, // copy input using an existing pipe
2316 } kind;
2317 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2318 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
2319 if (!mTeeSinkInputEnabled) {
2320 kind = TEE_SINK_NO;
2321 } else if (!Format_isValid(format)) {
2322 kind = TEE_SINK_NO;
2323 } else if (mRecordTeeSink == 0) {
2324 kind = TEE_SINK_NEW;
2325 } else if (mRecordTeeSink->getStrongCount() != 1) {
2326 kind = TEE_SINK_NO;
2327 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
2328 kind = TEE_SINK_OLD;
2329 } else {
2330 kind = TEE_SINK_NEW;
2331 }
2332 switch (kind) {
2333 case TEE_SINK_NEW: {
2334 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
2335 size_t numCounterOffers = 0;
2336 const NBAIO_Format offers[1] = {format};
2337 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2338 ALOG_ASSERT(index == 0);
2339 PipeReader *pipeReader = new PipeReader(*pipe);
2340 numCounterOffers = 0;
2341 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2342 ALOG_ASSERT(index == 0);
2343 mRecordTeeSink = pipe;
2344 mRecordTeeSource = pipeReader;
2345 teeSink = pipe;
2346 }
2347 break;
2348 case TEE_SINK_OLD:
2349 teeSink = mRecordTeeSink;
2350 break;
2351 case TEE_SINK_NO:
2352 default:
2353 break;
2354 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002355#endif
Eric Laurent6acd1d42017-01-04 14:23:29 -08002356
2357 // Start record thread
2358 // RecordThread requires both input and output device indication to forward to audio
2359 // pre processing modules
2360 sp<RecordThread> thread = new RecordThread(this,
2361 inputStream,
2362 *input,
2363 primaryOutputDevice_l(),
2364 devices,
2365 mSystemReady
2366#ifdef TEE_SINK
2367 , teeSink
2368#endif
2369 );
2370 mRecordThreads.add(*input, thread);
2371 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
2372 return thread;
2373 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002374 }
2375
Eric Laurentcf2c0212014-07-25 16:20:43 -07002376 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002377 return 0;
2378}
2379
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002380status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002381{
Glenn Kastend96c5722012-04-25 13:44:49 -07002382 return closeInput_nonvirtual(input);
2383}
2384
2385status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2386{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387 // keep strong reference on the record thread so that
2388 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002389 sp<RecordThread> recordThread;
2390 sp<MmapCaptureThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002391 {
2392 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002393 recordThread = checkRecordThread_l(input);
2394 if (recordThread != 0) {
2395 ALOGV("closeInput() %d", input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002396
Eric Laurent6acd1d42017-01-04 14:23:29 -08002397 // If we still have effect chains, it means that a client still holds a handle
2398 // on at least one effect. We must either move the chain to an existing thread with the
2399 // same session ID or put it aside in case a new record thread is opened for a
2400 // new capture on the same session
2401 sp<EffectChain> chain;
2402 {
2403 Mutex::Autolock _sl(recordThread->mLock);
2404 Vector< sp<EffectChain> > effectChains = recordThread->getEffectChains_l();
2405 // Note: maximum one chain per record thread
2406 if (effectChains.size() != 0) {
2407 chain = effectChains[0];
Eric Laurent1b928682014-10-02 19:41:47 -07002408 }
2409 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002410 if (chain != 0) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002411 // first check if a record thread is already opened with a client on same session.
Eric Laurent6acd1d42017-01-04 14:23:29 -08002412 // This should only happen in case of overlap between one thread tear down and the
2413 // creation of its replacement
2414 size_t i;
2415 for (i = 0; i < mRecordThreads.size(); i++) {
2416 sp<RecordThread> t = mRecordThreads.valueAt(i);
2417 if (t == recordThread) {
2418 continue;
2419 }
2420 if (t->hasAudioSession(chain->sessionId()) != 0) {
2421 Mutex::Autolock _l(t->mLock);
2422 ALOGV("closeInput() found thread %d for effect session %d",
2423 t->id(), chain->sessionId());
2424 t->addEffectChain_l(chain);
2425 break;
2426 }
2427 }
Glenn Kastend3bb6452016-12-05 18:14:37 -08002428 // put the chain aside if we could not find a record thread with the same session id
Eric Laurent6acd1d42017-01-04 14:23:29 -08002429 if (i == mRecordThreads.size()) {
2430 putOrphanEffectChain_l(chain);
2431 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002432 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002433 mRecordThreads.removeItem(input);
2434 } else {
2435 mmapThread = (MmapCaptureThread *)checkMmapThread_l(input);
2436 if (mmapThread == 0) {
2437 return BAD_VALUE;
2438 }
2439 mMmapThreads.removeItem(input);
Eric Laurentaaa44472014-09-12 17:41:50 -07002440 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002441 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2442 ioDesc->mIoHandle = input;
2443 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002444 }
Eric Laurent83b88082014-06-20 18:31:16 -07002445 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2446 // we have a different lock for notification client
Eric Laurent6acd1d42017-01-04 14:23:29 -08002447 if (recordThread != 0) {
2448 closeInputFinish(recordThread);
2449 } else if (mmapThread != 0) {
2450 mmapThread->exit();
2451 AudioStreamIn *in = mmapThread->clearInput();
2452 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
2453 // from now on thread->mInput is NULL
2454 delete in;
2455 }
Eric Laurent83b88082014-06-20 18:31:16 -07002456 return NO_ERROR;
2457}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002458
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002459void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002460{
2461 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002462 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002463 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002464 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002465 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002466}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002467
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002468void AudioFlinger::closeInputInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002469{
2470 mRecordThreads.removeItem(thread->mId);
2471 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002472}
2473
Glenn Kastend2304db2014-02-03 07:40:31 -08002474status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002475{
2476 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002477 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002478
2479 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2480 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002481 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002482 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002483 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2484 mMmapThreads[i]->invalidateTracks(stream);
2485 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002486 return NO_ERROR;
2487}
2488
2489
Glenn Kasteneeecb982016-02-26 10:44:04 -08002490audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002491{
Glenn Kasten9d003132016-04-06 14:38:09 -07002492 // This is a binder API, so a malicious client could pass in a bad parameter.
2493 // Check for that before calling the internal API nextUniqueId().
2494 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2495 ALOGE("newAudioUniqueId invalid use %d", use);
2496 return AUDIO_UNIQUE_ID_ALLOCATE;
2497 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002498 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002499}
2500
Glenn Kastend848eb42016-03-08 13:42:11 -08002501void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002502{
2503 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002504 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002505 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2506 if (pid != -1 && (caller == getpid_cached)) {
2507 caller = pid;
2508 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002509
Eric Laurent021cf962014-05-13 10:18:14 -07002510 {
2511 Mutex::Autolock _cl(mClientLock);
2512 // Ignore requests received from processes not known as notification client. The request
2513 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2514 // called from a different pid leaving a stale session reference. Also we don't know how
2515 // to clear this reference if the client process dies.
2516 if (mNotificationClients.indexOfKey(caller) < 0) {
2517 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2518 return;
2519 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002520 }
2521
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002522 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002523 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002524 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002525 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2526 ref->mCnt++;
2527 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002528 return;
2529 }
2530 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002531 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2532 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002533}
2534
Glenn Kastend848eb42016-03-08 13:42:11 -08002535void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002536{
2537 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002538 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002539 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2540 if (pid != -1 && (caller == getpid_cached)) {
2541 caller = pid;
2542 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002543 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002544 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002545 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002546 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2547 ref->mCnt--;
2548 ALOGV(" decremented refcount to %d", ref->mCnt);
2549 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002550 mAudioSessionRefs.removeAt(i);
2551 delete ref;
2552 purgeStaleEffects_l();
2553 }
2554 return;
2555 }
2556 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002557 // If the caller is mediaserver it is likely that the session being released was acquired
2558 // on behalf of a process not in notification clients and we ignore the warning.
2559 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002560}
2561
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002562bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
2563{
2564 size_t num = mAudioSessionRefs.size();
2565 for (size_t i = 0; i < num; i++) {
2566 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2567 if (ref->mSessionid == audioSession) {
2568 return true;
2569 }
2570 }
2571 return false;
2572}
2573
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002574void AudioFlinger::purgeStaleEffects_l() {
2575
Steve Block3856b092011-10-20 11:56:00 +01002576 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002577
2578 Vector< sp<EffectChain> > chains;
2579
2580 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2581 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2582 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2583 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002584 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2585 chains.push(ec);
2586 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002587 }
2588 }
2589 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2590 sp<RecordThread> t = mRecordThreads.valueAt(i);
2591 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2592 sp<EffectChain> ec = t->mEffectChains[j];
2593 chains.push(ec);
2594 }
2595 }
2596
2597 for (size_t i = 0; i < chains.size(); i++) {
2598 sp<EffectChain> ec = chains[i];
2599 int sessionid = ec->sessionId();
2600 sp<ThreadBase> t = ec->mThread.promote();
2601 if (t == 0) {
2602 continue;
2603 }
2604 size_t numsessionrefs = mAudioSessionRefs.size();
2605 bool found = false;
2606 for (size_t k = 0; k < numsessionrefs; k++) {
2607 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002608 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002609 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002610 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002611 found = true;
2612 break;
2613 }
2614 }
2615 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002616 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002617 // remove all effects from the chain
2618 while (ec->mEffects.size()) {
2619 sp<EffectModule> effect = ec->mEffects[0];
2620 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002621 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002622 if (effect->purgeHandles()) {
2623 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002624 }
2625 AudioSystem::unregisterEffect(effect->id());
2626 }
2627 }
2628 }
2629 return;
2630}
2631
Glenn Kasteneeecb982016-02-26 10:44:04 -08002632// checkThread_l() must be called with AudioFlinger::mLock held
2633AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2634{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002635 ThreadBase *thread = checkMmapThread_l(ioHandle);
2636 if (thread == 0) {
2637 switch (audio_unique_id_get_use(ioHandle)) {
2638 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2639 thread = checkPlaybackThread_l(ioHandle);
2640 break;
2641 case AUDIO_UNIQUE_ID_USE_INPUT:
2642 thread = checkRecordThread_l(ioHandle);
2643 break;
2644 default:
2645 break;
2646 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002647 }
2648 return thread;
2649}
2650
Mathias Agopian65ab4712010-07-14 17:59:35 -07002651// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002652AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002653{
Glenn Kastena1117922012-01-26 10:53:32 -08002654 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002655}
2656
2657// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002658AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002659{
2660 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002661 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002662}
2663
2664// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002665AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002666{
Glenn Kastena1117922012-01-26 10:53:32 -08002667 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002668}
2669
Eric Laurent6acd1d42017-01-04 14:23:29 -08002670// checkMmapThread_l() must be called with AudioFlinger::mLock held
2671AudioFlinger::MmapThread *AudioFlinger::checkMmapThread_l(audio_io_handle_t io) const
2672{
2673 return mMmapThreads.valueFor(io).get();
2674}
2675
2676
2677// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
2678AudioFlinger::VolumeInterface *AudioFlinger::getVolumeInterface_l(audio_io_handle_t output) const
2679{
Eric Laurent7459b902017-04-19 18:10:41 -07002680 VolumeInterface *volumeInterface = mPlaybackThreads.valueFor(output).get();
Eric Laurent6acd1d42017-01-04 14:23:29 -08002681 if (volumeInterface == nullptr) {
2682 MmapThread *mmapThread = mMmapThreads.valueFor(output).get();
2683 if (mmapThread != nullptr) {
2684 if (mmapThread->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07002685 MmapPlaybackThread *mmapPlaybackThread =
2686 static_cast<MmapPlaybackThread *>(mmapThread);
2687 volumeInterface = mmapPlaybackThread;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002688 }
2689 }
2690 }
2691 return volumeInterface;
2692}
2693
2694Vector <AudioFlinger::VolumeInterface *> AudioFlinger::getAllVolumeInterfaces_l() const
2695{
2696 Vector <VolumeInterface *> volumeInterfaces;
2697 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7459b902017-04-19 18:10:41 -07002698 volumeInterfaces.add(mPlaybackThreads.valueAt(i).get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002699 }
2700 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2701 if (mMmapThreads.valueAt(i)->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07002702 MmapPlaybackThread *mmapPlaybackThread =
2703 static_cast<MmapPlaybackThread *>(mMmapThreads.valueAt(i).get());
2704 volumeInterfaces.add(mmapPlaybackThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002705 }
2706 }
2707 return volumeInterfaces;
2708}
2709
Glenn Kasteneeecb982016-02-26 10:44:04 -08002710audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002711{
Glenn Kasten9d003132016-04-06 14:38:09 -07002712 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002713 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002714 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2715 for (int retry = 0; retry < maxRetries; retry++) {
2716 // The cast allows wraparound from max positive to min negative instead of abort
2717 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2718 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2719 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2720 // allow wrap by skipping 0 and -1 for session ids
2721 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2722 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2723 return (audio_unique_id_t) (base | use);
2724 }
2725 }
2726 // We have no way of recovering from wraparound
2727 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2728 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002729}
2730
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002731AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002732{
2733 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2734 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002735 if(thread->isDuplicating()) {
2736 continue;
2737 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002738 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002739 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002740 return thread;
2741 }
2742 }
2743 return NULL;
2744}
2745
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002746audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002747{
2748 PlaybackThread *thread = primaryPlaybackThread_l();
2749
2750 if (thread == NULL) {
2751 return 0;
2752 }
2753
Eric Laurentf1c04f92012-08-28 14:26:53 -07002754 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002755}
2756
Glenn Kastena7335632016-06-09 17:09:53 -07002757AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
2758{
2759 size_t minFrameCount = 0;
2760 PlaybackThread *minThread = NULL;
2761 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2762 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2763 if (!thread->isDuplicating()) {
2764 size_t frameCount = thread->frameCountHAL();
2765 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
2766 (frameCount == minFrameCount && thread->hasFastMixer() &&
2767 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
2768 minFrameCount = frameCount;
2769 minThread = thread;
2770 }
2771 }
2772 }
2773 return minThread;
2774}
2775
Eric Laurenta011e352012-03-29 15:51:43 -07002776sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002777 audio_session_t triggerSession,
2778 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002779 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002780 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002781{
2782 Mutex::Autolock _l(mLock);
2783
2784 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2785 status_t playStatus = NAME_NOT_FOUND;
2786 status_t recStatus = NAME_NOT_FOUND;
2787 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2788 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2789 if (playStatus == NO_ERROR) {
2790 return event;
2791 }
2792 }
2793 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2794 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2795 if (recStatus == NO_ERROR) {
2796 return event;
2797 }
2798 }
2799 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2800 mPendingSyncEvents.add(event);
2801 } else {
2802 ALOGV("createSyncEvent() invalid event %d", event->type());
2803 event.clear();
2804 }
2805 return event;
2806}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002807
Mathias Agopian65ab4712010-07-14 17:59:35 -07002808// ----------------------------------------------------------------------------
2809// Effect management
2810// ----------------------------------------------------------------------------
2811
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002812sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
2813 return mEffectsFactoryHal;
2814}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002815
Glenn Kastenf587ba52012-01-26 16:25:10 -08002816status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002817{
2818 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002819 if (mEffectsFactoryHal.get()) {
2820 return mEffectsFactoryHal->queryNumberEffects(numEffects);
2821 } else {
2822 return -ENODEV;
2823 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002824}
2825
Glenn Kastenf587ba52012-01-26 16:25:10 -08002826status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002827{
2828 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002829 if (mEffectsFactoryHal.get()) {
2830 return mEffectsFactoryHal->getDescriptor(index, descriptor);
2831 } else {
2832 return -ENODEV;
2833 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002834}
2835
Glenn Kasten5e92a782012-01-30 07:40:52 -08002836status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002837 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002838{
2839 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002840 if (mEffectsFactoryHal.get()) {
2841 return mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
2842 } else {
2843 return -ENODEV;
2844 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002845}
2846
2847
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002848sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002849 effect_descriptor_t *pDesc,
2850 const sp<IEffectClient>& effectClient,
2851 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002852 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002853 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002854 const String16& opPackageName,
Eric Laurentb6436272016-12-07 19:24:50 -08002855 pid_t pid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002856 status_t *status,
2857 int *id,
2858 int *enabled)
2859{
2860 status_t lStatus = NO_ERROR;
2861 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002862 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002863
Eric Laurentb6436272016-12-07 19:24:50 -08002864 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
2865 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
2866 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
2867 ALOGW_IF(pid != -1 && pid != callingPid,
2868 "%s uid %d pid %d tried to pass itself off as pid %d",
2869 __func__, callingUid, callingPid, pid);
2870 pid = callingPid;
2871 }
2872
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002873 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
2874 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002875
2876 if (pDesc == NULL) {
2877 lStatus = BAD_VALUE;
2878 goto Exit;
2879 }
2880
Eric Laurent84e9a102010-09-23 16:10:16 -07002881 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002882 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002883 lStatus = PERMISSION_DENIED;
2884 goto Exit;
2885 }
2886
Dima Zavinfce7a472011-04-19 22:30:36 -07002887 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002888 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002889 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002890 lStatus = PERMISSION_DENIED;
2891 goto Exit;
2892 }
2893
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002894 if (mEffectsFactoryHal == 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002895 lStatus = NO_INIT;
2896 goto Exit;
2897 }
2898
Mathias Agopian65ab4712010-07-14 17:59:35 -07002899 {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002900 if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002901 // if uuid is specified, request effect descriptor
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002902 lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002903 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002904 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002905 goto Exit;
2906 }
2907 } else {
2908 // if uuid is not specified, look for an available implementation
2909 // of the required type in effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002910 if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002911 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002912 lStatus = BAD_VALUE;
2913 goto Exit;
2914 }
2915 uint32_t numEffects = 0;
2916 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002917 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002918 bool found = false;
2919
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002920 lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002921 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002922 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002923 goto Exit;
2924 }
2925 for (uint32_t i = 0; i < numEffects; i++) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002926 lStatus = mEffectsFactoryHal->getDescriptor(i, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002927 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002928 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002929 continue;
2930 }
2931 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2932 // If matching type found save effect descriptor. If the session is
2933 // 0 and the effect is not auxiliary, continue enumeration in case
2934 // an auxiliary version of this effect type is available
2935 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002936 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002937 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002938 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2939 break;
2940 }
2941 }
2942 }
2943 if (!found) {
2944 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002945 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002946 goto Exit;
2947 }
2948 // For same effect type, chose auxiliary version over insert version if
2949 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002950 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002951 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002952 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002953 }
2954 }
2955
2956 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002957 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002958 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2959 lStatus = INVALID_OPERATION;
2960 goto Exit;
2961 }
2962
Eric Laurent59255e42011-07-27 19:49:51 -07002963 // check recording permission for visualizer
2964 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002965 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002966 lStatus = PERMISSION_DENIED;
2967 goto Exit;
2968 }
2969
Mathias Agopian65ab4712010-07-14 17:59:35 -07002970 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002971 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002972 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002973 // if the output returned by getOutputForEffect() is removed before we lock the
2974 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2975 // and we will exit safely
2976 io = AudioSystem::getOutputForEffect(&desc);
2977 ALOGV("createEffect got output %d", io);
2978 }
2979
2980 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002981
2982 // If output is not specified try to find a matching audio session ID in one of the
2983 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002984 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2985 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002986 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002987 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002988 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2989 // output must be specified by AudioPolicyManager when using session
2990 // AUDIO_SESSION_OUTPUT_STAGE
2991 lStatus = BAD_VALUE;
2992 goto Exit;
2993 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002994 // look for the thread where the specified audio session is present
2995 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2996 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2997 io = mPlaybackThreads.keyAt(i);
2998 break;
2999 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003000 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08003001 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07003002 for (size_t i = 0; i < mRecordThreads.size(); i++) {
3003 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
3004 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07003005 break;
3006 }
3007 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003008 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08003009 if (io == AUDIO_IO_HANDLE_NONE) {
3010 for (size_t i = 0; i < mMmapThreads.size(); i++) {
3011 if (mMmapThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
3012 io = mMmapThreads.keyAt(i);
3013 break;
3014 }
3015 }
3016 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003017 // If no output thread contains the requested session ID, default to
3018 // first output. The effect chain will be moved to the correct output
3019 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07003020 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003021 io = mPlaybackThreads.keyAt(0);
3022 }
Steve Block3856b092011-10-20 11:56:00 +01003023 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003024 }
3025 ThreadBase *thread = checkRecordThread_l(io);
3026 if (thread == NULL) {
3027 thread = checkPlaybackThread_l(io);
3028 if (thread == NULL) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08003029 thread = checkMmapThread_l(io);
3030 if (thread == NULL) {
3031 ALOGE("createEffect() unknown output thread");
3032 lStatus = BAD_VALUE;
3033 goto Exit;
3034 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003035 }
Eric Laurentaaa44472014-09-12 17:41:50 -07003036 } else {
3037 // Check if one effect chain was awaiting for an effect to be created on this
3038 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08003039 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07003040 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07003041 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07003042 thread->addEffectChain_l(chain);
3043 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003044 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003045
Eric Laurent021cf962014-05-13 10:18:14 -07003046 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003047
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003048 // create effect on selected output thread
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003049 bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003050 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003051 &desc, enabled, &lStatus, pinned);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003052 if (handle != 0 && id != NULL) {
3053 *id = handle->id();
3054 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003055 if (handle == 0) {
3056 // remove local strong reference to Client with mClientLock held
3057 Mutex::Autolock _cl(mClientLock);
3058 client.clear();
3059 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003060 }
3061
3062Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07003063 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003064 return handle;
3065}
3066
Glenn Kastend848eb42016-03-08 13:42:11 -08003067status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003068 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07003069{
Steve Block3856b092011-10-20 11:56:00 +01003070 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07003071 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003072 Mutex::Autolock _l(mLock);
3073 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003074 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003075 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003076 }
Eric Laurentde070132010-07-13 04:45:46 -07003077 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
3078 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003079 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003080 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003081 }
Eric Laurentde070132010-07-13 04:45:46 -07003082 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
3083 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003084 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003085 return BAD_VALUE;
3086 }
3087
3088 Mutex::Autolock _dl(dstThread->mLock);
3089 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003090 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003091}
3092
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003093// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08003094status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07003095 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07003096 AudioFlinger::PlaybackThread *dstThread,
3097 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07003098{
Steve Block3856b092011-10-20 11:56:00 +01003099 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003100 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07003101
Eric Laurent59255e42011-07-27 19:49:51 -07003102 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003103 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003104 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003105 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07003106 return INVALID_OPERATION;
3107 }
3108
Eric Laurent4c415062016-06-17 16:14:16 -07003109 // Check whether the destination thread and all effects in the chain are compatible
3110 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07003111 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07003112 " destination thread %p is not compatible with effects in the chain",
3113 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07003114 return INVALID_OPERATION;
3115 }
3116
Eric Laurent39e94f82010-07-28 01:32:47 -07003117 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07003118 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07003119 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07003120 // removed.
3121 srcThread->removeEffectChain_l(chain);
3122
3123 // transfer all effects one by one so that new effect chain is created on new thread with
3124 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07003125 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003126 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07003127 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003128 Vector< sp<EffectModule> > removed;
3129 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07003130 while (effect != 0) {
3131 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003132 removed.add(effect);
3133 status = dstThread->addEffect_l(effect);
3134 if (status != NO_ERROR) {
3135 break;
3136 }
Eric Laurentec35a142011-10-05 17:42:25 -07003137 // removeEffect_l() has stopped the effect if it was active so it must be restarted
3138 if (effect->state() == EffectModule::ACTIVE ||
3139 effect->state() == EffectModule::STOPPING) {
3140 effect->start();
3141 }
Eric Laurent39e94f82010-07-28 01:32:47 -07003142 // if the move request is not received from audio policy manager, the effect must be
3143 // re-registered with the new strategy and output
3144 if (dstChain == 0) {
3145 dstChain = effect->chain().promote();
3146 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003147 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003148 status = NO_INIT;
3149 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07003150 }
3151 strategy = dstChain->strategy();
3152 }
3153 if (reRegister) {
3154 AudioSystem::unregisterEffect(effect->id());
3155 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07003156 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07003157 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07003158 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07003159 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003160 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07003161 }
Eric Laurentde070132010-07-13 04:45:46 -07003162 effect = chain->getEffectFromId_l(0);
3163 }
3164
Eric Laurent5baf2af2013-09-12 17:37:00 -07003165 if (status != NO_ERROR) {
3166 for (size_t i = 0; i < removed.size(); i++) {
3167 srcThread->addEffect_l(removed[i]);
3168 if (dstChain != 0 && reRegister) {
3169 AudioSystem::unregisterEffect(removed[i]->id());
3170 AudioSystem::registerEffect(&removed[i]->desc(),
3171 srcThread->id(),
3172 strategy,
3173 sessionId,
3174 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003175 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003176 }
3177 }
3178 }
3179
3180 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003181}
3182
Eric Laurent5baf2af2013-09-12 17:37:00 -07003183bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07003184{
3185 if (mGlobalEffectEnableTime != 0 &&
3186 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
3187 return true;
3188 }
3189
3190 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3191 sp<EffectChain> ec =
3192 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003193 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07003194 return true;
3195 }
3196 }
3197 return false;
3198}
3199
Eric Laurent5baf2af2013-09-12 17:37:00 -07003200void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07003201{
3202 Mutex::Autolock _l(mLock);
3203
3204 mGlobalEffectEnableTime = systemTime();
3205
3206 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3207 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
3208 if (t->mType == ThreadBase::OFFLOAD) {
3209 t->invalidateTracks(AUDIO_STREAM_MUSIC);
3210 }
3211 }
3212
3213}
3214
Eric Laurentaaa44472014-09-12 17:41:50 -07003215status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
3216{
Glenn Kastend848eb42016-03-08 13:42:11 -08003217 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003218 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003219 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003220 if (index >= 0) {
3221 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
3222 return ALREADY_EXISTS;
3223 }
3224 mOrphanEffectChains.add(session, chain);
3225 return NO_ERROR;
3226}
3227
3228sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
3229{
3230 sp<EffectChain> chain;
3231 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003232 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003233 if (index >= 0) {
3234 chain = mOrphanEffectChains.valueAt(index);
3235 mOrphanEffectChains.removeItemsAt(index);
3236 }
3237 return chain;
3238}
3239
3240bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
3241{
3242 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08003243 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003244 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003245 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003246 if (index >= 0) {
3247 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003248 if (chain->removeEffect_l(effect, true) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003249 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003250 mOrphanEffectChains.removeItemsAt(index);
3251 }
3252 return true;
3253 }
3254 return false;
3255}
3256
3257
Glenn Kastenda6ef132013-01-10 12:31:01 -08003258struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003259#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
3260 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003261};
3262
3263int comparEntry(const void *p1, const void *p2)
3264{
Glenn Kasten2f55e762015-03-05 16:05:08 -08003265 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003266}
3267
Glenn Kasten46909e72013-02-26 09:20:22 -08003268#ifdef TEE_SINK
Glenn Kasten5b2191a2016-08-19 11:44:47 -07003269void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id, char suffix)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003270{
Eric Laurent81784c32012-11-19 14:55:58 -08003271 NBAIO_Source *teeSource = source.get();
3272 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003273 // .wav rotation
3274 // There is a benign race condition if 2 threads call this simultaneously.
3275 // They would both traverse the directory, but the result would simply be
3276 // failures at unlink() which are ignored. It's also unlikely since
3277 // normally dumpsys is only done by bugreport or from the command line.
3278 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08003279 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08003280 size_t teePathLen = strlen(teePath);
3281 DIR *dir = opendir(teePath);
3282 teePath[teePathLen++] = '/';
3283 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003284#define TEE_MAX_SORT 20 // number of entries to sort
3285#define TEE_MAX_KEEP 10 // number of entries to keep
3286 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003287 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08003288 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003289 struct dirent de;
3290 struct dirent *result = NULL;
3291 int rc = readdir_r(dir, &de, &result);
3292 if (rc != 0) {
3293 ALOGW("readdir_r failed %d", rc);
3294 break;
3295 }
3296 if (result == NULL) {
3297 break;
3298 }
3299 if (result != &de) {
3300 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
3301 break;
3302 }
3303 // ignore non .wav file entries
3304 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003305 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08003306 strcmp(&de.d_name[nameLen - 4], ".wav")) {
3307 continue;
3308 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08003309 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003310 }
3311 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003312 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003313 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003314 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
3315 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003316 (void) unlink(teePath);
3317 }
3318 }
3319 } else {
3320 if (fd >= 0) {
Glenn Kastenfbd87e82016-07-18 15:45:56 -07003321 dprintf(fd, "unable to rotate tees in %.*s: %s\n", (int) teePathLen, teePath,
Glenn Kasten9a003992016-02-23 15:24:34 -08003322 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003323 }
3324 }
Eric Laurent81784c32012-11-19 14:55:58 -08003325 char teeTime[16];
3326 struct timeval tv;
3327 gettimeofday(&tv, NULL);
3328 struct tm tm;
3329 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003330 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
Glenn Kasten5b2191a2016-08-19 11:44:47 -07003331 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d_%c.wav", teeTime, id,
3332 suffix);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003333 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3334 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003335 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003336 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003337 char wavHeader[44];
3338 memcpy(wavHeader,
3339 "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",
3340 sizeof(wavHeader));
3341 NBAIO_Format format = teeSource->format();
3342 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003343 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003344 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003345 wavHeader[22] = channelCount; // number of channels
3346 wavHeader[24] = sampleRate; // sample rate
3347 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003348 wavHeader[32] = frameSize; // block alignment
3349 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003350 write(teeFd, wavHeader, sizeof(wavHeader));
3351 size_t total = 0;
3352 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003353#define TEE_SINK_READ 1024 // frames per I/O operation
3354 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003355 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003356 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003357 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003358 bool wasFirstRead = firstRead;
3359 firstRead = false;
3360 if (actual <= 0) {
3361 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3362 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003363 }
Eric Laurent81784c32012-11-19 14:55:58 -08003364 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003365 }
Eric Laurent81784c32012-11-19 14:55:58 -08003366 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003367 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003368 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003369 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003370 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003371 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003372 uint32_t temp = 44 + total * frameSize - 8;
3373 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003374 write(teeFd, &temp, sizeof(temp));
3375 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003376 temp = total * frameSize;
3377 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003378 write(teeFd, &temp, sizeof(temp));
3379 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003380 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003381 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003382 }
Eric Laurent59255e42011-07-27 19:49:51 -07003383 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003384 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003385 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003386 }
Eric Laurent59255e42011-07-27 19:49:51 -07003387 }
3388 }
3389}
Glenn Kasten46909e72013-02-26 09:20:22 -08003390#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003391
Mathias Agopian65ab4712010-07-14 17:59:35 -07003392// ----------------------------------------------------------------------------
3393
3394status_t AudioFlinger::onTransact(
3395 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3396{
3397 return BnAudioFlinger::onTransact(code, data, reply, flags);
3398}
3399
Glenn Kasten63238ef2015-03-02 15:50:29 -08003400} // namespace android