blob: 0df9a39c921be29998b2968de5edec132175026f [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 Laurentcb4dae22017-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 Laurentcb4dae22017-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 Laurentcb4dae22017-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 Laurentcb4dae22017-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 Laurentcb4dae22017-07-01 19:39:32 -0700288 sp<MmapStreamInterface>& interface,
289 audio_port_handle_t *handle)
Eric Laurentfc235202016-12-20 18:48:17 -0800290{
291 status_t ret = initCheck();
292 if (ret != NO_ERROR) {
293 return ret;
294 }
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 Laurentcb4dae22017-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 Laurent2ac76942017-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 Laurent2ac76942017-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 Laurentcb4dae22017-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 Laurentd8365c52017-07-16 15:27:05 -07001196 if (mBtNrecIsOff.exchange(btNrecIsOff) != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001197 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentd8365c52017-07-16 15:27:05 -07001198 mRecordThreads.valueAt(i)->checkBtNrec();
Eric Laurent59bd0da2011-08-01 09:52:20 -07001199 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001200 }
1201 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001202 String8 screenState;
1203 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
Mikhail Naganov00260b52016-10-13 12:54:24 -07001204 bool isOff = (screenState == AudioParameter::valueOff);
Eric Laurent81784c32012-11-19 14:55:58 -08001205 if (isOff != (AudioFlinger::mScreenState & 1)) {
1206 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001207 }
1208 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001209 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001210 }
1211
1212 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1213 // and the thread is exited once the lock is released
1214 sp<ThreadBase> thread;
1215 {
1216 Mutex::Autolock _l(mLock);
1217 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001218 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219 thread = checkRecordThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001220 if (thread == 0) {
1221 thread = checkMmapThread_l(ioHandle);
1222 }
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001223 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001224 // indicate output device change to all input threads for pre processing
1225 AudioParameter param = AudioParameter(keyValuePairs);
1226 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001227 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1228 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001229 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001230 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231 }
1232 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001233 if (thread != 0) {
1234 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235 }
1236 return BAD_VALUE;
1237}
1238
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001239String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001240{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001241 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1242 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001243
Eric Laurenta4c5a552012-03-29 10:12:40 -07001244 Mutex::Autolock _l(mLock);
1245
Glenn Kasten142f5192014-03-25 17:44:59 -07001246 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001247 String8 out_s8;
1248
Dima Zavin799a70e2011-04-18 16:57:27 -07001249 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001250 String8 s;
1251 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001252 {
1253 AutoMutex lock(mHardwareLock);
1254 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001255 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1256 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001257 mHardwareStatus = AUDIO_HW_IDLE;
1258 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001259 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001260 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001261 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262 }
1263
Eric Laurent6acd1d42017-01-04 14:23:29 -08001264 ThreadBase *thread = (ThreadBase *)checkPlaybackThread_l(ioHandle);
1265 if (thread == NULL) {
1266 thread = (ThreadBase *)checkRecordThread_l(ioHandle);
1267 if (thread == NULL) {
1268 thread = (ThreadBase *)checkMmapThread_l(ioHandle);
1269 if (thread == NULL) {
Mikhail Naganovaa165e52017-07-12 10:39:16 -07001270 return String8("");
Eric Laurent6acd1d42017-01-04 14:23:29 -08001271 }
1272 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001273 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001274 return thread->getParameters(keys);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275}
1276
Glenn Kastendd8104c2012-07-02 12:42:44 -07001277size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1278 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001279{
Eric Laurenta1884f92011-08-23 08:25:03 -07001280 status_t ret = initCheck();
1281 if (ret != NO_ERROR) {
1282 return 0;
1283 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001284 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001285 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001286 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001287 return 0;
1288 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001289
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001290 AutoMutex lock(mHardwareLock);
1291 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001292 audio_config_t config, proposed;
1293 memset(&proposed, 0, sizeof(proposed));
1294 proposed.sample_rate = sampleRate;
1295 proposed.channel_mask = channelMask;
1296 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001297
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001298 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001299 size_t frames;
1300 for (;;) {
1301 // Note: config is currently a const parameter for get_input_buffer_size()
1302 // but we use a copy from proposed in case config changes from the call.
1303 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001304 status_t result = dev->getInputBufferSize(&config, &frames);
1305 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001306 break; // hal success, config is the result
1307 }
1308 // change one parameter of the configuration each iteration to a more "common" value
1309 // to see if the device will support it.
1310 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1311 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1312 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1313 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1314 } else {
1315 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1316 "format %#x, channelMask 0x%X",
1317 sampleRate, format, channelMask);
1318 break; // retries failed, break out of loop with frames == 0.
1319 }
1320 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001321 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001322 if (frames > 0 && config.sample_rate != sampleRate) {
1323 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1324 }
1325 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001326}
1327
Glenn Kasten5f972c02014-01-13 09:59:31 -08001328uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001329{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001330 Mutex::Autolock _l(mLock);
1331
1332 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1333 if (recordThread != NULL) {
1334 return recordThread->getInputFramesLost();
1335 }
1336 return 0;
1337}
1338
1339status_t AudioFlinger::setVoiceVolume(float value)
1340{
Eric Laurenta1884f92011-08-23 08:25:03 -07001341 status_t ret = initCheck();
1342 if (ret != NO_ERROR) {
1343 return ret;
1344 }
1345
Mathias Agopian65ab4712010-07-14 17:59:35 -07001346 // check calling permissions
1347 if (!settingsAllowed()) {
1348 return PERMISSION_DENIED;
1349 }
1350
1351 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001352 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001353 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001354 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355 mHardwareStatus = AUDIO_HW_IDLE;
1356
1357 return ret;
1358}
1359
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001360status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001361 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001362{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001363 Mutex::Autolock _l(mLock);
1364
1365 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1366 if (playbackThread != NULL) {
1367 return playbackThread->getRenderPosition(halFrames, dspFrames);
1368 }
1369
1370 return BAD_VALUE;
1371}
1372
1373void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1374{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001375 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001376 if (client == 0) {
1377 return;
1378 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001379 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001380 {
1381 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001382 if (mNotificationClients.indexOfKey(pid) < 0) {
1383 sp<NotificationClient> notificationClient = new NotificationClient(this,
1384 client,
1385 pid);
1386 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001387
Eric Laurent021cf962014-05-13 10:18:14 -07001388 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001389
Marco Nelissen06b46062014-11-14 07:58:25 -08001390 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001391 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001392 }
1393 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001394
Eric Laurent021cf962014-05-13 10:18:14 -07001395 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001396 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001397 // the config change is always sent from playback or record threads to avoid deadlock
1398 // with AudioSystem::gLock
1399 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1400 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1401 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001403 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1404 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001405 }
1406}
1407
1408void AudioFlinger::removeNotificationClient(pid_t pid)
1409{
1410 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001411 {
1412 Mutex::Autolock _cl(mClientLock);
1413 mNotificationClients.removeItem(pid);
1414 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001415
Steve Block3856b092011-10-20 11:56:00 +01001416 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001417 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001418 bool removed = false;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001419 for (size_t i = 0; i < num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001420 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001421 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001422 if (ref->mPid == pid) {
1423 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001424 mAudioSessionRefs.removeAt(i);
1425 delete ref;
1426 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001427 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001428 } else {
1429 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001430 }
1431 }
1432 if (removed) {
1433 purgeStaleEffects_l();
1434 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435}
1436
Eric Laurent73e26b62015-04-27 16:55:58 -07001437void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001438 const sp<AudioIoDescriptor>& ioDesc,
1439 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440{
Eric Laurent021cf962014-05-13 10:18:14 -07001441 Mutex::Autolock _l(mClientLock);
1442 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001443 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001444 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1445 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1446 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447 }
1448}
1449
Eric Laurent021cf962014-05-13 10:18:14 -07001450// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001451void AudioFlinger::removeClient_l(pid_t pid)
1452{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001453 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001454 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001455 mClients.removeItem(pid);
1456}
1457
Eric Laurent717e1282012-06-29 16:36:52 -07001458// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001459sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1460 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001461{
1462 sp<PlaybackThread> thread;
1463
1464 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1465 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1466 ALOG_ASSERT(thread == 0);
1467 thread = mPlaybackThreads.valueAt(i);
1468 }
1469 }
1470
1471 return thread;
1472}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474
Mathias Agopian65ab4712010-07-14 17:59:35 -07001475
1476// ----------------------------------------------------------------------------
1477
1478AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1479 : RefBase(),
1480 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001481 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301483 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1484 heapSize *= 1024;
1485 if (!heapSize) {
1486 heapSize = kClientSharedHeapSizeBytes;
1487 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1488 // invalidated tracks
1489 if (!audioFlinger->isLowRamDevice()) {
1490 heapSize *= kClientSharedHeapSizeMultiplier;
1491 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001492 }
1493 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001494}
1495
Eric Laurent021cf962014-05-13 10:18:14 -07001496// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001497AudioFlinger::Client::~Client()
1498{
1499 mAudioFlinger->removeClient_l(mPid);
1500}
1501
Glenn Kasten435dbe62012-01-30 10:15:48 -08001502sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001503{
1504 return mMemoryDealer;
1505}
1506
1507// ----------------------------------------------------------------------------
1508
1509AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1510 const sp<IAudioFlingerClient>& client,
1511 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001512 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001513{
1514}
1515
1516AudioFlinger::NotificationClient::~NotificationClient()
1517{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001518}
1519
Glenn Kasten0f11b512014-01-31 16:18:54 -08001520void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521{
1522 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001523 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001524}
1525
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001526// ----------------------------------------------------------------------------
1527AudioFlinger::MediaLogNotifier::MediaLogNotifier()
1528 : mPendingRequests(false) {}
1529
1530
1531void AudioFlinger::MediaLogNotifier::requestMerge() {
1532 AutoMutex _l(mMutex);
1533 mPendingRequests = true;
1534 mCond.signal();
1535}
1536
1537bool AudioFlinger::MediaLogNotifier::threadLoop() {
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001538 // Should already have been checked, but just in case
1539 if (sMediaLogService == 0) {
1540 return false;
1541 }
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001542 // Wait until there are pending requests
1543 {
1544 AutoMutex _l(mMutex);
1545 mPendingRequests = false; // to ignore past requests
1546 while (!mPendingRequests) {
1547 mCond.wait(mMutex);
1548 // TODO may also need an exitPending check
1549 }
1550 mPendingRequests = false;
1551 }
1552 // Execute the actual MediaLogService binder call and ignore extra requests for a while
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001553 sMediaLogService->requestMergeWakeup();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001554 usleep(kPostTriggerSleepPeriod);
1555 return true;
1556}
1557
1558void AudioFlinger::requestLogMerge() {
1559 mMediaLogNotifier->requestMerge();
1560}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001561
1562// ----------------------------------------------------------------------------
1563
1564sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001565 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001566 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001567 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001568 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001569 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001570 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -07001571 audio_input_flags_t *flags,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001572 pid_t pid,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001573 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001574 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001575 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001576 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001577 sp<IMemory>& cblk,
1578 sp<IMemory>& buffers,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001579 status_t *status,
1580 audio_port_handle_t portId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001581{
1582 sp<RecordThread::RecordTrack> recordTrack;
1583 sp<RecordHandle> recordHandle;
1584 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001585 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001586 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001587
Glenn Kastend776ac62014-05-07 09:16:09 -07001588 cblk.clear();
1589 buffers.clear();
1590
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001591 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001592 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1593 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001594 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001595 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1596 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001597 updatePid = true;
1598 }
1599
1600 if (updatePid) {
1601 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
1602 ALOGW_IF(pid != -1 && pid != callingPid,
1603 "%s uid %d pid %d tried to pass itself off as pid %d",
1604 __func__, callingUid, callingPid, pid);
1605 pid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001606 }
1607
Mathias Agopian65ab4712010-07-14 17:59:35 -07001608 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001609 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001610 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001611 lStatus = PERMISSION_DENIED;
1612 goto Exit;
1613 }
1614
Glenn Kasten53b5d092014-02-05 10:00:23 -08001615 // further sample rate checks are performed by createRecordTrack_l()
1616 if (sampleRate == 0) {
1617 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1618 lStatus = BAD_VALUE;
1619 goto Exit;
1620 }
1621
Andy Hung6770c6f2015-04-07 13:43:36 -07001622 // we don't yet support anything other than linear PCM
1623 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001624 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001625 lStatus = BAD_VALUE;
1626 goto Exit;
1627 }
1628
Glenn Kasten53b5d092014-02-05 10:00:23 -08001629 // further channel mask checks are performed by createRecordTrack_l()
1630 if (!audio_is_input_channel(channelMask)) {
1631 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1632 lStatus = BAD_VALUE;
1633 goto Exit;
1634 }
1635
Glenn Kasten05997e22014-03-13 15:08:33 -07001636 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001637 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001638 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001639 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001640 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001641 lStatus = BAD_VALUE;
1642 goto Exit;
1643 }
1644
Eric Laurent021cf962014-05-13 10:18:14 -07001645 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001646
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001647 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001648 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1649 lStatus = BAD_VALUE;
1650 goto Exit;
1651 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001652 lSessionId = *sessionId;
1653 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001654 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001655 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001656 if (sessionId != NULL) {
1657 *sessionId = lSessionId;
1658 }
1659 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001660 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001661
Glenn Kasten1879fff2012-07-11 15:36:59 -07001662 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001663 frameCount, lSessionId, notificationFrames,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001664 clientUid, flags, tid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001665 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001666
1667 if (lStatus == NO_ERROR) {
1668 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1669 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001670 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001671 if (chain != 0) {
1672 Mutex::Autolock _l(thread->mLock);
1673 thread->addEffectChain_l(chain);
1674 }
1675 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001676 }
Glenn Kastene198c362013-08-13 09:13:36 -07001677
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001678 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001679 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001680 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001681 // Don't hold mClientLock when releasing the reference on the track as the
1682 // destructor will acquire it.
1683 {
1684 Mutex::Autolock _cl(mClientLock);
1685 client.clear();
1686 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001687 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001688 goto Exit;
1689 }
1690
Glenn Kastend776ac62014-05-07 09:16:09 -07001691 cblk = recordTrack->getCblk();
1692 buffers = recordTrack->getBuffers();
1693
Glenn Kasten2fc14732013-08-05 14:58:14 -07001694 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001695 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001696
1697Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001698 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001699 return recordHandle;
1700}
1701
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001702
1703
Mathias Agopian65ab4712010-07-14 17:59:35 -07001704// ----------------------------------------------------------------------------
1705
Eric Laurenta4c5a552012-03-29 10:12:40 -07001706audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1707{
Eric Laurent44622db2014-08-01 19:00:33 -07001708 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001709 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001710 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001711 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001712 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001713 }
1714 Mutex::Autolock _l(mLock);
1715 return loadHwModule_l(name);
1716}
1717
1718// loadHwModule_l() must be called with AudioFlinger::mLock held
1719audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1720{
1721 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1722 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1723 ALOGW("loadHwModule() module %s already loaded", name);
1724 return mAudioHwDevs.keyAt(i);
1725 }
1726 }
1727
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001728 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001729
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001730 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001731 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001732 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001733 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001734 }
1735
1736 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001737 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07001738 mHardwareStatus = AUDIO_HW_IDLE;
1739 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001740 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001741 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001742 }
1743
John Grossmanee578c02012-07-23 17:05:46 -07001744 // Check and cache this HAL's level of support for master mute and master
1745 // volume. If this is the first HAL opened, and it supports the get
1746 // methods, use the initial values provided by the HAL as the current
1747 // master mute and volume settings.
1748
1749 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1750 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001751 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001752
1753 if (0 == mAudioHwDevs.size()) {
1754 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001755 float mv;
1756 if (OK == dev->getMasterVolume(&mv)) {
1757 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07001758 }
1759
1760 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001761 bool mm;
1762 if (OK == dev->getMasterMute(&mm)) {
1763 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07001764 }
1765 }
1766
Eric Laurenta4c5a552012-03-29 10:12:40 -07001767 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001768 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07001769 flags = static_cast<AudioHwDevice::Flags>(flags |
1770 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1771 }
1772
1773 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001774 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07001775 flags = static_cast<AudioHwDevice::Flags>(flags |
1776 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1777 }
1778
Eric Laurenta4c5a552012-03-29 10:12:40 -07001779 mHardwareStatus = AUDIO_HW_IDLE;
1780 }
1781
Glenn Kastena13cde92016-03-28 15:26:02 -07001782 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001783 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001784
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001785 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001786
1787 return handle;
1788
1789}
1790
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001791// ----------------------------------------------------------------------------
1792
Glenn Kasten3b16c762012-11-14 08:44:39 -08001793uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001794{
1795 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001796 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001797 return thread != NULL ? thread->sampleRate() : 0;
1798}
1799
Glenn Kastene33054e2012-11-14 12:54:39 -08001800size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001801{
1802 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001803 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001804 return thread != NULL ? thread->frameCountHAL() : 0;
1805}
1806
1807// ----------------------------------------------------------------------------
1808
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001809status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1810{
1811 uid_t uid = IPCThreadState::self()->getCallingUid();
1812 if (uid != AID_SYSTEM) {
1813 return PERMISSION_DENIED;
1814 }
1815 Mutex::Autolock _l(mLock);
1816 if (mIsDeviceTypeKnown) {
1817 return INVALID_OPERATION;
1818 }
1819 mIsLowRamDevice = isLowRamDevice;
1820 mIsDeviceTypeKnown = true;
1821 return NO_ERROR;
1822}
1823
Eric Laurent93c3d412014-08-01 14:48:35 -07001824audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1825{
1826 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001827
1828 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1829 if (index >= 0) {
1830 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1831 mHwAvSyncIds.valueAt(index), sessionId);
1832 return mHwAvSyncIds.valueAt(index);
1833 }
1834
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001835 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07001836 if (dev == NULL) {
1837 return AUDIO_HW_SYNC_INVALID;
1838 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001839 String8 reply;
1840 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001841 if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001842 param = AudioParameter(reply);
1843 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001844
1845 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001846 if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001847 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1848 return AUDIO_HW_SYNC_INVALID;
1849 }
1850
1851 // allow only one session for a given HW A/V sync ID.
1852 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1853 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1854 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1855 value, mHwAvSyncIds.keyAt(i));
1856 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001857 break;
1858 }
1859 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001860
1861 mHwAvSyncIds.add(sessionId, value);
1862
1863 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1864 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1865 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07001866 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001867 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001868 param.addInt(String8(AudioParameter::keyStreamHwAvSync), value);
Eric Laurentfa90e842014-10-17 18:12:31 -07001869 thread->setParameters(param.toString());
1870 break;
1871 }
1872 }
1873
1874 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1875 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001876}
1877
Eric Laurent72e3f392015-05-20 14:43:50 -07001878status_t AudioFlinger::systemReady()
1879{
1880 Mutex::Autolock _l(mLock);
1881 ALOGI("%s", __FUNCTION__);
1882 if (mSystemReady) {
1883 ALOGW("%s called twice", __FUNCTION__);
1884 return NO_ERROR;
1885 }
1886 mSystemReady = true;
1887 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1888 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1889 thread->systemReady();
1890 }
1891 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1892 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1893 thread->systemReady();
1894 }
1895 return NO_ERROR;
1896}
1897
Eric Laurentfa90e842014-10-17 18:12:31 -07001898// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1899void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1900{
1901 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1902 if (index >= 0) {
1903 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1904 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1905 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001906 param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId);
Eric Laurentfa90e842014-10-17 18:12:31 -07001907 thread->setParameters(param.toString());
1908 }
1909}
1910
1911
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001912// ----------------------------------------------------------------------------
1913
Eric Laurent83b88082014-06-20 18:31:16 -07001914
Eric Laurent6acd1d42017-01-04 14:23:29 -08001915sp<AudioFlinger::ThreadBase> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001916 audio_io_handle_t *output,
1917 audio_config_t *config,
1918 audio_devices_t devices,
1919 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001920 audio_output_flags_t flags)
1921{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001922 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001923 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001924 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001925 }
1926
Eric Laurentcf2c0212014-07-25 16:20:43 -07001927 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001928 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1929 } else {
1930 // Audio Policy does not currently request a specific output handle.
1931 // If this is ever needed, see openInput_l() for example code.
1932 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1933 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001934 }
Eric Laurent83b88082014-06-20 18:31:16 -07001935
1936 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1937
Eric Laurent83b88082014-06-20 18:31:16 -07001938 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001939 // This if statement allows overriding the audio policy settings
1940 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1941 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1942 // Check only for Normal Mixing mode
1943 if (kEnableExtendedPrecision) {
1944 // Specify format (uncomment one below to choose)
1945 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1946 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1947 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1948 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001949 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001950 }
1951 if (kEnableExtendedChannels) {
1952 // Specify channel mask (uncomment one below to choose)
1953 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1954 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1955 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1956 }
Eric Laurent83b88082014-06-20 18:31:16 -07001957 }
1958
Phil Burk062e67a2015-02-11 13:40:50 -08001959 AudioStreamOut *outputStream = NULL;
1960 status_t status = outHwDev->openOutputStream(
1961 &outputStream,
1962 *output,
1963 devices,
1964 flags,
1965 config,
1966 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001967
1968 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001969
Phil Burk062e67a2015-02-11 13:40:50 -08001970 if (status == NO_ERROR) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001971 if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
1972 sp<MmapPlaybackThread> thread =
1973 new MmapPlaybackThread(this, *output, outHwDev, outputStream,
1974 devices, AUDIO_DEVICE_NONE, mSystemReady);
1975 mMmapThreads.add(*output, thread);
1976 ALOGV("openOutput_l() created mmap playback thread: ID %d thread %p",
1977 *output, thread.get());
1978 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07001979 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001980 sp<PlaybackThread> thread;
1981 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1982 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
1983 ALOGV("openOutput_l() created offload output: ID %d thread %p",
1984 *output, thread.get());
1985 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1986 || !isValidPcmSinkFormat(config->format)
1987 || !isValidPcmSinkChannelMask(config->channel_mask)) {
1988 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
1989 ALOGV("openOutput_l() created direct output: ID %d thread %p",
1990 *output, thread.get());
1991 } else {
1992 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
1993 ALOGV("openOutput_l() created mixer output: ID %d thread %p",
1994 *output, thread.get());
1995 }
1996 mPlaybackThreads.add(*output, thread);
1997 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07001998 }
Eric Laurent83b88082014-06-20 18:31:16 -07001999 }
2000
2001 return 0;
2002}
2003
Eric Laurentcf2c0212014-07-25 16:20:43 -07002004status_t AudioFlinger::openOutput(audio_module_handle_t module,
2005 audio_io_handle_t *output,
2006 audio_config_t *config,
2007 audio_devices_t *devices,
2008 const String8& address,
2009 uint32_t *latencyMs,
2010 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002011{
Glenn Kastend3bb6452016-12-05 18:14:37 -08002012 ALOGI("openOutput() this %p, module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, "
2013 "flags %x",
Eric Laurent6acd1d42017-01-04 14:23:29 -08002014 this, module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002015 (devices != NULL) ? *devices : 0,
2016 config->sample_rate,
2017 config->format,
2018 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002019 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002020
Yunlian Jiang32ba9862017-01-31 15:55:00 -08002021 if (devices == NULL || *devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002022 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002023 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002024
Mathias Agopian65ab4712010-07-14 17:59:35 -07002025 Mutex::Autolock _l(mLock);
2026
Eric Laurent6acd1d42017-01-04 14:23:29 -08002027 sp<ThreadBase> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002028 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002029 if ((flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) {
2030 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
2031 *latencyMs = playbackThread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002032
Eric Laurent6acd1d42017-01-04 14:23:29 -08002033 // notify client processes of the new output creation
2034 playbackThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002035
Eric Laurent6acd1d42017-01-04 14:23:29 -08002036 // the first primary output opened designates the primary hw device
2037 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -08002038 ALOGI("Using module %d as the primary audio interface", module);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002039 mPrimaryHardwareDev = playbackThread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002040
Eric Laurent6acd1d42017-01-04 14:23:29 -08002041 AutoMutex lock(mHardwareLock);
2042 mHardwareStatus = AUDIO_HW_SET_MODE;
2043 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
2044 mHardwareStatus = AUDIO_HW_IDLE;
2045 }
2046 } else {
2047 MmapThread *mmapThread = (MmapThread *)thread.get();
2048 mmapThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002049 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002050 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002051 }
2052
Eric Laurentcf2c0212014-07-25 16:20:43 -07002053 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002054}
2055
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002056audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
2057 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002058{
2059 Mutex::Autolock _l(mLock);
2060 MixerThread *thread1 = checkMixerThread_l(output1);
2061 MixerThread *thread2 = checkMixerThread_l(output2);
2062
2063 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002064 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
2065 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07002066 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002067 }
2068
Glenn Kasteneeecb982016-02-26 10:44:04 -08002069 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07002070 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002071 thread->addOutputTrack(thread2);
2072 mPlaybackThreads.add(id, thread);
2073 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002074 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002075 return id;
2076}
2077
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002078status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002079{
Glenn Kastend96c5722012-04-25 13:44:49 -07002080 return closeOutput_nonvirtual(output);
2081}
2082
2083status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
2084{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002085 // keep strong reference on the playback thread so that
2086 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002087 sp<PlaybackThread> playbackThread;
2088 sp<MmapPlaybackThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002089 {
2090 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002091 playbackThread = checkPlaybackThread_l(output);
2092 if (playbackThread != NULL) {
2093 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002094
Eric Laurent6acd1d42017-01-04 14:23:29 -08002095 if (playbackThread->type() == ThreadBase::MIXER) {
2096 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2097 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
2098 DuplicatingThread *dupThread =
2099 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
2100 dupThread->removeOutputTrack((MixerThread *)playbackThread.get());
2101 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002102 }
2103 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002104
2105
Eric Laurent6acd1d42017-01-04 14:23:29 -08002106 mPlaybackThreads.removeItem(output);
2107 // save all effects to the default thread
2108 if (mPlaybackThreads.size()) {
2109 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
2110 if (dstThread != NULL) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002111 // audioflinger lock is held so order of thread lock acquisition doesn't matter
Eric Laurent6acd1d42017-01-04 14:23:29 -08002112 Mutex::Autolock _dl(dstThread->mLock);
2113 Mutex::Autolock _sl(playbackThread->mLock);
2114 Vector< sp<EffectChain> > effectChains = playbackThread->getEffectChains_l();
2115 for (size_t i = 0; i < effectChains.size(); i ++) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002116 moveEffectChain_l(effectChains[i]->sessionId(), playbackThread.get(),
2117 dstThread, true);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002118 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002119 }
2120 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002121 } else {
2122 mmapThread = (MmapPlaybackThread *)checkMmapThread_l(output);
2123 if (mmapThread == 0) {
2124 return BAD_VALUE;
2125 }
2126 mMmapThreads.removeItem(output);
Phil Burk7f6b40d2017-02-09 13:18:38 -08002127 ALOGD("closing mmapThread %p", mmapThread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002128 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002129 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2130 ioDesc->mIoHandle = output;
2131 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002132 }
Glenn Kastenb28686f2012-01-06 08:39:38 -08002133 // The thread entity (active unit of execution) is no longer running here,
2134 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002135
Eric Laurent6acd1d42017-01-04 14:23:29 -08002136 if (playbackThread != 0) {
2137 playbackThread->exit();
2138 if (!playbackThread->isDuplicating()) {
2139 closeOutputFinish(playbackThread);
2140 }
2141 } else if (mmapThread != 0) {
Phil Burk7f6b40d2017-02-09 13:18:38 -08002142 ALOGD("mmapThread exit()");
Eric Laurent6acd1d42017-01-04 14:23:29 -08002143 mmapThread->exit();
2144 AudioStreamOut *out = mmapThread->clearOutput();
2145 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2146 // from now on thread->mOutput is NULL
2147 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002148 }
2149 return NO_ERROR;
2150}
2151
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002152void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002153{
2154 AudioStreamOut *out = thread->clearOutput();
2155 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2156 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07002157 delete out;
2158}
2159
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002160void AudioFlinger::closeOutputInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002161{
2162 mPlaybackThreads.removeItem(thread->mId);
2163 thread->exit();
2164 closeOutputFinish(thread);
2165}
2166
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002167status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002168{
2169 Mutex::Autolock _l(mLock);
2170 PlaybackThread *thread = checkPlaybackThread_l(output);
2171
2172 if (thread == NULL) {
2173 return BAD_VALUE;
2174 }
2175
Steve Block3856b092011-10-20 11:56:00 +01002176 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002177 thread->suspend();
2178
2179 return NO_ERROR;
2180}
2181
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002182status_t AudioFlinger::restoreOutput(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("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192
2193 thread->restore();
2194
2195 return NO_ERROR;
2196}
2197
Eric Laurentcf2c0212014-07-25 16:20:43 -07002198status_t AudioFlinger::openInput(audio_module_handle_t module,
2199 audio_io_handle_t *input,
2200 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002201 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002202 const String8& address,
2203 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002204 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002205{
Eric Laurent83b88082014-06-20 18:31:16 -07002206 Mutex::Autolock _l(mLock);
2207
Glenn Kastene7d66712015-03-05 13:46:52 -08002208 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002209 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002210 }
2211
Eric Laurent6acd1d42017-01-04 14:23:29 -08002212 sp<ThreadBase> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002213
2214 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002215 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002216 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002217 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002219 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002220}
Dima Zavin799a70e2011-04-18 16:57:27 -07002221
Eric Laurent6acd1d42017-01-04 14:23:29 -08002222sp<AudioFlinger::ThreadBase> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002223 audio_io_handle_t *input,
2224 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002225 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002226 const String8& address,
2227 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002228 audio_input_flags_t flags)
2229{
Glenn Kastene7d66712015-03-05 13:46:52 -08002230 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002231 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002232 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002233 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002234 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002235
Glenn Kasteneeecb982016-02-26 10:44:04 -08002236 // Audio Policy can request a specific handle for hardware hotword.
2237 // The goal here is not to re-open an already opened input.
2238 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002239 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002240 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2241 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2242 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2243 return 0;
2244 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2245 // This should not happen in a transient state with current design.
2246 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2247 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002248 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002249
Eric Laurentcf2c0212014-07-25 16:20:43 -07002250 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002251 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002252 sp<StreamInHalInterface> inStream;
2253 status_t status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002254 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Mikhail Naganovf558e022016-11-14 17:45:17 -08002255 ALOGV("openInput_l() openInputStream returned input %p, devices %x, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002256 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002257 inStream.get(),
Mikhail Naganovf558e022016-11-14 17:45:17 -08002258 devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002259 halconfig.sample_rate,
2260 halconfig.format,
2261 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002262 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002263 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002264
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002265 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002266 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002267 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002268 audio_is_linear_pcm(config->format) &&
2269 audio_is_linear_pcm(halconfig.format) &&
2270 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002271 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2272 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002273 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002274 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002275 inStream.clear();
2276 status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002277 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002278 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002279 }
2280
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002281 if (status == NO_ERROR && inStream != 0) {
Eric Laurent05067782016-06-01 18:27:28 -07002282 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002283 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) {
2284 sp<MmapCaptureThread> thread =
2285 new MmapCaptureThread(this, *input,
2286 inHwDev, inputStream,
2287 primaryOutputDevice_l(), devices, mSystemReady);
2288 mMmapThreads.add(*input, thread);
Glenn Kastend3bb6452016-12-05 18:14:37 -08002289 ALOGV("openInput_l() created mmap capture thread: ID %d thread %p", *input,
2290 thread.get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002291 return thread;
2292 } else {
Glenn Kasten46909e72013-02-26 09:20:22 -08002293#ifdef TEE_SINK
Eric Laurent6acd1d42017-01-04 14:23:29 -08002294 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2295 // or (re-)create if current Pipe is idle and does not match the new format
2296 sp<NBAIO_Sink> teeSink;
2297 enum {
2298 TEE_SINK_NO, // don't copy input
2299 TEE_SINK_NEW, // copy input using a new pipe
2300 TEE_SINK_OLD, // copy input using an existing pipe
2301 } kind;
2302 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2303 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
2304 if (!mTeeSinkInputEnabled) {
2305 kind = TEE_SINK_NO;
2306 } else if (!Format_isValid(format)) {
2307 kind = TEE_SINK_NO;
2308 } else if (mRecordTeeSink == 0) {
2309 kind = TEE_SINK_NEW;
2310 } else if (mRecordTeeSink->getStrongCount() != 1) {
2311 kind = TEE_SINK_NO;
2312 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
2313 kind = TEE_SINK_OLD;
2314 } else {
2315 kind = TEE_SINK_NEW;
2316 }
2317 switch (kind) {
2318 case TEE_SINK_NEW: {
2319 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
2320 size_t numCounterOffers = 0;
2321 const NBAIO_Format offers[1] = {format};
2322 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2323 ALOG_ASSERT(index == 0);
2324 PipeReader *pipeReader = new PipeReader(*pipe);
2325 numCounterOffers = 0;
2326 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2327 ALOG_ASSERT(index == 0);
2328 mRecordTeeSink = pipe;
2329 mRecordTeeSource = pipeReader;
2330 teeSink = pipe;
2331 }
2332 break;
2333 case TEE_SINK_OLD:
2334 teeSink = mRecordTeeSink;
2335 break;
2336 case TEE_SINK_NO:
2337 default:
2338 break;
2339 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002340#endif
Eric Laurent6acd1d42017-01-04 14:23:29 -08002341
2342 // Start record thread
2343 // RecordThread requires both input and output device indication to forward to audio
2344 // pre processing modules
2345 sp<RecordThread> thread = new RecordThread(this,
2346 inputStream,
2347 *input,
2348 primaryOutputDevice_l(),
2349 devices,
2350 mSystemReady
2351#ifdef TEE_SINK
2352 , teeSink
2353#endif
2354 );
2355 mRecordThreads.add(*input, thread);
2356 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
2357 return thread;
2358 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002359 }
2360
Eric Laurentcf2c0212014-07-25 16:20:43 -07002361 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002362 return 0;
2363}
2364
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002365status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002366{
Glenn Kastend96c5722012-04-25 13:44:49 -07002367 return closeInput_nonvirtual(input);
2368}
2369
2370status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2371{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002372 // keep strong reference on the record thread so that
2373 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002374 sp<RecordThread> recordThread;
2375 sp<MmapCaptureThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002376 {
2377 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002378 recordThread = checkRecordThread_l(input);
2379 if (recordThread != 0) {
2380 ALOGV("closeInput() %d", input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002381
Eric Laurent6acd1d42017-01-04 14:23:29 -08002382 // If we still have effect chains, it means that a client still holds a handle
2383 // on at least one effect. We must either move the chain to an existing thread with the
2384 // same session ID or put it aside in case a new record thread is opened for a
2385 // new capture on the same session
2386 sp<EffectChain> chain;
2387 {
2388 Mutex::Autolock _sl(recordThread->mLock);
2389 Vector< sp<EffectChain> > effectChains = recordThread->getEffectChains_l();
2390 // Note: maximum one chain per record thread
2391 if (effectChains.size() != 0) {
2392 chain = effectChains[0];
Eric Laurent1b928682014-10-02 19:41:47 -07002393 }
2394 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002395 if (chain != 0) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002396 // first check if a record thread is already opened with a client on same session.
Eric Laurent6acd1d42017-01-04 14:23:29 -08002397 // This should only happen in case of overlap between one thread tear down and the
2398 // creation of its replacement
2399 size_t i;
2400 for (i = 0; i < mRecordThreads.size(); i++) {
2401 sp<RecordThread> t = mRecordThreads.valueAt(i);
2402 if (t == recordThread) {
2403 continue;
2404 }
2405 if (t->hasAudioSession(chain->sessionId()) != 0) {
2406 Mutex::Autolock _l(t->mLock);
2407 ALOGV("closeInput() found thread %d for effect session %d",
2408 t->id(), chain->sessionId());
2409 t->addEffectChain_l(chain);
2410 break;
2411 }
2412 }
Glenn Kastend3bb6452016-12-05 18:14:37 -08002413 // put the chain aside if we could not find a record thread with the same session id
Eric Laurent6acd1d42017-01-04 14:23:29 -08002414 if (i == mRecordThreads.size()) {
2415 putOrphanEffectChain_l(chain);
2416 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002417 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002418 mRecordThreads.removeItem(input);
2419 } else {
2420 mmapThread = (MmapCaptureThread *)checkMmapThread_l(input);
2421 if (mmapThread == 0) {
2422 return BAD_VALUE;
2423 }
2424 mMmapThreads.removeItem(input);
Eric Laurentaaa44472014-09-12 17:41:50 -07002425 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002426 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2427 ioDesc->mIoHandle = input;
2428 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002429 }
Eric Laurent83b88082014-06-20 18:31:16 -07002430 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2431 // we have a different lock for notification client
Eric Laurent6acd1d42017-01-04 14:23:29 -08002432 if (recordThread != 0) {
2433 closeInputFinish(recordThread);
2434 } else if (mmapThread != 0) {
2435 mmapThread->exit();
2436 AudioStreamIn *in = mmapThread->clearInput();
2437 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
2438 // from now on thread->mInput is NULL
2439 delete in;
2440 }
Eric Laurent83b88082014-06-20 18:31:16 -07002441 return NO_ERROR;
2442}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002443
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002444void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002445{
2446 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002447 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002448 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002449 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002450 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002451}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002452
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002453void AudioFlinger::closeInputInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002454{
2455 mRecordThreads.removeItem(thread->mId);
2456 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002457}
2458
Glenn Kastend2304db2014-02-03 07:40:31 -08002459status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002460{
2461 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002462 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002463
2464 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2465 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002466 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002467 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002468 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2469 mMmapThreads[i]->invalidateTracks(stream);
2470 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002471 return NO_ERROR;
2472}
2473
2474
Glenn Kasteneeecb982016-02-26 10:44:04 -08002475audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002476{
Glenn Kasten9d003132016-04-06 14:38:09 -07002477 // This is a binder API, so a malicious client could pass in a bad parameter.
2478 // Check for that before calling the internal API nextUniqueId().
2479 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2480 ALOGE("newAudioUniqueId invalid use %d", use);
2481 return AUDIO_UNIQUE_ID_ALLOCATE;
2482 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002483 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002484}
2485
Glenn Kastend848eb42016-03-08 13:42:11 -08002486void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002487{
2488 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002489 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002490 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2491 if (pid != -1 && (caller == getpid_cached)) {
2492 caller = pid;
2493 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002494
Eric Laurent021cf962014-05-13 10:18:14 -07002495 {
2496 Mutex::Autolock _cl(mClientLock);
2497 // Ignore requests received from processes not known as notification client. The request
2498 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2499 // called from a different pid leaving a stale session reference. Also we don't know how
2500 // to clear this reference if the client process dies.
2501 if (mNotificationClients.indexOfKey(caller) < 0) {
2502 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2503 return;
2504 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002505 }
2506
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002507 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002508 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002509 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002510 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2511 ref->mCnt++;
2512 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002513 return;
2514 }
2515 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002516 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2517 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002518}
2519
Glenn Kastend848eb42016-03-08 13:42:11 -08002520void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002521{
2522 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002523 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002524 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2525 if (pid != -1 && (caller == getpid_cached)) {
2526 caller = pid;
2527 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002528 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002529 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002530 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002531 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2532 ref->mCnt--;
2533 ALOGV(" decremented refcount to %d", ref->mCnt);
2534 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002535 mAudioSessionRefs.removeAt(i);
2536 delete ref;
2537 purgeStaleEffects_l();
2538 }
2539 return;
2540 }
2541 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002542 // If the caller is mediaserver it is likely that the session being released was acquired
2543 // on behalf of a process not in notification clients and we ignore the warning.
2544 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002545}
2546
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002547bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
2548{
2549 size_t num = mAudioSessionRefs.size();
2550 for (size_t i = 0; i < num; i++) {
2551 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2552 if (ref->mSessionid == audioSession) {
2553 return true;
2554 }
2555 }
2556 return false;
2557}
2558
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002559void AudioFlinger::purgeStaleEffects_l() {
2560
Steve Block3856b092011-10-20 11:56:00 +01002561 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002562
2563 Vector< sp<EffectChain> > chains;
2564
2565 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2566 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2567 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2568 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002569 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2570 chains.push(ec);
2571 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002572 }
2573 }
2574 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2575 sp<RecordThread> t = mRecordThreads.valueAt(i);
2576 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2577 sp<EffectChain> ec = t->mEffectChains[j];
2578 chains.push(ec);
2579 }
2580 }
2581
2582 for (size_t i = 0; i < chains.size(); i++) {
2583 sp<EffectChain> ec = chains[i];
2584 int sessionid = ec->sessionId();
2585 sp<ThreadBase> t = ec->mThread.promote();
2586 if (t == 0) {
2587 continue;
2588 }
2589 size_t numsessionrefs = mAudioSessionRefs.size();
2590 bool found = false;
2591 for (size_t k = 0; k < numsessionrefs; k++) {
2592 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002593 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002594 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002595 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002596 found = true;
2597 break;
2598 }
2599 }
2600 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002601 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002602 // remove all effects from the chain
2603 while (ec->mEffects.size()) {
2604 sp<EffectModule> effect = ec->mEffects[0];
2605 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002606 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002607 if (effect->purgeHandles()) {
2608 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002609 }
2610 AudioSystem::unregisterEffect(effect->id());
2611 }
2612 }
2613 }
2614 return;
2615}
2616
Glenn Kasteneeecb982016-02-26 10:44:04 -08002617// checkThread_l() must be called with AudioFlinger::mLock held
2618AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2619{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002620 ThreadBase *thread = checkMmapThread_l(ioHandle);
2621 if (thread == 0) {
2622 switch (audio_unique_id_get_use(ioHandle)) {
2623 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2624 thread = checkPlaybackThread_l(ioHandle);
2625 break;
2626 case AUDIO_UNIQUE_ID_USE_INPUT:
2627 thread = checkRecordThread_l(ioHandle);
2628 break;
2629 default:
2630 break;
2631 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002632 }
2633 return thread;
2634}
2635
Mathias Agopian65ab4712010-07-14 17:59:35 -07002636// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002637AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002638{
Glenn Kastena1117922012-01-26 10:53:32 -08002639 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002640}
2641
2642// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002643AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002644{
2645 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002646 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002647}
2648
2649// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002650AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002651{
Glenn Kastena1117922012-01-26 10:53:32 -08002652 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002653}
2654
Eric Laurent6acd1d42017-01-04 14:23:29 -08002655// checkMmapThread_l() must be called with AudioFlinger::mLock held
2656AudioFlinger::MmapThread *AudioFlinger::checkMmapThread_l(audio_io_handle_t io) const
2657{
2658 return mMmapThreads.valueFor(io).get();
2659}
2660
2661
2662// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
2663AudioFlinger::VolumeInterface *AudioFlinger::getVolumeInterface_l(audio_io_handle_t output) const
2664{
Eric Laurent7459b902017-04-19 18:10:41 -07002665 VolumeInterface *volumeInterface = mPlaybackThreads.valueFor(output).get();
Eric Laurent6acd1d42017-01-04 14:23:29 -08002666 if (volumeInterface == nullptr) {
2667 MmapThread *mmapThread = mMmapThreads.valueFor(output).get();
2668 if (mmapThread != nullptr) {
2669 if (mmapThread->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07002670 MmapPlaybackThread *mmapPlaybackThread =
2671 static_cast<MmapPlaybackThread *>(mmapThread);
2672 volumeInterface = mmapPlaybackThread;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002673 }
2674 }
2675 }
2676 return volumeInterface;
2677}
2678
2679Vector <AudioFlinger::VolumeInterface *> AudioFlinger::getAllVolumeInterfaces_l() const
2680{
2681 Vector <VolumeInterface *> volumeInterfaces;
2682 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7459b902017-04-19 18:10:41 -07002683 volumeInterfaces.add(mPlaybackThreads.valueAt(i).get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002684 }
2685 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2686 if (mMmapThreads.valueAt(i)->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07002687 MmapPlaybackThread *mmapPlaybackThread =
2688 static_cast<MmapPlaybackThread *>(mMmapThreads.valueAt(i).get());
2689 volumeInterfaces.add(mmapPlaybackThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002690 }
2691 }
2692 return volumeInterfaces;
2693}
2694
Glenn Kasteneeecb982016-02-26 10:44:04 -08002695audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002696{
Glenn Kasten9d003132016-04-06 14:38:09 -07002697 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002698 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002699 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2700 for (int retry = 0; retry < maxRetries; retry++) {
2701 // The cast allows wraparound from max positive to min negative instead of abort
2702 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2703 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2704 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2705 // allow wrap by skipping 0 and -1 for session ids
2706 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2707 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2708 return (audio_unique_id_t) (base | use);
2709 }
2710 }
2711 // We have no way of recovering from wraparound
2712 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2713 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002714}
2715
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002716AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002717{
2718 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2719 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002720 if(thread->isDuplicating()) {
2721 continue;
2722 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002723 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002724 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002725 return thread;
2726 }
2727 }
2728 return NULL;
2729}
2730
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002731audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002732{
2733 PlaybackThread *thread = primaryPlaybackThread_l();
2734
2735 if (thread == NULL) {
2736 return 0;
2737 }
2738
Eric Laurentf1c04f92012-08-28 14:26:53 -07002739 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002740}
2741
Glenn Kastena7335632016-06-09 17:09:53 -07002742AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
2743{
2744 size_t minFrameCount = 0;
2745 PlaybackThread *minThread = NULL;
2746 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2747 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2748 if (!thread->isDuplicating()) {
2749 size_t frameCount = thread->frameCountHAL();
2750 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
2751 (frameCount == minFrameCount && thread->hasFastMixer() &&
2752 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
2753 minFrameCount = frameCount;
2754 minThread = thread;
2755 }
2756 }
2757 }
2758 return minThread;
2759}
2760
Eric Laurenta011e352012-03-29 15:51:43 -07002761sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002762 audio_session_t triggerSession,
2763 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002764 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002765 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002766{
2767 Mutex::Autolock _l(mLock);
2768
2769 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2770 status_t playStatus = NAME_NOT_FOUND;
2771 status_t recStatus = NAME_NOT_FOUND;
2772 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2773 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2774 if (playStatus == NO_ERROR) {
2775 return event;
2776 }
2777 }
2778 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2779 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2780 if (recStatus == NO_ERROR) {
2781 return event;
2782 }
2783 }
2784 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2785 mPendingSyncEvents.add(event);
2786 } else {
2787 ALOGV("createSyncEvent() invalid event %d", event->type());
2788 event.clear();
2789 }
2790 return event;
2791}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002792
Mathias Agopian65ab4712010-07-14 17:59:35 -07002793// ----------------------------------------------------------------------------
2794// Effect management
2795// ----------------------------------------------------------------------------
2796
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002797sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
2798 return mEffectsFactoryHal;
2799}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002800
Glenn Kastenf587ba52012-01-26 16:25:10 -08002801status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002802{
2803 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002804 if (mEffectsFactoryHal.get()) {
2805 return mEffectsFactoryHal->queryNumberEffects(numEffects);
2806 } else {
2807 return -ENODEV;
2808 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002809}
2810
Glenn Kastenf587ba52012-01-26 16:25:10 -08002811status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002812{
2813 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002814 if (mEffectsFactoryHal.get()) {
2815 return mEffectsFactoryHal->getDescriptor(index, descriptor);
2816 } else {
2817 return -ENODEV;
2818 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002819}
2820
Glenn Kasten5e92a782012-01-30 07:40:52 -08002821status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002822 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002823{
2824 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002825 if (mEffectsFactoryHal.get()) {
2826 return mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
2827 } else {
2828 return -ENODEV;
2829 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002830}
2831
2832
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002833sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002834 effect_descriptor_t *pDesc,
2835 const sp<IEffectClient>& effectClient,
2836 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002837 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002838 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002839 const String16& opPackageName,
Eric Laurentb6436272016-12-07 19:24:50 -08002840 pid_t pid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002841 status_t *status,
2842 int *id,
2843 int *enabled)
2844{
2845 status_t lStatus = NO_ERROR;
2846 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002847 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002848
Eric Laurentb6436272016-12-07 19:24:50 -08002849 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
2850 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
2851 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
2852 ALOGW_IF(pid != -1 && pid != callingPid,
2853 "%s uid %d pid %d tried to pass itself off as pid %d",
2854 __func__, callingUid, callingPid, pid);
2855 pid = callingPid;
2856 }
2857
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002858 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
2859 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002860
2861 if (pDesc == NULL) {
2862 lStatus = BAD_VALUE;
2863 goto Exit;
2864 }
2865
Eric Laurent84e9a102010-09-23 16:10:16 -07002866 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002867 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002868 lStatus = PERMISSION_DENIED;
2869 goto Exit;
2870 }
2871
Dima Zavinfce7a472011-04-19 22:30:36 -07002872 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002873 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002874 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002875 lStatus = PERMISSION_DENIED;
2876 goto Exit;
2877 }
2878
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002879 if (mEffectsFactoryHal == 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002880 lStatus = NO_INIT;
2881 goto Exit;
2882 }
2883
Mathias Agopian65ab4712010-07-14 17:59:35 -07002884 {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002885 if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002886 // if uuid is specified, request effect descriptor
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002887 lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002888 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002889 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002890 goto Exit;
2891 }
2892 } else {
2893 // if uuid is not specified, look for an available implementation
2894 // of the required type in effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002895 if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002896 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002897 lStatus = BAD_VALUE;
2898 goto Exit;
2899 }
2900 uint32_t numEffects = 0;
2901 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002902 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002903 bool found = false;
2904
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002905 lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002906 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002907 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002908 goto Exit;
2909 }
2910 for (uint32_t i = 0; i < numEffects; i++) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002911 lStatus = mEffectsFactoryHal->getDescriptor(i, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002912 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002913 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002914 continue;
2915 }
2916 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2917 // If matching type found save effect descriptor. If the session is
2918 // 0 and the effect is not auxiliary, continue enumeration in case
2919 // an auxiliary version of this effect type is available
2920 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002921 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002922 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002923 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2924 break;
2925 }
2926 }
2927 }
2928 if (!found) {
2929 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002930 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002931 goto Exit;
2932 }
2933 // For same effect type, chose auxiliary version over insert version if
2934 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002935 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002936 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002937 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002938 }
2939 }
2940
2941 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002942 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002943 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2944 lStatus = INVALID_OPERATION;
2945 goto Exit;
2946 }
2947
Eric Laurent59255e42011-07-27 19:49:51 -07002948 // check recording permission for visualizer
2949 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002950 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002951 lStatus = PERMISSION_DENIED;
2952 goto Exit;
2953 }
2954
Mathias Agopian65ab4712010-07-14 17:59:35 -07002955 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002956 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002957 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002958 // if the output returned by getOutputForEffect() is removed before we lock the
2959 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2960 // and we will exit safely
2961 io = AudioSystem::getOutputForEffect(&desc);
2962 ALOGV("createEffect got output %d", io);
2963 }
2964
2965 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002966
2967 // If output is not specified try to find a matching audio session ID in one of the
2968 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002969 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2970 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002971 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002972 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002973 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2974 // output must be specified by AudioPolicyManager when using session
2975 // AUDIO_SESSION_OUTPUT_STAGE
2976 lStatus = BAD_VALUE;
2977 goto Exit;
2978 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002979 // look for the thread where the specified audio session is present
2980 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2981 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2982 io = mPlaybackThreads.keyAt(i);
2983 break;
2984 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002985 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002986 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002987 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2988 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2989 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002990 break;
2991 }
2992 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002993 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002994 if (io == AUDIO_IO_HANDLE_NONE) {
2995 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2996 if (mMmapThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2997 io = mMmapThreads.keyAt(i);
2998 break;
2999 }
3000 }
3001 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003002 // If no output thread contains the requested session ID, default to
3003 // first output. The effect chain will be moved to the correct output
3004 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07003005 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003006 io = mPlaybackThreads.keyAt(0);
3007 }
Steve Block3856b092011-10-20 11:56:00 +01003008 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003009 }
3010 ThreadBase *thread = checkRecordThread_l(io);
3011 if (thread == NULL) {
3012 thread = checkPlaybackThread_l(io);
3013 if (thread == NULL) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08003014 thread = checkMmapThread_l(io);
3015 if (thread == NULL) {
3016 ALOGE("createEffect() unknown output thread");
3017 lStatus = BAD_VALUE;
3018 goto Exit;
3019 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003020 }
Eric Laurentaaa44472014-09-12 17:41:50 -07003021 } else {
3022 // Check if one effect chain was awaiting for an effect to be created on this
3023 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08003024 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07003025 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07003026 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07003027 thread->addEffectChain_l(chain);
3028 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003029 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003030
Eric Laurent021cf962014-05-13 10:18:14 -07003031 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003032
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003033 // create effect on selected output thread
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003034 bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003035 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003036 &desc, enabled, &lStatus, pinned);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003037 if (handle != 0 && id != NULL) {
3038 *id = handle->id();
3039 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003040 if (handle == 0) {
3041 // remove local strong reference to Client with mClientLock held
3042 Mutex::Autolock _cl(mClientLock);
3043 client.clear();
3044 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003045 }
3046
3047Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07003048 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003049 return handle;
3050}
3051
Glenn Kastend848eb42016-03-08 13:42:11 -08003052status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003053 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07003054{
Steve Block3856b092011-10-20 11:56:00 +01003055 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07003056 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003057 Mutex::Autolock _l(mLock);
3058 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003059 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003060 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003061 }
Eric Laurentde070132010-07-13 04:45:46 -07003062 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
3063 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003064 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003065 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003066 }
Eric Laurentde070132010-07-13 04:45:46 -07003067 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
3068 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003069 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003070 return BAD_VALUE;
3071 }
3072
3073 Mutex::Autolock _dl(dstThread->mLock);
3074 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003075 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003076}
3077
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003078// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08003079status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07003080 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07003081 AudioFlinger::PlaybackThread *dstThread,
3082 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07003083{
Steve Block3856b092011-10-20 11:56:00 +01003084 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003085 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07003086
Eric Laurent59255e42011-07-27 19:49:51 -07003087 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003088 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003089 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003090 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07003091 return INVALID_OPERATION;
3092 }
3093
Eric Laurent4c415062016-06-17 16:14:16 -07003094 // Check whether the destination thread and all effects in the chain are compatible
3095 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07003096 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07003097 " destination thread %p is not compatible with effects in the chain",
3098 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07003099 return INVALID_OPERATION;
3100 }
3101
Eric Laurent39e94f82010-07-28 01:32:47 -07003102 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07003103 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07003104 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07003105 // removed.
3106 srcThread->removeEffectChain_l(chain);
3107
3108 // transfer all effects one by one so that new effect chain is created on new thread with
3109 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07003110 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003111 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07003112 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003113 Vector< sp<EffectModule> > removed;
3114 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07003115 while (effect != 0) {
3116 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003117 removed.add(effect);
3118 status = dstThread->addEffect_l(effect);
3119 if (status != NO_ERROR) {
3120 break;
3121 }
Eric Laurentec35a142011-10-05 17:42:25 -07003122 // removeEffect_l() has stopped the effect if it was active so it must be restarted
3123 if (effect->state() == EffectModule::ACTIVE ||
3124 effect->state() == EffectModule::STOPPING) {
3125 effect->start();
3126 }
Eric Laurent39e94f82010-07-28 01:32:47 -07003127 // if the move request is not received from audio policy manager, the effect must be
3128 // re-registered with the new strategy and output
3129 if (dstChain == 0) {
3130 dstChain = effect->chain().promote();
3131 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003132 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003133 status = NO_INIT;
3134 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07003135 }
3136 strategy = dstChain->strategy();
3137 }
3138 if (reRegister) {
3139 AudioSystem::unregisterEffect(effect->id());
3140 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07003141 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07003142 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07003143 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07003144 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003145 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07003146 }
Eric Laurentde070132010-07-13 04:45:46 -07003147 effect = chain->getEffectFromId_l(0);
3148 }
3149
Eric Laurent5baf2af2013-09-12 17:37:00 -07003150 if (status != NO_ERROR) {
3151 for (size_t i = 0; i < removed.size(); i++) {
3152 srcThread->addEffect_l(removed[i]);
3153 if (dstChain != 0 && reRegister) {
3154 AudioSystem::unregisterEffect(removed[i]->id());
3155 AudioSystem::registerEffect(&removed[i]->desc(),
3156 srcThread->id(),
3157 strategy,
3158 sessionId,
3159 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003160 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003161 }
3162 }
3163 }
3164
3165 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003166}
3167
Eric Laurent5baf2af2013-09-12 17:37:00 -07003168bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07003169{
3170 if (mGlobalEffectEnableTime != 0 &&
3171 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
3172 return true;
3173 }
3174
3175 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3176 sp<EffectChain> ec =
3177 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003178 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07003179 return true;
3180 }
3181 }
3182 return false;
3183}
3184
Eric Laurent5baf2af2013-09-12 17:37:00 -07003185void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07003186{
3187 Mutex::Autolock _l(mLock);
3188
3189 mGlobalEffectEnableTime = systemTime();
3190
3191 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3192 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
3193 if (t->mType == ThreadBase::OFFLOAD) {
3194 t->invalidateTracks(AUDIO_STREAM_MUSIC);
3195 }
3196 }
3197
3198}
3199
Eric Laurentaaa44472014-09-12 17:41:50 -07003200status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
3201{
Eric Laurentd8365c52017-07-16 15:27:05 -07003202 // clear possible suspended state before parking the chain so that it starts in default state
3203 // when attached to a new record thread
3204 chain->setEffectSuspended_l(FX_IID_AEC, false);
3205 chain->setEffectSuspended_l(FX_IID_NS, false);
3206
Glenn Kastend848eb42016-03-08 13:42:11 -08003207 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003208 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003209 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003210 if (index >= 0) {
3211 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
3212 return ALREADY_EXISTS;
3213 }
3214 mOrphanEffectChains.add(session, chain);
3215 return NO_ERROR;
3216}
3217
3218sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
3219{
3220 sp<EffectChain> chain;
3221 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003222 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003223 if (index >= 0) {
3224 chain = mOrphanEffectChains.valueAt(index);
3225 mOrphanEffectChains.removeItemsAt(index);
3226 }
3227 return chain;
3228}
3229
3230bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
3231{
3232 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08003233 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003234 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003235 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003236 if (index >= 0) {
3237 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003238 if (chain->removeEffect_l(effect, true) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003239 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003240 mOrphanEffectChains.removeItemsAt(index);
3241 }
3242 return true;
3243 }
3244 return false;
3245}
3246
3247
Glenn Kastenda6ef132013-01-10 12:31:01 -08003248struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003249#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
3250 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003251};
3252
3253int comparEntry(const void *p1, const void *p2)
3254{
Glenn Kasten2f55e762015-03-05 16:05:08 -08003255 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003256}
3257
Glenn Kasten46909e72013-02-26 09:20:22 -08003258#ifdef TEE_SINK
Glenn Kasten5b2191a2016-08-19 11:44:47 -07003259void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id, char suffix)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003260{
Eric Laurent81784c32012-11-19 14:55:58 -08003261 NBAIO_Source *teeSource = source.get();
3262 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003263 // .wav rotation
3264 // There is a benign race condition if 2 threads call this simultaneously.
3265 // They would both traverse the directory, but the result would simply be
3266 // failures at unlink() which are ignored. It's also unlikely since
3267 // normally dumpsys is only done by bugreport or from the command line.
3268 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08003269 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08003270 size_t teePathLen = strlen(teePath);
3271 DIR *dir = opendir(teePath);
3272 teePath[teePathLen++] = '/';
3273 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003274#define TEE_MAX_SORT 20 // number of entries to sort
3275#define TEE_MAX_KEEP 10 // number of entries to keep
3276 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003277 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08003278 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003279 struct dirent de;
3280 struct dirent *result = NULL;
3281 int rc = readdir_r(dir, &de, &result);
3282 if (rc != 0) {
3283 ALOGW("readdir_r failed %d", rc);
3284 break;
3285 }
3286 if (result == NULL) {
3287 break;
3288 }
3289 if (result != &de) {
3290 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
3291 break;
3292 }
3293 // ignore non .wav file entries
3294 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003295 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08003296 strcmp(&de.d_name[nameLen - 4], ".wav")) {
3297 continue;
3298 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08003299 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003300 }
3301 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003302 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003303 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003304 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
3305 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003306 (void) unlink(teePath);
3307 }
3308 }
3309 } else {
3310 if (fd >= 0) {
Glenn Kastenfbd87e82016-07-18 15:45:56 -07003311 dprintf(fd, "unable to rotate tees in %.*s: %s\n", (int) teePathLen, teePath,
Glenn Kasten9a003992016-02-23 15:24:34 -08003312 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003313 }
3314 }
Eric Laurent81784c32012-11-19 14:55:58 -08003315 char teeTime[16];
3316 struct timeval tv;
3317 gettimeofday(&tv, NULL);
3318 struct tm tm;
3319 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003320 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
Glenn Kasten5b2191a2016-08-19 11:44:47 -07003321 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d_%c.wav", teeTime, id,
3322 suffix);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003323 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3324 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003325 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003326 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003327 char wavHeader[44];
3328 memcpy(wavHeader,
3329 "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",
3330 sizeof(wavHeader));
3331 NBAIO_Format format = teeSource->format();
3332 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003333 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003334 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003335 wavHeader[22] = channelCount; // number of channels
3336 wavHeader[24] = sampleRate; // sample rate
3337 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003338 wavHeader[32] = frameSize; // block alignment
3339 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003340 write(teeFd, wavHeader, sizeof(wavHeader));
3341 size_t total = 0;
3342 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003343#define TEE_SINK_READ 1024 // frames per I/O operation
3344 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003345 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003346 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003347 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003348 bool wasFirstRead = firstRead;
3349 firstRead = false;
3350 if (actual <= 0) {
3351 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3352 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003353 }
Eric Laurent81784c32012-11-19 14:55:58 -08003354 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003355 }
Eric Laurent81784c32012-11-19 14:55:58 -08003356 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003357 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003358 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003359 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003360 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003361 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003362 uint32_t temp = 44 + total * frameSize - 8;
3363 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003364 write(teeFd, &temp, sizeof(temp));
3365 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003366 temp = total * frameSize;
3367 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003368 write(teeFd, &temp, sizeof(temp));
3369 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003370 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003371 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003372 }
Eric Laurent59255e42011-07-27 19:49:51 -07003373 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003374 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003375 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003376 }
Eric Laurent59255e42011-07-27 19:49:51 -07003377 }
3378 }
3379}
Glenn Kasten46909e72013-02-26 09:20:22 -08003380#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003381
Mathias Agopian65ab4712010-07-14 17:59:35 -07003382// ----------------------------------------------------------------------------
3383
3384status_t AudioFlinger::onTransact(
3385 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3386{
3387 return BnAudioFlinger::onTransact(code, data, reply, flags);
3388}
3389
Glenn Kasten63238ef2015-03-02 15:50:29 -08003390} // namespace android