blob: 9cb0357f5e806b015a2899dc8b20f0ba82b1e3b1 [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);
Eric Laurent7aa0ccb2017-08-28 11:12:52 -0700329 thread->configure(attr, streamType, sessionId, callback, *deviceId, portId);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700330 *handle = portId;
Eric Laurent6acd1d42017-01-04 14:23:29 -0800331 } else {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700332 if (direction == MmapStreamInterface::DIRECTION_OUTPUT) {
333 AudioSystem::releaseOutput(io, streamType, sessionId);
334 } else {
335 AudioSystem::releaseInput(io, sessionId);
336 }
Eric Laurent6acd1d42017-01-04 14:23:29 -0800337 ret = NO_INIT;
338 }
339
340 ALOGV("%s done status %d portId %d", __FUNCTION__, ret, portId);
341
Eric Laurentfc235202016-12-20 18:48:17 -0800342 return ret;
343}
344
Eric Laurenta4c5a552012-03-29 10:12:40 -0700345static const char * const audio_interfaces[] = {
346 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
347 AUDIO_HARDWARE_MODULE_ID_A2DP,
348 AUDIO_HARDWARE_MODULE_ID_USB,
349};
Eric Laurenta4c5a552012-03-29 10:12:40 -0700350
Phil Burk062e67a2015-02-11 13:40:50 -0800351AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700352 audio_module_handle_t module,
353 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700354{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700355 // if module is 0, the request comes from an old policy manager and we should load
356 // well known modules
357 if (module == 0) {
358 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
Mikhail Naganovbf493082017-04-17 17:37:12 -0700359 for (size_t i = 0; i < arraysize(audio_interfaces); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700360 loadHwModule_l(audio_interfaces[i]);
361 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700362 // then try to find a module supporting the requested device.
363 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
364 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700365 sp<DeviceHalInterface> dev = audioHwDevice->hwDevice();
366 uint32_t supportedDevices;
367 if (dev->getSupportedDevices(&supportedDevices) == OK &&
368 (supportedDevices & devices) == devices) {
Eric Laurentf1c04f92012-08-28 14:26:53 -0700369 return audioHwDevice;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700370 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700371 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700372 } else {
373 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700374 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
375 if (audioHwDevice != NULL) {
376 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700377 }
378 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700379
Dima Zavin799a70e2011-04-18 16:57:27 -0700380 return NULL;
381}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382
Glenn Kasten0f11b512014-01-31 16:18:54 -0800383void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384{
385 const size_t SIZE = 256;
386 char buffer[SIZE];
387 String8 result;
388
389 result.append("Clients:\n");
390 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800391 sp<Client> client = mClients.valueAt(i).promote();
392 if (client != 0) {
393 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
394 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395 }
396 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700397
Eric Laurentd1b28d42013-09-18 18:47:13 -0700398 result.append("Notification Clients:\n");
399 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
400 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
401 result.append(buffer);
402 }
403
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700404 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800405 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700406 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
407 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800408 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700409 result.append(buffer);
410 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412}
413
414
Glenn Kasten0f11b512014-01-31 16:18:54 -0800415void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416{
417 const size_t SIZE = 256;
418 char buffer[SIZE];
419 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800420 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421
John Grossman4ff14ba2012-02-08 16:37:41 -0800422 snprintf(buffer, SIZE, "Hardware status: %d\n"
423 "Standby Time mSec: %u\n",
424 hardwareStatus,
425 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426 result.append(buffer);
427 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428}
429
Glenn Kasten0f11b512014-01-31 16:18:54 -0800430void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431{
432 const size_t SIZE = 256;
433 char buffer[SIZE];
434 String8 result;
435 snprintf(buffer, SIZE, "Permission Denial: "
436 "can't dump AudioFlinger from pid=%d, uid=%d\n",
437 IPCThreadState::self()->getCallingPid(),
438 IPCThreadState::self()->getCallingUid());
439 result.append(buffer);
440 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441}
442
Eric Laurent81784c32012-11-19 14:55:58 -0800443bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700444{
445 bool locked = false;
446 for (int i = 0; i < kDumpLockRetries; ++i) {
447 if (mutex.tryLock() == NO_ERROR) {
448 locked = true;
449 break;
450 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800451 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 }
453 return locked;
454}
455
456status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
457{
Glenn Kasten44deb052012-02-05 18:09:08 -0800458 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700459 dumpPermissionDenial(fd, args);
460 } else {
461 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800462 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463 if (!hardwareLocked) {
464 String8 result(kHardwareLockedString);
465 write(fd, result.string(), result.size());
466 } else {
467 mHardwareLock.unlock();
468 }
469
Eric Laurent81784c32012-11-19 14:55:58 -0800470 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700471
472 // failed to lock - AudioFlinger is probably deadlocked
473 if (!locked) {
474 String8 result(kDeadlockedString);
475 write(fd, result.string(), result.size());
476 }
477
Eric Laurent021cf962014-05-13 10:18:14 -0700478 bool clientLocked = dumpTryLock(mClientLock);
479 if (!clientLocked) {
480 String8 result(kClientLockedString);
481 write(fd, result.string(), result.size());
482 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700483
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700484 if (mEffectsFactoryHal != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700485 mEffectsFactoryHal->dumpEffects(fd);
486 } else {
487 String8 result(kNoEffectsFactory);
488 write(fd, result.string(), result.size());
489 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700490
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700492 if (clientLocked) {
493 mClientLock.unlock();
494 }
495
Mathias Agopian65ab4712010-07-14 17:59:35 -0700496 dumpInternals(fd, args);
497
498 // dump playback threads
499 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
500 mPlaybackThreads.valueAt(i)->dump(fd, args);
501 }
502
503 // dump record threads
504 for (size_t i = 0; i < mRecordThreads.size(); i++) {
505 mRecordThreads.valueAt(i)->dump(fd, args);
506 }
507
Eric Laurent6acd1d42017-01-04 14:23:29 -0800508 // dump mmap threads
509 for (size_t i = 0; i < mMmapThreads.size(); i++) {
510 mMmapThreads.valueAt(i)->dump(fd, args);
511 }
512
Eric Laurentaaa44472014-09-12 17:41:50 -0700513 // dump orphan effect chains
514 if (mOrphanEffectChains.size() != 0) {
515 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
516 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
517 mOrphanEffectChains.valueAt(i)->dump(fd, args);
518 }
519 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700520 // dump all hardware devs
521 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700522 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
523 dev->dump(fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700524 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700525
Glenn Kasten46909e72013-02-26 09:20:22 -0800526#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700527 // dump the serially shared record tee sink
528 if (mRecordTeeSource != 0) {
Glenn Kasten5b2191a2016-08-19 11:44:47 -0700529 dumpTee(fd, mRecordTeeSource, AUDIO_IO_HANDLE_NONE, 'C');
Glenn Kastend06785b2012-09-30 12:29:28 -0700530 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800531#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700532
rago3bd1c872016-09-26 12:58:14 -0700533 BUFLOG_RESET;
534
Glenn Kastend65d73c2012-06-22 17:21:07 -0700535 if (locked) {
536 mLock.unlock();
537 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800538
539 // append a copy of media.log here by forwarding fd to it, but don't attempt
540 // to lookup the service if it's not running, as it will block for a second
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700541 if (sMediaLogServiceAsBinder != 0) {
542 dprintf(fd, "\nmedia.log:\n");
543 Vector<String16> args;
544 sMediaLogServiceAsBinder->dump(fd, args);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800545 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700546
547 // check for optional arguments
Andy Hung07b745e2016-05-23 16:21:07 -0700548 bool dumpMem = false;
Andy Hung35fec5f2016-04-13 14:21:48 -0700549 bool unreachableMemory = false;
550 for (const auto &arg : args) {
Andy Hung07b745e2016-05-23 16:21:07 -0700551 if (arg == String16("-m")) {
552 dumpMem = true;
553 } else if (arg == String16("--unreachable")) {
Andy Hung35fec5f2016-04-13 14:21:48 -0700554 unreachableMemory = true;
555 }
556 }
557
Andy Hung07b745e2016-05-23 16:21:07 -0700558 if (dumpMem) {
559 dprintf(fd, "\nDumping memory:\n");
560 std::string s = dumpMemoryAddresses(100 /* limit */);
561 write(fd, s.c_str(), s.size());
562 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700563 if (unreachableMemory) {
564 dprintf(fd, "\nDumping unreachable memory:\n");
565 // TODO - should limit be an argument parameter?
Andy Hung07b745e2016-05-23 16:21:07 -0700566 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
Andy Hung35fec5f2016-04-13 14:21:48 -0700567 write(fd, s.c_str(), s.size());
568 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569 }
570 return NO_ERROR;
571}
572
Eric Laurent021cf962014-05-13 10:18:14 -0700573sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800574{
Eric Laurent021cf962014-05-13 10:18:14 -0700575 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800576 // If pid is already in the mClients wp<> map, then use that entry
577 // (for which promote() is always != 0), otherwise create a new entry and Client.
578 sp<Client> client = mClients.valueFor(pid).promote();
579 if (client == 0) {
580 client = new Client(this, pid);
581 mClients.add(pid, client);
582 }
583
584 return client;
585}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586
Glenn Kasten9e58b552013-01-18 15:09:48 -0800587sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
588{
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700589 // If there is no memory allocated for logs, return a dummy writer that does nothing.
590 // Similarly if we can't contact the media.log service, also return a dummy writer.
591 if (mLogMemoryDealer == 0 || sMediaLogService == 0) {
Glenn Kasten9e58b552013-01-18 15:09:48 -0800592 return new NBLog::Writer();
593 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700594 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
595 // If allocation fails, consult the vector of previously unregistered writers
596 // and garbage-collect one or more them until an allocation succeeds
597 if (shared == 0) {
598 Mutex::Autolock _l(mUnregisteredWritersLock);
599 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
600 {
601 // Pick the oldest stale writer to garbage-collect
602 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
603 mUnregisteredWriters.removeAt(0);
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700604 sMediaLogService->unregisterWriter(iMemory);
Glenn Kasten481fb672013-09-30 14:39:28 -0700605 // Now the media.log remote reference to IMemory is gone. When our last local
606 // reference to IMemory also drops to zero at end of this block,
607 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
608 }
609 // Re-attempt the allocation
610 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
611 if (shared != 0) {
612 goto success;
613 }
614 }
615 // Even after garbage-collecting all old writers, there is still not enough memory,
616 // so return a dummy writer
617 return new NBLog::Writer();
618 }
619success:
Glenn Kasten535e1612016-12-05 12:19:36 -0800620 NBLog::Shared *sharedRawPtr = (NBLog::Shared *) shared->pointer();
621 new((void *) sharedRawPtr) NBLog::Shared(); // placement new here, but the corresponding
622 // explicit destructor not needed since it is POD
Glenn Kasten04b96fc2017-04-10 14:58:47 -0700623 sMediaLogService->registerWriter(shared, size, name);
Glenn Kasten535e1612016-12-05 12:19:36 -0800624 return new NBLog::Writer(shared, size);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800625}
626
627void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
628{
Glenn Kasten685ef092013-02-04 08:15:34 -0800629 if (writer == 0) {
630 return;
631 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800632 sp<IMemory> iMemory(writer->getIMemory());
633 if (iMemory == 0) {
634 return;
635 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700636 // Rather than removing the writer immediately, append it to a queue of old writers to
637 // be garbage-collected later. This allows us to continue to view old logs for a while.
638 Mutex::Autolock _l(mUnregisteredWritersLock);
639 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800640}
641
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642// IAudioFlinger interface
643
Eric Laurent21da6472017-11-09 16:29:26 -0800644sp<IAudioTrack> AudioFlinger::createTrack(const CreateTrackInput& input,
645 CreateTrackOutput& output,
646 status_t *status)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647{
648 sp<PlaybackThread::Track> track;
649 sp<TrackHandle> trackHandle;
650 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651 status_t lStatus;
Eric Laurent21da6472017-11-09 16:29:26 -0800652 audio_stream_type_t streamType;
653 audio_port_handle_t portId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654
Eric Laurent21da6472017-11-09 16:29:26 -0800655 bool updatePid = (input.clientInfo.clientPid == -1);
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700656 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurent21da6472017-11-09 16:29:26 -0800657 uid_t clientUid = input.clientInfo.clientUid;
658 if (!isTrustedCallingUid(callingUid)) {
659 ALOGW_IF(clientUid != callingUid,
660 "%s uid %d tried to pass itself off as %d",
661 __FUNCTION__, callingUid, clientUid);
662 clientUid = callingUid;
663 updatePid = true;
664 }
665 pid_t clientPid = input.clientInfo.clientPid;
666 if (updatePid) {
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700667 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurent21da6472017-11-09 16:29:26 -0800668 ALOGW_IF(clientPid != -1 && clientPid != callingPid,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700669 "%s uid %d pid %d tried to pass itself off as pid %d",
Eric Laurent21da6472017-11-09 16:29:26 -0800670 __func__, callingUid, callingPid, clientPid);
671 clientPid = callingPid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700672 }
673
Eric Laurent21da6472017-11-09 16:29:26 -0800674 audio_session_t sessionId = input.sessionId;
675 if (sessionId == AUDIO_SESSION_ALLOCATE) {
676 sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
677 }
678 output.sessionId = sessionId;
679 output.outputId = AUDIO_IO_HANDLE_NONE;
680 output.selectedDeviceId = input.selectedDeviceId;
681
682 lStatus = AudioSystem::getOutputForAttr(&input.attr, &output.outputId, sessionId, &streamType,
683 clientUid, &input.config, input.flags,
684 &output.selectedDeviceId, &portId);
685
686 if (lStatus != NO_ERROR || output.outputId == AUDIO_IO_HANDLE_NONE) {
687 ALOGE("createTrack() getOutputForAttr() return error %d or invalid output handle", lStatus);
688 goto Exit;
689 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800690 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
691 // but if someone uses binder directly they could bypass that and cause us to crash
692 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000693 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 lStatus = BAD_VALUE;
695 goto Exit;
696 }
697
Glenn Kasten53b5d092014-02-05 10:00:23 -0800698 // further channel mask checks are performed by createTrack_l() depending on the thread type
Eric Laurent21da6472017-11-09 16:29:26 -0800699 if (!audio_is_output_channel(input.config.channel_mask)) {
700 ALOGE("createTrack() invalid channel mask %#x", input.config.channel_mask);
Glenn Kasten53b5d092014-02-05 10:00:23 -0800701 lStatus = BAD_VALUE;
702 goto Exit;
703 }
704
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700705 // further format checks are performed by createTrack_l() depending on the thread type
Eric Laurent21da6472017-11-09 16:29:26 -0800706 if (!audio_is_valid_format(input.config.format)) {
707 ALOGE("createTrack() invalid format %#x", input.config.format);
Glenn Kasten663c2242013-09-24 11:52:37 -0700708 lStatus = BAD_VALUE;
709 goto Exit;
710 }
711
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 {
713 Mutex::Autolock _l(mLock);
Eric Laurent21da6472017-11-09 16:29:26 -0800714 PlaybackThread *thread = checkPlaybackThread_l(output.outputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715 if (thread == NULL) {
Eric Laurent21da6472017-11-09 16:29:26 -0800716 ALOGE("no playback thread found for output handle %d", output.outputId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717 lStatus = BAD_VALUE;
718 goto Exit;
719 }
720
Eric Laurent21da6472017-11-09 16:29:26 -0800721 client = registerPid(clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722
Glenn Kastene848bd92014-03-13 15:00:32 -0700723 PlaybackThread *effectThread = NULL;
Eric Laurent21da6472017-11-09 16:29:26 -0800724 // check if an effect chain with the same session ID is present on another
725 // output thread and move it here.
726 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
727 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
728 if (mPlaybackThreads.keyAt(i) != output.outputId) {
729 uint32_t sessions = t->hasAudioSession(sessionId);
730 if (sessions & ThreadBase::EFFECT_SESSION) {
731 effectThread = t.get();
732 break;
Eric Laurentde070132010-07-13 04:45:46 -0700733 }
734 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 }
Eric Laurent21da6472017-11-09 16:29:26 -0800736 ALOGV("createTrack() sessionId: %d", sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700737
Eric Laurent21da6472017-11-09 16:29:26 -0800738 output.sampleRate = input.config.sample_rate;
739 output.frameCount = input.frameCount;
740 output.notificationFrameCount = input.notificationFrameCount;
741 output.flags = input.flags;
742
743 track = thread->createTrack_l(client, streamType, &output.sampleRate, input.config.format,
744 input.config.channel_mask,
745 &output.frameCount, &output.notificationFrameCount,
746 input.notificationsPerBuffer, input.speed,
747 input.sharedBuffer, sessionId, &output.flags,
748 input.clientInfo.clientTid, 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
Eric Laurent21da6472017-11-09 16:29:26 -0800752 output.afFrameCount = thread->frameCount();
753 output.afSampleRate = thread->sampleRate();
754 output.afLatencyMs = thread->latency();
755
Eric Laurent39e94f82010-07-28 01:32:47 -0700756 // move effect chain to this output thread if an effect on same session was waiting
757 // for a track to be created
758 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700759 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700760 Mutex::Autolock _dl(thread->mLock);
761 Mutex::Autolock _sl(effectThread->mLock);
Eric Laurent21da6472017-11-09 16:29:26 -0800762 moveEffectChain_l(sessionId, effectThread, thread, true);
Eric Laurent39e94f82010-07-28 01:32:47 -0700763 }
Eric Laurenta011e352012-03-29 15:51:43 -0700764
765 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700766 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurent21da6472017-11-09 16:29:26 -0800767 if (mPendingSyncEvents[i]->triggerSession() == sessionId) {
Eric Laurenta011e352012-03-29 15:51:43 -0700768 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700769 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700770 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700771 } else {
772 mPendingSyncEvents[i]->cancel();
773 }
Eric Laurenta011e352012-03-29 15:51:43 -0700774 mPendingSyncEvents.removeAt(i);
775 i--;
776 }
777 }
778 }
Glenn Kastene198c362013-08-13 09:13:36 -0700779
Eric Laurent21da6472017-11-09 16:29:26 -0800780 setAudioHwSyncForSession_l(thread, sessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700781 }
Glenn Kastene198c362013-08-13 09:13:36 -0700782
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700783 if (lStatus != NO_ERROR) {
784 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700785 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700786 // Don't hold mClientLock when releasing the reference on the track as the
787 // destructor will acquire it.
788 {
789 Mutex::Autolock _cl(mClientLock);
790 client.clear();
791 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700793 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794 }
795
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700796 // return handle to client
797 trackHandle = new TrackHandle(track);
798
Mathias Agopian65ab4712010-07-14 17:59:35 -0700799Exit:
Eric Laurent21da6472017-11-09 16:29:26 -0800800 if (lStatus != NO_ERROR && output.outputId != AUDIO_IO_HANDLE_NONE) {
801 AudioSystem::releaseOutput(output.outputId, streamType, sessionId);
802 }
Glenn Kasten9156ef32013-08-06 15:39:08 -0700803 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 return trackHandle;
805}
806
Glenn Kasten2c073da2016-02-26 09:14:08 -0800807uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808{
809 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800810 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800812 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700813 return 0;
814 }
815 return thread->sampleRate();
816}
817
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800818audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819{
820 Mutex::Autolock _l(mLock);
821 PlaybackThread *thread = checkPlaybackThread_l(output);
822 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000823 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800824 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825 }
826 return thread->format();
827}
828
Glenn Kasten2c073da2016-02-26 09:14:08 -0800829size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700830{
831 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800832 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800834 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835 return 0;
836 }
Glenn Kasten58912562012-04-03 10:45:00 -0700837 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
838 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839 return thread->frameCount();
840}
841
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700842size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
843{
844 Mutex::Autolock _l(mLock);
845 ThreadBase *thread = checkThread_l(ioHandle);
846 if (thread == NULL) {
847 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
848 return 0;
849 }
850 return thread->frameCountHAL();
851}
852
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800853uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854{
855 Mutex::Autolock _l(mLock);
856 PlaybackThread *thread = checkPlaybackThread_l(output);
857 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800858 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 return 0;
860 }
861 return thread->latency();
862}
863
864status_t AudioFlinger::setMasterVolume(float value)
865{
Eric Laurenta1884f92011-08-23 08:25:03 -0700866 status_t ret = initCheck();
867 if (ret != NO_ERROR) {
868 return ret;
869 }
870
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871 // check calling permissions
872 if (!settingsAllowed()) {
873 return PERMISSION_DENIED;
874 }
875
Eric Laurenta4c5a552012-03-29 10:12:40 -0700876 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700877 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700878
John Grossmanee578c02012-07-23 17:05:46 -0700879 // Set master volume in the HALs which support it.
880 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
881 AutoMutex lock(mHardwareLock);
882 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800883
John Grossmanee578c02012-07-23 17:05:46 -0700884 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
885 if (dev->canSetMasterVolume()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700886 dev->hwDevice()->setMasterVolume(value);
Eric Laurent93575202011-01-18 18:39:02 -0800887 }
John Grossmanee578c02012-07-23 17:05:46 -0700888 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890
John Grossmanee578c02012-07-23 17:05:46 -0700891 // Now set the master volume in each playback thread. Playback threads
892 // assigned to HALs which do not have master volume support will apply
893 // master volume during the mix operation. Threads with HALs which do
894 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700895 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
896 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
897 continue;
898 }
John Grossmanee578c02012-07-23 17:05:46 -0700899 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700900 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700901
902 return NO_ERROR;
903}
904
Glenn Kastenf78aee72012-01-04 11:00:47 -0800905status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906{
Eric Laurenta1884f92011-08-23 08:25:03 -0700907 status_t ret = initCheck();
908 if (ret != NO_ERROR) {
909 return ret;
910 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911
912 // check calling permissions
913 if (!settingsAllowed()) {
914 return PERMISSION_DENIED;
915 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800916 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000917 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 return BAD_VALUE;
919 }
920
921 { // scope for the lock
922 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700923 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700925 ret = dev->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 mHardwareStatus = AUDIO_HW_IDLE;
927 }
928
929 if (NO_ERROR == ret) {
930 Mutex::Autolock _l(mLock);
931 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800932 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700933 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934 }
935
936 return ret;
937}
938
939status_t AudioFlinger::setMicMute(bool state)
940{
Eric Laurenta1884f92011-08-23 08:25:03 -0700941 status_t ret = initCheck();
942 if (ret != NO_ERROR) {
943 return ret;
944 }
945
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 // check calling permissions
947 if (!settingsAllowed()) {
948 return PERMISSION_DENIED;
949 }
950
951 AutoMutex lock(mHardwareLock);
952 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700953 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700954 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
955 status_t result = dev->setMicMute(state);
Eric Laurent2f035f52014-09-14 12:11:52 -0700956 if (result != NO_ERROR) {
957 ret = result;
958 }
959 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 mHardwareStatus = AUDIO_HW_IDLE;
961 return ret;
962}
963
964bool AudioFlinger::getMicMute() const
965{
Eric Laurenta1884f92011-08-23 08:25:03 -0700966 status_t ret = initCheck();
967 if (ret != NO_ERROR) {
968 return false;
969 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800970 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700971 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800972 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800974 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700975 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
976 status_t result = dev->getMicMute(&state);
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800977 if (result == NO_ERROR) {
978 mute = mute && state;
979 }
980 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800982
983 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984}
985
986status_t AudioFlinger::setMasterMute(bool muted)
987{
John Grossmand8f178d2012-07-20 14:51:35 -0700988 status_t ret = initCheck();
989 if (ret != NO_ERROR) {
990 return ret;
991 }
992
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993 // check calling permissions
994 if (!settingsAllowed()) {
995 return PERMISSION_DENIED;
996 }
997
John Grossmanee578c02012-07-23 17:05:46 -0700998 Mutex::Autolock _l(mLock);
999 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -07001000
John Grossmanee578c02012-07-23 17:05:46 -07001001 // Set master mute in the HALs which support it.
1002 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1003 AutoMutex lock(mHardwareLock);
1004 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -07001005
John Grossmanee578c02012-07-23 17:05:46 -07001006 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1007 if (dev->canSetMasterMute()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001008 dev->hwDevice()->setMasterMute(muted);
John Grossmand8f178d2012-07-20 14:51:35 -07001009 }
John Grossmanee578c02012-07-23 17:05:46 -07001010 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -07001011 }
1012
John Grossmanee578c02012-07-23 17:05:46 -07001013 // Now set the master mute in each playback thread. Playback threads
1014 // assigned to HALs which do not have master mute support will apply master
1015 // mute during the mix operation. Threads with HALs which do support master
1016 // mute will simply ignore the setting.
Eric Laurent6acd1d42017-01-04 14:23:29 -08001017 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1018 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1019 volumeInterfaces[i]->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -07001020 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021
1022 return NO_ERROR;
1023}
1024
1025float AudioFlinger::masterVolume() const
1026{
Glenn Kasten98067102011-12-13 11:47:54 -08001027 Mutex::Autolock _l(mLock);
1028 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001029}
1030
1031bool AudioFlinger::masterMute() const
1032{
Glenn Kasten98067102011-12-13 11:47:54 -08001033 Mutex::Autolock _l(mLock);
1034 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001035}
1036
John Grossman4ff14ba2012-02-08 16:37:41 -08001037float AudioFlinger::masterVolume_l() const
1038{
John Grossman4ff14ba2012-02-08 16:37:41 -08001039 return mMasterVolume;
1040}
1041
John Grossmand8f178d2012-07-20 14:51:35 -07001042bool AudioFlinger::masterMute_l() const
1043{
John Grossmanee578c02012-07-23 17:05:46 -07001044 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -07001045}
1046
Eric Laurent223fd5c2014-11-11 13:43:36 -08001047status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
1048{
1049 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001050 ALOGW("checkStreamType() invalid stream %d", stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001051 return BAD_VALUE;
1052 }
1053 pid_t caller = IPCThreadState::self()->getCallingPid();
1054 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001055 ALOGW("checkStreamType() pid %d cannot use internal stream type %d", caller, stream);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001056 return PERMISSION_DENIED;
1057 }
1058
1059 return NO_ERROR;
1060}
1061
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001062status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
1063 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064{
1065 // check calling permissions
1066 if (!settingsAllowed()) {
1067 return PERMISSION_DENIED;
1068 }
1069
Eric Laurent223fd5c2014-11-11 13:43:36 -08001070 status_t status = checkStreamType(stream);
1071 if (status != NO_ERROR) {
1072 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001074 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001075
1076 AutoMutex lock(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001077 Vector<VolumeInterface *> volumeInterfaces;
Glenn Kasten142f5192014-03-25 17:44:59 -07001078 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001079 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1080 if (volumeInterface == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001081 return BAD_VALUE;
1082 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001083 volumeInterfaces.add(volumeInterface);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001084 }
1085
1086 mStreamTypes[stream].volume = value;
1087
Eric Laurent6acd1d42017-01-04 14:23:29 -08001088 if (volumeInterfaces.size() == 0) {
1089 volumeInterfaces = getAllVolumeInterfaces_l();
1090 }
1091 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1092 volumeInterfaces[i]->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001093 }
1094
1095 return NO_ERROR;
1096}
1097
Glenn Kastenfff6d712012-01-12 16:38:12 -08001098status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001099{
1100 // check calling permissions
1101 if (!settingsAllowed()) {
1102 return PERMISSION_DENIED;
1103 }
1104
Eric Laurent223fd5c2014-11-11 13:43:36 -08001105 status_t status = checkStreamType(stream);
1106 if (status != NO_ERROR) {
1107 return status;
1108 }
1109 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1110
1111 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001112 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001113 return BAD_VALUE;
1114 }
1115
Eric Laurent93575202011-01-18 18:39:02 -08001116 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117 mStreamTypes[stream].mute = muted;
Eric Laurent6acd1d42017-01-04 14:23:29 -08001118 Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l();
1119 for (size_t i = 0; i < volumeInterfaces.size(); i++) {
1120 volumeInterfaces[i]->setStreamMute(stream, muted);
1121 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001122
1123 return NO_ERROR;
1124}
1125
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001126float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001127{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001128 status_t status = checkStreamType(stream);
1129 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130 return 0.0f;
1131 }
1132
1133 AutoMutex lock(mLock);
1134 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001135 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001136 VolumeInterface *volumeInterface = getVolumeInterface_l(output);
1137 if (volumeInterface != NULL) {
1138 volume = volumeInterface->streamVolume(stream);
1139 } else {
1140 volume = 0.0f;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001143 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001144 }
1145
1146 return volume;
1147}
1148
Glenn Kastenfff6d712012-01-12 16:38:12 -08001149bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001150{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001151 status_t status = checkStreamType(stream);
1152 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001153 return true;
1154 }
1155
Glenn Kasten6637baa2012-01-09 09:40:36 -08001156 AutoMutex lock(mLock);
1157 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158}
1159
Eric Laurent054d9d32015-04-24 08:48:48 -07001160
1161void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1162{
1163 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1164 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1165 }
1166}
1167
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001168status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001169{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001170 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1171 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001172
Mathias Agopian65ab4712010-07-14 17:59:35 -07001173 // check calling permissions
1174 if (!settingsAllowed()) {
1175 return PERMISSION_DENIED;
1176 }
1177
Glenn Kasten142f5192014-03-25 17:44:59 -07001178 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1179 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001180 Mutex::Autolock _l(mLock);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001181 // result will remain NO_INIT if no audio device is present
1182 status_t final_result = NO_INIT;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001183 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001184 AutoMutex lock(mHardwareLock);
1185 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1186 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001187 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1188 status_t result = dev->setParameters(keyValuePairs);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001189 // return success if at least one audio device accepts the parameters as not all
1190 // HALs are requested to support all parameters. If no audio device supports the
1191 // requested parameters, the last error is reported.
1192 if (final_result != NO_ERROR) {
1193 final_result = result;
1194 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001195 }
1196 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001197 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001198 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1199 AudioParameter param = AudioParameter(keyValuePairs);
1200 String8 value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001201 if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) {
1202 bool btNrecIsOff = (value == AudioParameter::valueOff);
Eric Laurentd8365c52017-07-16 15:27:05 -07001203 if (mBtNrecIsOff.exchange(btNrecIsOff) != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001204 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentd8365c52017-07-16 15:27:05 -07001205 mRecordThreads.valueAt(i)->checkBtNrec();
Eric Laurent59bd0da2011-08-01 09:52:20 -07001206 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001207 }
1208 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001209 String8 screenState;
1210 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
Mikhail Naganov00260b52016-10-13 12:54:24 -07001211 bool isOff = (screenState == AudioParameter::valueOff);
Eric Laurent81784c32012-11-19 14:55:58 -08001212 if (isOff != (AudioFlinger::mScreenState & 1)) {
1213 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001214 }
1215 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001216 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217 }
1218
1219 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1220 // and the thread is exited once the lock is released
1221 sp<ThreadBase> thread;
1222 {
1223 Mutex::Autolock _l(mLock);
1224 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001225 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226 thread = checkRecordThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -08001227 if (thread == 0) {
1228 thread = checkMmapThread_l(ioHandle);
1229 }
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001230 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001231 // indicate output device change to all input threads for pre processing
1232 AudioParameter param = AudioParameter(keyValuePairs);
1233 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001234 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1235 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001236 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001237 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238 }
1239 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001240 if (thread != 0) {
1241 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001242 }
1243 return BAD_VALUE;
1244}
1245
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001246String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001247{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001248 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1249 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001250
Eric Laurenta4c5a552012-03-29 10:12:40 -07001251 Mutex::Autolock _l(mLock);
1252
Glenn Kasten142f5192014-03-25 17:44:59 -07001253 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001254 String8 out_s8;
1255
Dima Zavin799a70e2011-04-18 16:57:27 -07001256 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001257 String8 s;
1258 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001259 {
1260 AutoMutex lock(mHardwareLock);
1261 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001262 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1263 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001264 mHardwareStatus = AUDIO_HW_IDLE;
1265 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001266 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001267 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001268 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269 }
1270
Eric Laurent6acd1d42017-01-04 14:23:29 -08001271 ThreadBase *thread = (ThreadBase *)checkPlaybackThread_l(ioHandle);
1272 if (thread == NULL) {
1273 thread = (ThreadBase *)checkRecordThread_l(ioHandle);
1274 if (thread == NULL) {
1275 thread = (ThreadBase *)checkMmapThread_l(ioHandle);
1276 if (thread == NULL) {
Mikhail Naganovaa165e52017-07-12 10:39:16 -07001277 return String8("");
Eric Laurent6acd1d42017-01-04 14:23:29 -08001278 }
1279 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001280 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08001281 return thread->getParameters(keys);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001282}
1283
Glenn Kastendd8104c2012-07-02 12:42:44 -07001284size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1285 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286{
Eric Laurenta1884f92011-08-23 08:25:03 -07001287 status_t ret = initCheck();
1288 if (ret != NO_ERROR) {
1289 return 0;
1290 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001291 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001292 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001293 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001294 return 0;
1295 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001296
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001297 AutoMutex lock(mHardwareLock);
1298 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001299 audio_config_t config, proposed;
1300 memset(&proposed, 0, sizeof(proposed));
1301 proposed.sample_rate = sampleRate;
1302 proposed.channel_mask = channelMask;
1303 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001304
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001305 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001306 size_t frames;
1307 for (;;) {
1308 // Note: config is currently a const parameter for get_input_buffer_size()
1309 // but we use a copy from proposed in case config changes from the call.
1310 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001311 status_t result = dev->getInputBufferSize(&config, &frames);
1312 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001313 break; // hal success, config is the result
1314 }
1315 // change one parameter of the configuration each iteration to a more "common" value
1316 // to see if the device will support it.
1317 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1318 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1319 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1320 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1321 } else {
1322 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1323 "format %#x, channelMask 0x%X",
1324 sampleRate, format, channelMask);
1325 break; // retries failed, break out of loop with frames == 0.
1326 }
1327 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001328 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001329 if (frames > 0 && config.sample_rate != sampleRate) {
1330 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1331 }
1332 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001333}
1334
Glenn Kasten5f972c02014-01-13 09:59:31 -08001335uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001336{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001337 Mutex::Autolock _l(mLock);
1338
1339 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1340 if (recordThread != NULL) {
1341 return recordThread->getInputFramesLost();
1342 }
1343 return 0;
1344}
1345
1346status_t AudioFlinger::setVoiceVolume(float value)
1347{
Eric Laurenta1884f92011-08-23 08:25:03 -07001348 status_t ret = initCheck();
1349 if (ret != NO_ERROR) {
1350 return ret;
1351 }
1352
Mathias Agopian65ab4712010-07-14 17:59:35 -07001353 // check calling permissions
1354 if (!settingsAllowed()) {
1355 return PERMISSION_DENIED;
1356 }
1357
1358 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001359 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001360 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001361 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001362 mHardwareStatus = AUDIO_HW_IDLE;
1363
1364 return ret;
1365}
1366
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001367status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001368 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001369{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001370 Mutex::Autolock _l(mLock);
1371
1372 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1373 if (playbackThread != NULL) {
1374 return playbackThread->getRenderPosition(halFrames, dspFrames);
1375 }
1376
1377 return BAD_VALUE;
1378}
1379
1380void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1381{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001382 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001383 if (client == 0) {
1384 return;
1385 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001386 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001387 {
1388 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001389 if (mNotificationClients.indexOfKey(pid) < 0) {
1390 sp<NotificationClient> notificationClient = new NotificationClient(this,
1391 client,
1392 pid);
1393 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001394
Eric Laurent021cf962014-05-13 10:18:14 -07001395 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001396
Marco Nelissen06b46062014-11-14 07:58:25 -08001397 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001398 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001399 }
1400 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401
Eric Laurent021cf962014-05-13 10:18:14 -07001402 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001403 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001404 // the config change is always sent from playback or record threads to avoid deadlock
1405 // with AudioSystem::gLock
1406 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001407 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_REGISTERED, pid);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001408 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001410 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -07001411 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_REGISTERED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412 }
1413}
1414
1415void AudioFlinger::removeNotificationClient(pid_t pid)
1416{
1417 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001418 {
1419 Mutex::Autolock _cl(mClientLock);
1420 mNotificationClients.removeItem(pid);
1421 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001422
Steve Block3856b092011-10-20 11:56:00 +01001423 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001424 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001425 bool removed = false;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001426 for (size_t i = 0; i < num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001427 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001428 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001429 if (ref->mPid == pid) {
1430 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001431 mAudioSessionRefs.removeAt(i);
1432 delete ref;
1433 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001434 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001435 } else {
1436 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001437 }
1438 }
1439 if (removed) {
1440 purgeStaleEffects_l();
1441 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001442}
1443
Eric Laurent73e26b62015-04-27 16:55:58 -07001444void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001445 const sp<AudioIoDescriptor>& ioDesc,
1446 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447{
Eric Laurent021cf962014-05-13 10:18:14 -07001448 Mutex::Autolock _l(mClientLock);
1449 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001450 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001451 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1452 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1453 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001454 }
1455}
1456
Eric Laurent021cf962014-05-13 10:18:14 -07001457// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001458void AudioFlinger::removeClient_l(pid_t pid)
1459{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001460 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001461 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001462 mClients.removeItem(pid);
1463}
1464
Eric Laurent717e1282012-06-29 16:36:52 -07001465// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001466sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1467 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001468{
1469 sp<PlaybackThread> thread;
1470
1471 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1472 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1473 ALOG_ASSERT(thread == 0);
1474 thread = mPlaybackThreads.valueAt(i);
1475 }
1476 }
1477
1478 return thread;
1479}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001480
Mathias Agopian65ab4712010-07-14 17:59:35 -07001481
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482
1483// ----------------------------------------------------------------------------
1484
1485AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1486 : RefBase(),
1487 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001488 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001489{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301490 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1491 heapSize *= 1024;
1492 if (!heapSize) {
1493 heapSize = kClientSharedHeapSizeBytes;
1494 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1495 // invalidated tracks
1496 if (!audioFlinger->isLowRamDevice()) {
1497 heapSize *= kClientSharedHeapSizeMultiplier;
1498 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001499 }
1500 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001501}
1502
Eric Laurent021cf962014-05-13 10:18:14 -07001503// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001504AudioFlinger::Client::~Client()
1505{
1506 mAudioFlinger->removeClient_l(mPid);
1507}
1508
Glenn Kasten435dbe62012-01-30 10:15:48 -08001509sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001510{
1511 return mMemoryDealer;
1512}
1513
1514// ----------------------------------------------------------------------------
1515
1516AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1517 const sp<IAudioFlingerClient>& client,
1518 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001519 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520{
1521}
1522
1523AudioFlinger::NotificationClient::~NotificationClient()
1524{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525}
1526
Glenn Kasten0f11b512014-01-31 16:18:54 -08001527void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001528{
1529 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001530 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001531}
1532
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001533// ----------------------------------------------------------------------------
1534AudioFlinger::MediaLogNotifier::MediaLogNotifier()
1535 : mPendingRequests(false) {}
1536
1537
1538void AudioFlinger::MediaLogNotifier::requestMerge() {
1539 AutoMutex _l(mMutex);
1540 mPendingRequests = true;
1541 mCond.signal();
1542}
1543
1544bool AudioFlinger::MediaLogNotifier::threadLoop() {
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001545 // Should already have been checked, but just in case
1546 if (sMediaLogService == 0) {
1547 return false;
1548 }
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001549 // Wait until there are pending requests
1550 {
1551 AutoMutex _l(mMutex);
1552 mPendingRequests = false; // to ignore past requests
1553 while (!mPendingRequests) {
1554 mCond.wait(mMutex);
1555 // TODO may also need an exitPending check
1556 }
1557 mPendingRequests = false;
1558 }
1559 // Execute the actual MediaLogService binder call and ignore extra requests for a while
Glenn Kasten04b96fc2017-04-10 14:58:47 -07001560 sMediaLogService->requestMergeWakeup();
Nicolas Rouletdcdfaec2017-02-14 10:18:39 -08001561 usleep(kPostTriggerSleepPeriod);
1562 return true;
1563}
1564
1565void AudioFlinger::requestLogMerge() {
1566 mMediaLogNotifier->requestMerge();
1567}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001568
1569// ----------------------------------------------------------------------------
1570
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001571sp<media::IAudioRecord> AudioFlinger::openRecord(
1572 audio_io_handle_t input,
1573 uint32_t sampleRate,
1574 audio_format_t format,
1575 audio_channel_mask_t channelMask,
1576 const String16& opPackageName,
1577 size_t *frameCount,
1578 audio_input_flags_t *flags,
1579 pid_t pid,
1580 pid_t tid,
1581 int clientUid,
1582 audio_session_t *sessionId,
1583 size_t *notificationFrames,
1584 sp<IMemory>& cblk,
1585 sp<IMemory>& buffers,
1586 status_t *status,
1587 audio_port_handle_t portId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001588{
1589 sp<RecordThread::RecordTrack> recordTrack;
1590 sp<RecordHandle> recordHandle;
1591 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001592 status_t lStatus;
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001593 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001595 cblk.clear();
1596 buffers.clear();
Glenn Kastend776ac62014-05-07 09:16:09 -07001597
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001598 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001599 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1600 if (!isTrustedCallingUid(callingUid)) {
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001601 ALOGW_IF((uid_t)clientUid != callingUid,
1602 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001603 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001604 updatePid = true;
1605 }
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001606
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001607 if (updatePid) {
1608 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001609 ALOGW_IF(pid != -1 && pid != callingPid,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001610 "%s uid %d pid %d tried to pass itself off as pid %d",
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001611 __func__, callingUid, callingPid, pid);
1612 pid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001613 }
1614
Mathias Agopian65ab4712010-07-14 17:59:35 -07001615 // check calling permissions
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001616 if (!recordingAllowed(opPackageName, tid, clientUid)) {
1617 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001618 lStatus = PERMISSION_DENIED;
1619 goto Exit;
1620 }
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001621
1622 // further sample rate checks are performed by createRecordTrack_l()
1623 if (sampleRate == 0) {
1624 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1625 lStatus = BAD_VALUE;
1626 goto Exit;
1627 }
1628
Andy Hung6770c6f2015-04-07 13:43:36 -07001629 // we don't yet support anything other than linear PCM
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001630 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
1631 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001632 lStatus = BAD_VALUE;
1633 goto Exit;
1634 }
1635
Glenn Kasten53b5d092014-02-05 10:00:23 -08001636 // further channel mask checks are performed by createRecordTrack_l()
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001637 if (!audio_is_input_channel(channelMask)) {
1638 ALOGE("openRecord() invalid channel mask %#x", channelMask);
Glenn Kasten53b5d092014-02-05 10:00:23 -08001639 lStatus = BAD_VALUE;
1640 goto Exit;
1641 }
1642
Glenn Kasten05997e22014-03-13 15:08:33 -07001643 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001644 Mutex::Autolock _l(mLock);
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001645 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001646 if (thread == NULL) {
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001647 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648 lStatus = BAD_VALUE;
1649 goto Exit;
1650 }
1651
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001652 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001653
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001654 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
1655 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1656 lStatus = BAD_VALUE;
1657 goto Exit;
1658 }
1659 lSessionId = *sessionId;
1660 } else {
1661 // if no audio session id is provided, create one here
1662 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
1663 if (sessionId != NULL) {
1664 *sessionId = lSessionId;
1665 }
1666 }
1667 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001668
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001669 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1670 frameCount, lSessionId, notificationFrames,
1671 clientUid, flags, tid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001672 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001673
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001674 if (lStatus == NO_ERROR) {
1675 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1676 // session and move it to this thread.
1677 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
1678 if (chain != 0) {
1679 Mutex::Autolock _l(thread->mLock);
1680 thread->addEffectChain_l(chain);
1681 }
Eric Laurent1b928682014-10-02 19:41:47 -07001682 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001683 }
Glenn Kastene198c362013-08-13 09:13:36 -07001684
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001685 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001686 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001687 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001688 // Don't hold mClientLock when releasing the reference on the track as the
1689 // destructor will acquire it.
1690 {
1691 Mutex::Autolock _cl(mClientLock);
1692 client.clear();
1693 }
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001694 recordTrack.clear();
1695 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001696 }
1697
Eric Laurent0aa3c6e2017-12-08 18:13:22 +00001698 cblk = recordTrack->getCblk();
1699 buffers = recordTrack->getBuffers();
1700
1701 // return handle to client
1702 recordHandle = new RecordHandle(recordTrack);
1703
1704Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001705 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001706 return recordHandle;
1707}
1708
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001709
1710
Mathias Agopian65ab4712010-07-14 17:59:35 -07001711// ----------------------------------------------------------------------------
1712
Eric Laurenta4c5a552012-03-29 10:12:40 -07001713audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1714{
Eric Laurent44622db2014-08-01 19:00:33 -07001715 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001716 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001717 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001718 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001719 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001720 }
1721 Mutex::Autolock _l(mLock);
1722 return loadHwModule_l(name);
1723}
1724
1725// loadHwModule_l() must be called with AudioFlinger::mLock held
1726audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1727{
1728 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1729 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1730 ALOGW("loadHwModule() module %s already loaded", name);
1731 return mAudioHwDevs.keyAt(i);
1732 }
1733 }
1734
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001735 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001736
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001737 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001738 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001739 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001740 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001741 }
1742
1743 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001744 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07001745 mHardwareStatus = AUDIO_HW_IDLE;
1746 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001747 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001748 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001749 }
1750
John Grossmanee578c02012-07-23 17:05:46 -07001751 // Check and cache this HAL's level of support for master mute and master
1752 // volume. If this is the first HAL opened, and it supports the get
1753 // methods, use the initial values provided by the HAL as the current
1754 // master mute and volume settings.
1755
1756 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1757 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001758 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001759
1760 if (0 == mAudioHwDevs.size()) {
1761 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001762 float mv;
1763 if (OK == dev->getMasterVolume(&mv)) {
1764 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07001765 }
1766
1767 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001768 bool mm;
1769 if (OK == dev->getMasterMute(&mm)) {
1770 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07001771 }
1772 }
1773
Eric Laurenta4c5a552012-03-29 10:12:40 -07001774 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001775 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07001776 flags = static_cast<AudioHwDevice::Flags>(flags |
1777 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1778 }
1779
1780 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001781 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07001782 flags = static_cast<AudioHwDevice::Flags>(flags |
1783 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1784 }
1785
Eric Laurenta4c5a552012-03-29 10:12:40 -07001786 mHardwareStatus = AUDIO_HW_IDLE;
1787 }
1788
Glenn Kastena13cde92016-03-28 15:26:02 -07001789 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001790 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001791
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001792 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001793
1794 return handle;
1795
1796}
1797
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001798// ----------------------------------------------------------------------------
1799
Glenn Kasten3b16c762012-11-14 08:44:39 -08001800uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
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->sampleRate() : 0;
1805}
1806
Glenn Kastene33054e2012-11-14 12:54:39 -08001807size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001808{
1809 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001810 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001811 return thread != NULL ? thread->frameCountHAL() : 0;
1812}
1813
1814// ----------------------------------------------------------------------------
1815
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001816status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1817{
1818 uid_t uid = IPCThreadState::self()->getCallingUid();
1819 if (uid != AID_SYSTEM) {
1820 return PERMISSION_DENIED;
1821 }
1822 Mutex::Autolock _l(mLock);
1823 if (mIsDeviceTypeKnown) {
1824 return INVALID_OPERATION;
1825 }
1826 mIsLowRamDevice = isLowRamDevice;
1827 mIsDeviceTypeKnown = true;
1828 return NO_ERROR;
1829}
1830
Eric Laurent93c3d412014-08-01 14:48:35 -07001831audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1832{
1833 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001834
1835 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1836 if (index >= 0) {
1837 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1838 mHwAvSyncIds.valueAt(index), sessionId);
1839 return mHwAvSyncIds.valueAt(index);
1840 }
1841
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001842 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07001843 if (dev == NULL) {
1844 return AUDIO_HW_SYNC_INVALID;
1845 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001846 String8 reply;
1847 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001848 if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001849 param = AudioParameter(reply);
1850 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001851
1852 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001853 if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001854 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1855 return AUDIO_HW_SYNC_INVALID;
1856 }
1857
1858 // allow only one session for a given HW A/V sync ID.
1859 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1860 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1861 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1862 value, mHwAvSyncIds.keyAt(i));
1863 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001864 break;
1865 }
1866 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001867
1868 mHwAvSyncIds.add(sessionId, value);
1869
1870 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1871 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1872 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07001873 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001874 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001875 param.addInt(String8(AudioParameter::keyStreamHwAvSync), value);
Eric Laurentfa90e842014-10-17 18:12:31 -07001876 thread->setParameters(param.toString());
1877 break;
1878 }
1879 }
1880
1881 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1882 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001883}
1884
Eric Laurent72e3f392015-05-20 14:43:50 -07001885status_t AudioFlinger::systemReady()
1886{
1887 Mutex::Autolock _l(mLock);
1888 ALOGI("%s", __FUNCTION__);
1889 if (mSystemReady) {
1890 ALOGW("%s called twice", __FUNCTION__);
1891 return NO_ERROR;
1892 }
1893 mSystemReady = true;
1894 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1895 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1896 thread->systemReady();
1897 }
1898 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1899 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1900 thread->systemReady();
1901 }
1902 return NO_ERROR;
1903}
1904
Eric Laurentfa90e842014-10-17 18:12:31 -07001905// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1906void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1907{
1908 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1909 if (index >= 0) {
1910 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1911 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1912 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001913 param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId);
Eric Laurentfa90e842014-10-17 18:12:31 -07001914 thread->setParameters(param.toString());
1915 }
1916}
1917
1918
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001919// ----------------------------------------------------------------------------
1920
Eric Laurent83b88082014-06-20 18:31:16 -07001921
Eric Laurent6acd1d42017-01-04 14:23:29 -08001922sp<AudioFlinger::ThreadBase> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001923 audio_io_handle_t *output,
1924 audio_config_t *config,
1925 audio_devices_t devices,
1926 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001927 audio_output_flags_t flags)
1928{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001929 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001930 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001931 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001932 }
1933
Eric Laurentcf2c0212014-07-25 16:20:43 -07001934 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001935 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1936 } else {
1937 // Audio Policy does not currently request a specific output handle.
1938 // If this is ever needed, see openInput_l() for example code.
1939 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1940 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001941 }
Eric Laurent83b88082014-06-20 18:31:16 -07001942
1943 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1944
Eric Laurent83b88082014-06-20 18:31:16 -07001945 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001946 // This if statement allows overriding the audio policy settings
1947 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1948 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1949 // Check only for Normal Mixing mode
1950 if (kEnableExtendedPrecision) {
1951 // Specify format (uncomment one below to choose)
1952 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1953 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1954 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1955 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001956 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001957 }
1958 if (kEnableExtendedChannels) {
1959 // Specify channel mask (uncomment one below to choose)
1960 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1961 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1962 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1963 }
Eric Laurent83b88082014-06-20 18:31:16 -07001964 }
1965
Phil Burk062e67a2015-02-11 13:40:50 -08001966 AudioStreamOut *outputStream = NULL;
1967 status_t status = outHwDev->openOutputStream(
1968 &outputStream,
1969 *output,
1970 devices,
1971 flags,
1972 config,
1973 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001974
1975 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001976
Phil Burk062e67a2015-02-11 13:40:50 -08001977 if (status == NO_ERROR) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001978 if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
1979 sp<MmapPlaybackThread> thread =
1980 new MmapPlaybackThread(this, *output, outHwDev, outputStream,
1981 devices, AUDIO_DEVICE_NONE, mSystemReady);
1982 mMmapThreads.add(*output, thread);
1983 ALOGV("openOutput_l() created mmap playback thread: ID %d thread %p",
1984 *output, thread.get());
1985 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07001986 } else {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001987 sp<PlaybackThread> thread;
1988 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1989 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
1990 ALOGV("openOutput_l() created offload output: ID %d thread %p",
1991 *output, thread.get());
1992 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1993 || !isValidPcmSinkFormat(config->format)
1994 || !isValidPcmSinkChannelMask(config->channel_mask)) {
1995 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
1996 ALOGV("openOutput_l() created direct output: ID %d thread %p",
1997 *output, thread.get());
1998 } else {
1999 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
2000 ALOGV("openOutput_l() created mixer output: ID %d thread %p",
2001 *output, thread.get());
2002 }
2003 mPlaybackThreads.add(*output, thread);
2004 return thread;
Eric Laurent83b88082014-06-20 18:31:16 -07002005 }
Eric Laurent83b88082014-06-20 18:31:16 -07002006 }
2007
2008 return 0;
2009}
2010
Eric Laurentcf2c0212014-07-25 16:20:43 -07002011status_t AudioFlinger::openOutput(audio_module_handle_t module,
2012 audio_io_handle_t *output,
2013 audio_config_t *config,
2014 audio_devices_t *devices,
2015 const String8& address,
2016 uint32_t *latencyMs,
2017 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002018{
Glenn Kastend3bb6452016-12-05 18:14:37 -08002019 ALOGI("openOutput() this %p, module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, "
2020 "flags %x",
Eric Laurent6acd1d42017-01-04 14:23:29 -08002021 this, module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002022 (devices != NULL) ? *devices : 0,
2023 config->sample_rate,
2024 config->format,
2025 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002026 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002027
Yunlian Jiang32ba9862017-01-31 15:55:00 -08002028 if (devices == NULL || *devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002029 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002030 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002031
Mathias Agopian65ab4712010-07-14 17:59:35 -07002032 Mutex::Autolock _l(mLock);
2033
Eric Laurent6acd1d42017-01-04 14:23:29 -08002034 sp<ThreadBase> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002035 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08002036 if ((flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) {
2037 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
2038 *latencyMs = playbackThread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002039
Eric Laurent6acd1d42017-01-04 14:23:29 -08002040 // notify client processes of the new output creation
2041 playbackThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002042
Eric Laurent6acd1d42017-01-04 14:23:29 -08002043 // the first primary output opened designates the primary hw device
2044 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -08002045 ALOGI("Using module %d as the primary audio interface", module);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002046 mPrimaryHardwareDev = playbackThread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07002047
Eric Laurent6acd1d42017-01-04 14:23:29 -08002048 AutoMutex lock(mHardwareLock);
2049 mHardwareStatus = AUDIO_HW_SET_MODE;
2050 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
2051 mHardwareStatus = AUDIO_HW_IDLE;
2052 }
2053 } else {
2054 MmapThread *mmapThread = (MmapThread *)thread.get();
2055 mmapThread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002056 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002057 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002058 }
2059
Eric Laurentcf2c0212014-07-25 16:20:43 -07002060 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002061}
2062
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002063audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
2064 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002065{
2066 Mutex::Autolock _l(mLock);
2067 MixerThread *thread1 = checkMixerThread_l(output1);
2068 MixerThread *thread2 = checkMixerThread_l(output2);
2069
2070 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002071 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
2072 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07002073 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002074 }
2075
Glenn Kasteneeecb982016-02-26 10:44:04 -08002076 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07002077 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002078 thread->addOutputTrack(thread2);
2079 mPlaybackThreads.add(id, thread);
2080 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002081 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002082 return id;
2083}
2084
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002085status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002086{
Glenn Kastend96c5722012-04-25 13:44:49 -07002087 return closeOutput_nonvirtual(output);
2088}
2089
2090status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
2091{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002092 // keep strong reference on the playback thread so that
2093 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002094 sp<PlaybackThread> playbackThread;
2095 sp<MmapPlaybackThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002096 {
2097 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002098 playbackThread = checkPlaybackThread_l(output);
2099 if (playbackThread != NULL) {
2100 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002101
Eric Laurent6acd1d42017-01-04 14:23:29 -08002102 if (playbackThread->type() == ThreadBase::MIXER) {
2103 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2104 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
2105 DuplicatingThread *dupThread =
2106 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
2107 dupThread->removeOutputTrack((MixerThread *)playbackThread.get());
2108 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002109 }
2110 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08002111
2112
Eric Laurent6acd1d42017-01-04 14:23:29 -08002113 mPlaybackThreads.removeItem(output);
2114 // save all effects to the default thread
2115 if (mPlaybackThreads.size()) {
2116 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
2117 if (dstThread != NULL) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002118 // audioflinger lock is held so order of thread lock acquisition doesn't matter
Eric Laurent6acd1d42017-01-04 14:23:29 -08002119 Mutex::Autolock _dl(dstThread->mLock);
2120 Mutex::Autolock _sl(playbackThread->mLock);
2121 Vector< sp<EffectChain> > effectChains = playbackThread->getEffectChains_l();
2122 for (size_t i = 0; i < effectChains.size(); i ++) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002123 moveEffectChain_l(effectChains[i]->sessionId(), playbackThread.get(),
2124 dstThread, true);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002125 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002126 }
2127 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002128 } else {
2129 mmapThread = (MmapPlaybackThread *)checkMmapThread_l(output);
2130 if (mmapThread == 0) {
2131 return BAD_VALUE;
2132 }
2133 mMmapThreads.removeItem(output);
Phil Burk7f6b40d2017-02-09 13:18:38 -08002134 ALOGD("closing mmapThread %p", mmapThread.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002135 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002136 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2137 ioDesc->mIoHandle = output;
2138 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002139 }
Glenn Kastenb28686f2012-01-06 08:39:38 -08002140 // The thread entity (active unit of execution) is no longer running here,
2141 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002142
Eric Laurent6acd1d42017-01-04 14:23:29 -08002143 if (playbackThread != 0) {
2144 playbackThread->exit();
2145 if (!playbackThread->isDuplicating()) {
2146 closeOutputFinish(playbackThread);
2147 }
2148 } else if (mmapThread != 0) {
Phil Burk7f6b40d2017-02-09 13:18:38 -08002149 ALOGD("mmapThread exit()");
Eric Laurent6acd1d42017-01-04 14:23:29 -08002150 mmapThread->exit();
2151 AudioStreamOut *out = mmapThread->clearOutput();
2152 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2153 // from now on thread->mOutput is NULL
2154 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002155 }
2156 return NO_ERROR;
2157}
2158
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002159void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002160{
2161 AudioStreamOut *out = thread->clearOutput();
2162 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2163 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07002164 delete out;
2165}
2166
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002167void AudioFlinger::closeOutputInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002168{
2169 mPlaybackThreads.removeItem(thread->mId);
2170 thread->exit();
2171 closeOutputFinish(thread);
2172}
2173
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002174status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002175{
2176 Mutex::Autolock _l(mLock);
2177 PlaybackThread *thread = checkPlaybackThread_l(output);
2178
2179 if (thread == NULL) {
2180 return BAD_VALUE;
2181 }
2182
Steve Block3856b092011-10-20 11:56:00 +01002183 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184 thread->suspend();
2185
2186 return NO_ERROR;
2187}
2188
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002189status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002190{
2191 Mutex::Autolock _l(mLock);
2192 PlaybackThread *thread = checkPlaybackThread_l(output);
2193
2194 if (thread == NULL) {
2195 return BAD_VALUE;
2196 }
2197
Steve Block3856b092011-10-20 11:56:00 +01002198 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002199
2200 thread->restore();
2201
2202 return NO_ERROR;
2203}
2204
Eric Laurentcf2c0212014-07-25 16:20:43 -07002205status_t AudioFlinger::openInput(audio_module_handle_t module,
2206 audio_io_handle_t *input,
2207 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002208 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002209 const String8& address,
2210 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002211 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002212{
Eric Laurent83b88082014-06-20 18:31:16 -07002213 Mutex::Autolock _l(mLock);
2214
Glenn Kastene7d66712015-03-05 13:46:52 -08002215 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002216 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002217 }
2218
Eric Laurent6acd1d42017-01-04 14:23:29 -08002219 sp<ThreadBase> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002220
2221 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002222 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002223 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002224 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002225 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002226 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002227}
Dima Zavin799a70e2011-04-18 16:57:27 -07002228
Eric Laurent6acd1d42017-01-04 14:23:29 -08002229sp<AudioFlinger::ThreadBase> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002230 audio_io_handle_t *input,
2231 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002232 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002233 const String8& address,
2234 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002235 audio_input_flags_t flags)
2236{
Glenn Kastene7d66712015-03-05 13:46:52 -08002237 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002238 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002239 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002240 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002241 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002242
Glenn Kasteneeecb982016-02-26 10:44:04 -08002243 // Audio Policy can request a specific handle for hardware hotword.
2244 // The goal here is not to re-open an already opened input.
2245 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002246 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002247 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2248 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2249 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2250 return 0;
2251 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2252 // This should not happen in a transient state with current design.
2253 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2254 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002255 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002256
Eric Laurentcf2c0212014-07-25 16:20:43 -07002257 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002258 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002259 sp<StreamInHalInterface> inStream;
2260 status_t status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002261 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Mikhail Naganovf558e022016-11-14 17:45:17 -08002262 ALOGV("openInput_l() openInputStream returned input %p, devices %x, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002263 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002264 inStream.get(),
Mikhail Naganovf558e022016-11-14 17:45:17 -08002265 devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002266 halconfig.sample_rate,
2267 halconfig.format,
2268 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002269 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002270 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002271
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002272 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002273 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002274 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002275 audio_is_linear_pcm(config->format) &&
2276 audio_is_linear_pcm(halconfig.format) &&
2277 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002278 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2279 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002280 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002281 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002282 inStream.clear();
2283 status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002284 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002285 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002286 }
2287
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002288 if (status == NO_ERROR && inStream != 0) {
Eric Laurent05067782016-06-01 18:27:28 -07002289 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002290 if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) {
2291 sp<MmapCaptureThread> thread =
2292 new MmapCaptureThread(this, *input,
2293 inHwDev, inputStream,
2294 primaryOutputDevice_l(), devices, mSystemReady);
2295 mMmapThreads.add(*input, thread);
Glenn Kastend3bb6452016-12-05 18:14:37 -08002296 ALOGV("openInput_l() created mmap capture thread: ID %d thread %p", *input,
2297 thread.get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002298 return thread;
2299 } else {
Glenn Kasten46909e72013-02-26 09:20:22 -08002300#ifdef TEE_SINK
Eric Laurent6acd1d42017-01-04 14:23:29 -08002301 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2302 // or (re-)create if current Pipe is idle and does not match the new format
2303 sp<NBAIO_Sink> teeSink;
2304 enum {
2305 TEE_SINK_NO, // don't copy input
2306 TEE_SINK_NEW, // copy input using a new pipe
2307 TEE_SINK_OLD, // copy input using an existing pipe
2308 } kind;
2309 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2310 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
2311 if (!mTeeSinkInputEnabled) {
2312 kind = TEE_SINK_NO;
2313 } else if (!Format_isValid(format)) {
2314 kind = TEE_SINK_NO;
2315 } else if (mRecordTeeSink == 0) {
2316 kind = TEE_SINK_NEW;
2317 } else if (mRecordTeeSink->getStrongCount() != 1) {
2318 kind = TEE_SINK_NO;
2319 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
2320 kind = TEE_SINK_OLD;
2321 } else {
2322 kind = TEE_SINK_NEW;
2323 }
2324 switch (kind) {
2325 case TEE_SINK_NEW: {
2326 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
2327 size_t numCounterOffers = 0;
2328 const NBAIO_Format offers[1] = {format};
2329 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2330 ALOG_ASSERT(index == 0);
2331 PipeReader *pipeReader = new PipeReader(*pipe);
2332 numCounterOffers = 0;
2333 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2334 ALOG_ASSERT(index == 0);
2335 mRecordTeeSink = pipe;
2336 mRecordTeeSource = pipeReader;
2337 teeSink = pipe;
2338 }
2339 break;
2340 case TEE_SINK_OLD:
2341 teeSink = mRecordTeeSink;
2342 break;
2343 case TEE_SINK_NO:
2344 default:
2345 break;
2346 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002347#endif
Eric Laurent6acd1d42017-01-04 14:23:29 -08002348
2349 // Start record thread
2350 // RecordThread requires both input and output device indication to forward to audio
2351 // pre processing modules
2352 sp<RecordThread> thread = new RecordThread(this,
2353 inputStream,
2354 *input,
2355 primaryOutputDevice_l(),
2356 devices,
2357 mSystemReady
2358#ifdef TEE_SINK
2359 , teeSink
2360#endif
2361 );
2362 mRecordThreads.add(*input, thread);
2363 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
2364 return thread;
2365 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002366 }
2367
Eric Laurentcf2c0212014-07-25 16:20:43 -07002368 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002369 return 0;
2370}
2371
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002372status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002373{
Glenn Kastend96c5722012-04-25 13:44:49 -07002374 return closeInput_nonvirtual(input);
2375}
2376
2377status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2378{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002379 // keep strong reference on the record thread so that
2380 // it is not destroyed while exit() is executed
Eric Laurent6acd1d42017-01-04 14:23:29 -08002381 sp<RecordThread> recordThread;
2382 sp<MmapCaptureThread> mmapThread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002383 {
2384 Mutex::Autolock _l(mLock);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002385 recordThread = checkRecordThread_l(input);
2386 if (recordThread != 0) {
2387 ALOGV("closeInput() %d", input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002388
Eric Laurent6acd1d42017-01-04 14:23:29 -08002389 // If we still have effect chains, it means that a client still holds a handle
2390 // on at least one effect. We must either move the chain to an existing thread with the
2391 // same session ID or put it aside in case a new record thread is opened for a
2392 // new capture on the same session
2393 sp<EffectChain> chain;
2394 {
2395 Mutex::Autolock _sl(recordThread->mLock);
2396 Vector< sp<EffectChain> > effectChains = recordThread->getEffectChains_l();
2397 // Note: maximum one chain per record thread
2398 if (effectChains.size() != 0) {
2399 chain = effectChains[0];
Eric Laurent1b928682014-10-02 19:41:47 -07002400 }
2401 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002402 if (chain != 0) {
Glenn Kastend3bb6452016-12-05 18:14:37 -08002403 // first check if a record thread is already opened with a client on same session.
Eric Laurent6acd1d42017-01-04 14:23:29 -08002404 // This should only happen in case of overlap between one thread tear down and the
2405 // creation of its replacement
2406 size_t i;
2407 for (i = 0; i < mRecordThreads.size(); i++) {
2408 sp<RecordThread> t = mRecordThreads.valueAt(i);
2409 if (t == recordThread) {
2410 continue;
2411 }
2412 if (t->hasAudioSession(chain->sessionId()) != 0) {
2413 Mutex::Autolock _l(t->mLock);
2414 ALOGV("closeInput() found thread %d for effect session %d",
2415 t->id(), chain->sessionId());
2416 t->addEffectChain_l(chain);
2417 break;
2418 }
2419 }
Glenn Kastend3bb6452016-12-05 18:14:37 -08002420 // put the chain aside if we could not find a record thread with the same session id
Eric Laurent6acd1d42017-01-04 14:23:29 -08002421 if (i == mRecordThreads.size()) {
2422 putOrphanEffectChain_l(chain);
2423 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002424 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002425 mRecordThreads.removeItem(input);
2426 } else {
2427 mmapThread = (MmapCaptureThread *)checkMmapThread_l(input);
2428 if (mmapThread == 0) {
2429 return BAD_VALUE;
2430 }
2431 mMmapThreads.removeItem(input);
Eric Laurentaaa44472014-09-12 17:41:50 -07002432 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002433 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2434 ioDesc->mIoHandle = input;
2435 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002436 }
Eric Laurent83b88082014-06-20 18:31:16 -07002437 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2438 // we have a different lock for notification client
Eric Laurent6acd1d42017-01-04 14:23:29 -08002439 if (recordThread != 0) {
2440 closeInputFinish(recordThread);
2441 } else if (mmapThread != 0) {
2442 mmapThread->exit();
2443 AudioStreamIn *in = mmapThread->clearInput();
2444 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
2445 // from now on thread->mInput is NULL
2446 delete in;
2447 }
Eric Laurent83b88082014-06-20 18:31:16 -07002448 return NO_ERROR;
2449}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002450
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002451void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002452{
2453 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002454 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002455 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002456 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002457 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002458}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002459
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002460void AudioFlinger::closeInputInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002461{
2462 mRecordThreads.removeItem(thread->mId);
2463 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002464}
2465
Glenn Kastend2304db2014-02-03 07:40:31 -08002466status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002467{
2468 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002469 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002470
2471 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2472 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002473 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002474 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002475 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2476 mMmapThreads[i]->invalidateTracks(stream);
2477 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002478 return NO_ERROR;
2479}
2480
2481
Glenn Kasteneeecb982016-02-26 10:44:04 -08002482audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002483{
Glenn Kasten9d003132016-04-06 14:38:09 -07002484 // This is a binder API, so a malicious client could pass in a bad parameter.
2485 // Check for that before calling the internal API nextUniqueId().
2486 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2487 ALOGE("newAudioUniqueId invalid use %d", use);
2488 return AUDIO_UNIQUE_ID_ALLOCATE;
2489 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002490 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002491}
2492
Glenn Kastend848eb42016-03-08 13:42:11 -08002493void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002494{
2495 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002496 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002497 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2498 if (pid != -1 && (caller == getpid_cached)) {
2499 caller = pid;
2500 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002501
Eric Laurent021cf962014-05-13 10:18:14 -07002502 {
2503 Mutex::Autolock _cl(mClientLock);
2504 // Ignore requests received from processes not known as notification client. The request
2505 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2506 // called from a different pid leaving a stale session reference. Also we don't know how
2507 // to clear this reference if the client process dies.
2508 if (mNotificationClients.indexOfKey(caller) < 0) {
2509 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2510 return;
2511 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002512 }
2513
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002514 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002515 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002516 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002517 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2518 ref->mCnt++;
2519 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002520 return;
2521 }
2522 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002523 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2524 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002525}
2526
Glenn Kastend848eb42016-03-08 13:42:11 -08002527void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002528{
2529 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002530 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002531 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2532 if (pid != -1 && (caller == getpid_cached)) {
2533 caller = pid;
2534 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002535 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002536 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002537 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002538 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2539 ref->mCnt--;
2540 ALOGV(" decremented refcount to %d", ref->mCnt);
2541 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002542 mAudioSessionRefs.removeAt(i);
2543 delete ref;
2544 purgeStaleEffects_l();
2545 }
2546 return;
2547 }
2548 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002549 // If the caller is mediaserver it is likely that the session being released was acquired
2550 // on behalf of a process not in notification clients and we ignore the warning.
2551 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002552}
2553
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002554bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
2555{
2556 size_t num = mAudioSessionRefs.size();
2557 for (size_t i = 0; i < num; i++) {
2558 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2559 if (ref->mSessionid == audioSession) {
2560 return true;
2561 }
2562 }
2563 return false;
2564}
2565
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002566void AudioFlinger::purgeStaleEffects_l() {
2567
Steve Block3856b092011-10-20 11:56:00 +01002568 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002569
2570 Vector< sp<EffectChain> > chains;
2571
2572 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2573 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2574 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2575 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002576 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2577 chains.push(ec);
2578 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002579 }
2580 }
2581 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2582 sp<RecordThread> t = mRecordThreads.valueAt(i);
2583 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2584 sp<EffectChain> ec = t->mEffectChains[j];
2585 chains.push(ec);
2586 }
2587 }
2588
2589 for (size_t i = 0; i < chains.size(); i++) {
2590 sp<EffectChain> ec = chains[i];
2591 int sessionid = ec->sessionId();
2592 sp<ThreadBase> t = ec->mThread.promote();
2593 if (t == 0) {
2594 continue;
2595 }
2596 size_t numsessionrefs = mAudioSessionRefs.size();
2597 bool found = false;
2598 for (size_t k = 0; k < numsessionrefs; k++) {
2599 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002600 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002601 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002602 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002603 found = true;
2604 break;
2605 }
2606 }
2607 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002608 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002609 // remove all effects from the chain
2610 while (ec->mEffects.size()) {
2611 sp<EffectModule> effect = ec->mEffects[0];
2612 effect->unPin();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07002613 t->removeEffect_l(effect, /*release*/ true);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002614 if (effect->purgeHandles()) {
2615 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002616 }
2617 AudioSystem::unregisterEffect(effect->id());
2618 }
2619 }
2620 }
2621 return;
2622}
2623
Glenn Kasteneeecb982016-02-26 10:44:04 -08002624// checkThread_l() must be called with AudioFlinger::mLock held
2625AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2626{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002627 ThreadBase *thread = checkMmapThread_l(ioHandle);
2628 if (thread == 0) {
2629 switch (audio_unique_id_get_use(ioHandle)) {
2630 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2631 thread = checkPlaybackThread_l(ioHandle);
2632 break;
2633 case AUDIO_UNIQUE_ID_USE_INPUT:
2634 thread = checkRecordThread_l(ioHandle);
2635 break;
2636 default:
2637 break;
2638 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002639 }
2640 return thread;
2641}
2642
Mathias Agopian65ab4712010-07-14 17:59:35 -07002643// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002644AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002645{
Glenn Kastena1117922012-01-26 10:53:32 -08002646 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002647}
2648
2649// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002650AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002651{
2652 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002653 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002654}
2655
2656// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002657AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002658{
Glenn Kastena1117922012-01-26 10:53:32 -08002659 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002660}
2661
Eric Laurent6acd1d42017-01-04 14:23:29 -08002662// checkMmapThread_l() must be called with AudioFlinger::mLock held
2663AudioFlinger::MmapThread *AudioFlinger::checkMmapThread_l(audio_io_handle_t io) const
2664{
2665 return mMmapThreads.valueFor(io).get();
2666}
2667
2668
2669// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
2670AudioFlinger::VolumeInterface *AudioFlinger::getVolumeInterface_l(audio_io_handle_t output) const
2671{
Eric Laurent7459b902017-04-19 18:10:41 -07002672 VolumeInterface *volumeInterface = mPlaybackThreads.valueFor(output).get();
Eric Laurent6acd1d42017-01-04 14:23:29 -08002673 if (volumeInterface == nullptr) {
2674 MmapThread *mmapThread = mMmapThreads.valueFor(output).get();
2675 if (mmapThread != nullptr) {
2676 if (mmapThread->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07002677 MmapPlaybackThread *mmapPlaybackThread =
2678 static_cast<MmapPlaybackThread *>(mmapThread);
2679 volumeInterface = mmapPlaybackThread;
Eric Laurent6acd1d42017-01-04 14:23:29 -08002680 }
2681 }
2682 }
2683 return volumeInterface;
2684}
2685
2686Vector <AudioFlinger::VolumeInterface *> AudioFlinger::getAllVolumeInterfaces_l() const
2687{
2688 Vector <VolumeInterface *> volumeInterfaces;
2689 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7459b902017-04-19 18:10:41 -07002690 volumeInterfaces.add(mPlaybackThreads.valueAt(i).get());
Eric Laurent6acd1d42017-01-04 14:23:29 -08002691 }
2692 for (size_t i = 0; i < mMmapThreads.size(); i++) {
2693 if (mMmapThreads.valueAt(i)->isOutput()) {
Eric Laurent7459b902017-04-19 18:10:41 -07002694 MmapPlaybackThread *mmapPlaybackThread =
2695 static_cast<MmapPlaybackThread *>(mMmapThreads.valueAt(i).get());
2696 volumeInterfaces.add(mmapPlaybackThread);
Eric Laurent6acd1d42017-01-04 14:23:29 -08002697 }
2698 }
2699 return volumeInterfaces;
2700}
2701
Glenn Kasteneeecb982016-02-26 10:44:04 -08002702audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002703{
Glenn Kasten9d003132016-04-06 14:38:09 -07002704 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002705 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002706 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2707 for (int retry = 0; retry < maxRetries; retry++) {
2708 // The cast allows wraparound from max positive to min negative instead of abort
2709 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2710 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2711 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2712 // allow wrap by skipping 0 and -1 for session ids
2713 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2714 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2715 return (audio_unique_id_t) (base | use);
2716 }
2717 }
2718 // We have no way of recovering from wraparound
2719 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2720 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002721}
2722
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002723AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002724{
2725 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2726 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002727 if(thread->isDuplicating()) {
2728 continue;
2729 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002730 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002731 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002732 return thread;
2733 }
2734 }
2735 return NULL;
2736}
2737
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002738audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002739{
2740 PlaybackThread *thread = primaryPlaybackThread_l();
2741
2742 if (thread == NULL) {
2743 return 0;
2744 }
2745
Eric Laurentf1c04f92012-08-28 14:26:53 -07002746 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002747}
2748
Glenn Kastena7335632016-06-09 17:09:53 -07002749AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
2750{
2751 size_t minFrameCount = 0;
2752 PlaybackThread *minThread = NULL;
2753 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2754 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2755 if (!thread->isDuplicating()) {
2756 size_t frameCount = thread->frameCountHAL();
2757 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
2758 (frameCount == minFrameCount && thread->hasFastMixer() &&
2759 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
2760 minFrameCount = frameCount;
2761 minThread = thread;
2762 }
2763 }
2764 }
2765 return minThread;
2766}
2767
Eric Laurenta011e352012-03-29 15:51:43 -07002768sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002769 audio_session_t triggerSession,
2770 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002771 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002772 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002773{
2774 Mutex::Autolock _l(mLock);
2775
2776 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2777 status_t playStatus = NAME_NOT_FOUND;
2778 status_t recStatus = NAME_NOT_FOUND;
2779 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2780 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2781 if (playStatus == NO_ERROR) {
2782 return event;
2783 }
2784 }
2785 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2786 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2787 if (recStatus == NO_ERROR) {
2788 return event;
2789 }
2790 }
2791 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2792 mPendingSyncEvents.add(event);
2793 } else {
2794 ALOGV("createSyncEvent() invalid event %d", event->type());
2795 event.clear();
2796 }
2797 return event;
2798}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002799
Mathias Agopian65ab4712010-07-14 17:59:35 -07002800// ----------------------------------------------------------------------------
2801// Effect management
2802// ----------------------------------------------------------------------------
2803
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002804sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
2805 return mEffectsFactoryHal;
2806}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002807
Glenn Kastenf587ba52012-01-26 16:25:10 -08002808status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002809{
2810 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002811 if (mEffectsFactoryHal.get()) {
2812 return mEffectsFactoryHal->queryNumberEffects(numEffects);
2813 } else {
2814 return -ENODEV;
2815 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002816}
2817
Glenn Kastenf587ba52012-01-26 16:25:10 -08002818status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002819{
2820 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002821 if (mEffectsFactoryHal.get()) {
2822 return mEffectsFactoryHal->getDescriptor(index, descriptor);
2823 } else {
2824 return -ENODEV;
2825 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002826}
2827
Glenn Kasten5e92a782012-01-30 07:40:52 -08002828status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002829 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002830{
2831 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002832 if (mEffectsFactoryHal.get()) {
2833 return mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
2834 } else {
2835 return -ENODEV;
2836 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002837}
2838
2839
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002840sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002841 effect_descriptor_t *pDesc,
2842 const sp<IEffectClient>& effectClient,
2843 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002844 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002845 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002846 const String16& opPackageName,
Eric Laurentb6436272016-12-07 19:24:50 -08002847 pid_t pid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002848 status_t *status,
2849 int *id,
2850 int *enabled)
2851{
2852 status_t lStatus = NO_ERROR;
2853 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002854 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002855
Eric Laurentb6436272016-12-07 19:24:50 -08002856 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
2857 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
2858 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
2859 ALOGW_IF(pid != -1 && pid != callingPid,
2860 "%s uid %d pid %d tried to pass itself off as pid %d",
2861 __func__, callingUid, callingPid, pid);
2862 pid = callingPid;
2863 }
2864
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002865 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
2866 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002867
2868 if (pDesc == NULL) {
2869 lStatus = BAD_VALUE;
2870 goto Exit;
2871 }
2872
Eric Laurent84e9a102010-09-23 16:10:16 -07002873 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002874 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002875 lStatus = PERMISSION_DENIED;
2876 goto Exit;
2877 }
2878
Dima Zavinfce7a472011-04-19 22:30:36 -07002879 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002880 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002881 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002882 lStatus = PERMISSION_DENIED;
2883 goto Exit;
2884 }
2885
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002886 if (mEffectsFactoryHal == 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002887 lStatus = NO_INIT;
2888 goto Exit;
2889 }
2890
Mathias Agopian65ab4712010-07-14 17:59:35 -07002891 {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002892 if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002893 // if uuid is specified, request effect descriptor
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002894 lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002895 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002896 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002897 goto Exit;
2898 }
2899 } else {
2900 // if uuid is not specified, look for an available implementation
2901 // of the required type in effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002902 if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002903 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002904 lStatus = BAD_VALUE;
2905 goto Exit;
2906 }
2907 uint32_t numEffects = 0;
2908 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002909 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002910 bool found = false;
2911
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002912 lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002913 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002914 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002915 goto Exit;
2916 }
2917 for (uint32_t i = 0; i < numEffects; i++) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002918 lStatus = mEffectsFactoryHal->getDescriptor(i, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002919 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002920 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002921 continue;
2922 }
2923 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2924 // If matching type found save effect descriptor. If the session is
2925 // 0 and the effect is not auxiliary, continue enumeration in case
2926 // an auxiliary version of this effect type is available
2927 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002928 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002929 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002930 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2931 break;
2932 }
2933 }
2934 }
2935 if (!found) {
2936 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002937 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002938 goto Exit;
2939 }
2940 // For same effect type, chose auxiliary version over insert version if
2941 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002942 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002943 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002944 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002945 }
2946 }
2947
2948 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002949 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002950 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2951 lStatus = INVALID_OPERATION;
2952 goto Exit;
2953 }
2954
Eric Laurent59255e42011-07-27 19:49:51 -07002955 // check recording permission for visualizer
2956 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002957 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002958 lStatus = PERMISSION_DENIED;
2959 goto Exit;
2960 }
2961
Mathias Agopian65ab4712010-07-14 17:59:35 -07002962 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002963 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002964 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002965 // if the output returned by getOutputForEffect() is removed before we lock the
2966 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2967 // and we will exit safely
2968 io = AudioSystem::getOutputForEffect(&desc);
2969 ALOGV("createEffect got output %d", io);
2970 }
2971
2972 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002973
2974 // If output is not specified try to find a matching audio session ID in one of the
2975 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002976 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2977 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002978 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002979 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002980 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2981 // output must be specified by AudioPolicyManager when using session
2982 // AUDIO_SESSION_OUTPUT_STAGE
2983 lStatus = BAD_VALUE;
2984 goto Exit;
2985 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002986 // look for the thread where the specified audio session is present
2987 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2988 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2989 io = mPlaybackThreads.keyAt(i);
2990 break;
2991 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002992 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08002993 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002994 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2995 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2996 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002997 break;
2998 }
2999 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003000 }
Eric Laurent6acd1d42017-01-04 14:23:29 -08003001 if (io == AUDIO_IO_HANDLE_NONE) {
3002 for (size_t i = 0; i < mMmapThreads.size(); i++) {
3003 if (mMmapThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
3004 io = mMmapThreads.keyAt(i);
3005 break;
3006 }
3007 }
3008 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003009 // If no output thread contains the requested session ID, default to
3010 // first output. The effect chain will be moved to the correct output
3011 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07003012 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003013 io = mPlaybackThreads.keyAt(0);
3014 }
Steve Block3856b092011-10-20 11:56:00 +01003015 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07003016 }
3017 ThreadBase *thread = checkRecordThread_l(io);
3018 if (thread == NULL) {
3019 thread = checkPlaybackThread_l(io);
3020 if (thread == NULL) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08003021 thread = checkMmapThread_l(io);
3022 if (thread == NULL) {
3023 ALOGE("createEffect() unknown output thread");
3024 lStatus = BAD_VALUE;
3025 goto Exit;
3026 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003027 }
Eric Laurentaaa44472014-09-12 17:41:50 -07003028 } else {
3029 // Check if one effect chain was awaiting for an effect to be created on this
3030 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08003031 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07003032 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07003033 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07003034 thread->addEffectChain_l(chain);
3035 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003036 }
Eric Laurent84e9a102010-09-23 16:10:16 -07003037
Eric Laurent021cf962014-05-13 10:18:14 -07003038 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003039
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003040 // create effect on selected output thread
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003041 bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003042 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003043 &desc, enabled, &lStatus, pinned);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003044 if (handle != 0 && id != NULL) {
3045 *id = handle->id();
3046 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07003047 if (handle == 0) {
3048 // remove local strong reference to Client with mClientLock held
3049 Mutex::Autolock _cl(mClientLock);
3050 client.clear();
3051 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07003052 }
3053
3054Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07003055 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003056 return handle;
3057}
3058
Glenn Kastend848eb42016-03-08 13:42:11 -08003059status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08003060 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07003061{
Steve Block3856b092011-10-20 11:56:00 +01003062 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07003063 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003064 Mutex::Autolock _l(mLock);
3065 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003066 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003067 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003068 }
Eric Laurentde070132010-07-13 04:45:46 -07003069 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
3070 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003071 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003072 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003073 }
Eric Laurentde070132010-07-13 04:45:46 -07003074 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
3075 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003076 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07003077 return BAD_VALUE;
3078 }
3079
3080 Mutex::Autolock _dl(dstThread->mLock);
3081 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003082 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07003083}
3084
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003085// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08003086status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07003087 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07003088 AudioFlinger::PlaybackThread *dstThread,
3089 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07003090{
Steve Block3856b092011-10-20 11:56:00 +01003091 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003092 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07003093
Eric Laurent59255e42011-07-27 19:49:51 -07003094 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07003095 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003096 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07003097 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07003098 return INVALID_OPERATION;
3099 }
3100
Eric Laurent4c415062016-06-17 16:14:16 -07003101 // Check whether the destination thread and all effects in the chain are compatible
3102 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07003103 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07003104 " destination thread %p is not compatible with effects in the chain",
3105 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07003106 return INVALID_OPERATION;
3107 }
3108
Eric Laurent39e94f82010-07-28 01:32:47 -07003109 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07003110 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07003111 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07003112 // removed.
3113 srcThread->removeEffectChain_l(chain);
3114
3115 // transfer all effects one by one so that new effect chain is created on new thread with
3116 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07003117 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07003118 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07003119 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003120 Vector< sp<EffectModule> > removed;
3121 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07003122 while (effect != 0) {
3123 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003124 removed.add(effect);
3125 status = dstThread->addEffect_l(effect);
3126 if (status != NO_ERROR) {
3127 break;
3128 }
Eric Laurentec35a142011-10-05 17:42:25 -07003129 // removeEffect_l() has stopped the effect if it was active so it must be restarted
3130 if (effect->state() == EffectModule::ACTIVE ||
3131 effect->state() == EffectModule::STOPPING) {
3132 effect->start();
3133 }
Eric Laurent39e94f82010-07-28 01:32:47 -07003134 // if the move request is not received from audio policy manager, the effect must be
3135 // re-registered with the new strategy and output
3136 if (dstChain == 0) {
3137 dstChain = effect->chain().promote();
3138 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003139 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003140 status = NO_INIT;
3141 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07003142 }
3143 strategy = dstChain->strategy();
3144 }
3145 if (reRegister) {
3146 AudioSystem::unregisterEffect(effect->id());
3147 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07003148 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07003149 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07003150 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07003151 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003152 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07003153 }
Eric Laurentde070132010-07-13 04:45:46 -07003154 effect = chain->getEffectFromId_l(0);
3155 }
3156
Eric Laurent5baf2af2013-09-12 17:37:00 -07003157 if (status != NO_ERROR) {
3158 for (size_t i = 0; i < removed.size(); i++) {
3159 srcThread->addEffect_l(removed[i]);
3160 if (dstChain != 0 && reRegister) {
3161 AudioSystem::unregisterEffect(removed[i]->id());
3162 AudioSystem::registerEffect(&removed[i]->desc(),
3163 srcThread->id(),
3164 strategy,
3165 sessionId,
3166 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07003167 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07003168 }
3169 }
3170 }
3171
3172 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07003173}
3174
Eric Laurent5baf2af2013-09-12 17:37:00 -07003175bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07003176{
3177 if (mGlobalEffectEnableTime != 0 &&
3178 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
3179 return true;
3180 }
3181
3182 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3183 sp<EffectChain> ec =
3184 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07003185 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07003186 return true;
3187 }
3188 }
3189 return false;
3190}
3191
Eric Laurent5baf2af2013-09-12 17:37:00 -07003192void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07003193{
3194 Mutex::Autolock _l(mLock);
3195
3196 mGlobalEffectEnableTime = systemTime();
3197
3198 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
3199 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
3200 if (t->mType == ThreadBase::OFFLOAD) {
3201 t->invalidateTracks(AUDIO_STREAM_MUSIC);
3202 }
3203 }
3204
3205}
3206
Eric Laurentaaa44472014-09-12 17:41:50 -07003207status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
3208{
Eric Laurentd8365c52017-07-16 15:27:05 -07003209 // clear possible suspended state before parking the chain so that it starts in default state
3210 // when attached to a new record thread
3211 chain->setEffectSuspended_l(FX_IID_AEC, false);
3212 chain->setEffectSuspended_l(FX_IID_NS, false);
3213
Glenn Kastend848eb42016-03-08 13:42:11 -08003214 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003215 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003216 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003217 if (index >= 0) {
3218 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
3219 return ALREADY_EXISTS;
3220 }
3221 mOrphanEffectChains.add(session, chain);
3222 return NO_ERROR;
3223}
3224
3225sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
3226{
3227 sp<EffectChain> chain;
3228 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003229 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003230 if (index >= 0) {
3231 chain = mOrphanEffectChains.valueAt(index);
3232 mOrphanEffectChains.removeItemsAt(index);
3233 }
3234 return chain;
3235}
3236
3237bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
3238{
3239 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08003240 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07003241 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003242 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003243 if (index >= 0) {
3244 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08003245 if (chain->removeEffect_l(effect, true) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07003246 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003247 mOrphanEffectChains.removeItemsAt(index);
3248 }
3249 return true;
3250 }
3251 return false;
3252}
3253
3254
Glenn Kastenda6ef132013-01-10 12:31:01 -08003255struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003256#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
3257 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003258};
3259
3260int comparEntry(const void *p1, const void *p2)
3261{
Glenn Kasten2f55e762015-03-05 16:05:08 -08003262 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003263}
3264
Glenn Kasten46909e72013-02-26 09:20:22 -08003265#ifdef TEE_SINK
Glenn Kasten5b2191a2016-08-19 11:44:47 -07003266void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id, char suffix)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003267{
Eric Laurent81784c32012-11-19 14:55:58 -08003268 NBAIO_Source *teeSource = source.get();
3269 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003270 // .wav rotation
3271 // There is a benign race condition if 2 threads call this simultaneously.
3272 // They would both traverse the directory, but the result would simply be
3273 // failures at unlink() which are ignored. It's also unlikely since
3274 // normally dumpsys is only done by bugreport or from the command line.
3275 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08003276 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08003277 size_t teePathLen = strlen(teePath);
3278 DIR *dir = opendir(teePath);
3279 teePath[teePathLen++] = '/';
3280 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003281#define TEE_MAX_SORT 20 // number of entries to sort
3282#define TEE_MAX_KEEP 10 // number of entries to keep
3283 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003284 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08003285 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003286 struct dirent de;
3287 struct dirent *result = NULL;
3288 int rc = readdir_r(dir, &de, &result);
3289 if (rc != 0) {
3290 ALOGW("readdir_r failed %d", rc);
3291 break;
3292 }
3293 if (result == NULL) {
3294 break;
3295 }
3296 if (result != &de) {
3297 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
3298 break;
3299 }
3300 // ignore non .wav file entries
3301 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003302 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08003303 strcmp(&de.d_name[nameLen - 4], ".wav")) {
3304 continue;
3305 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08003306 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003307 }
3308 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003309 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003310 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003311 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
3312 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003313 (void) unlink(teePath);
3314 }
3315 }
3316 } else {
3317 if (fd >= 0) {
Glenn Kastenfbd87e82016-07-18 15:45:56 -07003318 dprintf(fd, "unable to rotate tees in %.*s: %s\n", (int) teePathLen, teePath,
Glenn Kasten9a003992016-02-23 15:24:34 -08003319 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003320 }
3321 }
Eric Laurent81784c32012-11-19 14:55:58 -08003322 char teeTime[16];
3323 struct timeval tv;
3324 gettimeofday(&tv, NULL);
3325 struct tm tm;
3326 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003327 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
Glenn Kasten5b2191a2016-08-19 11:44:47 -07003328 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d_%c.wav", teeTime, id,
3329 suffix);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003330 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3331 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003332 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003333 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003334 char wavHeader[44];
3335 memcpy(wavHeader,
3336 "RIFF\0\0\0\0WAVEfmt \20\0\0\0\1\0\2\0\104\254\0\0\0\0\0\0\4\0\20\0data\0\0\0\0",
3337 sizeof(wavHeader));
3338 NBAIO_Format format = teeSource->format();
3339 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003340 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003341 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003342 wavHeader[22] = channelCount; // number of channels
3343 wavHeader[24] = sampleRate; // sample rate
3344 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003345 wavHeader[32] = frameSize; // block alignment
3346 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003347 write(teeFd, wavHeader, sizeof(wavHeader));
3348 size_t total = 0;
3349 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003350#define TEE_SINK_READ 1024 // frames per I/O operation
3351 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003352 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003353 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003354 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003355 bool wasFirstRead = firstRead;
3356 firstRead = false;
3357 if (actual <= 0) {
3358 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3359 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003360 }
Eric Laurent81784c32012-11-19 14:55:58 -08003361 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003362 }
Eric Laurent81784c32012-11-19 14:55:58 -08003363 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003364 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003365 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003366 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003367 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003368 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003369 uint32_t temp = 44 + total * frameSize - 8;
3370 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003371 write(teeFd, &temp, sizeof(temp));
3372 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003373 temp = total * frameSize;
3374 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003375 write(teeFd, &temp, sizeof(temp));
3376 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003377 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003378 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003379 }
Eric Laurent59255e42011-07-27 19:49:51 -07003380 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003381 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003382 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003383 }
Eric Laurent59255e42011-07-27 19:49:51 -07003384 }
3385 }
3386}
Glenn Kasten46909e72013-02-26 09:20:22 -08003387#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003388
Mathias Agopian65ab4712010-07-14 17:59:35 -07003389// ----------------------------------------------------------------------------
3390
3391status_t AudioFlinger::onTransact(
3392 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3393{
3394 return BnAudioFlinger::onTransact(code, data, reply, flags);
3395}
3396
Glenn Kasten63238ef2015-03-02 15:50:29 -08003397} // namespace android