blob: 2b0d4c850f0ce0245669946c6d6aad5e31e2ae99 [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 Kasten4a8308b2016-04-18 14:10:01 -0700749size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
750{
751 Mutex::Autolock _l(mLock);
752 ThreadBase *thread = checkThread_l(ioHandle);
753 if (thread == NULL) {
754 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
755 return 0;
756 }
757 return thread->frameCountHAL();
758}
759
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800760uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761{
762 Mutex::Autolock _l(mLock);
763 PlaybackThread *thread = checkPlaybackThread_l(output);
764 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800765 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766 return 0;
767 }
768 return thread->latency();
769}
770
771status_t AudioFlinger::setMasterVolume(float value)
772{
Eric Laurenta1884f92011-08-23 08:25:03 -0700773 status_t ret = initCheck();
774 if (ret != NO_ERROR) {
775 return ret;
776 }
777
Mathias Agopian65ab4712010-07-14 17:59:35 -0700778 // check calling permissions
779 if (!settingsAllowed()) {
780 return PERMISSION_DENIED;
781 }
782
Eric Laurenta4c5a552012-03-29 10:12:40 -0700783 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700784 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700785
John Grossmanee578c02012-07-23 17:05:46 -0700786 // Set master volume in the HALs which support it.
787 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
788 AutoMutex lock(mHardwareLock);
789 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800790
John Grossmanee578c02012-07-23 17:05:46 -0700791 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
792 if (dev->canSetMasterVolume()) {
793 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800794 }
John Grossmanee578c02012-07-23 17:05:46 -0700795 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797
John Grossmanee578c02012-07-23 17:05:46 -0700798 // Now set the master volume in each playback thread. Playback threads
799 // assigned to HALs which do not have master volume support will apply
800 // master volume during the mix operation. Threads with HALs which do
801 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700802 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
803 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
804 continue;
805 }
John Grossmanee578c02012-07-23 17:05:46 -0700806 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700807 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808
809 return NO_ERROR;
810}
811
Glenn Kastenf78aee72012-01-04 11:00:47 -0800812status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700813{
Eric Laurenta1884f92011-08-23 08:25:03 -0700814 status_t ret = initCheck();
815 if (ret != NO_ERROR) {
816 return ret;
817 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818
819 // check calling permissions
820 if (!settingsAllowed()) {
821 return PERMISSION_DENIED;
822 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800823 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000824 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825 return BAD_VALUE;
826 }
827
828 { // scope for the lock
829 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700830 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700832 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 mHardwareStatus = AUDIO_HW_IDLE;
834 }
835
836 if (NO_ERROR == ret) {
837 Mutex::Autolock _l(mLock);
838 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800839 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700840 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841 }
842
843 return ret;
844}
845
846status_t AudioFlinger::setMicMute(bool state)
847{
Eric Laurenta1884f92011-08-23 08:25:03 -0700848 status_t ret = initCheck();
849 if (ret != NO_ERROR) {
850 return ret;
851 }
852
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853 // check calling permissions
854 if (!settingsAllowed()) {
855 return PERMISSION_DENIED;
856 }
857
858 AutoMutex lock(mHardwareLock);
859 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700860 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
861 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
862 status_t result = dev->set_mic_mute(dev, state);
863 if (result != NO_ERROR) {
864 ret = result;
865 }
866 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867 mHardwareStatus = AUDIO_HW_IDLE;
868 return ret;
869}
870
871bool AudioFlinger::getMicMute() const
872{
Eric Laurenta1884f92011-08-23 08:25:03 -0700873 status_t ret = initCheck();
874 if (ret != NO_ERROR) {
875 return false;
876 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800877 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700878 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800879 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800881 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
882 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
883 status_t result = dev->get_mic_mute(dev, &state);
884 if (result == NO_ERROR) {
885 mute = mute && state;
886 }
887 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800889
890 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891}
892
893status_t AudioFlinger::setMasterMute(bool muted)
894{
John Grossmand8f178d2012-07-20 14:51:35 -0700895 status_t ret = initCheck();
896 if (ret != NO_ERROR) {
897 return ret;
898 }
899
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 // check calling permissions
901 if (!settingsAllowed()) {
902 return PERMISSION_DENIED;
903 }
904
John Grossmanee578c02012-07-23 17:05:46 -0700905 Mutex::Autolock _l(mLock);
906 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700907
John Grossmanee578c02012-07-23 17:05:46 -0700908 // Set master mute in the HALs which support it.
909 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
910 AutoMutex lock(mHardwareLock);
911 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700912
John Grossmanee578c02012-07-23 17:05:46 -0700913 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
914 if (dev->canSetMasterMute()) {
915 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700916 }
John Grossmanee578c02012-07-23 17:05:46 -0700917 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700918 }
919
John Grossmanee578c02012-07-23 17:05:46 -0700920 // Now set the master mute in each playback thread. Playback threads
921 // assigned to HALs which do not have master mute support will apply master
922 // mute during the mix operation. Threads with HALs which do support master
923 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700924 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
925 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
926 continue;
927 }
John Grossmanee578c02012-07-23 17:05:46 -0700928 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700929 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930
931 return NO_ERROR;
932}
933
934float AudioFlinger::masterVolume() const
935{
Glenn Kasten98067102011-12-13 11:47:54 -0800936 Mutex::Autolock _l(mLock);
937 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938}
939
940bool AudioFlinger::masterMute() const
941{
Glenn Kasten98067102011-12-13 11:47:54 -0800942 Mutex::Autolock _l(mLock);
943 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944}
945
John Grossman4ff14ba2012-02-08 16:37:41 -0800946float AudioFlinger::masterVolume_l() const
947{
John Grossman4ff14ba2012-02-08 16:37:41 -0800948 return mMasterVolume;
949}
950
John Grossmand8f178d2012-07-20 14:51:35 -0700951bool AudioFlinger::masterMute_l() const
952{
John Grossmanee578c02012-07-23 17:05:46 -0700953 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700954}
955
Eric Laurent223fd5c2014-11-11 13:43:36 -0800956status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
957{
958 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
959 ALOGW("setStreamVolume() invalid stream %d", stream);
960 return BAD_VALUE;
961 }
962 pid_t caller = IPCThreadState::self()->getCallingPid();
963 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
964 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
965 return PERMISSION_DENIED;
966 }
967
968 return NO_ERROR;
969}
970
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800971status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
972 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973{
974 // check calling permissions
975 if (!settingsAllowed()) {
976 return PERMISSION_DENIED;
977 }
978
Eric Laurent223fd5c2014-11-11 13:43:36 -0800979 status_t status = checkStreamType(stream);
980 if (status != NO_ERROR) {
981 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700982 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800983 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984
985 AutoMutex lock(mLock);
986 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700987 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700988 thread = checkPlaybackThread_l(output);
989 if (thread == NULL) {
990 return BAD_VALUE;
991 }
992 }
993
994 mStreamTypes[stream].volume = value;
995
996 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800997 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700998 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999 }
1000 } else {
1001 thread->setStreamVolume(stream, value);
1002 }
1003
1004 return NO_ERROR;
1005}
1006
Glenn Kastenfff6d712012-01-12 16:38:12 -08001007status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008{
1009 // check calling permissions
1010 if (!settingsAllowed()) {
1011 return PERMISSION_DENIED;
1012 }
1013
Eric Laurent223fd5c2014-11-11 13:43:36 -08001014 status_t status = checkStreamType(stream);
1015 if (status != NO_ERROR) {
1016 return status;
1017 }
1018 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1019
1020 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001021 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022 return BAD_VALUE;
1023 }
1024
Eric Laurent93575202011-01-18 18:39:02 -08001025 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -07001027 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001028 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001029
1030 return NO_ERROR;
1031}
1032
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001033float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001035 status_t status = checkStreamType(stream);
1036 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001037 return 0.0f;
1038 }
1039
1040 AutoMutex lock(mLock);
1041 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001042 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001043 PlaybackThread *thread = checkPlaybackThread_l(output);
1044 if (thread == NULL) {
1045 return 0.0f;
1046 }
1047 volume = thread->streamVolume(stream);
1048 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001049 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050 }
1051
1052 return volume;
1053}
1054
Glenn Kastenfff6d712012-01-12 16:38:12 -08001055bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001056{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001057 status_t status = checkStreamType(stream);
1058 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001059 return true;
1060 }
1061
Glenn Kasten6637baa2012-01-09 09:40:36 -08001062 AutoMutex lock(mLock);
1063 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064}
1065
Eric Laurent054d9d32015-04-24 08:48:48 -07001066
1067void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1068{
1069 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1070 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1071 }
1072}
1073
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001074status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001075{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001076 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1077 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001078
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079 // check calling permissions
1080 if (!settingsAllowed()) {
1081 return PERMISSION_DENIED;
1082 }
1083
Glenn Kasten142f5192014-03-25 17:44:59 -07001084 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1085 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001086 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001087 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001088 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001089 AutoMutex lock(mHardwareLock);
1090 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1091 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1092 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1093 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1094 final_result = result ?: final_result;
1095 }
1096 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001097 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001098 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1099 AudioParameter param = AudioParameter(keyValuePairs);
1100 String8 value;
1101 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001102 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1103 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001104 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1105 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001106 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001107 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1108 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001109 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001110 // suspend effects associated with those session IDs
1111 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001112 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001113 thread->setEffectSuspended(FX_IID_AEC,
1114 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001115 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001116 thread->setEffectSuspended(FX_IID_NS,
1117 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001118 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001119 }
1120 }
Eric Laurentbee53372011-08-29 12:42:48 -07001121 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001122 }
1123 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001124 String8 screenState;
1125 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1126 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001127 if (isOff != (AudioFlinger::mScreenState & 1)) {
1128 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001129 }
1130 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001131 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001132 }
1133
1134 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1135 // and the thread is exited once the lock is released
1136 sp<ThreadBase> thread;
1137 {
1138 Mutex::Autolock _l(mLock);
1139 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001140 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001142 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001143 // indicate output device change to all input threads for pre processing
1144 AudioParameter param = AudioParameter(keyValuePairs);
1145 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001146 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1147 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001148 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001149 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001150 }
1151 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001152 if (thread != 0) {
1153 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154 }
1155 return BAD_VALUE;
1156}
1157
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001158String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001159{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001160 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1161 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162
Eric Laurenta4c5a552012-03-29 10:12:40 -07001163 Mutex::Autolock _l(mLock);
1164
Glenn Kasten142f5192014-03-25 17:44:59 -07001165 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001166 String8 out_s8;
1167
Dima Zavin799a70e2011-04-18 16:57:27 -07001168 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001169 char *s;
1170 {
1171 AutoMutex lock(mHardwareLock);
1172 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001173 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001174 s = dev->get_parameters(dev, keys.string());
1175 mHardwareStatus = AUDIO_HW_IDLE;
1176 }
John Grossmanef7740b2012-02-09 11:28:36 -08001177 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001178 free(s);
1179 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001180 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001181 }
1182
Mathias Agopian65ab4712010-07-14 17:59:35 -07001183 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1184 if (playbackThread != NULL) {
1185 return playbackThread->getParameters(keys);
1186 }
1187 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1188 if (recordThread != NULL) {
1189 return recordThread->getParameters(keys);
1190 }
1191 return String8("");
1192}
1193
Glenn Kastendd8104c2012-07-02 12:42:44 -07001194size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1195 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001196{
Eric Laurenta1884f92011-08-23 08:25:03 -07001197 status_t ret = initCheck();
1198 if (ret != NO_ERROR) {
1199 return 0;
1200 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001201 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001202 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001203 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001204 return 0;
1205 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001206
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001207 AutoMutex lock(mHardwareLock);
1208 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001209 audio_config_t config, proposed;
1210 memset(&proposed, 0, sizeof(proposed));
1211 proposed.sample_rate = sampleRate;
1212 proposed.channel_mask = channelMask;
1213 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001214
John Grossmanee578c02012-07-23 17:05:46 -07001215 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001216 size_t frames;
1217 for (;;) {
1218 // Note: config is currently a const parameter for get_input_buffer_size()
1219 // but we use a copy from proposed in case config changes from the call.
1220 config = proposed;
1221 frames = dev->get_input_buffer_size(dev, &config);
1222 if (frames != 0) {
1223 break; // hal success, config is the result
1224 }
1225 // change one parameter of the configuration each iteration to a more "common" value
1226 // to see if the device will support it.
1227 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1228 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1229 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1230 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1231 } else {
1232 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1233 "format %#x, channelMask 0x%X",
1234 sampleRate, format, channelMask);
1235 break; // retries failed, break out of loop with frames == 0.
1236 }
1237 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001238 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001239 if (frames > 0 && config.sample_rate != sampleRate) {
1240 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1241 }
1242 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001243}
1244
Glenn Kasten5f972c02014-01-13 09:59:31 -08001245uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001246{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001247 Mutex::Autolock _l(mLock);
1248
1249 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1250 if (recordThread != NULL) {
1251 return recordThread->getInputFramesLost();
1252 }
1253 return 0;
1254}
1255
1256status_t AudioFlinger::setVoiceVolume(float value)
1257{
Eric Laurenta1884f92011-08-23 08:25:03 -07001258 status_t ret = initCheck();
1259 if (ret != NO_ERROR) {
1260 return ret;
1261 }
1262
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263 // check calling permissions
1264 if (!settingsAllowed()) {
1265 return PERMISSION_DENIED;
1266 }
1267
1268 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001269 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001270 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001271 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272 mHardwareStatus = AUDIO_HW_IDLE;
1273
1274 return ret;
1275}
1276
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001277status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001278 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001279{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001280 Mutex::Autolock _l(mLock);
1281
1282 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1283 if (playbackThread != NULL) {
1284 return playbackThread->getRenderPosition(halFrames, dspFrames);
1285 }
1286
1287 return BAD_VALUE;
1288}
1289
1290void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1291{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001292 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001293 if (client == 0) {
1294 return;
1295 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001296 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001297 {
1298 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001299 if (mNotificationClients.indexOfKey(pid) < 0) {
1300 sp<NotificationClient> notificationClient = new NotificationClient(this,
1301 client,
1302 pid);
1303 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001304
Eric Laurent021cf962014-05-13 10:18:14 -07001305 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001306
Marco Nelissen06b46062014-11-14 07:58:25 -08001307 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001308 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001309 }
1310 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001311
Eric Laurent021cf962014-05-13 10:18:14 -07001312 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001313 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001314 // the config change is always sent from playback or record threads to avoid deadlock
1315 // with AudioSystem::gLock
1316 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1317 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1318 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001319
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001320 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1321 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001322 }
1323}
1324
1325void AudioFlinger::removeNotificationClient(pid_t pid)
1326{
1327 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001328 {
1329 Mutex::Autolock _cl(mClientLock);
1330 mNotificationClients.removeItem(pid);
1331 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001332
Steve Block3856b092011-10-20 11:56:00 +01001333 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001334 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001335 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001336 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001337 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001338 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001339 if (ref->mPid == pid) {
1340 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001341 mAudioSessionRefs.removeAt(i);
1342 delete ref;
1343 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001344 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001345 } else {
1346 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001347 }
1348 }
1349 if (removed) {
1350 purgeStaleEffects_l();
1351 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001352}
1353
Eric Laurent73e26b62015-04-27 16:55:58 -07001354void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001355 const sp<AudioIoDescriptor>& ioDesc,
1356 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001357{
Eric Laurent021cf962014-05-13 10:18:14 -07001358 Mutex::Autolock _l(mClientLock);
1359 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001361 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1362 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1363 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001364 }
1365}
1366
Eric Laurent021cf962014-05-13 10:18:14 -07001367// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001368void AudioFlinger::removeClient_l(pid_t pid)
1369{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001370 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001371 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372 mClients.removeItem(pid);
1373}
1374
Eric Laurent717e1282012-06-29 16:36:52 -07001375// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001376sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1377 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001378{
1379 sp<PlaybackThread> thread;
1380
1381 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1382 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1383 ALOG_ASSERT(thread == 0);
1384 thread = mPlaybackThreads.valueAt(i);
1385 }
1386 }
1387
1388 return thread;
1389}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001390
Mathias Agopian65ab4712010-07-14 17:59:35 -07001391
Mathias Agopian65ab4712010-07-14 17:59:35 -07001392
1393// ----------------------------------------------------------------------------
1394
1395AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1396 : RefBase(),
1397 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001398 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399{
Eric Laurentda73b6c2015-08-20 16:18:53 -07001400 size_t heapSize = kClientSharedHeapSizeBytes;
1401 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1402 // invalidated tracks
1403 if (!audioFlinger->isLowRamDevice()) {
1404 heapSize *= kClientSharedHeapSizeMultiplier;
1405 }
1406 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407}
1408
Eric Laurent021cf962014-05-13 10:18:14 -07001409// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001410AudioFlinger::Client::~Client()
1411{
1412 mAudioFlinger->removeClient_l(mPid);
1413}
1414
Glenn Kasten435dbe62012-01-30 10:15:48 -08001415sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001416{
1417 return mMemoryDealer;
1418}
1419
1420// ----------------------------------------------------------------------------
1421
1422AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1423 const sp<IAudioFlingerClient>& client,
1424 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001425 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001426{
1427}
1428
1429AudioFlinger::NotificationClient::~NotificationClient()
1430{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001431}
1432
Glenn Kasten0f11b512014-01-31 16:18:54 -08001433void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001434{
1435 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001436 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437}
1438
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439
1440// ----------------------------------------------------------------------------
1441
1442sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001443 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001445 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001446 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001447 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001448 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001449 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001450 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001451 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001452 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001453 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001454 sp<IMemory>& cblk,
1455 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456 status_t *status)
1457{
1458 sp<RecordThread::RecordTrack> recordTrack;
1459 sp<RecordHandle> recordHandle;
1460 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001461 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001462 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001463
Glenn Kastend776ac62014-05-07 09:16:09 -07001464 cblk.clear();
1465 buffers.clear();
1466
Marco Nelissendcb346b2015-09-09 10:47:29 -07001467 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1468 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001469 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001470 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1471 clientUid = callingUid;
1472 }
1473
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001475 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001476 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001477 lStatus = PERMISSION_DENIED;
1478 goto Exit;
1479 }
1480
Glenn Kasten53b5d092014-02-05 10:00:23 -08001481 // further sample rate checks are performed by createRecordTrack_l()
1482 if (sampleRate == 0) {
1483 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1484 lStatus = BAD_VALUE;
1485 goto Exit;
1486 }
1487
Andy Hung6770c6f2015-04-07 13:43:36 -07001488 // we don't yet support anything other than linear PCM
1489 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001490 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001491 lStatus = BAD_VALUE;
1492 goto Exit;
1493 }
1494
Glenn Kasten53b5d092014-02-05 10:00:23 -08001495 // further channel mask checks are performed by createRecordTrack_l()
1496 if (!audio_is_input_channel(channelMask)) {
1497 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1498 lStatus = BAD_VALUE;
1499 goto Exit;
1500 }
1501
Glenn Kasten05997e22014-03-13 15:08:33 -07001502 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001503 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001504 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001505 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001506 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001507 lStatus = BAD_VALUE;
1508 goto Exit;
1509 }
1510
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001511 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001512 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001513
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001514 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001515 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1516 lStatus = BAD_VALUE;
1517 goto Exit;
1518 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519 lSessionId = *sessionId;
1520 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001521 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001522 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001523 if (sessionId != NULL) {
1524 *sessionId = lSessionId;
1525 }
1526 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001527 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001528
Glenn Kasten1879fff2012-07-11 15:36:59 -07001529 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001530 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001531 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001532 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001533
1534 if (lStatus == NO_ERROR) {
1535 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1536 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001537 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001538 if (chain != 0) {
1539 Mutex::Autolock _l(thread->mLock);
1540 thread->addEffectChain_l(chain);
1541 }
1542 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543 }
Glenn Kastene198c362013-08-13 09:13:36 -07001544
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001545 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001546 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001547 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001548 // Don't hold mClientLock when releasing the reference on the track as the
1549 // destructor will acquire it.
1550 {
1551 Mutex::Autolock _cl(mClientLock);
1552 client.clear();
1553 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001554 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001555 goto Exit;
1556 }
1557
Glenn Kastend776ac62014-05-07 09:16:09 -07001558 cblk = recordTrack->getCblk();
1559 buffers = recordTrack->getBuffers();
1560
Glenn Kasten2fc14732013-08-05 14:58:14 -07001561 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563
1564Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001565 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001566 return recordHandle;
1567}
1568
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001569
1570
Mathias Agopian65ab4712010-07-14 17:59:35 -07001571// ----------------------------------------------------------------------------
1572
Eric Laurenta4c5a552012-03-29 10:12:40 -07001573audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1574{
Eric Laurent44622db2014-08-01 19:00:33 -07001575 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001576 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001577 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001578 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001579 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001580 }
1581 Mutex::Autolock _l(mLock);
1582 return loadHwModule_l(name);
1583}
1584
1585// loadHwModule_l() must be called with AudioFlinger::mLock held
1586audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1587{
1588 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1589 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1590 ALOGW("loadHwModule() module %s already loaded", name);
1591 return mAudioHwDevs.keyAt(i);
1592 }
1593 }
1594
Eric Laurenta4c5a552012-03-29 10:12:40 -07001595 audio_hw_device_t *dev;
1596
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001597 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001598 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001599 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001600 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001601 }
1602
1603 mHardwareStatus = AUDIO_HW_INIT;
1604 rc = dev->init_check(dev);
1605 mHardwareStatus = AUDIO_HW_IDLE;
1606 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001607 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001608 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001609 }
1610
John Grossmanee578c02012-07-23 17:05:46 -07001611 // Check and cache this HAL's level of support for master mute and master
1612 // volume. If this is the first HAL opened, and it supports the get
1613 // methods, use the initial values provided by the HAL as the current
1614 // master mute and volume settings.
1615
1616 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1617 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001618 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001619
1620 if (0 == mAudioHwDevs.size()) {
1621 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1622 if (NULL != dev->get_master_volume) {
1623 float mv;
1624 if (OK == dev->get_master_volume(dev, &mv)) {
1625 mMasterVolume = mv;
1626 }
1627 }
1628
1629 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1630 if (NULL != dev->get_master_mute) {
1631 bool mm;
1632 if (OK == dev->get_master_mute(dev, &mm)) {
1633 mMasterMute = mm;
1634 }
1635 }
1636 }
1637
Eric Laurenta4c5a552012-03-29 10:12:40 -07001638 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001639 if ((NULL != dev->set_master_volume) &&
1640 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1641 flags = static_cast<AudioHwDevice::Flags>(flags |
1642 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1643 }
1644
1645 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1646 if ((NULL != dev->set_master_mute) &&
1647 (OK == dev->set_master_mute(dev, mMasterMute))) {
1648 flags = static_cast<AudioHwDevice::Flags>(flags |
1649 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1650 }
1651
Eric Laurenta4c5a552012-03-29 10:12:40 -07001652 mHardwareStatus = AUDIO_HW_IDLE;
1653 }
1654
Glenn Kastena13cde92016-03-28 15:26:02 -07001655 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001656 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001657
1658 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001659 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001660
1661 return handle;
1662
1663}
1664
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001665// ----------------------------------------------------------------------------
1666
Glenn Kasten3b16c762012-11-14 08:44:39 -08001667uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001668{
1669 Mutex::Autolock _l(mLock);
1670 PlaybackThread *thread = primaryPlaybackThread_l();
1671 return thread != NULL ? thread->sampleRate() : 0;
1672}
1673
Glenn Kastene33054e2012-11-14 12:54:39 -08001674size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001675{
1676 Mutex::Autolock _l(mLock);
1677 PlaybackThread *thread = primaryPlaybackThread_l();
1678 return thread != NULL ? thread->frameCountHAL() : 0;
1679}
1680
1681// ----------------------------------------------------------------------------
1682
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001683status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1684{
1685 uid_t uid = IPCThreadState::self()->getCallingUid();
1686 if (uid != AID_SYSTEM) {
1687 return PERMISSION_DENIED;
1688 }
1689 Mutex::Autolock _l(mLock);
1690 if (mIsDeviceTypeKnown) {
1691 return INVALID_OPERATION;
1692 }
1693 mIsLowRamDevice = isLowRamDevice;
1694 mIsDeviceTypeKnown = true;
1695 return NO_ERROR;
1696}
1697
Eric Laurent93c3d412014-08-01 14:48:35 -07001698audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1699{
1700 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001701
1702 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1703 if (index >= 0) {
1704 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1705 mHwAvSyncIds.valueAt(index), sessionId);
1706 return mHwAvSyncIds.valueAt(index);
1707 }
1708
1709 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1710 if (dev == NULL) {
1711 return AUDIO_HW_SYNC_INVALID;
1712 }
1713 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1714 AudioParameter param = AudioParameter(String8(reply));
1715 free(reply);
1716
1717 int value;
1718 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1719 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1720 return AUDIO_HW_SYNC_INVALID;
1721 }
1722
1723 // allow only one session for a given HW A/V sync ID.
1724 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1725 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1726 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1727 value, mHwAvSyncIds.keyAt(i));
1728 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001729 break;
1730 }
1731 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001732
1733 mHwAvSyncIds.add(sessionId, value);
1734
1735 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1736 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1737 uint32_t sessions = thread->hasAudioSession(sessionId);
1738 if (sessions & PlaybackThread::TRACK_SESSION) {
1739 AudioParameter param = AudioParameter();
1740 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1741 thread->setParameters(param.toString());
1742 break;
1743 }
1744 }
1745
1746 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1747 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001748}
1749
Eric Laurent72e3f392015-05-20 14:43:50 -07001750status_t AudioFlinger::systemReady()
1751{
1752 Mutex::Autolock _l(mLock);
1753 ALOGI("%s", __FUNCTION__);
1754 if (mSystemReady) {
1755 ALOGW("%s called twice", __FUNCTION__);
1756 return NO_ERROR;
1757 }
1758 mSystemReady = true;
1759 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1760 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1761 thread->systemReady();
1762 }
1763 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1764 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1765 thread->systemReady();
1766 }
1767 return NO_ERROR;
1768}
1769
Eric Laurentfa90e842014-10-17 18:12:31 -07001770// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1771void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1772{
1773 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1774 if (index >= 0) {
1775 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1776 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1777 AudioParameter param = AudioParameter();
1778 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1779 thread->setParameters(param.toString());
1780 }
1781}
1782
1783
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001784// ----------------------------------------------------------------------------
1785
Eric Laurent83b88082014-06-20 18:31:16 -07001786
1787sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001788 audio_io_handle_t *output,
1789 audio_config_t *config,
1790 audio_devices_t devices,
1791 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001792 audio_output_flags_t flags)
1793{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001794 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001795 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001796 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001797 }
1798
Eric Laurentcf2c0212014-07-25 16:20:43 -07001799 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001800 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1801 } else {
1802 // Audio Policy does not currently request a specific output handle.
1803 // If this is ever needed, see openInput_l() for example code.
1804 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1805 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001806 }
Eric Laurent83b88082014-06-20 18:31:16 -07001807
1808 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1809
Eric Laurent83b88082014-06-20 18:31:16 -07001810 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001811 // This if statement allows overriding the audio policy settings
1812 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1813 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1814 // Check only for Normal Mixing mode
1815 if (kEnableExtendedPrecision) {
1816 // Specify format (uncomment one below to choose)
1817 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1818 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1819 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1820 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001821 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001822 }
1823 if (kEnableExtendedChannels) {
1824 // Specify channel mask (uncomment one below to choose)
1825 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1826 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1827 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1828 }
Eric Laurent83b88082014-06-20 18:31:16 -07001829 }
1830
Phil Burk062e67a2015-02-11 13:40:50 -08001831 AudioStreamOut *outputStream = NULL;
1832 status_t status = outHwDev->openOutputStream(
1833 &outputStream,
1834 *output,
1835 devices,
1836 flags,
1837 config,
1838 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001839
1840 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001841
Phil Burk062e67a2015-02-11 13:40:50 -08001842 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001843
1844 PlaybackThread *thread;
1845 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurente93cc032016-05-05 10:15:10 -07001846 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001847 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001848 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1849 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001850 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001851 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001852 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001853 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001854 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001855 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001856 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001857 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001858 return thread;
1859 }
1860
1861 return 0;
1862}
1863
Eric Laurentcf2c0212014-07-25 16:20:43 -07001864status_t AudioFlinger::openOutput(audio_module_handle_t module,
1865 audio_io_handle_t *output,
1866 audio_config_t *config,
1867 audio_devices_t *devices,
1868 const String8& address,
1869 uint32_t *latencyMs,
1870 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001871{
Phil Burk062e67a2015-02-11 13:40:50 -08001872 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001873 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001874 (devices != NULL) ? *devices : 0,
1875 config->sample_rate,
1876 config->format,
1877 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001878 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001879
Eric Laurentcf2c0212014-07-25 16:20:43 -07001880 if (*devices == AUDIO_DEVICE_NONE) {
1881 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001882 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001883
Mathias Agopian65ab4712010-07-14 17:59:35 -07001884 Mutex::Autolock _l(mLock);
1885
Eric Laurentcf2c0212014-07-25 16:20:43 -07001886 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001887 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001888 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001889
1890 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001891 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001892
1893 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001894 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001895 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001896 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001897
1898 AutoMutex lock(mHardwareLock);
1899 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001900 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001901 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001902 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001903 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001904 }
1905
Eric Laurentcf2c0212014-07-25 16:20:43 -07001906 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001907}
1908
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001909audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1910 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001911{
1912 Mutex::Autolock _l(mLock);
1913 MixerThread *thread1 = checkMixerThread_l(output1);
1914 MixerThread *thread2 = checkMixerThread_l(output2);
1915
1916 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001917 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1918 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001919 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001920 }
1921
Glenn Kasteneeecb982016-02-26 10:44:04 -08001922 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07001923 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001924 thread->addOutputTrack(thread2);
1925 mPlaybackThreads.add(id, thread);
1926 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001927 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928 return id;
1929}
1930
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001931status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001932{
Glenn Kastend96c5722012-04-25 13:44:49 -07001933 return closeOutput_nonvirtual(output);
1934}
1935
1936status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1937{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001938 // keep strong reference on the playback thread so that
1939 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001940 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941 {
1942 Mutex::Autolock _l(mLock);
1943 thread = checkPlaybackThread_l(output);
1944 if (thread == NULL) {
1945 return BAD_VALUE;
1946 }
1947
Steve Block3856b092011-10-20 11:56:00 +01001948 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001949
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001950 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001951 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001952 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001953 DuplicatingThread *dupThread =
1954 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001956 }
1957 }
1958 }
1959
1960
1961 mPlaybackThreads.removeItem(output);
1962 // save all effects to the default thread
1963 if (mPlaybackThreads.size()) {
1964 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1965 if (dstThread != NULL) {
1966 // audioflinger lock is held here so the acquisition order of thread locks does not
1967 // matter
1968 Mutex::Autolock _dl(dstThread->mLock);
1969 Mutex::Autolock _sl(thread->mLock);
1970 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1971 for (size_t i = 0; i < effectChains.size(); i ++) {
1972 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001973 }
1974 }
1975 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001976 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1977 ioDesc->mIoHandle = output;
1978 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979 }
1980 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001981 // The thread entity (active unit of execution) is no longer running here,
1982 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001983
Eric Laurentf6870ae2015-05-08 10:50:03 -07001984 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001985 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001986 }
Eric Laurent83b88082014-06-20 18:31:16 -07001987
Mathias Agopian65ab4712010-07-14 17:59:35 -07001988 return NO_ERROR;
1989}
1990
Eric Laurent83b88082014-06-20 18:31:16 -07001991void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1992{
1993 AudioStreamOut *out = thread->clearOutput();
1994 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1995 // from now on thread->mOutput is NULL
1996 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1997 delete out;
1998}
1999
2000void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
2001{
2002 mPlaybackThreads.removeItem(thread->mId);
2003 thread->exit();
2004 closeOutputFinish(thread);
2005}
2006
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002007status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002008{
2009 Mutex::Autolock _l(mLock);
2010 PlaybackThread *thread = checkPlaybackThread_l(output);
2011
2012 if (thread == NULL) {
2013 return BAD_VALUE;
2014 }
2015
Steve Block3856b092011-10-20 11:56:00 +01002016 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002017 thread->suspend();
2018
2019 return NO_ERROR;
2020}
2021
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002022status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002023{
2024 Mutex::Autolock _l(mLock);
2025 PlaybackThread *thread = checkPlaybackThread_l(output);
2026
2027 if (thread == NULL) {
2028 return BAD_VALUE;
2029 }
2030
Steve Block3856b092011-10-20 11:56:00 +01002031 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002032
2033 thread->restore();
2034
2035 return NO_ERROR;
2036}
2037
Eric Laurentcf2c0212014-07-25 16:20:43 -07002038status_t AudioFlinger::openInput(audio_module_handle_t module,
2039 audio_io_handle_t *input,
2040 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002041 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002042 const String8& address,
2043 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002044 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002045{
Eric Laurent83b88082014-06-20 18:31:16 -07002046 Mutex::Autolock _l(mLock);
2047
Glenn Kastene7d66712015-03-05 13:46:52 -08002048 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002049 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002050 }
2051
Glenn Kastene7d66712015-03-05 13:46:52 -08002052 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002053
2054 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002055 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002056 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002057 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002058 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002059 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002060}
Dima Zavin799a70e2011-04-18 16:57:27 -07002061
Eric Laurent83b88082014-06-20 18:31:16 -07002062sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002063 audio_io_handle_t *input,
2064 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002065 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002066 const String8& address,
2067 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002068 audio_input_flags_t flags)
2069{
Glenn Kastene7d66712015-03-05 13:46:52 -08002070 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002071 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002072 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002073 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002074 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002075
Glenn Kasteneeecb982016-02-26 10:44:04 -08002076 // Audio Policy can request a specific handle for hardware hotword.
2077 // The goal here is not to re-open an already opened input.
2078 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002079 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002080 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2081 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2082 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2083 return 0;
2084 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2085 // This should not happen in a transient state with current design.
2086 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2087 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002088 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002089
Eric Laurentcf2c0212014-07-25 16:20:43 -07002090 audio_config_t halconfig = *config;
2091 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002092 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002093 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002094 &inStream, flags, address.string(), source);
2095 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002096 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002097 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002098 halconfig.sample_rate,
2099 halconfig.format,
2100 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002101 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002102 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002103
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002104 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002105 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002106 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002107 audio_is_linear_pcm(config->format) &&
2108 audio_is_linear_pcm(halconfig.format) &&
2109 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002110 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2111 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002112 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002113 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002114 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002115 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002116 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002117 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002118 }
2119
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002120 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002121
Glenn Kasten46909e72013-02-26 09:20:22 -08002122#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002123 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2124 // or (re-)create if current Pipe is idle and does not match the new format
2125 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002126 enum {
2127 TEE_SINK_NO, // don't copy input
2128 TEE_SINK_NEW, // copy input using a new pipe
2129 TEE_SINK_OLD, // copy input using an existing pipe
2130 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002131 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2132 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002133 if (!mTeeSinkInputEnabled) {
2134 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002135 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002136 kind = TEE_SINK_NO;
2137 } else if (mRecordTeeSink == 0) {
2138 kind = TEE_SINK_NEW;
2139 } else if (mRecordTeeSink->getStrongCount() != 1) {
2140 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002141 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002142 kind = TEE_SINK_OLD;
2143 } else {
2144 kind = TEE_SINK_NEW;
2145 }
2146 switch (kind) {
2147 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002148 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002149 size_t numCounterOffers = 0;
2150 const NBAIO_Format offers[1] = {format};
2151 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2152 ALOG_ASSERT(index == 0);
2153 PipeReader *pipeReader = new PipeReader(*pipe);
2154 numCounterOffers = 0;
2155 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2156 ALOG_ASSERT(index == 0);
2157 mRecordTeeSink = pipe;
2158 mRecordTeeSource = pipeReader;
2159 teeSink = pipe;
2160 }
2161 break;
2162 case TEE_SINK_OLD:
2163 teeSink = mRecordTeeSink;
2164 break;
2165 case TEE_SINK_NO:
2166 default:
2167 break;
2168 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002169#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002170
Eric Laurentcf2c0212014-07-25 16:20:43 -07002171 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002172
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002173 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002174 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002175 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002176 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002177 inputStream,
2178 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002179 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002180 devices,
2181 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002182#ifdef TEE_SINK
2183 , teeSink
2184#endif
2185 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002186 mRecordThreads.add(*input, thread);
2187 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002188 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002189 }
2190
Eric Laurentcf2c0212014-07-25 16:20:43 -07002191 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192 return 0;
2193}
2194
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002195status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002196{
Glenn Kastend96c5722012-04-25 13:44:49 -07002197 return closeInput_nonvirtual(input);
2198}
2199
2200status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2201{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002202 // keep strong reference on the record thread so that
2203 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002204 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002205 {
2206 Mutex::Autolock _l(mLock);
2207 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002208 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002209 return BAD_VALUE;
2210 }
2211
Steve Block3856b092011-10-20 11:56:00 +01002212 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002213
2214 // If we still have effect chains, it means that a client still holds a handle
2215 // on at least one effect. We must either move the chain to an existing thread with the
2216 // same session ID or put it aside in case a new record thread is opened for a
2217 // new capture on the same session
2218 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002219 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002220 Mutex::Autolock _sl(thread->mLock);
2221 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002222 // Note: maximum one chain per record thread
2223 if (effectChains.size() != 0) {
2224 chain = effectChains[0];
2225 }
2226 }
2227 if (chain != 0) {
2228 // first check if a record thread is already opened with a client on the same session.
2229 // This should only happen in case of overlap between one thread tear down and the
2230 // creation of its replacement
2231 size_t i;
2232 for (i = 0; i < mRecordThreads.size(); i++) {
2233 sp<RecordThread> t = mRecordThreads.valueAt(i);
2234 if (t == thread) {
2235 continue;
2236 }
2237 if (t->hasAudioSession(chain->sessionId()) != 0) {
2238 Mutex::Autolock _l(t->mLock);
2239 ALOGV("closeInput() found thread %d for effect session %d",
2240 t->id(), chain->sessionId());
2241 t->addEffectChain_l(chain);
2242 break;
2243 }
2244 }
2245 // put the chain aside if we could not find a record thread with the same session id.
2246 if (i == mRecordThreads.size()) {
2247 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002248 }
2249 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002250 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2251 ioDesc->mIoHandle = input;
2252 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002253 mRecordThreads.removeItem(input);
2254 }
Eric Laurent83b88082014-06-20 18:31:16 -07002255 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2256 // we have a different lock for notification client
2257 closeInputFinish(thread);
2258 return NO_ERROR;
2259}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002260
Eric Laurent83b88082014-06-20 18:31:16 -07002261void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2262{
2263 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002264 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002265 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002266 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002267 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002268 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002269}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002270
Eric Laurent83b88082014-06-20 18:31:16 -07002271void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2272{
2273 mRecordThreads.removeItem(thread->mId);
2274 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002275}
2276
Glenn Kastend2304db2014-02-03 07:40:31 -08002277status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002278{
2279 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002280 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002281
2282 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2283 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002284 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002285 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002286
2287 return NO_ERROR;
2288}
2289
2290
Glenn Kasteneeecb982016-02-26 10:44:04 -08002291audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002292{
Glenn Kasten9d003132016-04-06 14:38:09 -07002293 // This is a binder API, so a malicious client could pass in a bad parameter.
2294 // Check for that before calling the internal API nextUniqueId().
2295 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2296 ALOGE("newAudioUniqueId invalid use %d", use);
2297 return AUDIO_UNIQUE_ID_ALLOCATE;
2298 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002299 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002300}
2301
Glenn Kastend848eb42016-03-08 13:42:11 -08002302void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002303{
2304 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002305 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002306 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2307 if (pid != -1 && (caller == getpid_cached)) {
2308 caller = pid;
2309 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002310
Eric Laurent021cf962014-05-13 10:18:14 -07002311 {
2312 Mutex::Autolock _cl(mClientLock);
2313 // Ignore requests received from processes not known as notification client. The request
2314 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2315 // called from a different pid leaving a stale session reference. Also we don't know how
2316 // to clear this reference if the client process dies.
2317 if (mNotificationClients.indexOfKey(caller) < 0) {
2318 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2319 return;
2320 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002321 }
2322
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002323 size_t num = mAudioSessionRefs.size();
2324 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002325 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002326 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2327 ref->mCnt++;
2328 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002329 return;
2330 }
2331 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002332 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2333 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002334}
2335
Glenn Kastend848eb42016-03-08 13:42:11 -08002336void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002337{
2338 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002339 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002340 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2341 if (pid != -1 && (caller == getpid_cached)) {
2342 caller = pid;
2343 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002344 size_t num = mAudioSessionRefs.size();
2345 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002346 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002347 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2348 ref->mCnt--;
2349 ALOGV(" decremented refcount to %d", ref->mCnt);
2350 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002351 mAudioSessionRefs.removeAt(i);
2352 delete ref;
2353 purgeStaleEffects_l();
2354 }
2355 return;
2356 }
2357 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002358 // If the caller is mediaserver it is likely that the session being released was acquired
2359 // on behalf of a process not in notification clients and we ignore the warning.
2360 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002361}
2362
2363void AudioFlinger::purgeStaleEffects_l() {
2364
Steve Block3856b092011-10-20 11:56:00 +01002365 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002366
2367 Vector< sp<EffectChain> > chains;
2368
2369 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2370 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2371 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2372 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002373 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2374 chains.push(ec);
2375 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002376 }
2377 }
2378 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2379 sp<RecordThread> t = mRecordThreads.valueAt(i);
2380 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2381 sp<EffectChain> ec = t->mEffectChains[j];
2382 chains.push(ec);
2383 }
2384 }
2385
2386 for (size_t i = 0; i < chains.size(); i++) {
2387 sp<EffectChain> ec = chains[i];
2388 int sessionid = ec->sessionId();
2389 sp<ThreadBase> t = ec->mThread.promote();
2390 if (t == 0) {
2391 continue;
2392 }
2393 size_t numsessionrefs = mAudioSessionRefs.size();
2394 bool found = false;
2395 for (size_t k = 0; k < numsessionrefs; k++) {
2396 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002397 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002398 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002399 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002400 found = true;
2401 break;
2402 }
2403 }
2404 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002405 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002406 // remove all effects from the chain
2407 while (ec->mEffects.size()) {
2408 sp<EffectModule> effect = ec->mEffects[0];
2409 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002410 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002411 if (effect->purgeHandles()) {
2412 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002413 }
2414 AudioSystem::unregisterEffect(effect->id());
2415 }
2416 }
2417 }
2418 return;
2419}
2420
Glenn Kasteneeecb982016-02-26 10:44:04 -08002421// checkThread_l() must be called with AudioFlinger::mLock held
2422AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2423{
2424 ThreadBase *thread = NULL;
2425 switch (audio_unique_id_get_use(ioHandle)) {
2426 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2427 thread = checkPlaybackThread_l(ioHandle);
2428 break;
2429 case AUDIO_UNIQUE_ID_USE_INPUT:
2430 thread = checkRecordThread_l(ioHandle);
2431 break;
2432 default:
2433 break;
2434 }
2435 return thread;
2436}
2437
Mathias Agopian65ab4712010-07-14 17:59:35 -07002438// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002439AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002440{
Glenn Kastena1117922012-01-26 10:53:32 -08002441 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002442}
2443
2444// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002445AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002446{
2447 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002448 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002449}
2450
2451// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002452AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002453{
Glenn Kastena1117922012-01-26 10:53:32 -08002454 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002455}
2456
Glenn Kasteneeecb982016-02-26 10:44:04 -08002457audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002458{
Glenn Kasten9d003132016-04-06 14:38:09 -07002459 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002460 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002461 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2462 for (int retry = 0; retry < maxRetries; retry++) {
2463 // The cast allows wraparound from max positive to min negative instead of abort
2464 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2465 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2466 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2467 // allow wrap by skipping 0 and -1 for session ids
2468 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2469 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2470 return (audio_unique_id_t) (base | use);
2471 }
2472 }
2473 // We have no way of recovering from wraparound
2474 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2475 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002476}
2477
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002478AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002479{
2480 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2481 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002482 if(thread->isDuplicating()) {
2483 continue;
2484 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002485 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002486 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002487 return thread;
2488 }
2489 }
2490 return NULL;
2491}
2492
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002493audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002494{
2495 PlaybackThread *thread = primaryPlaybackThread_l();
2496
2497 if (thread == NULL) {
2498 return 0;
2499 }
2500
Eric Laurentf1c04f92012-08-28 14:26:53 -07002501 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002502}
2503
Eric Laurenta011e352012-03-29 15:51:43 -07002504sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002505 audio_session_t triggerSession,
2506 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002507 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002508 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002509{
2510 Mutex::Autolock _l(mLock);
2511
2512 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2513 status_t playStatus = NAME_NOT_FOUND;
2514 status_t recStatus = NAME_NOT_FOUND;
2515 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2516 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2517 if (playStatus == NO_ERROR) {
2518 return event;
2519 }
2520 }
2521 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2522 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2523 if (recStatus == NO_ERROR) {
2524 return event;
2525 }
2526 }
2527 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2528 mPendingSyncEvents.add(event);
2529 } else {
2530 ALOGV("createSyncEvent() invalid event %d", event->type());
2531 event.clear();
2532 }
2533 return event;
2534}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002535
Mathias Agopian65ab4712010-07-14 17:59:35 -07002536// ----------------------------------------------------------------------------
2537// Effect management
2538// ----------------------------------------------------------------------------
2539
2540
Glenn Kastenf587ba52012-01-26 16:25:10 -08002541status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002542{
2543 Mutex::Autolock _l(mLock);
2544 return EffectQueryNumberEffects(numEffects);
2545}
2546
Glenn Kastenf587ba52012-01-26 16:25:10 -08002547status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002548{
2549 Mutex::Autolock _l(mLock);
2550 return EffectQueryEffect(index, descriptor);
2551}
2552
Glenn Kasten5e92a782012-01-30 07:40:52 -08002553status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002554 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002555{
2556 Mutex::Autolock _l(mLock);
2557 return EffectGetDescriptor(pUuid, descriptor);
2558}
2559
2560
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002561sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002562 effect_descriptor_t *pDesc,
2563 const sp<IEffectClient>& effectClient,
2564 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002565 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002566 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002567 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002568 status_t *status,
2569 int *id,
2570 int *enabled)
2571{
2572 status_t lStatus = NO_ERROR;
2573 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002574 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002575
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002576 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002577 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002578 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002579
2580 if (pDesc == NULL) {
2581 lStatus = BAD_VALUE;
2582 goto Exit;
2583 }
2584
Eric Laurent84e9a102010-09-23 16:10:16 -07002585 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002586 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002587 lStatus = PERMISSION_DENIED;
2588 goto Exit;
2589 }
2590
Dima Zavinfce7a472011-04-19 22:30:36 -07002591 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002592 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002593 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002594 lStatus = PERMISSION_DENIED;
2595 goto Exit;
2596 }
2597
Mathias Agopian65ab4712010-07-14 17:59:35 -07002598 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002599 if (!EffectIsNullUuid(&pDesc->uuid)) {
2600 // if uuid is specified, request effect descriptor
2601 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2602 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002603 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002604 goto Exit;
2605 }
2606 } else {
2607 // if uuid is not specified, look for an available implementation
2608 // of the required type in effect factory
2609 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002610 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002611 lStatus = BAD_VALUE;
2612 goto Exit;
2613 }
2614 uint32_t numEffects = 0;
2615 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002616 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002617 bool found = false;
2618
2619 lStatus = EffectQueryNumberEffects(&numEffects);
2620 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002621 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002622 goto Exit;
2623 }
2624 for (uint32_t i = 0; i < numEffects; i++) {
2625 lStatus = EffectQueryEffect(i, &desc);
2626 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002627 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002628 continue;
2629 }
2630 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2631 // If matching type found save effect descriptor. If the session is
2632 // 0 and the effect is not auxiliary, continue enumeration in case
2633 // an auxiliary version of this effect type is available
2634 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002635 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002636 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002637 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2638 break;
2639 }
2640 }
2641 }
2642 if (!found) {
2643 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002644 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002645 goto Exit;
2646 }
2647 // For same effect type, chose auxiliary version over insert version if
2648 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002649 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002650 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002651 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002652 }
2653 }
2654
2655 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002656 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002657 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2658 lStatus = INVALID_OPERATION;
2659 goto Exit;
2660 }
2661
Eric Laurent59255e42011-07-27 19:49:51 -07002662 // check recording permission for visualizer
2663 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002664 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002665 lStatus = PERMISSION_DENIED;
2666 goto Exit;
2667 }
2668
Mathias Agopian65ab4712010-07-14 17:59:35 -07002669 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002670 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002671 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002672 // if the output returned by getOutputForEffect() is removed before we lock the
2673 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2674 // and we will exit safely
2675 io = AudioSystem::getOutputForEffect(&desc);
2676 ALOGV("createEffect got output %d", io);
2677 }
2678
2679 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002680
2681 // If output is not specified try to find a matching audio session ID in one of the
2682 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002683 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2684 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002685 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002686 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002687 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2688 // output must be specified by AudioPolicyManager when using session
2689 // AUDIO_SESSION_OUTPUT_STAGE
2690 lStatus = BAD_VALUE;
2691 goto Exit;
2692 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002693 // look for the thread where the specified audio session is present
2694 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2695 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2696 io = mPlaybackThreads.keyAt(i);
2697 break;
2698 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002699 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002700 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002701 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2702 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2703 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002704 break;
2705 }
2706 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002707 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002708 // If no output thread contains the requested session ID, default to
2709 // first output. The effect chain will be moved to the correct output
2710 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002711 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002712 io = mPlaybackThreads.keyAt(0);
2713 }
Steve Block3856b092011-10-20 11:56:00 +01002714 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002715 }
2716 ThreadBase *thread = checkRecordThread_l(io);
2717 if (thread == NULL) {
2718 thread = checkPlaybackThread_l(io);
2719 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002720 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002721 lStatus = BAD_VALUE;
2722 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002723 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002724 } else {
2725 // Check if one effect chain was awaiting for an effect to be created on this
2726 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08002727 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07002728 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002729 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002730 thread->addEffectChain_l(chain);
2731 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002732 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002733
Eric Laurent021cf962014-05-13 10:18:14 -07002734 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002735
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002736 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002737 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2738 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002739 if (handle != 0 && id != NULL) {
2740 *id = handle->id();
2741 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002742 if (handle == 0) {
2743 // remove local strong reference to Client with mClientLock held
2744 Mutex::Autolock _cl(mClientLock);
2745 client.clear();
2746 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002747 }
2748
2749Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002750 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002751 return handle;
2752}
2753
Glenn Kastend848eb42016-03-08 13:42:11 -08002754status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002755 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002756{
Steve Block3856b092011-10-20 11:56:00 +01002757 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002758 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002759 Mutex::Autolock _l(mLock);
2760 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002761 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002762 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002763 }
Eric Laurentde070132010-07-13 04:45:46 -07002764 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2765 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002766 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002767 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002768 }
Eric Laurentde070132010-07-13 04:45:46 -07002769 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2770 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002771 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002772 return BAD_VALUE;
2773 }
2774
2775 Mutex::Autolock _dl(dstThread->mLock);
2776 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002777 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002778}
2779
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002780// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08002781status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002782 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002783 AudioFlinger::PlaybackThread *dstThread,
2784 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002785{
Steve Block3856b092011-10-20 11:56:00 +01002786 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002787 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002788
Eric Laurent59255e42011-07-27 19:49:51 -07002789 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002790 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002791 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002792 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002793 return INVALID_OPERATION;
2794 }
2795
Andy Hung9a592762014-07-21 21:56:01 -07002796 // Check whether the destination thread has a channel count of FCC_2, which is
2797 // currently required for (most) effects. Prevent moving the effect chain here rather
2798 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentf6870ae2015-05-08 10:50:03 -07002799 if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
Eric Laurentb10352f2014-11-03 16:25:39 -08002800 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002801 ALOGW("moveEffectChain_l() effect chain failed because"
2802 " destination thread %p channel count(%u) != %u",
2803 dstThread, dstThread->mChannelCount, FCC_2);
2804 return INVALID_OPERATION;
2805 }
2806
Eric Laurent39e94f82010-07-28 01:32:47 -07002807 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002808 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002809 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002810 // removed.
2811 srcThread->removeEffectChain_l(chain);
2812
2813 // transfer all effects one by one so that new effect chain is created on new thread with
2814 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002815 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002816 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002817 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002818 Vector< sp<EffectModule> > removed;
2819 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002820 while (effect != 0) {
2821 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002822 removed.add(effect);
2823 status = dstThread->addEffect_l(effect);
2824 if (status != NO_ERROR) {
2825 break;
2826 }
Eric Laurentec35a142011-10-05 17:42:25 -07002827 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2828 if (effect->state() == EffectModule::ACTIVE ||
2829 effect->state() == EffectModule::STOPPING) {
2830 effect->start();
2831 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002832 // if the move request is not received from audio policy manager, the effect must be
2833 // re-registered with the new strategy and output
2834 if (dstChain == 0) {
2835 dstChain = effect->chain().promote();
2836 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002837 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002838 status = NO_INIT;
2839 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002840 }
2841 strategy = dstChain->strategy();
2842 }
2843 if (reRegister) {
2844 AudioSystem::unregisterEffect(effect->id());
2845 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002846 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002847 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002848 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002849 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002850 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002851 }
Eric Laurentde070132010-07-13 04:45:46 -07002852 effect = chain->getEffectFromId_l(0);
2853 }
2854
Eric Laurent5baf2af2013-09-12 17:37:00 -07002855 if (status != NO_ERROR) {
2856 for (size_t i = 0; i < removed.size(); i++) {
2857 srcThread->addEffect_l(removed[i]);
2858 if (dstChain != 0 && reRegister) {
2859 AudioSystem::unregisterEffect(removed[i]->id());
2860 AudioSystem::registerEffect(&removed[i]->desc(),
2861 srcThread->id(),
2862 strategy,
2863 sessionId,
2864 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002865 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002866 }
2867 }
2868 }
2869
2870 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002871}
2872
Eric Laurent5baf2af2013-09-12 17:37:00 -07002873bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002874{
2875 if (mGlobalEffectEnableTime != 0 &&
2876 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2877 return true;
2878 }
2879
2880 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2881 sp<EffectChain> ec =
2882 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002883 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002884 return true;
2885 }
2886 }
2887 return false;
2888}
2889
Eric Laurent5baf2af2013-09-12 17:37:00 -07002890void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002891{
2892 Mutex::Autolock _l(mLock);
2893
2894 mGlobalEffectEnableTime = systemTime();
2895
2896 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2897 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2898 if (t->mType == ThreadBase::OFFLOAD) {
2899 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2900 }
2901 }
2902
2903}
2904
Eric Laurentaaa44472014-09-12 17:41:50 -07002905status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2906{
Glenn Kastend848eb42016-03-08 13:42:11 -08002907 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002908 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002909 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002910 if (index >= 0) {
2911 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2912 return ALREADY_EXISTS;
2913 }
2914 mOrphanEffectChains.add(session, chain);
2915 return NO_ERROR;
2916}
2917
2918sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2919{
2920 sp<EffectChain> chain;
2921 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002922 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002923 if (index >= 0) {
2924 chain = mOrphanEffectChains.valueAt(index);
2925 mOrphanEffectChains.removeItemsAt(index);
2926 }
2927 return chain;
2928}
2929
2930bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2931{
2932 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08002933 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002934 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002935 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002936 if (index >= 0) {
2937 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2938 if (chain->removeEffect_l(effect) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002939 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002940 mOrphanEffectChains.removeItemsAt(index);
2941 }
2942 return true;
2943 }
2944 return false;
2945}
2946
2947
Glenn Kastenda6ef132013-01-10 12:31:01 -08002948struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002949#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2950 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002951};
2952
2953int comparEntry(const void *p1, const void *p2)
2954{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002955 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002956}
2957
Glenn Kasten46909e72013-02-26 09:20:22 -08002958#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002959void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002960{
Eric Laurent81784c32012-11-19 14:55:58 -08002961 NBAIO_Source *teeSource = source.get();
2962 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002963 // .wav rotation
2964 // There is a benign race condition if 2 threads call this simultaneously.
2965 // They would both traverse the directory, but the result would simply be
2966 // failures at unlink() which are ignored. It's also unlikely since
2967 // normally dumpsys is only done by bugreport or from the command line.
2968 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08002969 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08002970 size_t teePathLen = strlen(teePath);
2971 DIR *dir = opendir(teePath);
2972 teePath[teePathLen++] = '/';
2973 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002974#define TEE_MAX_SORT 20 // number of entries to sort
2975#define TEE_MAX_KEEP 10 // number of entries to keep
2976 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002977 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002978 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002979 struct dirent de;
2980 struct dirent *result = NULL;
2981 int rc = readdir_r(dir, &de, &result);
2982 if (rc != 0) {
2983 ALOGW("readdir_r failed %d", rc);
2984 break;
2985 }
2986 if (result == NULL) {
2987 break;
2988 }
2989 if (result != &de) {
2990 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2991 break;
2992 }
2993 // ignore non .wav file entries
2994 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002995 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08002996 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2997 continue;
2998 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08002999 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003000 }
3001 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003002 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003003 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003004 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
3005 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003006 (void) unlink(teePath);
3007 }
3008 }
3009 } else {
3010 if (fd >= 0) {
Glenn Kasten9a003992016-02-23 15:24:34 -08003011 dprintf(fd, "unable to rotate tees in %.*s: %s\n", teePathLen, teePath,
3012 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003013 }
3014 }
Eric Laurent81784c32012-11-19 14:55:58 -08003015 char teeTime[16];
3016 struct timeval tv;
3017 gettimeofday(&tv, NULL);
3018 struct tm tm;
3019 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003020 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
3021 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
3022 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3023 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003024 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003025 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003026 char wavHeader[44];
3027 memcpy(wavHeader,
3028 "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",
3029 sizeof(wavHeader));
3030 NBAIO_Format format = teeSource->format();
3031 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003032 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003033 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003034 wavHeader[22] = channelCount; // number of channels
3035 wavHeader[24] = sampleRate; // sample rate
3036 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003037 wavHeader[32] = frameSize; // block alignment
3038 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003039 write(teeFd, wavHeader, sizeof(wavHeader));
3040 size_t total = 0;
3041 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003042#define TEE_SINK_READ 1024 // frames per I/O operation
3043 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003044 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003045 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003046 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003047 bool wasFirstRead = firstRead;
3048 firstRead = false;
3049 if (actual <= 0) {
3050 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3051 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003052 }
Eric Laurent81784c32012-11-19 14:55:58 -08003053 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003054 }
Eric Laurent81784c32012-11-19 14:55:58 -08003055 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003056 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003057 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003058 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003059 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003060 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003061 uint32_t temp = 44 + total * frameSize - 8;
3062 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003063 write(teeFd, &temp, sizeof(temp));
3064 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003065 temp = total * frameSize;
3066 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003067 write(teeFd, &temp, sizeof(temp));
3068 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003069 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003070 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003071 }
Eric Laurent59255e42011-07-27 19:49:51 -07003072 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003073 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003074 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003075 }
Eric Laurent59255e42011-07-27 19:49:51 -07003076 }
3077 }
3078}
Glenn Kasten46909e72013-02-26 09:20:22 -08003079#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003080
Mathias Agopian65ab4712010-07-14 17:59:35 -07003081// ----------------------------------------------------------------------------
3082
3083status_t AudioFlinger::onTransact(
3084 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3085{
3086 return BnAudioFlinger::onTransact(code, data, reply, flags);
3087}
3088
Glenn Kasten63238ef2015-03-02 15:50:29 -08003089} // namespace android