blob: 0e70ad978a6a81f4a8761b695e5db609713d78e5 [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,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 status_t *status)
551{
552 sp<PlaybackThread::Track> track;
553 sp<TrackHandle> trackHandle;
554 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700555 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -0800556 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700558 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
559 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
560 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
561 ALOGW_IF(pid != -1 && pid != callingPid,
562 "%s uid %d pid %d tried to pass itself off as pid %d",
563 __func__, callingUid, callingPid, pid);
564 pid = callingPid;
565 }
566
Glenn Kasten263709e2012-01-06 08:40:01 -0800567 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
568 // but if someone uses binder directly they could bypass that and cause us to crash
569 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000570 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571 lStatus = BAD_VALUE;
572 goto Exit;
573 }
574
Glenn Kasten53b5d092014-02-05 10:00:23 -0800575 // further sample rate checks are performed by createTrack_l() depending on the thread type
576 if (sampleRate == 0) {
577 ALOGE("createTrack() invalid sample rate %u", sampleRate);
578 lStatus = BAD_VALUE;
579 goto Exit;
580 }
581
582 // further channel mask checks are performed by createTrack_l() depending on the thread type
583 if (!audio_is_output_channel(channelMask)) {
584 ALOGE("createTrack() invalid channel mask %#x", channelMask);
585 lStatus = BAD_VALUE;
586 goto Exit;
587 }
588
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700589 // further format checks are performed by createTrack_l() depending on the thread type
590 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800591 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700592 lStatus = BAD_VALUE;
593 goto Exit;
594 }
595
Glenn Kasten663c2242013-09-24 11:52:37 -0700596 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
597 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
598 lStatus = BAD_VALUE;
599 goto Exit;
600 }
601
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 {
603 Mutex::Autolock _l(mLock);
604 PlaybackThread *thread = checkPlaybackThread_l(output);
605 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800606 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607 lStatus = BAD_VALUE;
608 goto Exit;
609 }
610
Eric Laurent021cf962014-05-13 10:18:14 -0700611 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612
Glenn Kastene848bd92014-03-13 15:00:32 -0700613 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700614 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800615 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
616 ALOGE("createTrack() invalid session ID %d", *sessionId);
617 lStatus = BAD_VALUE;
618 goto Exit;
619 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700620 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700621 // check if an effect chain with the same session ID is present on another
622 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700623 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700624 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
625 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700626 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent4c415062016-06-17 16:14:16 -0700627 if (sessions & ThreadBase::EFFECT_SESSION) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700628 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700629 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700630 }
Eric Laurentde070132010-07-13 04:45:46 -0700631 }
632 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700634 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800635 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636 if (sessionId != NULL) {
637 *sessionId = lSessionId;
638 }
639 }
Steve Block3856b092011-10-20 11:56:00 +0100640 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641
642 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800643 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800644 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700645 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700646
647 // move effect chain to this output thread if an effect on same session was waiting
648 // for a track to be created
649 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700650 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700651 Mutex::Autolock _dl(thread->mLock);
652 Mutex::Autolock _sl(effectThread->mLock);
653 moveEffectChain_l(lSessionId, effectThread, thread, true);
654 }
Eric Laurenta011e352012-03-29 15:51:43 -0700655
656 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700657 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700658 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
659 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700660 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700661 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700662 } else {
663 mPendingSyncEvents[i]->cancel();
664 }
Eric Laurenta011e352012-03-29 15:51:43 -0700665 mPendingSyncEvents.removeAt(i);
666 i--;
667 }
668 }
669 }
Glenn Kastene198c362013-08-13 09:13:36 -0700670
Glenn Kastend848eb42016-03-08 13:42:11 -0800671 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 }
Glenn Kastene198c362013-08-13 09:13:36 -0700673
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700674 if (lStatus != NO_ERROR) {
675 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700676 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700677 // Don't hold mClientLock when releasing the reference on the track as the
678 // destructor will acquire it.
679 {
680 Mutex::Autolock _cl(mClientLock);
681 client.clear();
682 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700684 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 }
686
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700687 // return handle to client
688 trackHandle = new TrackHandle(track);
689
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700691 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692 return trackHandle;
693}
694
Glenn Kasten2c073da2016-02-26 09:14:08 -0800695uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696{
697 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800698 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800700 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 return 0;
702 }
703 return thread->sampleRate();
704}
705
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800706audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707{
708 Mutex::Autolock _l(mLock);
709 PlaybackThread *thread = checkPlaybackThread_l(output);
710 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000711 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800712 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713 }
714 return thread->format();
715}
716
Glenn Kasten2c073da2016-02-26 09:14:08 -0800717size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700718{
719 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800720 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700721 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800722 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723 return 0;
724 }
Glenn Kasten58912562012-04-03 10:45:00 -0700725 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
726 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727 return thread->frameCount();
728}
729
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700730size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
731{
732 Mutex::Autolock _l(mLock);
733 ThreadBase *thread = checkThread_l(ioHandle);
734 if (thread == NULL) {
735 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
736 return 0;
737 }
738 return thread->frameCountHAL();
739}
740
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800741uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742{
743 Mutex::Autolock _l(mLock);
744 PlaybackThread *thread = checkPlaybackThread_l(output);
745 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800746 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747 return 0;
748 }
749 return thread->latency();
750}
751
752status_t AudioFlinger::setMasterVolume(float value)
753{
Eric Laurenta1884f92011-08-23 08:25:03 -0700754 status_t ret = initCheck();
755 if (ret != NO_ERROR) {
756 return ret;
757 }
758
Mathias Agopian65ab4712010-07-14 17:59:35 -0700759 // check calling permissions
760 if (!settingsAllowed()) {
761 return PERMISSION_DENIED;
762 }
763
Eric Laurenta4c5a552012-03-29 10:12:40 -0700764 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700765 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700766
John Grossmanee578c02012-07-23 17:05:46 -0700767 // Set master volume in the HALs which support it.
768 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
769 AutoMutex lock(mHardwareLock);
770 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800771
John Grossmanee578c02012-07-23 17:05:46 -0700772 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
773 if (dev->canSetMasterVolume()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700774 dev->hwDevice()->setMasterVolume(value);
Eric Laurent93575202011-01-18 18:39:02 -0800775 }
John Grossmanee578c02012-07-23 17:05:46 -0700776 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700778
John Grossmanee578c02012-07-23 17:05:46 -0700779 // Now set the master volume in each playback thread. Playback threads
780 // assigned to HALs which do not have master volume support will apply
781 // master volume during the mix operation. Threads with HALs which do
782 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700783 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
784 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
785 continue;
786 }
John Grossmanee578c02012-07-23 17:05:46 -0700787 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700788 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789
790 return NO_ERROR;
791}
792
Glenn Kastenf78aee72012-01-04 11:00:47 -0800793status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794{
Eric Laurenta1884f92011-08-23 08:25:03 -0700795 status_t ret = initCheck();
796 if (ret != NO_ERROR) {
797 return ret;
798 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700799
800 // check calling permissions
801 if (!settingsAllowed()) {
802 return PERMISSION_DENIED;
803 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800804 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000805 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700806 return BAD_VALUE;
807 }
808
809 { // scope for the lock
810 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700811 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700813 ret = dev->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 mHardwareStatus = AUDIO_HW_IDLE;
815 }
816
817 if (NO_ERROR == ret) {
818 Mutex::Autolock _l(mLock);
819 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800820 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700821 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700822 }
823
824 return ret;
825}
826
827status_t AudioFlinger::setMicMute(bool state)
828{
Eric Laurenta1884f92011-08-23 08:25:03 -0700829 status_t ret = initCheck();
830 if (ret != NO_ERROR) {
831 return ret;
832 }
833
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 // check calling permissions
835 if (!settingsAllowed()) {
836 return PERMISSION_DENIED;
837 }
838
839 AutoMutex lock(mHardwareLock);
840 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700841 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700842 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
843 status_t result = dev->setMicMute(state);
Eric Laurent2f035f52014-09-14 12:11:52 -0700844 if (result != NO_ERROR) {
845 ret = result;
846 }
847 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848 mHardwareStatus = AUDIO_HW_IDLE;
849 return ret;
850}
851
852bool AudioFlinger::getMicMute() const
853{
Eric Laurenta1884f92011-08-23 08:25:03 -0700854 status_t ret = initCheck();
855 if (ret != NO_ERROR) {
856 return false;
857 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800858 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700859 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800860 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800862 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700863 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
864 status_t result = dev->getMicMute(&state);
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800865 if (result == NO_ERROR) {
866 mute = mute && state;
867 }
868 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800870
871 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872}
873
874status_t AudioFlinger::setMasterMute(bool muted)
875{
John Grossmand8f178d2012-07-20 14:51:35 -0700876 status_t ret = initCheck();
877 if (ret != NO_ERROR) {
878 return ret;
879 }
880
Mathias Agopian65ab4712010-07-14 17:59:35 -0700881 // check calling permissions
882 if (!settingsAllowed()) {
883 return PERMISSION_DENIED;
884 }
885
John Grossmanee578c02012-07-23 17:05:46 -0700886 Mutex::Autolock _l(mLock);
887 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700888
John Grossmanee578c02012-07-23 17:05:46 -0700889 // Set master mute in the HALs which support it.
890 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
891 AutoMutex lock(mHardwareLock);
892 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700893
John Grossmanee578c02012-07-23 17:05:46 -0700894 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
895 if (dev->canSetMasterMute()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700896 dev->hwDevice()->setMasterMute(muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700897 }
John Grossmanee578c02012-07-23 17:05:46 -0700898 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700899 }
900
John Grossmanee578c02012-07-23 17:05:46 -0700901 // Now set the master mute in each playback thread. Playback threads
902 // assigned to HALs which do not have master mute support will apply master
903 // mute during the mix operation. Threads with HALs which do support master
904 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700905 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
906 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
907 continue;
908 }
John Grossmanee578c02012-07-23 17:05:46 -0700909 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700910 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911
912 return NO_ERROR;
913}
914
915float AudioFlinger::masterVolume() const
916{
Glenn Kasten98067102011-12-13 11:47:54 -0800917 Mutex::Autolock _l(mLock);
918 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919}
920
921bool AudioFlinger::masterMute() const
922{
Glenn Kasten98067102011-12-13 11:47:54 -0800923 Mutex::Autolock _l(mLock);
924 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925}
926
John Grossman4ff14ba2012-02-08 16:37:41 -0800927float AudioFlinger::masterVolume_l() const
928{
John Grossman4ff14ba2012-02-08 16:37:41 -0800929 return mMasterVolume;
930}
931
John Grossmand8f178d2012-07-20 14:51:35 -0700932bool AudioFlinger::masterMute_l() const
933{
John Grossmanee578c02012-07-23 17:05:46 -0700934 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700935}
936
Eric Laurent223fd5c2014-11-11 13:43:36 -0800937status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
938{
939 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
940 ALOGW("setStreamVolume() invalid stream %d", stream);
941 return BAD_VALUE;
942 }
943 pid_t caller = IPCThreadState::self()->getCallingPid();
944 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
945 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
946 return PERMISSION_DENIED;
947 }
948
949 return NO_ERROR;
950}
951
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800952status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
953 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954{
955 // check calling permissions
956 if (!settingsAllowed()) {
957 return PERMISSION_DENIED;
958 }
959
Eric Laurent223fd5c2014-11-11 13:43:36 -0800960 status_t status = checkStreamType(stream);
961 if (status != NO_ERROR) {
962 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800964 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965
966 AutoMutex lock(mLock);
967 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700968 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969 thread = checkPlaybackThread_l(output);
970 if (thread == NULL) {
971 return BAD_VALUE;
972 }
973 }
974
975 mStreamTypes[stream].volume = value;
976
977 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800978 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700979 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700980 }
981 } else {
982 thread->setStreamVolume(stream, value);
983 }
984
985 return NO_ERROR;
986}
987
Glenn Kastenfff6d712012-01-12 16:38:12 -0800988status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700989{
990 // check calling permissions
991 if (!settingsAllowed()) {
992 return PERMISSION_DENIED;
993 }
994
Eric Laurent223fd5c2014-11-11 13:43:36 -0800995 status_t status = checkStreamType(stream);
996 if (status != NO_ERROR) {
997 return status;
998 }
999 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1000
1001 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001002 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001003 return BAD_VALUE;
1004 }
1005
Eric Laurent93575202011-01-18 18:39:02 -08001006 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001007 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -07001008 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001009 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001010
1011 return NO_ERROR;
1012}
1013
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001014float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001016 status_t status = checkStreamType(stream);
1017 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001018 return 0.0f;
1019 }
1020
1021 AutoMutex lock(mLock);
1022 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001023 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001024 PlaybackThread *thread = checkPlaybackThread_l(output);
1025 if (thread == NULL) {
1026 return 0.0f;
1027 }
1028 volume = thread->streamVolume(stream);
1029 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001030 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001031 }
1032
1033 return volume;
1034}
1035
Glenn Kastenfff6d712012-01-12 16:38:12 -08001036bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001037{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001038 status_t status = checkStreamType(stream);
1039 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040 return true;
1041 }
1042
Glenn Kasten6637baa2012-01-09 09:40:36 -08001043 AutoMutex lock(mLock);
1044 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045}
1046
Eric Laurent054d9d32015-04-24 08:48:48 -07001047
1048void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1049{
1050 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1051 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1052 }
1053}
1054
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001055status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001056{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001057 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1058 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001059
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060 // check calling permissions
1061 if (!settingsAllowed()) {
1062 return PERMISSION_DENIED;
1063 }
1064
Glenn Kasten142f5192014-03-25 17:44:59 -07001065 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1066 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001067 Mutex::Autolock _l(mLock);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001068 // result will remain NO_INIT if no audio device is present
1069 status_t final_result = NO_INIT;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001070 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001071 AutoMutex lock(mHardwareLock);
1072 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1073 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001074 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1075 status_t result = dev->setParameters(keyValuePairs);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001076 // return success if at least one audio device accepts the parameters as not all
1077 // HALs are requested to support all parameters. If no audio device supports the
1078 // requested parameters, the last error is reported.
1079 if (final_result != NO_ERROR) {
1080 final_result = result;
1081 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001082 }
1083 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001084 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001085 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1086 AudioParameter param = AudioParameter(keyValuePairs);
1087 String8 value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001088 if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) {
1089 bool btNrecIsOff = (value == AudioParameter::valueOff);
Eric Laurentbee53372011-08-29 12:42:48 -07001090 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001091 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1092 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001093 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001094 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1095 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001096 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001097 // suspend effects associated with those session IDs
1098 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001099 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001100 thread->setEffectSuspended(FX_IID_AEC,
1101 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001102 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001103 thread->setEffectSuspended(FX_IID_NS,
1104 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001105 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001106 }
1107 }
Eric Laurentbee53372011-08-29 12:42:48 -07001108 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001109 }
1110 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001111 String8 screenState;
1112 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
Mikhail Naganov00260b52016-10-13 12:54:24 -07001113 bool isOff = (screenState == AudioParameter::valueOff);
Eric Laurent81784c32012-11-19 14:55:58 -08001114 if (isOff != (AudioFlinger::mScreenState & 1)) {
1115 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001116 }
1117 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001118 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001119 }
1120
1121 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1122 // and the thread is exited once the lock is released
1123 sp<ThreadBase> thread;
1124 {
1125 Mutex::Autolock _l(mLock);
1126 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001127 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001129 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001130 // indicate output device change to all input threads for pre processing
1131 AudioParameter param = AudioParameter(keyValuePairs);
1132 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001133 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1134 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001135 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001136 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137 }
1138 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001139 if (thread != 0) {
1140 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141 }
1142 return BAD_VALUE;
1143}
1144
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001145String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001147 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1148 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149
Eric Laurenta4c5a552012-03-29 10:12:40 -07001150 Mutex::Autolock _l(mLock);
1151
Glenn Kasten142f5192014-03-25 17:44:59 -07001152 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001153 String8 out_s8;
1154
Dima Zavin799a70e2011-04-18 16:57:27 -07001155 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001156 String8 s;
1157 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001158 {
1159 AutoMutex lock(mHardwareLock);
1160 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001161 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1162 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001163 mHardwareStatus = AUDIO_HW_IDLE;
1164 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001165 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001166 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001167 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001168 }
1169
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1171 if (playbackThread != NULL) {
1172 return playbackThread->getParameters(keys);
1173 }
1174 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1175 if (recordThread != NULL) {
1176 return recordThread->getParameters(keys);
1177 }
1178 return String8("");
1179}
1180
Glenn Kastendd8104c2012-07-02 12:42:44 -07001181size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1182 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001183{
Eric Laurenta1884f92011-08-23 08:25:03 -07001184 status_t ret = initCheck();
1185 if (ret != NO_ERROR) {
1186 return 0;
1187 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001188 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001189 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001190 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001191 return 0;
1192 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001193
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001194 AutoMutex lock(mHardwareLock);
1195 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001196 audio_config_t config, proposed;
1197 memset(&proposed, 0, sizeof(proposed));
1198 proposed.sample_rate = sampleRate;
1199 proposed.channel_mask = channelMask;
1200 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001201
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001202 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001203 size_t frames;
1204 for (;;) {
1205 // Note: config is currently a const parameter for get_input_buffer_size()
1206 // but we use a copy from proposed in case config changes from the call.
1207 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001208 status_t result = dev->getInputBufferSize(&config, &frames);
1209 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001210 break; // hal success, config is the result
1211 }
1212 // change one parameter of the configuration each iteration to a more "common" value
1213 // to see if the device will support it.
1214 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1215 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1216 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1217 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1218 } else {
1219 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1220 "format %#x, channelMask 0x%X",
1221 sampleRate, format, channelMask);
1222 break; // retries failed, break out of loop with frames == 0.
1223 }
1224 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001225 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001226 if (frames > 0 && config.sample_rate != sampleRate) {
1227 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1228 }
1229 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230}
1231
Glenn Kasten5f972c02014-01-13 09:59:31 -08001232uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001233{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001234 Mutex::Autolock _l(mLock);
1235
1236 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1237 if (recordThread != NULL) {
1238 return recordThread->getInputFramesLost();
1239 }
1240 return 0;
1241}
1242
1243status_t AudioFlinger::setVoiceVolume(float value)
1244{
Eric Laurenta1884f92011-08-23 08:25:03 -07001245 status_t ret = initCheck();
1246 if (ret != NO_ERROR) {
1247 return ret;
1248 }
1249
Mathias Agopian65ab4712010-07-14 17:59:35 -07001250 // check calling permissions
1251 if (!settingsAllowed()) {
1252 return PERMISSION_DENIED;
1253 }
1254
1255 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001256 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001257 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001258 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 mHardwareStatus = AUDIO_HW_IDLE;
1260
1261 return ret;
1262}
1263
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001264status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001265 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001266{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001267 Mutex::Autolock _l(mLock);
1268
1269 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1270 if (playbackThread != NULL) {
1271 return playbackThread->getRenderPosition(halFrames, dspFrames);
1272 }
1273
1274 return BAD_VALUE;
1275}
1276
1277void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1278{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001279 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001280 if (client == 0) {
1281 return;
1282 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001283 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001284 {
1285 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001286 if (mNotificationClients.indexOfKey(pid) < 0) {
1287 sp<NotificationClient> notificationClient = new NotificationClient(this,
1288 client,
1289 pid);
1290 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001291
Eric Laurent021cf962014-05-13 10:18:14 -07001292 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001293
Marco Nelissen06b46062014-11-14 07:58:25 -08001294 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001295 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001296 }
1297 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001298
Eric Laurent021cf962014-05-13 10:18:14 -07001299 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001300 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001301 // the config change is always sent from playback or record threads to avoid deadlock
1302 // with AudioSystem::gLock
1303 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1304 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1305 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001306
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001307 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1308 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001309 }
1310}
1311
1312void AudioFlinger::removeNotificationClient(pid_t pid)
1313{
1314 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001315 {
1316 Mutex::Autolock _cl(mClientLock);
1317 mNotificationClients.removeItem(pid);
1318 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001319
Steve Block3856b092011-10-20 11:56:00 +01001320 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001321 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001322 bool removed = false;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001323 for (size_t i = 0; i < num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001324 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001325 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001326 if (ref->mPid == pid) {
1327 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001328 mAudioSessionRefs.removeAt(i);
1329 delete ref;
1330 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001331 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001332 } else {
1333 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001334 }
1335 }
1336 if (removed) {
1337 purgeStaleEffects_l();
1338 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001339}
1340
Eric Laurent73e26b62015-04-27 16:55:58 -07001341void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001342 const sp<AudioIoDescriptor>& ioDesc,
1343 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001344{
Eric Laurent021cf962014-05-13 10:18:14 -07001345 Mutex::Autolock _l(mClientLock);
1346 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001347 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001348 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1349 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1350 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001351 }
1352}
1353
Eric Laurent021cf962014-05-13 10:18:14 -07001354// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355void AudioFlinger::removeClient_l(pid_t pid)
1356{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001357 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001358 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001359 mClients.removeItem(pid);
1360}
1361
Eric Laurent717e1282012-06-29 16:36:52 -07001362// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001363sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1364 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001365{
1366 sp<PlaybackThread> thread;
1367
1368 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1369 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1370 ALOG_ASSERT(thread == 0);
1371 thread = mPlaybackThreads.valueAt(i);
1372 }
1373 }
1374
1375 return thread;
1376}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001377
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378
Mathias Agopian65ab4712010-07-14 17:59:35 -07001379
1380// ----------------------------------------------------------------------------
1381
1382AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1383 : RefBase(),
1384 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001385 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301387 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1388 heapSize *= 1024;
1389 if (!heapSize) {
1390 heapSize = kClientSharedHeapSizeBytes;
1391 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1392 // invalidated tracks
1393 if (!audioFlinger->isLowRamDevice()) {
1394 heapSize *= kClientSharedHeapSizeMultiplier;
1395 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001396 }
1397 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001398}
1399
Eric Laurent021cf962014-05-13 10:18:14 -07001400// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401AudioFlinger::Client::~Client()
1402{
1403 mAudioFlinger->removeClient_l(mPid);
1404}
1405
Glenn Kasten435dbe62012-01-30 10:15:48 -08001406sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407{
1408 return mMemoryDealer;
1409}
1410
1411// ----------------------------------------------------------------------------
1412
1413AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1414 const sp<IAudioFlingerClient>& client,
1415 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001416 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001417{
1418}
1419
1420AudioFlinger::NotificationClient::~NotificationClient()
1421{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001422}
1423
Glenn Kasten0f11b512014-01-31 16:18:54 -08001424void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001425{
1426 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001427 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428}
1429
Mathias Agopian65ab4712010-07-14 17:59:35 -07001430
1431// ----------------------------------------------------------------------------
1432
1433sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001434 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001436 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001437 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001438 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001439 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -07001440 audio_input_flags_t *flags,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001441 pid_t pid,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001442 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001443 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001444 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001445 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001446 sp<IMemory>& cblk,
1447 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001448 status_t *status)
1449{
1450 sp<RecordThread::RecordTrack> recordTrack;
1451 sp<RecordHandle> recordHandle;
1452 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001454 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001455
Glenn Kastend776ac62014-05-07 09:16:09 -07001456 cblk.clear();
1457 buffers.clear();
1458
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001459 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001460 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1461 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001462 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001463 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1464 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001465 updatePid = true;
1466 }
1467
1468 if (updatePid) {
1469 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
1470 ALOGW_IF(pid != -1 && pid != callingPid,
1471 "%s uid %d pid %d tried to pass itself off as pid %d",
1472 __func__, callingUid, callingPid, pid);
1473 pid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001474 }
1475
Mathias Agopian65ab4712010-07-14 17:59:35 -07001476 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001477 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001478 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001479 lStatus = PERMISSION_DENIED;
1480 goto Exit;
1481 }
1482
Glenn Kasten53b5d092014-02-05 10:00:23 -08001483 // further sample rate checks are performed by createRecordTrack_l()
1484 if (sampleRate == 0) {
1485 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1486 lStatus = BAD_VALUE;
1487 goto Exit;
1488 }
1489
Andy Hung6770c6f2015-04-07 13:43:36 -07001490 // we don't yet support anything other than linear PCM
1491 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001492 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001493 lStatus = BAD_VALUE;
1494 goto Exit;
1495 }
1496
Glenn Kasten53b5d092014-02-05 10:00:23 -08001497 // further channel mask checks are performed by createRecordTrack_l()
1498 if (!audio_is_input_channel(channelMask)) {
1499 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1500 lStatus = BAD_VALUE;
1501 goto Exit;
1502 }
1503
Glenn Kasten05997e22014-03-13 15:08:33 -07001504 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001505 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001506 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001507 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001508 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001509 lStatus = BAD_VALUE;
1510 goto Exit;
1511 }
1512
Eric Laurent021cf962014-05-13 10:18:14 -07001513 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001514
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001515 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001516 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1517 lStatus = BAD_VALUE;
1518 goto Exit;
1519 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520 lSessionId = *sessionId;
1521 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001522 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001523 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001524 if (sessionId != NULL) {
1525 *sessionId = lSessionId;
1526 }
1527 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001528 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001529
Glenn Kasten1879fff2012-07-11 15:36:59 -07001530 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001531 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001532 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001533 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001534
1535 if (lStatus == NO_ERROR) {
1536 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1537 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001538 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001539 if (chain != 0) {
1540 Mutex::Autolock _l(thread->mLock);
1541 thread->addEffectChain_l(chain);
1542 }
1543 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001544 }
Glenn Kastene198c362013-08-13 09:13:36 -07001545
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001546 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001547 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001548 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001549 // Don't hold mClientLock when releasing the reference on the track as the
1550 // destructor will acquire it.
1551 {
1552 Mutex::Autolock _cl(mClientLock);
1553 client.clear();
1554 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001555 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556 goto Exit;
1557 }
1558
Glenn Kastend776ac62014-05-07 09:16:09 -07001559 cblk = recordTrack->getCblk();
1560 buffers = recordTrack->getBuffers();
1561
Glenn Kasten2fc14732013-08-05 14:58:14 -07001562 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001564
1565Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001566 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001567 return recordHandle;
1568}
1569
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001570
1571
Mathias Agopian65ab4712010-07-14 17:59:35 -07001572// ----------------------------------------------------------------------------
1573
Eric Laurenta4c5a552012-03-29 10:12:40 -07001574audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1575{
Eric Laurent44622db2014-08-01 19:00:33 -07001576 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001577 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001578 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001579 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001580 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001581 }
1582 Mutex::Autolock _l(mLock);
1583 return loadHwModule_l(name);
1584}
1585
1586// loadHwModule_l() must be called with AudioFlinger::mLock held
1587audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1588{
1589 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1590 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1591 ALOGW("loadHwModule() module %s already loaded", name);
1592 return mAudioHwDevs.keyAt(i);
1593 }
1594 }
1595
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001596 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001597
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001598 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001599 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001600 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001601 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001602 }
1603
1604 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001605 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07001606 mHardwareStatus = AUDIO_HW_IDLE;
1607 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001608 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001609 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001610 }
1611
John Grossmanee578c02012-07-23 17:05:46 -07001612 // Check and cache this HAL's level of support for master mute and master
1613 // volume. If this is the first HAL opened, and it supports the get
1614 // methods, use the initial values provided by the HAL as the current
1615 // master mute and volume settings.
1616
1617 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1618 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001619 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001620
1621 if (0 == mAudioHwDevs.size()) {
1622 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001623 float mv;
1624 if (OK == dev->getMasterVolume(&mv)) {
1625 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07001626 }
1627
1628 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001629 bool mm;
1630 if (OK == dev->getMasterMute(&mm)) {
1631 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07001632 }
1633 }
1634
Eric Laurenta4c5a552012-03-29 10:12:40 -07001635 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001636 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07001637 flags = static_cast<AudioHwDevice::Flags>(flags |
1638 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1639 }
1640
1641 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001642 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07001643 flags = static_cast<AudioHwDevice::Flags>(flags |
1644 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1645 }
1646
Eric Laurenta4c5a552012-03-29 10:12:40 -07001647 mHardwareStatus = AUDIO_HW_IDLE;
1648 }
1649
Glenn Kastena13cde92016-03-28 15:26:02 -07001650 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001651 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001652
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001653 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001654
1655 return handle;
1656
1657}
1658
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001659// ----------------------------------------------------------------------------
1660
Glenn Kasten3b16c762012-11-14 08:44:39 -08001661uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001662{
1663 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001664 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001665 return thread != NULL ? thread->sampleRate() : 0;
1666}
1667
Glenn Kastene33054e2012-11-14 12:54:39 -08001668size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001669{
1670 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001671 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001672 return thread != NULL ? thread->frameCountHAL() : 0;
1673}
1674
1675// ----------------------------------------------------------------------------
1676
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001677status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1678{
1679 uid_t uid = IPCThreadState::self()->getCallingUid();
1680 if (uid != AID_SYSTEM) {
1681 return PERMISSION_DENIED;
1682 }
1683 Mutex::Autolock _l(mLock);
1684 if (mIsDeviceTypeKnown) {
1685 return INVALID_OPERATION;
1686 }
1687 mIsLowRamDevice = isLowRamDevice;
1688 mIsDeviceTypeKnown = true;
1689 return NO_ERROR;
1690}
1691
Eric Laurent93c3d412014-08-01 14:48:35 -07001692audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1693{
1694 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001695
1696 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1697 if (index >= 0) {
1698 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1699 mHwAvSyncIds.valueAt(index), sessionId);
1700 return mHwAvSyncIds.valueAt(index);
1701 }
1702
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001703 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07001704 if (dev == NULL) {
1705 return AUDIO_HW_SYNC_INVALID;
1706 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001707 String8 reply;
1708 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001709 if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001710 param = AudioParameter(reply);
1711 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001712
1713 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001714 if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001715 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1716 return AUDIO_HW_SYNC_INVALID;
1717 }
1718
1719 // allow only one session for a given HW A/V sync ID.
1720 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1721 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1722 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1723 value, mHwAvSyncIds.keyAt(i));
1724 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001725 break;
1726 }
1727 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001728
1729 mHwAvSyncIds.add(sessionId, value);
1730
1731 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1732 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1733 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07001734 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001735 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001736 param.addInt(String8(AudioParameter::keyStreamHwAvSync), value);
Eric Laurentfa90e842014-10-17 18:12:31 -07001737 thread->setParameters(param.toString());
1738 break;
1739 }
1740 }
1741
1742 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1743 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001744}
1745
Eric Laurent72e3f392015-05-20 14:43:50 -07001746status_t AudioFlinger::systemReady()
1747{
1748 Mutex::Autolock _l(mLock);
1749 ALOGI("%s", __FUNCTION__);
1750 if (mSystemReady) {
1751 ALOGW("%s called twice", __FUNCTION__);
1752 return NO_ERROR;
1753 }
1754 mSystemReady = true;
1755 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1756 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1757 thread->systemReady();
1758 }
1759 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1760 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1761 thread->systemReady();
1762 }
1763 return NO_ERROR;
1764}
1765
Eric Laurentfa90e842014-10-17 18:12:31 -07001766// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1767void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1768{
1769 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1770 if (index >= 0) {
1771 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1772 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1773 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001774 param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId);
Eric Laurentfa90e842014-10-17 18:12:31 -07001775 thread->setParameters(param.toString());
1776 }
1777}
1778
1779
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001780// ----------------------------------------------------------------------------
1781
Eric Laurent83b88082014-06-20 18:31:16 -07001782
1783sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001784 audio_io_handle_t *output,
1785 audio_config_t *config,
1786 audio_devices_t devices,
1787 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001788 audio_output_flags_t flags)
1789{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001790 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001791 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001792 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001793 }
1794
Eric Laurentcf2c0212014-07-25 16:20:43 -07001795 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001796 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1797 } else {
1798 // Audio Policy does not currently request a specific output handle.
1799 // If this is ever needed, see openInput_l() for example code.
1800 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1801 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001802 }
Eric Laurent83b88082014-06-20 18:31:16 -07001803
1804 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1805
Eric Laurent83b88082014-06-20 18:31:16 -07001806 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001807 // This if statement allows overriding the audio policy settings
1808 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1809 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1810 // Check only for Normal Mixing mode
1811 if (kEnableExtendedPrecision) {
1812 // Specify format (uncomment one below to choose)
1813 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1814 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1815 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1816 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001817 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001818 }
1819 if (kEnableExtendedChannels) {
1820 // Specify channel mask (uncomment one below to choose)
1821 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1822 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1823 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1824 }
Eric Laurent83b88082014-06-20 18:31:16 -07001825 }
1826
Phil Burk062e67a2015-02-11 13:40:50 -08001827 AudioStreamOut *outputStream = NULL;
1828 status_t status = outHwDev->openOutputStream(
1829 &outputStream,
1830 *output,
1831 devices,
1832 flags,
1833 config,
1834 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001835
1836 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001837
Phil Burk062e67a2015-02-11 13:40:50 -08001838 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001839
1840 PlaybackThread *thread;
1841 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurente93cc032016-05-05 10:15:10 -07001842 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001843 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001844 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1845 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001846 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001847 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001848 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001849 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001850 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001851 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001852 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001853 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001854 return thread;
1855 }
1856
1857 return 0;
1858}
1859
Eric Laurentcf2c0212014-07-25 16:20:43 -07001860status_t AudioFlinger::openOutput(audio_module_handle_t module,
1861 audio_io_handle_t *output,
1862 audio_config_t *config,
1863 audio_devices_t *devices,
1864 const String8& address,
1865 uint32_t *latencyMs,
1866 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001867{
Phil Burk062e67a2015-02-11 13:40:50 -08001868 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001869 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001870 (devices != NULL) ? *devices : 0,
1871 config->sample_rate,
1872 config->format,
1873 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001874 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001875
Eric Laurentcf2c0212014-07-25 16:20:43 -07001876 if (*devices == AUDIO_DEVICE_NONE) {
1877 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001879
Mathias Agopian65ab4712010-07-14 17:59:35 -07001880 Mutex::Autolock _l(mLock);
1881
Eric Laurentcf2c0212014-07-25 16:20:43 -07001882 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001883 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001884 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885
1886 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001887 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001888
1889 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001890 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001891 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001892 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001893
1894 AutoMutex lock(mHardwareLock);
1895 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001896 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001897 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001898 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001899 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900 }
1901
Eric Laurentcf2c0212014-07-25 16:20:43 -07001902 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001903}
1904
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001905audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1906 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001907{
1908 Mutex::Autolock _l(mLock);
1909 MixerThread *thread1 = checkMixerThread_l(output1);
1910 MixerThread *thread2 = checkMixerThread_l(output2);
1911
1912 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001913 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1914 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001915 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001916 }
1917
Glenn Kasteneeecb982016-02-26 10:44:04 -08001918 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07001919 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001920 thread->addOutputTrack(thread2);
1921 mPlaybackThreads.add(id, thread);
1922 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001923 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001924 return id;
1925}
1926
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001927status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928{
Glenn Kastend96c5722012-04-25 13:44:49 -07001929 return closeOutput_nonvirtual(output);
1930}
1931
1932status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1933{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001934 // keep strong reference on the playback thread so that
1935 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001936 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937 {
1938 Mutex::Autolock _l(mLock);
1939 thread = checkPlaybackThread_l(output);
1940 if (thread == NULL) {
1941 return BAD_VALUE;
1942 }
1943
Steve Block3856b092011-10-20 11:56:00 +01001944 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001945
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001946 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001947 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001948 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001949 DuplicatingThread *dupThread =
1950 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001951 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001952 }
1953 }
1954 }
1955
1956
1957 mPlaybackThreads.removeItem(output);
1958 // save all effects to the default thread
1959 if (mPlaybackThreads.size()) {
1960 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1961 if (dstThread != NULL) {
1962 // audioflinger lock is held here so the acquisition order of thread locks does not
1963 // matter
1964 Mutex::Autolock _dl(dstThread->mLock);
1965 Mutex::Autolock _sl(thread->mLock);
1966 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1967 for (size_t i = 0; i < effectChains.size(); i ++) {
1968 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001969 }
1970 }
1971 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001972 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1973 ioDesc->mIoHandle = output;
1974 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001975 }
1976 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001977 // The thread entity (active unit of execution) is no longer running here,
1978 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979
Eric Laurentf6870ae2015-05-08 10:50:03 -07001980 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001981 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001982 }
Eric Laurent83b88082014-06-20 18:31:16 -07001983
Mathias Agopian65ab4712010-07-14 17:59:35 -07001984 return NO_ERROR;
1985}
1986
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001987void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07001988{
1989 AudioStreamOut *out = thread->clearOutput();
1990 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1991 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07001992 delete out;
1993}
1994
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001995void AudioFlinger::closeOutputInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07001996{
1997 mPlaybackThreads.removeItem(thread->mId);
1998 thread->exit();
1999 closeOutputFinish(thread);
2000}
2001
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002002status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002003{
2004 Mutex::Autolock _l(mLock);
2005 PlaybackThread *thread = checkPlaybackThread_l(output);
2006
2007 if (thread == NULL) {
2008 return BAD_VALUE;
2009 }
2010
Steve Block3856b092011-10-20 11:56:00 +01002011 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002012 thread->suspend();
2013
2014 return NO_ERROR;
2015}
2016
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002017status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002018{
2019 Mutex::Autolock _l(mLock);
2020 PlaybackThread *thread = checkPlaybackThread_l(output);
2021
2022 if (thread == NULL) {
2023 return BAD_VALUE;
2024 }
2025
Steve Block3856b092011-10-20 11:56:00 +01002026 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002027
2028 thread->restore();
2029
2030 return NO_ERROR;
2031}
2032
Eric Laurentcf2c0212014-07-25 16:20:43 -07002033status_t AudioFlinger::openInput(audio_module_handle_t module,
2034 audio_io_handle_t *input,
2035 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002036 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002037 const String8& address,
2038 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002039 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002040{
Eric Laurent83b88082014-06-20 18:31:16 -07002041 Mutex::Autolock _l(mLock);
2042
Glenn Kastene7d66712015-03-05 13:46:52 -08002043 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002044 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002045 }
2046
Glenn Kastene7d66712015-03-05 13:46:52 -08002047 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002048
2049 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002050 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002051 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002052 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002053 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002054 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002055}
Dima Zavin799a70e2011-04-18 16:57:27 -07002056
Eric Laurent83b88082014-06-20 18:31:16 -07002057sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002058 audio_io_handle_t *input,
2059 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002060 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002061 const String8& address,
2062 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002063 audio_input_flags_t flags)
2064{
Glenn Kastene7d66712015-03-05 13:46:52 -08002065 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002066 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002067 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002068 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002069 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002070
Glenn Kasteneeecb982016-02-26 10:44:04 -08002071 // Audio Policy can request a specific handle for hardware hotword.
2072 // The goal here is not to re-open an already opened input.
2073 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002074 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002075 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2076 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2077 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2078 return 0;
2079 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2080 // This should not happen in a transient state with current design.
2081 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2082 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002083 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002084
Eric Laurentcf2c0212014-07-25 16:20:43 -07002085 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002086 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002087 sp<StreamInHalInterface> inStream;
2088 status_t status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002089 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Mikhail Naganovf558e022016-11-14 17:45:17 -08002090 ALOGV("openInput_l() openInputStream returned input %p, devices %x, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002091 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002092 inStream.get(),
Mikhail Naganovf558e022016-11-14 17:45:17 -08002093 devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002094 halconfig.sample_rate,
2095 halconfig.format,
2096 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002097 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002098 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002099
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002100 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002101 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002102 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002103 audio_is_linear_pcm(config->format) &&
2104 audio_is_linear_pcm(halconfig.format) &&
2105 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002106 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2107 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002108 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002109 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002110 inStream.clear();
2111 status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002112 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002113 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002114 }
2115
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002116 if (status == NO_ERROR && inStream != 0) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002117
Glenn Kasten46909e72013-02-26 09:20:22 -08002118#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002119 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2120 // or (re-)create if current Pipe is idle and does not match the new format
2121 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002122 enum {
2123 TEE_SINK_NO, // don't copy input
2124 TEE_SINK_NEW, // copy input using a new pipe
2125 TEE_SINK_OLD, // copy input using an existing pipe
2126 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002127 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2128 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002129 if (!mTeeSinkInputEnabled) {
2130 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002131 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002132 kind = TEE_SINK_NO;
2133 } else if (mRecordTeeSink == 0) {
2134 kind = TEE_SINK_NEW;
2135 } else if (mRecordTeeSink->getStrongCount() != 1) {
2136 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002137 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002138 kind = TEE_SINK_OLD;
2139 } else {
2140 kind = TEE_SINK_NEW;
2141 }
2142 switch (kind) {
2143 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002144 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002145 size_t numCounterOffers = 0;
2146 const NBAIO_Format offers[1] = {format};
2147 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2148 ALOG_ASSERT(index == 0);
2149 PipeReader *pipeReader = new PipeReader(*pipe);
2150 numCounterOffers = 0;
2151 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2152 ALOG_ASSERT(index == 0);
2153 mRecordTeeSink = pipe;
2154 mRecordTeeSource = pipeReader;
2155 teeSink = pipe;
2156 }
2157 break;
2158 case TEE_SINK_OLD:
2159 teeSink = mRecordTeeSink;
2160 break;
2161 case TEE_SINK_NO:
2162 default:
2163 break;
2164 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002165#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002166
Eric Laurent05067782016-06-01 18:27:28 -07002167 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07002168
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002169 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002170 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002171 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002172 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002173 inputStream,
2174 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002175 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002176 devices,
2177 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002178#ifdef TEE_SINK
2179 , teeSink
2180#endif
2181 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002182 mRecordThreads.add(*input, thread);
2183 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002184 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002185 }
2186
Eric Laurentcf2c0212014-07-25 16:20:43 -07002187 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002188 return 0;
2189}
2190
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002191status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192{
Glenn Kastend96c5722012-04-25 13:44:49 -07002193 return closeInput_nonvirtual(input);
2194}
2195
2196status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2197{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002198 // keep strong reference on the record thread so that
2199 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002200 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002201 {
2202 Mutex::Autolock _l(mLock);
2203 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002204 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002205 return BAD_VALUE;
2206 }
2207
Steve Block3856b092011-10-20 11:56:00 +01002208 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002209
2210 // If we still have effect chains, it means that a client still holds a handle
2211 // on at least one effect. We must either move the chain to an existing thread with the
2212 // same session ID or put it aside in case a new record thread is opened for a
2213 // new capture on the same session
2214 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002215 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002216 Mutex::Autolock _sl(thread->mLock);
2217 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002218 // Note: maximum one chain per record thread
2219 if (effectChains.size() != 0) {
2220 chain = effectChains[0];
2221 }
2222 }
2223 if (chain != 0) {
2224 // first check if a record thread is already opened with a client on the same session.
2225 // This should only happen in case of overlap between one thread tear down and the
2226 // creation of its replacement
2227 size_t i;
2228 for (i = 0; i < mRecordThreads.size(); i++) {
2229 sp<RecordThread> t = mRecordThreads.valueAt(i);
2230 if (t == thread) {
2231 continue;
2232 }
2233 if (t->hasAudioSession(chain->sessionId()) != 0) {
2234 Mutex::Autolock _l(t->mLock);
2235 ALOGV("closeInput() found thread %d for effect session %d",
2236 t->id(), chain->sessionId());
2237 t->addEffectChain_l(chain);
2238 break;
2239 }
2240 }
2241 // put the chain aside if we could not find a record thread with the same session id.
2242 if (i == mRecordThreads.size()) {
2243 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002244 }
2245 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002246 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2247 ioDesc->mIoHandle = input;
2248 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002249 mRecordThreads.removeItem(input);
2250 }
Eric Laurent83b88082014-06-20 18:31:16 -07002251 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2252 // we have a different lock for notification client
2253 closeInputFinish(thread);
2254 return NO_ERROR;
2255}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002256
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002257void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002258{
2259 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002260 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002261 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002262 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002263 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002264}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002265
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002266void AudioFlinger::closeInputInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002267{
2268 mRecordThreads.removeItem(thread->mId);
2269 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002270}
2271
Glenn Kastend2304db2014-02-03 07:40:31 -08002272status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002273{
2274 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002275 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002276
2277 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2278 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002279 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002280 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002281
2282 return NO_ERROR;
2283}
2284
2285
Glenn Kasteneeecb982016-02-26 10:44:04 -08002286audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002287{
Glenn Kasten9d003132016-04-06 14:38:09 -07002288 // This is a binder API, so a malicious client could pass in a bad parameter.
2289 // Check for that before calling the internal API nextUniqueId().
2290 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2291 ALOGE("newAudioUniqueId invalid use %d", use);
2292 return AUDIO_UNIQUE_ID_ALLOCATE;
2293 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002294 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002295}
2296
Glenn Kastend848eb42016-03-08 13:42:11 -08002297void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002298{
2299 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002300 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002301 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2302 if (pid != -1 && (caller == getpid_cached)) {
2303 caller = pid;
2304 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002305
Eric Laurent021cf962014-05-13 10:18:14 -07002306 {
2307 Mutex::Autolock _cl(mClientLock);
2308 // Ignore requests received from processes not known as notification client. The request
2309 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2310 // called from a different pid leaving a stale session reference. Also we don't know how
2311 // to clear this reference if the client process dies.
2312 if (mNotificationClients.indexOfKey(caller) < 0) {
2313 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2314 return;
2315 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002316 }
2317
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002318 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002319 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002320 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002321 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2322 ref->mCnt++;
2323 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002324 return;
2325 }
2326 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002327 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2328 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002329}
2330
Glenn Kastend848eb42016-03-08 13:42:11 -08002331void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002332{
2333 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002334 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002335 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2336 if (pid != -1 && (caller == getpid_cached)) {
2337 caller = pid;
2338 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002339 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002340 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002341 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002342 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2343 ref->mCnt--;
2344 ALOGV(" decremented refcount to %d", ref->mCnt);
2345 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002346 mAudioSessionRefs.removeAt(i);
2347 delete ref;
2348 purgeStaleEffects_l();
2349 }
2350 return;
2351 }
2352 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002353 // If the caller is mediaserver it is likely that the session being released was acquired
2354 // on behalf of a process not in notification clients and we ignore the warning.
2355 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002356}
2357
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002358bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
2359{
2360 size_t num = mAudioSessionRefs.size();
2361 for (size_t i = 0; i < num; i++) {
2362 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2363 if (ref->mSessionid == audioSession) {
2364 return true;
2365 }
2366 }
2367 return false;
2368}
2369
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002370void AudioFlinger::purgeStaleEffects_l() {
2371
Steve Block3856b092011-10-20 11:56:00 +01002372 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002373
2374 Vector< sp<EffectChain> > chains;
2375
2376 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2377 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2378 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2379 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002380 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2381 chains.push(ec);
2382 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002383 }
2384 }
2385 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2386 sp<RecordThread> t = mRecordThreads.valueAt(i);
2387 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2388 sp<EffectChain> ec = t->mEffectChains[j];
2389 chains.push(ec);
2390 }
2391 }
2392
2393 for (size_t i = 0; i < chains.size(); i++) {
2394 sp<EffectChain> ec = chains[i];
2395 int sessionid = ec->sessionId();
2396 sp<ThreadBase> t = ec->mThread.promote();
2397 if (t == 0) {
2398 continue;
2399 }
2400 size_t numsessionrefs = mAudioSessionRefs.size();
2401 bool found = false;
2402 for (size_t k = 0; k < numsessionrefs; k++) {
2403 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002404 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002405 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002406 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002407 found = true;
2408 break;
2409 }
2410 }
2411 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002412 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002413 // remove all effects from the chain
2414 while (ec->mEffects.size()) {
2415 sp<EffectModule> effect = ec->mEffects[0];
2416 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002417 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002418 if (effect->purgeHandles()) {
2419 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002420 }
2421 AudioSystem::unregisterEffect(effect->id());
2422 }
2423 }
2424 }
2425 return;
2426}
2427
Glenn Kasteneeecb982016-02-26 10:44:04 -08002428// checkThread_l() must be called with AudioFlinger::mLock held
2429AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2430{
2431 ThreadBase *thread = NULL;
2432 switch (audio_unique_id_get_use(ioHandle)) {
2433 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2434 thread = checkPlaybackThread_l(ioHandle);
2435 break;
2436 case AUDIO_UNIQUE_ID_USE_INPUT:
2437 thread = checkRecordThread_l(ioHandle);
2438 break;
2439 default:
2440 break;
2441 }
2442 return thread;
2443}
2444
Mathias Agopian65ab4712010-07-14 17:59:35 -07002445// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002446AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002447{
Glenn Kastena1117922012-01-26 10:53:32 -08002448 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002449}
2450
2451// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002452AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002453{
2454 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002455 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002456}
2457
2458// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002459AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002460{
Glenn Kastena1117922012-01-26 10:53:32 -08002461 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002462}
2463
Glenn Kasteneeecb982016-02-26 10:44:04 -08002464audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002465{
Glenn Kasten9d003132016-04-06 14:38:09 -07002466 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002467 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002468 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2469 for (int retry = 0; retry < maxRetries; retry++) {
2470 // The cast allows wraparound from max positive to min negative instead of abort
2471 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2472 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2473 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2474 // allow wrap by skipping 0 and -1 for session ids
2475 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2476 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2477 return (audio_unique_id_t) (base | use);
2478 }
2479 }
2480 // We have no way of recovering from wraparound
2481 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2482 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002483}
2484
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002485AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002486{
2487 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2488 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002489 if(thread->isDuplicating()) {
2490 continue;
2491 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002492 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002493 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002494 return thread;
2495 }
2496 }
2497 return NULL;
2498}
2499
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002500audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002501{
2502 PlaybackThread *thread = primaryPlaybackThread_l();
2503
2504 if (thread == NULL) {
2505 return 0;
2506 }
2507
Eric Laurentf1c04f92012-08-28 14:26:53 -07002508 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002509}
2510
Glenn Kastena7335632016-06-09 17:09:53 -07002511AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
2512{
2513 size_t minFrameCount = 0;
2514 PlaybackThread *minThread = NULL;
2515 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2516 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2517 if (!thread->isDuplicating()) {
2518 size_t frameCount = thread->frameCountHAL();
2519 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
2520 (frameCount == minFrameCount && thread->hasFastMixer() &&
2521 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
2522 minFrameCount = frameCount;
2523 minThread = thread;
2524 }
2525 }
2526 }
2527 return minThread;
2528}
2529
Eric Laurenta011e352012-03-29 15:51:43 -07002530sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002531 audio_session_t triggerSession,
2532 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002533 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002534 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002535{
2536 Mutex::Autolock _l(mLock);
2537
2538 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2539 status_t playStatus = NAME_NOT_FOUND;
2540 status_t recStatus = NAME_NOT_FOUND;
2541 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2542 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2543 if (playStatus == NO_ERROR) {
2544 return event;
2545 }
2546 }
2547 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2548 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2549 if (recStatus == NO_ERROR) {
2550 return event;
2551 }
2552 }
2553 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2554 mPendingSyncEvents.add(event);
2555 } else {
2556 ALOGV("createSyncEvent() invalid event %d", event->type());
2557 event.clear();
2558 }
2559 return event;
2560}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002561
Mathias Agopian65ab4712010-07-14 17:59:35 -07002562// ----------------------------------------------------------------------------
2563// Effect management
2564// ----------------------------------------------------------------------------
2565
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002566sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
2567 return mEffectsFactoryHal;
2568}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002569
Glenn Kastenf587ba52012-01-26 16:25:10 -08002570status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002571{
2572 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002573 if (mEffectsFactoryHal.get()) {
2574 return mEffectsFactoryHal->queryNumberEffects(numEffects);
2575 } else {
2576 return -ENODEV;
2577 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002578}
2579
Glenn Kastenf587ba52012-01-26 16:25:10 -08002580status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002581{
2582 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002583 if (mEffectsFactoryHal.get()) {
2584 return mEffectsFactoryHal->getDescriptor(index, descriptor);
2585 } else {
2586 return -ENODEV;
2587 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002588}
2589
Glenn Kasten5e92a782012-01-30 07:40:52 -08002590status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002591 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002592{
2593 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002594 if (mEffectsFactoryHal.get()) {
2595 return mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
2596 } else {
2597 return -ENODEV;
2598 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002599}
2600
2601
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002602sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002603 effect_descriptor_t *pDesc,
2604 const sp<IEffectClient>& effectClient,
2605 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002606 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002607 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002608 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002609 status_t *status,
2610 int *id,
2611 int *enabled)
2612{
2613 status_t lStatus = NO_ERROR;
2614 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002615 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002616
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002617 pid_t pid = IPCThreadState::self()->getCallingPid();
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002618 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
2619 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002620
2621 if (pDesc == NULL) {
2622 lStatus = BAD_VALUE;
2623 goto Exit;
2624 }
2625
Eric Laurent84e9a102010-09-23 16:10:16 -07002626 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002627 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002628 lStatus = PERMISSION_DENIED;
2629 goto Exit;
2630 }
2631
Dima Zavinfce7a472011-04-19 22:30:36 -07002632 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002633 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002634 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002635 lStatus = PERMISSION_DENIED;
2636 goto Exit;
2637 }
2638
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002639 if (mEffectsFactoryHal == 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002640 lStatus = NO_INIT;
2641 goto Exit;
2642 }
2643
Mathias Agopian65ab4712010-07-14 17:59:35 -07002644 {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002645 if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002646 // if uuid is specified, request effect descriptor
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002647 lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002648 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002649 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002650 goto Exit;
2651 }
2652 } else {
2653 // if uuid is not specified, look for an available implementation
2654 // of the required type in effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002655 if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002656 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002657 lStatus = BAD_VALUE;
2658 goto Exit;
2659 }
2660 uint32_t numEffects = 0;
2661 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002662 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002663 bool found = false;
2664
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002665 lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002666 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002667 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002668 goto Exit;
2669 }
2670 for (uint32_t i = 0; i < numEffects; i++) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002671 lStatus = mEffectsFactoryHal->getDescriptor(i, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002672 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002673 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002674 continue;
2675 }
2676 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2677 // If matching type found save effect descriptor. If the session is
2678 // 0 and the effect is not auxiliary, continue enumeration in case
2679 // an auxiliary version of this effect type is available
2680 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002681 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002682 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002683 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2684 break;
2685 }
2686 }
2687 }
2688 if (!found) {
2689 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002690 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002691 goto Exit;
2692 }
2693 // For same effect type, chose auxiliary version over insert version if
2694 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002695 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002696 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002697 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002698 }
2699 }
2700
2701 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002702 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002703 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2704 lStatus = INVALID_OPERATION;
2705 goto Exit;
2706 }
2707
Eric Laurent59255e42011-07-27 19:49:51 -07002708 // check recording permission for visualizer
2709 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002710 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002711 lStatus = PERMISSION_DENIED;
2712 goto Exit;
2713 }
2714
Mathias Agopian65ab4712010-07-14 17:59:35 -07002715 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002716 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002717 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002718 // if the output returned by getOutputForEffect() is removed before we lock the
2719 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2720 // and we will exit safely
2721 io = AudioSystem::getOutputForEffect(&desc);
2722 ALOGV("createEffect got output %d", io);
2723 }
2724
2725 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002726
2727 // If output is not specified try to find a matching audio session ID in one of the
2728 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002729 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2730 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002731 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002732 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002733 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2734 // output must be specified by AudioPolicyManager when using session
2735 // AUDIO_SESSION_OUTPUT_STAGE
2736 lStatus = BAD_VALUE;
2737 goto Exit;
2738 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002739 // look for the thread where the specified audio session is present
2740 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2741 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2742 io = mPlaybackThreads.keyAt(i);
2743 break;
2744 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002745 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002746 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002747 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2748 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2749 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002750 break;
2751 }
2752 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002753 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002754 // If no output thread contains the requested session ID, default to
2755 // first output. The effect chain will be moved to the correct output
2756 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002757 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002758 io = mPlaybackThreads.keyAt(0);
2759 }
Steve Block3856b092011-10-20 11:56:00 +01002760 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002761 }
2762 ThreadBase *thread = checkRecordThread_l(io);
2763 if (thread == NULL) {
2764 thread = checkPlaybackThread_l(io);
2765 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002766 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002767 lStatus = BAD_VALUE;
2768 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002769 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002770 } else {
2771 // Check if one effect chain was awaiting for an effect to be created on this
2772 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08002773 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07002774 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002775 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002776 thread->addEffectChain_l(chain);
2777 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002778 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002779
Eric Laurent021cf962014-05-13 10:18:14 -07002780 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002781
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002782 // create effect on selected output thread
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002783 bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002784 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002785 &desc, enabled, &lStatus, pinned);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002786 if (handle != 0 && id != NULL) {
2787 *id = handle->id();
2788 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002789 if (handle == 0) {
2790 // remove local strong reference to Client with mClientLock held
2791 Mutex::Autolock _cl(mClientLock);
2792 client.clear();
2793 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002794 }
2795
2796Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002797 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002798 return handle;
2799}
2800
Glenn Kastend848eb42016-03-08 13:42:11 -08002801status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002802 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002803{
Steve Block3856b092011-10-20 11:56:00 +01002804 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002805 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002806 Mutex::Autolock _l(mLock);
2807 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002808 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002809 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002810 }
Eric Laurentde070132010-07-13 04:45:46 -07002811 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2812 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002813 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002814 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002815 }
Eric Laurentde070132010-07-13 04:45:46 -07002816 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2817 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002818 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002819 return BAD_VALUE;
2820 }
2821
2822 Mutex::Autolock _dl(dstThread->mLock);
2823 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002824 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002825}
2826
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002827// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08002828status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002829 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002830 AudioFlinger::PlaybackThread *dstThread,
2831 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002832{
Steve Block3856b092011-10-20 11:56:00 +01002833 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002834 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002835
Eric Laurent59255e42011-07-27 19:49:51 -07002836 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002837 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002838 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002839 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002840 return INVALID_OPERATION;
2841 }
2842
Eric Laurent4c415062016-06-17 16:14:16 -07002843 // Check whether the destination thread and all effects in the chain are compatible
2844 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07002845 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07002846 " destination thread %p is not compatible with effects in the chain",
2847 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07002848 return INVALID_OPERATION;
2849 }
2850
Eric Laurent39e94f82010-07-28 01:32:47 -07002851 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002852 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002853 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002854 // removed.
2855 srcThread->removeEffectChain_l(chain);
2856
2857 // transfer all effects one by one so that new effect chain is created on new thread with
2858 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002859 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002860 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002861 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002862 Vector< sp<EffectModule> > removed;
2863 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002864 while (effect != 0) {
2865 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002866 removed.add(effect);
2867 status = dstThread->addEffect_l(effect);
2868 if (status != NO_ERROR) {
2869 break;
2870 }
Eric Laurentec35a142011-10-05 17:42:25 -07002871 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2872 if (effect->state() == EffectModule::ACTIVE ||
2873 effect->state() == EffectModule::STOPPING) {
2874 effect->start();
2875 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002876 // if the move request is not received from audio policy manager, the effect must be
2877 // re-registered with the new strategy and output
2878 if (dstChain == 0) {
2879 dstChain = effect->chain().promote();
2880 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002881 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002882 status = NO_INIT;
2883 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002884 }
2885 strategy = dstChain->strategy();
2886 }
2887 if (reRegister) {
2888 AudioSystem::unregisterEffect(effect->id());
2889 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002890 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002891 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002892 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002893 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002894 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002895 }
Eric Laurentde070132010-07-13 04:45:46 -07002896 effect = chain->getEffectFromId_l(0);
2897 }
2898
Eric Laurent5baf2af2013-09-12 17:37:00 -07002899 if (status != NO_ERROR) {
2900 for (size_t i = 0; i < removed.size(); i++) {
2901 srcThread->addEffect_l(removed[i]);
2902 if (dstChain != 0 && reRegister) {
2903 AudioSystem::unregisterEffect(removed[i]->id());
2904 AudioSystem::registerEffect(&removed[i]->desc(),
2905 srcThread->id(),
2906 strategy,
2907 sessionId,
2908 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002909 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002910 }
2911 }
2912 }
2913
2914 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002915}
2916
Eric Laurent5baf2af2013-09-12 17:37:00 -07002917bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002918{
2919 if (mGlobalEffectEnableTime != 0 &&
2920 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2921 return true;
2922 }
2923
2924 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2925 sp<EffectChain> ec =
2926 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002927 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002928 return true;
2929 }
2930 }
2931 return false;
2932}
2933
Eric Laurent5baf2af2013-09-12 17:37:00 -07002934void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002935{
2936 Mutex::Autolock _l(mLock);
2937
2938 mGlobalEffectEnableTime = systemTime();
2939
2940 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2941 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2942 if (t->mType == ThreadBase::OFFLOAD) {
2943 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2944 }
2945 }
2946
2947}
2948
Eric Laurentaaa44472014-09-12 17:41:50 -07002949status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2950{
Glenn Kastend848eb42016-03-08 13:42:11 -08002951 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002952 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002953 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002954 if (index >= 0) {
2955 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2956 return ALREADY_EXISTS;
2957 }
2958 mOrphanEffectChains.add(session, chain);
2959 return NO_ERROR;
2960}
2961
2962sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2963{
2964 sp<EffectChain> chain;
2965 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002966 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002967 if (index >= 0) {
2968 chain = mOrphanEffectChains.valueAt(index);
2969 mOrphanEffectChains.removeItemsAt(index);
2970 }
2971 return chain;
2972}
2973
2974bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2975{
2976 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08002977 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002978 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002979 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002980 if (index >= 0) {
2981 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002982 if (chain->removeEffect_l(effect, true) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002983 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002984 mOrphanEffectChains.removeItemsAt(index);
2985 }
2986 return true;
2987 }
2988 return false;
2989}
2990
2991
Glenn Kastenda6ef132013-01-10 12:31:01 -08002992struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002993#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2994 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002995};
2996
2997int comparEntry(const void *p1, const void *p2)
2998{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002999 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003000}
3001
Glenn Kasten46909e72013-02-26 09:20:22 -08003002#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08003003void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003004{
Eric Laurent81784c32012-11-19 14:55:58 -08003005 NBAIO_Source *teeSource = source.get();
3006 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003007 // .wav rotation
3008 // There is a benign race condition if 2 threads call this simultaneously.
3009 // They would both traverse the directory, but the result would simply be
3010 // failures at unlink() which are ignored. It's also unlikely since
3011 // normally dumpsys is only done by bugreport or from the command line.
3012 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08003013 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08003014 size_t teePathLen = strlen(teePath);
3015 DIR *dir = opendir(teePath);
3016 teePath[teePathLen++] = '/';
3017 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003018#define TEE_MAX_SORT 20 // number of entries to sort
3019#define TEE_MAX_KEEP 10 // number of entries to keep
3020 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003021 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08003022 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003023 struct dirent de;
3024 struct dirent *result = NULL;
3025 int rc = readdir_r(dir, &de, &result);
3026 if (rc != 0) {
3027 ALOGW("readdir_r failed %d", rc);
3028 break;
3029 }
3030 if (result == NULL) {
3031 break;
3032 }
3033 if (result != &de) {
3034 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
3035 break;
3036 }
3037 // ignore non .wav file entries
3038 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003039 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08003040 strcmp(&de.d_name[nameLen - 4], ".wav")) {
3041 continue;
3042 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08003043 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003044 }
3045 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003046 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003047 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003048 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
3049 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003050 (void) unlink(teePath);
3051 }
3052 }
3053 } else {
3054 if (fd >= 0) {
Glenn Kastenfbd87e82016-07-18 15:45:56 -07003055 dprintf(fd, "unable to rotate tees in %.*s: %s\n", (int) teePathLen, teePath,
Glenn Kasten9a003992016-02-23 15:24:34 -08003056 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003057 }
3058 }
Eric Laurent81784c32012-11-19 14:55:58 -08003059 char teeTime[16];
3060 struct timeval tv;
3061 gettimeofday(&tv, NULL);
3062 struct tm tm;
3063 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003064 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
3065 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
3066 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3067 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003068 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003069 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003070 char wavHeader[44];
3071 memcpy(wavHeader,
3072 "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",
3073 sizeof(wavHeader));
3074 NBAIO_Format format = teeSource->format();
3075 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003076 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003077 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003078 wavHeader[22] = channelCount; // number of channels
3079 wavHeader[24] = sampleRate; // sample rate
3080 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003081 wavHeader[32] = frameSize; // block alignment
3082 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003083 write(teeFd, wavHeader, sizeof(wavHeader));
3084 size_t total = 0;
3085 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003086#define TEE_SINK_READ 1024 // frames per I/O operation
3087 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003088 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003089 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003090 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003091 bool wasFirstRead = firstRead;
3092 firstRead = false;
3093 if (actual <= 0) {
3094 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3095 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003096 }
Eric Laurent81784c32012-11-19 14:55:58 -08003097 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003098 }
Eric Laurent81784c32012-11-19 14:55:58 -08003099 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003100 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003101 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003102 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003103 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003104 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003105 uint32_t temp = 44 + total * frameSize - 8;
3106 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003107 write(teeFd, &temp, sizeof(temp));
3108 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003109 temp = total * frameSize;
3110 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003111 write(teeFd, &temp, sizeof(temp));
3112 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003113 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003114 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003115 }
Eric Laurent59255e42011-07-27 19:49:51 -07003116 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003117 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003118 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003119 }
Eric Laurent59255e42011-07-27 19:49:51 -07003120 }
3121 }
3122}
Glenn Kasten46909e72013-02-26 09:20:22 -08003123#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003124
Mathias Agopian65ab4712010-07-14 17:59:35 -07003125// ----------------------------------------------------------------------------
3126
3127status_t AudioFlinger::onTransact(
3128 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3129{
3130 return BnAudioFlinger::onTransact(code, data, reply, flags);
3131}
3132
Glenn Kasten63238ef2015-03-02 15:50:29 -08003133} // namespace android