blob: 75998efc47470aed600c2ab3e9094790afc0d51b [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>
Andy Hung07b745e2016-05-23 16:21:07 -070061#include <media/MemoryLeakTrackUtil.h>
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
Andy Hung07b745e2016-05-23 16:21:07 -0700474 bool dumpMem = false;
Andy Hung35fec5f2016-04-13 14:21:48 -0700475 bool unreachableMemory = false;
476 for (const auto &arg : args) {
Andy Hung07b745e2016-05-23 16:21:07 -0700477 if (arg == String16("-m")) {
478 dumpMem = true;
479 } else if (arg == String16("--unreachable")) {
Andy Hung35fec5f2016-04-13 14:21:48 -0700480 unreachableMemory = true;
481 }
482 }
483
Andy Hung07b745e2016-05-23 16:21:07 -0700484 if (dumpMem) {
485 dprintf(fd, "\nDumping memory:\n");
486 std::string s = dumpMemoryAddresses(100 /* limit */);
487 write(fd, s.c_str(), s.size());
488 }
Andy Hung35fec5f2016-04-13 14:21:48 -0700489 if (unreachableMemory) {
490 dprintf(fd, "\nDumping unreachable memory:\n");
491 // TODO - should limit be an argument parameter?
Andy Hung07b745e2016-05-23 16:21:07 -0700492 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
Andy Hung35fec5f2016-04-13 14:21:48 -0700493 write(fd, s.c_str(), s.size());
494 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700495 }
496 return NO_ERROR;
497}
498
Eric Laurent021cf962014-05-13 10:18:14 -0700499sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800500{
Eric Laurent021cf962014-05-13 10:18:14 -0700501 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800502 // If pid is already in the mClients wp<> map, then use that entry
503 // (for which promote() is always != 0), otherwise create a new entry and Client.
504 sp<Client> client = mClients.valueFor(pid).promote();
505 if (client == 0) {
506 client = new Client(this, pid);
507 mClients.add(pid, client);
508 }
509
510 return client;
511}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512
Glenn Kasten9e58b552013-01-18 15:09:48 -0800513sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
514{
Glenn Kasten481fb672013-09-30 14:39:28 -0700515 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800516 if (mLogMemoryDealer == 0) {
517 return new NBLog::Writer();
518 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800519 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700520 // Similarly if we can't contact the media.log service, also return a dummy writer
521 if (binder == 0) {
522 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800523 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700524 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
525 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
526 // If allocation fails, consult the vector of previously unregistered writers
527 // and garbage-collect one or more them until an allocation succeeds
528 if (shared == 0) {
529 Mutex::Autolock _l(mUnregisteredWritersLock);
530 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
531 {
532 // Pick the oldest stale writer to garbage-collect
533 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
534 mUnregisteredWriters.removeAt(0);
535 mediaLogService->unregisterWriter(iMemory);
536 // Now the media.log remote reference to IMemory is gone. When our last local
537 // reference to IMemory also drops to zero at end of this block,
538 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
539 }
540 // Re-attempt the allocation
541 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
542 if (shared != 0) {
543 goto success;
544 }
545 }
546 // Even after garbage-collecting all old writers, there is still not enough memory,
547 // so return a dummy writer
548 return new NBLog::Writer();
549 }
550success:
551 mediaLogService->registerWriter(shared, size, name);
552 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800553}
554
555void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
556{
Glenn Kasten685ef092013-02-04 08:15:34 -0800557 if (writer == 0) {
558 return;
559 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800560 sp<IMemory> iMemory(writer->getIMemory());
561 if (iMemory == 0) {
562 return;
563 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700564 // Rather than removing the writer immediately, append it to a queue of old writers to
565 // be garbage-collected later. This allows us to continue to view old logs for a while.
566 Mutex::Autolock _l(mUnregisteredWritersLock);
567 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800568}
569
Mathias Agopian65ab4712010-07-14 17:59:35 -0700570// IAudioFlinger interface
571
572
573sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800574 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800576 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700577 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800578 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800579 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800581 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800582 pid_t tid,
Glenn Kastend848eb42016-03-08 13:42:11 -0800583 audio_session_t *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800584 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585 status_t *status)
586{
587 sp<PlaybackThread::Track> track;
588 sp<TrackHandle> trackHandle;
589 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -0800591 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592
Glenn Kasten263709e2012-01-06 08:40:01 -0800593 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
594 // but if someone uses binder directly they could bypass that and cause us to crash
595 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000596 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597 lStatus = BAD_VALUE;
598 goto Exit;
599 }
600
Glenn Kasten53b5d092014-02-05 10:00:23 -0800601 // further sample rate checks are performed by createTrack_l() depending on the thread type
602 if (sampleRate == 0) {
603 ALOGE("createTrack() invalid sample rate %u", sampleRate);
604 lStatus = BAD_VALUE;
605 goto Exit;
606 }
607
608 // further channel mask checks are performed by createTrack_l() depending on the thread type
609 if (!audio_is_output_channel(channelMask)) {
610 ALOGE("createTrack() invalid channel mask %#x", channelMask);
611 lStatus = BAD_VALUE;
612 goto Exit;
613 }
614
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700615 // further format checks are performed by createTrack_l() depending on the thread type
616 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800617 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700618 lStatus = BAD_VALUE;
619 goto Exit;
620 }
621
Glenn Kasten663c2242013-09-24 11:52:37 -0700622 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
623 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
624 lStatus = BAD_VALUE;
625 goto Exit;
626 }
627
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 {
629 Mutex::Autolock _l(mLock);
630 PlaybackThread *thread = checkPlaybackThread_l(output);
631 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800632 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633 lStatus = BAD_VALUE;
634 goto Exit;
635 }
636
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800637 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700638 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639
Glenn Kastene848bd92014-03-13 15:00:32 -0700640 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700641 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800642 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
643 ALOGE("createTrack() invalid session ID %d", *sessionId);
644 lStatus = BAD_VALUE;
645 goto Exit;
646 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700647 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700648 // check if an effect chain with the same session ID is present on another
649 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700650 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700651 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
652 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700653 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700654 if (sessions & PlaybackThread::EFFECT_SESSION) {
655 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700656 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700657 }
Eric Laurentde070132010-07-13 04:45:46 -0700658 }
659 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700661 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800662 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663 if (sessionId != NULL) {
664 *sessionId = lSessionId;
665 }
666 }
Steve Block3856b092011-10-20 11:56:00 +0100667 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668
669 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800670 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800671 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700672 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700673
674 // move effect chain to this output thread if an effect on same session was waiting
675 // for a track to be created
676 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700677 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700678 Mutex::Autolock _dl(thread->mLock);
679 Mutex::Autolock _sl(effectThread->mLock);
680 moveEffectChain_l(lSessionId, effectThread, thread, true);
681 }
Eric Laurenta011e352012-03-29 15:51:43 -0700682
683 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700684 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700685 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
686 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700687 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700688 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700689 } else {
690 mPendingSyncEvents[i]->cancel();
691 }
Eric Laurenta011e352012-03-29 15:51:43 -0700692 mPendingSyncEvents.removeAt(i);
693 i--;
694 }
695 }
696 }
Glenn Kastene198c362013-08-13 09:13:36 -0700697
Glenn Kastend848eb42016-03-08 13:42:11 -0800698 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 }
Glenn Kastene198c362013-08-13 09:13:36 -0700700
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700701 if (lStatus != NO_ERROR) {
702 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700703 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700704 // Don't hold mClientLock when releasing the reference on the track as the
705 // destructor will acquire it.
706 {
707 Mutex::Autolock _cl(mClientLock);
708 client.clear();
709 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700711 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 }
713
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700714 // return handle to client
715 trackHandle = new TrackHandle(track);
716
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700718 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700719 return trackHandle;
720}
721
Glenn Kasten2c073da2016-02-26 09:14:08 -0800722uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723{
724 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800725 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800727 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728 return 0;
729 }
730 return thread->sampleRate();
731}
732
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800733audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734{
735 Mutex::Autolock _l(mLock);
736 PlaybackThread *thread = checkPlaybackThread_l(output);
737 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000738 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800739 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740 }
741 return thread->format();
742}
743
Glenn Kasten2c073da2016-02-26 09:14:08 -0800744size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700745{
746 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800747 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800749 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750 return 0;
751 }
Glenn Kasten58912562012-04-03 10:45:00 -0700752 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
753 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754 return thread->frameCount();
755}
756
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700757size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
758{
759 Mutex::Autolock _l(mLock);
760 ThreadBase *thread = checkThread_l(ioHandle);
761 if (thread == NULL) {
762 ALOGW("frameCountHAL() unknown thread %d", ioHandle);
763 return 0;
764 }
765 return thread->frameCountHAL();
766}
767
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800768uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769{
770 Mutex::Autolock _l(mLock);
771 PlaybackThread *thread = checkPlaybackThread_l(output);
772 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800773 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700774 return 0;
775 }
776 return thread->latency();
777}
778
779status_t AudioFlinger::setMasterVolume(float value)
780{
Eric Laurenta1884f92011-08-23 08:25:03 -0700781 status_t ret = initCheck();
782 if (ret != NO_ERROR) {
783 return ret;
784 }
785
Mathias Agopian65ab4712010-07-14 17:59:35 -0700786 // check calling permissions
787 if (!settingsAllowed()) {
788 return PERMISSION_DENIED;
789 }
790
Eric Laurenta4c5a552012-03-29 10:12:40 -0700791 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700792 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700793
John Grossmanee578c02012-07-23 17:05:46 -0700794 // Set master volume in the HALs which support it.
795 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
796 AutoMutex lock(mHardwareLock);
797 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800798
John Grossmanee578c02012-07-23 17:05:46 -0700799 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
800 if (dev->canSetMasterVolume()) {
801 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800802 }
John Grossmanee578c02012-07-23 17:05:46 -0700803 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805
John Grossmanee578c02012-07-23 17:05:46 -0700806 // Now set the master volume in each playback thread. Playback threads
807 // assigned to HALs which do not have master volume support will apply
808 // master volume during the mix operation. Threads with HALs which do
809 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700810 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
811 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
812 continue;
813 }
John Grossmanee578c02012-07-23 17:05:46 -0700814 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700815 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816
817 return NO_ERROR;
818}
819
Glenn Kastenf78aee72012-01-04 11:00:47 -0800820status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700821{
Eric Laurenta1884f92011-08-23 08:25:03 -0700822 status_t ret = initCheck();
823 if (ret != NO_ERROR) {
824 return ret;
825 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826
827 // check calling permissions
828 if (!settingsAllowed()) {
829 return PERMISSION_DENIED;
830 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800831 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000832 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 return BAD_VALUE;
834 }
835
836 { // scope for the lock
837 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700838 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700840 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841 mHardwareStatus = AUDIO_HW_IDLE;
842 }
843
844 if (NO_ERROR == ret) {
845 Mutex::Autolock _l(mLock);
846 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800847 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700848 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849 }
850
851 return ret;
852}
853
854status_t AudioFlinger::setMicMute(bool state)
855{
Eric Laurenta1884f92011-08-23 08:25:03 -0700856 status_t ret = initCheck();
857 if (ret != NO_ERROR) {
858 return ret;
859 }
860
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 // check calling permissions
862 if (!settingsAllowed()) {
863 return PERMISSION_DENIED;
864 }
865
866 AutoMutex lock(mHardwareLock);
867 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700868 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
869 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
870 status_t result = dev->set_mic_mute(dev, state);
871 if (result != NO_ERROR) {
872 ret = result;
873 }
874 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700875 mHardwareStatus = AUDIO_HW_IDLE;
876 return ret;
877}
878
879bool AudioFlinger::getMicMute() const
880{
Eric Laurenta1884f92011-08-23 08:25:03 -0700881 status_t ret = initCheck();
882 if (ret != NO_ERROR) {
883 return false;
884 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800885 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700886 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800887 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800889 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
890 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
891 status_t result = dev->get_mic_mute(dev, &state);
892 if (result == NO_ERROR) {
893 mute = mute && state;
894 }
895 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700896 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800897
898 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899}
900
901status_t AudioFlinger::setMasterMute(bool muted)
902{
John Grossmand8f178d2012-07-20 14:51:35 -0700903 status_t ret = initCheck();
904 if (ret != NO_ERROR) {
905 return ret;
906 }
907
Mathias Agopian65ab4712010-07-14 17:59:35 -0700908 // check calling permissions
909 if (!settingsAllowed()) {
910 return PERMISSION_DENIED;
911 }
912
John Grossmanee578c02012-07-23 17:05:46 -0700913 Mutex::Autolock _l(mLock);
914 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700915
John Grossmanee578c02012-07-23 17:05:46 -0700916 // Set master mute in the HALs which support it.
917 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
918 AutoMutex lock(mHardwareLock);
919 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700920
John Grossmanee578c02012-07-23 17:05:46 -0700921 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
922 if (dev->canSetMasterMute()) {
923 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700924 }
John Grossmanee578c02012-07-23 17:05:46 -0700925 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700926 }
927
John Grossmanee578c02012-07-23 17:05:46 -0700928 // Now set the master mute in each playback thread. Playback threads
929 // assigned to HALs which do not have master mute support will apply master
930 // mute during the mix operation. Threads with HALs which do support master
931 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700932 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
933 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
934 continue;
935 }
John Grossmanee578c02012-07-23 17:05:46 -0700936 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700937 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938
939 return NO_ERROR;
940}
941
942float AudioFlinger::masterVolume() const
943{
Glenn Kasten98067102011-12-13 11:47:54 -0800944 Mutex::Autolock _l(mLock);
945 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946}
947
948bool AudioFlinger::masterMute() const
949{
Glenn Kasten98067102011-12-13 11:47:54 -0800950 Mutex::Autolock _l(mLock);
951 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952}
953
John Grossman4ff14ba2012-02-08 16:37:41 -0800954float AudioFlinger::masterVolume_l() const
955{
John Grossman4ff14ba2012-02-08 16:37:41 -0800956 return mMasterVolume;
957}
958
John Grossmand8f178d2012-07-20 14:51:35 -0700959bool AudioFlinger::masterMute_l() const
960{
John Grossmanee578c02012-07-23 17:05:46 -0700961 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700962}
963
Eric Laurent223fd5c2014-11-11 13:43:36 -0800964status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
965{
966 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
967 ALOGW("setStreamVolume() invalid stream %d", stream);
968 return BAD_VALUE;
969 }
970 pid_t caller = IPCThreadState::self()->getCallingPid();
971 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
972 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
973 return PERMISSION_DENIED;
974 }
975
976 return NO_ERROR;
977}
978
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800979status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
980 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981{
982 // check calling permissions
983 if (!settingsAllowed()) {
984 return PERMISSION_DENIED;
985 }
986
Eric Laurent223fd5c2014-11-11 13:43:36 -0800987 status_t status = checkStreamType(stream);
988 if (status != NO_ERROR) {
989 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800991 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992
993 AutoMutex lock(mLock);
994 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700995 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996 thread = checkPlaybackThread_l(output);
997 if (thread == NULL) {
998 return BAD_VALUE;
999 }
1000 }
1001
1002 mStreamTypes[stream].volume = value;
1003
1004 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001005 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001006 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001007 }
1008 } else {
1009 thread->setStreamVolume(stream, value);
1010 }
1011
1012 return NO_ERROR;
1013}
1014
Glenn Kastenfff6d712012-01-12 16:38:12 -08001015status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016{
1017 // check calling permissions
1018 if (!settingsAllowed()) {
1019 return PERMISSION_DENIED;
1020 }
1021
Eric Laurent223fd5c2014-11-11 13:43:36 -08001022 status_t status = checkStreamType(stream);
1023 if (status != NO_ERROR) {
1024 return status;
1025 }
1026 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1027
1028 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +00001029 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001030 return BAD_VALUE;
1031 }
1032
Eric Laurent93575202011-01-18 18:39:02 -08001033 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -07001035 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001036 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001037
1038 return NO_ERROR;
1039}
1040
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001041float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001043 status_t status = checkStreamType(stream);
1044 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045 return 0.0f;
1046 }
1047
1048 AutoMutex lock(mLock);
1049 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001050 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051 PlaybackThread *thread = checkPlaybackThread_l(output);
1052 if (thread == NULL) {
1053 return 0.0f;
1054 }
1055 volume = thread->streamVolume(stream);
1056 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001057 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058 }
1059
1060 return volume;
1061}
1062
Glenn Kastenfff6d712012-01-12 16:38:12 -08001063bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001065 status_t status = checkStreamType(stream);
1066 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001067 return true;
1068 }
1069
Glenn Kasten6637baa2012-01-09 09:40:36 -08001070 AutoMutex lock(mLock);
1071 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001072}
1073
Eric Laurent054d9d32015-04-24 08:48:48 -07001074
1075void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1076{
1077 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1078 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1079 }
1080}
1081
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001082status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001083{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001084 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1085 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001086
Mathias Agopian65ab4712010-07-14 17:59:35 -07001087 // check calling permissions
1088 if (!settingsAllowed()) {
1089 return PERMISSION_DENIED;
1090 }
1091
Glenn Kasten142f5192014-03-25 17:44:59 -07001092 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1093 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001094 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001095 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001096 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001097 AutoMutex lock(mHardwareLock);
1098 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1099 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1100 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1101 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1102 final_result = result ?: final_result;
1103 }
1104 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001105 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001106 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1107 AudioParameter param = AudioParameter(keyValuePairs);
1108 String8 value;
1109 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001110 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1111 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001112 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1113 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001114 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001115 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1116 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001117 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001118 // suspend effects associated with those session IDs
1119 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001120 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001121 thread->setEffectSuspended(FX_IID_AEC,
1122 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001123 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001124 thread->setEffectSuspended(FX_IID_NS,
1125 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001126 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001127 }
1128 }
Eric Laurentbee53372011-08-29 12:42:48 -07001129 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001130 }
1131 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001132 String8 screenState;
1133 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1134 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001135 if (isOff != (AudioFlinger::mScreenState & 1)) {
1136 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001137 }
1138 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001139 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001140 }
1141
1142 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1143 // and the thread is exited once the lock is released
1144 sp<ThreadBase> thread;
1145 {
1146 Mutex::Autolock _l(mLock);
1147 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001148 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001150 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001151 // indicate output device change to all input threads for pre processing
1152 AudioParameter param = AudioParameter(keyValuePairs);
1153 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001154 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1155 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001156 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001157 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158 }
1159 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001160 if (thread != 0) {
1161 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162 }
1163 return BAD_VALUE;
1164}
1165
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001166String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001168 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1169 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170
Eric Laurenta4c5a552012-03-29 10:12:40 -07001171 Mutex::Autolock _l(mLock);
1172
Glenn Kasten142f5192014-03-25 17:44:59 -07001173 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001174 String8 out_s8;
1175
Dima Zavin799a70e2011-04-18 16:57:27 -07001176 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001177 char *s;
1178 {
1179 AutoMutex lock(mHardwareLock);
1180 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001181 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001182 s = dev->get_parameters(dev, keys.string());
1183 mHardwareStatus = AUDIO_HW_IDLE;
1184 }
John Grossmanef7740b2012-02-09 11:28:36 -08001185 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001186 free(s);
1187 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001188 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001189 }
1190
Mathias Agopian65ab4712010-07-14 17:59:35 -07001191 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1192 if (playbackThread != NULL) {
1193 return playbackThread->getParameters(keys);
1194 }
1195 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1196 if (recordThread != NULL) {
1197 return recordThread->getParameters(keys);
1198 }
1199 return String8("");
1200}
1201
Glenn Kastendd8104c2012-07-02 12:42:44 -07001202size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1203 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204{
Eric Laurenta1884f92011-08-23 08:25:03 -07001205 status_t ret = initCheck();
1206 if (ret != NO_ERROR) {
1207 return 0;
1208 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001209 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001210 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001211 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001212 return 0;
1213 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001214
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001215 AutoMutex lock(mHardwareLock);
1216 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001217 audio_config_t config, proposed;
1218 memset(&proposed, 0, sizeof(proposed));
1219 proposed.sample_rate = sampleRate;
1220 proposed.channel_mask = channelMask;
1221 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001222
John Grossmanee578c02012-07-23 17:05:46 -07001223 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001224 size_t frames;
1225 for (;;) {
1226 // Note: config is currently a const parameter for get_input_buffer_size()
1227 // but we use a copy from proposed in case config changes from the call.
1228 config = proposed;
1229 frames = dev->get_input_buffer_size(dev, &config);
1230 if (frames != 0) {
1231 break; // hal success, config is the result
1232 }
1233 // change one parameter of the configuration each iteration to a more "common" value
1234 // to see if the device will support it.
1235 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1236 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1237 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1238 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1239 } else {
1240 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1241 "format %#x, channelMask 0x%X",
1242 sampleRate, format, channelMask);
1243 break; // retries failed, break out of loop with frames == 0.
1244 }
1245 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001246 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001247 if (frames > 0 && config.sample_rate != sampleRate) {
1248 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1249 }
1250 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251}
1252
Glenn Kasten5f972c02014-01-13 09:59:31 -08001253uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001254{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255 Mutex::Autolock _l(mLock);
1256
1257 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1258 if (recordThread != NULL) {
1259 return recordThread->getInputFramesLost();
1260 }
1261 return 0;
1262}
1263
1264status_t AudioFlinger::setVoiceVolume(float value)
1265{
Eric Laurenta1884f92011-08-23 08:25:03 -07001266 status_t ret = initCheck();
1267 if (ret != NO_ERROR) {
1268 return ret;
1269 }
1270
Mathias Agopian65ab4712010-07-14 17:59:35 -07001271 // check calling permissions
1272 if (!settingsAllowed()) {
1273 return PERMISSION_DENIED;
1274 }
1275
1276 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001277 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001278 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001279 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001280 mHardwareStatus = AUDIO_HW_IDLE;
1281
1282 return ret;
1283}
1284
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001285status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001286 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001287{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 Mutex::Autolock _l(mLock);
1289
1290 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1291 if (playbackThread != NULL) {
1292 return playbackThread->getRenderPosition(halFrames, dspFrames);
1293 }
1294
1295 return BAD_VALUE;
1296}
1297
1298void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1299{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001301 if (client == 0) {
1302 return;
1303 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001304 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001305 {
1306 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001307 if (mNotificationClients.indexOfKey(pid) < 0) {
1308 sp<NotificationClient> notificationClient = new NotificationClient(this,
1309 client,
1310 pid);
1311 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312
Eric Laurent021cf962014-05-13 10:18:14 -07001313 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314
Marco Nelissen06b46062014-11-14 07:58:25 -08001315 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001316 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001317 }
1318 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001319
Eric Laurent021cf962014-05-13 10:18:14 -07001320 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001321 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001322 // the config change is always sent from playback or record threads to avoid deadlock
1323 // with AudioSystem::gLock
1324 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1325 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1326 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001328 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1329 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001330 }
1331}
1332
1333void AudioFlinger::removeNotificationClient(pid_t pid)
1334{
1335 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001336 {
1337 Mutex::Autolock _cl(mClientLock);
1338 mNotificationClients.removeItem(pid);
1339 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001340
Steve Block3856b092011-10-20 11:56:00 +01001341 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001342 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001343 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001344 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001345 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001346 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001347 if (ref->mPid == pid) {
1348 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001349 mAudioSessionRefs.removeAt(i);
1350 delete ref;
1351 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001352 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001353 } else {
1354 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001355 }
1356 }
1357 if (removed) {
1358 purgeStaleEffects_l();
1359 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360}
1361
Eric Laurent73e26b62015-04-27 16:55:58 -07001362void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001363 const sp<AudioIoDescriptor>& ioDesc,
1364 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365{
Eric Laurent021cf962014-05-13 10:18:14 -07001366 Mutex::Autolock _l(mClientLock);
1367 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001368 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001369 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1370 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1371 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372 }
1373}
1374
Eric Laurent021cf962014-05-13 10:18:14 -07001375// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001376void AudioFlinger::removeClient_l(pid_t pid)
1377{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001378 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001379 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380 mClients.removeItem(pid);
1381}
1382
Eric Laurent717e1282012-06-29 16:36:52 -07001383// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001384sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1385 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001386{
1387 sp<PlaybackThread> thread;
1388
1389 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1390 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1391 ALOG_ASSERT(thread == 0);
1392 thread = mPlaybackThreads.valueAt(i);
1393 }
1394 }
1395
1396 return thread;
1397}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001398
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399
Mathias Agopian65ab4712010-07-14 17:59:35 -07001400
1401// ----------------------------------------------------------------------------
1402
1403AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1404 : RefBase(),
1405 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001406 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407{
Eric Laurentda73b6c2015-08-20 16:18:53 -07001408 size_t heapSize = kClientSharedHeapSizeBytes;
1409 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1410 // invalidated tracks
1411 if (!audioFlinger->isLowRamDevice()) {
1412 heapSize *= kClientSharedHeapSizeMultiplier;
1413 }
1414 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001415}
1416
Eric Laurent021cf962014-05-13 10:18:14 -07001417// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001418AudioFlinger::Client::~Client()
1419{
1420 mAudioFlinger->removeClient_l(mPid);
1421}
1422
Glenn Kasten435dbe62012-01-30 10:15:48 -08001423sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424{
1425 return mMemoryDealer;
1426}
1427
1428// ----------------------------------------------------------------------------
1429
1430AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1431 const sp<IAudioFlingerClient>& client,
1432 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001433 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001434{
1435}
1436
1437AudioFlinger::NotificationClient::~NotificationClient()
1438{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439}
1440
Glenn Kasten0f11b512014-01-31 16:18:54 -08001441void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001442{
1443 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001444 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445}
1446
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447
1448// ----------------------------------------------------------------------------
1449
1450sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001451 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001452 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001453 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001454 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001455 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001456 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001457 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001458 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001459 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001460 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001461 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001462 sp<IMemory>& cblk,
1463 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001464 status_t *status)
1465{
1466 sp<RecordThread::RecordTrack> recordTrack;
1467 sp<RecordHandle> recordHandle;
1468 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001469 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001470 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471
Glenn Kastend776ac62014-05-07 09:16:09 -07001472 cblk.clear();
1473 buffers.clear();
1474
Marco Nelissendcb346b2015-09-09 10:47:29 -07001475 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1476 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001477 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001478 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1479 clientUid = callingUid;
1480 }
1481
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001483 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001484 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485 lStatus = PERMISSION_DENIED;
1486 goto Exit;
1487 }
1488
Glenn Kasten53b5d092014-02-05 10:00:23 -08001489 // further sample rate checks are performed by createRecordTrack_l()
1490 if (sampleRate == 0) {
1491 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1492 lStatus = BAD_VALUE;
1493 goto Exit;
1494 }
1495
Andy Hung6770c6f2015-04-07 13:43:36 -07001496 // we don't yet support anything other than linear PCM
1497 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001498 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001499 lStatus = BAD_VALUE;
1500 goto Exit;
1501 }
1502
Glenn Kasten53b5d092014-02-05 10:00:23 -08001503 // further channel mask checks are performed by createRecordTrack_l()
1504 if (!audio_is_input_channel(channelMask)) {
1505 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1506 lStatus = BAD_VALUE;
1507 goto Exit;
1508 }
1509
Glenn Kasten05997e22014-03-13 15:08:33 -07001510 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001511 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001512 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001513 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001514 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001515 lStatus = BAD_VALUE;
1516 goto Exit;
1517 }
1518
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001519 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001520 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001522 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001523 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1524 lStatus = BAD_VALUE;
1525 goto Exit;
1526 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527 lSessionId = *sessionId;
1528 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001529 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001530 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001531 if (sessionId != NULL) {
1532 *sessionId = lSessionId;
1533 }
1534 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001535 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001536
Glenn Kasten1879fff2012-07-11 15:36:59 -07001537 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001538 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001539 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001540 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001541
1542 if (lStatus == NO_ERROR) {
1543 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1544 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001545 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001546 if (chain != 0) {
1547 Mutex::Autolock _l(thread->mLock);
1548 thread->addEffectChain_l(chain);
1549 }
1550 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551 }
Glenn Kastene198c362013-08-13 09:13:36 -07001552
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001553 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001554 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001555 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001556 // Don't hold mClientLock when releasing the reference on the track as the
1557 // destructor will acquire it.
1558 {
1559 Mutex::Autolock _cl(mClientLock);
1560 client.clear();
1561 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563 goto Exit;
1564 }
1565
Glenn Kastend776ac62014-05-07 09:16:09 -07001566 cblk = recordTrack->getCblk();
1567 buffers = recordTrack->getBuffers();
1568
Glenn Kasten2fc14732013-08-05 14:58:14 -07001569 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001570 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001571
1572Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001573 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001574 return recordHandle;
1575}
1576
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001577
1578
Mathias Agopian65ab4712010-07-14 17:59:35 -07001579// ----------------------------------------------------------------------------
1580
Eric Laurenta4c5a552012-03-29 10:12:40 -07001581audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1582{
Eric Laurent44622db2014-08-01 19:00:33 -07001583 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001584 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001585 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001586 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001587 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001588 }
1589 Mutex::Autolock _l(mLock);
1590 return loadHwModule_l(name);
1591}
1592
1593// loadHwModule_l() must be called with AudioFlinger::mLock held
1594audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1595{
1596 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1597 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1598 ALOGW("loadHwModule() module %s already loaded", name);
1599 return mAudioHwDevs.keyAt(i);
1600 }
1601 }
1602
Eric Laurenta4c5a552012-03-29 10:12:40 -07001603 audio_hw_device_t *dev;
1604
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001605 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001606 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001607 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001608 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001609 }
1610
1611 mHardwareStatus = AUDIO_HW_INIT;
1612 rc = dev->init_check(dev);
1613 mHardwareStatus = AUDIO_HW_IDLE;
1614 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001615 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001616 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001617 }
1618
John Grossmanee578c02012-07-23 17:05:46 -07001619 // Check and cache this HAL's level of support for master mute and master
1620 // volume. If this is the first HAL opened, and it supports the get
1621 // methods, use the initial values provided by the HAL as the current
1622 // master mute and volume settings.
1623
1624 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1625 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001626 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001627
1628 if (0 == mAudioHwDevs.size()) {
1629 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1630 if (NULL != dev->get_master_volume) {
1631 float mv;
1632 if (OK == dev->get_master_volume(dev, &mv)) {
1633 mMasterVolume = mv;
1634 }
1635 }
1636
1637 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1638 if (NULL != dev->get_master_mute) {
1639 bool mm;
1640 if (OK == dev->get_master_mute(dev, &mm)) {
1641 mMasterMute = mm;
1642 }
1643 }
1644 }
1645
Eric Laurenta4c5a552012-03-29 10:12:40 -07001646 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001647 if ((NULL != dev->set_master_volume) &&
1648 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1649 flags = static_cast<AudioHwDevice::Flags>(flags |
1650 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1651 }
1652
1653 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1654 if ((NULL != dev->set_master_mute) &&
1655 (OK == dev->set_master_mute(dev, mMasterMute))) {
1656 flags = static_cast<AudioHwDevice::Flags>(flags |
1657 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1658 }
1659
Eric Laurenta4c5a552012-03-29 10:12:40 -07001660 mHardwareStatus = AUDIO_HW_IDLE;
1661 }
1662
Glenn Kastena13cde92016-03-28 15:26:02 -07001663 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001664 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001665
1666 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001667 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001668
1669 return handle;
1670
1671}
1672
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001673// ----------------------------------------------------------------------------
1674
Glenn Kasten3b16c762012-11-14 08:44:39 -08001675uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001676{
1677 Mutex::Autolock _l(mLock);
1678 PlaybackThread *thread = primaryPlaybackThread_l();
1679 return thread != NULL ? thread->sampleRate() : 0;
1680}
1681
Glenn Kastene33054e2012-11-14 12:54:39 -08001682size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001683{
1684 Mutex::Autolock _l(mLock);
1685 PlaybackThread *thread = primaryPlaybackThread_l();
1686 return thread != NULL ? thread->frameCountHAL() : 0;
1687}
1688
1689// ----------------------------------------------------------------------------
1690
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001691status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1692{
1693 uid_t uid = IPCThreadState::self()->getCallingUid();
1694 if (uid != AID_SYSTEM) {
1695 return PERMISSION_DENIED;
1696 }
1697 Mutex::Autolock _l(mLock);
1698 if (mIsDeviceTypeKnown) {
1699 return INVALID_OPERATION;
1700 }
1701 mIsLowRamDevice = isLowRamDevice;
1702 mIsDeviceTypeKnown = true;
1703 return NO_ERROR;
1704}
1705
Eric Laurent93c3d412014-08-01 14:48:35 -07001706audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1707{
1708 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001709
1710 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1711 if (index >= 0) {
1712 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1713 mHwAvSyncIds.valueAt(index), sessionId);
1714 return mHwAvSyncIds.valueAt(index);
1715 }
1716
1717 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1718 if (dev == NULL) {
1719 return AUDIO_HW_SYNC_INVALID;
1720 }
1721 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1722 AudioParameter param = AudioParameter(String8(reply));
1723 free(reply);
1724
1725 int value;
1726 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1727 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1728 return AUDIO_HW_SYNC_INVALID;
1729 }
1730
1731 // allow only one session for a given HW A/V sync ID.
1732 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1733 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1734 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1735 value, mHwAvSyncIds.keyAt(i));
1736 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001737 break;
1738 }
1739 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001740
1741 mHwAvSyncIds.add(sessionId, value);
1742
1743 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1744 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1745 uint32_t sessions = thread->hasAudioSession(sessionId);
1746 if (sessions & PlaybackThread::TRACK_SESSION) {
1747 AudioParameter param = AudioParameter();
1748 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1749 thread->setParameters(param.toString());
1750 break;
1751 }
1752 }
1753
1754 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1755 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001756}
1757
Eric Laurent72e3f392015-05-20 14:43:50 -07001758status_t AudioFlinger::systemReady()
1759{
1760 Mutex::Autolock _l(mLock);
1761 ALOGI("%s", __FUNCTION__);
1762 if (mSystemReady) {
1763 ALOGW("%s called twice", __FUNCTION__);
1764 return NO_ERROR;
1765 }
1766 mSystemReady = true;
1767 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1768 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1769 thread->systemReady();
1770 }
1771 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1772 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1773 thread->systemReady();
1774 }
1775 return NO_ERROR;
1776}
1777
Eric Laurentfa90e842014-10-17 18:12:31 -07001778// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1779void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1780{
1781 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1782 if (index >= 0) {
1783 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1784 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1785 AudioParameter param = AudioParameter();
1786 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1787 thread->setParameters(param.toString());
1788 }
1789}
1790
1791
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001792// ----------------------------------------------------------------------------
1793
Eric Laurent83b88082014-06-20 18:31:16 -07001794
1795sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001796 audio_io_handle_t *output,
1797 audio_config_t *config,
1798 audio_devices_t devices,
1799 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001800 audio_output_flags_t flags)
1801{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001802 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001803 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001804 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001805 }
1806
Eric Laurentcf2c0212014-07-25 16:20:43 -07001807 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001808 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1809 } else {
1810 // Audio Policy does not currently request a specific output handle.
1811 // If this is ever needed, see openInput_l() for example code.
1812 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1813 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001814 }
Eric Laurent83b88082014-06-20 18:31:16 -07001815
1816 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1817
Eric Laurent83b88082014-06-20 18:31:16 -07001818 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001819 // This if statement allows overriding the audio policy settings
1820 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1821 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1822 // Check only for Normal Mixing mode
1823 if (kEnableExtendedPrecision) {
1824 // Specify format (uncomment one below to choose)
1825 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1826 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1827 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1828 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001829 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001830 }
1831 if (kEnableExtendedChannels) {
1832 // Specify channel mask (uncomment one below to choose)
1833 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1834 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1835 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1836 }
Eric Laurent83b88082014-06-20 18:31:16 -07001837 }
1838
Phil Burk062e67a2015-02-11 13:40:50 -08001839 AudioStreamOut *outputStream = NULL;
1840 status_t status = outHwDev->openOutputStream(
1841 &outputStream,
1842 *output,
1843 devices,
1844 flags,
1845 config,
1846 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001847
1848 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001849
Phil Burk062e67a2015-02-11 13:40:50 -08001850 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001851
1852 PlaybackThread *thread;
1853 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurente93cc032016-05-05 10:15:10 -07001854 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001855 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001856 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1857 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001858 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001859 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001860 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001861 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001862 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001863 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001864 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001865 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001866 return thread;
1867 }
1868
1869 return 0;
1870}
1871
Eric Laurentcf2c0212014-07-25 16:20:43 -07001872status_t AudioFlinger::openOutput(audio_module_handle_t module,
1873 audio_io_handle_t *output,
1874 audio_config_t *config,
1875 audio_devices_t *devices,
1876 const String8& address,
1877 uint32_t *latencyMs,
1878 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001879{
Phil Burk062e67a2015-02-11 13:40:50 -08001880 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001881 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001882 (devices != NULL) ? *devices : 0,
1883 config->sample_rate,
1884 config->format,
1885 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001886 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001887
Eric Laurentcf2c0212014-07-25 16:20:43 -07001888 if (*devices == AUDIO_DEVICE_NONE) {
1889 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001890 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001891
Mathias Agopian65ab4712010-07-14 17:59:35 -07001892 Mutex::Autolock _l(mLock);
1893
Eric Laurentcf2c0212014-07-25 16:20:43 -07001894 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001895 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001896 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001897
1898 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001899 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001900
1901 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001902 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001903 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001904 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001905
1906 AutoMutex lock(mHardwareLock);
1907 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001908 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001909 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001910 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001911 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001912 }
1913
Eric Laurentcf2c0212014-07-25 16:20:43 -07001914 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001915}
1916
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001917audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1918 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001919{
1920 Mutex::Autolock _l(mLock);
1921 MixerThread *thread1 = checkMixerThread_l(output1);
1922 MixerThread *thread2 = checkMixerThread_l(output2);
1923
1924 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001925 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1926 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001927 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928 }
1929
Glenn Kasteneeecb982016-02-26 10:44:04 -08001930 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07001931 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001932 thread->addOutputTrack(thread2);
1933 mPlaybackThreads.add(id, thread);
1934 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001935 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001936 return id;
1937}
1938
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001939status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001940{
Glenn Kastend96c5722012-04-25 13:44:49 -07001941 return closeOutput_nonvirtual(output);
1942}
1943
1944status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1945{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001946 // keep strong reference on the playback thread so that
1947 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001948 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001949 {
1950 Mutex::Autolock _l(mLock);
1951 thread = checkPlaybackThread_l(output);
1952 if (thread == NULL) {
1953 return BAD_VALUE;
1954 }
1955
Steve Block3856b092011-10-20 11:56:00 +01001956 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001958 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001959 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001960 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001961 DuplicatingThread *dupThread =
1962 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001963 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001964 }
1965 }
1966 }
1967
1968
1969 mPlaybackThreads.removeItem(output);
1970 // save all effects to the default thread
1971 if (mPlaybackThreads.size()) {
1972 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1973 if (dstThread != NULL) {
1974 // audioflinger lock is held here so the acquisition order of thread locks does not
1975 // matter
1976 Mutex::Autolock _dl(dstThread->mLock);
1977 Mutex::Autolock _sl(thread->mLock);
1978 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1979 for (size_t i = 0; i < effectChains.size(); i ++) {
1980 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001981 }
1982 }
1983 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001984 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1985 ioDesc->mIoHandle = output;
1986 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001987 }
1988 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001989 // The thread entity (active unit of execution) is no longer running here,
1990 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001991
Eric Laurentf6870ae2015-05-08 10:50:03 -07001992 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001993 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001994 }
Eric Laurent83b88082014-06-20 18:31:16 -07001995
Mathias Agopian65ab4712010-07-14 17:59:35 -07001996 return NO_ERROR;
1997}
1998
Eric Laurent83b88082014-06-20 18:31:16 -07001999void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
2000{
2001 AudioStreamOut *out = thread->clearOutput();
2002 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
2003 // from now on thread->mOutput is NULL
2004 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
2005 delete out;
2006}
2007
2008void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
2009{
2010 mPlaybackThreads.removeItem(thread->mId);
2011 thread->exit();
2012 closeOutputFinish(thread);
2013}
2014
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002015status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002016{
2017 Mutex::Autolock _l(mLock);
2018 PlaybackThread *thread = checkPlaybackThread_l(output);
2019
2020 if (thread == NULL) {
2021 return BAD_VALUE;
2022 }
2023
Steve Block3856b092011-10-20 11:56:00 +01002024 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002025 thread->suspend();
2026
2027 return NO_ERROR;
2028}
2029
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002030status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002031{
2032 Mutex::Autolock _l(mLock);
2033 PlaybackThread *thread = checkPlaybackThread_l(output);
2034
2035 if (thread == NULL) {
2036 return BAD_VALUE;
2037 }
2038
Steve Block3856b092011-10-20 11:56:00 +01002039 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002040
2041 thread->restore();
2042
2043 return NO_ERROR;
2044}
2045
Eric Laurentcf2c0212014-07-25 16:20:43 -07002046status_t AudioFlinger::openInput(audio_module_handle_t module,
2047 audio_io_handle_t *input,
2048 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002049 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002050 const String8& address,
2051 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002052 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002053{
Eric Laurent83b88082014-06-20 18:31:16 -07002054 Mutex::Autolock _l(mLock);
2055
Glenn Kastene7d66712015-03-05 13:46:52 -08002056 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002057 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002058 }
2059
Glenn Kastene7d66712015-03-05 13:46:52 -08002060 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002061
2062 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002063 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002064 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002065 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002066 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002067 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002068}
Dima Zavin799a70e2011-04-18 16:57:27 -07002069
Eric Laurent83b88082014-06-20 18:31:16 -07002070sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002071 audio_io_handle_t *input,
2072 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002073 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002074 const String8& address,
2075 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002076 audio_input_flags_t flags)
2077{
Glenn Kastene7d66712015-03-05 13:46:52 -08002078 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002079 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002080 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002081 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002082 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002083
Glenn Kasteneeecb982016-02-26 10:44:04 -08002084 // Audio Policy can request a specific handle for hardware hotword.
2085 // The goal here is not to re-open an already opened input.
2086 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002087 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002088 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2089 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2090 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2091 return 0;
2092 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2093 // This should not happen in a transient state with current design.
2094 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2095 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002096 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002097
Eric Laurentcf2c0212014-07-25 16:20:43 -07002098 audio_config_t halconfig = *config;
2099 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002100 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002101 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002102 &inStream, flags, address.string(), source);
2103 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002104 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002105 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002106 halconfig.sample_rate,
2107 halconfig.format,
2108 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002109 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002110 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002111
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002112 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002113 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002114 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002115 audio_is_linear_pcm(config->format) &&
2116 audio_is_linear_pcm(halconfig.format) &&
2117 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
vivek mehta75346662016-05-04 18:45:46 -07002118 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2119 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002120 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002121 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002122 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002123 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002124 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002125 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002126 }
2127
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002128 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002129
Glenn Kasten46909e72013-02-26 09:20:22 -08002130#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002131 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2132 // or (re-)create if current Pipe is idle and does not match the new format
2133 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002134 enum {
2135 TEE_SINK_NO, // don't copy input
2136 TEE_SINK_NEW, // copy input using a new pipe
2137 TEE_SINK_OLD, // copy input using an existing pipe
2138 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002139 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2140 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002141 if (!mTeeSinkInputEnabled) {
2142 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002143 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002144 kind = TEE_SINK_NO;
2145 } else if (mRecordTeeSink == 0) {
2146 kind = TEE_SINK_NEW;
2147 } else if (mRecordTeeSink->getStrongCount() != 1) {
2148 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002149 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002150 kind = TEE_SINK_OLD;
2151 } else {
2152 kind = TEE_SINK_NEW;
2153 }
2154 switch (kind) {
2155 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002156 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002157 size_t numCounterOffers = 0;
2158 const NBAIO_Format offers[1] = {format};
2159 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2160 ALOG_ASSERT(index == 0);
2161 PipeReader *pipeReader = new PipeReader(*pipe);
2162 numCounterOffers = 0;
2163 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2164 ALOG_ASSERT(index == 0);
2165 mRecordTeeSink = pipe;
2166 mRecordTeeSource = pipeReader;
2167 teeSink = pipe;
2168 }
2169 break;
2170 case TEE_SINK_OLD:
2171 teeSink = mRecordTeeSink;
2172 break;
2173 case TEE_SINK_NO:
2174 default:
2175 break;
2176 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002177#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002178
Eric Laurentcf2c0212014-07-25 16:20:43 -07002179 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002180
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002181 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002182 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002183 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002184 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002185 inputStream,
2186 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002187 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002188 devices,
2189 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002190#ifdef TEE_SINK
2191 , teeSink
2192#endif
2193 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002194 mRecordThreads.add(*input, thread);
2195 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002196 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002197 }
2198
Eric Laurentcf2c0212014-07-25 16:20:43 -07002199 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002200 return 0;
2201}
2202
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002203status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002204{
Glenn Kastend96c5722012-04-25 13:44:49 -07002205 return closeInput_nonvirtual(input);
2206}
2207
2208status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2209{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002210 // keep strong reference on the record thread so that
2211 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002212 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002213 {
2214 Mutex::Autolock _l(mLock);
2215 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002216 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002217 return BAD_VALUE;
2218 }
2219
Steve Block3856b092011-10-20 11:56:00 +01002220 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002221
2222 // If we still have effect chains, it means that a client still holds a handle
2223 // on at least one effect. We must either move the chain to an existing thread with the
2224 // same session ID or put it aside in case a new record thread is opened for a
2225 // new capture on the same session
2226 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002227 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002228 Mutex::Autolock _sl(thread->mLock);
2229 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002230 // Note: maximum one chain per record thread
2231 if (effectChains.size() != 0) {
2232 chain = effectChains[0];
2233 }
2234 }
2235 if (chain != 0) {
2236 // first check if a record thread is already opened with a client on the same session.
2237 // This should only happen in case of overlap between one thread tear down and the
2238 // creation of its replacement
2239 size_t i;
2240 for (i = 0; i < mRecordThreads.size(); i++) {
2241 sp<RecordThread> t = mRecordThreads.valueAt(i);
2242 if (t == thread) {
2243 continue;
2244 }
2245 if (t->hasAudioSession(chain->sessionId()) != 0) {
2246 Mutex::Autolock _l(t->mLock);
2247 ALOGV("closeInput() found thread %d for effect session %d",
2248 t->id(), chain->sessionId());
2249 t->addEffectChain_l(chain);
2250 break;
2251 }
2252 }
2253 // put the chain aside if we could not find a record thread with the same session id.
2254 if (i == mRecordThreads.size()) {
2255 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002256 }
2257 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002258 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2259 ioDesc->mIoHandle = input;
2260 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002261 mRecordThreads.removeItem(input);
2262 }
Eric Laurent83b88082014-06-20 18:31:16 -07002263 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2264 // we have a different lock for notification client
2265 closeInputFinish(thread);
2266 return NO_ERROR;
2267}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002268
Eric Laurent83b88082014-06-20 18:31:16 -07002269void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2270{
2271 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002272 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002273 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002274 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002275 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002276 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002277}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002278
Eric Laurent83b88082014-06-20 18:31:16 -07002279void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2280{
2281 mRecordThreads.removeItem(thread->mId);
2282 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002283}
2284
Glenn Kastend2304db2014-02-03 07:40:31 -08002285status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002286{
2287 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002288 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002289
2290 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2291 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002292 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002293 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002294
2295 return NO_ERROR;
2296}
2297
2298
Glenn Kasteneeecb982016-02-26 10:44:04 -08002299audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002300{
Glenn Kasten9d003132016-04-06 14:38:09 -07002301 // This is a binder API, so a malicious client could pass in a bad parameter.
2302 // Check for that before calling the internal API nextUniqueId().
2303 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2304 ALOGE("newAudioUniqueId invalid use %d", use);
2305 return AUDIO_UNIQUE_ID_ALLOCATE;
2306 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002307 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002308}
2309
Glenn Kastend848eb42016-03-08 13:42:11 -08002310void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002311{
2312 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002313 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002314 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2315 if (pid != -1 && (caller == getpid_cached)) {
2316 caller = pid;
2317 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002318
Eric Laurent021cf962014-05-13 10:18:14 -07002319 {
2320 Mutex::Autolock _cl(mClientLock);
2321 // Ignore requests received from processes not known as notification client. The request
2322 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2323 // called from a different pid leaving a stale session reference. Also we don't know how
2324 // to clear this reference if the client process dies.
2325 if (mNotificationClients.indexOfKey(caller) < 0) {
2326 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2327 return;
2328 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002329 }
2330
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002331 size_t num = mAudioSessionRefs.size();
2332 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002333 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002334 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2335 ref->mCnt++;
2336 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002337 return;
2338 }
2339 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002340 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2341 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002342}
2343
Glenn Kastend848eb42016-03-08 13:42:11 -08002344void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002345{
2346 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002347 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002348 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2349 if (pid != -1 && (caller == getpid_cached)) {
2350 caller = pid;
2351 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002352 size_t num = mAudioSessionRefs.size();
2353 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002354 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002355 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2356 ref->mCnt--;
2357 ALOGV(" decremented refcount to %d", ref->mCnt);
2358 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002359 mAudioSessionRefs.removeAt(i);
2360 delete ref;
2361 purgeStaleEffects_l();
2362 }
2363 return;
2364 }
2365 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002366 // If the caller is mediaserver it is likely that the session being released was acquired
2367 // on behalf of a process not in notification clients and we ignore the warning.
2368 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002369}
2370
2371void AudioFlinger::purgeStaleEffects_l() {
2372
Steve Block3856b092011-10-20 11:56:00 +01002373 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002374
2375 Vector< sp<EffectChain> > chains;
2376
2377 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2378 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2379 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2380 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002381 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2382 chains.push(ec);
2383 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002384 }
2385 }
2386 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2387 sp<RecordThread> t = mRecordThreads.valueAt(i);
2388 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2389 sp<EffectChain> ec = t->mEffectChains[j];
2390 chains.push(ec);
2391 }
2392 }
2393
2394 for (size_t i = 0; i < chains.size(); i++) {
2395 sp<EffectChain> ec = chains[i];
2396 int sessionid = ec->sessionId();
2397 sp<ThreadBase> t = ec->mThread.promote();
2398 if (t == 0) {
2399 continue;
2400 }
2401 size_t numsessionrefs = mAudioSessionRefs.size();
2402 bool found = false;
2403 for (size_t k = 0; k < numsessionrefs; k++) {
2404 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002405 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002406 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002407 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002408 found = true;
2409 break;
2410 }
2411 }
2412 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002413 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002414 // remove all effects from the chain
2415 while (ec->mEffects.size()) {
2416 sp<EffectModule> effect = ec->mEffects[0];
2417 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002418 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002419 if (effect->purgeHandles()) {
2420 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002421 }
2422 AudioSystem::unregisterEffect(effect->id());
2423 }
2424 }
2425 }
2426 return;
2427}
2428
Glenn Kasteneeecb982016-02-26 10:44:04 -08002429// checkThread_l() must be called with AudioFlinger::mLock held
2430AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2431{
2432 ThreadBase *thread = NULL;
2433 switch (audio_unique_id_get_use(ioHandle)) {
2434 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2435 thread = checkPlaybackThread_l(ioHandle);
2436 break;
2437 case AUDIO_UNIQUE_ID_USE_INPUT:
2438 thread = checkRecordThread_l(ioHandle);
2439 break;
2440 default:
2441 break;
2442 }
2443 return thread;
2444}
2445
Mathias Agopian65ab4712010-07-14 17:59:35 -07002446// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002447AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002448{
Glenn Kastena1117922012-01-26 10:53:32 -08002449 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002450}
2451
2452// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002453AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002454{
2455 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002456 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002457}
2458
2459// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002460AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002461{
Glenn Kastena1117922012-01-26 10:53:32 -08002462 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002463}
2464
Glenn Kasteneeecb982016-02-26 10:44:04 -08002465audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002466{
Glenn Kasten9d003132016-04-06 14:38:09 -07002467 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002468 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
Glenn Kastend2e67e12016-04-11 08:26:37 -07002469 const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2470 for (int retry = 0; retry < maxRetries; retry++) {
2471 // The cast allows wraparound from max positive to min negative instead of abort
2472 uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2473 (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2474 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2475 // allow wrap by skipping 0 and -1 for session ids
2476 if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2477 ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2478 return (audio_unique_id_t) (base | use);
2479 }
2480 }
2481 // We have no way of recovering from wraparound
2482 LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2483 // TODO Use a floor after wraparound. This may need a mutex.
Mathias Agopian65ab4712010-07-14 17:59:35 -07002484}
2485
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002486AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002487{
2488 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2489 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002490 if(thread->isDuplicating()) {
2491 continue;
2492 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002493 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002494 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002495 return thread;
2496 }
2497 }
2498 return NULL;
2499}
2500
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002501audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002502{
2503 PlaybackThread *thread = primaryPlaybackThread_l();
2504
2505 if (thread == NULL) {
2506 return 0;
2507 }
2508
Eric Laurentf1c04f92012-08-28 14:26:53 -07002509 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002510}
2511
Eric Laurenta011e352012-03-29 15:51:43 -07002512sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002513 audio_session_t triggerSession,
2514 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002515 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002516 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002517{
2518 Mutex::Autolock _l(mLock);
2519
2520 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2521 status_t playStatus = NAME_NOT_FOUND;
2522 status_t recStatus = NAME_NOT_FOUND;
2523 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2524 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2525 if (playStatus == NO_ERROR) {
2526 return event;
2527 }
2528 }
2529 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2530 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2531 if (recStatus == NO_ERROR) {
2532 return event;
2533 }
2534 }
2535 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2536 mPendingSyncEvents.add(event);
2537 } else {
2538 ALOGV("createSyncEvent() invalid event %d", event->type());
2539 event.clear();
2540 }
2541 return event;
2542}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002543
Mathias Agopian65ab4712010-07-14 17:59:35 -07002544// ----------------------------------------------------------------------------
2545// Effect management
2546// ----------------------------------------------------------------------------
2547
2548
Glenn Kastenf587ba52012-01-26 16:25:10 -08002549status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002550{
2551 Mutex::Autolock _l(mLock);
2552 return EffectQueryNumberEffects(numEffects);
2553}
2554
Glenn Kastenf587ba52012-01-26 16:25:10 -08002555status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002556{
2557 Mutex::Autolock _l(mLock);
2558 return EffectQueryEffect(index, descriptor);
2559}
2560
Glenn Kasten5e92a782012-01-30 07:40:52 -08002561status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002562 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002563{
2564 Mutex::Autolock _l(mLock);
2565 return EffectGetDescriptor(pUuid, descriptor);
2566}
2567
2568
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002569sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002570 effect_descriptor_t *pDesc,
2571 const sp<IEffectClient>& effectClient,
2572 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002573 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002574 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002575 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002576 status_t *status,
2577 int *id,
2578 int *enabled)
2579{
2580 status_t lStatus = NO_ERROR;
2581 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002582 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002583
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002584 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002585 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002586 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002587
2588 if (pDesc == NULL) {
2589 lStatus = BAD_VALUE;
2590 goto Exit;
2591 }
2592
Eric Laurent84e9a102010-09-23 16:10:16 -07002593 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002594 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002595 lStatus = PERMISSION_DENIED;
2596 goto Exit;
2597 }
2598
Dima Zavinfce7a472011-04-19 22:30:36 -07002599 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002600 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002601 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002602 lStatus = PERMISSION_DENIED;
2603 goto Exit;
2604 }
2605
Mathias Agopian65ab4712010-07-14 17:59:35 -07002606 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002607 if (!EffectIsNullUuid(&pDesc->uuid)) {
2608 // if uuid is specified, request effect descriptor
2609 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2610 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002611 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002612 goto Exit;
2613 }
2614 } else {
2615 // if uuid is not specified, look for an available implementation
2616 // of the required type in effect factory
2617 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002618 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002619 lStatus = BAD_VALUE;
2620 goto Exit;
2621 }
2622 uint32_t numEffects = 0;
2623 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002624 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002625 bool found = false;
2626
2627 lStatus = EffectQueryNumberEffects(&numEffects);
2628 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002629 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002630 goto Exit;
2631 }
2632 for (uint32_t i = 0; i < numEffects; i++) {
2633 lStatus = EffectQueryEffect(i, &desc);
2634 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002635 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002636 continue;
2637 }
2638 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2639 // If matching type found save effect descriptor. If the session is
2640 // 0 and the effect is not auxiliary, continue enumeration in case
2641 // an auxiliary version of this effect type is available
2642 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002643 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002644 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002645 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2646 break;
2647 }
2648 }
2649 }
2650 if (!found) {
2651 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002652 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002653 goto Exit;
2654 }
2655 // For same effect type, chose auxiliary version over insert version if
2656 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002657 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002658 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002659 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002660 }
2661 }
2662
2663 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002664 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002665 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2666 lStatus = INVALID_OPERATION;
2667 goto Exit;
2668 }
2669
Eric Laurent59255e42011-07-27 19:49:51 -07002670 // check recording permission for visualizer
2671 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002672 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002673 lStatus = PERMISSION_DENIED;
2674 goto Exit;
2675 }
2676
Mathias Agopian65ab4712010-07-14 17:59:35 -07002677 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002678 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002679 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002680 // if the output returned by getOutputForEffect() is removed before we lock the
2681 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2682 // and we will exit safely
2683 io = AudioSystem::getOutputForEffect(&desc);
2684 ALOGV("createEffect got output %d", io);
2685 }
2686
2687 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002688
2689 // If output is not specified try to find a matching audio session ID in one of the
2690 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002691 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2692 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002693 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002694 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002695 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2696 // output must be specified by AudioPolicyManager when using session
2697 // AUDIO_SESSION_OUTPUT_STAGE
2698 lStatus = BAD_VALUE;
2699 goto Exit;
2700 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002701 // look for the thread where the specified audio session is present
2702 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2703 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2704 io = mPlaybackThreads.keyAt(i);
2705 break;
2706 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002707 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002708 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002709 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2710 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2711 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002712 break;
2713 }
2714 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002715 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002716 // If no output thread contains the requested session ID, default to
2717 // first output. The effect chain will be moved to the correct output
2718 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002719 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002720 io = mPlaybackThreads.keyAt(0);
2721 }
Steve Block3856b092011-10-20 11:56:00 +01002722 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002723 }
2724 ThreadBase *thread = checkRecordThread_l(io);
2725 if (thread == NULL) {
2726 thread = checkPlaybackThread_l(io);
2727 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002728 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002729 lStatus = BAD_VALUE;
2730 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002731 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002732 } else {
2733 // Check if one effect chain was awaiting for an effect to be created on this
2734 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08002735 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07002736 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002737 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002738 thread->addEffectChain_l(chain);
2739 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002740 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002741
Eric Laurent021cf962014-05-13 10:18:14 -07002742 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002743
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002744 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002745 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2746 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002747 if (handle != 0 && id != NULL) {
2748 *id = handle->id();
2749 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002750 if (handle == 0) {
2751 // remove local strong reference to Client with mClientLock held
2752 Mutex::Autolock _cl(mClientLock);
2753 client.clear();
2754 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002755 }
2756
2757Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002758 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002759 return handle;
2760}
2761
Glenn Kastend848eb42016-03-08 13:42:11 -08002762status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002763 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002764{
Steve Block3856b092011-10-20 11:56:00 +01002765 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002766 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002767 Mutex::Autolock _l(mLock);
2768 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002769 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002770 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002771 }
Eric Laurentde070132010-07-13 04:45:46 -07002772 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2773 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002774 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002775 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002776 }
Eric Laurentde070132010-07-13 04:45:46 -07002777 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2778 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002779 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002780 return BAD_VALUE;
2781 }
2782
2783 Mutex::Autolock _dl(dstThread->mLock);
2784 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002785 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002786}
2787
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002788// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08002789status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002790 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002791 AudioFlinger::PlaybackThread *dstThread,
2792 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002793{
Steve Block3856b092011-10-20 11:56:00 +01002794 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002795 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002796
Eric Laurent59255e42011-07-27 19:49:51 -07002797 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002798 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002799 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002800 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002801 return INVALID_OPERATION;
2802 }
2803
Andy Hung9a592762014-07-21 21:56:01 -07002804 // Check whether the destination thread has a channel count of FCC_2, which is
2805 // currently required for (most) effects. Prevent moving the effect chain here rather
2806 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentf6870ae2015-05-08 10:50:03 -07002807 if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
Eric Laurentb10352f2014-11-03 16:25:39 -08002808 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002809 ALOGW("moveEffectChain_l() effect chain failed because"
2810 " destination thread %p channel count(%u) != %u",
2811 dstThread, dstThread->mChannelCount, FCC_2);
2812 return INVALID_OPERATION;
2813 }
2814
Eric Laurent39e94f82010-07-28 01:32:47 -07002815 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002816 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002817 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002818 // removed.
2819 srcThread->removeEffectChain_l(chain);
2820
2821 // transfer all effects one by one so that new effect chain is created on new thread with
2822 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002823 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002824 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002825 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002826 Vector< sp<EffectModule> > removed;
2827 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002828 while (effect != 0) {
2829 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002830 removed.add(effect);
2831 status = dstThread->addEffect_l(effect);
2832 if (status != NO_ERROR) {
2833 break;
2834 }
Eric Laurentec35a142011-10-05 17:42:25 -07002835 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2836 if (effect->state() == EffectModule::ACTIVE ||
2837 effect->state() == EffectModule::STOPPING) {
2838 effect->start();
2839 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002840 // if the move request is not received from audio policy manager, the effect must be
2841 // re-registered with the new strategy and output
2842 if (dstChain == 0) {
2843 dstChain = effect->chain().promote();
2844 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002845 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002846 status = NO_INIT;
2847 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002848 }
2849 strategy = dstChain->strategy();
2850 }
2851 if (reRegister) {
2852 AudioSystem::unregisterEffect(effect->id());
2853 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002854 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002855 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002856 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002857 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002858 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002859 }
Eric Laurentde070132010-07-13 04:45:46 -07002860 effect = chain->getEffectFromId_l(0);
2861 }
2862
Eric Laurent5baf2af2013-09-12 17:37:00 -07002863 if (status != NO_ERROR) {
2864 for (size_t i = 0; i < removed.size(); i++) {
2865 srcThread->addEffect_l(removed[i]);
2866 if (dstChain != 0 && reRegister) {
2867 AudioSystem::unregisterEffect(removed[i]->id());
2868 AudioSystem::registerEffect(&removed[i]->desc(),
2869 srcThread->id(),
2870 strategy,
2871 sessionId,
2872 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002873 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002874 }
2875 }
2876 }
2877
2878 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002879}
2880
Eric Laurent5baf2af2013-09-12 17:37:00 -07002881bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002882{
2883 if (mGlobalEffectEnableTime != 0 &&
2884 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2885 return true;
2886 }
2887
2888 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2889 sp<EffectChain> ec =
2890 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002891 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002892 return true;
2893 }
2894 }
2895 return false;
2896}
2897
Eric Laurent5baf2af2013-09-12 17:37:00 -07002898void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002899{
2900 Mutex::Autolock _l(mLock);
2901
2902 mGlobalEffectEnableTime = systemTime();
2903
2904 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2905 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2906 if (t->mType == ThreadBase::OFFLOAD) {
2907 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2908 }
2909 }
2910
2911}
2912
Eric Laurentaaa44472014-09-12 17:41:50 -07002913status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2914{
Glenn Kastend848eb42016-03-08 13:42:11 -08002915 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002916 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002917 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002918 if (index >= 0) {
2919 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2920 return ALREADY_EXISTS;
2921 }
2922 mOrphanEffectChains.add(session, chain);
2923 return NO_ERROR;
2924}
2925
2926sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2927{
2928 sp<EffectChain> chain;
2929 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002930 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002931 if (index >= 0) {
2932 chain = mOrphanEffectChains.valueAt(index);
2933 mOrphanEffectChains.removeItemsAt(index);
2934 }
2935 return chain;
2936}
2937
2938bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2939{
2940 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08002941 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002942 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002943 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002944 if (index >= 0) {
2945 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2946 if (chain->removeEffect_l(effect) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002947 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002948 mOrphanEffectChains.removeItemsAt(index);
2949 }
2950 return true;
2951 }
2952 return false;
2953}
2954
2955
Glenn Kastenda6ef132013-01-10 12:31:01 -08002956struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002957#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2958 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002959};
2960
2961int comparEntry(const void *p1, const void *p2)
2962{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002963 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002964}
2965
Glenn Kasten46909e72013-02-26 09:20:22 -08002966#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002967void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002968{
Eric Laurent81784c32012-11-19 14:55:58 -08002969 NBAIO_Source *teeSource = source.get();
2970 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002971 // .wav rotation
2972 // There is a benign race condition if 2 threads call this simultaneously.
2973 // They would both traverse the directory, but the result would simply be
2974 // failures at unlink() which are ignored. It's also unlikely since
2975 // normally dumpsys is only done by bugreport or from the command line.
2976 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08002977 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08002978 size_t teePathLen = strlen(teePath);
2979 DIR *dir = opendir(teePath);
2980 teePath[teePathLen++] = '/';
2981 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002982#define TEE_MAX_SORT 20 // number of entries to sort
2983#define TEE_MAX_KEEP 10 // number of entries to keep
2984 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002985 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002986 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002987 struct dirent de;
2988 struct dirent *result = NULL;
2989 int rc = readdir_r(dir, &de, &result);
2990 if (rc != 0) {
2991 ALOGW("readdir_r failed %d", rc);
2992 break;
2993 }
2994 if (result == NULL) {
2995 break;
2996 }
2997 if (result != &de) {
2998 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2999 break;
3000 }
3001 // ignore non .wav file entries
3002 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003003 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08003004 strcmp(&de.d_name[nameLen - 4], ".wav")) {
3005 continue;
3006 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08003007 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003008 }
3009 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003010 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003011 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08003012 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
3013 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003014 (void) unlink(teePath);
3015 }
3016 }
3017 } else {
3018 if (fd >= 0) {
Glenn Kasten9a003992016-02-23 15:24:34 -08003019 dprintf(fd, "unable to rotate tees in %.*s: %s\n", teePathLen, teePath,
3020 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003021 }
3022 }
Eric Laurent81784c32012-11-19 14:55:58 -08003023 char teeTime[16];
3024 struct timeval tv;
3025 gettimeofday(&tv, NULL);
3026 struct tm tm;
3027 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003028 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
3029 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
3030 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3031 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08003032 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07003033 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08003034 char wavHeader[44];
3035 memcpy(wavHeader,
3036 "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",
3037 sizeof(wavHeader));
3038 NBAIO_Format format = teeSource->format();
3039 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003040 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07003041 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08003042 wavHeader[22] = channelCount; // number of channels
3043 wavHeader[24] = sampleRate; // sample rate
3044 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003045 wavHeader[32] = frameSize; // block alignment
3046 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003047 write(teeFd, wavHeader, sizeof(wavHeader));
3048 size_t total = 0;
3049 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003050#define TEE_SINK_READ 1024 // frames per I/O operation
3051 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003052 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003053 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003054 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003055 bool wasFirstRead = firstRead;
3056 firstRead = false;
3057 if (actual <= 0) {
3058 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3059 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003060 }
Eric Laurent81784c32012-11-19 14:55:58 -08003061 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003062 }
Eric Laurent81784c32012-11-19 14:55:58 -08003063 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003064 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003065 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003066 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003067 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003068 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003069 uint32_t temp = 44 + total * frameSize - 8;
3070 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003071 write(teeFd, &temp, sizeof(temp));
3072 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003073 temp = total * frameSize;
3074 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003075 write(teeFd, &temp, sizeof(temp));
3076 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003077 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003078 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003079 }
Eric Laurent59255e42011-07-27 19:49:51 -07003080 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003081 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003082 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003083 }
Eric Laurent59255e42011-07-27 19:49:51 -07003084 }
3085 }
3086}
Glenn Kasten46909e72013-02-26 09:20:22 -08003087#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003088
Mathias Agopian65ab4712010-07-14 17:59:35 -07003089// ----------------------------------------------------------------------------
3090
3091status_t AudioFlinger::onTransact(
3092 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3093{
3094 return BnAudioFlinger::onTransact(code, data, reply, flags);
3095}
3096
Glenn Kasten63238ef2015-03-02 15:50:29 -08003097} // namespace android