blob: c85d3778bffbe9973a5e895ccca84a33d3cf0b24 [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>
Andy Hung35fec5f2016-04-13 14:21:48 -070037#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <utils/String16.h>
39#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070040#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070041
Dima Zavinfce7a472011-04-19 22:30:36 -070042#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043#include <cutils/properties.h>
44
Dima Zavin64760242011-05-11 14:15:23 -070045#include <system/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070046
47#include "AudioMixer.h"
48#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080049#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
Andy Hung6770c6f2015-04-07 13:43:36 -070051#include <media/AudioResamplerPublic.h>
52
Eric Laurent6d8b6942011-06-24 07:01:31 -070053#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070054#include <audio_effects/effect_ns.h>
55#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
Glenn Kasten3b21c502011-12-15 09:52:39 -080057#include <audio_utils/primitives.h>
58
Eric Laurentfeb0db62011-07-22 09:04:31 -070059#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080060
Glenn Kasten9e58b552013-01-18 15:09:48 -080061#include <media/IMediaLogService.h>
Andy Hung07b745e2016-05-23 16:21:07 -070062#include <media/MemoryLeakTrackUtil.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080063#include <media/nbaio/Pipe.h>
64#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070065#include <media/AudioParameter.h>
Wei Jia3f273d12015-11-24 09:06:49 -080066#include <mediautils/BatteryNotifier.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070067#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080068
Mathias Agopian65ab4712010-07-14 17:59:35 -070069// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070070
John Grossman1c345192012-03-27 14:00:17 -070071// Note: the following macro is used for extremely verbose logging message. In
72// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
73// 0; but one side effect of this is to turn all LOGV's as well. Some messages
74// are so verbose that we want to suppress them even when we have ALOG_ASSERT
75// turned on. Do not uncomment the #def below unless you really know what you
76// are doing and want to see all of the extremely verbose messages.
77//#define VERY_VERY_VERBOSE_LOGGING
78#ifdef VERY_VERY_VERBOSE_LOGGING
79#define ALOGVV ALOGV
80#else
81#define ALOGVV(a...) do { } while(0)
82#endif
Eric Laurentde070132010-07-13 04:45:46 -070083
Mathias Agopian65ab4712010-07-14 17:59:35 -070084namespace android {
85
Glenn Kastenec1d6b52011-12-12 09:04:45 -080086static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
87static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070088static const char kClientLockedString[] = "Client lock is taken\n";
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070089static const char kNoEffectsFactory[] = "Effects Factory is absent\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070090
Glenn Kasten58912562012-04-03 10:45:00 -070091
John Grossman4ff14ba2012-02-08 16:37:41 -080092nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080093
Eric Laurent81784c32012-11-19 14:55:58 -080094uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070095
Glenn Kasten46909e72013-02-26 09:20:22 -080096#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080097bool AudioFlinger::mTeeSinkInputEnabled = false;
98bool AudioFlinger::mTeeSinkOutputEnabled = false;
99bool AudioFlinger::mTeeSinkTrackEnabled = false;
100
101size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
102size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
103size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800104#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800105
Eric Laurent813e2a72013-08-31 12:59:48 -0700106// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
107// we define a minimum time during which a global effect is considered enabled.
108static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
109
Mathias Agopian65ab4712010-07-14 17:59:35 -0700110// ----------------------------------------------------------------------------
111
Marco Nelissenb2208842014-02-07 14:00:50 -0800112const char *formatToString(audio_format_t format) {
Phil Burkfdb3c072016-02-09 10:47:02 -0800113 switch (audio_get_main_format(format)) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530114 case AUDIO_FORMAT_PCM:
115 switch (format) {
116 case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
117 case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
118 case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
119 case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
120 case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
121 case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
122 default:
123 break;
124 }
125 break;
Marco Nelissenb2208842014-02-07 14:00:50 -0800126 case AUDIO_FORMAT_MP3: return "mp3";
127 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
128 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
129 case AUDIO_FORMAT_AAC: return "aac";
130 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
131 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
132 case AUDIO_FORMAT_VORBIS: return "vorbis";
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530133 case AUDIO_FORMAT_OPUS: return "opus";
134 case AUDIO_FORMAT_AC3: return "ac-3";
135 case AUDIO_FORMAT_E_AC3: return "e-ac-3";
Phil Burkfdb3c072016-02-09 10:47:02 -0800136 case AUDIO_FORMAT_IEC61937: return "iec61937";
Eric Laurente30f2092016-07-07 18:56:57 -0700137 case AUDIO_FORMAT_DTS: return "dts";
138 case AUDIO_FORMAT_DTS_HD: return "dts-hd";
139 case AUDIO_FORMAT_DOLBY_TRUEHD: return "dolby-truehd";
Marco Nelissenb2208842014-02-07 14:00:50 -0800140 default:
141 break;
142 }
143 return "unknown";
144}
145
Mathias Agopian65ab4712010-07-14 17:59:35 -0700146// ----------------------------------------------------------------------------
147
148AudioFlinger::AudioFlinger()
149 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800150 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800151 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700152 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800153 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800154 mMasterMute(false),
Glenn Kastend2e67e12016-04-11 08:26:37 -0700155 // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),
John Grossman4ff14ba2012-02-08 16:37:41 -0800156 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700157 mBtNrecIsOff(false),
158 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700159 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700160 mGlobalEffectEnableTime(0),
Eric Laurent72e3f392015-05-20 14:43:50 -0700161 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700162{
Glenn Kastend2e67e12016-04-11 08:26:37 -0700163 // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum
164 for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) {
165 // zero ID has a special meaning, so unavailable
166 mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX;
167 }
168
Glenn Kasten949a9262013-04-16 12:35:20 -0700169 getpid_cached = getpid();
Glenn Kastenae0cff12016-02-24 13:57:49 -0800170 const bool doLog = property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800171 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800172 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
173 MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800174 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700175
Wei Jia3f273d12015-11-24 09:06:49 -0800176 // reset battery stats.
177 // if the audio service has crashed, battery stats could be left
178 // in bad state, reset the state upon service start.
179 BatteryNotifier::getInstance().noteResetAudio();
180
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700181 mDevicesFactoryHal = DevicesFactoryHalInterface::create();
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700182 mEffectsFactoryHal = EffectsFactoryHalInterface::create();
183
Glenn Kasten46909e72013-02-26 09:20:22 -0800184#ifdef TEE_SINK
Glenn Kasten9a003992016-02-23 15:24:34 -0800185 char value[PROPERTY_VALUE_MAX];
Glenn Kastenda6ef132013-01-10 12:31:01 -0800186 (void) property_get("ro.debuggable", value, "0");
187 int debuggable = atoi(value);
188 int teeEnabled = 0;
189 if (debuggable) {
190 (void) property_get("af.tee", value, "0");
191 teeEnabled = atoi(value);
192 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800193 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700194 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800195 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700196 }
197 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800198 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700199 }
200 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800201 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700202 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800203#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700204}
205
206void AudioFlinger::onFirstRef()
207{
Eric Laurent93575202011-01-18 18:39:02 -0800208 Mutex::Autolock _l(mLock);
209
Dima Zavin799a70e2011-04-18 16:57:27 -0700210 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800211 char val_str[PROPERTY_VALUE_MAX] = { 0 };
212 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
213 uint32_t int_val;
214 if (1 == sscanf(val_str, "%u", &int_val)) {
215 mStandbyTimeInNsecs = milliseconds(int_val);
216 ALOGI("Using %u mSec as standby time.", int_val);
217 } else {
218 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
219 ALOGI("Using default %u mSec as standby time.",
220 (uint32_t)(mStandbyTimeInNsecs / 1000000));
221 }
222 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700223
Eric Laurent1c333e22014-05-20 10:48:17 -0700224 mPatchPanel = new PatchPanel(this);
Andy Hungdeb03352016-08-29 14:40:14 -0700225
Eric Laurenta4c5a552012-03-29 10:12:40 -0700226 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700227}
228
229AudioFlinger::~AudioFlinger()
230{
231 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700232 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700233 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234 }
235 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700236 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700237 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700239
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800240 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
241 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700242 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700244
245 // Tell media.log service about any old writers that still need to be unregistered
Andy Hungce717682015-12-22 13:40:32 -0800246 if (mLogMemoryDealer != 0) {
247 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
248 if (binder != 0) {
249 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
250 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
251 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
252 mUnregisteredWriters.pop();
253 mediaLogService->unregisterWriter(iMemory);
254 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700255 }
256 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700257}
258
Eric Laurenta4c5a552012-03-29 10:12:40 -0700259static const char * const audio_interfaces[] = {
260 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
261 AUDIO_HARDWARE_MODULE_ID_A2DP,
262 AUDIO_HARDWARE_MODULE_ID_USB,
263};
264#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
265
Phil Burk062e67a2015-02-11 13:40:50 -0800266AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700267 audio_module_handle_t module,
268 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700269{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700270 // if module is 0, the request comes from an old policy manager and we should load
271 // well known modules
272 if (module == 0) {
273 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
274 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
275 loadHwModule_l(audio_interfaces[i]);
276 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700277 // then try to find a module supporting the requested device.
278 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
279 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700280 sp<DeviceHalInterface> dev = audioHwDevice->hwDevice();
281 uint32_t supportedDevices;
282 if (dev->getSupportedDevices(&supportedDevices) == OK &&
283 (supportedDevices & devices) == devices) {
Eric Laurentf1c04f92012-08-28 14:26:53 -0700284 return audioHwDevice;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700285 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700286 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700287 } else {
288 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700289 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
290 if (audioHwDevice != NULL) {
291 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700292 }
293 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700294
Dima Zavin799a70e2011-04-18 16:57:27 -0700295 return NULL;
296}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700297
Glenn Kasten0f11b512014-01-31 16:18:54 -0800298void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299{
300 const size_t SIZE = 256;
301 char buffer[SIZE];
302 String8 result;
303
304 result.append("Clients:\n");
305 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800306 sp<Client> client = mClients.valueAt(i).promote();
307 if (client != 0) {
308 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
309 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310 }
311 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700312
Eric Laurentd1b28d42013-09-18 18:47:13 -0700313 result.append("Notification Clients:\n");
314 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
315 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
316 result.append(buffer);
317 }
318
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700319 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800320 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700321 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
322 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800323 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700324 result.append(buffer);
325 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327}
328
329
Glenn Kasten0f11b512014-01-31 16:18:54 -0800330void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331{
332 const size_t SIZE = 256;
333 char buffer[SIZE];
334 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800335 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336
John Grossman4ff14ba2012-02-08 16:37:41 -0800337 snprintf(buffer, SIZE, "Hardware status: %d\n"
338 "Standby Time mSec: %u\n",
339 hardwareStatus,
340 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341 result.append(buffer);
342 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343}
344
Glenn Kasten0f11b512014-01-31 16:18:54 -0800345void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346{
347 const size_t SIZE = 256;
348 char buffer[SIZE];
349 String8 result;
350 snprintf(buffer, SIZE, "Permission Denial: "
351 "can't dump AudioFlinger from pid=%d, uid=%d\n",
352 IPCThreadState::self()->getCallingPid(),
353 IPCThreadState::self()->getCallingUid());
354 result.append(buffer);
355 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356}
357
Eric Laurent81784c32012-11-19 14:55:58 -0800358bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359{
360 bool locked = false;
361 for (int i = 0; i < kDumpLockRetries; ++i) {
362 if (mutex.tryLock() == NO_ERROR) {
363 locked = true;
364 break;
365 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800366 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700367 }
368 return locked;
369}
370
371status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
372{
Glenn Kasten44deb052012-02-05 18:09:08 -0800373 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374 dumpPermissionDenial(fd, args);
375 } else {
376 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800377 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378 if (!hardwareLocked) {
379 String8 result(kHardwareLockedString);
380 write(fd, result.string(), result.size());
381 } else {
382 mHardwareLock.unlock();
383 }
384
Eric Laurent81784c32012-11-19 14:55:58 -0800385 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700386
387 // failed to lock - AudioFlinger is probably deadlocked
388 if (!locked) {
389 String8 result(kDeadlockedString);
390 write(fd, result.string(), result.size());
391 }
392
Eric Laurent021cf962014-05-13 10:18:14 -0700393 bool clientLocked = dumpTryLock(mClientLock);
394 if (!clientLocked) {
395 String8 result(kClientLockedString);
396 write(fd, result.string(), result.size());
397 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700398
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700399 if (mEffectsFactoryHal != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700400 mEffectsFactoryHal->dumpEffects(fd);
401 } else {
402 String8 result(kNoEffectsFactory);
403 write(fd, result.string(), result.size());
404 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700405
Mathias Agopian65ab4712010-07-14 17:59:35 -0700406 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700407 if (clientLocked) {
408 mClientLock.unlock();
409 }
410
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411 dumpInternals(fd, args);
412
413 // dump playback threads
414 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
415 mPlaybackThreads.valueAt(i)->dump(fd, args);
416 }
417
418 // dump record threads
419 for (size_t i = 0; i < mRecordThreads.size(); i++) {
420 mRecordThreads.valueAt(i)->dump(fd, args);
421 }
422
Eric Laurentaaa44472014-09-12 17:41:50 -0700423 // dump orphan effect chains
424 if (mOrphanEffectChains.size() != 0) {
425 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
426 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
427 mOrphanEffectChains.valueAt(i)->dump(fd, args);
428 }
429 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700430 // dump all hardware devs
431 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700432 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
433 dev->dump(fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700434 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700435
Glenn Kasten46909e72013-02-26 09:20:22 -0800436#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700437 // dump the serially shared record tee sink
438 if (mRecordTeeSource != 0) {
439 dumpTee(fd, mRecordTeeSource);
440 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800441#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700442
Glenn Kastend65d73c2012-06-22 17:21:07 -0700443 if (locked) {
444 mLock.unlock();
445 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800446
447 // append a copy of media.log here by forwarding fd to it, but don't attempt
448 // to lookup the service if it's not running, as it will block for a second
449 if (mLogMemoryDealer != 0) {
450 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
451 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700452 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800453 Vector<String16> args;
454 binder->dump(fd, args);
455 }
456 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700457
458 // check for optional arguments
Andy Hung07b745e2016-05-23 16:21:07 -0700459 bool dumpMem = false;
Andy Hung35fec5f2016-04-13 14:21:48 -0700460 bool unreachableMemory = false;
461 for (const auto &arg : args) {
Andy Hung07b745e2016-05-23 16:21:07 -0700462 if (arg == String16("-m")) {
463 dumpMem = true;
464 } else if (arg == String16("--unreachable")) {
Andy Hung35fec5f2016-04-13 14:21:48 -0700465 unreachableMemory = true;
466 }
467 }
468
Andy Hung07b745e2016-05-23 16:21:07 -0700469 if (dumpMem) {
470 dprintf(fd, "\nDumping memory:\n");
471 std::string s = dumpMemoryAddresses(100 /* limit */);
472 write(fd, s.c_str(), s.size());
473 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700474 if (unreachableMemory) {
475 dprintf(fd, "\nDumping unreachable memory:\n");
476 // TODO - should limit be an argument parameter?
Andy Hung07b745e2016-05-23 16:21:07 -0700477 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
Andy Hung35fec5f2016-04-13 14:21:48 -0700478 write(fd, s.c_str(), s.size());
479 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700480 }
481 return NO_ERROR;
482}
483
Eric Laurent021cf962014-05-13 10:18:14 -0700484sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800485{
Eric Laurent021cf962014-05-13 10:18:14 -0700486 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800487 // If pid is already in the mClients wp<> map, then use that entry
488 // (for which promote() is always != 0), otherwise create a new entry and Client.
489 sp<Client> client = mClients.valueFor(pid).promote();
490 if (client == 0) {
491 client = new Client(this, pid);
492 mClients.add(pid, client);
493 }
494
495 return client;
496}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700497
Glenn Kasten9e58b552013-01-18 15:09:48 -0800498sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
499{
Glenn Kasten481fb672013-09-30 14:39:28 -0700500 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800501 if (mLogMemoryDealer == 0) {
502 return new NBLog::Writer();
503 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800504 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700505 // Similarly if we can't contact the media.log service, also return a dummy writer
506 if (binder == 0) {
507 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800508 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700509 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
510 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
511 // If allocation fails, consult the vector of previously unregistered writers
512 // and garbage-collect one or more them until an allocation succeeds
513 if (shared == 0) {
514 Mutex::Autolock _l(mUnregisteredWritersLock);
515 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
516 {
517 // Pick the oldest stale writer to garbage-collect
518 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
519 mUnregisteredWriters.removeAt(0);
520 mediaLogService->unregisterWriter(iMemory);
521 // Now the media.log remote reference to IMemory is gone. When our last local
522 // reference to IMemory also drops to zero at end of this block,
523 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
524 }
525 // Re-attempt the allocation
526 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
527 if (shared != 0) {
528 goto success;
529 }
530 }
531 // Even after garbage-collecting all old writers, there is still not enough memory,
532 // so return a dummy writer
533 return new NBLog::Writer();
534 }
535success:
536 mediaLogService->registerWriter(shared, size, name);
537 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800538}
539
540void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
541{
Glenn Kasten685ef092013-02-04 08:15:34 -0800542 if (writer == 0) {
543 return;
544 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800545 sp<IMemory> iMemory(writer->getIMemory());
546 if (iMemory == 0) {
547 return;
548 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700549 // Rather than removing the writer immediately, append it to a queue of old writers to
550 // be garbage-collected later. This allows us to continue to view old logs for a while.
551 Mutex::Autolock _l(mUnregisteredWritersLock);
552 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800553}
554
Mathias Agopian65ab4712010-07-14 17:59:35 -0700555// IAudioFlinger interface
556
557
558sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800559 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800561 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700562 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800563 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -0700564 audio_output_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700565 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800566 audio_io_handle_t output,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700567 pid_t pid,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800568 pid_t tid,
Glenn Kastend848eb42016-03-08 13:42:11 -0800569 audio_session_t *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800570 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571 status_t *status)
572{
573 sp<PlaybackThread::Track> track;
574 sp<TrackHandle> trackHandle;
575 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700576 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -0800577 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -0700579 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
580 if (pid == -1 || !isTrustedCallingUid(callingUid)) {
581 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
582 ALOGW_IF(pid != -1 && pid != callingPid,
583 "%s uid %d pid %d tried to pass itself off as pid %d",
584 __func__, callingUid, callingPid, pid);
585 pid = callingPid;
586 }
587
Glenn Kasten263709e2012-01-06 08:40:01 -0800588 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
589 // but if someone uses binder directly they could bypass that and cause us to crash
590 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000591 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592 lStatus = BAD_VALUE;
593 goto Exit;
594 }
595
Glenn Kasten53b5d092014-02-05 10:00:23 -0800596 // further sample rate checks are performed by createTrack_l() depending on the thread type
597 if (sampleRate == 0) {
598 ALOGE("createTrack() invalid sample rate %u", sampleRate);
599 lStatus = BAD_VALUE;
600 goto Exit;
601 }
602
603 // further channel mask checks are performed by createTrack_l() depending on the thread type
604 if (!audio_is_output_channel(channelMask)) {
605 ALOGE("createTrack() invalid channel mask %#x", channelMask);
606 lStatus = BAD_VALUE;
607 goto Exit;
608 }
609
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700610 // further format checks are performed by createTrack_l() depending on the thread type
611 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800612 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700613 lStatus = BAD_VALUE;
614 goto Exit;
615 }
616
Glenn Kasten663c2242013-09-24 11:52:37 -0700617 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
618 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
619 lStatus = BAD_VALUE;
620 goto Exit;
621 }
622
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623 {
624 Mutex::Autolock _l(mLock);
625 PlaybackThread *thread = checkPlaybackThread_l(output);
626 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800627 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 lStatus = BAD_VALUE;
629 goto Exit;
630 }
631
Eric Laurent021cf962014-05-13 10:18:14 -0700632 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633
Glenn Kastene848bd92014-03-13 15:00:32 -0700634 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700635 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800636 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
637 ALOGE("createTrack() invalid session ID %d", *sessionId);
638 lStatus = BAD_VALUE;
639 goto Exit;
640 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700641 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700642 // check if an effect chain with the same session ID is present on another
643 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700644 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700645 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
646 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700647 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent4c415062016-06-17 16:14:16 -0700648 if (sessions & ThreadBase::EFFECT_SESSION) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700649 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700650 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700651 }
Eric Laurentde070132010-07-13 04:45:46 -0700652 }
653 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700655 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800656 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 if (sessionId != NULL) {
658 *sessionId = lSessionId;
659 }
660 }
Steve Block3856b092011-10-20 11:56:00 +0100661 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700662
663 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800664 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800665 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700666 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700667
668 // move effect chain to this output thread if an effect on same session was waiting
669 // for a track to be created
670 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700671 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700672 Mutex::Autolock _dl(thread->mLock);
673 Mutex::Autolock _sl(effectThread->mLock);
674 moveEffectChain_l(lSessionId, effectThread, thread, true);
675 }
Eric Laurenta011e352012-03-29 15:51:43 -0700676
677 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700678 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700679 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
680 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700681 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700682 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700683 } else {
684 mPendingSyncEvents[i]->cancel();
685 }
Eric Laurenta011e352012-03-29 15:51:43 -0700686 mPendingSyncEvents.removeAt(i);
687 i--;
688 }
689 }
690 }
Glenn Kastene198c362013-08-13 09:13:36 -0700691
Glenn Kastend848eb42016-03-08 13:42:11 -0800692 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700693 }
Glenn Kastene198c362013-08-13 09:13:36 -0700694
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700695 if (lStatus != NO_ERROR) {
696 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700697 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700698 // Don't hold mClientLock when releasing the reference on the track as the
699 // destructor will acquire it.
700 {
701 Mutex::Autolock _cl(mClientLock);
702 client.clear();
703 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700705 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706 }
707
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700708 // return handle to client
709 trackHandle = new TrackHandle(track);
710
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700712 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713 return trackHandle;
714}
715
Glenn Kasten2c073da2016-02-26 09:14:08 -0800716uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717{
718 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800719 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800721 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 return 0;
723 }
724 return thread->sampleRate();
725}
726
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800727audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728{
729 Mutex::Autolock _l(mLock);
730 PlaybackThread *thread = checkPlaybackThread_l(output);
731 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000732 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800733 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734 }
735 return thread->format();
736}
737
Glenn Kasten2c073da2016-02-26 09:14:08 -0800738size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700739{
740 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800741 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800743 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700744 return 0;
745 }
Glenn Kasten58912562012-04-03 10:45:00 -0700746 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
747 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748 return thread->frameCount();
749}
750
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700751size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
752{
753 Mutex::Autolock _l(mLock);
754 ThreadBase *thread = checkThread_l(ioHandle);
755 if (thread == NULL) {
756 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
757 return 0;
758 }
759 return thread->frameCountHAL();
760}
761
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800762uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763{
764 Mutex::Autolock _l(mLock);
765 PlaybackThread *thread = checkPlaybackThread_l(output);
766 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800767 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768 return 0;
769 }
770 return thread->latency();
771}
772
773status_t AudioFlinger::setMasterVolume(float value)
774{
Eric Laurenta1884f92011-08-23 08:25:03 -0700775 status_t ret = initCheck();
776 if (ret != NO_ERROR) {
777 return ret;
778 }
779
Mathias Agopian65ab4712010-07-14 17:59:35 -0700780 // check calling permissions
781 if (!settingsAllowed()) {
782 return PERMISSION_DENIED;
783 }
784
Eric Laurenta4c5a552012-03-29 10:12:40 -0700785 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700786 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700787
John Grossmanee578c02012-07-23 17:05:46 -0700788 // Set master volume in the HALs which support it.
789 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
790 AutoMutex lock(mHardwareLock);
791 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800792
John Grossmanee578c02012-07-23 17:05:46 -0700793 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
794 if (dev->canSetMasterVolume()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700795 dev->hwDevice()->setMasterVolume(value);
Eric Laurent93575202011-01-18 18:39:02 -0800796 }
John Grossmanee578c02012-07-23 17:05:46 -0700797 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700799
John Grossmanee578c02012-07-23 17:05:46 -0700800 // Now set the master volume in each playback thread. Playback threads
801 // assigned to HALs which do not have master volume support will apply
802 // master volume during the mix operation. Threads with HALs which do
803 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700804 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
805 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
806 continue;
807 }
John Grossmanee578c02012-07-23 17:05:46 -0700808 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700809 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700810
811 return NO_ERROR;
812}
813
Glenn Kastenf78aee72012-01-04 11:00:47 -0800814status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815{
Eric Laurenta1884f92011-08-23 08:25:03 -0700816 status_t ret = initCheck();
817 if (ret != NO_ERROR) {
818 return ret;
819 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820
821 // check calling permissions
822 if (!settingsAllowed()) {
823 return PERMISSION_DENIED;
824 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800825 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000826 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700827 return BAD_VALUE;
828 }
829
830 { // scope for the lock
831 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700832 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700834 ret = dev->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835 mHardwareStatus = AUDIO_HW_IDLE;
836 }
837
838 if (NO_ERROR == ret) {
839 Mutex::Autolock _l(mLock);
840 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800841 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700842 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843 }
844
845 return ret;
846}
847
848status_t AudioFlinger::setMicMute(bool state)
849{
Eric Laurenta1884f92011-08-23 08:25:03 -0700850 status_t ret = initCheck();
851 if (ret != NO_ERROR) {
852 return ret;
853 }
854
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 // check calling permissions
856 if (!settingsAllowed()) {
857 return PERMISSION_DENIED;
858 }
859
860 AutoMutex lock(mHardwareLock);
861 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700862 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700863 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
864 status_t result = dev->setMicMute(state);
Eric Laurent2f035f52014-09-14 12:11:52 -0700865 if (result != NO_ERROR) {
866 ret = result;
867 }
868 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869 mHardwareStatus = AUDIO_HW_IDLE;
870 return ret;
871}
872
873bool AudioFlinger::getMicMute() const
874{
Eric Laurenta1884f92011-08-23 08:25:03 -0700875 status_t ret = initCheck();
876 if (ret != NO_ERROR) {
877 return false;
878 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800879 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700880 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800881 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800883 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700884 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
885 status_t result = dev->getMicMute(&state);
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800886 if (result == NO_ERROR) {
887 mute = mute && state;
888 }
889 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800891
892 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700893}
894
895status_t AudioFlinger::setMasterMute(bool muted)
896{
John Grossmand8f178d2012-07-20 14:51:35 -0700897 status_t ret = initCheck();
898 if (ret != NO_ERROR) {
899 return ret;
900 }
901
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902 // check calling permissions
903 if (!settingsAllowed()) {
904 return PERMISSION_DENIED;
905 }
906
John Grossmanee578c02012-07-23 17:05:46 -0700907 Mutex::Autolock _l(mLock);
908 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700909
John Grossmanee578c02012-07-23 17:05:46 -0700910 // Set master mute in the HALs which support it.
911 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
912 AutoMutex lock(mHardwareLock);
913 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700914
John Grossmanee578c02012-07-23 17:05:46 -0700915 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
916 if (dev->canSetMasterMute()) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700917 dev->hwDevice()->setMasterMute(muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700918 }
John Grossmanee578c02012-07-23 17:05:46 -0700919 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700920 }
921
John Grossmanee578c02012-07-23 17:05:46 -0700922 // Now set the master mute in each playback thread. Playback threads
923 // assigned to HALs which do not have master mute support will apply master
924 // mute during the mix operation. Threads with HALs which do support master
925 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700926 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
927 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
928 continue;
929 }
John Grossmanee578c02012-07-23 17:05:46 -0700930 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700931 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932
933 return NO_ERROR;
934}
935
936float AudioFlinger::masterVolume() const
937{
Glenn Kasten98067102011-12-13 11:47:54 -0800938 Mutex::Autolock _l(mLock);
939 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940}
941
942bool AudioFlinger::masterMute() const
943{
Glenn Kasten98067102011-12-13 11:47:54 -0800944 Mutex::Autolock _l(mLock);
945 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946}
947
John Grossman4ff14ba2012-02-08 16:37:41 -0800948float AudioFlinger::masterVolume_l() const
949{
John Grossman4ff14ba2012-02-08 16:37:41 -0800950 return mMasterVolume;
951}
952
John Grossmand8f178d2012-07-20 14:51:35 -0700953bool AudioFlinger::masterMute_l() const
954{
John Grossmanee578c02012-07-23 17:05:46 -0700955 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700956}
957
Eric Laurent223fd5c2014-11-11 13:43:36 -0800958status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
959{
960 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
961 ALOGW("setStreamVolume() invalid stream %d", stream);
962 return BAD_VALUE;
963 }
964 pid_t caller = IPCThreadState::self()->getCallingPid();
965 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
966 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
967 return PERMISSION_DENIED;
968 }
969
970 return NO_ERROR;
971}
972
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800973status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
974 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975{
976 // check calling permissions
977 if (!settingsAllowed()) {
978 return PERMISSION_DENIED;
979 }
980
Eric Laurent223fd5c2014-11-11 13:43:36 -0800981 status_t status = checkStreamType(stream);
982 if (status != NO_ERROR) {
983 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800985 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700986
987 AutoMutex lock(mLock);
988 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700989 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990 thread = checkPlaybackThread_l(output);
991 if (thread == NULL) {
992 return BAD_VALUE;
993 }
994 }
995
996 mStreamTypes[stream].volume = value;
997
998 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800999 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001000 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001001 }
1002 } else {
1003 thread->setStreamVolume(stream, value);
1004 }
1005
1006 return NO_ERROR;
1007}
1008
Glenn Kastenfff6d712012-01-12 16:38:12 -08001009status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001010{
1011 // check calling permissions
1012 if (!settingsAllowed()) {
1013 return PERMISSION_DENIED;
1014 }
1015
Eric Laurent223fd5c2014-11-11 13:43:36 -08001016 status_t status = checkStreamType(stream);
1017 if (status != NO_ERROR) {
1018 return status;
1019 }
1020 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1021
1022 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001023 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001024 return BAD_VALUE;
1025 }
1026
Eric Laurent93575202011-01-18 18:39:02 -08001027 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -07001029 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001030 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001031
1032 return NO_ERROR;
1033}
1034
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001035float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001036{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001037 status_t status = checkStreamType(stream);
1038 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039 return 0.0f;
1040 }
1041
1042 AutoMutex lock(mLock);
1043 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001044 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045 PlaybackThread *thread = checkPlaybackThread_l(output);
1046 if (thread == NULL) {
1047 return 0.0f;
1048 }
1049 volume = thread->streamVolume(stream);
1050 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001051 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052 }
1053
1054 return volume;
1055}
1056
Glenn Kastenfff6d712012-01-12 16:38:12 -08001057bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001059 status_t status = checkStreamType(stream);
1060 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001061 return true;
1062 }
1063
Glenn Kasten6637baa2012-01-09 09:40:36 -08001064 AutoMutex lock(mLock);
1065 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001066}
1067
Eric Laurent054d9d32015-04-24 08:48:48 -07001068
1069void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1070{
1071 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1072 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1073 }
1074}
1075
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001076status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001078 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1079 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001080
Mathias Agopian65ab4712010-07-14 17:59:35 -07001081 // check calling permissions
1082 if (!settingsAllowed()) {
1083 return PERMISSION_DENIED;
1084 }
1085
Glenn Kasten142f5192014-03-25 17:44:59 -07001086 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1087 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001088 Mutex::Autolock _l(mLock);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001089 // result will remain NO_INIT if no audio device is present
1090 status_t final_result = NO_INIT;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001091 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001092 AutoMutex lock(mHardwareLock);
1093 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1094 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001095 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1096 status_t result = dev->setParameters(keyValuePairs);
Eric Laurentd21bcd22016-09-08 18:25:54 -07001097 // return success if at least one audio device accepts the parameters as not all
1098 // HALs are requested to support all parameters. If no audio device supports the
1099 // requested parameters, the last error is reported.
1100 if (final_result != NO_ERROR) {
1101 final_result = result;
1102 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001103 }
1104 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001105 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001106 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1107 AudioParameter param = AudioParameter(keyValuePairs);
1108 String8 value;
1109 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001110 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1111 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001112 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1113 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001114 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001115 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1116 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001117 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001118 // suspend effects associated with those session IDs
1119 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001120 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001121 thread->setEffectSuspended(FX_IID_AEC,
1122 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001123 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001124 thread->setEffectSuspended(FX_IID_NS,
1125 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001126 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001127 }
1128 }
Eric Laurentbee53372011-08-29 12:42:48 -07001129 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001130 }
1131 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001132 String8 screenState;
1133 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1134 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001135 if (isOff != (AudioFlinger::mScreenState & 1)) {
1136 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001137 }
1138 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001139 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001140 }
1141
1142 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1143 // and the thread is exited once the lock is released
1144 sp<ThreadBase> thread;
1145 {
1146 Mutex::Autolock _l(mLock);
1147 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001148 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001150 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001151 // indicate output device change to all input threads for pre processing
1152 AudioParameter param = AudioParameter(keyValuePairs);
1153 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001154 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1155 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001156 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001157 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158 }
1159 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001160 if (thread != 0) {
1161 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162 }
1163 return BAD_VALUE;
1164}
1165
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001166String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001168 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1169 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170
Eric Laurenta4c5a552012-03-29 10:12:40 -07001171 Mutex::Autolock _l(mLock);
1172
Glenn Kasten142f5192014-03-25 17:44:59 -07001173 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001174 String8 out_s8;
1175
Dima Zavin799a70e2011-04-18 16:57:27 -07001176 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001177 String8 s;
1178 status_t result;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001179 {
1180 AutoMutex lock(mHardwareLock);
1181 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001182 sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice();
1183 result = dev->getParameters(keys, &s);
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001184 mHardwareStatus = AUDIO_HW_IDLE;
1185 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001186 if (result == OK) out_s8 += s;
Dima Zavin799a70e2011-04-18 16:57:27 -07001187 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001188 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001189 }
1190
Mathias Agopian65ab4712010-07-14 17:59:35 -07001191 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1192 if (playbackThread != NULL) {
1193 return playbackThread->getParameters(keys);
1194 }
1195 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1196 if (recordThread != NULL) {
1197 return recordThread->getParameters(keys);
1198 }
1199 return String8("");
1200}
1201
Glenn Kastendd8104c2012-07-02 12:42:44 -07001202size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1203 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204{
Eric Laurenta1884f92011-08-23 08:25:03 -07001205 status_t ret = initCheck();
1206 if (ret != NO_ERROR) {
1207 return 0;
1208 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001209 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001210 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001211 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001212 return 0;
1213 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001214
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001215 AutoMutex lock(mHardwareLock);
1216 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001217 audio_config_t config, proposed;
1218 memset(&proposed, 0, sizeof(proposed));
1219 proposed.sample_rate = sampleRate;
1220 proposed.channel_mask = channelMask;
1221 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001222
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001223 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001224 size_t frames;
1225 for (;;) {
1226 // Note: config is currently a const parameter for get_input_buffer_size()
1227 // but we use a copy from proposed in case config changes from the call.
1228 config = proposed;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001229 status_t result = dev->getInputBufferSize(&config, &frames);
1230 if (result == OK && frames != 0) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001231 break; // hal success, config is the result
1232 }
1233 // change one parameter of the configuration each iteration to a more "common" value
1234 // to see if the device will support it.
1235 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1236 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1237 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1238 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1239 } else {
1240 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1241 "format %#x, channelMask 0x%X",
1242 sampleRate, format, channelMask);
1243 break; // retries failed, break out of loop with frames == 0.
1244 }
1245 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001246 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001247 if (frames > 0 && config.sample_rate != sampleRate) {
1248 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1249 }
1250 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251}
1252
Glenn Kasten5f972c02014-01-13 09:59:31 -08001253uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001254{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255 Mutex::Autolock _l(mLock);
1256
1257 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1258 if (recordThread != NULL) {
1259 return recordThread->getInputFramesLost();
1260 }
1261 return 0;
1262}
1263
1264status_t AudioFlinger::setVoiceVolume(float value)
1265{
Eric Laurenta1884f92011-08-23 08:25:03 -07001266 status_t ret = initCheck();
1267 if (ret != NO_ERROR) {
1268 return ret;
1269 }
1270
Mathias Agopian65ab4712010-07-14 17:59:35 -07001271 // check calling permissions
1272 if (!settingsAllowed()) {
1273 return PERMISSION_DENIED;
1274 }
1275
1276 AutoMutex lock(mHardwareLock);
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001277 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001278 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001279 ret = dev->setVoiceVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001280 mHardwareStatus = AUDIO_HW_IDLE;
1281
1282 return ret;
1283}
1284
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001285status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001286 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001287{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 Mutex::Autolock _l(mLock);
1289
1290 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1291 if (playbackThread != NULL) {
1292 return playbackThread->getRenderPosition(halFrames, dspFrames);
1293 }
1294
1295 return BAD_VALUE;
1296}
1297
1298void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1299{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001301 if (client == 0) {
1302 return;
1303 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001304 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001305 {
1306 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001307 if (mNotificationClients.indexOfKey(pid) < 0) {
1308 sp<NotificationClient> notificationClient = new NotificationClient(this,
1309 client,
1310 pid);
1311 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312
Eric Laurent021cf962014-05-13 10:18:14 -07001313 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314
Marco Nelissen06b46062014-11-14 07:58:25 -08001315 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001316 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001317 }
1318 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001319
Eric Laurent021cf962014-05-13 10:18:14 -07001320 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001321 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001322 // the config change is always sent from playback or record threads to avoid deadlock
1323 // with AudioSystem::gLock
1324 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1325 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1326 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001328 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1329 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001330 }
1331}
1332
1333void AudioFlinger::removeNotificationClient(pid_t pid)
1334{
1335 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001336 {
1337 Mutex::Autolock _cl(mClientLock);
1338 mNotificationClients.removeItem(pid);
1339 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001340
Steve Block3856b092011-10-20 11:56:00 +01001341 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001342 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001343 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001344 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001345 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001346 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001347 if (ref->mPid == pid) {
1348 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001349 mAudioSessionRefs.removeAt(i);
1350 delete ref;
1351 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001352 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001353 } else {
1354 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001355 }
1356 }
1357 if (removed) {
1358 purgeStaleEffects_l();
1359 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360}
1361
Eric Laurent73e26b62015-04-27 16:55:58 -07001362void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001363 const sp<AudioIoDescriptor>& ioDesc,
1364 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365{
Eric Laurent021cf962014-05-13 10:18:14 -07001366 Mutex::Autolock _l(mClientLock);
1367 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001368 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001369 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1370 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1371 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372 }
1373}
1374
Eric Laurent021cf962014-05-13 10:18:14 -07001375// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001376void AudioFlinger::removeClient_l(pid_t pid)
1377{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001378 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001379 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380 mClients.removeItem(pid);
1381}
1382
Eric Laurent717e1282012-06-29 16:36:52 -07001383// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001384sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1385 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001386{
1387 sp<PlaybackThread> thread;
1388
1389 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1390 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1391 ALOG_ASSERT(thread == 0);
1392 thread = mPlaybackThreads.valueAt(i);
1393 }
1394 }
1395
1396 return thread;
1397}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001398
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399
Mathias Agopian65ab4712010-07-14 17:59:35 -07001400
1401// ----------------------------------------------------------------------------
1402
1403AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1404 : RefBase(),
1405 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001406 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301408 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1409 heapSize *= 1024;
1410 if (!heapSize) {
1411 heapSize = kClientSharedHeapSizeBytes;
1412 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1413 // invalidated tracks
1414 if (!audioFlinger->isLowRamDevice()) {
1415 heapSize *= kClientSharedHeapSizeMultiplier;
1416 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001417 }
1418 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001419}
1420
Eric Laurent021cf962014-05-13 10:18:14 -07001421// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001422AudioFlinger::Client::~Client()
1423{
1424 mAudioFlinger->removeClient_l(mPid);
1425}
1426
Glenn Kasten435dbe62012-01-30 10:15:48 -08001427sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428{
1429 return mMemoryDealer;
1430}
1431
1432// ----------------------------------------------------------------------------
1433
1434AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1435 const sp<IAudioFlingerClient>& client,
1436 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001437 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001438{
1439}
1440
1441AudioFlinger::NotificationClient::~NotificationClient()
1442{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001443}
1444
Glenn Kasten0f11b512014-01-31 16:18:54 -08001445void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001446{
1447 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001448 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449}
1450
Mathias Agopian65ab4712010-07-14 17:59:35 -07001451
1452// ----------------------------------------------------------------------------
1453
1454sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001455 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001457 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001458 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001459 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001460 size_t *frameCount,
Eric Laurent05067782016-06-01 18:27:28 -07001461 audio_input_flags_t *flags,
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001462 pid_t pid,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001463 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001464 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001465 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001466 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001467 sp<IMemory>& cblk,
1468 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001469 status_t *status)
1470{
1471 sp<RecordThread::RecordTrack> recordTrack;
1472 sp<RecordHandle> recordHandle;
1473 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001475 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001476
Glenn Kastend776ac62014-05-07 09:16:09 -07001477 cblk.clear();
1478 buffers.clear();
1479
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001480 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -07001481 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1482 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001483 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001484 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1485 clientUid = callingUid;
Haynes Mathew George9ea77cd2016-04-06 17:07:48 -07001486 updatePid = true;
1487 }
1488
1489 if (updatePid) {
1490 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
1491 ALOGW_IF(pid != -1 && pid != callingPid,
1492 "%s uid %d pid %d tried to pass itself off as pid %d",
1493 __func__, callingUid, callingPid, pid);
1494 pid = callingPid;
Marco Nelissendcb346b2015-09-09 10:47:29 -07001495 }
1496
Mathias Agopian65ab4712010-07-14 17:59:35 -07001497 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001498 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001499 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001500 lStatus = PERMISSION_DENIED;
1501 goto Exit;
1502 }
1503
Glenn Kasten53b5d092014-02-05 10:00:23 -08001504 // further sample rate checks are performed by createRecordTrack_l()
1505 if (sampleRate == 0) {
1506 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1507 lStatus = BAD_VALUE;
1508 goto Exit;
1509 }
1510
Andy Hung6770c6f2015-04-07 13:43:36 -07001511 // we don't yet support anything other than linear PCM
1512 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001513 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001514 lStatus = BAD_VALUE;
1515 goto Exit;
1516 }
1517
Glenn Kasten53b5d092014-02-05 10:00:23 -08001518 // further channel mask checks are performed by createRecordTrack_l()
1519 if (!audio_is_input_channel(channelMask)) {
1520 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1521 lStatus = BAD_VALUE;
1522 goto Exit;
1523 }
1524
Glenn Kasten05997e22014-03-13 15:08:33 -07001525 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001526 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001527 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001528 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001529 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001530 lStatus = BAD_VALUE;
1531 goto Exit;
1532 }
1533
Eric Laurent021cf962014-05-13 10:18:14 -07001534 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001535
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001536 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001537 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1538 lStatus = BAD_VALUE;
1539 goto Exit;
1540 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001541 lSessionId = *sessionId;
1542 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001543 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001544 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001545 if (sessionId != NULL) {
1546 *sessionId = lSessionId;
1547 }
1548 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001549 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001550
Glenn Kasten1879fff2012-07-11 15:36:59 -07001551 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001552 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001553 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001554 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001555
1556 if (lStatus == NO_ERROR) {
1557 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1558 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001559 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001560 if (chain != 0) {
1561 Mutex::Autolock _l(thread->mLock);
1562 thread->addEffectChain_l(chain);
1563 }
1564 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001565 }
Glenn Kastene198c362013-08-13 09:13:36 -07001566
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001567 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001568 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001569 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001570 // Don't hold mClientLock when releasing the reference on the track as the
1571 // destructor will acquire it.
1572 {
1573 Mutex::Autolock _cl(mClientLock);
1574 client.clear();
1575 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001576 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001577 goto Exit;
1578 }
1579
Glenn Kastend776ac62014-05-07 09:16:09 -07001580 cblk = recordTrack->getCblk();
1581 buffers = recordTrack->getBuffers();
1582
Glenn Kasten2fc14732013-08-05 14:58:14 -07001583 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001584 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001585
1586Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001587 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001588 return recordHandle;
1589}
1590
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001591
1592
Mathias Agopian65ab4712010-07-14 17:59:35 -07001593// ----------------------------------------------------------------------------
1594
Eric Laurenta4c5a552012-03-29 10:12:40 -07001595audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1596{
Eric Laurent44622db2014-08-01 19:00:33 -07001597 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001598 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001599 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001600 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001601 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001602 }
1603 Mutex::Autolock _l(mLock);
1604 return loadHwModule_l(name);
1605}
1606
1607// loadHwModule_l() must be called with AudioFlinger::mLock held
1608audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1609{
1610 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1611 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1612 ALOGW("loadHwModule() module %s already loaded", name);
1613 return mAudioHwDevs.keyAt(i);
1614 }
1615 }
1616
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001617 sp<DeviceHalInterface> dev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001618
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001619 int rc = mDevicesFactoryHal->openDevice(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001620 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001621 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001622 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001623 }
1624
1625 mHardwareStatus = AUDIO_HW_INIT;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001626 rc = dev->initCheck();
Eric Laurenta4c5a552012-03-29 10:12:40 -07001627 mHardwareStatus = AUDIO_HW_IDLE;
1628 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001629 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001630 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001631 }
1632
John Grossmanee578c02012-07-23 17:05:46 -07001633 // Check and cache this HAL's level of support for master mute and master
1634 // volume. If this is the first HAL opened, and it supports the get
1635 // methods, use the initial values provided by the HAL as the current
1636 // master mute and volume settings.
1637
1638 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1639 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001640 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001641
1642 if (0 == mAudioHwDevs.size()) {
1643 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001644 float mv;
1645 if (OK == dev->getMasterVolume(&mv)) {
1646 mMasterVolume = mv;
John Grossmanee578c02012-07-23 17:05:46 -07001647 }
1648
1649 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001650 bool mm;
1651 if (OK == dev->getMasterMute(&mm)) {
1652 mMasterMute = mm;
John Grossmanee578c02012-07-23 17:05:46 -07001653 }
1654 }
1655
Eric Laurenta4c5a552012-03-29 10:12:40 -07001656 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001657 if (OK == dev->setMasterVolume(mMasterVolume)) {
John Grossmanee578c02012-07-23 17:05:46 -07001658 flags = static_cast<AudioHwDevice::Flags>(flags |
1659 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1660 }
1661
1662 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001663 if (OK == dev->setMasterMute(mMasterMute)) {
John Grossmanee578c02012-07-23 17:05:46 -07001664 flags = static_cast<AudioHwDevice::Flags>(flags |
1665 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1666 }
1667
Eric Laurenta4c5a552012-03-29 10:12:40 -07001668 mHardwareStatus = AUDIO_HW_IDLE;
1669 }
1670
Glenn Kastena13cde92016-03-28 15:26:02 -07001671 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001672 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001673
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001674 ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001675
1676 return handle;
1677
1678}
1679
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001680// ----------------------------------------------------------------------------
1681
Glenn Kasten3b16c762012-11-14 08:44:39 -08001682uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001683{
1684 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001685 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001686 return thread != NULL ? thread->sampleRate() : 0;
1687}
1688
Glenn Kastene33054e2012-11-14 12:54:39 -08001689size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001690{
1691 Mutex::Autolock _l(mLock);
Glenn Kastena7335632016-06-09 17:09:53 -07001692 PlaybackThread *thread = fastPlaybackThread_l();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001693 return thread != NULL ? thread->frameCountHAL() : 0;
1694}
1695
1696// ----------------------------------------------------------------------------
1697
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001698status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1699{
1700 uid_t uid = IPCThreadState::self()->getCallingUid();
1701 if (uid != AID_SYSTEM) {
1702 return PERMISSION_DENIED;
1703 }
1704 Mutex::Autolock _l(mLock);
1705 if (mIsDeviceTypeKnown) {
1706 return INVALID_OPERATION;
1707 }
1708 mIsLowRamDevice = isLowRamDevice;
1709 mIsDeviceTypeKnown = true;
1710 return NO_ERROR;
1711}
1712
Eric Laurent93c3d412014-08-01 14:48:35 -07001713audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1714{
1715 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001716
1717 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1718 if (index >= 0) {
1719 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1720 mHwAvSyncIds.valueAt(index), sessionId);
1721 return mHwAvSyncIds.valueAt(index);
1722 }
1723
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001724 sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice();
Eric Laurentfa90e842014-10-17 18:12:31 -07001725 if (dev == NULL) {
1726 return AUDIO_HW_SYNC_INVALID;
1727 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001728 String8 reply;
1729 AudioParameter param;
1730 if (dev->getParameters(String8(AUDIO_PARAMETER_HW_AV_SYNC), &reply) == OK) {
1731 param = AudioParameter(reply);
1732 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001733
1734 int value;
1735 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1736 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1737 return AUDIO_HW_SYNC_INVALID;
1738 }
1739
1740 // allow only one session for a given HW A/V sync ID.
1741 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1742 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1743 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1744 value, mHwAvSyncIds.keyAt(i));
1745 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001746 break;
1747 }
1748 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001749
1750 mHwAvSyncIds.add(sessionId, value);
1751
1752 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1753 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1754 uint32_t sessions = thread->hasAudioSession(sessionId);
Eric Laurent4c415062016-06-17 16:14:16 -07001755 if (sessions & ThreadBase::TRACK_SESSION) {
Eric Laurentfa90e842014-10-17 18:12:31 -07001756 AudioParameter param = AudioParameter();
1757 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1758 thread->setParameters(param.toString());
1759 break;
1760 }
1761 }
1762
1763 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1764 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001765}
1766
Eric Laurent72e3f392015-05-20 14:43:50 -07001767status_t AudioFlinger::systemReady()
1768{
1769 Mutex::Autolock _l(mLock);
1770 ALOGI("%s", __FUNCTION__);
1771 if (mSystemReady) {
1772 ALOGW("%s called twice", __FUNCTION__);
1773 return NO_ERROR;
1774 }
1775 mSystemReady = true;
1776 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1777 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1778 thread->systemReady();
1779 }
1780 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1781 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1782 thread->systemReady();
1783 }
1784 return NO_ERROR;
1785}
1786
Eric Laurentfa90e842014-10-17 18:12:31 -07001787// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1788void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1789{
1790 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1791 if (index >= 0) {
1792 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1793 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1794 AudioParameter param = AudioParameter();
1795 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1796 thread->setParameters(param.toString());
1797 }
1798}
1799
1800
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001801// ----------------------------------------------------------------------------
1802
Eric Laurent83b88082014-06-20 18:31:16 -07001803
1804sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001805 audio_io_handle_t *output,
1806 audio_config_t *config,
1807 audio_devices_t devices,
1808 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001809 audio_output_flags_t flags)
1810{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001811 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001812 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001813 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001814 }
1815
Eric Laurentcf2c0212014-07-25 16:20:43 -07001816 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001817 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1818 } else {
1819 // Audio Policy does not currently request a specific output handle.
1820 // If this is ever needed, see openInput_l() for example code.
1821 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1822 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001823 }
Eric Laurent83b88082014-06-20 18:31:16 -07001824
1825 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1826
Eric Laurent83b88082014-06-20 18:31:16 -07001827 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001828 // This if statement allows overriding the audio policy settings
1829 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1830 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1831 // Check only for Normal Mixing mode
1832 if (kEnableExtendedPrecision) {
1833 // Specify format (uncomment one below to choose)
1834 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1835 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1836 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1837 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001838 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001839 }
1840 if (kEnableExtendedChannels) {
1841 // Specify channel mask (uncomment one below to choose)
1842 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1843 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1844 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1845 }
Eric Laurent83b88082014-06-20 18:31:16 -07001846 }
1847
Phil Burk062e67a2015-02-11 13:40:50 -08001848 AudioStreamOut *outputStream = NULL;
1849 status_t status = outHwDev->openOutputStream(
1850 &outputStream,
1851 *output,
1852 devices,
1853 flags,
1854 config,
1855 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001856
1857 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001858
Phil Burk062e67a2015-02-11 13:40:50 -08001859 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001860
1861 PlaybackThread *thread;
1862 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurente93cc032016-05-05 10:15:10 -07001863 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001864 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001865 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1866 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001867 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001868 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001869 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001870 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001871 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001872 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001873 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001874 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001875 return thread;
1876 }
1877
1878 return 0;
1879}
1880
Eric Laurentcf2c0212014-07-25 16:20:43 -07001881status_t AudioFlinger::openOutput(audio_module_handle_t module,
1882 audio_io_handle_t *output,
1883 audio_config_t *config,
1884 audio_devices_t *devices,
1885 const String8& address,
1886 uint32_t *latencyMs,
1887 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001888{
Phil Burk062e67a2015-02-11 13:40:50 -08001889 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001890 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001891 (devices != NULL) ? *devices : 0,
1892 config->sample_rate,
1893 config->format,
1894 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001895 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001896
Eric Laurentcf2c0212014-07-25 16:20:43 -07001897 if (*devices == AUDIO_DEVICE_NONE) {
1898 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001899 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001900
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901 Mutex::Autolock _l(mLock);
1902
Eric Laurentcf2c0212014-07-25 16:20:43 -07001903 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001904 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001905 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001906
1907 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001908 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001909
1910 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001911 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001912 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001913 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001914
1915 AutoMutex lock(mHardwareLock);
1916 mHardwareStatus = AUDIO_HW_SET_MODE;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07001917 mPrimaryHardwareDev->hwDevice()->setMode(mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001918 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001919 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001920 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001921 }
1922
Eric Laurentcf2c0212014-07-25 16:20:43 -07001923 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001924}
1925
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001926audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1927 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928{
1929 Mutex::Autolock _l(mLock);
1930 MixerThread *thread1 = checkMixerThread_l(output1);
1931 MixerThread *thread2 = checkMixerThread_l(output2);
1932
1933 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001934 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1935 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001936 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937 }
1938
Glenn Kasteneeecb982016-02-26 10:44:04 -08001939 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07001940 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941 thread->addOutputTrack(thread2);
1942 mPlaybackThreads.add(id, thread);
1943 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001944 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001945 return id;
1946}
1947
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001948status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001949{
Glenn Kastend96c5722012-04-25 13:44:49 -07001950 return closeOutput_nonvirtual(output);
1951}
1952
1953status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1954{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955 // keep strong reference on the playback thread so that
1956 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001957 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001958 {
1959 Mutex::Autolock _l(mLock);
1960 thread = checkPlaybackThread_l(output);
1961 if (thread == NULL) {
1962 return BAD_VALUE;
1963 }
1964
Steve Block3856b092011-10-20 11:56:00 +01001965 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001966
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001967 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001969 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001970 DuplicatingThread *dupThread =
1971 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001972 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001973 }
1974 }
1975 }
1976
1977
1978 mPlaybackThreads.removeItem(output);
1979 // save all effects to the default thread
1980 if (mPlaybackThreads.size()) {
1981 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1982 if (dstThread != NULL) {
1983 // audioflinger lock is held here so the acquisition order of thread locks does not
1984 // matter
1985 Mutex::Autolock _dl(dstThread->mLock);
1986 Mutex::Autolock _sl(thread->mLock);
1987 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1988 for (size_t i = 0; i < effectChains.size(); i ++) {
1989 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001990 }
1991 }
1992 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001993 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1994 ioDesc->mIoHandle = output;
1995 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001996 }
1997 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001998 // The thread entity (active unit of execution) is no longer running here,
1999 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002000
Eric Laurentf6870ae2015-05-08 10:50:03 -07002001 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07002002 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002003 }
Eric Laurent83b88082014-06-20 18:31:16 -07002004
Mathias Agopian65ab4712010-07-14 17:59:35 -07002005 return NO_ERROR;
2006}
2007
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002008void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002009{
2010 AudioStreamOut *out = thread->clearOutput();
2011 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2012 // from now on thread->mOutput is NULL
Eric Laurent83b88082014-06-20 18:31:16 -07002013 delete out;
2014}
2015
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002016void AudioFlinger::closeOutputInternal_l(const sp<PlaybackThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002017{
2018 mPlaybackThreads.removeItem(thread->mId);
2019 thread->exit();
2020 closeOutputFinish(thread);
2021}
2022
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002023status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024{
2025 Mutex::Autolock _l(mLock);
2026 PlaybackThread *thread = checkPlaybackThread_l(output);
2027
2028 if (thread == NULL) {
2029 return BAD_VALUE;
2030 }
2031
Steve Block3856b092011-10-20 11:56:00 +01002032 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002033 thread->suspend();
2034
2035 return NO_ERROR;
2036}
2037
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002038status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002039{
2040 Mutex::Autolock _l(mLock);
2041 PlaybackThread *thread = checkPlaybackThread_l(output);
2042
2043 if (thread == NULL) {
2044 return BAD_VALUE;
2045 }
2046
Steve Block3856b092011-10-20 11:56:00 +01002047 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002048
2049 thread->restore();
2050
2051 return NO_ERROR;
2052}
2053
Eric Laurentcf2c0212014-07-25 16:20:43 -07002054status_t AudioFlinger::openInput(audio_module_handle_t module,
2055 audio_io_handle_t *input,
2056 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002057 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002058 const String8& address,
2059 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002060 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002061{
Eric Laurent83b88082014-06-20 18:31:16 -07002062 Mutex::Autolock _l(mLock);
2063
Glenn Kastene7d66712015-03-05 13:46:52 -08002064 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002065 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002066 }
2067
Glenn Kastene7d66712015-03-05 13:46:52 -08002068 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002069
2070 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002071 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002072 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002073 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002074 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002075 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002076}
Dima Zavin799a70e2011-04-18 16:57:27 -07002077
Eric Laurent83b88082014-06-20 18:31:16 -07002078sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002079 audio_io_handle_t *input,
2080 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002081 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002082 const String8& address,
2083 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002084 audio_input_flags_t flags)
2085{
Glenn Kastene7d66712015-03-05 13:46:52 -08002086 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002087 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002088 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002089 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002090 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002091
Glenn Kasteneeecb982016-02-26 10:44:04 -08002092 // Audio Policy can request a specific handle for hardware hotword.
2093 // The goal here is not to re-open an already opened input.
2094 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002095 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002096 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2097 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2098 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2099 return 0;
2100 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2101 // This should not happen in a transient state with current design.
2102 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2103 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002104 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002105
Eric Laurentcf2c0212014-07-25 16:20:43 -07002106 audio_config_t halconfig = *config;
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002107 sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice();
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002108 sp<StreamInHalInterface> inStream;
2109 status_t status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002110 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002111 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002112 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002113 inStream.get(),
Eric Laurentcf2c0212014-07-25 16:20:43 -07002114 halconfig.sample_rate,
2115 halconfig.format,
2116 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002117 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002118 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002119
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002120 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002121 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002122 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002123 audio_is_linear_pcm(config->format) &&
2124 audio_is_linear_pcm(halconfig.format) &&
2125 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002126 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2127 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002128 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002129 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002130 inStream.clear();
2131 status = inHwHal->openInputStream(
Mikhail Naganove4f1f632016-08-31 11:35:10 -07002132 *input, devices, &halconfig, flags, address.string(), source, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07002133 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002134 }
2135
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002136 if (status == NO_ERROR && inStream != 0) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002137
Glenn Kasten46909e72013-02-26 09:20:22 -08002138#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002139 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2140 // or (re-)create if current Pipe is idle and does not match the new format
2141 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002142 enum {
2143 TEE_SINK_NO, // don't copy input
2144 TEE_SINK_NEW, // copy input using a new pipe
2145 TEE_SINK_OLD, // copy input using an existing pipe
2146 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002147 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2148 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002149 if (!mTeeSinkInputEnabled) {
2150 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002151 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002152 kind = TEE_SINK_NO;
2153 } else if (mRecordTeeSink == 0) {
2154 kind = TEE_SINK_NEW;
2155 } else if (mRecordTeeSink->getStrongCount() != 1) {
2156 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002157 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002158 kind = TEE_SINK_OLD;
2159 } else {
2160 kind = TEE_SINK_NEW;
2161 }
2162 switch (kind) {
2163 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002164 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002165 size_t numCounterOffers = 0;
2166 const NBAIO_Format offers[1] = {format};
2167 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2168 ALOG_ASSERT(index == 0);
2169 PipeReader *pipeReader = new PipeReader(*pipe);
2170 numCounterOffers = 0;
2171 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2172 ALOG_ASSERT(index == 0);
2173 mRecordTeeSink = pipe;
2174 mRecordTeeSource = pipeReader;
2175 teeSink = pipe;
2176 }
2177 break;
2178 case TEE_SINK_OLD:
2179 teeSink = mRecordTeeSink;
2180 break;
2181 case TEE_SINK_NO:
2182 default:
2183 break;
2184 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002185#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002186
Eric Laurent05067782016-06-01 18:27:28 -07002187 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07002188
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002189 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002190 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002191 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002192 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002193 inputStream,
2194 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002195 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002196 devices,
2197 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002198#ifdef TEE_SINK
2199 , teeSink
2200#endif
2201 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002202 mRecordThreads.add(*input, thread);
2203 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002204 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002205 }
2206
Eric Laurentcf2c0212014-07-25 16:20:43 -07002207 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002208 return 0;
2209}
2210
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002211status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002212{
Glenn Kastend96c5722012-04-25 13:44:49 -07002213 return closeInput_nonvirtual(input);
2214}
2215
2216status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2217{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218 // keep strong reference on the record thread so that
2219 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002220 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002221 {
2222 Mutex::Autolock _l(mLock);
2223 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002224 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002225 return BAD_VALUE;
2226 }
2227
Steve Block3856b092011-10-20 11:56:00 +01002228 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002229
2230 // If we still have effect chains, it means that a client still holds a handle
2231 // on at least one effect. We must either move the chain to an existing thread with the
2232 // same session ID or put it aside in case a new record thread is opened for a
2233 // new capture on the same session
2234 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002235 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002236 Mutex::Autolock _sl(thread->mLock);
2237 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002238 // Note: maximum one chain per record thread
2239 if (effectChains.size() != 0) {
2240 chain = effectChains[0];
2241 }
2242 }
2243 if (chain != 0) {
2244 // first check if a record thread is already opened with a client on the same session.
2245 // This should only happen in case of overlap between one thread tear down and the
2246 // creation of its replacement
2247 size_t i;
2248 for (i = 0; i < mRecordThreads.size(); i++) {
2249 sp<RecordThread> t = mRecordThreads.valueAt(i);
2250 if (t == thread) {
2251 continue;
2252 }
2253 if (t->hasAudioSession(chain->sessionId()) != 0) {
2254 Mutex::Autolock _l(t->mLock);
2255 ALOGV("closeInput() found thread %d for effect session %d",
2256 t->id(), chain->sessionId());
2257 t->addEffectChain_l(chain);
2258 break;
2259 }
2260 }
2261 // put the chain aside if we could not find a record thread with the same session id.
2262 if (i == mRecordThreads.size()) {
2263 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002264 }
2265 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002266 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2267 ioDesc->mIoHandle = input;
2268 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002269 mRecordThreads.removeItem(input);
2270 }
Eric Laurent83b88082014-06-20 18:31:16 -07002271 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2272 // we have a different lock for notification client
2273 closeInputFinish(thread);
2274 return NO_ERROR;
2275}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002276
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002277void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002278{
2279 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002280 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002281 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002282 // from now on thread->mInput is NULL
Dima Zavin799a70e2011-04-18 16:57:27 -07002283 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002284}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002285
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002286void AudioFlinger::closeInputInternal_l(const sp<RecordThread>& thread)
Eric Laurent83b88082014-06-20 18:31:16 -07002287{
2288 mRecordThreads.removeItem(thread->mId);
2289 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002290}
2291
Glenn Kastend2304db2014-02-03 07:40:31 -08002292status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002293{
2294 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002295 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002296
2297 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2298 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002299 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002300 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002301
2302 return NO_ERROR;
2303}
2304
2305
Glenn Kasteneeecb982016-02-26 10:44:04 -08002306audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002307{
Glenn Kasten9d003132016-04-06 14:38:09 -07002308 // This is a binder API, so a malicious client could pass in a bad parameter.
2309 // Check for that before calling the internal API nextUniqueId().
2310 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2311 ALOGE("newAudioUniqueId invalid use %d", use);
2312 return AUDIO_UNIQUE_ID_ALLOCATE;
2313 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002314 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002315}
2316
Glenn Kastend848eb42016-03-08 13:42:11 -08002317void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002318{
2319 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002320 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002321 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2322 if (pid != -1 && (caller == getpid_cached)) {
2323 caller = pid;
2324 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002325
Eric Laurent021cf962014-05-13 10:18:14 -07002326 {
2327 Mutex::Autolock _cl(mClientLock);
2328 // Ignore requests received from processes not known as notification client. The request
2329 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2330 // called from a different pid leaving a stale session reference. Also we don't know how
2331 // to clear this reference if the client process dies.
2332 if (mNotificationClients.indexOfKey(caller) < 0) {
2333 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2334 return;
2335 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002336 }
2337
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002338 size_t num = mAudioSessionRefs.size();
2339 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002340 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002341 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2342 ref->mCnt++;
2343 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002344 return;
2345 }
2346 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002347 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2348 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002349}
2350
Glenn Kastend848eb42016-03-08 13:42:11 -08002351void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002352{
2353 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002354 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002355 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2356 if (pid != -1 && (caller == getpid_cached)) {
2357 caller = pid;
2358 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002359 size_t num = mAudioSessionRefs.size();
2360 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002361 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002362 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2363 ref->mCnt--;
2364 ALOGV(" decremented refcount to %d", ref->mCnt);
2365 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002366 mAudioSessionRefs.removeAt(i);
2367 delete ref;
2368 purgeStaleEffects_l();
2369 }
2370 return;
2371 }
2372 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002373 // If the caller is mediaserver it is likely that the session being released was acquired
2374 // on behalf of a process not in notification clients and we ignore the warning.
2375 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002376}
2377
2378void AudioFlinger::purgeStaleEffects_l() {
2379
Steve Block3856b092011-10-20 11:56:00 +01002380 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002381
2382 Vector< sp<EffectChain> > chains;
2383
2384 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2385 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2386 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2387 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002388 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2389 chains.push(ec);
2390 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002391 }
2392 }
2393 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2394 sp<RecordThread> t = mRecordThreads.valueAt(i);
2395 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2396 sp<EffectChain> ec = t->mEffectChains[j];
2397 chains.push(ec);
2398 }
2399 }
2400
2401 for (size_t i = 0; i < chains.size(); i++) {
2402 sp<EffectChain> ec = chains[i];
2403 int sessionid = ec->sessionId();
2404 sp<ThreadBase> t = ec->mThread.promote();
2405 if (t == 0) {
2406 continue;
2407 }
2408 size_t numsessionrefs = mAudioSessionRefs.size();
2409 bool found = false;
2410 for (size_t k = 0; k < numsessionrefs; k++) {
2411 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002412 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002413 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002414 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002415 found = true;
2416 break;
2417 }
2418 }
2419 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002420 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002421 // remove all effects from the chain
2422 while (ec->mEffects.size()) {
2423 sp<EffectModule> effect = ec->mEffects[0];
2424 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002425 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002426 if (effect->purgeHandles()) {
2427 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002428 }
2429 AudioSystem::unregisterEffect(effect->id());
2430 }
2431 }
2432 }
2433 return;
2434}
2435
Glenn Kasteneeecb982016-02-26 10:44:04 -08002436// checkThread_l() must be called with AudioFlinger::mLock held
2437AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2438{
2439 ThreadBase *thread = NULL;
2440 switch (audio_unique_id_get_use(ioHandle)) {
2441 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2442 thread = checkPlaybackThread_l(ioHandle);
2443 break;
2444 case AUDIO_UNIQUE_ID_USE_INPUT:
2445 thread = checkRecordThread_l(ioHandle);
2446 break;
2447 default:
2448 break;
2449 }
2450 return thread;
2451}
2452
Mathias Agopian65ab4712010-07-14 17:59:35 -07002453// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002454AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002455{
Glenn Kastena1117922012-01-26 10:53:32 -08002456 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002457}
2458
2459// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002460AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002461{
2462 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002463 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002464}
2465
2466// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002467AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002468{
Glenn Kastena1117922012-01-26 10:53:32 -08002469 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002470}
2471
Glenn Kasteneeecb982016-02-26 10:44:04 -08002472audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002473{
Glenn Kasten9d003132016-04-06 14:38:09 -07002474 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002475 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002476 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2477 for (int retry = 0; retry < maxRetries; retry++) {
2478 // The cast allows wraparound from max positive to min negative instead of abort
2479 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2480 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2481 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2482 // allow wrap by skipping 0 and -1 for session ids
2483 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2484 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2485 return (audio_unique_id_t) (base | use);
2486 }
2487 }
2488 // We have no way of recovering from wraparound
2489 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2490 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002491}
2492
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002493AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002494{
2495 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2496 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002497 if(thread->isDuplicating()) {
2498 continue;
2499 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002500 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002501 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002502 return thread;
2503 }
2504 }
2505 return NULL;
2506}
2507
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002508audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002509{
2510 PlaybackThread *thread = primaryPlaybackThread_l();
2511
2512 if (thread == NULL) {
2513 return 0;
2514 }
2515
Eric Laurentf1c04f92012-08-28 14:26:53 -07002516 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002517}
2518
Glenn Kastena7335632016-06-09 17:09:53 -07002519AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
2520{
2521 size_t minFrameCount = 0;
2522 PlaybackThread *minThread = NULL;
2523 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2524 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2525 if (!thread->isDuplicating()) {
2526 size_t frameCount = thread->frameCountHAL();
2527 if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
2528 (frameCount == minFrameCount && thread->hasFastMixer() &&
2529 /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
2530 minFrameCount = frameCount;
2531 minThread = thread;
2532 }
2533 }
2534 }
2535 return minThread;
2536}
2537
Eric Laurenta011e352012-03-29 15:51:43 -07002538sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002539 audio_session_t triggerSession,
2540 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002541 sync_event_callback_t callBack,
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002542 const wp<RefBase>& cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002543{
2544 Mutex::Autolock _l(mLock);
2545
2546 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2547 status_t playStatus = NAME_NOT_FOUND;
2548 status_t recStatus = NAME_NOT_FOUND;
2549 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2550 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2551 if (playStatus == NO_ERROR) {
2552 return event;
2553 }
2554 }
2555 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2556 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2557 if (recStatus == NO_ERROR) {
2558 return event;
2559 }
2560 }
2561 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2562 mPendingSyncEvents.add(event);
2563 } else {
2564 ALOGV("createSyncEvent() invalid event %d", event->type());
2565 event.clear();
2566 }
2567 return event;
2568}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002569
Mathias Agopian65ab4712010-07-14 17:59:35 -07002570// ----------------------------------------------------------------------------
2571// Effect management
2572// ----------------------------------------------------------------------------
2573
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002574sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() {
2575 return mEffectsFactoryHal;
2576}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002577
Glenn Kastenf587ba52012-01-26 16:25:10 -08002578status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002579{
2580 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002581 if (mEffectsFactoryHal.get()) {
2582 return mEffectsFactoryHal->queryNumberEffects(numEffects);
2583 } else {
2584 return -ENODEV;
2585 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002586}
2587
Glenn Kastenf587ba52012-01-26 16:25:10 -08002588status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002589{
2590 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002591 if (mEffectsFactoryHal.get()) {
2592 return mEffectsFactoryHal->getDescriptor(index, descriptor);
2593 } else {
2594 return -ENODEV;
2595 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002596}
2597
Glenn Kasten5e92a782012-01-30 07:40:52 -08002598status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002599 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002600{
2601 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002602 if (mEffectsFactoryHal.get()) {
2603 return mEffectsFactoryHal->getDescriptor(pUuid, descriptor);
2604 } else {
2605 return -ENODEV;
2606 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002607}
2608
2609
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002610sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002611 effect_descriptor_t *pDesc,
2612 const sp<IEffectClient>& effectClient,
2613 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002614 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002615 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002616 const String16& opPackageName,
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
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002625 pid_t pid = IPCThreadState::self()->getCallingPid();
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002626 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
2627 pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002628
2629 if (pDesc == NULL) {
2630 lStatus = BAD_VALUE;
2631 goto Exit;
2632 }
2633
Eric Laurent84e9a102010-09-23 16:10:16 -07002634 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002635 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002636 lStatus = PERMISSION_DENIED;
2637 goto Exit;
2638 }
2639
Dima Zavinfce7a472011-04-19 22:30:36 -07002640 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002641 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002642 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002643 lStatus = PERMISSION_DENIED;
2644 goto Exit;
2645 }
2646
Mikhail Naganov1dc98672016-08-18 17:50:29 -07002647 if (mEffectsFactoryHal == 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002648 lStatus = NO_INIT;
2649 goto Exit;
2650 }
2651
Mathias Agopian65ab4712010-07-14 17:59:35 -07002652 {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002653 if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002654 // if uuid is specified, request effect descriptor
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002655 lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002656 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002657 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002658 goto Exit;
2659 }
2660 } else {
2661 // if uuid is not specified, look for an available implementation
2662 // of the required type in effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002663 if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002664 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002665 lStatus = BAD_VALUE;
2666 goto Exit;
2667 }
2668 uint32_t numEffects = 0;
2669 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002670 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002671 bool found = false;
2672
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002673 lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002674 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002675 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002676 goto Exit;
2677 }
2678 for (uint32_t i = 0; i < numEffects; i++) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07002679 lStatus = mEffectsFactoryHal->getDescriptor(i, &desc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002680 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002681 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002682 continue;
2683 }
2684 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2685 // If matching type found save effect descriptor. If the session is
2686 // 0 and the effect is not auxiliary, continue enumeration in case
2687 // an auxiliary version of this effect type is available
2688 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002689 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002690 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002691 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2692 break;
2693 }
2694 }
2695 }
2696 if (!found) {
2697 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002698 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002699 goto Exit;
2700 }
2701 // For same effect type, chose auxiliary version over insert version if
2702 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002703 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002704 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002705 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002706 }
2707 }
2708
2709 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002710 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002711 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2712 lStatus = INVALID_OPERATION;
2713 goto Exit;
2714 }
2715
Eric Laurent59255e42011-07-27 19:49:51 -07002716 // check recording permission for visualizer
2717 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002718 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002719 lStatus = PERMISSION_DENIED;
2720 goto Exit;
2721 }
2722
Mathias Agopian65ab4712010-07-14 17:59:35 -07002723 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002724 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002725 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002726 // if the output returned by getOutputForEffect() is removed before we lock the
2727 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2728 // and we will exit safely
2729 io = AudioSystem::getOutputForEffect(&desc);
2730 ALOGV("createEffect got output %d", io);
2731 }
2732
2733 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002734
2735 // If output is not specified try to find a matching audio session ID in one of the
2736 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002737 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2738 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002739 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002740 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002741 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2742 // output must be specified by AudioPolicyManager when using session
2743 // AUDIO_SESSION_OUTPUT_STAGE
2744 lStatus = BAD_VALUE;
2745 goto Exit;
2746 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002747 // look for the thread where the specified audio session is present
2748 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2749 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2750 io = mPlaybackThreads.keyAt(i);
2751 break;
2752 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002753 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002754 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002755 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2756 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2757 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002758 break;
2759 }
2760 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002761 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002762 // If no output thread contains the requested session ID, default to
2763 // first output. The effect chain will be moved to the correct output
2764 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002765 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002766 io = mPlaybackThreads.keyAt(0);
2767 }
Steve Block3856b092011-10-20 11:56:00 +01002768 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002769 }
2770 ThreadBase *thread = checkRecordThread_l(io);
2771 if (thread == NULL) {
2772 thread = checkPlaybackThread_l(io);
2773 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002774 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002775 lStatus = BAD_VALUE;
2776 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002777 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002778 } else {
2779 // Check if one effect chain was awaiting for an effect to be created on this
2780 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08002781 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07002782 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002783 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002784 thread->addEffectChain_l(chain);
2785 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002786 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002787
Eric Laurent021cf962014-05-13 10:18:14 -07002788 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002789
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002790 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002791 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2792 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002793 if (handle != 0 && id != NULL) {
2794 *id = handle->id();
2795 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002796 if (handle == 0) {
2797 // remove local strong reference to Client with mClientLock held
2798 Mutex::Autolock _cl(mClientLock);
2799 client.clear();
2800 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002801 }
2802
2803Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002804 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002805 return handle;
2806}
2807
Glenn Kastend848eb42016-03-08 13:42:11 -08002808status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002809 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002810{
Steve Block3856b092011-10-20 11:56:00 +01002811 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002812 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002813 Mutex::Autolock _l(mLock);
2814 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002815 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002816 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002817 }
Eric Laurentde070132010-07-13 04:45:46 -07002818 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2819 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002820 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002821 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002822 }
Eric Laurentde070132010-07-13 04:45:46 -07002823 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2824 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002825 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002826 return BAD_VALUE;
2827 }
2828
2829 Mutex::Autolock _dl(dstThread->mLock);
2830 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002831 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002832}
2833
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002834// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08002835status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002836 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002837 AudioFlinger::PlaybackThread *dstThread,
2838 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002839{
Steve Block3856b092011-10-20 11:56:00 +01002840 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002841 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002842
Eric Laurent59255e42011-07-27 19:49:51 -07002843 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002844 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002845 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002846 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002847 return INVALID_OPERATION;
2848 }
2849
Eric Laurent4c415062016-06-17 16:14:16 -07002850 // Check whether the destination thread and all effects in the chain are compatible
2851 if (!chain->isCompatibleWithThread_l(dstThread)) {
Andy Hung9a592762014-07-21 21:56:01 -07002852 ALOGW("moveEffectChain_l() effect chain failed because"
Eric Laurent4c415062016-06-17 16:14:16 -07002853 " destination thread %p is not compatible with effects in the chain",
2854 dstThread);
Andy Hung9a592762014-07-21 21:56:01 -07002855 return INVALID_OPERATION;
2856 }
2857
Eric Laurent39e94f82010-07-28 01:32:47 -07002858 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002859 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002860 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002861 // removed.
2862 srcThread->removeEffectChain_l(chain);
2863
2864 // transfer all effects one by one so that new effect chain is created on new thread with
2865 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002866 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002867 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002868 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002869 Vector< sp<EffectModule> > removed;
2870 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002871 while (effect != 0) {
2872 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002873 removed.add(effect);
2874 status = dstThread->addEffect_l(effect);
2875 if (status != NO_ERROR) {
2876 break;
2877 }
Eric Laurentec35a142011-10-05 17:42:25 -07002878 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2879 if (effect->state() == EffectModule::ACTIVE ||
2880 effect->state() == EffectModule::STOPPING) {
2881 effect->start();
2882 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002883 // if the move request is not received from audio policy manager, the effect must be
2884 // re-registered with the new strategy and output
2885 if (dstChain == 0) {
2886 dstChain = effect->chain().promote();
2887 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002888 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002889 status = NO_INIT;
2890 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002891 }
2892 strategy = dstChain->strategy();
2893 }
2894 if (reRegister) {
2895 AudioSystem::unregisterEffect(effect->id());
2896 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002897 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002898 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002899 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002900 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002901 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002902 }
Eric Laurentde070132010-07-13 04:45:46 -07002903 effect = chain->getEffectFromId_l(0);
2904 }
2905
Eric Laurent5baf2af2013-09-12 17:37:00 -07002906 if (status != NO_ERROR) {
2907 for (size_t i = 0; i < removed.size(); i++) {
2908 srcThread->addEffect_l(removed[i]);
2909 if (dstChain != 0 && reRegister) {
2910 AudioSystem::unregisterEffect(removed[i]->id());
2911 AudioSystem::registerEffect(&removed[i]->desc(),
2912 srcThread->id(),
2913 strategy,
2914 sessionId,
2915 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002916 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002917 }
2918 }
2919 }
2920
2921 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002922}
2923
Eric Laurent5baf2af2013-09-12 17:37:00 -07002924bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002925{
2926 if (mGlobalEffectEnableTime != 0 &&
2927 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2928 return true;
2929 }
2930
2931 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2932 sp<EffectChain> ec =
2933 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002934 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002935 return true;
2936 }
2937 }
2938 return false;
2939}
2940
Eric Laurent5baf2af2013-09-12 17:37:00 -07002941void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002942{
2943 Mutex::Autolock _l(mLock);
2944
2945 mGlobalEffectEnableTime = systemTime();
2946
2947 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2948 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2949 if (t->mType == ThreadBase::OFFLOAD) {
2950 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2951 }
2952 }
2953
2954}
2955
Eric Laurentaaa44472014-09-12 17:41:50 -07002956status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2957{
Glenn Kastend848eb42016-03-08 13:42:11 -08002958 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002959 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002960 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002961 if (index >= 0) {
2962 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2963 return ALREADY_EXISTS;
2964 }
2965 mOrphanEffectChains.add(session, chain);
2966 return NO_ERROR;
2967}
2968
2969sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2970{
2971 sp<EffectChain> chain;
2972 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002973 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002974 if (index >= 0) {
2975 chain = mOrphanEffectChains.valueAt(index);
2976 mOrphanEffectChains.removeItemsAt(index);
2977 }
2978 return chain;
2979}
2980
2981bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2982{
2983 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08002984 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002985 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002986 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002987 if (index >= 0) {
2988 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2989 if (chain->removeEffect_l(effect) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002990 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002991 mOrphanEffectChains.removeItemsAt(index);
2992 }
2993 return true;
2994 }
2995 return false;
2996}
2997
2998
Glenn Kastenda6ef132013-01-10 12:31:01 -08002999struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003000#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
3001 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003002};
3003
3004int comparEntry(const void *p1, const void *p2)
3005{
Glenn Kasten2f55e762015-03-05 16:05:08 -08003006 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003007}
3008
Glenn Kasten46909e72013-02-26 09:20:22 -08003009#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08003010void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07003011{
Eric Laurent81784c32012-11-19 14:55:58 -08003012 NBAIO_Source *teeSource = source.get();
3013 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003014 // .wav rotation
3015 // There is a benign race condition if 2 threads call this simultaneously.
3016 // They would both traverse the directory, but the result would simply be
3017 // failures at unlink() which are ignored. It's also unlikely since
3018 // normally dumpsys is only done by bugreport or from the command line.
3019 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08003020 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08003021 size_t teePathLen = strlen(teePath);
3022 DIR *dir = opendir(teePath);
3023 teePath[teePathLen++] = '/';
3024 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08003025#define TEE_MAX_SORT 20 // number of entries to sort
3026#define TEE_MAX_KEEP 10 // number of entries to keep
3027 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08003028 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08003029 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003030 struct dirent de;
3031 struct dirent *result = NULL;
3032 int rc = readdir_r(dir, &de, &result);
3033 if (rc != 0) {
3034 ALOGW("readdir_r failed %d", rc);
3035 break;
3036 }
3037 if (result == NULL) {
3038 break;
3039 }
3040 if (result != &de) {
3041 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
3042 break;
3043 }
3044 // ignore non .wav file entries
3045 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003046 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08003047 strcmp(&de.d_name[nameLen - 4], ".wav")) {
3048 continue;
3049 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08003050 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003051 }
3052 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003053 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003054 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003055 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
3056 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003057 (void) unlink(teePath);
3058 }
3059 }
3060 } else {
3061 if (fd >= 0) {
Glenn Kastenfbd87e82016-07-18 15:45:56 -07003062 dprintf(fd, "unable to rotate tees in %.*s: %s\n", (int) teePathLen, teePath,
Glenn Kasten9a003992016-02-23 15:24:34 -08003063 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003064 }
3065 }
Eric Laurent81784c32012-11-19 14:55:58 -08003066 char teeTime[16];
3067 struct timeval tv;
3068 gettimeofday(&tv, NULL);
3069 struct tm tm;
3070 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003071 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
3072 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
3073 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3074 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003075 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003076 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003077 char wavHeader[44];
3078 memcpy(wavHeader,
3079 "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",
3080 sizeof(wavHeader));
3081 NBAIO_Format format = teeSource->format();
3082 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003083 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003084 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003085 wavHeader[22] = channelCount; // number of channels
3086 wavHeader[24] = sampleRate; // sample rate
3087 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003088 wavHeader[32] = frameSize; // block alignment
3089 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003090 write(teeFd, wavHeader, sizeof(wavHeader));
3091 size_t total = 0;
3092 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003093#define TEE_SINK_READ 1024 // frames per I/O operation
3094 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003095 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003096 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003097 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003098 bool wasFirstRead = firstRead;
3099 firstRead = false;
3100 if (actual <= 0) {
3101 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3102 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003103 }
Eric Laurent81784c32012-11-19 14:55:58 -08003104 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003105 }
Eric Laurent81784c32012-11-19 14:55:58 -08003106 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003107 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003108 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003109 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003110 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003111 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003112 uint32_t temp = 44 + total * frameSize - 8;
3113 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003114 write(teeFd, &temp, sizeof(temp));
3115 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003116 temp = total * frameSize;
3117 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003118 write(teeFd, &temp, sizeof(temp));
3119 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003120 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003121 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003122 }
Eric Laurent59255e42011-07-27 19:49:51 -07003123 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003124 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003125 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003126 }
Eric Laurent59255e42011-07-27 19:49:51 -07003127 }
3128 }
3129}
Glenn Kasten46909e72013-02-26 09:20:22 -08003130#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003131
Mathias Agopian65ab4712010-07-14 17:59:35 -07003132// ----------------------------------------------------------------------------
3133
3134status_t AudioFlinger::onTransact(
3135 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3136{
3137 return BnAudioFlinger::onTransact(code, data, reply, flags);
3138}
3139
Glenn Kasten63238ef2015-03-02 15:50:29 -08003140} // namespace android