blob: 8917c2652ca4fcd557edc6943799cadfe3a02220 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080023#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <math.h>
25#include <signal.h>
26#include <sys/time.h>
27#include <sys/resource.h>
28
Gloria Wang9ee159b2011-02-24 14:51:45 -080029#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <binder/IServiceManager.h>
31#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070032#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <binder/Parcel.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070034#include <media/audiohal/DeviceHalInterface.h>
35#include <media/audiohal/DevicesFactoryHalInterface.h>
36#include <media/audiohal/EffectsFactoryHalInterface.h>
Mikhail Naganov00260b52016-10-13 12:54:24 -070037#include <media/AudioParameter.h>
Mikhail Naganov913d06c2016-11-01 12:49:22 -070038#include <media/TypeConverter.h>
Andy Hung35fec5f2016-04-13 14:21:48 -070039#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070040#include <utils/String16.h>
41#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070042#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
Dima Zavinfce7a472011-04-19 22:30:36 -070044#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045#include <cutils/properties.h>
46
Dima Zavin64760242011-05-11 14:15:23 -070047#include <system/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
49#include "AudioMixer.h"
50#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080051#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Andy Hung6770c6f2015-04-07 13:43:36 -070053#include <media/AudioResamplerPublic.h>
54
Mikhail Naganov9fe94012016-10-14 14:57:40 -070055#include <system/audio_effects/effect_visualizer.h>
56#include <system/audio_effects/effect_ns.h>
57#include <system/audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070058
Glenn Kasten3b21c502011-12-15 09:52:39 -080059#include <audio_utils/primitives.h>
60
Eric Laurentfeb0db62011-07-22 09:04:31 -070061#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080062
Glenn Kasten9e58b552013-01-18 15:09:48 -080063#include <media/IMediaLogService.h>
Andy Hung07b745e2016-05-23 16:21:07 -070064#include <media/MemoryLeakTrackUtil.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080065#include <media/nbaio/Pipe.h>
66#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070067#include <media/AudioParameter.h>
Wei Jia3f273d12015-11-24 09:06:49 -080068#include <mediautils/BatteryNotifier.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070069#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080070
rago3bd1c872016-09-26 12:58:14 -070071//#define BUFLOG_NDEBUG 0
72#include <BufLog.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
Glenn Kasten46909e72013-02-26 09:20:22 -0800101#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800102bool AudioFlinger::mTeeSinkInputEnabled = false;
103bool AudioFlinger::mTeeSinkOutputEnabled = false;
104bool AudioFlinger::mTeeSinkTrackEnabled = false;
105
106size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
107size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
108size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800109#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800110
Eric Laurent813e2a72013-08-31 12:59:48 -0700111// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
112// we define a minimum time during which a global effect is considered enabled.
113static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
114
Mathias Agopian65ab4712010-07-14 17:59:35 -0700115// ----------------------------------------------------------------------------
116
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700117std::string formatToString(audio_format_t format) {
118 std::string result;
119 FormatConverter::toString(format, result);
120 return result;
Marco Nelissenb2208842014-02-07 14:00:50 -0800121}
122
Mathias Agopian65ab4712010-07-14 17:59:35 -0700123// ----------------------------------------------------------------------------
124
125AudioFlinger::AudioFlinger()
126 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800127 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800128 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700129 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800130 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800131 mMasterMute(false),
Glenn Kastend2e67e12016-04-11 08:26:37 -0700132 // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),
John Grossman4ff14ba2012-02-08 16:37:41 -0800133 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700134 mBtNrecIsOff(false),
135 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700136 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700137 mGlobalEffectEnableTime(0),
Eric Laurent72e3f392015-05-20 14:43:50 -0700138 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700139{
Glenn Kastend2e67e12016-04-11 08:26:37 -0700140 // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum
141 for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) {
142 // zero ID has a special meaning, so unavailable
143 mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX;
144 }
145
Glenn Kasten949a9262013-04-16 12:35:20 -0700146 getpid_cached = getpid();
Glenn Kastenae0cff12016-02-24 13:57:49 -0800147 const bool doLog = property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800148 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800149 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
150 MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800151 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700152
Wei Jia3f273d12015-11-24 09:06:49 -0800153 // reset battery stats.
154 // if the audio service has crashed, battery stats could be left
155 // in bad state, reset the state upon service start.
156 BatteryNotifier::getInstance().noteResetAudio();
157
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700158 mDevicesFactoryHal = DevicesFactoryHalInterface::create();
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700159 mEffectsFactoryHal = EffectsFactoryHalInterface::create();
160
Glenn Kasten46909e72013-02-26 09:20:22 -0800161#ifdef TEE_SINK
Glenn Kasten9a003992016-02-23 15:24:34 -0800162 char value[PROPERTY_VALUE_MAX];
Glenn Kastenda6ef132013-01-10 12:31:01 -0800163 (void) property_get("ro.debuggable", value, "0");
164 int debuggable = atoi(value);
165 int teeEnabled = 0;
166 if (debuggable) {
167 (void) property_get("af.tee", value, "0");
168 teeEnabled = atoi(value);
169 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800170 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700171 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800172 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700173 }
174 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800175 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700176 }
177 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800178 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700179 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800180#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700181}
182
183void AudioFlinger::onFirstRef()
184{
Eric Laurent93575202011-01-18 18:39:02 -0800185 Mutex::Autolock _l(mLock);
186
Dima Zavin799a70e2011-04-18 16:57:27 -0700187 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800188 char val_str[PROPERTY_VALUE_MAX] = { 0 };
189 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
190 uint32_t int_val;
191 if (1 == sscanf(val_str, "%u", &int_val)) {
192 mStandbyTimeInNsecs = milliseconds(int_val);
193 ALOGI("Using %u mSec as standby time.", int_val);
194 } else {
195 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
196 ALOGI("Using default %u mSec as standby time.",
197 (uint32_t)(mStandbyTimeInNsecs / 1000000));
198 }
199 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700200
Eric Laurent1c333e22014-05-20 10:48:17 -0700201 mPatchPanel = new PatchPanel(this);
Andy Hungdeb03352016-08-29 14:40:14 -0700202
Eric Laurenta4c5a552012-03-29 10:12:40 -0700203 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700204}
205
206AudioFlinger::~AudioFlinger()
207{
208 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700209 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700210 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211 }
212 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700213 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700214 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700215 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700216
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800217 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
218 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700219 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700221
222 // Tell media.log service about any old writers that still need to be unregistered
Andy Hungce717682015-12-22 13:40:32 -0800223 if (mLogMemoryDealer != 0) {
224 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
225 if (binder != 0) {
226 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
227 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
228 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
229 mUnregisteredWriters.pop();
230 mediaLogService->unregisterWriter(iMemory);
231 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700232 }
233 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234}
235
Eric Laurenta4c5a552012-03-29 10:12:40 -0700236static const char * const audio_interfaces[] = {
237 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
238 AUDIO_HARDWARE_MODULE_ID_A2DP,
239 AUDIO_HARDWARE_MODULE_ID_USB,
240};
241#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
242
Phil Burk062e67a2015-02-11 13:40:50 -0800243AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700244 audio_module_handle_t module,
245 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700246{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700247 // if module is 0, the request comes from an old policy manager and we should load
248 // well known modules
249 if (module == 0) {
250 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
251 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
252 loadHwModule_l(audio_interfaces[i]);
253 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700254 // then try to find a module supporting the requested device.
255 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
256 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700257 sp<DeviceHalInterface> dev = audioHwDevice->hwDevice();
258 uint32_t supportedDevices;
259 if (dev->getSupportedDevices(&supportedDevices) == OK &&
260 (supportedDevices & devices) == devices) {
Eric Laurentf1c04f92012-08-28 14:26:53 -0700261 return audioHwDevice;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700262 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700263 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700264 } else {
265 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700266 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
267 if (audioHwDevice != NULL) {
268 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700269 }
270 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700271
Dima Zavin799a70e2011-04-18 16:57:27 -0700272 return NULL;
273}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274
Glenn Kasten0f11b512014-01-31 16:18:54 -0800275void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276{
277 const size_t SIZE = 256;
278 char buffer[SIZE];
279 String8 result;
280
281 result.append("Clients:\n");
282 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800283 sp<Client> client = mClients.valueAt(i).promote();
284 if (client != 0) {
285 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
286 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287 }
288 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700289
Eric Laurentd1b28d42013-09-18 18:47:13 -0700290 result.append("Notification Clients:\n");
291 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
292 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
293 result.append(buffer);
294 }
295
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700296 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800297 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700298 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
299 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800300 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700301 result.append(buffer);
302 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304}
305
306
Glenn Kasten0f11b512014-01-31 16:18:54 -0800307void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700308{
309 const size_t SIZE = 256;
310 char buffer[SIZE];
311 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800312 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313
John Grossman4ff14ba2012-02-08 16:37:41 -0800314 snprintf(buffer, SIZE, "Hardware status: %d\n"
315 "Standby Time mSec: %u\n",
316 hardwareStatus,
317 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700318 result.append(buffer);
319 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320}
321
Glenn Kasten0f11b512014-01-31 16:18:54 -0800322void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323{
324 const size_t SIZE = 256;
325 char buffer[SIZE];
326 String8 result;
327 snprintf(buffer, SIZE, "Permission Denial: "
328 "can't dump AudioFlinger from pid=%d, uid=%d\n",
329 IPCThreadState::self()->getCallingPid(),
330 IPCThreadState::self()->getCallingUid());
331 result.append(buffer);
332 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333}
334
Eric Laurent81784c32012-11-19 14:55:58 -0800335bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336{
337 bool locked = false;
338 for (int i = 0; i < kDumpLockRetries; ++i) {
339 if (mutex.tryLock() == NO_ERROR) {
340 locked = true;
341 break;
342 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800343 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700344 }
345 return locked;
346}
347
348status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
349{
Glenn Kasten44deb052012-02-05 18:09:08 -0800350 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 dumpPermissionDenial(fd, args);
352 } else {
353 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800354 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355 if (!hardwareLocked) {
356 String8 result(kHardwareLockedString);
357 write(fd, result.string(), result.size());
358 } else {
359 mHardwareLock.unlock();
360 }
361
Eric Laurent81784c32012-11-19 14:55:58 -0800362 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363
364 // failed to lock - AudioFlinger is probably deadlocked
365 if (!locked) {
366 String8 result(kDeadlockedString);
367 write(fd, result.string(), result.size());
368 }
369
Eric Laurent021cf962014-05-13 10:18:14 -0700370 bool clientLocked = dumpTryLock(mClientLock);
371 if (!clientLocked) {
372 String8 result(kClientLockedString);
373 write(fd, result.string(), result.size());
374 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700375
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700376 if (mEffectsFactoryHal != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700377 mEffectsFactoryHal->dumpEffects(fd);
378 } else {
379 String8 result(kNoEffectsFactory);
380 write(fd, result.string(), result.size());
381 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700382
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700384 if (clientLocked) {
385 mClientLock.unlock();
386 }
387
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388 dumpInternals(fd, args);
389
390 // dump playback threads
391 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
392 mPlaybackThreads.valueAt(i)->dump(fd, args);
393 }
394
395 // dump record threads
396 for (size_t i = 0; i < mRecordThreads.size(); i++) {
397 mRecordThreads.valueAt(i)->dump(fd, args);
398 }
399
Eric Laurentaaa44472014-09-12 17:41:50 -0700400 // dump orphan effect chains
401 if (mOrphanEffectChains.size() != 0) {
402 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
403 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
404 mOrphanEffectChains.valueAt(i)->dump(fd, args);
405 }
406 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700407 // dump all hardware devs
408 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700409 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
410 dev->dump(fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700412
Glenn Kasten46909e72013-02-26 09:20:22 -0800413#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700414 // dump the serially shared record tee sink
415 if (mRecordTeeSource != 0) {
416 dumpTee(fd, mRecordTeeSource);
417 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800418#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700419
rago3bd1c872016-09-26 12:58:14 -0700420 BUFLOG_RESET;
421
Glenn Kastend65d73c2012-06-22 17:21:07 -0700422 if (locked) {
423 mLock.unlock();
424 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800425
426 // append a copy of media.log here by forwarding fd to it, but don't attempt
427 // to lookup the service if it's not running, as it will block for a second
428 if (mLogMemoryDealer != 0) {
429 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
430 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700431 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800432 Vector<String16> args;
433 binder->dump(fd, args);
434 }
435 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700436
437 // check for optional arguments
Andy Hung07b745e2016-05-23 16:21:07 -0700438 bool dumpMem = false;
Andy Hung35fec5f2016-04-13 14:21:48 -0700439 bool unreachableMemory = false;
440 for (const auto &arg : args) {
Andy Hung07b745e2016-05-23 16:21:07 -0700441 if (arg == String16("-m")) {
442 dumpMem = true;
443 } else if (arg == String16("--unreachable")) {
Andy Hung35fec5f2016-04-13 14:21:48 -0700444 unreachableMemory = true;
445 }
446 }
447
Andy Hung07b745e2016-05-23 16:21:07 -0700448 if (dumpMem) {
449 dprintf(fd, "\nDumping memory:\n");
450 std::string s = dumpMemoryAddresses(100 /* limit */);
451 write(fd, s.c_str(), s.size());
452 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700453 if (unreachableMemory) {
454 dprintf(fd, "\nDumping unreachable memory:\n");
455 // TODO - should limit be an argument parameter?
Andy Hung07b745e2016-05-23 16:21:07 -0700456 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
Andy Hung35fec5f2016-04-13 14:21:48 -0700457 write(fd, s.c_str(), s.size());
458 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700459 }
460 return NO_ERROR;
461}
462
Eric Laurent021cf962014-05-13 10:18:14 -0700463sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800464{
Eric Laurent021cf962014-05-13 10:18:14 -0700465 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800466 // If pid is already in the mClients wp<> map, then use that entry
467 // (for which promote() is always != 0), otherwise create a new entry and Client.
468 sp<Client> client = mClients.valueFor(pid).promote();
469 if (client == 0) {
470 client = new Client(this, pid);
471 mClients.add(pid, client);
472 }
473
474 return client;
475}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476
Glenn Kasten9e58b552013-01-18 15:09:48 -0800477sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
478{
Glenn Kasten481fb672013-09-30 14:39:28 -0700479 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800480 if (mLogMemoryDealer == 0) {
481 return new NBLog::Writer();
482 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800483 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700484 // Similarly if we can't contact the media.log service, also return a dummy writer
485 if (binder == 0) {
486 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800487 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700488 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
489 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
490 // If allocation fails, consult the vector of previously unregistered writers
491 // and garbage-collect one or more them until an allocation succeeds
492 if (shared == 0) {
493 Mutex::Autolock _l(mUnregisteredWritersLock);
494 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
495 {
496 // Pick the oldest stale writer to garbage-collect
497 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
498 mUnregisteredWriters.removeAt(0);
499 mediaLogService->unregisterWriter(iMemory);
500 // Now the media.log remote reference to IMemory is gone. When our last local
501 // reference to IMemory also drops to zero at end of this block,
502 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
503 }
504 // Re-attempt the allocation
505 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
506 if (shared != 0) {
507 goto success;
508 }
509 }
510 // Even after garbage-collecting all old writers, there is still not enough memory,
511 // so return a dummy writer
512 return new NBLog::Writer();
513 }
514success:
515 mediaLogService->registerWriter(shared, size, name);
516 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800517}
518
519void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
520{
Glenn Kasten685ef092013-02-04 08:15:34 -0800521 if (writer == 0) {
522 return;
523 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800524 sp<IMemory> iMemory(writer->getIMemory());
525 if (iMemory == 0) {
526 return;
527 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700528 // Rather than removing the writer immediately, append it to a queue of old writers to
529 // be garbage-collected later. This allows us to continue to view old logs for a while.
530 Mutex::Autolock _l(mUnregisteredWritersLock);
531 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800532}
533
Mathias Agopian65ab4712010-07-14 17:59:35 -0700534// IAudioFlinger interface
535
536
537sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800538 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700539 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800540 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700541 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800542 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -0700543 audio_output_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700544 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800545 audio_io_handle_t output,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700546 pid_t pid,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800547 pid_t tid,
Glenn Kastend848eb42016-03-08 13:42:11 -0800548 audio_session_t *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800549 int clientUid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800550 status_t *status,
551 audio_port_handle_t portId)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552{
553 sp<PlaybackThread::Track> track;
554 sp<TrackHandle> trackHandle;
555 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -0800557 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700559 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
560 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
561 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
562 ALOGW_IF(pid != -1 && pid != callingPid,
563 "%s uid %d pid %d tried to pass itself off as pid %d",
564 __func__, callingUid, callingPid, pid);
565 pid = callingPid;
566 }
567
Glenn Kasten263709e2012-01-06 08:40:01 -0800568 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
569 // but if someone uses binder directly they could bypass that and cause us to crash
570 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000571 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572 lStatus = BAD_VALUE;
573 goto Exit;
574 }
575
Glenn Kasten53b5d092014-02-05 10:00:23 -0800576 // further sample rate checks are performed by createTrack_l() depending on the thread type
577 if (sampleRate == 0) {
578 ALOGE("createTrack() invalid sample rate %u", sampleRate);
579 lStatus = BAD_VALUE;
580 goto Exit;
581 }
582
583 // further channel mask checks are performed by createTrack_l() depending on the thread type
584 if (!audio_is_output_channel(channelMask)) {
585 ALOGE("createTrack() invalid channel mask %#x", channelMask);
586 lStatus = BAD_VALUE;
587 goto Exit;
588 }
589
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700590 // further format checks are performed by createTrack_l() depending on the thread type
591 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800592 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700593 lStatus = BAD_VALUE;
594 goto Exit;
595 }
596
Glenn Kasten663c2242013-09-24 11:52:37 -0700597 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
598 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
599 lStatus = BAD_VALUE;
600 goto Exit;
601 }
602
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603 {
604 Mutex::Autolock _l(mLock);
605 PlaybackThread *thread = checkPlaybackThread_l(output);
606 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800607 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700608 lStatus = BAD_VALUE;
609 goto Exit;
610 }
611
Eric Laurent021cf962014-05-13 10:18:14 -0700612 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700613
Glenn Kastene848bd92014-03-13 15:00:32 -0700614 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700615 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800616 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
617 ALOGE("createTrack() invalid session ID %d", *sessionId);
618 lStatus = BAD_VALUE;
619 goto Exit;
620 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700621 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700622 // check if an effect chain with the same session ID is present on another
623 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700624 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700625 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
626 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700627 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent4c415062016-06-17 16:14:16 -0700628 if (sessions & ThreadBase::EFFECT_SESSION) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700629 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700630 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700631 }
Eric Laurentde070132010-07-13 04:45:46 -0700632 }
633 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700635 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800636 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 if (sessionId != NULL) {
638 *sessionId = lSessionId;
639 }
640 }
Steve Block3856b092011-10-20 11:56:00 +0100641 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642
643 track = thread->createTrack_l(client, streamType, sampleRate, format,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800644 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid,
645 clientUid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800646 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700647 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700648
649 // move effect chain to this output thread if an effect on same session was waiting
650 // for a track to be created
651 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700652 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700653 Mutex::Autolock _dl(thread->mLock);
654 Mutex::Autolock _sl(effectThread->mLock);
655 moveEffectChain_l(lSessionId, effectThread, thread, true);
656 }
Eric Laurenta011e352012-03-29 15:51:43 -0700657
658 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700659 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700660 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
661 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700662 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700663 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700664 } else {
665 mPendingSyncEvents[i]->cancel();
666 }
Eric Laurenta011e352012-03-29 15:51:43 -0700667 mPendingSyncEvents.removeAt(i);
668 i--;
669 }
670 }
671 }
Glenn Kastene198c362013-08-13 09:13:36 -0700672
Glenn Kastend848eb42016-03-08 13:42:11 -0800673 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 }
Glenn Kastene198c362013-08-13 09:13:36 -0700675
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700676 if (lStatus != NO_ERROR) {
677 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700678 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700679 // Don't hold mClientLock when releasing the reference on the track as the
680 // destructor will acquire it.
681 {
682 Mutex::Autolock _cl(mClientLock);
683 client.clear();
684 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700686 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687 }
688
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700689 // return handle to client
690 trackHandle = new TrackHandle(track);
691
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700693 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 return trackHandle;
695}
696
Glenn Kasten2c073da2016-02-26 09:14:08 -0800697uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698{
699 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800700 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800702 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703 return 0;
704 }
705 return thread->sampleRate();
706}
707
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800708audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709{
710 Mutex::Autolock _l(mLock);
711 PlaybackThread *thread = checkPlaybackThread_l(output);
712 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000713 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800714 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715 }
716 return thread->format();
717}
718
Glenn Kasten2c073da2016-02-26 09:14:08 -0800719size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720{
721 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800722 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800724 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725 return 0;
726 }
Glenn Kasten58912562012-04-03 10:45:00 -0700727 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
728 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700729 return thread->frameCount();
730}
731
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700732size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
733{
734 Mutex::Autolock _l(mLock);
735 ThreadBase *thread = checkThread_l(ioHandle);
736 if (thread == NULL) {
737 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
738 return 0;
739 }
740 return thread->frameCountHAL();
741}
742
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800743uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700744{
745 Mutex::Autolock _l(mLock);
746 PlaybackThread *thread = checkPlaybackThread_l(output);
747 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800748 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749 return 0;
750 }
751 return thread->latency();
752}
753
754status_t AudioFlinger::setMasterVolume(float value)
755{
Eric Laurenta1884f92011-08-23 08:25:03 -0700756 status_t ret = initCheck();
757 if (ret != NO_ERROR) {
758 return ret;
759 }
760
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761 // check calling permissions
762 if (!settingsAllowed()) {
763 return PERMISSION_DENIED;
764 }
765
Eric Laurenta4c5a552012-03-29 10:12:40 -0700766 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700767 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700768
John Grossmanee578c02012-07-23 17:05:46 -0700769 // Set master volume in the HALs which support it.
770 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
771 AutoMutex lock(mHardwareLock);
772 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800773
John Grossmanee578c02012-07-23 17:05:46 -0700774 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
775 if (dev->canSetMasterVolume()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700776 dev->hwDevice()->setMasterVolume(value);
Eric Laurent93575202011-01-18 18:39:02 -0800777 }
John Grossmanee578c02012-07-23 17:05:46 -0700778 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700780
John Grossmanee578c02012-07-23 17:05:46 -0700781 // Now set the master volume in each playback thread. Playback threads
782 // assigned to HALs which do not have master volume support will apply
783 // master volume during the mix operation. Threads with HALs which do
784 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700785 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
786 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
787 continue;
788 }
John Grossmanee578c02012-07-23 17:05:46 -0700789 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700790 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791
792 return NO_ERROR;
793}
794
Glenn Kastenf78aee72012-01-04 11:00:47 -0800795status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796{
Eric Laurenta1884f92011-08-23 08:25:03 -0700797 status_t ret = initCheck();
798 if (ret != NO_ERROR) {
799 return ret;
800 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801
802 // check calling permissions
803 if (!settingsAllowed()) {
804 return PERMISSION_DENIED;
805 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800806 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000807 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808 return BAD_VALUE;
809 }
810
811 { // scope for the lock
812 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700813 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700815 ret = dev->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 mHardwareStatus = AUDIO_HW_IDLE;
817 }
818
819 if (NO_ERROR == ret) {
820 Mutex::Autolock _l(mLock);
821 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800822 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700823 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824 }
825
826 return ret;
827}
828
829status_t AudioFlinger::setMicMute(bool state)
830{
Eric Laurenta1884f92011-08-23 08:25:03 -0700831 status_t ret = initCheck();
832 if (ret != NO_ERROR) {
833 return ret;
834 }
835
Mathias Agopian65ab4712010-07-14 17:59:35 -0700836 // check calling permissions
837 if (!settingsAllowed()) {
838 return PERMISSION_DENIED;
839 }
840
841 AutoMutex lock(mHardwareLock);
842 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700843 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700844 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
845 status_t result = dev->setMicMute(state);
Eric Laurent2f035f52014-09-14 12:11:52 -0700846 if (result != NO_ERROR) {
847 ret = result;
848 }
849 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850 mHardwareStatus = AUDIO_HW_IDLE;
851 return ret;
852}
853
854bool AudioFlinger::getMicMute() const
855{
Eric Laurenta1884f92011-08-23 08:25:03 -0700856 status_t ret = initCheck();
857 if (ret != NO_ERROR) {
858 return false;
859 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800860 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700861 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800862 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800864 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700865 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
866 status_t result = dev->getMicMute(&state);
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800867 if (result == NO_ERROR) {
868 mute = mute && state;
869 }
870 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800872
873 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700874}
875
876status_t AudioFlinger::setMasterMute(bool muted)
877{
John Grossmand8f178d2012-07-20 14:51:35 -0700878 status_t ret = initCheck();
879 if (ret != NO_ERROR) {
880 return ret;
881 }
882
Mathias Agopian65ab4712010-07-14 17:59:35 -0700883 // check calling permissions
884 if (!settingsAllowed()) {
885 return PERMISSION_DENIED;
886 }
887
John Grossmanee578c02012-07-23 17:05:46 -0700888 Mutex::Autolock _l(mLock);
889 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700890
John Grossmanee578c02012-07-23 17:05:46 -0700891 // Set master mute in the HALs which support it.
892 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
893 AutoMutex lock(mHardwareLock);
894 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700895
John Grossmanee578c02012-07-23 17:05:46 -0700896 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
897 if (dev->canSetMasterMute()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700898 dev->hwDevice()->setMasterMute(muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700899 }
John Grossmanee578c02012-07-23 17:05:46 -0700900 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700901 }
902
John Grossmanee578c02012-07-23 17:05:46 -0700903 // Now set the master mute in each playback thread. Playback threads
904 // assigned to HALs which do not have master mute support will apply master
905 // mute during the mix operation. Threads with HALs which do support master
906 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700907 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
908 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
909 continue;
910 }
John Grossmanee578c02012-07-23 17:05:46 -0700911 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700912 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913
914 return NO_ERROR;
915}
916
917float AudioFlinger::masterVolume() const
918{
Glenn Kasten98067102011-12-13 11:47:54 -0800919 Mutex::Autolock _l(mLock);
920 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921}
922
923bool AudioFlinger::masterMute() const
924{
Glenn Kasten98067102011-12-13 11:47:54 -0800925 Mutex::Autolock _l(mLock);
926 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927}
928
John Grossman4ff14ba2012-02-08 16:37:41 -0800929float AudioFlinger::masterVolume_l() const
930{
John Grossman4ff14ba2012-02-08 16:37:41 -0800931 return mMasterVolume;
932}
933
John Grossmand8f178d2012-07-20 14:51:35 -0700934bool AudioFlinger::masterMute_l() const
935{
John Grossmanee578c02012-07-23 17:05:46 -0700936 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700937}
938
Eric Laurent223fd5c2014-11-11 13:43:36 -0800939status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
940{
941 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
942 ALOGW("setStreamVolume() invalid stream %d", stream);
943 return BAD_VALUE;
944 }
945 pid_t caller = IPCThreadState::self()->getCallingPid();
946 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
947 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
948 return PERMISSION_DENIED;
949 }
950
951 return NO_ERROR;
952}
953
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800954status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
955 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956{
957 // check calling permissions
958 if (!settingsAllowed()) {
959 return PERMISSION_DENIED;
960 }
961
Eric Laurent223fd5c2014-11-11 13:43:36 -0800962 status_t status = checkStreamType(stream);
963 if (status != NO_ERROR) {
964 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800966 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967
968 AutoMutex lock(mLock);
969 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700970 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 thread = checkPlaybackThread_l(output);
972 if (thread == NULL) {
973 return BAD_VALUE;
974 }
975 }
976
977 mStreamTypes[stream].volume = value;
978
979 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800980 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700981 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700982 }
983 } else {
984 thread->setStreamVolume(stream, value);
985 }
986
987 return NO_ERROR;
988}
989
Glenn Kastenfff6d712012-01-12 16:38:12 -0800990status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991{
992 // check calling permissions
993 if (!settingsAllowed()) {
994 return PERMISSION_DENIED;
995 }
996
Eric Laurent223fd5c2014-11-11 13:43:36 -0800997 status_t status = checkStreamType(stream);
998 if (status != NO_ERROR) {
999 return status;
1000 }
1001 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1002
1003 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001004 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001005 return BAD_VALUE;
1006 }
1007
Eric Laurent93575202011-01-18 18:39:02 -08001008 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001009 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -07001010 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001011 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012
1013 return NO_ERROR;
1014}
1015
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001016float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001017{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001018 status_t status = checkStreamType(stream);
1019 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001020 return 0.0f;
1021 }
1022
1023 AutoMutex lock(mLock);
1024 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001025 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 PlaybackThread *thread = checkPlaybackThread_l(output);
1027 if (thread == NULL) {
1028 return 0.0f;
1029 }
1030 volume = thread->streamVolume(stream);
1031 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001032 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001033 }
1034
1035 return volume;
1036}
1037
Glenn Kastenfff6d712012-01-12 16:38:12 -08001038bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001040 status_t status = checkStreamType(stream);
1041 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042 return true;
1043 }
1044
Glenn Kasten6637baa2012-01-09 09:40:36 -08001045 AutoMutex lock(mLock);
1046 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047}
1048
Eric Laurent054d9d32015-04-24 08:48:48 -07001049
1050void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1051{
1052 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1053 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1054 }
1055}
1056
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001057status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001059 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1060 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001061
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 // check calling permissions
1063 if (!settingsAllowed()) {
1064 return PERMISSION_DENIED;
1065 }
1066
Glenn Kasten142f5192014-03-25 17:44:59 -07001067 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1068 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001069 Mutex::Autolock _l(mLock);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001070 // result will remain NO_INIT if no audio device is present
1071 status_t final_result = NO_INIT;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001072 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001073 AutoMutex lock(mHardwareLock);
1074 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1075 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001076 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1077 status_t result = dev->setParameters(keyValuePairs);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001078 // return success if at least one audio device accepts the parameters as not all
1079 // HALs are requested to support all parameters. If no audio device supports the
1080 // requested parameters, the last error is reported.
1081 if (final_result != NO_ERROR) {
1082 final_result = result;
1083 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001084 }
1085 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001086 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001087 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1088 AudioParameter param = AudioParameter(keyValuePairs);
1089 String8 value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001090 if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) {
1091 bool btNrecIsOff = (value == AudioParameter::valueOff);
Eric Laurentbee53372011-08-29 12:42:48 -07001092 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001093 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1094 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001095 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001096 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1097 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001098 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001099 // suspend effects associated with those session IDs
1100 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001101 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001102 thread->setEffectSuspended(FX_IID_AEC,
1103 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001104 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001105 thread->setEffectSuspended(FX_IID_NS,
1106 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001107 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001108 }
1109 }
Eric Laurentbee53372011-08-29 12:42:48 -07001110 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001111 }
1112 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001113 String8 screenState;
1114 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
Mikhail Naganov00260b52016-10-13 12:54:24 -07001115 bool isOff = (screenState == AudioParameter::valueOff);
Eric Laurent81784c32012-11-19 14:55:58 -08001116 if (isOff != (AudioFlinger::mScreenState & 1)) {
1117 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001118 }
1119 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001120 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121 }
1122
1123 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1124 // and the thread is exited once the lock is released
1125 sp<ThreadBase> thread;
1126 {
1127 Mutex::Autolock _l(mLock);
1128 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001129 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001131 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001132 // indicate output device change to all input threads for pre processing
1133 AudioParameter param = AudioParameter(keyValuePairs);
1134 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001135 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1136 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001137 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001138 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139 }
1140 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001141 if (thread != 0) {
1142 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143 }
1144 return BAD_VALUE;
1145}
1146
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001147String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001149 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1150 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151
Eric Laurenta4c5a552012-03-29 10:12:40 -07001152 Mutex::Autolock _l(mLock);
1153
Glenn Kasten142f5192014-03-25 17:44:59 -07001154 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001155 String8 out_s8;
1156
Dima Zavin799a70e2011-04-18 16:57:27 -07001157 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001158 String8 s;
1159 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001160 {
1161 AutoMutex lock(mHardwareLock);
1162 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001163 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1164 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001165 mHardwareStatus = AUDIO_HW_IDLE;
1166 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001167 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001168 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001169 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170 }
1171
Mathias Agopian65ab4712010-07-14 17:59:35 -07001172 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1173 if (playbackThread != NULL) {
1174 return playbackThread->getParameters(keys);
1175 }
1176 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1177 if (recordThread != NULL) {
1178 return recordThread->getParameters(keys);
1179 }
1180 return String8("");
1181}
1182
Glenn Kastendd8104c2012-07-02 12:42:44 -07001183size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1184 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185{
Eric Laurenta1884f92011-08-23 08:25:03 -07001186 status_t ret = initCheck();
1187 if (ret != NO_ERROR) {
1188 return 0;
1189 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001190 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001191 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001192 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001193 return 0;
1194 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001195
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001196 AutoMutex lock(mHardwareLock);
1197 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001198 audio_config_t config, proposed;
1199 memset(&proposed, 0, sizeof(proposed));
1200 proposed.sample_rate = sampleRate;
1201 proposed.channel_mask = channelMask;
1202 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001203
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001204 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001205 size_t frames;
1206 for (;;) {
1207 // Note: config is currently a const parameter for get_input_buffer_size()
1208 // but we use a copy from proposed in case config changes from the call.
1209 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001210 status_t result = dev->getInputBufferSize(&config, &frames);
1211 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001212 break; // hal success, config is the result
1213 }
1214 // change one parameter of the configuration each iteration to a more "common" value
1215 // to see if the device will support it.
1216 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1217 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1218 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1219 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1220 } else {
1221 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1222 "format %#x, channelMask 0x%X",
1223 sampleRate, format, channelMask);
1224 break; // retries failed, break out of loop with frames == 0.
1225 }
1226 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001227 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001228 if (frames > 0 && config.sample_rate != sampleRate) {
1229 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1230 }
1231 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001232}
1233
Glenn Kasten5f972c02014-01-13 09:59:31 -08001234uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236 Mutex::Autolock _l(mLock);
1237
1238 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1239 if (recordThread != NULL) {
1240 return recordThread->getInputFramesLost();
1241 }
1242 return 0;
1243}
1244
1245status_t AudioFlinger::setVoiceVolume(float value)
1246{
Eric Laurenta1884f92011-08-23 08:25:03 -07001247 status_t ret = initCheck();
1248 if (ret != NO_ERROR) {
1249 return ret;
1250 }
1251
Mathias Agopian65ab4712010-07-14 17:59:35 -07001252 // check calling permissions
1253 if (!settingsAllowed()) {
1254 return PERMISSION_DENIED;
1255 }
1256
1257 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001258 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001259 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001260 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001261 mHardwareStatus = AUDIO_HW_IDLE;
1262
1263 return ret;
1264}
1265
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001266status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001267 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001268{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269 Mutex::Autolock _l(mLock);
1270
1271 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1272 if (playbackThread != NULL) {
1273 return playbackThread->getRenderPosition(halFrames, dspFrames);
1274 }
1275
1276 return BAD_VALUE;
1277}
1278
1279void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1280{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001281 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001282 if (client == 0) {
1283 return;
1284 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001285 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001286 {
1287 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001288 if (mNotificationClients.indexOfKey(pid) < 0) {
1289 sp<NotificationClient> notificationClient = new NotificationClient(this,
1290 client,
1291 pid);
1292 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001293
Eric Laurent021cf962014-05-13 10:18:14 -07001294 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001295
Marco Nelissen06b46062014-11-14 07:58:25 -08001296 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001297 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001298 }
1299 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300
Eric Laurent021cf962014-05-13 10:18:14 -07001301 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001302 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001303 // the config change is always sent from playback or record threads to avoid deadlock
1304 // with AudioSystem::gLock
1305 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1306 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1307 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001308
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001309 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1310 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001311 }
1312}
1313
1314void AudioFlinger::removeNotificationClient(pid_t pid)
1315{
1316 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001317 {
1318 Mutex::Autolock _cl(mClientLock);
1319 mNotificationClients.removeItem(pid);
1320 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001321
Steve Block3856b092011-10-20 11:56:00 +01001322 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001323 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001324 bool removed = false;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001325 for (size_t i = 0; i < num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001326 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001327 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001328 if (ref->mPid == pid) {
1329 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001330 mAudioSessionRefs.removeAt(i);
1331 delete ref;
1332 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001333 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001334 } else {
1335 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001336 }
1337 }
1338 if (removed) {
1339 purgeStaleEffects_l();
1340 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001341}
1342
Eric Laurent73e26b62015-04-27 16:55:58 -07001343void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001344 const sp<AudioIoDescriptor>& ioDesc,
1345 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001346{
Eric Laurent021cf962014-05-13 10:18:14 -07001347 Mutex::Autolock _l(mClientLock);
1348 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001350 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1351 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1352 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001353 }
1354}
1355
Eric Laurent021cf962014-05-13 10:18:14 -07001356// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001357void AudioFlinger::removeClient_l(pid_t pid)
1358{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001359 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001360 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001361 mClients.removeItem(pid);
1362}
1363
Eric Laurent717e1282012-06-29 16:36:52 -07001364// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001365sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1366 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001367{
1368 sp<PlaybackThread> thread;
1369
1370 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1371 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1372 ALOG_ASSERT(thread == 0);
1373 thread = mPlaybackThreads.valueAt(i);
1374 }
1375 }
1376
1377 return thread;
1378}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001379
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381
1382// ----------------------------------------------------------------------------
1383
1384AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1385 : RefBase(),
1386 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001387 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301389 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1390 heapSize *= 1024;
1391 if (!heapSize) {
1392 heapSize = kClientSharedHeapSizeBytes;
1393 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1394 // invalidated tracks
1395 if (!audioFlinger->isLowRamDevice()) {
1396 heapSize *= kClientSharedHeapSizeMultiplier;
1397 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001398 }
1399 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001400}
1401
Eric Laurent021cf962014-05-13 10:18:14 -07001402// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403AudioFlinger::Client::~Client()
1404{
1405 mAudioFlinger->removeClient_l(mPid);
1406}
1407
Glenn Kasten435dbe62012-01-30 10:15:48 -08001408sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409{
1410 return mMemoryDealer;
1411}
1412
1413// ----------------------------------------------------------------------------
1414
1415AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1416 const sp<IAudioFlingerClient>& client,
1417 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001418 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001419{
1420}
1421
1422AudioFlinger::NotificationClient::~NotificationClient()
1423{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424}
1425
Glenn Kasten0f11b512014-01-31 16:18:54 -08001426void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001427{
1428 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001429 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001430}
1431
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432
1433// ----------------------------------------------------------------------------
1434
1435sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001436 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001438 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001439 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001440 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001441 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -07001442 audio_input_flags_t *flags,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001443 pid_t pid,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001444 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001445 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001446 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001447 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001448 sp<IMemory>& cblk,
1449 sp<IMemory>& buffers,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001450 status_t *status,
1451 audio_port_handle_t portId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001452{
1453 sp<RecordThread::RecordTrack> recordTrack;
1454 sp<RecordHandle> recordHandle;
1455 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001457 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001458
Glenn Kastend776ac62014-05-07 09:16:09 -07001459 cblk.clear();
1460 buffers.clear();
1461
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001462 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001463 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1464 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001465 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001466 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1467 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001468 updatePid = true;
1469 }
1470
1471 if (updatePid) {
1472 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
1473 ALOGW_IF(pid != -1 && pid != callingPid,
1474 "%s uid %d pid %d tried to pass itself off as pid %d",
1475 __func__, callingUid, callingPid, pid);
1476 pid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001477 }
1478
Mathias Agopian65ab4712010-07-14 17:59:35 -07001479 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001480 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001481 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482 lStatus = PERMISSION_DENIED;
1483 goto Exit;
1484 }
1485
Glenn Kasten53b5d092014-02-05 10:00:23 -08001486 // further sample rate checks are performed by createRecordTrack_l()
1487 if (sampleRate == 0) {
1488 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1489 lStatus = BAD_VALUE;
1490 goto Exit;
1491 }
1492
Andy Hung6770c6f2015-04-07 13:43:36 -07001493 // we don't yet support anything other than linear PCM
1494 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001495 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001496 lStatus = BAD_VALUE;
1497 goto Exit;
1498 }
1499
Glenn Kasten53b5d092014-02-05 10:00:23 -08001500 // further channel mask checks are performed by createRecordTrack_l()
1501 if (!audio_is_input_channel(channelMask)) {
1502 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1503 lStatus = BAD_VALUE;
1504 goto Exit;
1505 }
1506
Glenn Kasten05997e22014-03-13 15:08:33 -07001507 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001508 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001509 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001510 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001511 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512 lStatus = BAD_VALUE;
1513 goto Exit;
1514 }
1515
Eric Laurent021cf962014-05-13 10:18:14 -07001516 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001517
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001518 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001519 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1520 lStatus = BAD_VALUE;
1521 goto Exit;
1522 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001523 lSessionId = *sessionId;
1524 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001525 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001526 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527 if (sessionId != NULL) {
1528 *sessionId = lSessionId;
1529 }
1530 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001531 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001532
Glenn Kasten1879fff2012-07-11 15:36:59 -07001533 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001534 frameCount, lSessionId, notificationFrames,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001535 clientUid, flags, tid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001536 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001537
1538 if (lStatus == NO_ERROR) {
1539 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1540 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001541 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001542 if (chain != 0) {
1543 Mutex::Autolock _l(thread->mLock);
1544 thread->addEffectChain_l(chain);
1545 }
1546 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001547 }
Glenn Kastene198c362013-08-13 09:13:36 -07001548
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001549 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001550 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001551 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001552 // Don't hold mClientLock when releasing the reference on the track as the
1553 // destructor will acquire it.
1554 {
1555 Mutex::Autolock _cl(mClientLock);
1556 client.clear();
1557 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001558 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001559 goto Exit;
1560 }
1561
Glenn Kastend776ac62014-05-07 09:16:09 -07001562 cblk = recordTrack->getCblk();
1563 buffers = recordTrack->getBuffers();
1564
Glenn Kasten2fc14732013-08-05 14:58:14 -07001565 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001566 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001567
1568Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001569 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001570 return recordHandle;
1571}
1572
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001573
1574
Mathias Agopian65ab4712010-07-14 17:59:35 -07001575// ----------------------------------------------------------------------------
1576
Eric Laurenta4c5a552012-03-29 10:12:40 -07001577audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1578{
Eric Laurent44622db2014-08-01 19:00:33 -07001579 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001580 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001581 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001582 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001583 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001584 }
1585 Mutex::Autolock _l(mLock);
1586 return loadHwModule_l(name);
1587}
1588
1589// loadHwModule_l() must be called with AudioFlinger::mLock held
1590audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1591{
1592 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1593 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1594 ALOGW("loadHwModule() module %s already loaded", name);
1595 return mAudioHwDevs.keyAt(i);
1596 }
1597 }
1598
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001599 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001600
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001601 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001602 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001603 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001604 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001605 }
1606
1607 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001608 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07001609 mHardwareStatus = AUDIO_HW_IDLE;
1610 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001611 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001612 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001613 }
1614
John Grossmanee578c02012-07-23 17:05:46 -07001615 // Check and cache this HAL's level of support for master mute and master
1616 // volume. If this is the first HAL opened, and it supports the get
1617 // methods, use the initial values provided by the HAL as the current
1618 // master mute and volume settings.
1619
1620 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1621 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001622 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001623
1624 if (0 == mAudioHwDevs.size()) {
1625 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001626 float mv;
1627 if (OK == dev->getMasterVolume(&mv)) {
1628 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07001629 }
1630
1631 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001632 bool mm;
1633 if (OK == dev->getMasterMute(&mm)) {
1634 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07001635 }
1636 }
1637
Eric Laurenta4c5a552012-03-29 10:12:40 -07001638 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001639 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07001640 flags = static_cast<AudioHwDevice::Flags>(flags |
1641 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1642 }
1643
1644 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001645 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07001646 flags = static_cast<AudioHwDevice::Flags>(flags |
1647 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1648 }
1649
Eric Laurenta4c5a552012-03-29 10:12:40 -07001650 mHardwareStatus = AUDIO_HW_IDLE;
1651 }
1652
Glenn Kastena13cde92016-03-28 15:26:02 -07001653 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001654 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001655
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001656 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001657
1658 return handle;
1659
1660}
1661
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001662// ----------------------------------------------------------------------------
1663
Glenn Kasten3b16c762012-11-14 08:44:39 -08001664uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001665{
1666 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001667 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001668 return thread != NULL ? thread->sampleRate() : 0;
1669}
1670
Glenn Kastene33054e2012-11-14 12:54:39 -08001671size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001672{
1673 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001674 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001675 return thread != NULL ? thread->frameCountHAL() : 0;
1676}
1677
1678// ----------------------------------------------------------------------------
1679
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001680status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1681{
1682 uid_t uid = IPCThreadState::self()->getCallingUid();
1683 if (uid != AID_SYSTEM) {
1684 return PERMISSION_DENIED;
1685 }
1686 Mutex::Autolock _l(mLock);
1687 if (mIsDeviceTypeKnown) {
1688 return INVALID_OPERATION;
1689 }
1690 mIsLowRamDevice = isLowRamDevice;
1691 mIsDeviceTypeKnown = true;
1692 return NO_ERROR;
1693}
1694
Eric Laurent93c3d412014-08-01 14:48:35 -07001695audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1696{
1697 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001698
1699 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1700 if (index >= 0) {
1701 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1702 mHwAvSyncIds.valueAt(index), sessionId);
1703 return mHwAvSyncIds.valueAt(index);
1704 }
1705
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001706 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07001707 if (dev == NULL) {
1708 return AUDIO_HW_SYNC_INVALID;
1709 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001710 String8 reply;
1711 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001712 if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001713 param = AudioParameter(reply);
1714 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001715
1716 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001717 if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001718 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1719 return AUDIO_HW_SYNC_INVALID;
1720 }
1721
1722 // allow only one session for a given HW A/V sync ID.
1723 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1724 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1725 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1726 value, mHwAvSyncIds.keyAt(i));
1727 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001728 break;
1729 }
1730 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001731
1732 mHwAvSyncIds.add(sessionId, value);
1733
1734 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1735 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1736 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07001737 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001738 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001739 param.addInt(String8(AudioParameter::keyStreamHwAvSync), value);
Eric Laurentfa90e842014-10-17 18:12:31 -07001740 thread->setParameters(param.toString());
1741 break;
1742 }
1743 }
1744
1745 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1746 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001747}
1748
Eric Laurent72e3f392015-05-20 14:43:50 -07001749status_t AudioFlinger::systemReady()
1750{
1751 Mutex::Autolock _l(mLock);
1752 ALOGI("%s", __FUNCTION__);
1753 if (mSystemReady) {
1754 ALOGW("%s called twice", __FUNCTION__);
1755 return NO_ERROR;
1756 }
1757 mSystemReady = true;
1758 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1759 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1760 thread->systemReady();
1761 }
1762 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1763 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1764 thread->systemReady();
1765 }
1766 return NO_ERROR;
1767}
1768
Eric Laurentfa90e842014-10-17 18:12:31 -07001769// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1770void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1771{
1772 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1773 if (index >= 0) {
1774 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1775 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1776 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001777 param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId);
Eric Laurentfa90e842014-10-17 18:12:31 -07001778 thread->setParameters(param.toString());
1779 }
1780}
1781
1782
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001783// ----------------------------------------------------------------------------
1784
Eric Laurent83b88082014-06-20 18:31:16 -07001785
1786sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001787 audio_io_handle_t *output,
1788 audio_config_t *config,
1789 audio_devices_t devices,
1790 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001791 audio_output_flags_t flags)
1792{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001793 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001794 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001795 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001796 }
1797
Eric Laurentcf2c0212014-07-25 16:20:43 -07001798 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001799 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1800 } else {
1801 // Audio Policy does not currently request a specific output handle.
1802 // If this is ever needed, see openInput_l() for example code.
1803 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1804 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001805 }
Eric Laurent83b88082014-06-20 18:31:16 -07001806
1807 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1808
Eric Laurent83b88082014-06-20 18:31:16 -07001809 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001810 // This if statement allows overriding the audio policy settings
1811 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1812 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1813 // Check only for Normal Mixing mode
1814 if (kEnableExtendedPrecision) {
1815 // Specify format (uncomment one below to choose)
1816 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1817 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1818 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1819 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001820 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001821 }
1822 if (kEnableExtendedChannels) {
1823 // Specify channel mask (uncomment one below to choose)
1824 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1825 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1826 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1827 }
Eric Laurent83b88082014-06-20 18:31:16 -07001828 }
1829
Phil Burk062e67a2015-02-11 13:40:50 -08001830 AudioStreamOut *outputStream = NULL;
1831 status_t status = outHwDev->openOutputStream(
1832 &outputStream,
1833 *output,
1834 devices,
1835 flags,
1836 config,
1837 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001838
1839 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001840
Phil Burk062e67a2015-02-11 13:40:50 -08001841 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001842
1843 PlaybackThread *thread;
1844 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurente93cc032016-05-05 10:15:10 -07001845 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001846 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001847 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1848 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001849 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001850 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001851 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001852 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001853 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001854 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001855 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001856 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001857 return thread;
1858 }
1859
1860 return 0;
1861}
1862
Eric Laurentcf2c0212014-07-25 16:20:43 -07001863status_t AudioFlinger::openOutput(audio_module_handle_t module,
1864 audio_io_handle_t *output,
1865 audio_config_t *config,
1866 audio_devices_t *devices,
1867 const String8& address,
1868 uint32_t *latencyMs,
1869 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001870{
Phil Burk062e67a2015-02-11 13:40:50 -08001871 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001872 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001873 (devices != NULL) ? *devices : 0,
1874 config->sample_rate,
1875 config->format,
1876 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001877 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878
Eric Laurentcf2c0212014-07-25 16:20:43 -07001879 if (*devices == AUDIO_DEVICE_NONE) {
1880 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001881 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001882
Mathias Agopian65ab4712010-07-14 17:59:35 -07001883 Mutex::Autolock _l(mLock);
1884
Eric Laurentcf2c0212014-07-25 16:20:43 -07001885 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001886 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001887 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001888
1889 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001890 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001891
1892 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001893 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001894 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001895 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001896
1897 AutoMutex lock(mHardwareLock);
1898 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001899 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001900 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001901 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001902 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001903 }
1904
Eric Laurentcf2c0212014-07-25 16:20:43 -07001905 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001906}
1907
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001908audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1909 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001910{
1911 Mutex::Autolock _l(mLock);
1912 MixerThread *thread1 = checkMixerThread_l(output1);
1913 MixerThread *thread2 = checkMixerThread_l(output2);
1914
1915 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001916 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1917 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001918 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001919 }
1920
Glenn Kasteneeecb982016-02-26 10:44:04 -08001921 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07001922 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923 thread->addOutputTrack(thread2);
1924 mPlaybackThreads.add(id, thread);
1925 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001926 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001927 return id;
1928}
1929
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001930status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001931{
Glenn Kastend96c5722012-04-25 13:44:49 -07001932 return closeOutput_nonvirtual(output);
1933}
1934
1935status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1936{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937 // keep strong reference on the playback thread so that
1938 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001939 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001940 {
1941 Mutex::Autolock _l(mLock);
1942 thread = checkPlaybackThread_l(output);
1943 if (thread == NULL) {
1944 return BAD_VALUE;
1945 }
1946
Steve Block3856b092011-10-20 11:56:00 +01001947 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001949 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001951 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001952 DuplicatingThread *dupThread =
1953 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001954 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001955 }
1956 }
1957 }
1958
1959
1960 mPlaybackThreads.removeItem(output);
1961 // save all effects to the default thread
1962 if (mPlaybackThreads.size()) {
1963 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1964 if (dstThread != NULL) {
1965 // audioflinger lock is held here so the acquisition order of thread locks does not
1966 // matter
1967 Mutex::Autolock _dl(dstThread->mLock);
1968 Mutex::Autolock _sl(thread->mLock);
1969 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1970 for (size_t i = 0; i < effectChains.size(); i ++) {
1971 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001972 }
1973 }
1974 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001975 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1976 ioDesc->mIoHandle = output;
1977 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001978 }
1979 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001980 // The thread entity (active unit of execution) is no longer running here,
1981 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001982
Eric Laurentf6870ae2015-05-08 10:50:03 -07001983 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001984 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985 }
Eric Laurent83b88082014-06-20 18:31:16 -07001986
Mathias Agopian65ab4712010-07-14 17:59:35 -07001987 return NO_ERROR;
1988}
1989
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001990void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07001991{
1992 AudioStreamOut *out = thread->clearOutput();
1993 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1994 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07001995 delete out;
1996}
1997
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001998void AudioFlinger::closeOutputInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07001999{
2000 mPlaybackThreads.removeItem(thread->mId);
2001 thread->exit();
2002 closeOutputFinish(thread);
2003}
2004
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002005status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002006{
2007 Mutex::Autolock _l(mLock);
2008 PlaybackThread *thread = checkPlaybackThread_l(output);
2009
2010 if (thread == NULL) {
2011 return BAD_VALUE;
2012 }
2013
Steve Block3856b092011-10-20 11:56:00 +01002014 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002015 thread->suspend();
2016
2017 return NO_ERROR;
2018}
2019
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002020status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002021{
2022 Mutex::Autolock _l(mLock);
2023 PlaybackThread *thread = checkPlaybackThread_l(output);
2024
2025 if (thread == NULL) {
2026 return BAD_VALUE;
2027 }
2028
Steve Block3856b092011-10-20 11:56:00 +01002029 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002030
2031 thread->restore();
2032
2033 return NO_ERROR;
2034}
2035
Eric Laurentcf2c0212014-07-25 16:20:43 -07002036status_t AudioFlinger::openInput(audio_module_handle_t module,
2037 audio_io_handle_t *input,
2038 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002039 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002040 const String8& address,
2041 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002042 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002043{
Eric Laurent83b88082014-06-20 18:31:16 -07002044 Mutex::Autolock _l(mLock);
2045
Glenn Kastene7d66712015-03-05 13:46:52 -08002046 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002047 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002048 }
2049
Glenn Kastene7d66712015-03-05 13:46:52 -08002050 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002051
2052 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002053 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002054 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002055 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002056 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002057 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002058}
Dima Zavin799a70e2011-04-18 16:57:27 -07002059
Eric Laurent83b88082014-06-20 18:31:16 -07002060sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002061 audio_io_handle_t *input,
2062 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002063 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002064 const String8& address,
2065 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002066 audio_input_flags_t flags)
2067{
Glenn Kastene7d66712015-03-05 13:46:52 -08002068 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002069 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002070 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002071 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002072 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002073
Glenn Kasteneeecb982016-02-26 10:44:04 -08002074 // Audio Policy can request a specific handle for hardware hotword.
2075 // The goal here is not to re-open an already opened input.
2076 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002077 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002078 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2079 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2080 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2081 return 0;
2082 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2083 // This should not happen in a transient state with current design.
2084 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2085 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002086 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002087
Eric Laurentcf2c0212014-07-25 16:20:43 -07002088 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002089 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002090 sp<StreamInHalInterface> inStream;
2091 status_t status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002092 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Mikhail Naganovf558e022016-11-14 17:45:17 -08002093 ALOGV("openInput_l() openInputStream returned input %p, devices %x, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002094 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002095 inStream.get(),
Mikhail Naganovf558e022016-11-14 17:45:17 -08002096 devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002097 halconfig.sample_rate,
2098 halconfig.format,
2099 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002100 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002101 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002102
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002103 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002104 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002105 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002106 audio_is_linear_pcm(config->format) &&
2107 audio_is_linear_pcm(halconfig.format) &&
2108 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002109 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2110 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002111 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002112 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002113 inStream.clear();
2114 status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002115 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002116 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002117 }
2118
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002119 if (status == NO_ERROR && inStream != 0) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002120
Glenn Kasten46909e72013-02-26 09:20:22 -08002121#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002122 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2123 // or (re-)create if current Pipe is idle and does not match the new format
2124 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002125 enum {
2126 TEE_SINK_NO, // don't copy input
2127 TEE_SINK_NEW, // copy input using a new pipe
2128 TEE_SINK_OLD, // copy input using an existing pipe
2129 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002130 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2131 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002132 if (!mTeeSinkInputEnabled) {
2133 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002134 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002135 kind = TEE_SINK_NO;
2136 } else if (mRecordTeeSink == 0) {
2137 kind = TEE_SINK_NEW;
2138 } else if (mRecordTeeSink->getStrongCount() != 1) {
2139 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002140 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002141 kind = TEE_SINK_OLD;
2142 } else {
2143 kind = TEE_SINK_NEW;
2144 }
2145 switch (kind) {
2146 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002147 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002148 size_t numCounterOffers = 0;
2149 const NBAIO_Format offers[1] = {format};
2150 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2151 ALOG_ASSERT(index == 0);
2152 PipeReader *pipeReader = new PipeReader(*pipe);
2153 numCounterOffers = 0;
2154 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2155 ALOG_ASSERT(index == 0);
2156 mRecordTeeSink = pipe;
2157 mRecordTeeSource = pipeReader;
2158 teeSink = pipe;
2159 }
2160 break;
2161 case TEE_SINK_OLD:
2162 teeSink = mRecordTeeSink;
2163 break;
2164 case TEE_SINK_NO:
2165 default:
2166 break;
2167 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002168#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002169
Eric Laurent05067782016-06-01 18:27:28 -07002170 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07002171
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002172 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002173 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002174 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002175 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002176 inputStream,
2177 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002178 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002179 devices,
2180 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002181#ifdef TEE_SINK
2182 , teeSink
2183#endif
2184 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002185 mRecordThreads.add(*input, thread);
2186 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002187 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002188 }
2189
Eric Laurentcf2c0212014-07-25 16:20:43 -07002190 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002191 return 0;
2192}
2193
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002194status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002195{
Glenn Kastend96c5722012-04-25 13:44:49 -07002196 return closeInput_nonvirtual(input);
2197}
2198
2199status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2200{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002201 // keep strong reference on the record thread so that
2202 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002203 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002204 {
2205 Mutex::Autolock _l(mLock);
2206 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002207 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002208 return BAD_VALUE;
2209 }
2210
Steve Block3856b092011-10-20 11:56:00 +01002211 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002212
2213 // If we still have effect chains, it means that a client still holds a handle
2214 // on at least one effect. We must either move the chain to an existing thread with the
2215 // same session ID or put it aside in case a new record thread is opened for a
2216 // new capture on the same session
2217 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002218 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002219 Mutex::Autolock _sl(thread->mLock);
2220 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002221 // Note: maximum one chain per record thread
2222 if (effectChains.size() != 0) {
2223 chain = effectChains[0];
2224 }
2225 }
2226 if (chain != 0) {
2227 // first check if a record thread is already opened with a client on the same session.
2228 // This should only happen in case of overlap between one thread tear down and the
2229 // creation of its replacement
2230 size_t i;
2231 for (i = 0; i < mRecordThreads.size(); i++) {
2232 sp<RecordThread> t = mRecordThreads.valueAt(i);
2233 if (t == thread) {
2234 continue;
2235 }
2236 if (t->hasAudioSession(chain->sessionId()) != 0) {
2237 Mutex::Autolock _l(t->mLock);
2238 ALOGV("closeInput() found thread %d for effect session %d",
2239 t->id(), chain->sessionId());
2240 t->addEffectChain_l(chain);
2241 break;
2242 }
2243 }
2244 // put the chain aside if we could not find a record thread with the same session id.
2245 if (i == mRecordThreads.size()) {
2246 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002247 }
2248 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002249 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2250 ioDesc->mIoHandle = input;
2251 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002252 mRecordThreads.removeItem(input);
2253 }
Eric Laurent83b88082014-06-20 18:31:16 -07002254 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2255 // we have a different lock for notification client
2256 closeInputFinish(thread);
2257 return NO_ERROR;
2258}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002259
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002260void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002261{
2262 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002263 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002264 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002265 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002266 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002267}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002268
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002269void AudioFlinger::closeInputInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002270{
2271 mRecordThreads.removeItem(thread->mId);
2272 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002273}
2274
Glenn Kastend2304db2014-02-03 07:40:31 -08002275status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002276{
2277 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002278 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002279
2280 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2281 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002282 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002283 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002284
2285 return NO_ERROR;
2286}
2287
2288
Glenn Kasteneeecb982016-02-26 10:44:04 -08002289audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002290{
Glenn Kasten9d003132016-04-06 14:38:09 -07002291 // This is a binder API, so a malicious client could pass in a bad parameter.
2292 // Check for that before calling the internal API nextUniqueId().
2293 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2294 ALOGE("newAudioUniqueId invalid use %d", use);
2295 return AUDIO_UNIQUE_ID_ALLOCATE;
2296 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002297 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002298}
2299
Glenn Kastend848eb42016-03-08 13:42:11 -08002300void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002301{
2302 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002303 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002304 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2305 if (pid != -1 && (caller == getpid_cached)) {
2306 caller = pid;
2307 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002308
Eric Laurent021cf962014-05-13 10:18:14 -07002309 {
2310 Mutex::Autolock _cl(mClientLock);
2311 // Ignore requests received from processes not known as notification client. The request
2312 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2313 // called from a different pid leaving a stale session reference. Also we don't know how
2314 // to clear this reference if the client process dies.
2315 if (mNotificationClients.indexOfKey(caller) < 0) {
2316 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2317 return;
2318 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002319 }
2320
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002321 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002322 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002323 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002324 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2325 ref->mCnt++;
2326 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002327 return;
2328 }
2329 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002330 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2331 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002332}
2333
Glenn Kastend848eb42016-03-08 13:42:11 -08002334void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002335{
2336 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002337 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002338 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2339 if (pid != -1 && (caller == getpid_cached)) {
2340 caller = pid;
2341 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002342 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002343 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002344 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002345 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2346 ref->mCnt--;
2347 ALOGV(" decremented refcount to %d", ref->mCnt);
2348 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002349 mAudioSessionRefs.removeAt(i);
2350 delete ref;
2351 purgeStaleEffects_l();
2352 }
2353 return;
2354 }
2355 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002356 // If the caller is mediaserver it is likely that the session being released was acquired
2357 // on behalf of a process not in notification clients and we ignore the warning.
2358 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002359}
2360
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002361bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
2362{
2363 size_t num = mAudioSessionRefs.size();
2364 for (size_t i = 0; i < num; i++) {
2365 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2366 if (ref->mSessionid == audioSession) {
2367 return true;
2368 }
2369 }
2370 return false;
2371}
2372
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002373void AudioFlinger::purgeStaleEffects_l() {
2374
Steve Block3856b092011-10-20 11:56:00 +01002375 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002376
2377 Vector< sp<EffectChain> > chains;
2378
2379 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2380 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2381 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2382 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002383 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2384 chains.push(ec);
2385 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002386 }
2387 }
2388 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2389 sp<RecordThread> t = mRecordThreads.valueAt(i);
2390 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2391 sp<EffectChain> ec = t->mEffectChains[j];
2392 chains.push(ec);
2393 }
2394 }
2395
2396 for (size_t i = 0; i < chains.size(); i++) {
2397 sp<EffectChain> ec = chains[i];
2398 int sessionid = ec->sessionId();
2399 sp<ThreadBase> t = ec->mThread.promote();
2400 if (t == 0) {
2401 continue;
2402 }
2403 size_t numsessionrefs = mAudioSessionRefs.size();
2404 bool found = false;
2405 for (size_t k = 0; k < numsessionrefs; k++) {
2406 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002407 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002408 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002409 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002410 found = true;
2411 break;
2412 }
2413 }
2414 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002415 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002416 // remove all effects from the chain
2417 while (ec->mEffects.size()) {
2418 sp<EffectModule> effect = ec->mEffects[0];
2419 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002420 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002421 if (effect->purgeHandles()) {
2422 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002423 }
2424 AudioSystem::unregisterEffect(effect->id());
2425 }
2426 }
2427 }
2428 return;
2429}
2430
Glenn Kasteneeecb982016-02-26 10:44:04 -08002431// checkThread_l() must be called with AudioFlinger::mLock held
2432AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2433{
2434 ThreadBase *thread = NULL;
2435 switch (audio_unique_id_get_use(ioHandle)) {
2436 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2437 thread = checkPlaybackThread_l(ioHandle);
2438 break;
2439 case AUDIO_UNIQUE_ID_USE_INPUT:
2440 thread = checkRecordThread_l(ioHandle);
2441 break;
2442 default:
2443 break;
2444 }
2445 return thread;
2446}
2447
Mathias Agopian65ab4712010-07-14 17:59:35 -07002448// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002449AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002450{
Glenn Kastena1117922012-01-26 10:53:32 -08002451 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002452}
2453
2454// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002455AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002456{
2457 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002458 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002459}
2460
2461// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002462AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002463{
Glenn Kastena1117922012-01-26 10:53:32 -08002464 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002465}
2466
Glenn Kasteneeecb982016-02-26 10:44:04 -08002467audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002468{
Glenn Kasten9d003132016-04-06 14:38:09 -07002469 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002470 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002471 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2472 for (int retry = 0; retry < maxRetries; retry++) {
2473 // The cast allows wraparound from max positive to min negative instead of abort
2474 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2475 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2476 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2477 // allow wrap by skipping 0 and -1 for session ids
2478 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2479 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2480 return (audio_unique_id_t) (base | use);
2481 }
2482 }
2483 // We have no way of recovering from wraparound
2484 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2485 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002486}
2487
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002488AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002489{
2490 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2491 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002492 if(thread->isDuplicating()) {
2493 continue;
2494 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002495 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002496 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002497 return thread;
2498 }
2499 }
2500 return NULL;
2501}
2502
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002503audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002504{
2505 PlaybackThread *thread = primaryPlaybackThread_l();
2506
2507 if (thread == NULL) {
2508 return 0;
2509 }
2510
Eric Laurentf1c04f92012-08-28 14:26:53 -07002511 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002512}
2513
Glenn Kastena7335632016-06-09 17:09:53 -07002514AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
2515{
2516 size_t minFrameCount = 0;
2517 PlaybackThread *minThread = NULL;
2518 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2519 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2520 if (!thread->isDuplicating()) {
2521 size_t frameCount = thread->frameCountHAL();
2522 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
2523 (frameCount == minFrameCount && thread->hasFastMixer() &&
2524 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
2525 minFrameCount = frameCount;
2526 minThread = thread;
2527 }
2528 }
2529 }
2530 return minThread;
2531}
2532
Eric Laurenta011e352012-03-29 15:51:43 -07002533sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002534 audio_session_t triggerSession,
2535 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002536 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002537 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002538{
2539 Mutex::Autolock _l(mLock);
2540
2541 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2542 status_t playStatus = NAME_NOT_FOUND;
2543 status_t recStatus = NAME_NOT_FOUND;
2544 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2545 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2546 if (playStatus == NO_ERROR) {
2547 return event;
2548 }
2549 }
2550 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2551 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2552 if (recStatus == NO_ERROR) {
2553 return event;
2554 }
2555 }
2556 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2557 mPendingSyncEvents.add(event);
2558 } else {
2559 ALOGV("createSyncEvent() invalid event %d", event->type());
2560 event.clear();
2561 }
2562 return event;
2563}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002564
Mathias Agopian65ab4712010-07-14 17:59:35 -07002565// ----------------------------------------------------------------------------
2566// Effect management
2567// ----------------------------------------------------------------------------
2568
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002569sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
2570 return mEffectsFactoryHal;
2571}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002572
Glenn Kastenf587ba52012-01-26 16:25:10 -08002573status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002574{
2575 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002576 if (mEffectsFactoryHal.get()) {
2577 return mEffectsFactoryHal->queryNumberEffects(numEffects);
2578 } else {
2579 return -ENODEV;
2580 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002581}
2582
Glenn Kastenf587ba52012-01-26 16:25:10 -08002583status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002584{
2585 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002586 if (mEffectsFactoryHal.get()) {
2587 return mEffectsFactoryHal->getDescriptor(index, descriptor);
2588 } else {
2589 return -ENODEV;
2590 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002591}
2592
Glenn Kasten5e92a782012-01-30 07:40:52 -08002593status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002594 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002595{
2596 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002597 if (mEffectsFactoryHal.get()) {
2598 return mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
2599 } else {
2600 return -ENODEV;
2601 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002602}
2603
2604
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002605sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002606 effect_descriptor_t *pDesc,
2607 const sp<IEffectClient>& effectClient,
2608 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002609 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002610 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002611 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002612 status_t *status,
2613 int *id,
2614 int *enabled)
2615{
2616 status_t lStatus = NO_ERROR;
2617 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002618 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002619
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002620 pid_t pid = IPCThreadState::self()->getCallingPid();
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002621 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
2622 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002623
2624 if (pDesc == NULL) {
2625 lStatus = BAD_VALUE;
2626 goto Exit;
2627 }
2628
Eric Laurent84e9a102010-09-23 16:10:16 -07002629 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002630 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002631 lStatus = PERMISSION_DENIED;
2632 goto Exit;
2633 }
2634
Dima Zavinfce7a472011-04-19 22:30:36 -07002635 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002636 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002637 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002638 lStatus = PERMISSION_DENIED;
2639 goto Exit;
2640 }
2641
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002642 if (mEffectsFactoryHal == 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002643 lStatus = NO_INIT;
2644 goto Exit;
2645 }
2646
Mathias Agopian65ab4712010-07-14 17:59:35 -07002647 {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002648 if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002649 // if uuid is specified, request effect descriptor
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002650 lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002651 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002652 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002653 goto Exit;
2654 }
2655 } else {
2656 // if uuid is not specified, look for an available implementation
2657 // of the required type in effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002658 if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002659 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002660 lStatus = BAD_VALUE;
2661 goto Exit;
2662 }
2663 uint32_t numEffects = 0;
2664 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002665 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002666 bool found = false;
2667
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002668 lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002669 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002670 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002671 goto Exit;
2672 }
2673 for (uint32_t i = 0; i < numEffects; i++) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002674 lStatus = mEffectsFactoryHal->getDescriptor(i, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002675 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002676 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002677 continue;
2678 }
2679 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2680 // If matching type found save effect descriptor. If the session is
2681 // 0 and the effect is not auxiliary, continue enumeration in case
2682 // an auxiliary version of this effect type is available
2683 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002684 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002685 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002686 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2687 break;
2688 }
2689 }
2690 }
2691 if (!found) {
2692 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002693 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002694 goto Exit;
2695 }
2696 // For same effect type, chose auxiliary version over insert version if
2697 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002698 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002699 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002700 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002701 }
2702 }
2703
2704 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002705 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002706 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2707 lStatus = INVALID_OPERATION;
2708 goto Exit;
2709 }
2710
Eric Laurent59255e42011-07-27 19:49:51 -07002711 // check recording permission for visualizer
2712 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002713 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002714 lStatus = PERMISSION_DENIED;
2715 goto Exit;
2716 }
2717
Mathias Agopian65ab4712010-07-14 17:59:35 -07002718 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002719 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002720 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002721 // if the output returned by getOutputForEffect() is removed before we lock the
2722 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2723 // and we will exit safely
2724 io = AudioSystem::getOutputForEffect(&desc);
2725 ALOGV("createEffect got output %d", io);
2726 }
2727
2728 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002729
2730 // If output is not specified try to find a matching audio session ID in one of the
2731 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002732 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2733 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002734 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002735 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002736 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2737 // output must be specified by AudioPolicyManager when using session
2738 // AUDIO_SESSION_OUTPUT_STAGE
2739 lStatus = BAD_VALUE;
2740 goto Exit;
2741 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002742 // look for the thread where the specified audio session is present
2743 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2744 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2745 io = mPlaybackThreads.keyAt(i);
2746 break;
2747 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002748 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002749 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002750 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2751 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2752 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002753 break;
2754 }
2755 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002756 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002757 // If no output thread contains the requested session ID, default to
2758 // first output. The effect chain will be moved to the correct output
2759 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002760 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002761 io = mPlaybackThreads.keyAt(0);
2762 }
Steve Block3856b092011-10-20 11:56:00 +01002763 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002764 }
2765 ThreadBase *thread = checkRecordThread_l(io);
2766 if (thread == NULL) {
2767 thread = checkPlaybackThread_l(io);
2768 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002769 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002770 lStatus = BAD_VALUE;
2771 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002772 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002773 } else {
2774 // Check if one effect chain was awaiting for an effect to be created on this
2775 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08002776 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07002777 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002778 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002779 thread->addEffectChain_l(chain);
2780 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002781 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002782
Eric Laurent021cf962014-05-13 10:18:14 -07002783 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002784
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002785 // create effect on selected output thread
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002786 bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002787 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002788 &desc, enabled, &lStatus, pinned);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002789 if (handle != 0 && id != NULL) {
2790 *id = handle->id();
2791 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002792 if (handle == 0) {
2793 // remove local strong reference to Client with mClientLock held
2794 Mutex::Autolock _cl(mClientLock);
2795 client.clear();
2796 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002797 }
2798
2799Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002800 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002801 return handle;
2802}
2803
Glenn Kastend848eb42016-03-08 13:42:11 -08002804status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002805 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002806{
Steve Block3856b092011-10-20 11:56:00 +01002807 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002808 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002809 Mutex::Autolock _l(mLock);
2810 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002811 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002812 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002813 }
Eric Laurentde070132010-07-13 04:45:46 -07002814 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2815 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002816 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002817 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002818 }
Eric Laurentde070132010-07-13 04:45:46 -07002819 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2820 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002821 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002822 return BAD_VALUE;
2823 }
2824
2825 Mutex::Autolock _dl(dstThread->mLock);
2826 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002827 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002828}
2829
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002830// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08002831status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002832 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002833 AudioFlinger::PlaybackThread *dstThread,
2834 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002835{
Steve Block3856b092011-10-20 11:56:00 +01002836 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002837 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002838
Eric Laurent59255e42011-07-27 19:49:51 -07002839 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002840 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002841 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002842 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002843 return INVALID_OPERATION;
2844 }
2845
Eric Laurent4c415062016-06-17 16:14:16 -07002846 // Check whether the destination thread and all effects in the chain are compatible
2847 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07002848 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07002849 " destination thread %p is not compatible with effects in the chain",
2850 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07002851 return INVALID_OPERATION;
2852 }
2853
Eric Laurent39e94f82010-07-28 01:32:47 -07002854 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002855 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002856 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002857 // removed.
2858 srcThread->removeEffectChain_l(chain);
2859
2860 // transfer all effects one by one so that new effect chain is created on new thread with
2861 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002862 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002863 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002864 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002865 Vector< sp<EffectModule> > removed;
2866 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002867 while (effect != 0) {
2868 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002869 removed.add(effect);
2870 status = dstThread->addEffect_l(effect);
2871 if (status != NO_ERROR) {
2872 break;
2873 }
Eric Laurentec35a142011-10-05 17:42:25 -07002874 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2875 if (effect->state() == EffectModule::ACTIVE ||
2876 effect->state() == EffectModule::STOPPING) {
2877 effect->start();
2878 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002879 // if the move request is not received from audio policy manager, the effect must be
2880 // re-registered with the new strategy and output
2881 if (dstChain == 0) {
2882 dstChain = effect->chain().promote();
2883 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002884 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002885 status = NO_INIT;
2886 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002887 }
2888 strategy = dstChain->strategy();
2889 }
2890 if (reRegister) {
2891 AudioSystem::unregisterEffect(effect->id());
2892 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002893 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002894 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002895 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002896 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002897 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002898 }
Eric Laurentde070132010-07-13 04:45:46 -07002899 effect = chain->getEffectFromId_l(0);
2900 }
2901
Eric Laurent5baf2af2013-09-12 17:37:00 -07002902 if (status != NO_ERROR) {
2903 for (size_t i = 0; i < removed.size(); i++) {
2904 srcThread->addEffect_l(removed[i]);
2905 if (dstChain != 0 && reRegister) {
2906 AudioSystem::unregisterEffect(removed[i]->id());
2907 AudioSystem::registerEffect(&removed[i]->desc(),
2908 srcThread->id(),
2909 strategy,
2910 sessionId,
2911 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002912 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002913 }
2914 }
2915 }
2916
2917 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002918}
2919
Eric Laurent5baf2af2013-09-12 17:37:00 -07002920bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002921{
2922 if (mGlobalEffectEnableTime != 0 &&
2923 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2924 return true;
2925 }
2926
2927 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2928 sp<EffectChain> ec =
2929 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002930 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002931 return true;
2932 }
2933 }
2934 return false;
2935}
2936
Eric Laurent5baf2af2013-09-12 17:37:00 -07002937void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002938{
2939 Mutex::Autolock _l(mLock);
2940
2941 mGlobalEffectEnableTime = systemTime();
2942
2943 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2944 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2945 if (t->mType == ThreadBase::OFFLOAD) {
2946 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2947 }
2948 }
2949
2950}
2951
Eric Laurentaaa44472014-09-12 17:41:50 -07002952status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2953{
Glenn Kastend848eb42016-03-08 13:42:11 -08002954 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002955 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002956 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002957 if (index >= 0) {
2958 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2959 return ALREADY_EXISTS;
2960 }
2961 mOrphanEffectChains.add(session, chain);
2962 return NO_ERROR;
2963}
2964
2965sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2966{
2967 sp<EffectChain> chain;
2968 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002969 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002970 if (index >= 0) {
2971 chain = mOrphanEffectChains.valueAt(index);
2972 mOrphanEffectChains.removeItemsAt(index);
2973 }
2974 return chain;
2975}
2976
2977bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2978{
2979 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08002980 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002981 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002982 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002983 if (index >= 0) {
2984 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002985 if (chain->removeEffect_l(effect, true) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002986 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002987 mOrphanEffectChains.removeItemsAt(index);
2988 }
2989 return true;
2990 }
2991 return false;
2992}
2993
2994
Glenn Kastenda6ef132013-01-10 12:31:01 -08002995struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002996#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2997 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002998};
2999
3000int comparEntry(const void *p1, const void *p2)
3001{
Glenn Kasten2f55e762015-03-05 16:05:08 -08003002 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003003}
3004
Glenn Kasten46909e72013-02-26 09:20:22 -08003005#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08003006void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003007{
Eric Laurent81784c32012-11-19 14:55:58 -08003008 NBAIO_Source *teeSource = source.get();
3009 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003010 // .wav rotation
3011 // There is a benign race condition if 2 threads call this simultaneously.
3012 // They would both traverse the directory, but the result would simply be
3013 // failures at unlink() which are ignored. It's also unlikely since
3014 // normally dumpsys is only done by bugreport or from the command line.
3015 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08003016 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08003017 size_t teePathLen = strlen(teePath);
3018 DIR *dir = opendir(teePath);
3019 teePath[teePathLen++] = '/';
3020 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003021#define TEE_MAX_SORT 20 // number of entries to sort
3022#define TEE_MAX_KEEP 10 // number of entries to keep
3023 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003024 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08003025 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003026 struct dirent de;
3027 struct dirent *result = NULL;
3028 int rc = readdir_r(dir, &de, &result);
3029 if (rc != 0) {
3030 ALOGW("readdir_r failed %d", rc);
3031 break;
3032 }
3033 if (result == NULL) {
3034 break;
3035 }
3036 if (result != &de) {
3037 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
3038 break;
3039 }
3040 // ignore non .wav file entries
3041 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003042 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08003043 strcmp(&de.d_name[nameLen - 4], ".wav")) {
3044 continue;
3045 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08003046 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003047 }
3048 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003049 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003050 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003051 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
3052 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003053 (void) unlink(teePath);
3054 }
3055 }
3056 } else {
3057 if (fd >= 0) {
Glenn Kastenfbd87e82016-07-18 15:45:56 -07003058 dprintf(fd, "unable to rotate tees in %.*s: %s\n", (int) teePathLen, teePath,
Glenn Kasten9a003992016-02-23 15:24:34 -08003059 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003060 }
3061 }
Eric Laurent81784c32012-11-19 14:55:58 -08003062 char teeTime[16];
3063 struct timeval tv;
3064 gettimeofday(&tv, NULL);
3065 struct tm tm;
3066 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003067 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
3068 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
3069 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3070 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003071 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003072 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003073 char wavHeader[44];
3074 memcpy(wavHeader,
3075 "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",
3076 sizeof(wavHeader));
3077 NBAIO_Format format = teeSource->format();
3078 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003079 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003080 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003081 wavHeader[22] = channelCount; // number of channels
3082 wavHeader[24] = sampleRate; // sample rate
3083 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003084 wavHeader[32] = frameSize; // block alignment
3085 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003086 write(teeFd, wavHeader, sizeof(wavHeader));
3087 size_t total = 0;
3088 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003089#define TEE_SINK_READ 1024 // frames per I/O operation
3090 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003091 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003092 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003093 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003094 bool wasFirstRead = firstRead;
3095 firstRead = false;
3096 if (actual <= 0) {
3097 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3098 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003099 }
Eric Laurent81784c32012-11-19 14:55:58 -08003100 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003101 }
Eric Laurent81784c32012-11-19 14:55:58 -08003102 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003103 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003104 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003105 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003106 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003107 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003108 uint32_t temp = 44 + total * frameSize - 8;
3109 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003110 write(teeFd, &temp, sizeof(temp));
3111 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003112 temp = total * frameSize;
3113 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003114 write(teeFd, &temp, sizeof(temp));
3115 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003116 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003117 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003118 }
Eric Laurent59255e42011-07-27 19:49:51 -07003119 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003120 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003121 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003122 }
Eric Laurent59255e42011-07-27 19:49:51 -07003123 }
3124 }
3125}
Glenn Kasten46909e72013-02-26 09:20:22 -08003126#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003127
Mathias Agopian65ab4712010-07-14 17:59:35 -07003128// ----------------------------------------------------------------------------
3129
3130status_t AudioFlinger::onTransact(
3131 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3132{
3133 return BnAudioFlinger::onTransact(code, data, reply, flags);
3134}
3135
Glenn Kasten63238ef2015-03-02 15:50:29 -08003136} // namespace android