blob: c737f4bdae94446da81d9f87ddd03445982b63f1 [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
Mathias Agopian65ab4712010-07-14 17:59:35 -070049#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080050#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
Andy Hung6770c6f2015-04-07 13:43:36 -070052#include <media/AudioResamplerPublic.h>
53
Mikhail Naganov9fe94012016-10-14 14:57:40 -070054#include <system/audio_effects/effect_visualizer.h>
55#include <system/audio_effects/effect_ns.h>
56#include <system/audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070057
Glenn Kasten3b21c502011-12-15 09:52:39 -080058#include <audio_utils/primitives.h>
59
Eric Laurentfeb0db62011-07-22 09:04:31 -070060#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080061
Glenn Kasten9e58b552013-01-18 15:09:48 -080062#include <media/IMediaLogService.h>
Andy Hung07b745e2016-05-23 16:21:07 -070063#include <media/MemoryLeakTrackUtil.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080064#include <media/nbaio/Pipe.h>
65#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070066#include <media/AudioParameter.h>
Wei Jia3f273d12015-11-24 09:06:49 -080067#include <mediautils/BatteryNotifier.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070068#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080069
rago3bd1c872016-09-26 12:58:14 -070070//#define BUFLOG_NDEBUG 0
71#include <BufLog.h>
72
Nicolas Rouletfe1e1442017-01-30 12:02:03 -080073#include "TypedLogger.h"
74
Mathias Agopian65ab4712010-07-14 17:59:35 -070075// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070076
John Grossman1c345192012-03-27 14:00:17 -070077// Note: the following macro is used for extremely verbose logging message. In
78// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
79// 0; but one side effect of this is to turn all LOGV's as well. Some messages
80// are so verbose that we want to suppress them even when we have ALOG_ASSERT
81// turned on. Do not uncomment the #def below unless you really know what you
82// are doing and want to see all of the extremely verbose messages.
83//#define VERY_VERY_VERBOSE_LOGGING
84#ifdef VERY_VERY_VERBOSE_LOGGING
85#define ALOGVV ALOGV
86#else
87#define ALOGVV(a...) do { } while(0)
88#endif
Eric Laurentde070132010-07-13 04:45:46 -070089
Mathias Agopian65ab4712010-07-14 17:59:35 -070090namespace android {
91
Glenn Kastenec1d6b52011-12-12 09:04:45 -080092static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
93static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070094static const char kClientLockedString[] = "Client lock is taken\n";
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070095static const char kNoEffectsFactory[] = "Effects Factory is absent\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070096
Glenn Kasten58912562012-04-03 10:45:00 -070097
John Grossman4ff14ba2012-02-08 16:37:41 -080098nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080099
Eric Laurent81784c32012-11-19 14:55:58 -0800100uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -0700101
Glenn Kasten46909e72013-02-26 09:20:22 -0800102#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800103bool AudioFlinger::mTeeSinkInputEnabled = false;
104bool AudioFlinger::mTeeSinkOutputEnabled = false;
105bool AudioFlinger::mTeeSinkTrackEnabled = false;
106
107size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
108size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
109size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800110#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800111
Eric Laurent813e2a72013-08-31 12:59:48 -0700112// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
113// we define a minimum time during which a global effect is considered enabled.
114static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
115
Mathias Agopian65ab4712010-07-14 17:59:35 -0700116// ----------------------------------------------------------------------------
117
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700118std::string formatToString(audio_format_t format) {
119 std::string result;
120 FormatConverter::toString(format, result);
121 return result;
Marco Nelissenb2208842014-02-07 14:00:50 -0800122}
123
Mathias Agopian65ab4712010-07-14 17:59:35 -0700124// ----------------------------------------------------------------------------
125
126AudioFlinger::AudioFlinger()
127 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800128 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800129 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700130 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800131 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800132 mMasterMute(false),
Glenn Kastend2e67e12016-04-11 08:26:37 -0700133 // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),
John Grossman4ff14ba2012-02-08 16:37:41 -0800134 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700135 mBtNrecIsOff(false),
136 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700137 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700138 mGlobalEffectEnableTime(0),
Eric Laurent72e3f392015-05-20 14:43:50 -0700139 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700140{
Glenn Kastend2e67e12016-04-11 08:26:37 -0700141 // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum
142 for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) {
143 // zero ID has a special meaning, so unavailable
144 mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX;
145 }
146
Glenn Kasten949a9262013-04-16 12:35:20 -0700147 getpid_cached = getpid();
Glenn Kastenae0cff12016-02-24 13:57:49 -0800148 const bool doLog = property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800149 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800150 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
151 MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800152 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700153
Wei Jia3f273d12015-11-24 09:06:49 -0800154 // reset battery stats.
155 // if the audio service has crashed, battery stats could be left
156 // in bad state, reset the state upon service start.
157 BatteryNotifier::getInstance().noteResetAudio();
158
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700159 mDevicesFactoryHal = DevicesFactoryHalInterface::create();
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700160 mEffectsFactoryHal = EffectsFactoryHalInterface::create();
161
Glenn Kasten46909e72013-02-26 09:20:22 -0800162#ifdef TEE_SINK
Glenn Kasten9a003992016-02-23 15:24:34 -0800163 char value[PROPERTY_VALUE_MAX];
Glenn Kastenda6ef132013-01-10 12:31:01 -0800164 (void) property_get("ro.debuggable", value, "0");
165 int debuggable = atoi(value);
166 int teeEnabled = 0;
167 if (debuggable) {
168 (void) property_get("af.tee", value, "0");
169 teeEnabled = atoi(value);
170 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800171 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700172 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800173 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700174 }
175 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800176 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700177 }
178 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800179 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700180 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800181#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700182}
183
184void AudioFlinger::onFirstRef()
185{
Eric Laurent93575202011-01-18 18:39:02 -0800186 Mutex::Autolock _l(mLock);
187
Dima Zavin799a70e2011-04-18 16:57:27 -0700188 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800189 char val_str[PROPERTY_VALUE_MAX] = { 0 };
190 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
191 uint32_t int_val;
192 if (1 == sscanf(val_str, "%u", &int_val)) {
193 mStandbyTimeInNsecs = milliseconds(int_val);
194 ALOGI("Using %u mSec as standby time.", int_val);
195 } else {
196 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
197 ALOGI("Using default %u mSec as standby time.",
198 (uint32_t)(mStandbyTimeInNsecs / 1000000));
199 }
200 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700201
Eric Laurent1c333e22014-05-20 10:48:17 -0700202 mPatchPanel = new PatchPanel(this);
Andy Hungdeb03352016-08-29 14:40:14 -0700203
Eric Laurenta4c5a552012-03-29 10:12:40 -0700204 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205}
206
207AudioFlinger::~AudioFlinger()
208{
209 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700210 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700211 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212 }
213 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700214 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700215 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700217
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800218 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
219 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700220 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700221 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700222
223 // Tell media.log service about any old writers that still need to be unregistered
Andy Hungce717682015-12-22 13:40:32 -0800224 if (mLogMemoryDealer != 0) {
225 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
226 if (binder != 0) {
227 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
228 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
229 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
230 mUnregisteredWriters.pop();
231 mediaLogService->unregisterWriter(iMemory);
232 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700233 }
234 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235}
236
Eric Laurenta4c5a552012-03-29 10:12:40 -0700237static const char * const audio_interfaces[] = {
238 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
239 AUDIO_HARDWARE_MODULE_ID_A2DP,
240 AUDIO_HARDWARE_MODULE_ID_USB,
241};
242#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
243
Phil Burk062e67a2015-02-11 13:40:50 -0800244AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700245 audio_module_handle_t module,
246 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700247{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700248 // if module is 0, the request comes from an old policy manager and we should load
249 // well known modules
250 if (module == 0) {
251 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
252 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
253 loadHwModule_l(audio_interfaces[i]);
254 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700255 // then try to find a module supporting the requested device.
256 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
257 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700258 sp<DeviceHalInterface> dev = audioHwDevice->hwDevice();
259 uint32_t supportedDevices;
260 if (dev->getSupportedDevices(&supportedDevices) == OK &&
261 (supportedDevices & devices) == devices) {
Eric Laurentf1c04f92012-08-28 14:26:53 -0700262 return audioHwDevice;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700263 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700264 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700265 } else {
266 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700267 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
268 if (audioHwDevice != NULL) {
269 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700270 }
271 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700272
Dima Zavin799a70e2011-04-18 16:57:27 -0700273 return NULL;
274}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275
Glenn Kasten0f11b512014-01-31 16:18:54 -0800276void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277{
278 const size_t SIZE = 256;
279 char buffer[SIZE];
280 String8 result;
281
282 result.append("Clients:\n");
283 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800284 sp<Client> client = mClients.valueAt(i).promote();
285 if (client != 0) {
286 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
287 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700288 }
289 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700290
Eric Laurentd1b28d42013-09-18 18:47:13 -0700291 result.append("Notification Clients:\n");
292 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
293 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
294 result.append(buffer);
295 }
296
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700297 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800298 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700299 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
300 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800301 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700302 result.append(buffer);
303 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305}
306
307
Glenn Kasten0f11b512014-01-31 16:18:54 -0800308void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309{
310 const size_t SIZE = 256;
311 char buffer[SIZE];
312 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800313 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700314
John Grossman4ff14ba2012-02-08 16:37:41 -0800315 snprintf(buffer, SIZE, "Hardware status: %d\n"
316 "Standby Time mSec: %u\n",
317 hardwareStatus,
318 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 result.append(buffer);
320 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700321}
322
Glenn Kasten0f11b512014-01-31 16:18:54 -0800323void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324{
325 const size_t SIZE = 256;
326 char buffer[SIZE];
327 String8 result;
328 snprintf(buffer, SIZE, "Permission Denial: "
329 "can't dump AudioFlinger from pid=%d, uid=%d\n",
330 IPCThreadState::self()->getCallingPid(),
331 IPCThreadState::self()->getCallingUid());
332 result.append(buffer);
333 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334}
335
Eric Laurent81784c32012-11-19 14:55:58 -0800336bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337{
338 bool locked = false;
339 for (int i = 0; i < kDumpLockRetries; ++i) {
340 if (mutex.tryLock() == NO_ERROR) {
341 locked = true;
342 break;
343 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800344 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 }
346 return locked;
347}
348
349status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
350{
Glenn Kasten44deb052012-02-05 18:09:08 -0800351 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352 dumpPermissionDenial(fd, args);
353 } else {
354 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800355 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356 if (!hardwareLocked) {
357 String8 result(kHardwareLockedString);
358 write(fd, result.string(), result.size());
359 } else {
360 mHardwareLock.unlock();
361 }
362
Eric Laurent81784c32012-11-19 14:55:58 -0800363 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364
365 // failed to lock - AudioFlinger is probably deadlocked
366 if (!locked) {
367 String8 result(kDeadlockedString);
368 write(fd, result.string(), result.size());
369 }
370
Eric Laurent021cf962014-05-13 10:18:14 -0700371 bool clientLocked = dumpTryLock(mClientLock);
372 if (!clientLocked) {
373 String8 result(kClientLockedString);
374 write(fd, result.string(), result.size());
375 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700376
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700377 if (mEffectsFactoryHal != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700378 mEffectsFactoryHal->dumpEffects(fd);
379 } else {
380 String8 result(kNoEffectsFactory);
381 write(fd, result.string(), result.size());
382 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700383
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700385 if (clientLocked) {
386 mClientLock.unlock();
387 }
388
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389 dumpInternals(fd, args);
390
391 // dump playback threads
392 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
393 mPlaybackThreads.valueAt(i)->dump(fd, args);
394 }
395
396 // dump record threads
397 for (size_t i = 0; i < mRecordThreads.size(); i++) {
398 mRecordThreads.valueAt(i)->dump(fd, args);
399 }
400
Eric Laurentaaa44472014-09-12 17:41:50 -0700401 // dump orphan effect chains
402 if (mOrphanEffectChains.size() != 0) {
403 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
404 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
405 mOrphanEffectChains.valueAt(i)->dump(fd, args);
406 }
407 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700408 // dump all hardware devs
409 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700410 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
411 dev->dump(fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700413
Glenn Kasten46909e72013-02-26 09:20:22 -0800414#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700415 // dump the serially shared record tee sink
416 if (mRecordTeeSource != 0) {
417 dumpTee(fd, mRecordTeeSource);
418 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800419#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700420
rago3bd1c872016-09-26 12:58:14 -0700421 BUFLOG_RESET;
422
Glenn Kastend65d73c2012-06-22 17:21:07 -0700423 if (locked) {
424 mLock.unlock();
425 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800426
427 // append a copy of media.log here by forwarding fd to it, but don't attempt
428 // to lookup the service if it's not running, as it will block for a second
429 if (mLogMemoryDealer != 0) {
430 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
431 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700432 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800433 Vector<String16> args;
434 binder->dump(fd, args);
435 }
436 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700437
438 // check for optional arguments
Andy Hung07b745e2016-05-23 16:21:07 -0700439 bool dumpMem = false;
Andy Hung35fec5f2016-04-13 14:21:48 -0700440 bool unreachableMemory = false;
441 for (const auto &arg : args) {
Andy Hung07b745e2016-05-23 16:21:07 -0700442 if (arg == String16("-m")) {
443 dumpMem = true;
444 } else if (arg == String16("--unreachable")) {
Andy Hung35fec5f2016-04-13 14:21:48 -0700445 unreachableMemory = true;
446 }
447 }
448
Andy Hung07b745e2016-05-23 16:21:07 -0700449 if (dumpMem) {
450 dprintf(fd, "\nDumping memory:\n");
451 std::string s = dumpMemoryAddresses(100 /* limit */);
452 write(fd, s.c_str(), s.size());
453 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700454 if (unreachableMemory) {
455 dprintf(fd, "\nDumping unreachable memory:\n");
456 // TODO - should limit be an argument parameter?
Andy Hung07b745e2016-05-23 16:21:07 -0700457 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
Andy Hung35fec5f2016-04-13 14:21:48 -0700458 write(fd, s.c_str(), s.size());
459 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700460 }
461 return NO_ERROR;
462}
463
Eric Laurent021cf962014-05-13 10:18:14 -0700464sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800465{
Eric Laurent021cf962014-05-13 10:18:14 -0700466 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800467 // If pid is already in the mClients wp<> map, then use that entry
468 // (for which promote() is always != 0), otherwise create a new entry and Client.
469 sp<Client> client = mClients.valueFor(pid).promote();
470 if (client == 0) {
471 client = new Client(this, pid);
472 mClients.add(pid, client);
473 }
474
475 return client;
476}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700477
Glenn Kasten9e58b552013-01-18 15:09:48 -0800478sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
479{
Glenn Kasten481fb672013-09-30 14:39:28 -0700480 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800481 if (mLogMemoryDealer == 0) {
482 return new NBLog::Writer();
483 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800484 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700485 // Similarly if we can't contact the media.log service, also return a dummy writer
486 if (binder == 0) {
487 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800488 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700489 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
490 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
491 // If allocation fails, consult the vector of previously unregistered writers
492 // and garbage-collect one or more them until an allocation succeeds
493 if (shared == 0) {
494 Mutex::Autolock _l(mUnregisteredWritersLock);
495 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
496 {
497 // Pick the oldest stale writer to garbage-collect
498 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
499 mUnregisteredWriters.removeAt(0);
500 mediaLogService->unregisterWriter(iMemory);
501 // Now the media.log remote reference to IMemory is gone. When our last local
502 // reference to IMemory also drops to zero at end of this block,
503 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
504 }
505 // Re-attempt the allocation
506 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
507 if (shared != 0) {
508 goto success;
509 }
510 }
511 // Even after garbage-collecting all old writers, there is still not enough memory,
512 // so return a dummy writer
513 return new NBLog::Writer();
514 }
515success:
Glenn Kasten535e1612016-12-05 12:19:36 -0800516 NBLog::Shared *sharedRawPtr = (NBLog::Shared *) shared->pointer();
517 new((void *) sharedRawPtr) NBLog::Shared(); // placement new here, but the corresponding
518 // explicit destructor not needed since it is POD
Glenn Kasten481fb672013-09-30 14:39:28 -0700519 mediaLogService->registerWriter(shared, size, name);
Glenn Kasten535e1612016-12-05 12:19:36 -0800520 return new NBLog::Writer(shared, size);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800521}
522
523void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
524{
Glenn Kasten685ef092013-02-04 08:15:34 -0800525 if (writer == 0) {
526 return;
527 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800528 sp<IMemory> iMemory(writer->getIMemory());
529 if (iMemory == 0) {
530 return;
531 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700532 // Rather than removing the writer immediately, append it to a queue of old writers to
533 // be garbage-collected later. This allows us to continue to view old logs for a while.
534 Mutex::Autolock _l(mUnregisteredWritersLock);
535 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800536}
537
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538// IAudioFlinger interface
539
540
541sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800542 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800544 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700545 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800546 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -0700547 audio_output_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800549 audio_io_handle_t output,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700550 pid_t pid,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800551 pid_t tid,
Glenn Kastend848eb42016-03-08 13:42:11 -0800552 audio_session_t *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800553 int clientUid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800554 status_t *status,
555 audio_port_handle_t portId)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556{
557 sp<PlaybackThread::Track> track;
558 sp<TrackHandle> trackHandle;
559 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -0800561 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700563 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
564 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
565 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
566 ALOGW_IF(pid != -1 && pid != callingPid,
567 "%s uid %d pid %d tried to pass itself off as pid %d",
568 __func__, callingUid, callingPid, pid);
569 pid = callingPid;
570 }
571
Glenn Kasten263709e2012-01-06 08:40:01 -0800572 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
573 // but if someone uses binder directly they could bypass that and cause us to crash
574 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000575 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700576 lStatus = BAD_VALUE;
577 goto Exit;
578 }
579
Glenn Kasten53b5d092014-02-05 10:00:23 -0800580 // further sample rate checks are performed by createTrack_l() depending on the thread type
581 if (sampleRate == 0) {
582 ALOGE("createTrack() invalid sample rate %u", sampleRate);
583 lStatus = BAD_VALUE;
584 goto Exit;
585 }
586
587 // further channel mask checks are performed by createTrack_l() depending on the thread type
588 if (!audio_is_output_channel(channelMask)) {
589 ALOGE("createTrack() invalid channel mask %#x", channelMask);
590 lStatus = BAD_VALUE;
591 goto Exit;
592 }
593
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700594 // further format checks are performed by createTrack_l() depending on the thread type
595 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800596 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700597 lStatus = BAD_VALUE;
598 goto Exit;
599 }
600
Glenn Kasten663c2242013-09-24 11:52:37 -0700601 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
602 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
603 lStatus = BAD_VALUE;
604 goto Exit;
605 }
606
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607 {
608 Mutex::Autolock _l(mLock);
609 PlaybackThread *thread = checkPlaybackThread_l(output);
610 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800611 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612 lStatus = BAD_VALUE;
613 goto Exit;
614 }
615
Eric Laurent021cf962014-05-13 10:18:14 -0700616 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700617
Glenn Kastene848bd92014-03-13 15:00:32 -0700618 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700619 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800620 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
621 ALOGE("createTrack() invalid session ID %d", *sessionId);
622 lStatus = BAD_VALUE;
623 goto Exit;
624 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700625 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700626 // check if an effect chain with the same session ID is present on another
627 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700628 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700629 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
630 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700631 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent4c415062016-06-17 16:14:16 -0700632 if (sessions & ThreadBase::EFFECT_SESSION) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700633 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700634 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700635 }
Eric Laurentde070132010-07-13 04:45:46 -0700636 }
637 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700639 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800640 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641 if (sessionId != NULL) {
642 *sessionId = lSessionId;
643 }
644 }
Steve Block3856b092011-10-20 11:56:00 +0100645 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646
647 track = thread->createTrack_l(client, streamType, sampleRate, format,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800648 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid,
649 clientUid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800650 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700651 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700652
653 // move effect chain to this output thread if an effect on same session was waiting
654 // for a track to be created
655 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700656 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700657 Mutex::Autolock _dl(thread->mLock);
658 Mutex::Autolock _sl(effectThread->mLock);
659 moveEffectChain_l(lSessionId, effectThread, thread, true);
660 }
Eric Laurenta011e352012-03-29 15:51:43 -0700661
662 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700663 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700664 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
665 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700666 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700667 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700668 } else {
669 mPendingSyncEvents[i]->cancel();
670 }
Eric Laurenta011e352012-03-29 15:51:43 -0700671 mPendingSyncEvents.removeAt(i);
672 i--;
673 }
674 }
675 }
Glenn Kastene198c362013-08-13 09:13:36 -0700676
Glenn Kastend848eb42016-03-08 13:42:11 -0800677 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 }
Glenn Kastene198c362013-08-13 09:13:36 -0700679
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700680 if (lStatus != NO_ERROR) {
681 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700682 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700683 // Don't hold mClientLock when releasing the reference on the track as the
684 // destructor will acquire it.
685 {
686 Mutex::Autolock _cl(mClientLock);
687 client.clear();
688 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700690 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691 }
692
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700693 // return handle to client
694 trackHandle = new TrackHandle(track);
695
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700697 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698 return trackHandle;
699}
700
Glenn Kasten2c073da2016-02-26 09:14:08 -0800701uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702{
703 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800704 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800706 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707 return 0;
708 }
709 return thread->sampleRate();
710}
711
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800712audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713{
714 Mutex::Autolock _l(mLock);
715 PlaybackThread *thread = checkPlaybackThread_l(output);
716 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000717 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800718 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700719 }
720 return thread->format();
721}
722
Glenn Kasten2c073da2016-02-26 09:14:08 -0800723size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724{
725 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800726 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800728 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700729 return 0;
730 }
Glenn Kasten58912562012-04-03 10:45:00 -0700731 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
732 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700733 return thread->frameCount();
734}
735
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700736size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
737{
738 Mutex::Autolock _l(mLock);
739 ThreadBase *thread = checkThread_l(ioHandle);
740 if (thread == NULL) {
741 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
742 return 0;
743 }
744 return thread->frameCountHAL();
745}
746
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800747uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748{
749 Mutex::Autolock _l(mLock);
750 PlaybackThread *thread = checkPlaybackThread_l(output);
751 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800752 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753 return 0;
754 }
755 return thread->latency();
756}
757
758status_t AudioFlinger::setMasterVolume(float value)
759{
Eric Laurenta1884f92011-08-23 08:25:03 -0700760 status_t ret = initCheck();
761 if (ret != NO_ERROR) {
762 return ret;
763 }
764
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765 // check calling permissions
766 if (!settingsAllowed()) {
767 return PERMISSION_DENIED;
768 }
769
Eric Laurenta4c5a552012-03-29 10:12:40 -0700770 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700771 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700772
John Grossmanee578c02012-07-23 17:05:46 -0700773 // Set master volume in the HALs which support it.
774 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
775 AutoMutex lock(mHardwareLock);
776 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800777
John Grossmanee578c02012-07-23 17:05:46 -0700778 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
779 if (dev->canSetMasterVolume()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700780 dev->hwDevice()->setMasterVolume(value);
Eric Laurent93575202011-01-18 18:39:02 -0800781 }
John Grossmanee578c02012-07-23 17:05:46 -0700782 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784
John Grossmanee578c02012-07-23 17:05:46 -0700785 // Now set the master volume in each playback thread. Playback threads
786 // assigned to HALs which do not have master volume support will apply
787 // master volume during the mix operation. Threads with HALs which do
788 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700789 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
790 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
791 continue;
792 }
John Grossmanee578c02012-07-23 17:05:46 -0700793 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700794 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795
796 return NO_ERROR;
797}
798
Glenn Kastenf78aee72012-01-04 11:00:47 -0800799status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800{
Eric Laurenta1884f92011-08-23 08:25:03 -0700801 status_t ret = initCheck();
802 if (ret != NO_ERROR) {
803 return ret;
804 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805
806 // check calling permissions
807 if (!settingsAllowed()) {
808 return PERMISSION_DENIED;
809 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800810 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000811 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 return BAD_VALUE;
813 }
814
815 { // scope for the lock
816 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700817 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700819 ret = dev->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 mHardwareStatus = AUDIO_HW_IDLE;
821 }
822
823 if (NO_ERROR == ret) {
824 Mutex::Autolock _l(mLock);
825 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800826 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700827 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828 }
829
830 return ret;
831}
832
833status_t AudioFlinger::setMicMute(bool state)
834{
Eric Laurenta1884f92011-08-23 08:25:03 -0700835 status_t ret = initCheck();
836 if (ret != NO_ERROR) {
837 return ret;
838 }
839
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840 // check calling permissions
841 if (!settingsAllowed()) {
842 return PERMISSION_DENIED;
843 }
844
845 AutoMutex lock(mHardwareLock);
846 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700847 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700848 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
849 status_t result = dev->setMicMute(state);
Eric Laurent2f035f52014-09-14 12:11:52 -0700850 if (result != NO_ERROR) {
851 ret = result;
852 }
853 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854 mHardwareStatus = AUDIO_HW_IDLE;
855 return ret;
856}
857
858bool AudioFlinger::getMicMute() const
859{
Eric Laurenta1884f92011-08-23 08:25:03 -0700860 status_t ret = initCheck();
861 if (ret != NO_ERROR) {
862 return false;
863 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800864 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700865 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800866 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800868 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700869 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
870 status_t result = dev->getMicMute(&state);
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800871 if (result == NO_ERROR) {
872 mute = mute && state;
873 }
874 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700875 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800876
877 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878}
879
880status_t AudioFlinger::setMasterMute(bool muted)
881{
John Grossmand8f178d2012-07-20 14:51:35 -0700882 status_t ret = initCheck();
883 if (ret != NO_ERROR) {
884 return ret;
885 }
886
Mathias Agopian65ab4712010-07-14 17:59:35 -0700887 // check calling permissions
888 if (!settingsAllowed()) {
889 return PERMISSION_DENIED;
890 }
891
John Grossmanee578c02012-07-23 17:05:46 -0700892 Mutex::Autolock _l(mLock);
893 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700894
John Grossmanee578c02012-07-23 17:05:46 -0700895 // Set master mute in the HALs which support it.
896 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
897 AutoMutex lock(mHardwareLock);
898 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700899
John Grossmanee578c02012-07-23 17:05:46 -0700900 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
901 if (dev->canSetMasterMute()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700902 dev->hwDevice()->setMasterMute(muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700903 }
John Grossmanee578c02012-07-23 17:05:46 -0700904 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700905 }
906
John Grossmanee578c02012-07-23 17:05:46 -0700907 // Now set the master mute in each playback thread. Playback threads
908 // assigned to HALs which do not have master mute support will apply master
909 // mute during the mix operation. Threads with HALs which do support master
910 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700911 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
912 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
913 continue;
914 }
John Grossmanee578c02012-07-23 17:05:46 -0700915 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700916 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917
918 return NO_ERROR;
919}
920
921float AudioFlinger::masterVolume() const
922{
Glenn Kasten98067102011-12-13 11:47:54 -0800923 Mutex::Autolock _l(mLock);
924 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925}
926
927bool AudioFlinger::masterMute() const
928{
Glenn Kasten98067102011-12-13 11:47:54 -0800929 Mutex::Autolock _l(mLock);
930 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931}
932
John Grossman4ff14ba2012-02-08 16:37:41 -0800933float AudioFlinger::masterVolume_l() const
934{
John Grossman4ff14ba2012-02-08 16:37:41 -0800935 return mMasterVolume;
936}
937
John Grossmand8f178d2012-07-20 14:51:35 -0700938bool AudioFlinger::masterMute_l() const
939{
John Grossmanee578c02012-07-23 17:05:46 -0700940 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700941}
942
Eric Laurent223fd5c2014-11-11 13:43:36 -0800943status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
944{
945 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
946 ALOGW("setStreamVolume() invalid stream %d", stream);
947 return BAD_VALUE;
948 }
949 pid_t caller = IPCThreadState::self()->getCallingPid();
950 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
951 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
952 return PERMISSION_DENIED;
953 }
954
955 return NO_ERROR;
956}
957
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800958status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
959 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960{
961 // check calling permissions
962 if (!settingsAllowed()) {
963 return PERMISSION_DENIED;
964 }
965
Eric Laurent223fd5c2014-11-11 13:43:36 -0800966 status_t status = checkStreamType(stream);
967 if (status != NO_ERROR) {
968 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800970 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971
972 AutoMutex lock(mLock);
973 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700974 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975 thread = checkPlaybackThread_l(output);
976 if (thread == NULL) {
977 return BAD_VALUE;
978 }
979 }
980
981 mStreamTypes[stream].volume = value;
982
983 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800984 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700985 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700986 }
987 } else {
988 thread->setStreamVolume(stream, value);
989 }
990
991 return NO_ERROR;
992}
993
Glenn Kastenfff6d712012-01-12 16:38:12 -0800994status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995{
996 // check calling permissions
997 if (!settingsAllowed()) {
998 return PERMISSION_DENIED;
999 }
1000
Eric Laurent223fd5c2014-11-11 13:43:36 -08001001 status_t status = checkStreamType(stream);
1002 if (status != NO_ERROR) {
1003 return status;
1004 }
1005 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1006
1007 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001008 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001009 return BAD_VALUE;
1010 }
1011
Eric Laurent93575202011-01-18 18:39:02 -08001012 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001013 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -07001014 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001015 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016
1017 return NO_ERROR;
1018}
1019
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001020float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001022 status_t status = checkStreamType(stream);
1023 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001024 return 0.0f;
1025 }
1026
1027 AutoMutex lock(mLock);
1028 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001029 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001030 PlaybackThread *thread = checkPlaybackThread_l(output);
1031 if (thread == NULL) {
1032 return 0.0f;
1033 }
1034 volume = thread->streamVolume(stream);
1035 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001036 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001037 }
1038
1039 return volume;
1040}
1041
Glenn Kastenfff6d712012-01-12 16:38:12 -08001042bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001043{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001044 status_t status = checkStreamType(stream);
1045 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046 return true;
1047 }
1048
Glenn Kasten6637baa2012-01-09 09:40:36 -08001049 AutoMutex lock(mLock);
1050 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051}
1052
Eric Laurent054d9d32015-04-24 08:48:48 -07001053
1054void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1055{
1056 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1057 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1058 }
1059}
1060
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001061status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001063 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1064 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001065
Mathias Agopian65ab4712010-07-14 17:59:35 -07001066 // check calling permissions
1067 if (!settingsAllowed()) {
1068 return PERMISSION_DENIED;
1069 }
1070
Glenn Kasten142f5192014-03-25 17:44:59 -07001071 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1072 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001073 Mutex::Autolock _l(mLock);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001074 // result will remain NO_INIT if no audio device is present
1075 status_t final_result = NO_INIT;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001076 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001077 AutoMutex lock(mHardwareLock);
1078 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1079 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001080 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1081 status_t result = dev->setParameters(keyValuePairs);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001082 // return success if at least one audio device accepts the parameters as not all
1083 // HALs are requested to support all parameters. If no audio device supports the
1084 // requested parameters, the last error is reported.
1085 if (final_result != NO_ERROR) {
1086 final_result = result;
1087 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001088 }
1089 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001090 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001091 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1092 AudioParameter param = AudioParameter(keyValuePairs);
1093 String8 value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001094 if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) {
1095 bool btNrecIsOff = (value == AudioParameter::valueOff);
Eric Laurentbee53372011-08-29 12:42:48 -07001096 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001097 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1098 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001099 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001100 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1101 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001102 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001103 // suspend effects associated with those session IDs
1104 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001105 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001106 thread->setEffectSuspended(FX_IID_AEC,
1107 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001108 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001109 thread->setEffectSuspended(FX_IID_NS,
1110 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001111 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001112 }
1113 }
Eric Laurentbee53372011-08-29 12:42:48 -07001114 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001115 }
1116 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001117 String8 screenState;
1118 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
Mikhail Naganov00260b52016-10-13 12:54:24 -07001119 bool isOff = (screenState == AudioParameter::valueOff);
Eric Laurent81784c32012-11-19 14:55:58 -08001120 if (isOff != (AudioFlinger::mScreenState & 1)) {
1121 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001122 }
1123 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001124 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125 }
1126
1127 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1128 // and the thread is exited once the lock is released
1129 sp<ThreadBase> thread;
1130 {
1131 Mutex::Autolock _l(mLock);
1132 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001133 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001135 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001136 // indicate output device change to all input threads for pre processing
1137 AudioParameter param = AudioParameter(keyValuePairs);
1138 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001139 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1140 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001141 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001142 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143 }
1144 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001145 if (thread != 0) {
1146 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001147 }
1148 return BAD_VALUE;
1149}
1150
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001151String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001153 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1154 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155
Eric Laurenta4c5a552012-03-29 10:12:40 -07001156 Mutex::Autolock _l(mLock);
1157
Glenn Kasten142f5192014-03-25 17:44:59 -07001158 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001159 String8 out_s8;
1160
Dima Zavin799a70e2011-04-18 16:57:27 -07001161 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001162 String8 s;
1163 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001164 {
1165 AutoMutex lock(mHardwareLock);
1166 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001167 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1168 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001169 mHardwareStatus = AUDIO_HW_IDLE;
1170 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001171 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001172 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001173 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001174 }
1175
Mathias Agopian65ab4712010-07-14 17:59:35 -07001176 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1177 if (playbackThread != NULL) {
1178 return playbackThread->getParameters(keys);
1179 }
1180 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1181 if (recordThread != NULL) {
1182 return recordThread->getParameters(keys);
1183 }
1184 return String8("");
1185}
1186
Glenn Kastendd8104c2012-07-02 12:42:44 -07001187size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1188 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001189{
Eric Laurenta1884f92011-08-23 08:25:03 -07001190 status_t ret = initCheck();
1191 if (ret != NO_ERROR) {
1192 return 0;
1193 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001194 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001195 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001196 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001197 return 0;
1198 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001199
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001200 AutoMutex lock(mHardwareLock);
1201 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001202 audio_config_t config, proposed;
1203 memset(&proposed, 0, sizeof(proposed));
1204 proposed.sample_rate = sampleRate;
1205 proposed.channel_mask = channelMask;
1206 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001207
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001208 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001209 size_t frames;
1210 for (;;) {
1211 // Note: config is currently a const parameter for get_input_buffer_size()
1212 // but we use a copy from proposed in case config changes from the call.
1213 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001214 status_t result = dev->getInputBufferSize(&config, &frames);
1215 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001216 break; // hal success, config is the result
1217 }
1218 // change one parameter of the configuration each iteration to a more "common" value
1219 // to see if the device will support it.
1220 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1221 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1222 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1223 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1224 } else {
1225 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1226 "format %#x, channelMask 0x%X",
1227 sampleRate, format, channelMask);
1228 break; // retries failed, break out of loop with frames == 0.
1229 }
1230 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001231 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001232 if (frames > 0 && config.sample_rate != sampleRate) {
1233 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1234 }
1235 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236}
1237
Glenn Kasten5f972c02014-01-13 09:59:31 -08001238uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001239{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001240 Mutex::Autolock _l(mLock);
1241
1242 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1243 if (recordThread != NULL) {
1244 return recordThread->getInputFramesLost();
1245 }
1246 return 0;
1247}
1248
1249status_t AudioFlinger::setVoiceVolume(float value)
1250{
Eric Laurenta1884f92011-08-23 08:25:03 -07001251 status_t ret = initCheck();
1252 if (ret != NO_ERROR) {
1253 return ret;
1254 }
1255
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256 // check calling permissions
1257 if (!settingsAllowed()) {
1258 return PERMISSION_DENIED;
1259 }
1260
1261 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001262 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001263 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001264 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265 mHardwareStatus = AUDIO_HW_IDLE;
1266
1267 return ret;
1268}
1269
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001270status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001271 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001273 Mutex::Autolock _l(mLock);
1274
1275 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1276 if (playbackThread != NULL) {
1277 return playbackThread->getRenderPosition(halFrames, dspFrames);
1278 }
1279
1280 return BAD_VALUE;
1281}
1282
1283void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1284{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001285 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001286 if (client == 0) {
1287 return;
1288 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001289 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001290 {
1291 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001292 if (mNotificationClients.indexOfKey(pid) < 0) {
1293 sp<NotificationClient> notificationClient = new NotificationClient(this,
1294 client,
1295 pid);
1296 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001297
Eric Laurent021cf962014-05-13 10:18:14 -07001298 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001299
Marco Nelissen06b46062014-11-14 07:58:25 -08001300 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001301 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001302 }
1303 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001304
Eric Laurent021cf962014-05-13 10:18:14 -07001305 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001306 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001307 // the config change is always sent from playback or record threads to avoid deadlock
1308 // with AudioSystem::gLock
1309 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1310 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1311 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001313 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1314 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001315 }
1316}
1317
1318void AudioFlinger::removeNotificationClient(pid_t pid)
1319{
1320 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001321 {
1322 Mutex::Autolock _cl(mClientLock);
1323 mNotificationClients.removeItem(pid);
1324 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001325
Steve Block3856b092011-10-20 11:56:00 +01001326 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001327 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001328 bool removed = false;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001329 for (size_t i = 0; i < num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001330 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001331 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001332 if (ref->mPid == pid) {
1333 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001334 mAudioSessionRefs.removeAt(i);
1335 delete ref;
1336 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001337 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001338 } else {
1339 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001340 }
1341 }
1342 if (removed) {
1343 purgeStaleEffects_l();
1344 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001345}
1346
Eric Laurent73e26b62015-04-27 16:55:58 -07001347void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001348 const sp<AudioIoDescriptor>& ioDesc,
1349 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350{
Eric Laurent021cf962014-05-13 10:18:14 -07001351 Mutex::Autolock _l(mClientLock);
1352 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001353 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001354 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1355 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1356 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001357 }
1358}
1359
Eric Laurent021cf962014-05-13 10:18:14 -07001360// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001361void AudioFlinger::removeClient_l(pid_t pid)
1362{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001363 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001364 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365 mClients.removeItem(pid);
1366}
1367
Eric Laurent717e1282012-06-29 16:36:52 -07001368// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001369sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1370 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001371{
1372 sp<PlaybackThread> thread;
1373
1374 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1375 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1376 ALOG_ASSERT(thread == 0);
1377 thread = mPlaybackThreads.valueAt(i);
1378 }
1379 }
1380
1381 return thread;
1382}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001383
Mathias Agopian65ab4712010-07-14 17:59:35 -07001384
Mathias Agopian65ab4712010-07-14 17:59:35 -07001385
1386// ----------------------------------------------------------------------------
1387
1388AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1389 : RefBase(),
1390 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001391 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001392{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301393 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1394 heapSize *= 1024;
1395 if (!heapSize) {
1396 heapSize = kClientSharedHeapSizeBytes;
1397 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1398 // invalidated tracks
1399 if (!audioFlinger->isLowRamDevice()) {
1400 heapSize *= kClientSharedHeapSizeMultiplier;
1401 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001402 }
1403 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404}
1405
Eric Laurent021cf962014-05-13 10:18:14 -07001406// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407AudioFlinger::Client::~Client()
1408{
1409 mAudioFlinger->removeClient_l(mPid);
1410}
1411
Glenn Kasten435dbe62012-01-30 10:15:48 -08001412sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413{
1414 return mMemoryDealer;
1415}
1416
1417// ----------------------------------------------------------------------------
1418
1419AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1420 const sp<IAudioFlingerClient>& client,
1421 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001422 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001423{
1424}
1425
1426AudioFlinger::NotificationClient::~NotificationClient()
1427{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428}
1429
Glenn Kasten0f11b512014-01-31 16:18:54 -08001430void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001431{
1432 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001433 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001434}
1435
Mathias Agopian65ab4712010-07-14 17:59:35 -07001436
1437// ----------------------------------------------------------------------------
1438
1439sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001440 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001441 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001442 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001443 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001444 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001445 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -07001446 audio_input_flags_t *flags,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001447 pid_t pid,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001448 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001449 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001450 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001451 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001452 sp<IMemory>& cblk,
1453 sp<IMemory>& buffers,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001454 status_t *status,
1455 audio_port_handle_t portId)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456{
1457 sp<RecordThread::RecordTrack> recordTrack;
1458 sp<RecordHandle> recordHandle;
1459 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001460 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001461 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001462
Glenn Kastend776ac62014-05-07 09:16:09 -07001463 cblk.clear();
1464 buffers.clear();
1465
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001466 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001467 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1468 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001469 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001470 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1471 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001472 updatePid = true;
1473 }
1474
1475 if (updatePid) {
1476 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
1477 ALOGW_IF(pid != -1 && pid != callingPid,
1478 "%s uid %d pid %d tried to pass itself off as pid %d",
1479 __func__, callingUid, callingPid, pid);
1480 pid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001481 }
1482
Mathias Agopian65ab4712010-07-14 17:59:35 -07001483 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001484 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001485 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001486 lStatus = PERMISSION_DENIED;
1487 goto Exit;
1488 }
1489
Glenn Kasten53b5d092014-02-05 10:00:23 -08001490 // further sample rate checks are performed by createRecordTrack_l()
1491 if (sampleRate == 0) {
1492 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1493 lStatus = BAD_VALUE;
1494 goto Exit;
1495 }
1496
Andy Hung6770c6f2015-04-07 13:43:36 -07001497 // we don't yet support anything other than linear PCM
1498 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001499 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001500 lStatus = BAD_VALUE;
1501 goto Exit;
1502 }
1503
Glenn Kasten53b5d092014-02-05 10:00:23 -08001504 // further channel mask checks are performed by createRecordTrack_l()
1505 if (!audio_is_input_channel(channelMask)) {
1506 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1507 lStatus = BAD_VALUE;
1508 goto Exit;
1509 }
1510
Glenn Kasten05997e22014-03-13 15:08:33 -07001511 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001513 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001514 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001515 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516 lStatus = BAD_VALUE;
1517 goto Exit;
1518 }
1519
Eric Laurent021cf962014-05-13 10:18:14 -07001520 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001522 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001523 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1524 lStatus = BAD_VALUE;
1525 goto Exit;
1526 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527 lSessionId = *sessionId;
1528 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001529 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001530 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001531 if (sessionId != NULL) {
1532 *sessionId = lSessionId;
1533 }
1534 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001535 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001536
Glenn Kasten1879fff2012-07-11 15:36:59 -07001537 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001538 frameCount, lSessionId, notificationFrames,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001539 clientUid, flags, tid, &lStatus, portId);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001540 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001541
1542 if (lStatus == NO_ERROR) {
1543 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1544 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001545 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001546 if (chain != 0) {
1547 Mutex::Autolock _l(thread->mLock);
1548 thread->addEffectChain_l(chain);
1549 }
1550 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551 }
Glenn Kastene198c362013-08-13 09:13:36 -07001552
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001553 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001554 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001555 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001556 // Don't hold mClientLock when releasing the reference on the track as the
1557 // destructor will acquire it.
1558 {
1559 Mutex::Autolock _cl(mClientLock);
1560 client.clear();
1561 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563 goto Exit;
1564 }
1565
Glenn Kastend776ac62014-05-07 09:16:09 -07001566 cblk = recordTrack->getCblk();
1567 buffers = recordTrack->getBuffers();
1568
Glenn Kasten2fc14732013-08-05 14:58:14 -07001569 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001570 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001571
1572Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001573 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001574 return recordHandle;
1575}
1576
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001577
1578
Mathias Agopian65ab4712010-07-14 17:59:35 -07001579// ----------------------------------------------------------------------------
1580
Eric Laurenta4c5a552012-03-29 10:12:40 -07001581audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1582{
Eric Laurent44622db2014-08-01 19:00:33 -07001583 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001584 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001585 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001586 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001587 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001588 }
1589 Mutex::Autolock _l(mLock);
1590 return loadHwModule_l(name);
1591}
1592
1593// loadHwModule_l() must be called with AudioFlinger::mLock held
1594audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1595{
1596 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1597 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1598 ALOGW("loadHwModule() module %s already loaded", name);
1599 return mAudioHwDevs.keyAt(i);
1600 }
1601 }
1602
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001603 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001604
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001605 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001606 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001607 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001608 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001609 }
1610
1611 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001612 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07001613 mHardwareStatus = AUDIO_HW_IDLE;
1614 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001615 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001616 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001617 }
1618
John Grossmanee578c02012-07-23 17:05:46 -07001619 // Check and cache this HAL's level of support for master mute and master
1620 // volume. If this is the first HAL opened, and it supports the get
1621 // methods, use the initial values provided by the HAL as the current
1622 // master mute and volume settings.
1623
1624 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1625 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001626 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001627
1628 if (0 == mAudioHwDevs.size()) {
1629 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001630 float mv;
1631 if (OK == dev->getMasterVolume(&mv)) {
1632 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07001633 }
1634
1635 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001636 bool mm;
1637 if (OK == dev->getMasterMute(&mm)) {
1638 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07001639 }
1640 }
1641
Eric Laurenta4c5a552012-03-29 10:12:40 -07001642 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001643 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07001644 flags = static_cast<AudioHwDevice::Flags>(flags |
1645 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1646 }
1647
1648 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001649 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07001650 flags = static_cast<AudioHwDevice::Flags>(flags |
1651 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1652 }
1653
Eric Laurenta4c5a552012-03-29 10:12:40 -07001654 mHardwareStatus = AUDIO_HW_IDLE;
1655 }
1656
Glenn Kastena13cde92016-03-28 15:26:02 -07001657 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001658 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001659
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001660 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001661
1662 return handle;
1663
1664}
1665
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001666// ----------------------------------------------------------------------------
1667
Glenn Kasten3b16c762012-11-14 08:44:39 -08001668uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
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->sampleRate() : 0;
1673}
1674
Glenn Kastene33054e2012-11-14 12:54:39 -08001675size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001676{
1677 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001678 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001679 return thread != NULL ? thread->frameCountHAL() : 0;
1680}
1681
1682// ----------------------------------------------------------------------------
1683
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001684status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1685{
1686 uid_t uid = IPCThreadState::self()->getCallingUid();
1687 if (uid != AID_SYSTEM) {
1688 return PERMISSION_DENIED;
1689 }
1690 Mutex::Autolock _l(mLock);
1691 if (mIsDeviceTypeKnown) {
1692 return INVALID_OPERATION;
1693 }
1694 mIsLowRamDevice = isLowRamDevice;
1695 mIsDeviceTypeKnown = true;
1696 return NO_ERROR;
1697}
1698
Eric Laurent93c3d412014-08-01 14:48:35 -07001699audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1700{
1701 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001702
1703 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1704 if (index >= 0) {
1705 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1706 mHwAvSyncIds.valueAt(index), sessionId);
1707 return mHwAvSyncIds.valueAt(index);
1708 }
1709
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001710 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07001711 if (dev == NULL) {
1712 return AUDIO_HW_SYNC_INVALID;
1713 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001714 String8 reply;
1715 AudioParameter param;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001716 if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001717 param = AudioParameter(reply);
1718 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001719
1720 int value;
Mikhail Naganov00260b52016-10-13 12:54:24 -07001721 if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001722 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1723 return AUDIO_HW_SYNC_INVALID;
1724 }
1725
1726 // allow only one session for a given HW A/V sync ID.
1727 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1728 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1729 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1730 value, mHwAvSyncIds.keyAt(i));
1731 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001732 break;
1733 }
1734 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001735
1736 mHwAvSyncIds.add(sessionId, value);
1737
1738 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1739 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1740 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07001741 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001742 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001743 param.addInt(String8(AudioParameter::keyStreamHwAvSync), value);
Eric Laurentfa90e842014-10-17 18:12:31 -07001744 thread->setParameters(param.toString());
1745 break;
1746 }
1747 }
1748
1749 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1750 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001751}
1752
Eric Laurent72e3f392015-05-20 14:43:50 -07001753status_t AudioFlinger::systemReady()
1754{
1755 Mutex::Autolock _l(mLock);
1756 ALOGI("%s", __FUNCTION__);
1757 if (mSystemReady) {
1758 ALOGW("%s called twice", __FUNCTION__);
1759 return NO_ERROR;
1760 }
1761 mSystemReady = true;
1762 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1763 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1764 thread->systemReady();
1765 }
1766 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1767 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1768 thread->systemReady();
1769 }
1770 return NO_ERROR;
1771}
1772
Eric Laurentfa90e842014-10-17 18:12:31 -07001773// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1774void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1775{
1776 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1777 if (index >= 0) {
1778 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1779 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1780 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -07001781 param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId);
Eric Laurentfa90e842014-10-17 18:12:31 -07001782 thread->setParameters(param.toString());
1783 }
1784}
1785
1786
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001787// ----------------------------------------------------------------------------
1788
Eric Laurent83b88082014-06-20 18:31:16 -07001789
1790sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001791 audio_io_handle_t *output,
1792 audio_config_t *config,
1793 audio_devices_t devices,
1794 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001795 audio_output_flags_t flags)
1796{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001797 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001798 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001799 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001800 }
1801
Eric Laurentcf2c0212014-07-25 16:20:43 -07001802 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001803 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1804 } else {
1805 // Audio Policy does not currently request a specific output handle.
1806 // If this is ever needed, see openInput_l() for example code.
1807 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1808 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001809 }
Eric Laurent83b88082014-06-20 18:31:16 -07001810
1811 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1812
Eric Laurent83b88082014-06-20 18:31:16 -07001813 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001814 // This if statement allows overriding the audio policy settings
1815 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1816 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1817 // Check only for Normal Mixing mode
1818 if (kEnableExtendedPrecision) {
1819 // Specify format (uncomment one below to choose)
1820 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1821 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1822 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1823 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001824 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001825 }
1826 if (kEnableExtendedChannels) {
1827 // Specify channel mask (uncomment one below to choose)
1828 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1829 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1830 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1831 }
Eric Laurent83b88082014-06-20 18:31:16 -07001832 }
1833
Phil Burk062e67a2015-02-11 13:40:50 -08001834 AudioStreamOut *outputStream = NULL;
1835 status_t status = outHwDev->openOutputStream(
1836 &outputStream,
1837 *output,
1838 devices,
1839 flags,
1840 config,
1841 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001842
1843 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001844
Phil Burk062e67a2015-02-11 13:40:50 -08001845 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001846
1847 PlaybackThread *thread;
1848 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurente93cc032016-05-05 10:15:10 -07001849 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001850 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001851 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1852 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001853 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001854 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001855 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001856 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001857 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001858 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001859 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001860 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001861 return thread;
1862 }
1863
1864 return 0;
1865}
1866
Eric Laurentcf2c0212014-07-25 16:20:43 -07001867status_t AudioFlinger::openOutput(audio_module_handle_t module,
1868 audio_io_handle_t *output,
1869 audio_config_t *config,
1870 audio_devices_t *devices,
1871 const String8& address,
1872 uint32_t *latencyMs,
1873 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001874{
Phil Burk062e67a2015-02-11 13:40:50 -08001875 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001876 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001877 (devices != NULL) ? *devices : 0,
1878 config->sample_rate,
1879 config->format,
1880 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001881 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001882
Yunlian Jiang32ba9862017-01-31 15:55:00 -08001883 if (devices == NULL || *devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001884 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001886
Mathias Agopian65ab4712010-07-14 17:59:35 -07001887 Mutex::Autolock _l(mLock);
1888
Eric Laurentcf2c0212014-07-25 16:20:43 -07001889 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001890 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001891 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001892
1893 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001894 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001895
1896 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001897 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001898 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001899 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001900
1901 AutoMutex lock(mHardwareLock);
1902 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001903 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001904 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001905 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001906 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001907 }
1908
Eric Laurentcf2c0212014-07-25 16:20:43 -07001909 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001910}
1911
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001912audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1913 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001914{
1915 Mutex::Autolock _l(mLock);
1916 MixerThread *thread1 = checkMixerThread_l(output1);
1917 MixerThread *thread2 = checkMixerThread_l(output2);
1918
1919 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001920 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1921 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001922 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923 }
1924
Glenn Kasteneeecb982016-02-26 10:44:04 -08001925 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07001926 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001927 thread->addOutputTrack(thread2);
1928 mPlaybackThreads.add(id, thread);
1929 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001930 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001931 return id;
1932}
1933
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001934status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935{
Glenn Kastend96c5722012-04-25 13:44:49 -07001936 return closeOutput_nonvirtual(output);
1937}
1938
1939status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1940{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941 // keep strong reference on the playback thread so that
1942 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001943 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944 {
1945 Mutex::Autolock _l(mLock);
1946 thread = checkPlaybackThread_l(output);
1947 if (thread == NULL) {
1948 return BAD_VALUE;
1949 }
1950
Steve Block3856b092011-10-20 11:56:00 +01001951 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001952
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001953 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001954 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001955 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001956 DuplicatingThread *dupThread =
1957 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001958 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001959 }
1960 }
1961 }
1962
1963
1964 mPlaybackThreads.removeItem(output);
1965 // save all effects to the default thread
1966 if (mPlaybackThreads.size()) {
1967 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1968 if (dstThread != NULL) {
1969 // audioflinger lock is held here so the acquisition order of thread locks does not
1970 // matter
1971 Mutex::Autolock _dl(dstThread->mLock);
1972 Mutex::Autolock _sl(thread->mLock);
1973 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1974 for (size_t i = 0; i < effectChains.size(); i ++) {
1975 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001976 }
1977 }
1978 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001979 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1980 ioDesc->mIoHandle = output;
1981 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001982 }
1983 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001984 // The thread entity (active unit of execution) is no longer running here,
1985 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001986
Eric Laurentf6870ae2015-05-08 10:50:03 -07001987 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001988 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001989 }
Eric Laurent83b88082014-06-20 18:31:16 -07001990
Mathias Agopian65ab4712010-07-14 17:59:35 -07001991 return NO_ERROR;
1992}
1993
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001994void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07001995{
1996 AudioStreamOut *out = thread->clearOutput();
1997 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1998 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07001999 delete out;
2000}
2001
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002002void AudioFlinger::closeOutputInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002003{
2004 mPlaybackThreads.removeItem(thread->mId);
2005 thread->exit();
2006 closeOutputFinish(thread);
2007}
2008
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002009status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002010{
2011 Mutex::Autolock _l(mLock);
2012 PlaybackThread *thread = checkPlaybackThread_l(output);
2013
2014 if (thread == NULL) {
2015 return BAD_VALUE;
2016 }
2017
Steve Block3856b092011-10-20 11:56:00 +01002018 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002019 thread->suspend();
2020
2021 return NO_ERROR;
2022}
2023
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002024status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002025{
2026 Mutex::Autolock _l(mLock);
2027 PlaybackThread *thread = checkPlaybackThread_l(output);
2028
2029 if (thread == NULL) {
2030 return BAD_VALUE;
2031 }
2032
Steve Block3856b092011-10-20 11:56:00 +01002033 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002034
2035 thread->restore();
2036
2037 return NO_ERROR;
2038}
2039
Eric Laurentcf2c0212014-07-25 16:20:43 -07002040status_t AudioFlinger::openInput(audio_module_handle_t module,
2041 audio_io_handle_t *input,
2042 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002043 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002044 const String8& address,
2045 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002046 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002047{
Eric Laurent83b88082014-06-20 18:31:16 -07002048 Mutex::Autolock _l(mLock);
2049
Glenn Kastene7d66712015-03-05 13:46:52 -08002050 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002051 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002052 }
2053
Glenn Kastene7d66712015-03-05 13:46:52 -08002054 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002055
2056 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002057 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002058 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002059 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002060 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002061 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002062}
Dima Zavin799a70e2011-04-18 16:57:27 -07002063
Eric Laurent83b88082014-06-20 18:31:16 -07002064sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002065 audio_io_handle_t *input,
2066 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002067 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002068 const String8& address,
2069 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002070 audio_input_flags_t flags)
2071{
Glenn Kastene7d66712015-03-05 13:46:52 -08002072 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002073 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002074 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002075 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002076 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002077
Glenn Kasteneeecb982016-02-26 10:44:04 -08002078 // Audio Policy can request a specific handle for hardware hotword.
2079 // The goal here is not to re-open an already opened input.
2080 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002081 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002082 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2083 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2084 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2085 return 0;
2086 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2087 // This should not happen in a transient state with current design.
2088 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2089 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002090 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002091
Eric Laurentcf2c0212014-07-25 16:20:43 -07002092 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002093 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002094 sp<StreamInHalInterface> inStream;
2095 status_t status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002096 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Mikhail Naganovf558e022016-11-14 17:45:17 -08002097 ALOGV("openInput_l() openInputStream returned input %p, devices %x, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002098 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002099 inStream.get(),
Mikhail Naganovf558e022016-11-14 17:45:17 -08002100 devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002101 halconfig.sample_rate,
2102 halconfig.format,
2103 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002104 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002105 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002106
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002107 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002108 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002109 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002110 audio_is_linear_pcm(config->format) &&
2111 audio_is_linear_pcm(halconfig.format) &&
2112 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002113 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2114 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002115 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002116 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002117 inStream.clear();
2118 status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002119 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002120 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002121 }
2122
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002123 if (status == NO_ERROR && inStream != 0) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002124
Glenn Kasten46909e72013-02-26 09:20:22 -08002125#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002126 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2127 // or (re-)create if current Pipe is idle and does not match the new format
2128 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002129 enum {
2130 TEE_SINK_NO, // don't copy input
2131 TEE_SINK_NEW, // copy input using a new pipe
2132 TEE_SINK_OLD, // copy input using an existing pipe
2133 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002134 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2135 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002136 if (!mTeeSinkInputEnabled) {
2137 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002138 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002139 kind = TEE_SINK_NO;
2140 } else if (mRecordTeeSink == 0) {
2141 kind = TEE_SINK_NEW;
2142 } else if (mRecordTeeSink->getStrongCount() != 1) {
2143 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002144 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002145 kind = TEE_SINK_OLD;
2146 } else {
2147 kind = TEE_SINK_NEW;
2148 }
2149 switch (kind) {
2150 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002151 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002152 size_t numCounterOffers = 0;
2153 const NBAIO_Format offers[1] = {format};
2154 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2155 ALOG_ASSERT(index == 0);
2156 PipeReader *pipeReader = new PipeReader(*pipe);
2157 numCounterOffers = 0;
2158 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2159 ALOG_ASSERT(index == 0);
2160 mRecordTeeSink = pipe;
2161 mRecordTeeSource = pipeReader;
2162 teeSink = pipe;
2163 }
2164 break;
2165 case TEE_SINK_OLD:
2166 teeSink = mRecordTeeSink;
2167 break;
2168 case TEE_SINK_NO:
2169 default:
2170 break;
2171 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002172#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002173
Eric Laurent05067782016-06-01 18:27:28 -07002174 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07002175
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002176 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002177 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002178 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002179 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002180 inputStream,
2181 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002182 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002183 devices,
2184 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002185#ifdef TEE_SINK
2186 , teeSink
2187#endif
2188 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002189 mRecordThreads.add(*input, thread);
2190 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002191 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192 }
2193
Eric Laurentcf2c0212014-07-25 16:20:43 -07002194 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002195 return 0;
2196}
2197
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002198status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002199{
Glenn Kastend96c5722012-04-25 13:44:49 -07002200 return closeInput_nonvirtual(input);
2201}
2202
2203status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2204{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002205 // keep strong reference on the record thread so that
2206 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002207 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002208 {
2209 Mutex::Autolock _l(mLock);
2210 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002211 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002212 return BAD_VALUE;
2213 }
2214
Steve Block3856b092011-10-20 11:56:00 +01002215 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002216
2217 // If we still have effect chains, it means that a client still holds a handle
2218 // on at least one effect. We must either move the chain to an existing thread with the
2219 // same session ID or put it aside in case a new record thread is opened for a
2220 // new capture on the same session
2221 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002222 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002223 Mutex::Autolock _sl(thread->mLock);
2224 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002225 // Note: maximum one chain per record thread
2226 if (effectChains.size() != 0) {
2227 chain = effectChains[0];
2228 }
2229 }
2230 if (chain != 0) {
2231 // first check if a record thread is already opened with a client on the same session.
2232 // This should only happen in case of overlap between one thread tear down and the
2233 // creation of its replacement
2234 size_t i;
2235 for (i = 0; i < mRecordThreads.size(); i++) {
2236 sp<RecordThread> t = mRecordThreads.valueAt(i);
2237 if (t == thread) {
2238 continue;
2239 }
2240 if (t->hasAudioSession(chain->sessionId()) != 0) {
2241 Mutex::Autolock _l(t->mLock);
2242 ALOGV("closeInput() found thread %d for effect session %d",
2243 t->id(), chain->sessionId());
2244 t->addEffectChain_l(chain);
2245 break;
2246 }
2247 }
2248 // put the chain aside if we could not find a record thread with the same session id.
2249 if (i == mRecordThreads.size()) {
2250 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002251 }
2252 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002253 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2254 ioDesc->mIoHandle = input;
2255 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002256 mRecordThreads.removeItem(input);
2257 }
Eric Laurent83b88082014-06-20 18:31:16 -07002258 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2259 // we have a different lock for notification client
2260 closeInputFinish(thread);
2261 return NO_ERROR;
2262}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002263
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002264void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002265{
2266 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002267 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002268 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002269 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002270 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002271}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002272
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002273void AudioFlinger::closeInputInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002274{
2275 mRecordThreads.removeItem(thread->mId);
2276 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002277}
2278
Glenn Kastend2304db2014-02-03 07:40:31 -08002279status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002280{
2281 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002282 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002283
2284 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2285 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002286 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002287 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002288
2289 return NO_ERROR;
2290}
2291
2292
Glenn Kasteneeecb982016-02-26 10:44:04 -08002293audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002294{
Glenn Kasten9d003132016-04-06 14:38:09 -07002295 // This is a binder API, so a malicious client could pass in a bad parameter.
2296 // Check for that before calling the internal API nextUniqueId().
2297 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2298 ALOGE("newAudioUniqueId invalid use %d", use);
2299 return AUDIO_UNIQUE_ID_ALLOCATE;
2300 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002301 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002302}
2303
Glenn Kastend848eb42016-03-08 13:42:11 -08002304void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002305{
2306 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002307 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002308 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2309 if (pid != -1 && (caller == getpid_cached)) {
2310 caller = pid;
2311 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002312
Eric Laurent021cf962014-05-13 10:18:14 -07002313 {
2314 Mutex::Autolock _cl(mClientLock);
2315 // Ignore requests received from processes not known as notification client. The request
2316 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2317 // called from a different pid leaving a stale session reference. Also we don't know how
2318 // to clear this reference if the client process dies.
2319 if (mNotificationClients.indexOfKey(caller) < 0) {
2320 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2321 return;
2322 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002323 }
2324
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002325 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002326 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002327 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002328 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2329 ref->mCnt++;
2330 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002331 return;
2332 }
2333 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002334 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2335 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002336}
2337
Glenn Kastend848eb42016-03-08 13:42:11 -08002338void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002339{
2340 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002341 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002342 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2343 if (pid != -1 && (caller == getpid_cached)) {
2344 caller = pid;
2345 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002346 size_t num = mAudioSessionRefs.size();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002347 for (size_t i = 0; i < num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002348 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002349 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2350 ref->mCnt--;
2351 ALOGV(" decremented refcount to %d", ref->mCnt);
2352 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002353 mAudioSessionRefs.removeAt(i);
2354 delete ref;
2355 purgeStaleEffects_l();
2356 }
2357 return;
2358 }
2359 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002360 // If the caller is mediaserver it is likely that the session being released was acquired
2361 // on behalf of a process not in notification clients and we ignore the warning.
2362 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002363}
2364
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002365bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
2366{
2367 size_t num = mAudioSessionRefs.size();
2368 for (size_t i = 0; i < num; i++) {
2369 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
2370 if (ref->mSessionid == audioSession) {
2371 return true;
2372 }
2373 }
2374 return false;
2375}
2376
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002377void AudioFlinger::purgeStaleEffects_l() {
2378
Steve Block3856b092011-10-20 11:56:00 +01002379 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002380
2381 Vector< sp<EffectChain> > chains;
2382
2383 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2384 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2385 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2386 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002387 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2388 chains.push(ec);
2389 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002390 }
2391 }
2392 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2393 sp<RecordThread> t = mRecordThreads.valueAt(i);
2394 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2395 sp<EffectChain> ec = t->mEffectChains[j];
2396 chains.push(ec);
2397 }
2398 }
2399
2400 for (size_t i = 0; i < chains.size(); i++) {
2401 sp<EffectChain> ec = chains[i];
2402 int sessionid = ec->sessionId();
2403 sp<ThreadBase> t = ec->mThread.promote();
2404 if (t == 0) {
2405 continue;
2406 }
2407 size_t numsessionrefs = mAudioSessionRefs.size();
2408 bool found = false;
2409 for (size_t k = 0; k < numsessionrefs; k++) {
2410 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002411 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002412 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002413 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002414 found = true;
2415 break;
2416 }
2417 }
2418 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002419 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002420 // remove all effects from the chain
2421 while (ec->mEffects.size()) {
2422 sp<EffectModule> effect = ec->mEffects[0];
2423 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002424 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002425 if (effect->purgeHandles()) {
2426 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002427 }
2428 AudioSystem::unregisterEffect(effect->id());
2429 }
2430 }
2431 }
2432 return;
2433}
2434
Glenn Kasteneeecb982016-02-26 10:44:04 -08002435// checkThread_l() must be called with AudioFlinger::mLock held
2436AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2437{
2438 ThreadBase *thread = NULL;
2439 switch (audio_unique_id_get_use(ioHandle)) {
2440 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2441 thread = checkPlaybackThread_l(ioHandle);
2442 break;
2443 case AUDIO_UNIQUE_ID_USE_INPUT:
2444 thread = checkRecordThread_l(ioHandle);
2445 break;
2446 default:
2447 break;
2448 }
2449 return thread;
2450}
2451
Mathias Agopian65ab4712010-07-14 17:59:35 -07002452// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002453AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002454{
Glenn Kastena1117922012-01-26 10:53:32 -08002455 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002456}
2457
2458// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002459AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002460{
2461 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002462 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002463}
2464
2465// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002466AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002467{
Glenn Kastena1117922012-01-26 10:53:32 -08002468 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002469}
2470
Glenn Kasteneeecb982016-02-26 10:44:04 -08002471audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002472{
Glenn Kasten9d003132016-04-06 14:38:09 -07002473 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002474 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002475 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2476 for (int retry = 0; retry < maxRetries; retry++) {
2477 // The cast allows wraparound from max positive to min negative instead of abort
2478 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2479 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2480 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2481 // allow wrap by skipping 0 and -1 for session ids
2482 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2483 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2484 return (audio_unique_id_t) (base | use);
2485 }
2486 }
2487 // We have no way of recovering from wraparound
2488 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2489 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002490}
2491
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002492AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002493{
2494 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2495 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002496 if(thread->isDuplicating()) {
2497 continue;
2498 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002499 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002500 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002501 return thread;
2502 }
2503 }
2504 return NULL;
2505}
2506
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002507audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002508{
2509 PlaybackThread *thread = primaryPlaybackThread_l();
2510
2511 if (thread == NULL) {
2512 return 0;
2513 }
2514
Eric Laurentf1c04f92012-08-28 14:26:53 -07002515 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002516}
2517
Glenn Kastena7335632016-06-09 17:09:53 -07002518AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
2519{
2520 size_t minFrameCount = 0;
2521 PlaybackThread *minThread = NULL;
2522 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2523 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2524 if (!thread->isDuplicating()) {
2525 size_t frameCount = thread->frameCountHAL();
2526 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
2527 (frameCount == minFrameCount && thread->hasFastMixer() &&
2528 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
2529 minFrameCount = frameCount;
2530 minThread = thread;
2531 }
2532 }
2533 }
2534 return minThread;
2535}
2536
Eric Laurenta011e352012-03-29 15:51:43 -07002537sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002538 audio_session_t triggerSession,
2539 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002540 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002541 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002542{
2543 Mutex::Autolock _l(mLock);
2544
2545 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2546 status_t playStatus = NAME_NOT_FOUND;
2547 status_t recStatus = NAME_NOT_FOUND;
2548 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2549 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2550 if (playStatus == NO_ERROR) {
2551 return event;
2552 }
2553 }
2554 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2555 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2556 if (recStatus == NO_ERROR) {
2557 return event;
2558 }
2559 }
2560 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2561 mPendingSyncEvents.add(event);
2562 } else {
2563 ALOGV("createSyncEvent() invalid event %d", event->type());
2564 event.clear();
2565 }
2566 return event;
2567}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002568
Mathias Agopian65ab4712010-07-14 17:59:35 -07002569// ----------------------------------------------------------------------------
2570// Effect management
2571// ----------------------------------------------------------------------------
2572
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002573sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
2574 return mEffectsFactoryHal;
2575}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002576
Glenn Kastenf587ba52012-01-26 16:25:10 -08002577status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002578{
2579 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002580 if (mEffectsFactoryHal.get()) {
2581 return mEffectsFactoryHal->queryNumberEffects(numEffects);
2582 } else {
2583 return -ENODEV;
2584 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002585}
2586
Glenn Kastenf587ba52012-01-26 16:25:10 -08002587status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002588{
2589 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002590 if (mEffectsFactoryHal.get()) {
2591 return mEffectsFactoryHal->getDescriptor(index, descriptor);
2592 } else {
2593 return -ENODEV;
2594 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002595}
2596
Glenn Kasten5e92a782012-01-30 07:40:52 -08002597status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002598 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002599{
2600 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002601 if (mEffectsFactoryHal.get()) {
2602 return mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
2603 } else {
2604 return -ENODEV;
2605 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002606}
2607
2608
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002609sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002610 effect_descriptor_t *pDesc,
2611 const sp<IEffectClient>& effectClient,
2612 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002613 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002614 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002615 const String16& opPackageName,
Eric Laurentb6436272016-12-07 19:24:50 -08002616 pid_t pid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002617 status_t *status,
2618 int *id,
2619 int *enabled)
2620{
2621 status_t lStatus = NO_ERROR;
2622 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002623 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002624
Eric Laurentb6436272016-12-07 19:24:50 -08002625 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
2626 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
2627 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
2628 ALOGW_IF(pid != -1 && pid != callingPid,
2629 "%s uid %d pid %d tried to pass itself off as pid %d",
2630 __func__, callingUid, callingPid, pid);
2631 pid = callingPid;
2632 }
2633
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002634 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
2635 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002636
2637 if (pDesc == NULL) {
2638 lStatus = BAD_VALUE;
2639 goto Exit;
2640 }
2641
Eric Laurent84e9a102010-09-23 16:10:16 -07002642 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002643 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002644 lStatus = PERMISSION_DENIED;
2645 goto Exit;
2646 }
2647
Dima Zavinfce7a472011-04-19 22:30:36 -07002648 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002649 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002650 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002651 lStatus = PERMISSION_DENIED;
2652 goto Exit;
2653 }
2654
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002655 if (mEffectsFactoryHal == 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002656 lStatus = NO_INIT;
2657 goto Exit;
2658 }
2659
Mathias Agopian65ab4712010-07-14 17:59:35 -07002660 {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002661 if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002662 // if uuid is specified, request effect descriptor
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002663 lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002664 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002665 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002666 goto Exit;
2667 }
2668 } else {
2669 // if uuid is not specified, look for an available implementation
2670 // of the required type in effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002671 if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002672 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002673 lStatus = BAD_VALUE;
2674 goto Exit;
2675 }
2676 uint32_t numEffects = 0;
2677 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002678 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002679 bool found = false;
2680
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002681 lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002682 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002683 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002684 goto Exit;
2685 }
2686 for (uint32_t i = 0; i < numEffects; i++) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002687 lStatus = mEffectsFactoryHal->getDescriptor(i, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002688 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002689 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002690 continue;
2691 }
2692 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2693 // If matching type found save effect descriptor. If the session is
2694 // 0 and the effect is not auxiliary, continue enumeration in case
2695 // an auxiliary version of this effect type is available
2696 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002697 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002698 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002699 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2700 break;
2701 }
2702 }
2703 }
2704 if (!found) {
2705 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002706 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002707 goto Exit;
2708 }
2709 // For same effect type, chose auxiliary version over insert version if
2710 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002711 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002712 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002713 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002714 }
2715 }
2716
2717 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002718 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002719 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2720 lStatus = INVALID_OPERATION;
2721 goto Exit;
2722 }
2723
Eric Laurent59255e42011-07-27 19:49:51 -07002724 // check recording permission for visualizer
2725 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002726 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002727 lStatus = PERMISSION_DENIED;
2728 goto Exit;
2729 }
2730
Mathias Agopian65ab4712010-07-14 17:59:35 -07002731 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002732 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002733 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002734 // if the output returned by getOutputForEffect() is removed before we lock the
2735 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2736 // and we will exit safely
2737 io = AudioSystem::getOutputForEffect(&desc);
2738 ALOGV("createEffect got output %d", io);
2739 }
2740
2741 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002742
2743 // If output is not specified try to find a matching audio session ID in one of the
2744 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002745 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2746 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002747 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002748 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002749 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2750 // output must be specified by AudioPolicyManager when using session
2751 // AUDIO_SESSION_OUTPUT_STAGE
2752 lStatus = BAD_VALUE;
2753 goto Exit;
2754 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002755 // look for the thread where the specified audio session is present
2756 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2757 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2758 io = mPlaybackThreads.keyAt(i);
2759 break;
2760 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002761 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002762 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002763 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2764 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2765 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002766 break;
2767 }
2768 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002769 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002770 // If no output thread contains the requested session ID, default to
2771 // first output. The effect chain will be moved to the correct output
2772 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002773 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002774 io = mPlaybackThreads.keyAt(0);
2775 }
Steve Block3856b092011-10-20 11:56:00 +01002776 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002777 }
2778 ThreadBase *thread = checkRecordThread_l(io);
2779 if (thread == NULL) {
2780 thread = checkPlaybackThread_l(io);
2781 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002782 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002783 lStatus = BAD_VALUE;
2784 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002785 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002786 } else {
2787 // Check if one effect chain was awaiting for an effect to be created on this
2788 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08002789 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07002790 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002791 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002792 thread->addEffectChain_l(chain);
2793 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002794 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002795
Eric Laurent021cf962014-05-13 10:18:14 -07002796 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002797
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002798 // create effect on selected output thread
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002799 bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002800 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002801 &desc, enabled, &lStatus, pinned);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002802 if (handle != 0 && id != NULL) {
2803 *id = handle->id();
2804 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002805 if (handle == 0) {
2806 // remove local strong reference to Client with mClientLock held
2807 Mutex::Autolock _cl(mClientLock);
2808 client.clear();
2809 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002810 }
2811
2812Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002813 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002814 return handle;
2815}
2816
Glenn Kastend848eb42016-03-08 13:42:11 -08002817status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002818 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002819{
Steve Block3856b092011-10-20 11:56:00 +01002820 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002821 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002822 Mutex::Autolock _l(mLock);
2823 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002824 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002825 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002826 }
Eric Laurentde070132010-07-13 04:45:46 -07002827 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2828 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002829 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002830 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002831 }
Eric Laurentde070132010-07-13 04:45:46 -07002832 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2833 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002834 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002835 return BAD_VALUE;
2836 }
2837
2838 Mutex::Autolock _dl(dstThread->mLock);
2839 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002840 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002841}
2842
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002843// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08002844status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002845 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002846 AudioFlinger::PlaybackThread *dstThread,
2847 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002848{
Steve Block3856b092011-10-20 11:56:00 +01002849 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002850 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002851
Eric Laurent59255e42011-07-27 19:49:51 -07002852 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002853 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002854 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002855 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002856 return INVALID_OPERATION;
2857 }
2858
Eric Laurent4c415062016-06-17 16:14:16 -07002859 // Check whether the destination thread and all effects in the chain are compatible
2860 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07002861 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07002862 " destination thread %p is not compatible with effects in the chain",
2863 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07002864 return INVALID_OPERATION;
2865 }
2866
Eric Laurent39e94f82010-07-28 01:32:47 -07002867 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002868 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002869 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002870 // removed.
2871 srcThread->removeEffectChain_l(chain);
2872
2873 // transfer all effects one by one so that new effect chain is created on new thread with
2874 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002875 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002876 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002877 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002878 Vector< sp<EffectModule> > removed;
2879 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002880 while (effect != 0) {
2881 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002882 removed.add(effect);
2883 status = dstThread->addEffect_l(effect);
2884 if (status != NO_ERROR) {
2885 break;
2886 }
Eric Laurentec35a142011-10-05 17:42:25 -07002887 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2888 if (effect->state() == EffectModule::ACTIVE ||
2889 effect->state() == EffectModule::STOPPING) {
2890 effect->start();
2891 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002892 // if the move request is not received from audio policy manager, the effect must be
2893 // re-registered with the new strategy and output
2894 if (dstChain == 0) {
2895 dstChain = effect->chain().promote();
2896 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002897 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002898 status = NO_INIT;
2899 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002900 }
2901 strategy = dstChain->strategy();
2902 }
2903 if (reRegister) {
2904 AudioSystem::unregisterEffect(effect->id());
2905 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002906 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002907 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002908 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002909 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002910 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002911 }
Eric Laurentde070132010-07-13 04:45:46 -07002912 effect = chain->getEffectFromId_l(0);
2913 }
2914
Eric Laurent5baf2af2013-09-12 17:37:00 -07002915 if (status != NO_ERROR) {
2916 for (size_t i = 0; i < removed.size(); i++) {
2917 srcThread->addEffect_l(removed[i]);
2918 if (dstChain != 0 && reRegister) {
2919 AudioSystem::unregisterEffect(removed[i]->id());
2920 AudioSystem::registerEffect(&removed[i]->desc(),
2921 srcThread->id(),
2922 strategy,
2923 sessionId,
2924 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002925 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002926 }
2927 }
2928 }
2929
2930 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002931}
2932
Eric Laurent5baf2af2013-09-12 17:37:00 -07002933bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002934{
2935 if (mGlobalEffectEnableTime != 0 &&
2936 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2937 return true;
2938 }
2939
2940 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2941 sp<EffectChain> ec =
2942 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002943 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002944 return true;
2945 }
2946 }
2947 return false;
2948}
2949
Eric Laurent5baf2af2013-09-12 17:37:00 -07002950void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002951{
2952 Mutex::Autolock _l(mLock);
2953
2954 mGlobalEffectEnableTime = systemTime();
2955
2956 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2957 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2958 if (t->mType == ThreadBase::OFFLOAD) {
2959 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2960 }
2961 }
2962
2963}
2964
Eric Laurentaaa44472014-09-12 17:41:50 -07002965status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2966{
Glenn Kastend848eb42016-03-08 13:42:11 -08002967 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002968 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002969 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002970 if (index >= 0) {
2971 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2972 return ALREADY_EXISTS;
2973 }
2974 mOrphanEffectChains.add(session, chain);
2975 return NO_ERROR;
2976}
2977
2978sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2979{
2980 sp<EffectChain> chain;
2981 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002982 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002983 if (index >= 0) {
2984 chain = mOrphanEffectChains.valueAt(index);
2985 mOrphanEffectChains.removeItemsAt(index);
2986 }
2987 return chain;
2988}
2989
2990bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2991{
2992 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08002993 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002994 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002995 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002996 if (index >= 0) {
2997 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002998 if (chain->removeEffect_l(effect, true) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002999 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07003000 mOrphanEffectChains.removeItemsAt(index);
3001 }
3002 return true;
3003 }
3004 return false;
3005}
3006
3007
Glenn Kastenda6ef132013-01-10 12:31:01 -08003008struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003009#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
3010 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003011};
3012
3013int comparEntry(const void *p1, const void *p2)
3014{
Glenn Kasten2f55e762015-03-05 16:05:08 -08003015 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003016}
3017
Glenn Kasten46909e72013-02-26 09:20:22 -08003018#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08003019void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003020{
Eric Laurent81784c32012-11-19 14:55:58 -08003021 NBAIO_Source *teeSource = source.get();
3022 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003023 // .wav rotation
3024 // There is a benign race condition if 2 threads call this simultaneously.
3025 // They would both traverse the directory, but the result would simply be
3026 // failures at unlink() which are ignored. It's also unlikely since
3027 // normally dumpsys is only done by bugreport or from the command line.
3028 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08003029 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08003030 size_t teePathLen = strlen(teePath);
3031 DIR *dir = opendir(teePath);
3032 teePath[teePathLen++] = '/';
3033 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003034#define TEE_MAX_SORT 20 // number of entries to sort
3035#define TEE_MAX_KEEP 10 // number of entries to keep
3036 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003037 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08003038 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003039 struct dirent de;
3040 struct dirent *result = NULL;
3041 int rc = readdir_r(dir, &de, &result);
3042 if (rc != 0) {
3043 ALOGW("readdir_r failed %d", rc);
3044 break;
3045 }
3046 if (result == NULL) {
3047 break;
3048 }
3049 if (result != &de) {
3050 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
3051 break;
3052 }
3053 // ignore non .wav file entries
3054 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003055 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08003056 strcmp(&de.d_name[nameLen - 4], ".wav")) {
3057 continue;
3058 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08003059 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003060 }
3061 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003062 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003063 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003064 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
3065 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003066 (void) unlink(teePath);
3067 }
3068 }
3069 } else {
3070 if (fd >= 0) {
Glenn Kastenfbd87e82016-07-18 15:45:56 -07003071 dprintf(fd, "unable to rotate tees in %.*s: %s\n", (int) teePathLen, teePath,
Glenn Kasten9a003992016-02-23 15:24:34 -08003072 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003073 }
3074 }
Eric Laurent81784c32012-11-19 14:55:58 -08003075 char teeTime[16];
3076 struct timeval tv;
3077 gettimeofday(&tv, NULL);
3078 struct tm tm;
3079 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003080 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
3081 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
3082 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3083 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003084 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003085 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003086 char wavHeader[44];
3087 memcpy(wavHeader,
3088 "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",
3089 sizeof(wavHeader));
3090 NBAIO_Format format = teeSource->format();
3091 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003092 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003093 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003094 wavHeader[22] = channelCount; // number of channels
3095 wavHeader[24] = sampleRate; // sample rate
3096 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003097 wavHeader[32] = frameSize; // block alignment
3098 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003099 write(teeFd, wavHeader, sizeof(wavHeader));
3100 size_t total = 0;
3101 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003102#define TEE_SINK_READ 1024 // frames per I/O operation
3103 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003104 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003105 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003106 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003107 bool wasFirstRead = firstRead;
3108 firstRead = false;
3109 if (actual <= 0) {
3110 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3111 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003112 }
Eric Laurent81784c32012-11-19 14:55:58 -08003113 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003114 }
Eric Laurent81784c32012-11-19 14:55:58 -08003115 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003116 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003117 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003118 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003119 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003120 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003121 uint32_t temp = 44 + total * frameSize - 8;
3122 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003123 write(teeFd, &temp, sizeof(temp));
3124 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003125 temp = total * frameSize;
3126 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003127 write(teeFd, &temp, sizeof(temp));
3128 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003129 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003130 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003131 }
Eric Laurent59255e42011-07-27 19:49:51 -07003132 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003133 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003134 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003135 }
Eric Laurent59255e42011-07-27 19:49:51 -07003136 }
3137 }
3138}
Glenn Kasten46909e72013-02-26 09:20:22 -08003139#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003140
Mathias Agopian65ab4712010-07-14 17:59:35 -07003141// ----------------------------------------------------------------------------
3142
3143status_t AudioFlinger::onTransact(
3144 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3145{
3146 return BnAudioFlinger::onTransact(code, data, reply, flags);
3147}
3148
Glenn Kasten63238ef2015-03-02 15:50:29 -08003149} // namespace android