blob: 3bd410dc9682e2cd0489b78c29c2b0c9dafe1eb3 [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 Kastend2e67e12016-04-11 08:26:37 -0700179 // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),
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 Kastend2e67e12016-04-11 08:26:37 -0700187 // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum
188 for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) {
189 // zero ID has a special meaning, so unavailable
190 mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX;
191 }
192
Glenn Kasten949a9262013-04-16 12:35:20 -0700193 getpid_cached = getpid();
Glenn Kastenae0cff12016-02-24 13:57:49 -0800194 const bool doLog = property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800195 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800196 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
197 MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800198 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700199
Wei Jia3f273d12015-11-24 09:06:49 -0800200 // reset battery stats.
201 // if the audio service has crashed, battery stats could be left
202 // in bad state, reset the state upon service start.
203 BatteryNotifier::getInstance().noteResetAudio();
204
Glenn Kasten46909e72013-02-26 09:20:22 -0800205#ifdef TEE_SINK
Glenn Kasten9a003992016-02-23 15:24:34 -0800206 char value[PROPERTY_VALUE_MAX];
Glenn Kastenda6ef132013-01-10 12:31:01 -0800207 (void) property_get("ro.debuggable", value, "0");
208 int debuggable = atoi(value);
209 int teeEnabled = 0;
210 if (debuggable) {
211 (void) property_get("af.tee", value, "0");
212 teeEnabled = atoi(value);
213 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800214 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700215 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800216 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700217 }
218 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800219 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700220 }
221 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800222 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700223 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800224#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700225}
226
227void AudioFlinger::onFirstRef()
228{
Eric Laurent93575202011-01-18 18:39:02 -0800229 Mutex::Autolock _l(mLock);
230
Dima Zavin799a70e2011-04-18 16:57:27 -0700231 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800232 char val_str[PROPERTY_VALUE_MAX] = { 0 };
233 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
234 uint32_t int_val;
235 if (1 == sscanf(val_str, "%u", &int_val)) {
236 mStandbyTimeInNsecs = milliseconds(int_val);
237 ALOGI("Using %u mSec as standby time.", int_val);
238 } else {
239 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
240 ALOGI("Using default %u mSec as standby time.",
241 (uint32_t)(mStandbyTimeInNsecs / 1000000));
242 }
243 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244
Eric Laurent1c333e22014-05-20 10:48:17 -0700245 mPatchPanel = new PatchPanel(this);
246
Eric Laurenta4c5a552012-03-29 10:12:40 -0700247 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248}
249
250AudioFlinger::~AudioFlinger()
251{
252 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700253 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700254 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255 }
256 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700257 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700258 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700260
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800261 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
262 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700263 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
264 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700266
267 // Tell media.log service about any old writers that still need to be unregistered
Andy Hungce717682015-12-22 13:40:32 -0800268 if (mLogMemoryDealer != 0) {
269 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
270 if (binder != 0) {
271 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
272 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
273 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
274 mUnregisteredWriters.pop();
275 mediaLogService->unregisterWriter(iMemory);
276 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700277 }
278 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279}
280
Eric Laurenta4c5a552012-03-29 10:12:40 -0700281static const char * const audio_interfaces[] = {
282 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
283 AUDIO_HARDWARE_MODULE_ID_A2DP,
284 AUDIO_HARDWARE_MODULE_ID_USB,
285};
286#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
287
Phil Burk062e67a2015-02-11 13:40:50 -0800288AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700289 audio_module_handle_t module,
290 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700291{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700292 // if module is 0, the request comes from an old policy manager and we should load
293 // well known modules
294 if (module == 0) {
295 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
296 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
297 loadHwModule_l(audio_interfaces[i]);
298 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700299 // then try to find a module supporting the requested device.
300 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
301 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
302 audio_hw_device_t *dev = audioHwDevice->hwDevice();
303 if ((dev->get_supported_devices != NULL) &&
304 (dev->get_supported_devices(dev) & devices) == devices)
305 return audioHwDevice;
306 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700307 } else {
308 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700309 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
310 if (audioHwDevice != NULL) {
311 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700312 }
313 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700314
Dima Zavin799a70e2011-04-18 16:57:27 -0700315 return NULL;
316}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317
Glenn Kasten0f11b512014-01-31 16:18:54 -0800318void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319{
320 const size_t SIZE = 256;
321 char buffer[SIZE];
322 String8 result;
323
324 result.append("Clients:\n");
325 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800326 sp<Client> client = mClients.valueAt(i).promote();
327 if (client != 0) {
328 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
329 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330 }
331 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700332
Eric Laurentd1b28d42013-09-18 18:47:13 -0700333 result.append("Notification Clients:\n");
334 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
335 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
336 result.append(buffer);
337 }
338
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700339 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800340 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700341 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
342 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800343 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700344 result.append(buffer);
345 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347}
348
349
Glenn Kasten0f11b512014-01-31 16:18:54 -0800350void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351{
352 const size_t SIZE = 256;
353 char buffer[SIZE];
354 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800355 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356
John Grossman4ff14ba2012-02-08 16:37:41 -0800357 snprintf(buffer, SIZE, "Hardware status: %d\n"
358 "Standby Time mSec: %u\n",
359 hardwareStatus,
360 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361 result.append(buffer);
362 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363}
364
Glenn Kasten0f11b512014-01-31 16:18:54 -0800365void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700366{
367 const size_t SIZE = 256;
368 char buffer[SIZE];
369 String8 result;
370 snprintf(buffer, SIZE, "Permission Denial: "
371 "can't dump AudioFlinger from pid=%d, uid=%d\n",
372 IPCThreadState::self()->getCallingPid(),
373 IPCThreadState::self()->getCallingUid());
374 result.append(buffer);
375 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376}
377
Eric Laurent81784c32012-11-19 14:55:58 -0800378bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379{
380 bool locked = false;
381 for (int i = 0; i < kDumpLockRetries; ++i) {
382 if (mutex.tryLock() == NO_ERROR) {
383 locked = true;
384 break;
385 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800386 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387 }
388 return locked;
389}
390
391status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
392{
Glenn Kasten44deb052012-02-05 18:09:08 -0800393 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394 dumpPermissionDenial(fd, args);
395 } else {
396 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800397 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700398 if (!hardwareLocked) {
399 String8 result(kHardwareLockedString);
400 write(fd, result.string(), result.size());
401 } else {
402 mHardwareLock.unlock();
403 }
404
Eric Laurent81784c32012-11-19 14:55:58 -0800405 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700406
407 // failed to lock - AudioFlinger is probably deadlocked
408 if (!locked) {
409 String8 result(kDeadlockedString);
410 write(fd, result.string(), result.size());
411 }
412
Eric Laurent021cf962014-05-13 10:18:14 -0700413 bool clientLocked = dumpTryLock(mClientLock);
414 if (!clientLocked) {
415 String8 result(kClientLockedString);
416 write(fd, result.string(), result.size());
417 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700418
419 EffectDumpEffects(fd);
420
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700422 if (clientLocked) {
423 mClientLock.unlock();
424 }
425
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426 dumpInternals(fd, args);
427
428 // dump playback threads
429 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
430 mPlaybackThreads.valueAt(i)->dump(fd, args);
431 }
432
433 // dump record threads
434 for (size_t i = 0; i < mRecordThreads.size(); i++) {
435 mRecordThreads.valueAt(i)->dump(fd, args);
436 }
437
Eric Laurentaaa44472014-09-12 17:41:50 -0700438 // dump orphan effect chains
439 if (mOrphanEffectChains.size() != 0) {
440 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
441 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
442 mOrphanEffectChains.valueAt(i)->dump(fd, args);
443 }
444 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700445 // dump all hardware devs
446 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700447 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700448 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700449 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700450
Glenn Kasten46909e72013-02-26 09:20:22 -0800451#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700452 // dump the serially shared record tee sink
453 if (mRecordTeeSource != 0) {
454 dumpTee(fd, mRecordTeeSource);
455 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800456#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700457
Glenn Kastend65d73c2012-06-22 17:21:07 -0700458 if (locked) {
459 mLock.unlock();
460 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800461
462 // append a copy of media.log here by forwarding fd to it, but don't attempt
463 // to lookup the service if it's not running, as it will block for a second
464 if (mLogMemoryDealer != 0) {
465 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
466 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700467 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800468 Vector<String16> args;
469 binder->dump(fd, args);
470 }
471 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700472
473 // check for optional arguments
474 bool unreachableMemory = false;
475 for (const auto &arg : args) {
476 if (arg == String16("--unreachable")) {
477 unreachableMemory = true;
478 }
479 }
480
481 if (unreachableMemory) {
482 dprintf(fd, "\nDumping unreachable memory:\n");
483 // TODO - should limit be an argument parameter?
484 std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */);
485 write(fd, s.c_str(), s.size());
486 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700487 }
488 return NO_ERROR;
489}
490
Eric Laurent021cf962014-05-13 10:18:14 -0700491sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800492{
Eric Laurent021cf962014-05-13 10:18:14 -0700493 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800494 // If pid is already in the mClients wp<> map, then use that entry
495 // (for which promote() is always != 0), otherwise create a new entry and Client.
496 sp<Client> client = mClients.valueFor(pid).promote();
497 if (client == 0) {
498 client = new Client(this, pid);
499 mClients.add(pid, client);
500 }
501
502 return client;
503}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700504
Glenn Kasten9e58b552013-01-18 15:09:48 -0800505sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
506{
Glenn Kasten481fb672013-09-30 14:39:28 -0700507 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800508 if (mLogMemoryDealer == 0) {
509 return new NBLog::Writer();
510 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800511 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700512 // Similarly if we can't contact the media.log service, also return a dummy writer
513 if (binder == 0) {
514 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800515 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700516 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
517 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
518 // If allocation fails, consult the vector of previously unregistered writers
519 // and garbage-collect one or more them until an allocation succeeds
520 if (shared == 0) {
521 Mutex::Autolock _l(mUnregisteredWritersLock);
522 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
523 {
524 // Pick the oldest stale writer to garbage-collect
525 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
526 mUnregisteredWriters.removeAt(0);
527 mediaLogService->unregisterWriter(iMemory);
528 // Now the media.log remote reference to IMemory is gone. When our last local
529 // reference to IMemory also drops to zero at end of this block,
530 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
531 }
532 // Re-attempt the allocation
533 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
534 if (shared != 0) {
535 goto success;
536 }
537 }
538 // Even after garbage-collecting all old writers, there is still not enough memory,
539 // so return a dummy writer
540 return new NBLog::Writer();
541 }
542success:
543 mediaLogService->registerWriter(shared, size, name);
544 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800545}
546
547void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
548{
Glenn Kasten685ef092013-02-04 08:15:34 -0800549 if (writer == 0) {
550 return;
551 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800552 sp<IMemory> iMemory(writer->getIMemory());
553 if (iMemory == 0) {
554 return;
555 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700556 // Rather than removing the writer immediately, append it to a queue of old writers to
557 // be garbage-collected later. This allows us to continue to view old logs for a while.
558 Mutex::Autolock _l(mUnregisteredWritersLock);
559 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800560}
561
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562// IAudioFlinger interface
563
564
565sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800566 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800568 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700569 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800570 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800571 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800573 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800574 pid_t tid,
Glenn Kastend848eb42016-03-08 13:42:11 -0800575 audio_session_t *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800576 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577 status_t *status)
578{
579 sp<PlaybackThread::Track> track;
580 sp<TrackHandle> trackHandle;
581 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -0800583 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584
Glenn Kasten263709e2012-01-06 08:40:01 -0800585 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
586 // but if someone uses binder directly they could bypass that and cause us to crash
587 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000588 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589 lStatus = BAD_VALUE;
590 goto Exit;
591 }
592
Glenn Kasten53b5d092014-02-05 10:00:23 -0800593 // further sample rate checks are performed by createTrack_l() depending on the thread type
594 if (sampleRate == 0) {
595 ALOGE("createTrack() invalid sample rate %u", sampleRate);
596 lStatus = BAD_VALUE;
597 goto Exit;
598 }
599
600 // further channel mask checks are performed by createTrack_l() depending on the thread type
601 if (!audio_is_output_channel(channelMask)) {
602 ALOGE("createTrack() invalid channel mask %#x", channelMask);
603 lStatus = BAD_VALUE;
604 goto Exit;
605 }
606
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700607 // further format checks are performed by createTrack_l() depending on the thread type
608 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800609 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700610 lStatus = BAD_VALUE;
611 goto Exit;
612 }
613
Glenn Kasten663c2242013-09-24 11:52:37 -0700614 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
615 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
616 lStatus = BAD_VALUE;
617 goto Exit;
618 }
619
Mathias Agopian65ab4712010-07-14 17:59:35 -0700620 {
621 Mutex::Autolock _l(mLock);
622 PlaybackThread *thread = checkPlaybackThread_l(output);
623 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800624 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625 lStatus = BAD_VALUE;
626 goto Exit;
627 }
628
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800629 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700630 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631
Glenn Kastene848bd92014-03-13 15:00:32 -0700632 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700633 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800634 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
635 ALOGE("createTrack() invalid session ID %d", *sessionId);
636 lStatus = BAD_VALUE;
637 goto Exit;
638 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700639 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700640 // check if an effect chain with the same session ID is present on another
641 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700642 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700643 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
644 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700645 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700646 if (sessions & PlaybackThread::EFFECT_SESSION) {
647 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700648 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700649 }
Eric Laurentde070132010-07-13 04:45:46 -0700650 }
651 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700653 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800654 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655 if (sessionId != NULL) {
656 *sessionId = lSessionId;
657 }
658 }
Steve Block3856b092011-10-20 11:56:00 +0100659 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660
661 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800662 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800663 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700664 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700665
666 // move effect chain to this output thread if an effect on same session was waiting
667 // for a track to be created
668 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700669 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700670 Mutex::Autolock _dl(thread->mLock);
671 Mutex::Autolock _sl(effectThread->mLock);
672 moveEffectChain_l(lSessionId, effectThread, thread, true);
673 }
Eric Laurenta011e352012-03-29 15:51:43 -0700674
675 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700676 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700677 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
678 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700679 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700680 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700681 } else {
682 mPendingSyncEvents[i]->cancel();
683 }
Eric Laurenta011e352012-03-29 15:51:43 -0700684 mPendingSyncEvents.removeAt(i);
685 i--;
686 }
687 }
688 }
Glenn Kastene198c362013-08-13 09:13:36 -0700689
Glenn Kastend848eb42016-03-08 13:42:11 -0800690 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691 }
Glenn Kastene198c362013-08-13 09:13:36 -0700692
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700693 if (lStatus != NO_ERROR) {
694 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700695 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700696 // Don't hold mClientLock when releasing the reference on the track as the
697 // destructor will acquire it.
698 {
699 Mutex::Autolock _cl(mClientLock);
700 client.clear();
701 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700703 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 }
705
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700706 // return handle to client
707 trackHandle = new TrackHandle(track);
708
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700710 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 return trackHandle;
712}
713
Glenn Kasten2c073da2016-02-26 09:14:08 -0800714uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715{
716 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800717 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700718 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800719 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 return 0;
721 }
722 return thread->sampleRate();
723}
724
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800725audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726{
727 Mutex::Autolock _l(mLock);
728 PlaybackThread *thread = checkPlaybackThread_l(output);
729 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000730 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800731 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732 }
733 return thread->format();
734}
735
Glenn Kasten2c073da2016-02-26 09:14:08 -0800736size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700737{
738 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800739 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800741 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742 return 0;
743 }
Glenn Kasten58912562012-04-03 10:45:00 -0700744 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
745 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700746 return thread->frameCount();
747}
748
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800749uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750{
751 Mutex::Autolock _l(mLock);
752 PlaybackThread *thread = checkPlaybackThread_l(output);
753 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800754 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755 return 0;
756 }
757 return thread->latency();
758}
759
760status_t AudioFlinger::setMasterVolume(float value)
761{
Eric Laurenta1884f92011-08-23 08:25:03 -0700762 status_t ret = initCheck();
763 if (ret != NO_ERROR) {
764 return ret;
765 }
766
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767 // check calling permissions
768 if (!settingsAllowed()) {
769 return PERMISSION_DENIED;
770 }
771
Eric Laurenta4c5a552012-03-29 10:12:40 -0700772 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700773 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700774
John Grossmanee578c02012-07-23 17:05:46 -0700775 // Set master volume in the HALs which support it.
776 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
777 AutoMutex lock(mHardwareLock);
778 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800779
John Grossmanee578c02012-07-23 17:05:46 -0700780 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
781 if (dev->canSetMasterVolume()) {
782 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800783 }
John Grossmanee578c02012-07-23 17:05:46 -0700784 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700785 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700786
John Grossmanee578c02012-07-23 17:05:46 -0700787 // Now set the master volume in each playback thread. Playback threads
788 // assigned to HALs which do not have master volume support will apply
789 // master volume during the mix operation. Threads with HALs which do
790 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700791 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
792 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
793 continue;
794 }
John Grossmanee578c02012-07-23 17:05:46 -0700795 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700796 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797
798 return NO_ERROR;
799}
800
Glenn Kastenf78aee72012-01-04 11:00:47 -0800801status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802{
Eric Laurenta1884f92011-08-23 08:25:03 -0700803 status_t ret = initCheck();
804 if (ret != NO_ERROR) {
805 return ret;
806 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807
808 // check calling permissions
809 if (!settingsAllowed()) {
810 return PERMISSION_DENIED;
811 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800812 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000813 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 return BAD_VALUE;
815 }
816
817 { // scope for the lock
818 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700819 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700821 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700822 mHardwareStatus = AUDIO_HW_IDLE;
823 }
824
825 if (NO_ERROR == ret) {
826 Mutex::Autolock _l(mLock);
827 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800828 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700829 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700830 }
831
832 return ret;
833}
834
835status_t AudioFlinger::setMicMute(bool state)
836{
Eric Laurenta1884f92011-08-23 08:25:03 -0700837 status_t ret = initCheck();
838 if (ret != NO_ERROR) {
839 return ret;
840 }
841
Mathias Agopian65ab4712010-07-14 17:59:35 -0700842 // check calling permissions
843 if (!settingsAllowed()) {
844 return PERMISSION_DENIED;
845 }
846
847 AutoMutex lock(mHardwareLock);
848 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700849 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
850 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
851 status_t result = dev->set_mic_mute(dev, state);
852 if (result != NO_ERROR) {
853 ret = result;
854 }
855 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856 mHardwareStatus = AUDIO_HW_IDLE;
857 return ret;
858}
859
860bool AudioFlinger::getMicMute() const
861{
Eric Laurenta1884f92011-08-23 08:25:03 -0700862 status_t ret = initCheck();
863 if (ret != NO_ERROR) {
864 return false;
865 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800866 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700867 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800868 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800870 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
871 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
872 status_t result = dev->get_mic_mute(dev, &state);
873 if (result == NO_ERROR) {
874 mute = mute && state;
875 }
876 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700877 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800878
879 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880}
881
882status_t AudioFlinger::setMasterMute(bool muted)
883{
John Grossmand8f178d2012-07-20 14:51:35 -0700884 status_t ret = initCheck();
885 if (ret != NO_ERROR) {
886 return ret;
887 }
888
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889 // check calling permissions
890 if (!settingsAllowed()) {
891 return PERMISSION_DENIED;
892 }
893
John Grossmanee578c02012-07-23 17:05:46 -0700894 Mutex::Autolock _l(mLock);
895 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700896
John Grossmanee578c02012-07-23 17:05:46 -0700897 // Set master mute in the HALs which support it.
898 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
899 AutoMutex lock(mHardwareLock);
900 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700901
John Grossmanee578c02012-07-23 17:05:46 -0700902 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
903 if (dev->canSetMasterMute()) {
904 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700905 }
John Grossmanee578c02012-07-23 17:05:46 -0700906 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700907 }
908
John Grossmanee578c02012-07-23 17:05:46 -0700909 // Now set the master mute in each playback thread. Playback threads
910 // assigned to HALs which do not have master mute support will apply master
911 // mute during the mix operation. Threads with HALs which do support master
912 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700913 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
914 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
915 continue;
916 }
John Grossmanee578c02012-07-23 17:05:46 -0700917 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700918 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919
920 return NO_ERROR;
921}
922
923float AudioFlinger::masterVolume() const
924{
Glenn Kasten98067102011-12-13 11:47:54 -0800925 Mutex::Autolock _l(mLock);
926 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927}
928
929bool AudioFlinger::masterMute() const
930{
Glenn Kasten98067102011-12-13 11:47:54 -0800931 Mutex::Autolock _l(mLock);
932 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933}
934
John Grossman4ff14ba2012-02-08 16:37:41 -0800935float AudioFlinger::masterVolume_l() const
936{
John Grossman4ff14ba2012-02-08 16:37:41 -0800937 return mMasterVolume;
938}
939
John Grossmand8f178d2012-07-20 14:51:35 -0700940bool AudioFlinger::masterMute_l() const
941{
John Grossmanee578c02012-07-23 17:05:46 -0700942 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700943}
944
Eric Laurent223fd5c2014-11-11 13:43:36 -0800945status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
946{
947 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
948 ALOGW("setStreamVolume() invalid stream %d", stream);
949 return BAD_VALUE;
950 }
951 pid_t caller = IPCThreadState::self()->getCallingPid();
952 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
953 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
954 return PERMISSION_DENIED;
955 }
956
957 return NO_ERROR;
958}
959
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800960status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
961 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700962{
963 // check calling permissions
964 if (!settingsAllowed()) {
965 return PERMISSION_DENIED;
966 }
967
Eric Laurent223fd5c2014-11-11 13:43:36 -0800968 status_t status = checkStreamType(stream);
969 if (status != NO_ERROR) {
970 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800972 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973
974 AutoMutex lock(mLock);
975 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700976 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977 thread = checkPlaybackThread_l(output);
978 if (thread == NULL) {
979 return BAD_VALUE;
980 }
981 }
982
983 mStreamTypes[stream].volume = value;
984
985 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800986 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700987 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700988 }
989 } else {
990 thread->setStreamVolume(stream, value);
991 }
992
993 return NO_ERROR;
994}
995
Glenn Kastenfff6d712012-01-12 16:38:12 -0800996status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700997{
998 // check calling permissions
999 if (!settingsAllowed()) {
1000 return PERMISSION_DENIED;
1001 }
1002
Eric Laurent223fd5c2014-11-11 13:43:36 -08001003 status_t status = checkStreamType(stream);
1004 if (status != NO_ERROR) {
1005 return status;
1006 }
1007 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1008
1009 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001010 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001011 return BAD_VALUE;
1012 }
1013
Eric Laurent93575202011-01-18 18:39:02 -08001014 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -07001016 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001017 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001018
1019 return NO_ERROR;
1020}
1021
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001022float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001024 status_t status = checkStreamType(stream);
1025 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 return 0.0f;
1027 }
1028
1029 AutoMutex lock(mLock);
1030 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001031 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001032 PlaybackThread *thread = checkPlaybackThread_l(output);
1033 if (thread == NULL) {
1034 return 0.0f;
1035 }
1036 volume = thread->streamVolume(stream);
1037 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001038 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039 }
1040
1041 return volume;
1042}
1043
Glenn Kastenfff6d712012-01-12 16:38:12 -08001044bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001046 status_t status = checkStreamType(stream);
1047 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048 return true;
1049 }
1050
Glenn Kasten6637baa2012-01-09 09:40:36 -08001051 AutoMutex lock(mLock);
1052 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001053}
1054
Eric Laurent054d9d32015-04-24 08:48:48 -07001055
1056void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1057{
1058 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1059 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1060 }
1061}
1062
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001063status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001065 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1066 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001067
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068 // check calling permissions
1069 if (!settingsAllowed()) {
1070 return PERMISSION_DENIED;
1071 }
1072
Glenn Kasten142f5192014-03-25 17:44:59 -07001073 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1074 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001075 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001076 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001077 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001078 AutoMutex lock(mHardwareLock);
1079 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1080 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1081 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1082 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1083 final_result = result ?: final_result;
1084 }
1085 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001086 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001087 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1088 AudioParameter param = AudioParameter(keyValuePairs);
1089 String8 value;
1090 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001091 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1092 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001093 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1094 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001095 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001096 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1097 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001098 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001099 // suspend effects associated with those session IDs
1100 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001101 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001102 thread->setEffectSuspended(FX_IID_AEC,
1103 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001104 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001105 thread->setEffectSuspended(FX_IID_NS,
1106 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001107 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001108 }
1109 }
Eric Laurentbee53372011-08-29 12:42:48 -07001110 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001111 }
1112 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001113 String8 screenState;
1114 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1115 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001116 if (isOff != (AudioFlinger::mScreenState & 1)) {
1117 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001118 }
1119 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001120 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121 }
1122
1123 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1124 // and the thread is exited once the lock is released
1125 sp<ThreadBase> thread;
1126 {
1127 Mutex::Autolock _l(mLock);
1128 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001129 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001131 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001132 // indicate output device change to all input threads for pre processing
1133 AudioParameter param = AudioParameter(keyValuePairs);
1134 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001135 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1136 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001137 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001138 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139 }
1140 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001141 if (thread != 0) {
1142 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143 }
1144 return BAD_VALUE;
1145}
1146
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001147String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001149 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1150 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151
Eric Laurenta4c5a552012-03-29 10:12:40 -07001152 Mutex::Autolock _l(mLock);
1153
Glenn Kasten142f5192014-03-25 17:44:59 -07001154 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001155 String8 out_s8;
1156
Dima Zavin799a70e2011-04-18 16:57:27 -07001157 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001158 char *s;
1159 {
1160 AutoMutex lock(mHardwareLock);
1161 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001162 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001163 s = dev->get_parameters(dev, keys.string());
1164 mHardwareStatus = AUDIO_HW_IDLE;
1165 }
John Grossmanef7740b2012-02-09 11:28:36 -08001166 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001167 free(s);
1168 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001169 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170 }
1171
Mathias Agopian65ab4712010-07-14 17:59:35 -07001172 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1173 if (playbackThread != NULL) {
1174 return playbackThread->getParameters(keys);
1175 }
1176 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1177 if (recordThread != NULL) {
1178 return recordThread->getParameters(keys);
1179 }
1180 return String8("");
1181}
1182
Glenn Kastendd8104c2012-07-02 12:42:44 -07001183size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1184 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185{
Eric Laurenta1884f92011-08-23 08:25:03 -07001186 status_t ret = initCheck();
1187 if (ret != NO_ERROR) {
1188 return 0;
1189 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001190 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001191 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001192 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001193 return 0;
1194 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001195
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001196 AutoMutex lock(mHardwareLock);
1197 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001198 audio_config_t config, proposed;
1199 memset(&proposed, 0, sizeof(proposed));
1200 proposed.sample_rate = sampleRate;
1201 proposed.channel_mask = channelMask;
1202 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001203
John Grossmanee578c02012-07-23 17:05:46 -07001204 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001205 size_t frames;
1206 for (;;) {
1207 // Note: config is currently a const parameter for get_input_buffer_size()
1208 // but we use a copy from proposed in case config changes from the call.
1209 config = proposed;
1210 frames = dev->get_input_buffer_size(dev, &config);
1211 if (frames != 0) {
1212 break; // hal success, config is the result
1213 }
1214 // change one parameter of the configuration each iteration to a more "common" value
1215 // to see if the device will support it.
1216 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1217 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1218 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1219 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1220 } else {
1221 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1222 "format %#x, channelMask 0x%X",
1223 sampleRate, format, channelMask);
1224 break; // retries failed, break out of loop with frames == 0.
1225 }
1226 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001227 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001228 if (frames > 0 && config.sample_rate != sampleRate) {
1229 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1230 }
1231 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001232}
1233
Glenn Kasten5f972c02014-01-13 09:59:31 -08001234uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236 Mutex::Autolock _l(mLock);
1237
1238 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1239 if (recordThread != NULL) {
1240 return recordThread->getInputFramesLost();
1241 }
1242 return 0;
1243}
1244
1245status_t AudioFlinger::setVoiceVolume(float value)
1246{
Eric Laurenta1884f92011-08-23 08:25:03 -07001247 status_t ret = initCheck();
1248 if (ret != NO_ERROR) {
1249 return ret;
1250 }
1251
Mathias Agopian65ab4712010-07-14 17:59:35 -07001252 // check calling permissions
1253 if (!settingsAllowed()) {
1254 return PERMISSION_DENIED;
1255 }
1256
1257 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001258 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001259 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001260 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001261 mHardwareStatus = AUDIO_HW_IDLE;
1262
1263 return ret;
1264}
1265
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001266status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001267 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001268{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269 Mutex::Autolock _l(mLock);
1270
1271 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1272 if (playbackThread != NULL) {
1273 return playbackThread->getRenderPosition(halFrames, dspFrames);
1274 }
1275
1276 return BAD_VALUE;
1277}
1278
1279void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1280{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001281 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001282 if (client == 0) {
1283 return;
1284 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001285 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001286 {
1287 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001288 if (mNotificationClients.indexOfKey(pid) < 0) {
1289 sp<NotificationClient> notificationClient = new NotificationClient(this,
1290 client,
1291 pid);
1292 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001293
Eric Laurent021cf962014-05-13 10:18:14 -07001294 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001295
Marco Nelissen06b46062014-11-14 07:58:25 -08001296 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001297 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001298 }
1299 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300
Eric Laurent021cf962014-05-13 10:18:14 -07001301 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001302 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001303 // the config change is always sent from playback or record threads to avoid deadlock
1304 // with AudioSystem::gLock
1305 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1306 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1307 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001308
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001309 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1310 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001311 }
1312}
1313
1314void AudioFlinger::removeNotificationClient(pid_t pid)
1315{
1316 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001317 {
1318 Mutex::Autolock _cl(mClientLock);
1319 mNotificationClients.removeItem(pid);
1320 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001321
Steve Block3856b092011-10-20 11:56:00 +01001322 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001323 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001324 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001325 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001326 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001327 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001328 if (ref->mPid == pid) {
1329 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001330 mAudioSessionRefs.removeAt(i);
1331 delete ref;
1332 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001333 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001334 } else {
1335 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001336 }
1337 }
1338 if (removed) {
1339 purgeStaleEffects_l();
1340 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001341}
1342
Eric Laurent73e26b62015-04-27 16:55:58 -07001343void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001344 const sp<AudioIoDescriptor>& ioDesc,
1345 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001346{
Eric Laurent021cf962014-05-13 10:18:14 -07001347 Mutex::Autolock _l(mClientLock);
1348 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001350 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1351 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1352 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001353 }
1354}
1355
Eric Laurent021cf962014-05-13 10:18:14 -07001356// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001357void AudioFlinger::removeClient_l(pid_t pid)
1358{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001359 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001360 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001361 mClients.removeItem(pid);
1362}
1363
Eric Laurent717e1282012-06-29 16:36:52 -07001364// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001365sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1366 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001367{
1368 sp<PlaybackThread> thread;
1369
1370 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1371 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1372 ALOG_ASSERT(thread == 0);
1373 thread = mPlaybackThreads.valueAt(i);
1374 }
1375 }
1376
1377 return thread;
1378}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001379
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381
1382// ----------------------------------------------------------------------------
1383
1384AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1385 : RefBase(),
1386 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001387 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301389 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1390 heapSize *= 1024;
1391 if (!heapSize) {
1392 heapSize = kClientSharedHeapSizeBytes;
1393 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1394 // invalidated tracks
1395 if (!audioFlinger->isLowRamDevice()) {
1396 heapSize *= kClientSharedHeapSizeMultiplier;
1397 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001398 }
1399 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001400}
1401
Eric Laurent021cf962014-05-13 10:18:14 -07001402// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403AudioFlinger::Client::~Client()
1404{
1405 mAudioFlinger->removeClient_l(mPid);
1406}
1407
Glenn Kasten435dbe62012-01-30 10:15:48 -08001408sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409{
1410 return mMemoryDealer;
1411}
1412
1413// ----------------------------------------------------------------------------
1414
1415AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1416 const sp<IAudioFlingerClient>& client,
1417 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001418 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001419{
1420}
1421
1422AudioFlinger::NotificationClient::~NotificationClient()
1423{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424}
1425
Glenn Kasten0f11b512014-01-31 16:18:54 -08001426void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001427{
1428 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001429 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001430}
1431
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432
1433// ----------------------------------------------------------------------------
1434
1435sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001436 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001438 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001439 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001440 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001441 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001442 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001443 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001444 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001445 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001446 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001447 sp<IMemory>& cblk,
1448 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449 status_t *status)
1450{
1451 sp<RecordThread::RecordTrack> recordTrack;
1452 sp<RecordHandle> recordHandle;
1453 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001454 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001455 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456
Glenn Kastend776ac62014-05-07 09:16:09 -07001457 cblk.clear();
1458 buffers.clear();
1459
Marco Nelissendcb346b2015-09-09 10:47:29 -07001460 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1461 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001462 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001463 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1464 clientUid = callingUid;
1465 }
1466
Mathias Agopian65ab4712010-07-14 17:59:35 -07001467 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001468 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001469 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001470 lStatus = PERMISSION_DENIED;
1471 goto Exit;
1472 }
1473
Glenn Kasten53b5d092014-02-05 10:00:23 -08001474 // further sample rate checks are performed by createRecordTrack_l()
1475 if (sampleRate == 0) {
1476 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1477 lStatus = BAD_VALUE;
1478 goto Exit;
1479 }
1480
Andy Hung6770c6f2015-04-07 13:43:36 -07001481 // we don't yet support anything other than linear PCM
1482 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001483 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001484 lStatus = BAD_VALUE;
1485 goto Exit;
1486 }
1487
Glenn Kasten53b5d092014-02-05 10:00:23 -08001488 // further channel mask checks are performed by createRecordTrack_l()
1489 if (!audio_is_input_channel(channelMask)) {
1490 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1491 lStatus = BAD_VALUE;
1492 goto Exit;
1493 }
1494
Glenn Kasten05997e22014-03-13 15:08:33 -07001495 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001496 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001497 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001499 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001500 lStatus = BAD_VALUE;
1501 goto Exit;
1502 }
1503
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001504 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001505 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001506
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001507 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001508 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1509 lStatus = BAD_VALUE;
1510 goto Exit;
1511 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512 lSessionId = *sessionId;
1513 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001514 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001515 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516 if (sessionId != NULL) {
1517 *sessionId = lSessionId;
1518 }
1519 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001520 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001521
Glenn Kasten1879fff2012-07-11 15:36:59 -07001522 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001523 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001524 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001525 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001526
1527 if (lStatus == NO_ERROR) {
1528 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1529 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001530 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001531 if (chain != 0) {
1532 Mutex::Autolock _l(thread->mLock);
1533 thread->addEffectChain_l(chain);
1534 }
1535 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001536 }
Glenn Kastene198c362013-08-13 09:13:36 -07001537
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001538 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001539 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001540 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001541 // Don't hold mClientLock when releasing the reference on the track as the
1542 // destructor will acquire it.
1543 {
1544 Mutex::Autolock _cl(mClientLock);
1545 client.clear();
1546 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001547 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001548 goto Exit;
1549 }
1550
Glenn Kastend776ac62014-05-07 09:16:09 -07001551 cblk = recordTrack->getCblk();
1552 buffers = recordTrack->getBuffers();
1553
Glenn Kasten2fc14732013-08-05 14:58:14 -07001554 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001555 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556
1557Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001558 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001559 return recordHandle;
1560}
1561
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001562
1563
Mathias Agopian65ab4712010-07-14 17:59:35 -07001564// ----------------------------------------------------------------------------
1565
Eric Laurenta4c5a552012-03-29 10:12:40 -07001566audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1567{
Eric Laurent44622db2014-08-01 19:00:33 -07001568 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001569 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001570 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001571 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001572 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001573 }
1574 Mutex::Autolock _l(mLock);
1575 return loadHwModule_l(name);
1576}
1577
1578// loadHwModule_l() must be called with AudioFlinger::mLock held
1579audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1580{
1581 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1582 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1583 ALOGW("loadHwModule() module %s already loaded", name);
1584 return mAudioHwDevs.keyAt(i);
1585 }
1586 }
1587
Eric Laurenta4c5a552012-03-29 10:12:40 -07001588 audio_hw_device_t *dev;
1589
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001590 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001591 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001592 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001593 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001594 }
1595
1596 mHardwareStatus = AUDIO_HW_INIT;
1597 rc = dev->init_check(dev);
1598 mHardwareStatus = AUDIO_HW_IDLE;
1599 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001600 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001601 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001602 }
1603
John Grossmanee578c02012-07-23 17:05:46 -07001604 // Check and cache this HAL's level of support for master mute and master
1605 // volume. If this is the first HAL opened, and it supports the get
1606 // methods, use the initial values provided by the HAL as the current
1607 // master mute and volume settings.
1608
1609 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1610 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001611 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001612
1613 if (0 == mAudioHwDevs.size()) {
1614 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1615 if (NULL != dev->get_master_volume) {
1616 float mv;
1617 if (OK == dev->get_master_volume(dev, &mv)) {
1618 mMasterVolume = mv;
1619 }
1620 }
1621
1622 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1623 if (NULL != dev->get_master_mute) {
1624 bool mm;
1625 if (OK == dev->get_master_mute(dev, &mm)) {
1626 mMasterMute = mm;
1627 }
1628 }
1629 }
1630
Eric Laurenta4c5a552012-03-29 10:12:40 -07001631 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001632 if ((NULL != dev->set_master_volume) &&
1633 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1634 flags = static_cast<AudioHwDevice::Flags>(flags |
1635 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1636 }
1637
1638 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1639 if ((NULL != dev->set_master_mute) &&
1640 (OK == dev->set_master_mute(dev, mMasterMute))) {
1641 flags = static_cast<AudioHwDevice::Flags>(flags |
1642 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1643 }
1644
Eric Laurenta4c5a552012-03-29 10:12:40 -07001645 mHardwareStatus = AUDIO_HW_IDLE;
1646 }
1647
Glenn Kastena13cde92016-03-28 15:26:02 -07001648 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001649 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001650
1651 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001652 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001653
1654 return handle;
1655
1656}
1657
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001658// ----------------------------------------------------------------------------
1659
Glenn Kasten3b16c762012-11-14 08:44:39 -08001660uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001661{
1662 Mutex::Autolock _l(mLock);
1663 PlaybackThread *thread = primaryPlaybackThread_l();
1664 return thread != NULL ? thread->sampleRate() : 0;
1665}
1666
Glenn Kastene33054e2012-11-14 12:54:39 -08001667size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001668{
1669 Mutex::Autolock _l(mLock);
1670 PlaybackThread *thread = primaryPlaybackThread_l();
1671 return thread != NULL ? thread->frameCountHAL() : 0;
1672}
1673
1674// ----------------------------------------------------------------------------
1675
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001676status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1677{
1678 uid_t uid = IPCThreadState::self()->getCallingUid();
1679 if (uid != AID_SYSTEM) {
1680 return PERMISSION_DENIED;
1681 }
1682 Mutex::Autolock _l(mLock);
1683 if (mIsDeviceTypeKnown) {
1684 return INVALID_OPERATION;
1685 }
1686 mIsLowRamDevice = isLowRamDevice;
1687 mIsDeviceTypeKnown = true;
1688 return NO_ERROR;
1689}
1690
Eric Laurent93c3d412014-08-01 14:48:35 -07001691audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1692{
1693 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001694
1695 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1696 if (index >= 0) {
1697 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1698 mHwAvSyncIds.valueAt(index), sessionId);
1699 return mHwAvSyncIds.valueAt(index);
1700 }
1701
1702 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1703 if (dev == NULL) {
1704 return AUDIO_HW_SYNC_INVALID;
1705 }
1706 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1707 AudioParameter param = AudioParameter(String8(reply));
1708 free(reply);
1709
1710 int value;
1711 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1712 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1713 return AUDIO_HW_SYNC_INVALID;
1714 }
1715
1716 // allow only one session for a given HW A/V sync ID.
1717 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1718 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1719 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1720 value, mHwAvSyncIds.keyAt(i));
1721 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001722 break;
1723 }
1724 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001725
1726 mHwAvSyncIds.add(sessionId, value);
1727
1728 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1729 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1730 uint32_t sessions = thread->hasAudioSession(sessionId);
1731 if (sessions & PlaybackThread::TRACK_SESSION) {
1732 AudioParameter param = AudioParameter();
1733 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1734 thread->setParameters(param.toString());
1735 break;
1736 }
1737 }
1738
1739 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1740 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001741}
1742
Eric Laurent72e3f392015-05-20 14:43:50 -07001743status_t AudioFlinger::systemReady()
1744{
1745 Mutex::Autolock _l(mLock);
1746 ALOGI("%s", __FUNCTION__);
1747 if (mSystemReady) {
1748 ALOGW("%s called twice", __FUNCTION__);
1749 return NO_ERROR;
1750 }
1751 mSystemReady = true;
1752 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1753 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1754 thread->systemReady();
1755 }
1756 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1757 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1758 thread->systemReady();
1759 }
1760 return NO_ERROR;
1761}
1762
Eric Laurentfa90e842014-10-17 18:12:31 -07001763// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1764void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1765{
1766 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1767 if (index >= 0) {
1768 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1769 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1770 AudioParameter param = AudioParameter();
1771 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1772 thread->setParameters(param.toString());
1773 }
1774}
1775
1776
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001777// ----------------------------------------------------------------------------
1778
Eric Laurent83b88082014-06-20 18:31:16 -07001779
1780sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001781 audio_io_handle_t *output,
1782 audio_config_t *config,
1783 audio_devices_t devices,
1784 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001785 audio_output_flags_t flags)
1786{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001787 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001788 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001789 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001790 }
1791
Eric Laurentcf2c0212014-07-25 16:20:43 -07001792 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001793 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1794 } else {
1795 // Audio Policy does not currently request a specific output handle.
1796 // If this is ever needed, see openInput_l() for example code.
1797 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1798 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001799 }
Eric Laurent83b88082014-06-20 18:31:16 -07001800
1801 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1802
Eric Laurent83b88082014-06-20 18:31:16 -07001803 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001804 // This if statement allows overriding the audio policy settings
1805 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1806 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1807 // Check only for Normal Mixing mode
1808 if (kEnableExtendedPrecision) {
1809 // Specify format (uncomment one below to choose)
1810 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1811 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1812 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1813 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001814 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001815 }
1816 if (kEnableExtendedChannels) {
1817 // Specify channel mask (uncomment one below to choose)
1818 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1819 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1820 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1821 }
Eric Laurent83b88082014-06-20 18:31:16 -07001822 }
1823
Phil Burk062e67a2015-02-11 13:40:50 -08001824 AudioStreamOut *outputStream = NULL;
1825 status_t status = outHwDev->openOutputStream(
1826 &outputStream,
1827 *output,
1828 devices,
1829 flags,
1830 config,
1831 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001832
1833 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001834
Phil Burk062e67a2015-02-11 13:40:50 -08001835 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001836
1837 PlaybackThread *thread;
1838 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurent51716182016-02-29 18:00:56 -08001839 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady,
1840 config->offload_info.bit_rate);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001841 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001842 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1843 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001844 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001845 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001846 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001847 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001848 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001849 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001850 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001851 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001852 return thread;
1853 }
1854
1855 return 0;
1856}
1857
Eric Laurentcf2c0212014-07-25 16:20:43 -07001858status_t AudioFlinger::openOutput(audio_module_handle_t module,
1859 audio_io_handle_t *output,
1860 audio_config_t *config,
1861 audio_devices_t *devices,
1862 const String8& address,
1863 uint32_t *latencyMs,
1864 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001865{
Phil Burk062e67a2015-02-11 13:40:50 -08001866 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001867 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001868 (devices != NULL) ? *devices : 0,
1869 config->sample_rate,
1870 config->format,
1871 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001872 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001873
Eric Laurentcf2c0212014-07-25 16:20:43 -07001874 if (*devices == AUDIO_DEVICE_NONE) {
1875 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001876 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001877
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878 Mutex::Autolock _l(mLock);
1879
Eric Laurentcf2c0212014-07-25 16:20:43 -07001880 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001881 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001882 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001883
1884 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001885 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001886
1887 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001888 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001889 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001890 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001891
1892 AutoMutex lock(mHardwareLock);
1893 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001894 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001895 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001896 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001897 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001898 }
1899
Eric Laurentcf2c0212014-07-25 16:20:43 -07001900 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901}
1902
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001903audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1904 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001905{
1906 Mutex::Autolock _l(mLock);
1907 MixerThread *thread1 = checkMixerThread_l(output1);
1908 MixerThread *thread2 = checkMixerThread_l(output2);
1909
1910 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001911 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1912 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001913 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001914 }
1915
Glenn Kasteneeecb982016-02-26 10:44:04 -08001916 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07001917 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001918 thread->addOutputTrack(thread2);
1919 mPlaybackThreads.add(id, thread);
1920 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001921 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001922 return id;
1923}
1924
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001925status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001926{
Glenn Kastend96c5722012-04-25 13:44:49 -07001927 return closeOutput_nonvirtual(output);
1928}
1929
1930status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1931{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001932 // keep strong reference on the playback thread so that
1933 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001934 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935 {
1936 Mutex::Autolock _l(mLock);
1937 thread = checkPlaybackThread_l(output);
1938 if (thread == NULL) {
1939 return BAD_VALUE;
1940 }
1941
Steve Block3856b092011-10-20 11:56:00 +01001942 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001943
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001944 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001945 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001946 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001947 DuplicatingThread *dupThread =
1948 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001949 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001950 }
1951 }
1952 }
1953
1954
1955 mPlaybackThreads.removeItem(output);
1956 // save all effects to the default thread
1957 if (mPlaybackThreads.size()) {
1958 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1959 if (dstThread != NULL) {
1960 // audioflinger lock is held here so the acquisition order of thread locks does not
1961 // matter
1962 Mutex::Autolock _dl(dstThread->mLock);
1963 Mutex::Autolock _sl(thread->mLock);
1964 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1965 for (size_t i = 0; i < effectChains.size(); i ++) {
1966 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001967 }
1968 }
1969 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001970 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1971 ioDesc->mIoHandle = output;
1972 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001973 }
1974 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001975 // The thread entity (active unit of execution) is no longer running here,
1976 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001977
Eric Laurentf6870ae2015-05-08 10:50:03 -07001978 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001979 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001980 }
Eric Laurent83b88082014-06-20 18:31:16 -07001981
Mathias Agopian65ab4712010-07-14 17:59:35 -07001982 return NO_ERROR;
1983}
1984
Eric Laurent83b88082014-06-20 18:31:16 -07001985void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1986{
1987 AudioStreamOut *out = thread->clearOutput();
1988 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1989 // from now on thread->mOutput is NULL
1990 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1991 delete out;
1992}
1993
1994void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1995{
1996 mPlaybackThreads.removeItem(thread->mId);
1997 thread->exit();
1998 closeOutputFinish(thread);
1999}
2000
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002001status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002002{
2003 Mutex::Autolock _l(mLock);
2004 PlaybackThread *thread = checkPlaybackThread_l(output);
2005
2006 if (thread == NULL) {
2007 return BAD_VALUE;
2008 }
2009
Steve Block3856b092011-10-20 11:56:00 +01002010 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002011 thread->suspend();
2012
2013 return NO_ERROR;
2014}
2015
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002016status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002017{
2018 Mutex::Autolock _l(mLock);
2019 PlaybackThread *thread = checkPlaybackThread_l(output);
2020
2021 if (thread == NULL) {
2022 return BAD_VALUE;
2023 }
2024
Steve Block3856b092011-10-20 11:56:00 +01002025 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002026
2027 thread->restore();
2028
2029 return NO_ERROR;
2030}
2031
Eric Laurentcf2c0212014-07-25 16:20:43 -07002032status_t AudioFlinger::openInput(audio_module_handle_t module,
2033 audio_io_handle_t *input,
2034 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002035 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002036 const String8& address,
2037 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002038 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002039{
Eric Laurent83b88082014-06-20 18:31:16 -07002040 Mutex::Autolock _l(mLock);
2041
Glenn Kastene7d66712015-03-05 13:46:52 -08002042 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002043 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002044 }
2045
Glenn Kastene7d66712015-03-05 13:46:52 -08002046 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002047
2048 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002049 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002050 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002051 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002052 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002053 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002054}
Dima Zavin799a70e2011-04-18 16:57:27 -07002055
Eric Laurent83b88082014-06-20 18:31:16 -07002056sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002057 audio_io_handle_t *input,
2058 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002059 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002060 const String8& address,
2061 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002062 audio_input_flags_t flags)
2063{
Glenn Kastene7d66712015-03-05 13:46:52 -08002064 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002065 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002066 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002067 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002068 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002069
Glenn Kasteneeecb982016-02-26 10:44:04 -08002070 // Audio Policy can request a specific handle for hardware hotword.
2071 // The goal here is not to re-open an already opened input.
2072 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002073 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002074 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2075 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2076 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2077 return 0;
2078 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2079 // This should not happen in a transient state with current design.
2080 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2081 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002082 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002083
Eric Laurentcf2c0212014-07-25 16:20:43 -07002084 audio_config_t halconfig = *config;
2085 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002086 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002087 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002088 &inStream, flags, address.string(), source);
2089 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002090 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002091 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002092 halconfig.sample_rate,
2093 halconfig.format,
2094 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002095 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002096 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002097
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002098 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002099 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002100 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002101 audio_is_linear_pcm(config->format) &&
2102 audio_is_linear_pcm(halconfig.format) &&
2103 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002104 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2105 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002106 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002107 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002108 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002109 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002110 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002111 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002112 }
2113
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002114 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002115
Glenn Kasten46909e72013-02-26 09:20:22 -08002116#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002117 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2118 // or (re-)create if current Pipe is idle and does not match the new format
2119 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002120 enum {
2121 TEE_SINK_NO, // don't copy input
2122 TEE_SINK_NEW, // copy input using a new pipe
2123 TEE_SINK_OLD, // copy input using an existing pipe
2124 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002125 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2126 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002127 if (!mTeeSinkInputEnabled) {
2128 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002129 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002130 kind = TEE_SINK_NO;
2131 } else if (mRecordTeeSink == 0) {
2132 kind = TEE_SINK_NEW;
2133 } else if (mRecordTeeSink->getStrongCount() != 1) {
2134 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002135 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002136 kind = TEE_SINK_OLD;
2137 } else {
2138 kind = TEE_SINK_NEW;
2139 }
2140 switch (kind) {
2141 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002142 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002143 size_t numCounterOffers = 0;
2144 const NBAIO_Format offers[1] = {format};
2145 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2146 ALOG_ASSERT(index == 0);
2147 PipeReader *pipeReader = new PipeReader(*pipe);
2148 numCounterOffers = 0;
2149 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2150 ALOG_ASSERT(index == 0);
2151 mRecordTeeSink = pipe;
2152 mRecordTeeSource = pipeReader;
2153 teeSink = pipe;
2154 }
2155 break;
2156 case TEE_SINK_OLD:
2157 teeSink = mRecordTeeSink;
2158 break;
2159 case TEE_SINK_NO:
2160 default:
2161 break;
2162 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002163#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002164
Eric Laurentcf2c0212014-07-25 16:20:43 -07002165 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002166
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002167 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002168 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002169 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002170 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002171 inputStream,
2172 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002173 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002174 devices,
2175 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002176#ifdef TEE_SINK
2177 , teeSink
2178#endif
2179 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002180 mRecordThreads.add(*input, thread);
2181 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002182 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002183 }
2184
Eric Laurentcf2c0212014-07-25 16:20:43 -07002185 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002186 return 0;
2187}
2188
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002189status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002190{
Glenn Kastend96c5722012-04-25 13:44:49 -07002191 return closeInput_nonvirtual(input);
2192}
2193
2194status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2195{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002196 // keep strong reference on the record thread so that
2197 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002198 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002199 {
2200 Mutex::Autolock _l(mLock);
2201 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002202 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002203 return BAD_VALUE;
2204 }
2205
Steve Block3856b092011-10-20 11:56:00 +01002206 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002207
2208 // If we still have effect chains, it means that a client still holds a handle
2209 // on at least one effect. We must either move the chain to an existing thread with the
2210 // same session ID or put it aside in case a new record thread is opened for a
2211 // new capture on the same session
2212 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002213 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002214 Mutex::Autolock _sl(thread->mLock);
2215 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002216 // Note: maximum one chain per record thread
2217 if (effectChains.size() != 0) {
2218 chain = effectChains[0];
2219 }
2220 }
2221 if (chain != 0) {
2222 // first check if a record thread is already opened with a client on the same session.
2223 // This should only happen in case of overlap between one thread tear down and the
2224 // creation of its replacement
2225 size_t i;
2226 for (i = 0; i < mRecordThreads.size(); i++) {
2227 sp<RecordThread> t = mRecordThreads.valueAt(i);
2228 if (t == thread) {
2229 continue;
2230 }
2231 if (t->hasAudioSession(chain->sessionId()) != 0) {
2232 Mutex::Autolock _l(t->mLock);
2233 ALOGV("closeInput() found thread %d for effect session %d",
2234 t->id(), chain->sessionId());
2235 t->addEffectChain_l(chain);
2236 break;
2237 }
2238 }
2239 // put the chain aside if we could not find a record thread with the same session id.
2240 if (i == mRecordThreads.size()) {
2241 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002242 }
2243 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002244 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2245 ioDesc->mIoHandle = input;
2246 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002247 mRecordThreads.removeItem(input);
2248 }
Eric Laurent83b88082014-06-20 18:31:16 -07002249 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2250 // we have a different lock for notification client
2251 closeInputFinish(thread);
2252 return NO_ERROR;
2253}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254
Eric Laurent83b88082014-06-20 18:31:16 -07002255void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2256{
2257 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002258 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002259 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002260 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002261 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002262 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002263}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002264
Eric Laurent83b88082014-06-20 18:31:16 -07002265void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2266{
2267 mRecordThreads.removeItem(thread->mId);
2268 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002269}
2270
Glenn Kastend2304db2014-02-03 07:40:31 -08002271status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002272{
2273 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002274 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002275
2276 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2277 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002278 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002279 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002280
2281 return NO_ERROR;
2282}
2283
2284
Glenn Kasteneeecb982016-02-26 10:44:04 -08002285audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002286{
Glenn Kasten9d003132016-04-06 14:38:09 -07002287 // This is a binder API, so a malicious client could pass in a bad parameter.
2288 // Check for that before calling the internal API nextUniqueId().
2289 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2290 ALOGE("newAudioUniqueId invalid use %d", use);
2291 return AUDIO_UNIQUE_ID_ALLOCATE;
2292 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002293 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002294}
2295
Glenn Kastend848eb42016-03-08 13:42:11 -08002296void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002297{
2298 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002299 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002300 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2301 if (pid != -1 && (caller == getpid_cached)) {
2302 caller = pid;
2303 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002304
Eric Laurent021cf962014-05-13 10:18:14 -07002305 {
2306 Mutex::Autolock _cl(mClientLock);
2307 // Ignore requests received from processes not known as notification client. The request
2308 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2309 // called from a different pid leaving a stale session reference. Also we don't know how
2310 // to clear this reference if the client process dies.
2311 if (mNotificationClients.indexOfKey(caller) < 0) {
2312 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2313 return;
2314 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002315 }
2316
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002317 size_t num = mAudioSessionRefs.size();
2318 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002319 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002320 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2321 ref->mCnt++;
2322 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002323 return;
2324 }
2325 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002326 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2327 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002328}
2329
Glenn Kastend848eb42016-03-08 13:42:11 -08002330void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002331{
2332 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002333 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002334 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2335 if (pid != -1 && (caller == getpid_cached)) {
2336 caller = pid;
2337 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002338 size_t num = mAudioSessionRefs.size();
2339 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002340 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002341 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2342 ref->mCnt--;
2343 ALOGV(" decremented refcount to %d", ref->mCnt);
2344 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002345 mAudioSessionRefs.removeAt(i);
2346 delete ref;
2347 purgeStaleEffects_l();
2348 }
2349 return;
2350 }
2351 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002352 // If the caller is mediaserver it is likely that the session being released was acquired
2353 // on behalf of a process not in notification clients and we ignore the warning.
2354 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002355}
2356
2357void AudioFlinger::purgeStaleEffects_l() {
2358
Steve Block3856b092011-10-20 11:56:00 +01002359 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002360
2361 Vector< sp<EffectChain> > chains;
2362
2363 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2364 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2365 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2366 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002367 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2368 chains.push(ec);
2369 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002370 }
2371 }
2372 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2373 sp<RecordThread> t = mRecordThreads.valueAt(i);
2374 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2375 sp<EffectChain> ec = t->mEffectChains[j];
2376 chains.push(ec);
2377 }
2378 }
2379
2380 for (size_t i = 0; i < chains.size(); i++) {
2381 sp<EffectChain> ec = chains[i];
2382 int sessionid = ec->sessionId();
2383 sp<ThreadBase> t = ec->mThread.promote();
2384 if (t == 0) {
2385 continue;
2386 }
2387 size_t numsessionrefs = mAudioSessionRefs.size();
2388 bool found = false;
2389 for (size_t k = 0; k < numsessionrefs; k++) {
2390 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002391 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002392 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002393 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002394 found = true;
2395 break;
2396 }
2397 }
2398 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002399 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002400 // remove all effects from the chain
2401 while (ec->mEffects.size()) {
2402 sp<EffectModule> effect = ec->mEffects[0];
2403 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002404 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002405 if (effect->purgeHandles()) {
2406 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002407 }
2408 AudioSystem::unregisterEffect(effect->id());
2409 }
2410 }
2411 }
2412 return;
2413}
2414
Glenn Kasteneeecb982016-02-26 10:44:04 -08002415// checkThread_l() must be called with AudioFlinger::mLock held
2416AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2417{
2418 ThreadBase *thread = NULL;
2419 switch (audio_unique_id_get_use(ioHandle)) {
2420 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2421 thread = checkPlaybackThread_l(ioHandle);
2422 break;
2423 case AUDIO_UNIQUE_ID_USE_INPUT:
2424 thread = checkRecordThread_l(ioHandle);
2425 break;
2426 default:
2427 break;
2428 }
2429 return thread;
2430}
2431
Mathias Agopian65ab4712010-07-14 17:59:35 -07002432// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002433AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002434{
Glenn Kastena1117922012-01-26 10:53:32 -08002435 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002436}
2437
2438// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002439AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002440{
2441 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002442 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002443}
2444
2445// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002446AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002447{
Glenn Kastena1117922012-01-26 10:53:32 -08002448 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002449}
2450
Glenn Kasteneeecb982016-02-26 10:44:04 -08002451audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002452{
Glenn Kasten9d003132016-04-06 14:38:09 -07002453 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002454 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002455 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2456 for (int retry = 0; retry < maxRetries; retry++) {
2457 // The cast allows wraparound from max positive to min negative instead of abort
2458 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2459 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2460 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2461 // allow wrap by skipping 0 and -1 for session ids
2462 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2463 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2464 return (audio_unique_id_t) (base | use);
2465 }
2466 }
2467 // We have no way of recovering from wraparound
2468 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2469 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002470}
2471
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002472AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002473{
2474 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2475 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002476 if(thread->isDuplicating()) {
2477 continue;
2478 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002479 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002480 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002481 return thread;
2482 }
2483 }
2484 return NULL;
2485}
2486
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002487audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002488{
2489 PlaybackThread *thread = primaryPlaybackThread_l();
2490
2491 if (thread == NULL) {
2492 return 0;
2493 }
2494
Eric Laurentf1c04f92012-08-28 14:26:53 -07002495 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002496}
2497
Eric Laurenta011e352012-03-29 15:51:43 -07002498sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002499 audio_session_t triggerSession,
2500 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002501 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002502 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002503{
2504 Mutex::Autolock _l(mLock);
2505
2506 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2507 status_t playStatus = NAME_NOT_FOUND;
2508 status_t recStatus = NAME_NOT_FOUND;
2509 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2510 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2511 if (playStatus == NO_ERROR) {
2512 return event;
2513 }
2514 }
2515 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2516 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2517 if (recStatus == NO_ERROR) {
2518 return event;
2519 }
2520 }
2521 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2522 mPendingSyncEvents.add(event);
2523 } else {
2524 ALOGV("createSyncEvent() invalid event %d", event->type());
2525 event.clear();
2526 }
2527 return event;
2528}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002529
Mathias Agopian65ab4712010-07-14 17:59:35 -07002530// ----------------------------------------------------------------------------
2531// Effect management
2532// ----------------------------------------------------------------------------
2533
2534
Glenn Kastenf587ba52012-01-26 16:25:10 -08002535status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002536{
2537 Mutex::Autolock _l(mLock);
2538 return EffectQueryNumberEffects(numEffects);
2539}
2540
Glenn Kastenf587ba52012-01-26 16:25:10 -08002541status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002542{
2543 Mutex::Autolock _l(mLock);
2544 return EffectQueryEffect(index, descriptor);
2545}
2546
Glenn Kasten5e92a782012-01-30 07:40:52 -08002547status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002548 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002549{
2550 Mutex::Autolock _l(mLock);
2551 return EffectGetDescriptor(pUuid, descriptor);
2552}
2553
2554
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002555sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002556 effect_descriptor_t *pDesc,
2557 const sp<IEffectClient>& effectClient,
2558 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002559 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002560 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002561 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002562 status_t *status,
2563 int *id,
2564 int *enabled)
2565{
2566 status_t lStatus = NO_ERROR;
2567 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002568 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002569
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002570 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002571 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002572 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002573
2574 if (pDesc == NULL) {
2575 lStatus = BAD_VALUE;
2576 goto Exit;
2577 }
2578
Eric Laurent84e9a102010-09-23 16:10:16 -07002579 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002580 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002581 lStatus = PERMISSION_DENIED;
2582 goto Exit;
2583 }
2584
Dima Zavinfce7a472011-04-19 22:30:36 -07002585 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002586 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002587 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002588 lStatus = PERMISSION_DENIED;
2589 goto Exit;
2590 }
2591
Mathias Agopian65ab4712010-07-14 17:59:35 -07002592 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002593 if (!EffectIsNullUuid(&pDesc->uuid)) {
2594 // if uuid is specified, request effect descriptor
2595 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2596 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002597 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002598 goto Exit;
2599 }
2600 } else {
2601 // if uuid is not specified, look for an available implementation
2602 // of the required type in effect factory
2603 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002604 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002605 lStatus = BAD_VALUE;
2606 goto Exit;
2607 }
2608 uint32_t numEffects = 0;
2609 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002610 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002611 bool found = false;
2612
2613 lStatus = EffectQueryNumberEffects(&numEffects);
2614 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002615 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002616 goto Exit;
2617 }
2618 for (uint32_t i = 0; i < numEffects; i++) {
2619 lStatus = EffectQueryEffect(i, &desc);
2620 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002621 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002622 continue;
2623 }
2624 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2625 // If matching type found save effect descriptor. If the session is
2626 // 0 and the effect is not auxiliary, continue enumeration in case
2627 // an auxiliary version of this effect type is available
2628 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002629 d = desc;
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 break;
2633 }
2634 }
2635 }
2636 if (!found) {
2637 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002638 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002639 goto Exit;
2640 }
2641 // For same effect type, chose auxiliary version over insert version if
2642 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002643 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002644 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002645 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002646 }
2647 }
2648
2649 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002650 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002651 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2652 lStatus = INVALID_OPERATION;
2653 goto Exit;
2654 }
2655
Eric Laurent59255e42011-07-27 19:49:51 -07002656 // check recording permission for visualizer
2657 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002658 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002659 lStatus = PERMISSION_DENIED;
2660 goto Exit;
2661 }
2662
Mathias Agopian65ab4712010-07-14 17:59:35 -07002663 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002664 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002665 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002666 // if the output returned by getOutputForEffect() is removed before we lock the
2667 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2668 // and we will exit safely
2669 io = AudioSystem::getOutputForEffect(&desc);
2670 ALOGV("createEffect got output %d", io);
2671 }
2672
2673 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002674
2675 // If output is not specified try to find a matching audio session ID in one of the
2676 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002677 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2678 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002679 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002680 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002681 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2682 // output must be specified by AudioPolicyManager when using session
2683 // AUDIO_SESSION_OUTPUT_STAGE
2684 lStatus = BAD_VALUE;
2685 goto Exit;
2686 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002687 // look for the thread where the specified audio session is present
2688 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2689 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2690 io = mPlaybackThreads.keyAt(i);
2691 break;
2692 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002693 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002694 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002695 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2696 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2697 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002698 break;
2699 }
2700 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002701 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002702 // If no output thread contains the requested session ID, default to
2703 // first output. The effect chain will be moved to the correct output
2704 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002705 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002706 io = mPlaybackThreads.keyAt(0);
2707 }
Steve Block3856b092011-10-20 11:56:00 +01002708 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002709 }
2710 ThreadBase *thread = checkRecordThread_l(io);
2711 if (thread == NULL) {
2712 thread = checkPlaybackThread_l(io);
2713 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002714 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002715 lStatus = BAD_VALUE;
2716 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002717 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002718 } else {
2719 // Check if one effect chain was awaiting for an effect to be created on this
2720 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08002721 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07002722 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002723 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002724 thread->addEffectChain_l(chain);
2725 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002726 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002727
Eric Laurent021cf962014-05-13 10:18:14 -07002728 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002729
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002730 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002731 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2732 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002733 if (handle != 0 && id != NULL) {
2734 *id = handle->id();
2735 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002736 if (handle == 0) {
2737 // remove local strong reference to Client with mClientLock held
2738 Mutex::Autolock _cl(mClientLock);
2739 client.clear();
2740 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002741 }
2742
2743Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002744 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002745 return handle;
2746}
2747
Glenn Kastend848eb42016-03-08 13:42:11 -08002748status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002749 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002750{
Steve Block3856b092011-10-20 11:56:00 +01002751 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002752 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002753 Mutex::Autolock _l(mLock);
2754 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002755 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002756 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002757 }
Eric Laurentde070132010-07-13 04:45:46 -07002758 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2759 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002760 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002761 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002762 }
Eric Laurentde070132010-07-13 04:45:46 -07002763 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2764 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002765 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002766 return BAD_VALUE;
2767 }
2768
2769 Mutex::Autolock _dl(dstThread->mLock);
2770 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002771 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002772}
2773
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002774// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08002775status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002776 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002777 AudioFlinger::PlaybackThread *dstThread,
2778 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002779{
Steve Block3856b092011-10-20 11:56:00 +01002780 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002781 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002782
Eric Laurent59255e42011-07-27 19:49:51 -07002783 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002784 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002785 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002786 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002787 return INVALID_OPERATION;
2788 }
2789
Andy Hung9a592762014-07-21 21:56:01 -07002790 // Check whether the destination thread has a channel count of FCC_2, which is
2791 // currently required for (most) effects. Prevent moving the effect chain here rather
2792 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentf6870ae2015-05-08 10:50:03 -07002793 if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
Eric Laurentb10352f2014-11-03 16:25:39 -08002794 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002795 ALOGW("moveEffectChain_l() effect chain failed because"
2796 " destination thread %p channel count(%u) != %u",
2797 dstThread, dstThread->mChannelCount, FCC_2);
2798 return INVALID_OPERATION;
2799 }
2800
Eric Laurent39e94f82010-07-28 01:32:47 -07002801 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002802 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002803 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002804 // removed.
2805 srcThread->removeEffectChain_l(chain);
2806
2807 // transfer all effects one by one so that new effect chain is created on new thread with
2808 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002809 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002810 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002811 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002812 Vector< sp<EffectModule> > removed;
2813 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002814 while (effect != 0) {
2815 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002816 removed.add(effect);
2817 status = dstThread->addEffect_l(effect);
2818 if (status != NO_ERROR) {
2819 break;
2820 }
Eric Laurentec35a142011-10-05 17:42:25 -07002821 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2822 if (effect->state() == EffectModule::ACTIVE ||
2823 effect->state() == EffectModule::STOPPING) {
2824 effect->start();
2825 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002826 // if the move request is not received from audio policy manager, the effect must be
2827 // re-registered with the new strategy and output
2828 if (dstChain == 0) {
2829 dstChain = effect->chain().promote();
2830 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002831 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002832 status = NO_INIT;
2833 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002834 }
2835 strategy = dstChain->strategy();
2836 }
2837 if (reRegister) {
2838 AudioSystem::unregisterEffect(effect->id());
2839 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002840 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002841 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002842 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002843 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002844 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002845 }
Eric Laurentde070132010-07-13 04:45:46 -07002846 effect = chain->getEffectFromId_l(0);
2847 }
2848
Eric Laurent5baf2af2013-09-12 17:37:00 -07002849 if (status != NO_ERROR) {
2850 for (size_t i = 0; i < removed.size(); i++) {
2851 srcThread->addEffect_l(removed[i]);
2852 if (dstChain != 0 && reRegister) {
2853 AudioSystem::unregisterEffect(removed[i]->id());
2854 AudioSystem::registerEffect(&removed[i]->desc(),
2855 srcThread->id(),
2856 strategy,
2857 sessionId,
2858 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002859 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002860 }
2861 }
2862 }
2863
2864 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002865}
2866
Eric Laurent5baf2af2013-09-12 17:37:00 -07002867bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002868{
2869 if (mGlobalEffectEnableTime != 0 &&
2870 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2871 return true;
2872 }
2873
2874 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2875 sp<EffectChain> ec =
2876 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002877 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002878 return true;
2879 }
2880 }
2881 return false;
2882}
2883
Eric Laurent5baf2af2013-09-12 17:37:00 -07002884void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002885{
2886 Mutex::Autolock _l(mLock);
2887
2888 mGlobalEffectEnableTime = systemTime();
2889
2890 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2891 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2892 if (t->mType == ThreadBase::OFFLOAD) {
2893 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2894 }
2895 }
2896
2897}
2898
Eric Laurentaaa44472014-09-12 17:41:50 -07002899status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2900{
Glenn Kastend848eb42016-03-08 13:42:11 -08002901 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002902 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002903 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002904 if (index >= 0) {
2905 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2906 return ALREADY_EXISTS;
2907 }
2908 mOrphanEffectChains.add(session, chain);
2909 return NO_ERROR;
2910}
2911
2912sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2913{
2914 sp<EffectChain> chain;
2915 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002916 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002917 if (index >= 0) {
2918 chain = mOrphanEffectChains.valueAt(index);
2919 mOrphanEffectChains.removeItemsAt(index);
2920 }
2921 return chain;
2922}
2923
2924bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2925{
2926 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08002927 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002928 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002929 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002930 if (index >= 0) {
2931 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2932 if (chain->removeEffect_l(effect) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002933 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002934 mOrphanEffectChains.removeItemsAt(index);
2935 }
2936 return true;
2937 }
2938 return false;
2939}
2940
2941
Glenn Kastenda6ef132013-01-10 12:31:01 -08002942struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002943#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2944 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002945};
2946
2947int comparEntry(const void *p1, const void *p2)
2948{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002949 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002950}
2951
Glenn Kasten46909e72013-02-26 09:20:22 -08002952#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002953void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002954{
Eric Laurent81784c32012-11-19 14:55:58 -08002955 NBAIO_Source *teeSource = source.get();
2956 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002957 // .wav rotation
2958 // There is a benign race condition if 2 threads call this simultaneously.
2959 // They would both traverse the directory, but the result would simply be
2960 // failures at unlink() which are ignored. It's also unlikely since
2961 // normally dumpsys is only done by bugreport or from the command line.
2962 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08002963 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08002964 size_t teePathLen = strlen(teePath);
2965 DIR *dir = opendir(teePath);
2966 teePath[teePathLen++] = '/';
2967 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002968#define TEE_MAX_SORT 20 // number of entries to sort
2969#define TEE_MAX_KEEP 10 // number of entries to keep
2970 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002971 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002972 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002973 struct dirent de;
2974 struct dirent *result = NULL;
2975 int rc = readdir_r(dir, &de, &result);
2976 if (rc != 0) {
2977 ALOGW("readdir_r failed %d", rc);
2978 break;
2979 }
2980 if (result == NULL) {
2981 break;
2982 }
2983 if (result != &de) {
2984 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2985 break;
2986 }
2987 // ignore non .wav file entries
2988 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002989 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08002990 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2991 continue;
2992 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08002993 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002994 }
2995 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002996 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002997 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002998 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
2999 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003000 (void) unlink(teePath);
3001 }
3002 }
3003 } else {
3004 if (fd >= 0) {
Glenn Kasten9a003992016-02-23 15:24:34 -08003005 dprintf(fd, "unable to rotate tees in %.*s: %s\n", teePathLen, teePath,
3006 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003007 }
3008 }
Eric Laurent81784c32012-11-19 14:55:58 -08003009 char teeTime[16];
3010 struct timeval tv;
3011 gettimeofday(&tv, NULL);
3012 struct tm tm;
3013 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003014 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
3015 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
3016 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3017 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003018 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003019 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003020 char wavHeader[44];
3021 memcpy(wavHeader,
3022 "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",
3023 sizeof(wavHeader));
3024 NBAIO_Format format = teeSource->format();
3025 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003026 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003027 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003028 wavHeader[22] = channelCount; // number of channels
3029 wavHeader[24] = sampleRate; // sample rate
3030 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003031 wavHeader[32] = frameSize; // block alignment
3032 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003033 write(teeFd, wavHeader, sizeof(wavHeader));
3034 size_t total = 0;
3035 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003036#define TEE_SINK_READ 1024 // frames per I/O operation
3037 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003038 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003039 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003040 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003041 bool wasFirstRead = firstRead;
3042 firstRead = false;
3043 if (actual <= 0) {
3044 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3045 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003046 }
Eric Laurent81784c32012-11-19 14:55:58 -08003047 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003048 }
Eric Laurent81784c32012-11-19 14:55:58 -08003049 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003050 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003051 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003052 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003053 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003054 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003055 uint32_t temp = 44 + total * frameSize - 8;
3056 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003057 write(teeFd, &temp, sizeof(temp));
3058 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003059 temp = total * frameSize;
3060 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003061 write(teeFd, &temp, sizeof(temp));
3062 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003063 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003064 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003065 }
Eric Laurent59255e42011-07-27 19:49:51 -07003066 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003067 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003068 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003069 }
Eric Laurent59255e42011-07-27 19:49:51 -07003070 }
3071 }
3072}
Glenn Kasten46909e72013-02-26 09:20:22 -08003073#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003074
Mathias Agopian65ab4712010-07-14 17:59:35 -07003075// ----------------------------------------------------------------------------
3076
3077status_t AudioFlinger::onTransact(
3078 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3079{
3080 return BnAudioFlinger::onTransact(code, data, reply, flags);
3081}
3082
Glenn Kasten63238ef2015-03-02 15:50:29 -08003083} // namespace android