blob: 1b69f8caa8b163e4abe7880212cf898091b84a33 [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>
Andy Hung35fec5f2016-04-13 14:21:48 -070034#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/String16.h>
36#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070037#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
Dima Zavinfce7a472011-04-19 22:30:36 -070039#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070040#include <cutils/properties.h>
41
Dima Zavin64760242011-05-11 14:15:23 -070042#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070043#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
45#include "AudioMixer.h"
46#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080047#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
Andy Hung6770c6f2015-04-07 13:43:36 -070049#include <media/AudioResamplerPublic.h>
50
Mathias Agopian65ab4712010-07-14 17:59:35 -070051#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070052#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070053#include <audio_effects/effect_ns.h>
54#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070055
Glenn Kasten3b21c502011-12-15 09:52:39 -080056#include <audio_utils/primitives.h>
57
Eric Laurentfeb0db62011-07-22 09:04:31 -070058#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080059
Glenn Kasten9e58b552013-01-18 15:09:48 -080060#include <media/IMediaLogService.h>
61
Glenn Kastenda6ef132013-01-10 12:31:01 -080062#include <media/nbaio/Pipe.h>
63#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070064#include <media/AudioParameter.h>
Wei Jia3f273d12015-11-24 09:06:49 -080065#include <mediautils/BatteryNotifier.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070066#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080067
Mathias Agopian65ab4712010-07-14 17:59:35 -070068// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070069
John Grossman1c345192012-03-27 14:00:17 -070070// Note: the following macro is used for extremely verbose logging message. In
71// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
72// 0; but one side effect of this is to turn all LOGV's as well. Some messages
73// are so verbose that we want to suppress them even when we have ALOG_ASSERT
74// turned on. Do not uncomment the #def below unless you really know what you
75// are doing and want to see all of the extremely verbose messages.
76//#define VERY_VERY_VERBOSE_LOGGING
77#ifdef VERY_VERY_VERBOSE_LOGGING
78#define ALOGVV ALOGV
79#else
80#define ALOGVV(a...) do { } while(0)
81#endif
Eric Laurentde070132010-07-13 04:45:46 -070082
Mathias Agopian65ab4712010-07-14 17:59:35 -070083namespace android {
84
Glenn Kastenec1d6b52011-12-12 09:04:45 -080085static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
86static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070087static const char kClientLockedString[] = "Client lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070088
Glenn Kasten58912562012-04-03 10:45:00 -070089
John Grossman4ff14ba2012-02-08 16:37:41 -080090nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080091
Eric Laurent81784c32012-11-19 14:55:58 -080092uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070093
Glenn Kasten46909e72013-02-26 09:20:22 -080094#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080095bool AudioFlinger::mTeeSinkInputEnabled = false;
96bool AudioFlinger::mTeeSinkOutputEnabled = false;
97bool AudioFlinger::mTeeSinkTrackEnabled = false;
98
99size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
100size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
101size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800102#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800103
Eric Laurent813e2a72013-08-31 12:59:48 -0700104// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
105// we define a minimum time during which a global effect is considered enabled.
106static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
107
Mathias Agopian65ab4712010-07-14 17:59:35 -0700108// ----------------------------------------------------------------------------
109
Marco Nelissenb2208842014-02-07 14:00:50 -0800110const char *formatToString(audio_format_t format) {
Phil Burkfdb3c072016-02-09 10:47:02 -0800111 switch (audio_get_main_format(format)) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530112 case AUDIO_FORMAT_PCM:
113 switch (format) {
114 case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
115 case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
116 case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
117 case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
118 case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
119 case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
120 default:
121 break;
122 }
123 break;
Marco Nelissenb2208842014-02-07 14:00:50 -0800124 case AUDIO_FORMAT_MP3: return "mp3";
125 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
126 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
127 case AUDIO_FORMAT_AAC: return "aac";
128 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
129 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
130 case AUDIO_FORMAT_VORBIS: return "vorbis";
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530131 case AUDIO_FORMAT_OPUS: return "opus";
132 case AUDIO_FORMAT_AC3: return "ac-3";
133 case AUDIO_FORMAT_E_AC3: return "e-ac-3";
Phil Burkfdb3c072016-02-09 10:47:02 -0800134 case AUDIO_FORMAT_IEC61937: return "iec61937";
Marco Nelissenb2208842014-02-07 14:00:50 -0800135 default:
136 break;
137 }
138 return "unknown";
139}
140
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700141static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700142{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700143 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700144 int rc;
145
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700146 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
147 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
148 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
149 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700150 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700151 }
152 rc = audio_hw_device_open(mod, dev);
153 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
154 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
155 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700156 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700157 }
Eric Laurent5806b352014-05-22 12:23:26 -0700158 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700159 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
160 rc = BAD_VALUE;
161 goto out;
162 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700163 return 0;
164
165out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700166 *dev = NULL;
167 return rc;
168}
169
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170// ----------------------------------------------------------------------------
171
172AudioFlinger::AudioFlinger()
173 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800174 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800175 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700176 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800177 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800178 mMasterMute(false),
Glenn Kasteneeecb982016-02-26 10:44:04 -0800179 mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX), // zero has a special meaning, so unavailable
John Grossman4ff14ba2012-02-08 16:37:41 -0800180 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700181 mBtNrecIsOff(false),
182 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700183 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700184 mGlobalEffectEnableTime(0),
Eric Laurent72e3f392015-05-20 14:43:50 -0700185 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186{
Glenn Kasten949a9262013-04-16 12:35:20 -0700187 getpid_cached = getpid();
Glenn Kastenae0cff12016-02-24 13:57:49 -0800188 const bool doLog = property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800189 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800190 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
191 MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800192 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700193
Wei Jia3f273d12015-11-24 09:06:49 -0800194 // reset battery stats.
195 // if the audio service has crashed, battery stats could be left
196 // in bad state, reset the state upon service start.
197 BatteryNotifier::getInstance().noteResetAudio();
198
Glenn Kasten46909e72013-02-26 09:20:22 -0800199#ifdef TEE_SINK
Glenn Kasten9a003992016-02-23 15:24:34 -0800200 char value[PROPERTY_VALUE_MAX];
Glenn Kastenda6ef132013-01-10 12:31:01 -0800201 (void) property_get("ro.debuggable", value, "0");
202 int debuggable = atoi(value);
203 int teeEnabled = 0;
204 if (debuggable) {
205 (void) property_get("af.tee", value, "0");
206 teeEnabled = atoi(value);
207 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800208 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700209 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800210 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700211 }
212 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800213 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700214 }
215 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800216 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700217 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800218#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700219}
220
221void AudioFlinger::onFirstRef()
222{
Eric Laurent93575202011-01-18 18:39:02 -0800223 Mutex::Autolock _l(mLock);
224
Dima Zavin799a70e2011-04-18 16:57:27 -0700225 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800226 char val_str[PROPERTY_VALUE_MAX] = { 0 };
227 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
228 uint32_t int_val;
229 if (1 == sscanf(val_str, "%u", &int_val)) {
230 mStandbyTimeInNsecs = milliseconds(int_val);
231 ALOGI("Using %u mSec as standby time.", int_val);
232 } else {
233 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
234 ALOGI("Using default %u mSec as standby time.",
235 (uint32_t)(mStandbyTimeInNsecs / 1000000));
236 }
237 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238
Eric Laurent1c333e22014-05-20 10:48:17 -0700239 mPatchPanel = new PatchPanel(this);
240
Eric Laurenta4c5a552012-03-29 10:12:40 -0700241 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242}
243
244AudioFlinger::~AudioFlinger()
245{
246 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700247 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700248 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249 }
250 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700251 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700252 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700254
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800255 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
256 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700257 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
258 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700260
261 // Tell media.log service about any old writers that still need to be unregistered
Andy Hungce717682015-12-22 13:40:32 -0800262 if (mLogMemoryDealer != 0) {
263 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
264 if (binder != 0) {
265 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
266 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
267 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
268 mUnregisteredWriters.pop();
269 mediaLogService->unregisterWriter(iMemory);
270 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700271 }
272 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273}
274
Eric Laurenta4c5a552012-03-29 10:12:40 -0700275static const char * const audio_interfaces[] = {
276 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
277 AUDIO_HARDWARE_MODULE_ID_A2DP,
278 AUDIO_HARDWARE_MODULE_ID_USB,
279};
280#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
281
Phil Burk062e67a2015-02-11 13:40:50 -0800282AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700283 audio_module_handle_t module,
284 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700285{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700286 // if module is 0, the request comes from an old policy manager and we should load
287 // well known modules
288 if (module == 0) {
289 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
290 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
291 loadHwModule_l(audio_interfaces[i]);
292 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700293 // then try to find a module supporting the requested device.
294 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
295 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
296 audio_hw_device_t *dev = audioHwDevice->hwDevice();
297 if ((dev->get_supported_devices != NULL) &&
298 (dev->get_supported_devices(dev) & devices) == devices)
299 return audioHwDevice;
300 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700301 } else {
302 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700303 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
304 if (audioHwDevice != NULL) {
305 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700306 }
307 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700308
Dima Zavin799a70e2011-04-18 16:57:27 -0700309 return NULL;
310}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700311
Glenn Kasten0f11b512014-01-31 16:18:54 -0800312void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313{
314 const size_t SIZE = 256;
315 char buffer[SIZE];
316 String8 result;
317
318 result.append("Clients:\n");
319 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800320 sp<Client> client = mClients.valueAt(i).promote();
321 if (client != 0) {
322 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
323 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 }
325 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700326
Eric Laurentd1b28d42013-09-18 18:47:13 -0700327 result.append("Notification Clients:\n");
328 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
329 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
330 result.append(buffer);
331 }
332
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700333 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800334 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700335 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
336 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800337 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700338 result.append(buffer);
339 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700340 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341}
342
343
Glenn Kasten0f11b512014-01-31 16:18:54 -0800344void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345{
346 const size_t SIZE = 256;
347 char buffer[SIZE];
348 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800349 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350
John Grossman4ff14ba2012-02-08 16:37:41 -0800351 snprintf(buffer, SIZE, "Hardware status: %d\n"
352 "Standby Time mSec: %u\n",
353 hardwareStatus,
354 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355 result.append(buffer);
356 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357}
358
Glenn Kasten0f11b512014-01-31 16:18:54 -0800359void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360{
361 const size_t SIZE = 256;
362 char buffer[SIZE];
363 String8 result;
364 snprintf(buffer, SIZE, "Permission Denial: "
365 "can't dump AudioFlinger from pid=%d, uid=%d\n",
366 IPCThreadState::self()->getCallingPid(),
367 IPCThreadState::self()->getCallingUid());
368 result.append(buffer);
369 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370}
371
Eric Laurent81784c32012-11-19 14:55:58 -0800372bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373{
374 bool locked = false;
375 for (int i = 0; i < kDumpLockRetries; ++i) {
376 if (mutex.tryLock() == NO_ERROR) {
377 locked = true;
378 break;
379 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800380 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381 }
382 return locked;
383}
384
385status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
386{
Glenn Kasten44deb052012-02-05 18:09:08 -0800387 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388 dumpPermissionDenial(fd, args);
389 } else {
390 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800391 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392 if (!hardwareLocked) {
393 String8 result(kHardwareLockedString);
394 write(fd, result.string(), result.size());
395 } else {
396 mHardwareLock.unlock();
397 }
398
Eric Laurent81784c32012-11-19 14:55:58 -0800399 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400
401 // failed to lock - AudioFlinger is probably deadlocked
402 if (!locked) {
403 String8 result(kDeadlockedString);
404 write(fd, result.string(), result.size());
405 }
406
Eric Laurent021cf962014-05-13 10:18:14 -0700407 bool clientLocked = dumpTryLock(mClientLock);
408 if (!clientLocked) {
409 String8 result(kClientLockedString);
410 write(fd, result.string(), result.size());
411 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700412
413 EffectDumpEffects(fd);
414
Mathias Agopian65ab4712010-07-14 17:59:35 -0700415 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700416 if (clientLocked) {
417 mClientLock.unlock();
418 }
419
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420 dumpInternals(fd, args);
421
422 // dump playback threads
423 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
424 mPlaybackThreads.valueAt(i)->dump(fd, args);
425 }
426
427 // dump record threads
428 for (size_t i = 0; i < mRecordThreads.size(); i++) {
429 mRecordThreads.valueAt(i)->dump(fd, args);
430 }
431
Eric Laurentaaa44472014-09-12 17:41:50 -0700432 // dump orphan effect chains
433 if (mOrphanEffectChains.size() != 0) {
434 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
435 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
436 mOrphanEffectChains.valueAt(i)->dump(fd, args);
437 }
438 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700439 // dump all hardware devs
440 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700441 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700442 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700444
Glenn Kasten46909e72013-02-26 09:20:22 -0800445#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700446 // dump the serially shared record tee sink
447 if (mRecordTeeSource != 0) {
448 dumpTee(fd, mRecordTeeSource);
449 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800450#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700451
Glenn Kastend65d73c2012-06-22 17:21:07 -0700452 if (locked) {
453 mLock.unlock();
454 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800455
456 // append a copy of media.log here by forwarding fd to it, but don't attempt
457 // to lookup the service if it's not running, as it will block for a second
458 if (mLogMemoryDealer != 0) {
459 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
460 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700461 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800462 Vector<String16> args;
463 binder->dump(fd, args);
464 }
465 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700466
467 // check for optional arguments
468 bool unreachableMemory = false;
469 for (const auto &arg : args) {
470 if (arg == String16("--unreachable")) {
471 unreachableMemory = true;
472 }
473 }
474
475 if (unreachableMemory) {
476 dprintf(fd, "\nDumping unreachable memory:\n");
477 // TODO - should limit be an argument parameter?
478 std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */);
479 write(fd, s.c_str(), s.size());
480 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700481 }
482 return NO_ERROR;
483}
484
Eric Laurent021cf962014-05-13 10:18:14 -0700485sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800486{
Eric Laurent021cf962014-05-13 10:18:14 -0700487 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800488 // If pid is already in the mClients wp<> map, then use that entry
489 // (for which promote() is always != 0), otherwise create a new entry and Client.
490 sp<Client> client = mClients.valueFor(pid).promote();
491 if (client == 0) {
492 client = new Client(this, pid);
493 mClients.add(pid, client);
494 }
495
496 return client;
497}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700498
Glenn Kasten9e58b552013-01-18 15:09:48 -0800499sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
500{
Glenn Kasten481fb672013-09-30 14:39:28 -0700501 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800502 if (mLogMemoryDealer == 0) {
503 return new NBLog::Writer();
504 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800505 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700506 // Similarly if we can't contact the media.log service, also return a dummy writer
507 if (binder == 0) {
508 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800509 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700510 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
511 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
512 // If allocation fails, consult the vector of previously unregistered writers
513 // and garbage-collect one or more them until an allocation succeeds
514 if (shared == 0) {
515 Mutex::Autolock _l(mUnregisteredWritersLock);
516 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
517 {
518 // Pick the oldest stale writer to garbage-collect
519 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
520 mUnregisteredWriters.removeAt(0);
521 mediaLogService->unregisterWriter(iMemory);
522 // Now the media.log remote reference to IMemory is gone. When our last local
523 // reference to IMemory also drops to zero at end of this block,
524 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
525 }
526 // Re-attempt the allocation
527 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
528 if (shared != 0) {
529 goto success;
530 }
531 }
532 // Even after garbage-collecting all old writers, there is still not enough memory,
533 // so return a dummy writer
534 return new NBLog::Writer();
535 }
536success:
537 mediaLogService->registerWriter(shared, size, name);
538 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800539}
540
541void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
542{
Glenn Kasten685ef092013-02-04 08:15:34 -0800543 if (writer == 0) {
544 return;
545 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800546 sp<IMemory> iMemory(writer->getIMemory());
547 if (iMemory == 0) {
548 return;
549 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700550 // Rather than removing the writer immediately, append it to a queue of old writers to
551 // be garbage-collected later. This allows us to continue to view old logs for a while.
552 Mutex::Autolock _l(mUnregisteredWritersLock);
553 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800554}
555
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556// IAudioFlinger interface
557
558
559sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800560 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800562 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700563 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800564 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800565 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700566 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800567 audio_io_handle_t output,
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
Glenn Kasten263709e2012-01-06 08:40:01 -0800579 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
580 // but if someone uses binder directly they could bypass that and cause us to crash
581 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000582 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583 lStatus = BAD_VALUE;
584 goto Exit;
585 }
586
Glenn Kasten53b5d092014-02-05 10:00:23 -0800587 // further sample rate checks are performed by createTrack_l() depending on the thread type
588 if (sampleRate == 0) {
589 ALOGE("createTrack() invalid sample rate %u", sampleRate);
590 lStatus = BAD_VALUE;
591 goto Exit;
592 }
593
594 // further channel mask checks are performed by createTrack_l() depending on the thread type
595 if (!audio_is_output_channel(channelMask)) {
596 ALOGE("createTrack() invalid channel mask %#x", channelMask);
597 lStatus = BAD_VALUE;
598 goto Exit;
599 }
600
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700601 // further format checks are performed by createTrack_l() depending on the thread type
602 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800603 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700604 lStatus = BAD_VALUE;
605 goto Exit;
606 }
607
Glenn Kasten663c2242013-09-24 11:52:37 -0700608 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
609 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
610 lStatus = BAD_VALUE;
611 goto Exit;
612 }
613
Mathias Agopian65ab4712010-07-14 17:59:35 -0700614 {
615 Mutex::Autolock _l(mLock);
616 PlaybackThread *thread = checkPlaybackThread_l(output);
617 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800618 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619 lStatus = BAD_VALUE;
620 goto Exit;
621 }
622
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800623 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700624 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625
Glenn Kastene848bd92014-03-13 15:00:32 -0700626 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700627 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800628 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
629 ALOGE("createTrack() invalid session ID %d", *sessionId);
630 lStatus = BAD_VALUE;
631 goto Exit;
632 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700633 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700634 // check if an effect chain with the same session ID is present on another
635 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700636 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700637 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
638 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700639 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700640 if (sessions & PlaybackThread::EFFECT_SESSION) {
641 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700642 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700643 }
Eric Laurentde070132010-07-13 04:45:46 -0700644 }
645 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700647 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800648 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649 if (sessionId != NULL) {
650 *sessionId = lSessionId;
651 }
652 }
Steve Block3856b092011-10-20 11:56:00 +0100653 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654
655 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800656 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800657 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700658 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700659
660 // move effect chain to this output thread if an effect on same session was waiting
661 // for a track to be created
662 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700663 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700664 Mutex::Autolock _dl(thread->mLock);
665 Mutex::Autolock _sl(effectThread->mLock);
666 moveEffectChain_l(lSessionId, effectThread, thread, true);
667 }
Eric Laurenta011e352012-03-29 15:51:43 -0700668
669 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700670 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700671 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
672 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700673 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700674 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700675 } else {
676 mPendingSyncEvents[i]->cancel();
677 }
Eric Laurenta011e352012-03-29 15:51:43 -0700678 mPendingSyncEvents.removeAt(i);
679 i--;
680 }
681 }
682 }
Glenn Kastene198c362013-08-13 09:13:36 -0700683
Glenn Kastend848eb42016-03-08 13:42:11 -0800684 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 }
Glenn Kastene198c362013-08-13 09:13:36 -0700686
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700687 if (lStatus != NO_ERROR) {
688 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700689 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700690 // Don't hold mClientLock when releasing the reference on the track as the
691 // destructor will acquire it.
692 {
693 Mutex::Autolock _cl(mClientLock);
694 client.clear();
695 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700697 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698 }
699
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700700 // return handle to client
701 trackHandle = new TrackHandle(track);
702
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700704 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705 return trackHandle;
706}
707
Glenn Kasten2c073da2016-02-26 09:14:08 -0800708uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709{
710 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800711 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800713 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 return 0;
715 }
716 return thread->sampleRate();
717}
718
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800719audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720{
721 Mutex::Autolock _l(mLock);
722 PlaybackThread *thread = checkPlaybackThread_l(output);
723 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000724 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800725 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726 }
727 return thread->format();
728}
729
Glenn Kasten2c073da2016-02-26 09:14:08 -0800730size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700731{
732 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800733 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800735 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700736 return 0;
737 }
Glenn Kasten58912562012-04-03 10:45:00 -0700738 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
739 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740 return thread->frameCount();
741}
742
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800743uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700744{
745 Mutex::Autolock _l(mLock);
746 PlaybackThread *thread = checkPlaybackThread_l(output);
747 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800748 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749 return 0;
750 }
751 return thread->latency();
752}
753
754status_t AudioFlinger::setMasterVolume(float value)
755{
Eric Laurenta1884f92011-08-23 08:25:03 -0700756 status_t ret = initCheck();
757 if (ret != NO_ERROR) {
758 return ret;
759 }
760
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761 // check calling permissions
762 if (!settingsAllowed()) {
763 return PERMISSION_DENIED;
764 }
765
Eric Laurenta4c5a552012-03-29 10:12:40 -0700766 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700767 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700768
John Grossmanee578c02012-07-23 17:05:46 -0700769 // Set master volume in the HALs which support it.
770 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
771 AutoMutex lock(mHardwareLock);
772 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800773
John Grossmanee578c02012-07-23 17:05:46 -0700774 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
775 if (dev->canSetMasterVolume()) {
776 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800777 }
John Grossmanee578c02012-07-23 17:05:46 -0700778 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700780
John Grossmanee578c02012-07-23 17:05:46 -0700781 // Now set the master volume in each playback thread. Playback threads
782 // assigned to HALs which do not have master volume support will apply
783 // master volume during the mix operation. Threads with HALs which do
784 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700785 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
786 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
787 continue;
788 }
John Grossmanee578c02012-07-23 17:05:46 -0700789 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700790 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791
792 return NO_ERROR;
793}
794
Glenn Kastenf78aee72012-01-04 11:00:47 -0800795status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796{
Eric Laurenta1884f92011-08-23 08:25:03 -0700797 status_t ret = initCheck();
798 if (ret != NO_ERROR) {
799 return ret;
800 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801
802 // check calling permissions
803 if (!settingsAllowed()) {
804 return PERMISSION_DENIED;
805 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800806 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000807 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808 return BAD_VALUE;
809 }
810
811 { // scope for the lock
812 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700813 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700815 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 mHardwareStatus = AUDIO_HW_IDLE;
817 }
818
819 if (NO_ERROR == ret) {
820 Mutex::Autolock _l(mLock);
821 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800822 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700823 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824 }
825
826 return ret;
827}
828
829status_t AudioFlinger::setMicMute(bool state)
830{
Eric Laurenta1884f92011-08-23 08:25:03 -0700831 status_t ret = initCheck();
832 if (ret != NO_ERROR) {
833 return ret;
834 }
835
Mathias Agopian65ab4712010-07-14 17:59:35 -0700836 // check calling permissions
837 if (!settingsAllowed()) {
838 return PERMISSION_DENIED;
839 }
840
841 AutoMutex lock(mHardwareLock);
842 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700843 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
844 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
845 status_t result = dev->set_mic_mute(dev, state);
846 if (result != NO_ERROR) {
847 ret = result;
848 }
849 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850 mHardwareStatus = AUDIO_HW_IDLE;
851 return ret;
852}
853
854bool AudioFlinger::getMicMute() const
855{
Eric Laurenta1884f92011-08-23 08:25:03 -0700856 status_t ret = initCheck();
857 if (ret != NO_ERROR) {
858 return false;
859 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800860 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700861 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800862 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800864 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
865 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
866 status_t result = dev->get_mic_mute(dev, &state);
867 if (result == NO_ERROR) {
868 mute = mute && state;
869 }
870 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800872
873 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700874}
875
876status_t AudioFlinger::setMasterMute(bool muted)
877{
John Grossmand8f178d2012-07-20 14:51:35 -0700878 status_t ret = initCheck();
879 if (ret != NO_ERROR) {
880 return ret;
881 }
882
Mathias Agopian65ab4712010-07-14 17:59:35 -0700883 // check calling permissions
884 if (!settingsAllowed()) {
885 return PERMISSION_DENIED;
886 }
887
John Grossmanee578c02012-07-23 17:05:46 -0700888 Mutex::Autolock _l(mLock);
889 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700890
John Grossmanee578c02012-07-23 17:05:46 -0700891 // Set master mute in the HALs which support it.
892 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
893 AutoMutex lock(mHardwareLock);
894 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700895
John Grossmanee578c02012-07-23 17:05:46 -0700896 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
897 if (dev->canSetMasterMute()) {
898 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700899 }
John Grossmanee578c02012-07-23 17:05:46 -0700900 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700901 }
902
John Grossmanee578c02012-07-23 17:05:46 -0700903 // Now set the master mute in each playback thread. Playback threads
904 // assigned to HALs which do not have master mute support will apply master
905 // mute during the mix operation. Threads with HALs which do support master
906 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700907 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
908 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
909 continue;
910 }
John Grossmanee578c02012-07-23 17:05:46 -0700911 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700912 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913
914 return NO_ERROR;
915}
916
917float AudioFlinger::masterVolume() const
918{
Glenn Kasten98067102011-12-13 11:47:54 -0800919 Mutex::Autolock _l(mLock);
920 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921}
922
923bool AudioFlinger::masterMute() const
924{
Glenn Kasten98067102011-12-13 11:47:54 -0800925 Mutex::Autolock _l(mLock);
926 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927}
928
John Grossman4ff14ba2012-02-08 16:37:41 -0800929float AudioFlinger::masterVolume_l() const
930{
John Grossman4ff14ba2012-02-08 16:37:41 -0800931 return mMasterVolume;
932}
933
John Grossmand8f178d2012-07-20 14:51:35 -0700934bool AudioFlinger::masterMute_l() const
935{
John Grossmanee578c02012-07-23 17:05:46 -0700936 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700937}
938
Eric Laurent223fd5c2014-11-11 13:43:36 -0800939status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
940{
941 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
942 ALOGW("setStreamVolume() invalid stream %d", stream);
943 return BAD_VALUE;
944 }
945 pid_t caller = IPCThreadState::self()->getCallingPid();
946 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
947 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
948 return PERMISSION_DENIED;
949 }
950
951 return NO_ERROR;
952}
953
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800954status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
955 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956{
957 // check calling permissions
958 if (!settingsAllowed()) {
959 return PERMISSION_DENIED;
960 }
961
Eric Laurent223fd5c2014-11-11 13:43:36 -0800962 status_t status = checkStreamType(stream);
963 if (status != NO_ERROR) {
964 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800966 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967
968 AutoMutex lock(mLock);
969 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700970 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 thread = checkPlaybackThread_l(output);
972 if (thread == NULL) {
973 return BAD_VALUE;
974 }
975 }
976
977 mStreamTypes[stream].volume = value;
978
979 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800980 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700981 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700982 }
983 } else {
984 thread->setStreamVolume(stream, value);
985 }
986
987 return NO_ERROR;
988}
989
Glenn Kastenfff6d712012-01-12 16:38:12 -0800990status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991{
992 // check calling permissions
993 if (!settingsAllowed()) {
994 return PERMISSION_DENIED;
995 }
996
Eric Laurent223fd5c2014-11-11 13:43:36 -0800997 status_t status = checkStreamType(stream);
998 if (status != NO_ERROR) {
999 return status;
1000 }
1001 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1002
1003 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001004 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001005 return BAD_VALUE;
1006 }
1007
Eric Laurent93575202011-01-18 18:39:02 -08001008 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001009 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -07001010 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001011 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012
1013 return NO_ERROR;
1014}
1015
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001016float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001017{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001018 status_t status = checkStreamType(stream);
1019 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001020 return 0.0f;
1021 }
1022
1023 AutoMutex lock(mLock);
1024 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001025 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 PlaybackThread *thread = checkPlaybackThread_l(output);
1027 if (thread == NULL) {
1028 return 0.0f;
1029 }
1030 volume = thread->streamVolume(stream);
1031 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001032 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001033 }
1034
1035 return volume;
1036}
1037
Glenn Kastenfff6d712012-01-12 16:38:12 -08001038bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001040 status_t status = checkStreamType(stream);
1041 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042 return true;
1043 }
1044
Glenn Kasten6637baa2012-01-09 09:40:36 -08001045 AutoMutex lock(mLock);
1046 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047}
1048
Eric Laurent054d9d32015-04-24 08:48:48 -07001049
1050void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1051{
1052 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1053 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1054 }
1055}
1056
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001057status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001059 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1060 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001061
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 // check calling permissions
1063 if (!settingsAllowed()) {
1064 return PERMISSION_DENIED;
1065 }
1066
Glenn Kasten142f5192014-03-25 17:44:59 -07001067 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1068 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001069 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001070 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001071 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001072 AutoMutex lock(mHardwareLock);
1073 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1074 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1075 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1076 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1077 final_result = result ?: final_result;
1078 }
1079 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001080 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001081 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1082 AudioParameter param = AudioParameter(keyValuePairs);
1083 String8 value;
1084 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001085 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1086 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001087 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1088 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001089 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001090 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1091 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001092 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001093 // suspend effects associated with those session IDs
1094 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001095 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001096 thread->setEffectSuspended(FX_IID_AEC,
1097 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001098 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001099 thread->setEffectSuspended(FX_IID_NS,
1100 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001101 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001102 }
1103 }
Eric Laurentbee53372011-08-29 12:42:48 -07001104 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001105 }
1106 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001107 String8 screenState;
1108 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1109 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001110 if (isOff != (AudioFlinger::mScreenState & 1)) {
1111 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001112 }
1113 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001114 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115 }
1116
1117 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1118 // and the thread is exited once the lock is released
1119 sp<ThreadBase> thread;
1120 {
1121 Mutex::Autolock _l(mLock);
1122 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001123 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001124 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001125 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001126 // indicate output device change to all input threads for pre processing
1127 AudioParameter param = AudioParameter(keyValuePairs);
1128 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001129 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1130 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001131 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001132 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133 }
1134 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001135 if (thread != 0) {
1136 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137 }
1138 return BAD_VALUE;
1139}
1140
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001141String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001143 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1144 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001145
Eric Laurenta4c5a552012-03-29 10:12:40 -07001146 Mutex::Autolock _l(mLock);
1147
Glenn Kasten142f5192014-03-25 17:44:59 -07001148 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001149 String8 out_s8;
1150
Dima Zavin799a70e2011-04-18 16:57:27 -07001151 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001152 char *s;
1153 {
1154 AutoMutex lock(mHardwareLock);
1155 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001156 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001157 s = dev->get_parameters(dev, keys.string());
1158 mHardwareStatus = AUDIO_HW_IDLE;
1159 }
John Grossmanef7740b2012-02-09 11:28:36 -08001160 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001161 free(s);
1162 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001163 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001164 }
1165
Mathias Agopian65ab4712010-07-14 17:59:35 -07001166 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1167 if (playbackThread != NULL) {
1168 return playbackThread->getParameters(keys);
1169 }
1170 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1171 if (recordThread != NULL) {
1172 return recordThread->getParameters(keys);
1173 }
1174 return String8("");
1175}
1176
Glenn Kastendd8104c2012-07-02 12:42:44 -07001177size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1178 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001179{
Eric Laurenta1884f92011-08-23 08:25:03 -07001180 status_t ret = initCheck();
1181 if (ret != NO_ERROR) {
1182 return 0;
1183 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001184 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001185 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001186 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001187 return 0;
1188 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001189
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001190 AutoMutex lock(mHardwareLock);
1191 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001192 audio_config_t config, proposed;
1193 memset(&proposed, 0, sizeof(proposed));
1194 proposed.sample_rate = sampleRate;
1195 proposed.channel_mask = channelMask;
1196 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001197
John Grossmanee578c02012-07-23 17:05:46 -07001198 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001199 size_t frames;
1200 for (;;) {
1201 // Note: config is currently a const parameter for get_input_buffer_size()
1202 // but we use a copy from proposed in case config changes from the call.
1203 config = proposed;
1204 frames = dev->get_input_buffer_size(dev, &config);
1205 if (frames != 0) {
1206 break; // hal success, config is the result
1207 }
1208 // change one parameter of the configuration each iteration to a more "common" value
1209 // to see if the device will support it.
1210 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1211 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1212 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1213 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1214 } else {
1215 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1216 "format %#x, channelMask 0x%X",
1217 sampleRate, format, channelMask);
1218 break; // retries failed, break out of loop with frames == 0.
1219 }
1220 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001221 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001222 if (frames > 0 && config.sample_rate != sampleRate) {
1223 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1224 }
1225 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226}
1227
Glenn Kasten5f972c02014-01-13 09:59:31 -08001228uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001229{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230 Mutex::Autolock _l(mLock);
1231
1232 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1233 if (recordThread != NULL) {
1234 return recordThread->getInputFramesLost();
1235 }
1236 return 0;
1237}
1238
1239status_t AudioFlinger::setVoiceVolume(float value)
1240{
Eric Laurenta1884f92011-08-23 08:25:03 -07001241 status_t ret = initCheck();
1242 if (ret != NO_ERROR) {
1243 return ret;
1244 }
1245
Mathias Agopian65ab4712010-07-14 17:59:35 -07001246 // check calling permissions
1247 if (!settingsAllowed()) {
1248 return PERMISSION_DENIED;
1249 }
1250
1251 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001252 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001253 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001254 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255 mHardwareStatus = AUDIO_HW_IDLE;
1256
1257 return ret;
1258}
1259
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001260status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001261 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263 Mutex::Autolock _l(mLock);
1264
1265 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1266 if (playbackThread != NULL) {
1267 return playbackThread->getRenderPosition(halFrames, dspFrames);
1268 }
1269
1270 return BAD_VALUE;
1271}
1272
1273void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1274{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001276 if (client == 0) {
1277 return;
1278 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001279 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001280 {
1281 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001282 if (mNotificationClients.indexOfKey(pid) < 0) {
1283 sp<NotificationClient> notificationClient = new NotificationClient(this,
1284 client,
1285 pid);
1286 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001287
Eric Laurent021cf962014-05-13 10:18:14 -07001288 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289
Marco Nelissen06b46062014-11-14 07:58:25 -08001290 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001291 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001292 }
1293 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001294
Eric Laurent021cf962014-05-13 10:18:14 -07001295 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001296 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001297 // the config change is always sent from playback or record threads to avoid deadlock
1298 // with AudioSystem::gLock
1299 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1300 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1301 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001302
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001303 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1304 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305 }
1306}
1307
1308void AudioFlinger::removeNotificationClient(pid_t pid)
1309{
1310 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001311 {
1312 Mutex::Autolock _cl(mClientLock);
1313 mNotificationClients.removeItem(pid);
1314 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001315
Steve Block3856b092011-10-20 11:56:00 +01001316 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001317 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001318 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001319 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001320 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001321 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001322 if (ref->mPid == pid) {
1323 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001324 mAudioSessionRefs.removeAt(i);
1325 delete ref;
1326 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001327 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001328 } else {
1329 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001330 }
1331 }
1332 if (removed) {
1333 purgeStaleEffects_l();
1334 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001335}
1336
Eric Laurent73e26b62015-04-27 16:55:58 -07001337void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001338 const sp<AudioIoDescriptor>& ioDesc,
1339 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001340{
Eric Laurent021cf962014-05-13 10:18:14 -07001341 Mutex::Autolock _l(mClientLock);
1342 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001343 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001344 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1345 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1346 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001347 }
1348}
1349
Eric Laurent021cf962014-05-13 10:18:14 -07001350// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001351void AudioFlinger::removeClient_l(pid_t pid)
1352{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001353 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001354 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355 mClients.removeItem(pid);
1356}
1357
Eric Laurent717e1282012-06-29 16:36:52 -07001358// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001359sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1360 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001361{
1362 sp<PlaybackThread> thread;
1363
1364 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1365 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1366 ALOG_ASSERT(thread == 0);
1367 thread = mPlaybackThreads.valueAt(i);
1368 }
1369 }
1370
1371 return thread;
1372}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001373
Mathias Agopian65ab4712010-07-14 17:59:35 -07001374
Mathias Agopian65ab4712010-07-14 17:59:35 -07001375
1376// ----------------------------------------------------------------------------
1377
1378AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1379 : RefBase(),
1380 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001381 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001382{
Eric Laurentda73b6c2015-08-20 16:18:53 -07001383 size_t heapSize = kClientSharedHeapSizeBytes;
1384 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1385 // invalidated tracks
1386 if (!audioFlinger->isLowRamDevice()) {
1387 heapSize *= kClientSharedHeapSizeMultiplier;
1388 }
1389 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001390}
1391
Eric Laurent021cf962014-05-13 10:18:14 -07001392// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001393AudioFlinger::Client::~Client()
1394{
1395 mAudioFlinger->removeClient_l(mPid);
1396}
1397
Glenn Kasten435dbe62012-01-30 10:15:48 -08001398sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399{
1400 return mMemoryDealer;
1401}
1402
1403// ----------------------------------------------------------------------------
1404
1405AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1406 const sp<IAudioFlingerClient>& client,
1407 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001408 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409{
1410}
1411
1412AudioFlinger::NotificationClient::~NotificationClient()
1413{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414}
1415
Glenn Kasten0f11b512014-01-31 16:18:54 -08001416void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001417{
1418 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001419 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001420}
1421
Mathias Agopian65ab4712010-07-14 17:59:35 -07001422
1423// ----------------------------------------------------------------------------
1424
1425sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001426 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001427 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001428 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001429 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001430 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001431 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001432 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001433 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001434 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001435 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001436 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001437 sp<IMemory>& cblk,
1438 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439 status_t *status)
1440{
1441 sp<RecordThread::RecordTrack> recordTrack;
1442 sp<RecordHandle> recordHandle;
1443 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001445 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001446
Glenn Kastend776ac62014-05-07 09:16:09 -07001447 cblk.clear();
1448 buffers.clear();
1449
Marco Nelissendcb346b2015-09-09 10:47:29 -07001450 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1451 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001452 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001453 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1454 clientUid = callingUid;
1455 }
1456
Mathias Agopian65ab4712010-07-14 17:59:35 -07001457 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001458 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001459 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001460 lStatus = PERMISSION_DENIED;
1461 goto Exit;
1462 }
1463
Glenn Kasten53b5d092014-02-05 10:00:23 -08001464 // further sample rate checks are performed by createRecordTrack_l()
1465 if (sampleRate == 0) {
1466 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1467 lStatus = BAD_VALUE;
1468 goto Exit;
1469 }
1470
Andy Hung6770c6f2015-04-07 13:43:36 -07001471 // we don't yet support anything other than linear PCM
1472 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001473 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001474 lStatus = BAD_VALUE;
1475 goto Exit;
1476 }
1477
Glenn Kasten53b5d092014-02-05 10:00:23 -08001478 // further channel mask checks are performed by createRecordTrack_l()
1479 if (!audio_is_input_channel(channelMask)) {
1480 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1481 lStatus = BAD_VALUE;
1482 goto Exit;
1483 }
1484
Glenn Kasten05997e22014-03-13 15:08:33 -07001485 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001486 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001487 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001488 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001489 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490 lStatus = BAD_VALUE;
1491 goto Exit;
1492 }
1493
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001494 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001495 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001496
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001497 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001498 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1499 lStatus = BAD_VALUE;
1500 goto Exit;
1501 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001502 lSessionId = *sessionId;
1503 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001504 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001505 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001506 if (sessionId != NULL) {
1507 *sessionId = lSessionId;
1508 }
1509 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001510 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001511
Glenn Kasten1879fff2012-07-11 15:36:59 -07001512 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001513 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001514 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001515 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001516
1517 if (lStatus == NO_ERROR) {
1518 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1519 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001520 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001521 if (chain != 0) {
1522 Mutex::Autolock _l(thread->mLock);
1523 thread->addEffectChain_l(chain);
1524 }
1525 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001526 }
Glenn Kastene198c362013-08-13 09:13:36 -07001527
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001528 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001529 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001530 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001531 // Don't hold mClientLock when releasing the reference on the track as the
1532 // destructor will acquire it.
1533 {
1534 Mutex::Autolock _cl(mClientLock);
1535 client.clear();
1536 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001537 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001538 goto Exit;
1539 }
1540
Glenn Kastend776ac62014-05-07 09:16:09 -07001541 cblk = recordTrack->getCblk();
1542 buffers = recordTrack->getBuffers();
1543
Glenn Kasten2fc14732013-08-05 14:58:14 -07001544 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001545 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001546
1547Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001548 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001549 return recordHandle;
1550}
1551
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001552
1553
Mathias Agopian65ab4712010-07-14 17:59:35 -07001554// ----------------------------------------------------------------------------
1555
Eric Laurenta4c5a552012-03-29 10:12:40 -07001556audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1557{
Eric Laurent44622db2014-08-01 19:00:33 -07001558 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001559 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001560 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001561 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001562 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001563 }
1564 Mutex::Autolock _l(mLock);
1565 return loadHwModule_l(name);
1566}
1567
1568// loadHwModule_l() must be called with AudioFlinger::mLock held
1569audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1570{
1571 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1572 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1573 ALOGW("loadHwModule() module %s already loaded", name);
1574 return mAudioHwDevs.keyAt(i);
1575 }
1576 }
1577
Eric Laurenta4c5a552012-03-29 10:12:40 -07001578 audio_hw_device_t *dev;
1579
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001580 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001581 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001582 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001583 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001584 }
1585
1586 mHardwareStatus = AUDIO_HW_INIT;
1587 rc = dev->init_check(dev);
1588 mHardwareStatus = AUDIO_HW_IDLE;
1589 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001590 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001591 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001592 }
1593
John Grossmanee578c02012-07-23 17:05:46 -07001594 // Check and cache this HAL's level of support for master mute and master
1595 // volume. If this is the first HAL opened, and it supports the get
1596 // methods, use the initial values provided by the HAL as the current
1597 // master mute and volume settings.
1598
1599 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1600 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001601 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001602
1603 if (0 == mAudioHwDevs.size()) {
1604 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1605 if (NULL != dev->get_master_volume) {
1606 float mv;
1607 if (OK == dev->get_master_volume(dev, &mv)) {
1608 mMasterVolume = mv;
1609 }
1610 }
1611
1612 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1613 if (NULL != dev->get_master_mute) {
1614 bool mm;
1615 if (OK == dev->get_master_mute(dev, &mm)) {
1616 mMasterMute = mm;
1617 }
1618 }
1619 }
1620
Eric Laurenta4c5a552012-03-29 10:12:40 -07001621 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001622 if ((NULL != dev->set_master_volume) &&
1623 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1624 flags = static_cast<AudioHwDevice::Flags>(flags |
1625 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1626 }
1627
1628 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1629 if ((NULL != dev->set_master_mute) &&
1630 (OK == dev->set_master_mute(dev, mMasterMute))) {
1631 flags = static_cast<AudioHwDevice::Flags>(flags |
1632 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1633 }
1634
Eric Laurenta4c5a552012-03-29 10:12:40 -07001635 mHardwareStatus = AUDIO_HW_IDLE;
1636 }
1637
Glenn Kastena13cde92016-03-28 15:26:02 -07001638 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001639 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001640
1641 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001642 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001643
1644 return handle;
1645
1646}
1647
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001648// ----------------------------------------------------------------------------
1649
Glenn Kasten3b16c762012-11-14 08:44:39 -08001650uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001651{
1652 Mutex::Autolock _l(mLock);
1653 PlaybackThread *thread = primaryPlaybackThread_l();
1654 return thread != NULL ? thread->sampleRate() : 0;
1655}
1656
Glenn Kastene33054e2012-11-14 12:54:39 -08001657size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001658{
1659 Mutex::Autolock _l(mLock);
1660 PlaybackThread *thread = primaryPlaybackThread_l();
1661 return thread != NULL ? thread->frameCountHAL() : 0;
1662}
1663
1664// ----------------------------------------------------------------------------
1665
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001666status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1667{
1668 uid_t uid = IPCThreadState::self()->getCallingUid();
1669 if (uid != AID_SYSTEM) {
1670 return PERMISSION_DENIED;
1671 }
1672 Mutex::Autolock _l(mLock);
1673 if (mIsDeviceTypeKnown) {
1674 return INVALID_OPERATION;
1675 }
1676 mIsLowRamDevice = isLowRamDevice;
1677 mIsDeviceTypeKnown = true;
1678 return NO_ERROR;
1679}
1680
Eric Laurent93c3d412014-08-01 14:48:35 -07001681audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1682{
1683 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001684
1685 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1686 if (index >= 0) {
1687 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1688 mHwAvSyncIds.valueAt(index), sessionId);
1689 return mHwAvSyncIds.valueAt(index);
1690 }
1691
1692 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1693 if (dev == NULL) {
1694 return AUDIO_HW_SYNC_INVALID;
1695 }
1696 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1697 AudioParameter param = AudioParameter(String8(reply));
1698 free(reply);
1699
1700 int value;
1701 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1702 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1703 return AUDIO_HW_SYNC_INVALID;
1704 }
1705
1706 // allow only one session for a given HW A/V sync ID.
1707 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1708 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1709 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1710 value, mHwAvSyncIds.keyAt(i));
1711 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001712 break;
1713 }
1714 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001715
1716 mHwAvSyncIds.add(sessionId, value);
1717
1718 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1719 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1720 uint32_t sessions = thread->hasAudioSession(sessionId);
1721 if (sessions & PlaybackThread::TRACK_SESSION) {
1722 AudioParameter param = AudioParameter();
1723 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1724 thread->setParameters(param.toString());
1725 break;
1726 }
1727 }
1728
1729 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1730 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001731}
1732
Eric Laurent72e3f392015-05-20 14:43:50 -07001733status_t AudioFlinger::systemReady()
1734{
1735 Mutex::Autolock _l(mLock);
1736 ALOGI("%s", __FUNCTION__);
1737 if (mSystemReady) {
1738 ALOGW("%s called twice", __FUNCTION__);
1739 return NO_ERROR;
1740 }
1741 mSystemReady = true;
1742 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1743 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1744 thread->systemReady();
1745 }
1746 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1747 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1748 thread->systemReady();
1749 }
1750 return NO_ERROR;
1751}
1752
Eric Laurentfa90e842014-10-17 18:12:31 -07001753// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1754void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1755{
1756 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1757 if (index >= 0) {
1758 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1759 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1760 AudioParameter param = AudioParameter();
1761 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1762 thread->setParameters(param.toString());
1763 }
1764}
1765
1766
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001767// ----------------------------------------------------------------------------
1768
Eric Laurent83b88082014-06-20 18:31:16 -07001769
1770sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001771 audio_io_handle_t *output,
1772 audio_config_t *config,
1773 audio_devices_t devices,
1774 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001775 audio_output_flags_t flags)
1776{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001777 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001778 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001779 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001780 }
1781
Eric Laurentcf2c0212014-07-25 16:20:43 -07001782 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001783 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1784 } else {
1785 // Audio Policy does not currently request a specific output handle.
1786 // If this is ever needed, see openInput_l() for example code.
1787 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1788 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001789 }
Eric Laurent83b88082014-06-20 18:31:16 -07001790
1791 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1792
Eric Laurent83b88082014-06-20 18:31:16 -07001793 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001794 // This if statement allows overriding the audio policy settings
1795 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1796 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1797 // Check only for Normal Mixing mode
1798 if (kEnableExtendedPrecision) {
1799 // Specify format (uncomment one below to choose)
1800 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1801 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1802 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1803 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001804 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001805 }
1806 if (kEnableExtendedChannels) {
1807 // Specify channel mask (uncomment one below to choose)
1808 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1809 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1810 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1811 }
Eric Laurent83b88082014-06-20 18:31:16 -07001812 }
1813
Phil Burk062e67a2015-02-11 13:40:50 -08001814 AudioStreamOut *outputStream = NULL;
1815 status_t status = outHwDev->openOutputStream(
1816 &outputStream,
1817 *output,
1818 devices,
1819 flags,
1820 config,
1821 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001822
1823 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001824
Phil Burk062e67a2015-02-11 13:40:50 -08001825 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001826
1827 PlaybackThread *thread;
1828 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurent51716182016-02-29 18:00:56 -08001829 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady,
1830 config->offload_info.bit_rate);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001831 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001832 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1833 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001834 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001835 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001836 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001837 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001838 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001839 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001840 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001841 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001842 return thread;
1843 }
1844
1845 return 0;
1846}
1847
Eric Laurentcf2c0212014-07-25 16:20:43 -07001848status_t AudioFlinger::openOutput(audio_module_handle_t module,
1849 audio_io_handle_t *output,
1850 audio_config_t *config,
1851 audio_devices_t *devices,
1852 const String8& address,
1853 uint32_t *latencyMs,
1854 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001855{
Phil Burk062e67a2015-02-11 13:40:50 -08001856 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001857 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001858 (devices != NULL) ? *devices : 0,
1859 config->sample_rate,
1860 config->format,
1861 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001862 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001863
Eric Laurentcf2c0212014-07-25 16:20:43 -07001864 if (*devices == AUDIO_DEVICE_NONE) {
1865 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001866 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001867
Mathias Agopian65ab4712010-07-14 17:59:35 -07001868 Mutex::Autolock _l(mLock);
1869
Eric Laurentcf2c0212014-07-25 16:20:43 -07001870 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001871 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001872 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001873
1874 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001875 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001876
1877 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001878 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001879 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001880 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001881
1882 AutoMutex lock(mHardwareLock);
1883 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001884 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001885 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001886 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001887 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001888 }
1889
Eric Laurentcf2c0212014-07-25 16:20:43 -07001890 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001891}
1892
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001893audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1894 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001895{
1896 Mutex::Autolock _l(mLock);
1897 MixerThread *thread1 = checkMixerThread_l(output1);
1898 MixerThread *thread2 = checkMixerThread_l(output2);
1899
1900 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001901 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1902 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001903 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001904 }
1905
Glenn Kasteneeecb982016-02-26 10:44:04 -08001906 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07001907 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001908 thread->addOutputTrack(thread2);
1909 mPlaybackThreads.add(id, thread);
1910 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001911 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001912 return id;
1913}
1914
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001915status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001916{
Glenn Kastend96c5722012-04-25 13:44:49 -07001917 return closeOutput_nonvirtual(output);
1918}
1919
1920status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1921{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001922 // keep strong reference on the playback thread so that
1923 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001924 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001925 {
1926 Mutex::Autolock _l(mLock);
1927 thread = checkPlaybackThread_l(output);
1928 if (thread == NULL) {
1929 return BAD_VALUE;
1930 }
1931
Steve Block3856b092011-10-20 11:56:00 +01001932 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001934 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001936 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001937 DuplicatingThread *dupThread =
1938 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001940 }
1941 }
1942 }
1943
1944
1945 mPlaybackThreads.removeItem(output);
1946 // save all effects to the default thread
1947 if (mPlaybackThreads.size()) {
1948 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1949 if (dstThread != NULL) {
1950 // audioflinger lock is held here so the acquisition order of thread locks does not
1951 // matter
1952 Mutex::Autolock _dl(dstThread->mLock);
1953 Mutex::Autolock _sl(thread->mLock);
1954 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1955 for (size_t i = 0; i < effectChains.size(); i ++) {
1956 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957 }
1958 }
1959 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001960 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1961 ioDesc->mIoHandle = output;
1962 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001963 }
1964 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001965 // The thread entity (active unit of execution) is no longer running here,
1966 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001967
Eric Laurentf6870ae2015-05-08 10:50:03 -07001968 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001969 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001970 }
Eric Laurent83b88082014-06-20 18:31:16 -07001971
Mathias Agopian65ab4712010-07-14 17:59:35 -07001972 return NO_ERROR;
1973}
1974
Eric Laurent83b88082014-06-20 18:31:16 -07001975void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1976{
1977 AudioStreamOut *out = thread->clearOutput();
1978 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1979 // from now on thread->mOutput is NULL
1980 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1981 delete out;
1982}
1983
1984void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1985{
1986 mPlaybackThreads.removeItem(thread->mId);
1987 thread->exit();
1988 closeOutputFinish(thread);
1989}
1990
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001991status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001992{
1993 Mutex::Autolock _l(mLock);
1994 PlaybackThread *thread = checkPlaybackThread_l(output);
1995
1996 if (thread == NULL) {
1997 return BAD_VALUE;
1998 }
1999
Steve Block3856b092011-10-20 11:56:00 +01002000 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002001 thread->suspend();
2002
2003 return NO_ERROR;
2004}
2005
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002006status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002007{
2008 Mutex::Autolock _l(mLock);
2009 PlaybackThread *thread = checkPlaybackThread_l(output);
2010
2011 if (thread == NULL) {
2012 return BAD_VALUE;
2013 }
2014
Steve Block3856b092011-10-20 11:56:00 +01002015 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002016
2017 thread->restore();
2018
2019 return NO_ERROR;
2020}
2021
Eric Laurentcf2c0212014-07-25 16:20:43 -07002022status_t AudioFlinger::openInput(audio_module_handle_t module,
2023 audio_io_handle_t *input,
2024 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002025 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002026 const String8& address,
2027 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002028 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002029{
Eric Laurent83b88082014-06-20 18:31:16 -07002030 Mutex::Autolock _l(mLock);
2031
Glenn Kastene7d66712015-03-05 13:46:52 -08002032 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002033 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002034 }
2035
Glenn Kastene7d66712015-03-05 13:46:52 -08002036 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002037
2038 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002039 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002040 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002041 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002042 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002043 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002044}
Dima Zavin799a70e2011-04-18 16:57:27 -07002045
Eric Laurent83b88082014-06-20 18:31:16 -07002046sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002047 audio_io_handle_t *input,
2048 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002049 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002050 const String8& address,
2051 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002052 audio_input_flags_t flags)
2053{
Glenn Kastene7d66712015-03-05 13:46:52 -08002054 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002055 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002056 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002057 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002058 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002059
Glenn Kasteneeecb982016-02-26 10:44:04 -08002060 // Audio Policy can request a specific handle for hardware hotword.
2061 // The goal here is not to re-open an already opened input.
2062 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002063 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002064 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2065 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2066 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2067 return 0;
2068 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2069 // This should not happen in a transient state with current design.
2070 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2071 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002072 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002073
Eric Laurentcf2c0212014-07-25 16:20:43 -07002074 audio_config_t halconfig = *config;
2075 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002076 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002077 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002078 &inStream, flags, address.string(), source);
2079 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002080 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002081 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002082 halconfig.sample_rate,
2083 halconfig.format,
2084 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002085 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002086 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002087
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002088 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002089 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002090 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002091 audio_is_linear_pcm(config->format) &&
2092 audio_is_linear_pcm(halconfig.format) &&
2093 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002094 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2095 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002096 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002097 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002098 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002099 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002100 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002101 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002102 }
2103
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002104 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002105
Glenn Kasten46909e72013-02-26 09:20:22 -08002106#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002107 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2108 // or (re-)create if current Pipe is idle and does not match the new format
2109 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002110 enum {
2111 TEE_SINK_NO, // don't copy input
2112 TEE_SINK_NEW, // copy input using a new pipe
2113 TEE_SINK_OLD, // copy input using an existing pipe
2114 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002115 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2116 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002117 if (!mTeeSinkInputEnabled) {
2118 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002119 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002120 kind = TEE_SINK_NO;
2121 } else if (mRecordTeeSink == 0) {
2122 kind = TEE_SINK_NEW;
2123 } else if (mRecordTeeSink->getStrongCount() != 1) {
2124 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002125 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002126 kind = TEE_SINK_OLD;
2127 } else {
2128 kind = TEE_SINK_NEW;
2129 }
2130 switch (kind) {
2131 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002132 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002133 size_t numCounterOffers = 0;
2134 const NBAIO_Format offers[1] = {format};
2135 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2136 ALOG_ASSERT(index == 0);
2137 PipeReader *pipeReader = new PipeReader(*pipe);
2138 numCounterOffers = 0;
2139 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2140 ALOG_ASSERT(index == 0);
2141 mRecordTeeSink = pipe;
2142 mRecordTeeSource = pipeReader;
2143 teeSink = pipe;
2144 }
2145 break;
2146 case TEE_SINK_OLD:
2147 teeSink = mRecordTeeSink;
2148 break;
2149 case TEE_SINK_NO:
2150 default:
2151 break;
2152 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002153#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002154
Eric Laurentcf2c0212014-07-25 16:20:43 -07002155 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002156
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002157 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002158 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002159 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002160 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002161 inputStream,
2162 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002163 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002164 devices,
2165 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002166#ifdef TEE_SINK
2167 , teeSink
2168#endif
2169 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002170 mRecordThreads.add(*input, thread);
2171 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002172 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002173 }
2174
Eric Laurentcf2c0212014-07-25 16:20:43 -07002175 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002176 return 0;
2177}
2178
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002179status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002180{
Glenn Kastend96c5722012-04-25 13:44:49 -07002181 return closeInput_nonvirtual(input);
2182}
2183
2184status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2185{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002186 // keep strong reference on the record thread so that
2187 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002188 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002189 {
2190 Mutex::Autolock _l(mLock);
2191 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002192 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002193 return BAD_VALUE;
2194 }
2195
Steve Block3856b092011-10-20 11:56:00 +01002196 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002197
2198 // If we still have effect chains, it means that a client still holds a handle
2199 // on at least one effect. We must either move the chain to an existing thread with the
2200 // same session ID or put it aside in case a new record thread is opened for a
2201 // new capture on the same session
2202 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002203 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002204 Mutex::Autolock _sl(thread->mLock);
2205 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002206 // Note: maximum one chain per record thread
2207 if (effectChains.size() != 0) {
2208 chain = effectChains[0];
2209 }
2210 }
2211 if (chain != 0) {
2212 // first check if a record thread is already opened with a client on the same session.
2213 // This should only happen in case of overlap between one thread tear down and the
2214 // creation of its replacement
2215 size_t i;
2216 for (i = 0; i < mRecordThreads.size(); i++) {
2217 sp<RecordThread> t = mRecordThreads.valueAt(i);
2218 if (t == thread) {
2219 continue;
2220 }
2221 if (t->hasAudioSession(chain->sessionId()) != 0) {
2222 Mutex::Autolock _l(t->mLock);
2223 ALOGV("closeInput() found thread %d for effect session %d",
2224 t->id(), chain->sessionId());
2225 t->addEffectChain_l(chain);
2226 break;
2227 }
2228 }
2229 // put the chain aside if we could not find a record thread with the same session id.
2230 if (i == mRecordThreads.size()) {
2231 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002232 }
2233 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002234 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2235 ioDesc->mIoHandle = input;
2236 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237 mRecordThreads.removeItem(input);
2238 }
Eric Laurent83b88082014-06-20 18:31:16 -07002239 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2240 // we have a different lock for notification client
2241 closeInputFinish(thread);
2242 return NO_ERROR;
2243}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002244
Eric Laurent83b88082014-06-20 18:31:16 -07002245void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2246{
2247 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002248 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002249 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002250 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002251 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002252 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002253}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254
Eric Laurent83b88082014-06-20 18:31:16 -07002255void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2256{
2257 mRecordThreads.removeItem(thread->mId);
2258 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002259}
2260
Glenn Kastend2304db2014-02-03 07:40:31 -08002261status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002262{
2263 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002264 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002265
2266 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2267 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002268 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002269 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002270
2271 return NO_ERROR;
2272}
2273
2274
Glenn Kasteneeecb982016-02-26 10:44:04 -08002275audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002276{
Glenn Kasten9d003132016-04-06 14:38:09 -07002277 // This is a binder API, so a malicious client could pass in a bad parameter.
2278 // Check for that before calling the internal API nextUniqueId().
2279 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2280 ALOGE("newAudioUniqueId invalid use %d", use);
2281 return AUDIO_UNIQUE_ID_ALLOCATE;
2282 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002283 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002284}
2285
Glenn Kastend848eb42016-03-08 13:42:11 -08002286void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002287{
2288 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002289 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002290 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2291 if (pid != -1 && (caller == getpid_cached)) {
2292 caller = pid;
2293 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002294
Eric Laurent021cf962014-05-13 10:18:14 -07002295 {
2296 Mutex::Autolock _cl(mClientLock);
2297 // Ignore requests received from processes not known as notification client. The request
2298 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2299 // called from a different pid leaving a stale session reference. Also we don't know how
2300 // to clear this reference if the client process dies.
2301 if (mNotificationClients.indexOfKey(caller) < 0) {
2302 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2303 return;
2304 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002305 }
2306
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002307 size_t num = mAudioSessionRefs.size();
2308 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002309 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002310 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2311 ref->mCnt++;
2312 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002313 return;
2314 }
2315 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002316 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2317 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002318}
2319
Glenn Kastend848eb42016-03-08 13:42:11 -08002320void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002321{
2322 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002323 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002324 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2325 if (pid != -1 && (caller == getpid_cached)) {
2326 caller = pid;
2327 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002328 size_t num = mAudioSessionRefs.size();
2329 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002330 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002331 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2332 ref->mCnt--;
2333 ALOGV(" decremented refcount to %d", ref->mCnt);
2334 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002335 mAudioSessionRefs.removeAt(i);
2336 delete ref;
2337 purgeStaleEffects_l();
2338 }
2339 return;
2340 }
2341 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002342 // If the caller is mediaserver it is likely that the session being released was acquired
2343 // on behalf of a process not in notification clients and we ignore the warning.
2344 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002345}
2346
2347void AudioFlinger::purgeStaleEffects_l() {
2348
Steve Block3856b092011-10-20 11:56:00 +01002349 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002350
2351 Vector< sp<EffectChain> > chains;
2352
2353 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2354 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2355 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2356 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002357 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2358 chains.push(ec);
2359 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002360 }
2361 }
2362 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2363 sp<RecordThread> t = mRecordThreads.valueAt(i);
2364 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2365 sp<EffectChain> ec = t->mEffectChains[j];
2366 chains.push(ec);
2367 }
2368 }
2369
2370 for (size_t i = 0; i < chains.size(); i++) {
2371 sp<EffectChain> ec = chains[i];
2372 int sessionid = ec->sessionId();
2373 sp<ThreadBase> t = ec->mThread.promote();
2374 if (t == 0) {
2375 continue;
2376 }
2377 size_t numsessionrefs = mAudioSessionRefs.size();
2378 bool found = false;
2379 for (size_t k = 0; k < numsessionrefs; k++) {
2380 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002381 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002382 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002383 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002384 found = true;
2385 break;
2386 }
2387 }
2388 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002389 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002390 // remove all effects from the chain
2391 while (ec->mEffects.size()) {
2392 sp<EffectModule> effect = ec->mEffects[0];
2393 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002394 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002395 if (effect->purgeHandles()) {
2396 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002397 }
2398 AudioSystem::unregisterEffect(effect->id());
2399 }
2400 }
2401 }
2402 return;
2403}
2404
Glenn Kasteneeecb982016-02-26 10:44:04 -08002405// checkThread_l() must be called with AudioFlinger::mLock held
2406AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2407{
2408 ThreadBase *thread = NULL;
2409 switch (audio_unique_id_get_use(ioHandle)) {
2410 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2411 thread = checkPlaybackThread_l(ioHandle);
2412 break;
2413 case AUDIO_UNIQUE_ID_USE_INPUT:
2414 thread = checkRecordThread_l(ioHandle);
2415 break;
2416 default:
2417 break;
2418 }
2419 return thread;
2420}
2421
Mathias Agopian65ab4712010-07-14 17:59:35 -07002422// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002423AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002424{
Glenn Kastena1117922012-01-26 10:53:32 -08002425 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002426}
2427
2428// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002429AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002430{
2431 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002432 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002433}
2434
2435// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002436AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002437{
Glenn Kastena1117922012-01-26 10:53:32 -08002438 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002439}
2440
Glenn Kasteneeecb982016-02-26 10:44:04 -08002441audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002442{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002443 int32_t base = android_atomic_add(AUDIO_UNIQUE_ID_USE_MAX, &mNextUniqueId);
2444 // We have no way of recovering from wraparound
2445 LOG_ALWAYS_FATAL_IF(base == 0, "unique ID overflow");
Glenn Kasten9d003132016-04-06 14:38:09 -07002446 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002447 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
2448 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2449 return (audio_unique_id_t) (base | use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002450}
2451
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002452AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002453{
2454 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2455 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002456 if(thread->isDuplicating()) {
2457 continue;
2458 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002459 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002460 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002461 return thread;
2462 }
2463 }
2464 return NULL;
2465}
2466
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002467audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002468{
2469 PlaybackThread *thread = primaryPlaybackThread_l();
2470
2471 if (thread == NULL) {
2472 return 0;
2473 }
2474
Eric Laurentf1c04f92012-08-28 14:26:53 -07002475 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002476}
2477
Eric Laurenta011e352012-03-29 15:51:43 -07002478sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002479 audio_session_t triggerSession,
2480 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002481 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002482 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002483{
2484 Mutex::Autolock _l(mLock);
2485
2486 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2487 status_t playStatus = NAME_NOT_FOUND;
2488 status_t recStatus = NAME_NOT_FOUND;
2489 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2490 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2491 if (playStatus == NO_ERROR) {
2492 return event;
2493 }
2494 }
2495 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2496 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2497 if (recStatus == NO_ERROR) {
2498 return event;
2499 }
2500 }
2501 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2502 mPendingSyncEvents.add(event);
2503 } else {
2504 ALOGV("createSyncEvent() invalid event %d", event->type());
2505 event.clear();
2506 }
2507 return event;
2508}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002509
Mathias Agopian65ab4712010-07-14 17:59:35 -07002510// ----------------------------------------------------------------------------
2511// Effect management
2512// ----------------------------------------------------------------------------
2513
2514
Glenn Kastenf587ba52012-01-26 16:25:10 -08002515status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002516{
2517 Mutex::Autolock _l(mLock);
2518 return EffectQueryNumberEffects(numEffects);
2519}
2520
Glenn Kastenf587ba52012-01-26 16:25:10 -08002521status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002522{
2523 Mutex::Autolock _l(mLock);
2524 return EffectQueryEffect(index, descriptor);
2525}
2526
Glenn Kasten5e92a782012-01-30 07:40:52 -08002527status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002528 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002529{
2530 Mutex::Autolock _l(mLock);
2531 return EffectGetDescriptor(pUuid, descriptor);
2532}
2533
2534
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002535sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002536 effect_descriptor_t *pDesc,
2537 const sp<IEffectClient>& effectClient,
2538 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002539 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002540 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002541 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002542 status_t *status,
2543 int *id,
2544 int *enabled)
2545{
2546 status_t lStatus = NO_ERROR;
2547 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002548 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002549
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002550 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002551 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002552 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002553
2554 if (pDesc == NULL) {
2555 lStatus = BAD_VALUE;
2556 goto Exit;
2557 }
2558
Eric Laurent84e9a102010-09-23 16:10:16 -07002559 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002560 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002561 lStatus = PERMISSION_DENIED;
2562 goto Exit;
2563 }
2564
Dima Zavinfce7a472011-04-19 22:30:36 -07002565 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002566 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002567 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002568 lStatus = PERMISSION_DENIED;
2569 goto Exit;
2570 }
2571
Mathias Agopian65ab4712010-07-14 17:59:35 -07002572 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002573 if (!EffectIsNullUuid(&pDesc->uuid)) {
2574 // if uuid is specified, request effect descriptor
2575 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2576 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002577 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002578 goto Exit;
2579 }
2580 } else {
2581 // if uuid is not specified, look for an available implementation
2582 // of the required type in effect factory
2583 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002584 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002585 lStatus = BAD_VALUE;
2586 goto Exit;
2587 }
2588 uint32_t numEffects = 0;
2589 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002590 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002591 bool found = false;
2592
2593 lStatus = EffectQueryNumberEffects(&numEffects);
2594 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002595 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002596 goto Exit;
2597 }
2598 for (uint32_t i = 0; i < numEffects; i++) {
2599 lStatus = EffectQueryEffect(i, &desc);
2600 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002601 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002602 continue;
2603 }
2604 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2605 // If matching type found save effect descriptor. If the session is
2606 // 0 and the effect is not auxiliary, continue enumeration in case
2607 // an auxiliary version of this effect type is available
2608 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002609 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002610 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002611 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2612 break;
2613 }
2614 }
2615 }
2616 if (!found) {
2617 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002618 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002619 goto Exit;
2620 }
2621 // For same effect type, chose auxiliary version over insert version if
2622 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002623 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002624 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002625 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002626 }
2627 }
2628
2629 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002630 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002631 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2632 lStatus = INVALID_OPERATION;
2633 goto Exit;
2634 }
2635
Eric Laurent59255e42011-07-27 19:49:51 -07002636 // check recording permission for visualizer
2637 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002638 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002639 lStatus = PERMISSION_DENIED;
2640 goto Exit;
2641 }
2642
Mathias Agopian65ab4712010-07-14 17:59:35 -07002643 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002644 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002645 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002646 // if the output returned by getOutputForEffect() is removed before we lock the
2647 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2648 // and we will exit safely
2649 io = AudioSystem::getOutputForEffect(&desc);
2650 ALOGV("createEffect got output %d", io);
2651 }
2652
2653 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002654
2655 // If output is not specified try to find a matching audio session ID in one of the
2656 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002657 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2658 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002659 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002660 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002661 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2662 // output must be specified by AudioPolicyManager when using session
2663 // AUDIO_SESSION_OUTPUT_STAGE
2664 lStatus = BAD_VALUE;
2665 goto Exit;
2666 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002667 // look for the thread where the specified audio session is present
2668 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2669 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2670 io = mPlaybackThreads.keyAt(i);
2671 break;
2672 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002673 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002674 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002675 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2676 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2677 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002678 break;
2679 }
2680 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002681 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002682 // If no output thread contains the requested session ID, default to
2683 // first output. The effect chain will be moved to the correct output
2684 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002685 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002686 io = mPlaybackThreads.keyAt(0);
2687 }
Steve Block3856b092011-10-20 11:56:00 +01002688 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002689 }
2690 ThreadBase *thread = checkRecordThread_l(io);
2691 if (thread == NULL) {
2692 thread = checkPlaybackThread_l(io);
2693 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002694 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002695 lStatus = BAD_VALUE;
2696 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002697 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002698 } else {
2699 // Check if one effect chain was awaiting for an effect to be created on this
2700 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08002701 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07002702 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002703 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002704 thread->addEffectChain_l(chain);
2705 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002706 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002707
Eric Laurent021cf962014-05-13 10:18:14 -07002708 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002709
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002710 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002711 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2712 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002713 if (handle != 0 && id != NULL) {
2714 *id = handle->id();
2715 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002716 if (handle == 0) {
2717 // remove local strong reference to Client with mClientLock held
2718 Mutex::Autolock _cl(mClientLock);
2719 client.clear();
2720 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002721 }
2722
2723Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002724 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002725 return handle;
2726}
2727
Glenn Kastend848eb42016-03-08 13:42:11 -08002728status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002729 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002730{
Steve Block3856b092011-10-20 11:56:00 +01002731 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002732 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002733 Mutex::Autolock _l(mLock);
2734 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002735 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002736 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002737 }
Eric Laurentde070132010-07-13 04:45:46 -07002738 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2739 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002740 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002741 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002742 }
Eric Laurentde070132010-07-13 04:45:46 -07002743 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2744 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002745 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002746 return BAD_VALUE;
2747 }
2748
2749 Mutex::Autolock _dl(dstThread->mLock);
2750 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002751 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002752}
2753
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002754// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08002755status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002756 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002757 AudioFlinger::PlaybackThread *dstThread,
2758 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002759{
Steve Block3856b092011-10-20 11:56:00 +01002760 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002761 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002762
Eric Laurent59255e42011-07-27 19:49:51 -07002763 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002764 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002765 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002766 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002767 return INVALID_OPERATION;
2768 }
2769
Andy Hung9a592762014-07-21 21:56:01 -07002770 // Check whether the destination thread has a channel count of FCC_2, which is
2771 // currently required for (most) effects. Prevent moving the effect chain here rather
2772 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentf6870ae2015-05-08 10:50:03 -07002773 if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
Eric Laurentb10352f2014-11-03 16:25:39 -08002774 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002775 ALOGW("moveEffectChain_l() effect chain failed because"
2776 " destination thread %p channel count(%u) != %u",
2777 dstThread, dstThread->mChannelCount, FCC_2);
2778 return INVALID_OPERATION;
2779 }
2780
Eric Laurent39e94f82010-07-28 01:32:47 -07002781 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002782 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002783 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002784 // removed.
2785 srcThread->removeEffectChain_l(chain);
2786
2787 // transfer all effects one by one so that new effect chain is created on new thread with
2788 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002789 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002790 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002791 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002792 Vector< sp<EffectModule> > removed;
2793 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002794 while (effect != 0) {
2795 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002796 removed.add(effect);
2797 status = dstThread->addEffect_l(effect);
2798 if (status != NO_ERROR) {
2799 break;
2800 }
Eric Laurentec35a142011-10-05 17:42:25 -07002801 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2802 if (effect->state() == EffectModule::ACTIVE ||
2803 effect->state() == EffectModule::STOPPING) {
2804 effect->start();
2805 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002806 // if the move request is not received from audio policy manager, the effect must be
2807 // re-registered with the new strategy and output
2808 if (dstChain == 0) {
2809 dstChain = effect->chain().promote();
2810 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002811 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002812 status = NO_INIT;
2813 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002814 }
2815 strategy = dstChain->strategy();
2816 }
2817 if (reRegister) {
2818 AudioSystem::unregisterEffect(effect->id());
2819 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002820 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002821 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002822 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002823 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002824 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002825 }
Eric Laurentde070132010-07-13 04:45:46 -07002826 effect = chain->getEffectFromId_l(0);
2827 }
2828
Eric Laurent5baf2af2013-09-12 17:37:00 -07002829 if (status != NO_ERROR) {
2830 for (size_t i = 0; i < removed.size(); i++) {
2831 srcThread->addEffect_l(removed[i]);
2832 if (dstChain != 0 && reRegister) {
2833 AudioSystem::unregisterEffect(removed[i]->id());
2834 AudioSystem::registerEffect(&removed[i]->desc(),
2835 srcThread->id(),
2836 strategy,
2837 sessionId,
2838 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002839 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002840 }
2841 }
2842 }
2843
2844 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002845}
2846
Eric Laurent5baf2af2013-09-12 17:37:00 -07002847bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002848{
2849 if (mGlobalEffectEnableTime != 0 &&
2850 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2851 return true;
2852 }
2853
2854 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2855 sp<EffectChain> ec =
2856 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002857 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002858 return true;
2859 }
2860 }
2861 return false;
2862}
2863
Eric Laurent5baf2af2013-09-12 17:37:00 -07002864void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002865{
2866 Mutex::Autolock _l(mLock);
2867
2868 mGlobalEffectEnableTime = systemTime();
2869
2870 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2871 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2872 if (t->mType == ThreadBase::OFFLOAD) {
2873 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2874 }
2875 }
2876
2877}
2878
Eric Laurentaaa44472014-09-12 17:41:50 -07002879status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2880{
Glenn Kastend848eb42016-03-08 13:42:11 -08002881 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002882 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002883 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002884 if (index >= 0) {
2885 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2886 return ALREADY_EXISTS;
2887 }
2888 mOrphanEffectChains.add(session, chain);
2889 return NO_ERROR;
2890}
2891
2892sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2893{
2894 sp<EffectChain> chain;
2895 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002896 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002897 if (index >= 0) {
2898 chain = mOrphanEffectChains.valueAt(index);
2899 mOrphanEffectChains.removeItemsAt(index);
2900 }
2901 return chain;
2902}
2903
2904bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2905{
2906 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08002907 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002908 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002909 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002910 if (index >= 0) {
2911 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2912 if (chain->removeEffect_l(effect) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002913 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002914 mOrphanEffectChains.removeItemsAt(index);
2915 }
2916 return true;
2917 }
2918 return false;
2919}
2920
2921
Glenn Kastenda6ef132013-01-10 12:31:01 -08002922struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002923#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2924 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002925};
2926
2927int comparEntry(const void *p1, const void *p2)
2928{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002929 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002930}
2931
Glenn Kasten46909e72013-02-26 09:20:22 -08002932#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002933void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002934{
Eric Laurent81784c32012-11-19 14:55:58 -08002935 NBAIO_Source *teeSource = source.get();
2936 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002937 // .wav rotation
2938 // There is a benign race condition if 2 threads call this simultaneously.
2939 // They would both traverse the directory, but the result would simply be
2940 // failures at unlink() which are ignored. It's also unlikely since
2941 // normally dumpsys is only done by bugreport or from the command line.
2942 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08002943 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08002944 size_t teePathLen = strlen(teePath);
2945 DIR *dir = opendir(teePath);
2946 teePath[teePathLen++] = '/';
2947 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002948#define TEE_MAX_SORT 20 // number of entries to sort
2949#define TEE_MAX_KEEP 10 // number of entries to keep
2950 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002951 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002952 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002953 struct dirent de;
2954 struct dirent *result = NULL;
2955 int rc = readdir_r(dir, &de, &result);
2956 if (rc != 0) {
2957 ALOGW("readdir_r failed %d", rc);
2958 break;
2959 }
2960 if (result == NULL) {
2961 break;
2962 }
2963 if (result != &de) {
2964 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2965 break;
2966 }
2967 // ignore non .wav file entries
2968 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002969 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08002970 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2971 continue;
2972 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08002973 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002974 }
2975 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002976 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002977 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002978 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
2979 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002980 (void) unlink(teePath);
2981 }
2982 }
2983 } else {
2984 if (fd >= 0) {
Glenn Kasten9a003992016-02-23 15:24:34 -08002985 dprintf(fd, "unable to rotate tees in %.*s: %s\n", teePathLen, teePath,
2986 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002987 }
2988 }
Eric Laurent81784c32012-11-19 14:55:58 -08002989 char teeTime[16];
2990 struct timeval tv;
2991 gettimeofday(&tv, NULL);
2992 struct tm tm;
2993 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002994 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2995 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2996 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2997 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002998 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002999 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003000 char wavHeader[44];
3001 memcpy(wavHeader,
3002 "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",
3003 sizeof(wavHeader));
3004 NBAIO_Format format = teeSource->format();
3005 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003006 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003007 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003008 wavHeader[22] = channelCount; // number of channels
3009 wavHeader[24] = sampleRate; // sample rate
3010 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003011 wavHeader[32] = frameSize; // block alignment
3012 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003013 write(teeFd, wavHeader, sizeof(wavHeader));
3014 size_t total = 0;
3015 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003016#define TEE_SINK_READ 1024 // frames per I/O operation
3017 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003018 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003019 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003020 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003021 bool wasFirstRead = firstRead;
3022 firstRead = false;
3023 if (actual <= 0) {
3024 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3025 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003026 }
Eric Laurent81784c32012-11-19 14:55:58 -08003027 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003028 }
Eric Laurent81784c32012-11-19 14:55:58 -08003029 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003030 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003031 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003032 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003033 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003034 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003035 uint32_t temp = 44 + total * frameSize - 8;
3036 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003037 write(teeFd, &temp, sizeof(temp));
3038 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003039 temp = total * frameSize;
3040 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003041 write(teeFd, &temp, sizeof(temp));
3042 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003043 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003044 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003045 }
Eric Laurent59255e42011-07-27 19:49:51 -07003046 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003047 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003048 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003049 }
Eric Laurent59255e42011-07-27 19:49:51 -07003050 }
3051 }
3052}
Glenn Kasten46909e72013-02-26 09:20:22 -08003053#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003054
Mathias Agopian65ab4712010-07-14 17:59:35 -07003055// ----------------------------------------------------------------------------
3056
3057status_t AudioFlinger::onTransact(
3058 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3059{
3060 return BnAudioFlinger::onTransact(code, data, reply, flags);
3061}
3062
Glenn Kasten63238ef2015-03-02 15:50:29 -08003063} // namespace android