blob: 69df8d34bf6baf9ee6aff3dd95d935f284051719 [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>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070036#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
Dima Zavinfce7a472011-04-19 22:30:36 -070038#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039#include <cutils/properties.h>
40
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
44#include "AudioMixer.h"
45#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080046#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
Andy Hung6770c6f2015-04-07 13:43:36 -070048#include <media/AudioResamplerPublic.h>
49
Mathias Agopian65ab4712010-07-14 17:59:35 -070050#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070051#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070052#include <audio_effects/effect_ns.h>
53#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
Glenn Kasten3b21c502011-12-15 09:52:39 -080055#include <audio_utils/primitives.h>
56
Eric Laurentfeb0db62011-07-22 09:04:31 -070057#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080058
Glenn Kasten9e58b552013-01-18 15:09:48 -080059#include <media/IMediaLogService.h>
60
Glenn Kastenda6ef132013-01-10 12:31:01 -080061#include <media/nbaio/Pipe.h>
62#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070063#include <media/AudioParameter.h>
Wei Jia3f273d12015-11-24 09:06:49 -080064#include <mediautils/BatteryNotifier.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070065#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080066
Mathias Agopian65ab4712010-07-14 17:59:35 -070067// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070068
John Grossman1c345192012-03-27 14:00:17 -070069// Note: the following macro is used for extremely verbose logging message. In
70// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
71// 0; but one side effect of this is to turn all LOGV's as well. Some messages
72// are so verbose that we want to suppress them even when we have ALOG_ASSERT
73// turned on. Do not uncomment the #def below unless you really know what you
74// are doing and want to see all of the extremely verbose messages.
75//#define VERY_VERY_VERBOSE_LOGGING
76#ifdef VERY_VERY_VERBOSE_LOGGING
77#define ALOGVV ALOGV
78#else
79#define ALOGVV(a...) do { } while(0)
80#endif
Eric Laurentde070132010-07-13 04:45:46 -070081
Mathias Agopian65ab4712010-07-14 17:59:35 -070082namespace android {
83
Glenn Kastenec1d6b52011-12-12 09:04:45 -080084static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
85static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070086static const char kClientLockedString[] = "Client lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070087
Glenn Kasten58912562012-04-03 10:45:00 -070088
John Grossman4ff14ba2012-02-08 16:37:41 -080089nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080090
Eric Laurent81784c32012-11-19 14:55:58 -080091uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070092
Glenn Kasten46909e72013-02-26 09:20:22 -080093#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080094bool AudioFlinger::mTeeSinkInputEnabled = false;
95bool AudioFlinger::mTeeSinkOutputEnabled = false;
96bool AudioFlinger::mTeeSinkTrackEnabled = false;
97
98size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
99size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
100size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800101#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800102
Eric Laurent813e2a72013-08-31 12:59:48 -0700103// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
104// we define a minimum time during which a global effect is considered enabled.
105static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
106
Mathias Agopian65ab4712010-07-14 17:59:35 -0700107// ----------------------------------------------------------------------------
108
Marco Nelissenb2208842014-02-07 14:00:50 -0800109const char *formatToString(audio_format_t format) {
Phil Burkfdb3c072016-02-09 10:47:02 -0800110 switch (audio_get_main_format(format)) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530111 case AUDIO_FORMAT_PCM:
112 switch (format) {
113 case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
114 case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
115 case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
116 case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
117 case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
118 case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
119 default:
120 break;
121 }
122 break;
Marco Nelissenb2208842014-02-07 14:00:50 -0800123 case AUDIO_FORMAT_MP3: return "mp3";
124 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
125 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
126 case AUDIO_FORMAT_AAC: return "aac";
127 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
128 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
129 case AUDIO_FORMAT_VORBIS: return "vorbis";
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530130 case AUDIO_FORMAT_OPUS: return "opus";
131 case AUDIO_FORMAT_AC3: return "ac-3";
132 case AUDIO_FORMAT_E_AC3: return "e-ac-3";
Phil Burkfdb3c072016-02-09 10:47:02 -0800133 case AUDIO_FORMAT_IEC61937: return "iec61937";
Marco Nelissenb2208842014-02-07 14:00:50 -0800134 default:
135 break;
136 }
137 return "unknown";
138}
139
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700140static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700141{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700142 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700143 int rc;
144
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700145 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
146 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
147 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
148 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700149 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700150 }
151 rc = audio_hw_device_open(mod, dev);
152 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
153 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
154 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700155 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700156 }
Eric Laurent5806b352014-05-22 12:23:26 -0700157 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700158 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
159 rc = BAD_VALUE;
160 goto out;
161 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700162 return 0;
163
164out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700165 *dev = NULL;
166 return rc;
167}
168
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169// ----------------------------------------------------------------------------
170
171AudioFlinger::AudioFlinger()
172 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800173 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800174 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700175 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800176 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800177 mMasterMute(false),
Glenn Kasteneeecb982016-02-26 10:44:04 -0800178 mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX), // zero has a special meaning, so unavailable
John Grossman4ff14ba2012-02-08 16:37:41 -0800179 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700180 mBtNrecIsOff(false),
181 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700182 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700183 mGlobalEffectEnableTime(0),
Eric Laurent72e3f392015-05-20 14:43:50 -0700184 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185{
Glenn Kasten949a9262013-04-16 12:35:20 -0700186 getpid_cached = getpid();
Glenn Kastenae0cff12016-02-24 13:57:49 -0800187 const bool doLog = property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800188 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800189 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
190 MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800191 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700192
Wei Jia3f273d12015-11-24 09:06:49 -0800193 // reset battery stats.
194 // if the audio service has crashed, battery stats could be left
195 // in bad state, reset the state upon service start.
196 BatteryNotifier::getInstance().noteResetAudio();
197
Glenn Kasten46909e72013-02-26 09:20:22 -0800198#ifdef TEE_SINK
Glenn Kasten9a003992016-02-23 15:24:34 -0800199 char value[PROPERTY_VALUE_MAX];
Glenn Kastenda6ef132013-01-10 12:31:01 -0800200 (void) property_get("ro.debuggable", value, "0");
201 int debuggable = atoi(value);
202 int teeEnabled = 0;
203 if (debuggable) {
204 (void) property_get("af.tee", value, "0");
205 teeEnabled = atoi(value);
206 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800207 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700208 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800209 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700210 }
211 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800212 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700213 }
214 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800215 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700216 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800217#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700218}
219
220void AudioFlinger::onFirstRef()
221{
Dima Zavin799a70e2011-04-18 16:57:27 -0700222 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700223
Eric Laurent93575202011-01-18 18:39:02 -0800224 Mutex::Autolock _l(mLock);
225
Dima Zavin799a70e2011-04-18 16:57:27 -0700226 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800227 char val_str[PROPERTY_VALUE_MAX] = { 0 };
228 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
229 uint32_t int_val;
230 if (1 == sscanf(val_str, "%u", &int_val)) {
231 mStandbyTimeInNsecs = milliseconds(int_val);
232 ALOGI("Using %u mSec as standby time.", int_val);
233 } else {
234 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
235 ALOGI("Using default %u mSec as standby time.",
236 (uint32_t)(mStandbyTimeInNsecs / 1000000));
237 }
238 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239
Eric Laurent1c333e22014-05-20 10:48:17 -0700240 mPatchPanel = new PatchPanel(this);
241
Eric Laurenta4c5a552012-03-29 10:12:40 -0700242 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243}
244
245AudioFlinger::~AudioFlinger()
246{
247 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700248 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700249 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700250 }
251 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700252 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700253 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700255
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800256 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
257 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700258 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
259 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700260 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700261
262 // Tell media.log service about any old writers that still need to be unregistered
Andy Hungce717682015-12-22 13:40:32 -0800263 if (mLogMemoryDealer != 0) {
264 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
265 if (binder != 0) {
266 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
267 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
268 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
269 mUnregisteredWriters.pop();
270 mediaLogService->unregisterWriter(iMemory);
271 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700272 }
273 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274}
275
Eric Laurenta4c5a552012-03-29 10:12:40 -0700276static const char * const audio_interfaces[] = {
277 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
278 AUDIO_HARDWARE_MODULE_ID_A2DP,
279 AUDIO_HARDWARE_MODULE_ID_USB,
280};
281#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
282
Phil Burk062e67a2015-02-11 13:40:50 -0800283AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700284 audio_module_handle_t module,
285 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700286{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700287 // if module is 0, the request comes from an old policy manager and we should load
288 // well known modules
289 if (module == 0) {
290 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
291 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
292 loadHwModule_l(audio_interfaces[i]);
293 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700294 // then try to find a module supporting the requested device.
295 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
296 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
297 audio_hw_device_t *dev = audioHwDevice->hwDevice();
298 if ((dev->get_supported_devices != NULL) &&
299 (dev->get_supported_devices(dev) & devices) == devices)
300 return audioHwDevice;
301 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700302 } else {
303 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700304 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
305 if (audioHwDevice != NULL) {
306 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700307 }
308 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700309
Dima Zavin799a70e2011-04-18 16:57:27 -0700310 return NULL;
311}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312
Glenn Kasten0f11b512014-01-31 16:18:54 -0800313void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700314{
315 const size_t SIZE = 256;
316 char buffer[SIZE];
317 String8 result;
318
319 result.append("Clients:\n");
320 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800321 sp<Client> client = mClients.valueAt(i).promote();
322 if (client != 0) {
323 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
324 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325 }
326 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700327
Eric Laurentd1b28d42013-09-18 18:47:13 -0700328 result.append("Notification Clients:\n");
329 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
330 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
331 result.append(buffer);
332 }
333
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700334 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800335 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700336 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
337 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800338 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700339 result.append(buffer);
340 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342}
343
344
Glenn Kasten0f11b512014-01-31 16:18:54 -0800345void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346{
347 const size_t SIZE = 256;
348 char buffer[SIZE];
349 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800350 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351
John Grossman4ff14ba2012-02-08 16:37:41 -0800352 snprintf(buffer, SIZE, "Hardware status: %d\n"
353 "Standby Time mSec: %u\n",
354 hardwareStatus,
355 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356 result.append(buffer);
357 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358}
359
Glenn Kasten0f11b512014-01-31 16:18:54 -0800360void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361{
362 const size_t SIZE = 256;
363 char buffer[SIZE];
364 String8 result;
365 snprintf(buffer, SIZE, "Permission Denial: "
366 "can't dump AudioFlinger from pid=%d, uid=%d\n",
367 IPCThreadState::self()->getCallingPid(),
368 IPCThreadState::self()->getCallingUid());
369 result.append(buffer);
370 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371}
372
Eric Laurent81784c32012-11-19 14:55:58 -0800373bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374{
375 bool locked = false;
376 for (int i = 0; i < kDumpLockRetries; ++i) {
377 if (mutex.tryLock() == NO_ERROR) {
378 locked = true;
379 break;
380 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800381 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382 }
383 return locked;
384}
385
386status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
387{
Glenn Kasten44deb052012-02-05 18:09:08 -0800388 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389 dumpPermissionDenial(fd, args);
390 } else {
391 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800392 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 if (!hardwareLocked) {
394 String8 result(kHardwareLockedString);
395 write(fd, result.string(), result.size());
396 } else {
397 mHardwareLock.unlock();
398 }
399
Eric Laurent81784c32012-11-19 14:55:58 -0800400 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700401
402 // failed to lock - AudioFlinger is probably deadlocked
403 if (!locked) {
404 String8 result(kDeadlockedString);
405 write(fd, result.string(), result.size());
406 }
407
Eric Laurent021cf962014-05-13 10:18:14 -0700408 bool clientLocked = dumpTryLock(mClientLock);
409 if (!clientLocked) {
410 String8 result(kClientLockedString);
411 write(fd, result.string(), result.size());
412 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700413
414 EffectDumpEffects(fd);
415
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700417 if (clientLocked) {
418 mClientLock.unlock();
419 }
420
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421 dumpInternals(fd, args);
422
423 // dump playback threads
424 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
425 mPlaybackThreads.valueAt(i)->dump(fd, args);
426 }
427
428 // dump record threads
429 for (size_t i = 0; i < mRecordThreads.size(); i++) {
430 mRecordThreads.valueAt(i)->dump(fd, args);
431 }
432
Eric Laurentaaa44472014-09-12 17:41:50 -0700433 // dump orphan effect chains
434 if (mOrphanEffectChains.size() != 0) {
435 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
436 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
437 mOrphanEffectChains.valueAt(i)->dump(fd, args);
438 }
439 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700440 // dump all hardware devs
441 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700442 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700443 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700444 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700445
Glenn Kasten46909e72013-02-26 09:20:22 -0800446#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700447 // dump the serially shared record tee sink
448 if (mRecordTeeSource != 0) {
449 dumpTee(fd, mRecordTeeSource);
450 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800451#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700452
Glenn Kastend65d73c2012-06-22 17:21:07 -0700453 if (locked) {
454 mLock.unlock();
455 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800456
457 // append a copy of media.log here by forwarding fd to it, but don't attempt
458 // to lookup the service if it's not running, as it will block for a second
459 if (mLogMemoryDealer != 0) {
460 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
461 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700462 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800463 Vector<String16> args;
464 binder->dump(fd, args);
465 }
466 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700467 }
468 return NO_ERROR;
469}
470
Eric Laurent021cf962014-05-13 10:18:14 -0700471sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800472{
Eric Laurent021cf962014-05-13 10:18:14 -0700473 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800474 // If pid is already in the mClients wp<> map, then use that entry
475 // (for which promote() is always != 0), otherwise create a new entry and Client.
476 sp<Client> client = mClients.valueFor(pid).promote();
477 if (client == 0) {
478 client = new Client(this, pid);
479 mClients.add(pid, client);
480 }
481
482 return client;
483}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700484
Glenn Kasten9e58b552013-01-18 15:09:48 -0800485sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
486{
Glenn Kasten481fb672013-09-30 14:39:28 -0700487 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800488 if (mLogMemoryDealer == 0) {
489 return new NBLog::Writer();
490 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800491 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700492 // Similarly if we can't contact the media.log service, also return a dummy writer
493 if (binder == 0) {
494 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800495 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700496 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
497 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
498 // If allocation fails, consult the vector of previously unregistered writers
499 // and garbage-collect one or more them until an allocation succeeds
500 if (shared == 0) {
501 Mutex::Autolock _l(mUnregisteredWritersLock);
502 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
503 {
504 // Pick the oldest stale writer to garbage-collect
505 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
506 mUnregisteredWriters.removeAt(0);
507 mediaLogService->unregisterWriter(iMemory);
508 // Now the media.log remote reference to IMemory is gone. When our last local
509 // reference to IMemory also drops to zero at end of this block,
510 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
511 }
512 // Re-attempt the allocation
513 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
514 if (shared != 0) {
515 goto success;
516 }
517 }
518 // Even after garbage-collecting all old writers, there is still not enough memory,
519 // so return a dummy writer
520 return new NBLog::Writer();
521 }
522success:
523 mediaLogService->registerWriter(shared, size, name);
524 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800525}
526
527void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
528{
Glenn Kasten685ef092013-02-04 08:15:34 -0800529 if (writer == 0) {
530 return;
531 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800532 sp<IMemory> iMemory(writer->getIMemory());
533 if (iMemory == 0) {
534 return;
535 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700536 // Rather than removing the writer immediately, append it to a queue of old writers to
537 // be garbage-collected later. This allows us to continue to view old logs for a while.
538 Mutex::Autolock _l(mUnregisteredWritersLock);
539 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800540}
541
Mathias Agopian65ab4712010-07-14 17:59:35 -0700542// IAudioFlinger interface
543
544
545sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800546 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800548 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700549 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800550 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800551 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800553 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800554 pid_t tid,
Glenn Kastend848eb42016-03-08 13:42:11 -0800555 audio_session_t *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800556 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557 status_t *status)
558{
559 sp<PlaybackThread::Track> track;
560 sp<TrackHandle> trackHandle;
561 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -0800563 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700564
Glenn Kasten263709e2012-01-06 08:40:01 -0800565 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
566 // but if someone uses binder directly they could bypass that and cause us to crash
567 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000568 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569 lStatus = BAD_VALUE;
570 goto Exit;
571 }
572
Glenn Kasten53b5d092014-02-05 10:00:23 -0800573 // further sample rate checks are performed by createTrack_l() depending on the thread type
574 if (sampleRate == 0) {
575 ALOGE("createTrack() invalid sample rate %u", sampleRate);
576 lStatus = BAD_VALUE;
577 goto Exit;
578 }
579
580 // further channel mask checks are performed by createTrack_l() depending on the thread type
581 if (!audio_is_output_channel(channelMask)) {
582 ALOGE("createTrack() invalid channel mask %#x", channelMask);
583 lStatus = BAD_VALUE;
584 goto Exit;
585 }
586
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700587 // further format checks are performed by createTrack_l() depending on the thread type
588 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800589 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700590 lStatus = BAD_VALUE;
591 goto Exit;
592 }
593
Glenn Kasten663c2242013-09-24 11:52:37 -0700594 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
595 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
596 lStatus = BAD_VALUE;
597 goto Exit;
598 }
599
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600 {
601 Mutex::Autolock _l(mLock);
602 PlaybackThread *thread = checkPlaybackThread_l(output);
603 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800604 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605 lStatus = BAD_VALUE;
606 goto Exit;
607 }
608
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800609 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700610 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700611
Glenn Kastene848bd92014-03-13 15:00:32 -0700612 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700613 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800614 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
615 ALOGE("createTrack() invalid session ID %d", *sessionId);
616 lStatus = BAD_VALUE;
617 goto Exit;
618 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700619 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700620 // check if an effect chain with the same session ID is present on another
621 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700622 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700623 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
624 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700625 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700626 if (sessions & PlaybackThread::EFFECT_SESSION) {
627 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700628 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700629 }
Eric Laurentde070132010-07-13 04:45:46 -0700630 }
631 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700633 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800634 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635 if (sessionId != NULL) {
636 *sessionId = lSessionId;
637 }
638 }
Steve Block3856b092011-10-20 11:56:00 +0100639 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640
641 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800642 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800643 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700644 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700645
646 // move effect chain to this output thread if an effect on same session was waiting
647 // for a track to be created
648 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700649 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700650 Mutex::Autolock _dl(thread->mLock);
651 Mutex::Autolock _sl(effectThread->mLock);
652 moveEffectChain_l(lSessionId, effectThread, thread, true);
653 }
Eric Laurenta011e352012-03-29 15:51:43 -0700654
655 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700656 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700657 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
658 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700659 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700660 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700661 } else {
662 mPendingSyncEvents[i]->cancel();
663 }
Eric Laurenta011e352012-03-29 15:51:43 -0700664 mPendingSyncEvents.removeAt(i);
665 i--;
666 }
667 }
668 }
Glenn Kastene198c362013-08-13 09:13:36 -0700669
Glenn Kastend848eb42016-03-08 13:42:11 -0800670 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671 }
Glenn Kastene198c362013-08-13 09:13:36 -0700672
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700673 if (lStatus != NO_ERROR) {
674 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700675 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700676 // Don't hold mClientLock when releasing the reference on the track as the
677 // destructor will acquire it.
678 {
679 Mutex::Autolock _cl(mClientLock);
680 client.clear();
681 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700683 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684 }
685
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700686 // return handle to client
687 trackHandle = new TrackHandle(track);
688
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700690 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691 return trackHandle;
692}
693
Glenn Kasten2c073da2016-02-26 09:14:08 -0800694uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695{
696 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800697 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800699 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700 return 0;
701 }
702 return thread->sampleRate();
703}
704
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800705audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706{
707 Mutex::Autolock _l(mLock);
708 PlaybackThread *thread = checkPlaybackThread_l(output);
709 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000710 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800711 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 }
713 return thread->format();
714}
715
Glenn Kasten2c073da2016-02-26 09:14:08 -0800716size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717{
718 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800719 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800721 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 return 0;
723 }
Glenn Kasten58912562012-04-03 10:45:00 -0700724 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
725 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726 return thread->frameCount();
727}
728
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800729uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700730{
731 Mutex::Autolock _l(mLock);
732 PlaybackThread *thread = checkPlaybackThread_l(output);
733 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800734 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 return 0;
736 }
737 return thread->latency();
738}
739
740status_t AudioFlinger::setMasterVolume(float value)
741{
Eric Laurenta1884f92011-08-23 08:25:03 -0700742 status_t ret = initCheck();
743 if (ret != NO_ERROR) {
744 return ret;
745 }
746
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747 // check calling permissions
748 if (!settingsAllowed()) {
749 return PERMISSION_DENIED;
750 }
751
Eric Laurenta4c5a552012-03-29 10:12:40 -0700752 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700753 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700754
John Grossmanee578c02012-07-23 17:05:46 -0700755 // Set master volume in the HALs which support it.
756 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
757 AutoMutex lock(mHardwareLock);
758 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800759
John Grossmanee578c02012-07-23 17:05:46 -0700760 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
761 if (dev->canSetMasterVolume()) {
762 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800763 }
John Grossmanee578c02012-07-23 17:05:46 -0700764 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766
John Grossmanee578c02012-07-23 17:05:46 -0700767 // Now set the master volume in each playback thread. Playback threads
768 // assigned to HALs which do not have master volume support will apply
769 // master volume during the mix operation. Threads with HALs which do
770 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700771 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
772 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
773 continue;
774 }
John Grossmanee578c02012-07-23 17:05:46 -0700775 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700776 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777
778 return NO_ERROR;
779}
780
Glenn Kastenf78aee72012-01-04 11:00:47 -0800781status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782{
Eric Laurenta1884f92011-08-23 08:25:03 -0700783 status_t ret = initCheck();
784 if (ret != NO_ERROR) {
785 return ret;
786 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700787
788 // check calling permissions
789 if (!settingsAllowed()) {
790 return PERMISSION_DENIED;
791 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800792 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000793 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794 return BAD_VALUE;
795 }
796
797 { // scope for the lock
798 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700799 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700801 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802 mHardwareStatus = AUDIO_HW_IDLE;
803 }
804
805 if (NO_ERROR == ret) {
806 Mutex::Autolock _l(mLock);
807 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800808 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700809 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700810 }
811
812 return ret;
813}
814
815status_t AudioFlinger::setMicMute(bool state)
816{
Eric Laurenta1884f92011-08-23 08:25:03 -0700817 status_t ret = initCheck();
818 if (ret != NO_ERROR) {
819 return ret;
820 }
821
Mathias Agopian65ab4712010-07-14 17:59:35 -0700822 // check calling permissions
823 if (!settingsAllowed()) {
824 return PERMISSION_DENIED;
825 }
826
827 AutoMutex lock(mHardwareLock);
828 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700829 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
830 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
831 status_t result = dev->set_mic_mute(dev, state);
832 if (result != NO_ERROR) {
833 ret = result;
834 }
835 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700836 mHardwareStatus = AUDIO_HW_IDLE;
837 return ret;
838}
839
840bool AudioFlinger::getMicMute() const
841{
Eric Laurenta1884f92011-08-23 08:25:03 -0700842 status_t ret = initCheck();
843 if (ret != NO_ERROR) {
844 return false;
845 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800846 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700847 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800848 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800850 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
851 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
852 status_t result = dev->get_mic_mute(dev, &state);
853 if (result == NO_ERROR) {
854 mute = mute && state;
855 }
856 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700857 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800858
859 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860}
861
862status_t AudioFlinger::setMasterMute(bool muted)
863{
John Grossmand8f178d2012-07-20 14:51:35 -0700864 status_t ret = initCheck();
865 if (ret != NO_ERROR) {
866 return ret;
867 }
868
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869 // check calling permissions
870 if (!settingsAllowed()) {
871 return PERMISSION_DENIED;
872 }
873
John Grossmanee578c02012-07-23 17:05:46 -0700874 Mutex::Autolock _l(mLock);
875 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700876
John Grossmanee578c02012-07-23 17:05:46 -0700877 // Set master mute in the HALs which support it.
878 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
879 AutoMutex lock(mHardwareLock);
880 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700881
John Grossmanee578c02012-07-23 17:05:46 -0700882 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
883 if (dev->canSetMasterMute()) {
884 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700885 }
John Grossmanee578c02012-07-23 17:05:46 -0700886 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700887 }
888
John Grossmanee578c02012-07-23 17:05:46 -0700889 // Now set the master mute in each playback thread. Playback threads
890 // assigned to HALs which do not have master mute support will apply master
891 // mute during the mix operation. Threads with HALs which do support master
892 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700893 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
894 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
895 continue;
896 }
John Grossmanee578c02012-07-23 17:05:46 -0700897 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700898 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899
900 return NO_ERROR;
901}
902
903float AudioFlinger::masterVolume() const
904{
Glenn Kasten98067102011-12-13 11:47:54 -0800905 Mutex::Autolock _l(mLock);
906 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907}
908
909bool AudioFlinger::masterMute() const
910{
Glenn Kasten98067102011-12-13 11:47:54 -0800911 Mutex::Autolock _l(mLock);
912 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913}
914
John Grossman4ff14ba2012-02-08 16:37:41 -0800915float AudioFlinger::masterVolume_l() const
916{
John Grossman4ff14ba2012-02-08 16:37:41 -0800917 return mMasterVolume;
918}
919
John Grossmand8f178d2012-07-20 14:51:35 -0700920bool AudioFlinger::masterMute_l() const
921{
John Grossmanee578c02012-07-23 17:05:46 -0700922 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700923}
924
Eric Laurent223fd5c2014-11-11 13:43:36 -0800925status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
926{
927 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
928 ALOGW("setStreamVolume() invalid stream %d", stream);
929 return BAD_VALUE;
930 }
931 pid_t caller = IPCThreadState::self()->getCallingPid();
932 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
933 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
934 return PERMISSION_DENIED;
935 }
936
937 return NO_ERROR;
938}
939
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800940status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
941 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942{
943 // check calling permissions
944 if (!settingsAllowed()) {
945 return PERMISSION_DENIED;
946 }
947
Eric Laurent223fd5c2014-11-11 13:43:36 -0800948 status_t status = checkStreamType(stream);
949 if (status != NO_ERROR) {
950 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800952 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953
954 AutoMutex lock(mLock);
955 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700956 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957 thread = checkPlaybackThread_l(output);
958 if (thread == NULL) {
959 return BAD_VALUE;
960 }
961 }
962
963 mStreamTypes[stream].volume = value;
964
965 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800966 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700967 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968 }
969 } else {
970 thread->setStreamVolume(stream, value);
971 }
972
973 return NO_ERROR;
974}
975
Glenn Kastenfff6d712012-01-12 16:38:12 -0800976status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977{
978 // check calling permissions
979 if (!settingsAllowed()) {
980 return PERMISSION_DENIED;
981 }
982
Eric Laurent223fd5c2014-11-11 13:43:36 -0800983 status_t status = checkStreamType(stream);
984 if (status != NO_ERROR) {
985 return status;
986 }
987 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
988
989 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000990 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 return BAD_VALUE;
992 }
993
Eric Laurent93575202011-01-18 18:39:02 -0800994 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700996 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700997 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998
999 return NO_ERROR;
1000}
1001
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001002float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001003{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001004 status_t status = checkStreamType(stream);
1005 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001006 return 0.0f;
1007 }
1008
1009 AutoMutex lock(mLock);
1010 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001011 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012 PlaybackThread *thread = checkPlaybackThread_l(output);
1013 if (thread == NULL) {
1014 return 0.0f;
1015 }
1016 volume = thread->streamVolume(stream);
1017 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001018 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001019 }
1020
1021 return volume;
1022}
1023
Glenn Kastenfff6d712012-01-12 16:38:12 -08001024bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001025{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001026 status_t status = checkStreamType(stream);
1027 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028 return true;
1029 }
1030
Glenn Kasten6637baa2012-01-09 09:40:36 -08001031 AutoMutex lock(mLock);
1032 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001033}
1034
Eric Laurent054d9d32015-04-24 08:48:48 -07001035
1036void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1037{
1038 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1039 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1040 }
1041}
1042
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001043status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001044{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001045 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1046 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001047
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048 // check calling permissions
1049 if (!settingsAllowed()) {
1050 return PERMISSION_DENIED;
1051 }
1052
Glenn Kasten142f5192014-03-25 17:44:59 -07001053 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1054 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001055 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001056 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001057 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001058 AutoMutex lock(mHardwareLock);
1059 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1060 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1061 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1062 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1063 final_result = result ?: final_result;
1064 }
1065 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001066 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001067 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1068 AudioParameter param = AudioParameter(keyValuePairs);
1069 String8 value;
1070 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001071 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1072 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001073 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1074 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001075 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001076 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1077 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001078 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001079 // suspend effects associated with those session IDs
1080 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001081 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001082 thread->setEffectSuspended(FX_IID_AEC,
1083 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001084 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001085 thread->setEffectSuspended(FX_IID_NS,
1086 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001087 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001088 }
1089 }
Eric Laurentbee53372011-08-29 12:42:48 -07001090 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001091 }
1092 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001093 String8 screenState;
1094 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1095 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001096 if (isOff != (AudioFlinger::mScreenState & 1)) {
1097 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001098 }
1099 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001100 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101 }
1102
1103 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1104 // and the thread is exited once the lock is released
1105 sp<ThreadBase> thread;
1106 {
1107 Mutex::Autolock _l(mLock);
1108 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001109 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001111 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001112 // indicate output device change to all input threads for pre processing
1113 AudioParameter param = AudioParameter(keyValuePairs);
1114 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001115 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1116 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001117 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001118 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001119 }
1120 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001121 if (thread != 0) {
1122 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123 }
1124 return BAD_VALUE;
1125}
1126
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001127String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001129 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1130 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001131
Eric Laurenta4c5a552012-03-29 10:12:40 -07001132 Mutex::Autolock _l(mLock);
1133
Glenn Kasten142f5192014-03-25 17:44:59 -07001134 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001135 String8 out_s8;
1136
Dima Zavin799a70e2011-04-18 16:57:27 -07001137 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001138 char *s;
1139 {
1140 AutoMutex lock(mHardwareLock);
1141 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001142 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001143 s = dev->get_parameters(dev, keys.string());
1144 mHardwareStatus = AUDIO_HW_IDLE;
1145 }
John Grossmanef7740b2012-02-09 11:28:36 -08001146 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001147 free(s);
1148 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001149 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001150 }
1151
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1153 if (playbackThread != NULL) {
1154 return playbackThread->getParameters(keys);
1155 }
1156 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1157 if (recordThread != NULL) {
1158 return recordThread->getParameters(keys);
1159 }
1160 return String8("");
1161}
1162
Glenn Kastendd8104c2012-07-02 12:42:44 -07001163size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1164 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001165{
Eric Laurenta1884f92011-08-23 08:25:03 -07001166 status_t ret = initCheck();
1167 if (ret != NO_ERROR) {
1168 return 0;
1169 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001170 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001171 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001172 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001173 return 0;
1174 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001175
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001176 AutoMutex lock(mHardwareLock);
1177 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001178 audio_config_t config, proposed;
1179 memset(&proposed, 0, sizeof(proposed));
1180 proposed.sample_rate = sampleRate;
1181 proposed.channel_mask = channelMask;
1182 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001183
John Grossmanee578c02012-07-23 17:05:46 -07001184 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001185 size_t frames;
1186 for (;;) {
1187 // Note: config is currently a const parameter for get_input_buffer_size()
1188 // but we use a copy from proposed in case config changes from the call.
1189 config = proposed;
1190 frames = dev->get_input_buffer_size(dev, &config);
1191 if (frames != 0) {
1192 break; // hal success, config is the result
1193 }
1194 // change one parameter of the configuration each iteration to a more "common" value
1195 // to see if the device will support it.
1196 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1197 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1198 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1199 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1200 } else {
1201 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1202 "format %#x, channelMask 0x%X",
1203 sampleRate, format, channelMask);
1204 break; // retries failed, break out of loop with frames == 0.
1205 }
1206 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001207 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001208 if (frames > 0 && config.sample_rate != sampleRate) {
1209 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1210 }
1211 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001212}
1213
Glenn Kasten5f972c02014-01-13 09:59:31 -08001214uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001215{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001216 Mutex::Autolock _l(mLock);
1217
1218 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1219 if (recordThread != NULL) {
1220 return recordThread->getInputFramesLost();
1221 }
1222 return 0;
1223}
1224
1225status_t AudioFlinger::setVoiceVolume(float value)
1226{
Eric Laurenta1884f92011-08-23 08:25:03 -07001227 status_t ret = initCheck();
1228 if (ret != NO_ERROR) {
1229 return ret;
1230 }
1231
Mathias Agopian65ab4712010-07-14 17:59:35 -07001232 // check calling permissions
1233 if (!settingsAllowed()) {
1234 return PERMISSION_DENIED;
1235 }
1236
1237 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001238 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001239 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001240 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001241 mHardwareStatus = AUDIO_HW_IDLE;
1242
1243 return ret;
1244}
1245
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001246status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001247 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001248{
1249 status_t status;
1250
1251 Mutex::Autolock _l(mLock);
1252
1253 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1254 if (playbackThread != NULL) {
1255 return playbackThread->getRenderPosition(halFrames, dspFrames);
1256 }
1257
1258 return BAD_VALUE;
1259}
1260
1261void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1262{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001264 if (client == 0) {
1265 return;
1266 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001267 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001268 {
1269 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001270 if (mNotificationClients.indexOfKey(pid) < 0) {
1271 sp<NotificationClient> notificationClient = new NotificationClient(this,
1272 client,
1273 pid);
1274 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275
Eric Laurent021cf962014-05-13 10:18:14 -07001276 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277
Marco Nelissen06b46062014-11-14 07:58:25 -08001278 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001279 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001280 }
1281 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001282
Eric Laurent021cf962014-05-13 10:18:14 -07001283 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001284 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001285 // the config change is always sent from playback or record threads to avoid deadlock
1286 // with AudioSystem::gLock
1287 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1288 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1289 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001290
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001291 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1292 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001293 }
1294}
1295
1296void AudioFlinger::removeNotificationClient(pid_t pid)
1297{
1298 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001299 {
1300 Mutex::Autolock _cl(mClientLock);
1301 mNotificationClients.removeItem(pid);
1302 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001303
Steve Block3856b092011-10-20 11:56:00 +01001304 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001305 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001306 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001307 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001308 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001309 ALOGV(" pid %d @ %d", ref->mPid, i);
1310 if (ref->mPid == pid) {
1311 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001312 mAudioSessionRefs.removeAt(i);
1313 delete ref;
1314 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001315 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001316 } else {
1317 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001318 }
1319 }
1320 if (removed) {
1321 purgeStaleEffects_l();
1322 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323}
1324
Eric Laurent73e26b62015-04-27 16:55:58 -07001325void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001326 const sp<AudioIoDescriptor>& ioDesc,
1327 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001328{
Eric Laurent021cf962014-05-13 10:18:14 -07001329 Mutex::Autolock _l(mClientLock);
1330 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001332 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1333 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1334 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001335 }
1336}
1337
Eric Laurent021cf962014-05-13 10:18:14 -07001338// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001339void AudioFlinger::removeClient_l(pid_t pid)
1340{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001341 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001342 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001343 mClients.removeItem(pid);
1344}
1345
Eric Laurent717e1282012-06-29 16:36:52 -07001346// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001347sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1348 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001349{
1350 sp<PlaybackThread> thread;
1351
1352 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1353 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1354 ALOG_ASSERT(thread == 0);
1355 thread = mPlaybackThreads.valueAt(i);
1356 }
1357 }
1358
1359 return thread;
1360}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001361
Mathias Agopian65ab4712010-07-14 17:59:35 -07001362
Mathias Agopian65ab4712010-07-14 17:59:35 -07001363
1364// ----------------------------------------------------------------------------
1365
1366AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1367 : RefBase(),
1368 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001369 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001370{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301371 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1372 heapSize *= 1024;
1373 if (!heapSize) {
1374 heapSize = kClientSharedHeapSizeBytes;
1375 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1376 // invalidated tracks
1377 if (!audioFlinger->isLowRamDevice()) {
1378 heapSize *= kClientSharedHeapSizeMultiplier;
1379 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001380 }
1381 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001382}
1383
Eric Laurent021cf962014-05-13 10:18:14 -07001384// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001385AudioFlinger::Client::~Client()
1386{
1387 mAudioFlinger->removeClient_l(mPid);
1388}
1389
Glenn Kasten435dbe62012-01-30 10:15:48 -08001390sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001391{
1392 return mMemoryDealer;
1393}
1394
1395// ----------------------------------------------------------------------------
1396
1397AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1398 const sp<IAudioFlingerClient>& client,
1399 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001400 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401{
1402}
1403
1404AudioFlinger::NotificationClient::~NotificationClient()
1405{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406}
1407
Glenn Kasten0f11b512014-01-31 16:18:54 -08001408void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409{
1410 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001411 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412}
1413
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414
1415// ----------------------------------------------------------------------------
1416
Jeff Brown893a5642013-08-16 20:19:26 -07001417static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1418 return audio_is_remote_submix_device(inDevice);
1419}
1420
Mathias Agopian65ab4712010-07-14 17:59:35 -07001421sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001422 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001423 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001424 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001425 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001426 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001427 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001428 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001429 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001430 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001431 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001432 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001433 sp<IMemory>& cblk,
1434 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435 status_t *status)
1436{
1437 sp<RecordThread::RecordTrack> recordTrack;
1438 sp<RecordHandle> recordHandle;
1439 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001441 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001442
Glenn Kastend776ac62014-05-07 09:16:09 -07001443 cblk.clear();
1444 buffers.clear();
1445
Marco Nelissendcb346b2015-09-09 10:47:29 -07001446 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1447 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001448 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001449 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1450 clientUid = callingUid;
1451 }
1452
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001454 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001455 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456 lStatus = PERMISSION_DENIED;
1457 goto Exit;
1458 }
1459
Glenn Kasten53b5d092014-02-05 10:00:23 -08001460 // further sample rate checks are performed by createRecordTrack_l()
1461 if (sampleRate == 0) {
1462 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1463 lStatus = BAD_VALUE;
1464 goto Exit;
1465 }
1466
Andy Hung6770c6f2015-04-07 13:43:36 -07001467 // we don't yet support anything other than linear PCM
1468 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001469 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001470 lStatus = BAD_VALUE;
1471 goto Exit;
1472 }
1473
Glenn Kasten53b5d092014-02-05 10:00:23 -08001474 // further channel mask checks are performed by createRecordTrack_l()
1475 if (!audio_is_input_channel(channelMask)) {
1476 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1477 lStatus = BAD_VALUE;
1478 goto Exit;
1479 }
1480
Glenn Kasten05997e22014-03-13 15:08:33 -07001481 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001483 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001484 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001485 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001486 lStatus = BAD_VALUE;
1487 goto Exit;
1488 }
1489
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001490 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001491 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001493 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001494 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1495 lStatus = BAD_VALUE;
1496 goto Exit;
1497 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498 lSessionId = *sessionId;
1499 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001500 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001501 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001502 if (sessionId != NULL) {
1503 *sessionId = lSessionId;
1504 }
1505 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001506 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001507
Glenn Kasten1879fff2012-07-11 15:36:59 -07001508 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001509 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001510 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001511 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001512
1513 if (lStatus == NO_ERROR) {
1514 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1515 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001516 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001517 if (chain != 0) {
1518 Mutex::Autolock _l(thread->mLock);
1519 thread->addEffectChain_l(chain);
1520 }
1521 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522 }
Glenn Kastene198c362013-08-13 09:13:36 -07001523
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001524 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001525 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001526 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001527 // Don't hold mClientLock when releasing the reference on the track as the
1528 // destructor will acquire it.
1529 {
1530 Mutex::Autolock _cl(mClientLock);
1531 client.clear();
1532 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001534 goto Exit;
1535 }
1536
Glenn Kastend776ac62014-05-07 09:16:09 -07001537 cblk = recordTrack->getCblk();
1538 buffers = recordTrack->getBuffers();
1539
Glenn Kasten2fc14732013-08-05 14:58:14 -07001540 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001541 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542
1543Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001544 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001545 return recordHandle;
1546}
1547
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001548
1549
Mathias Agopian65ab4712010-07-14 17:59:35 -07001550// ----------------------------------------------------------------------------
1551
Eric Laurenta4c5a552012-03-29 10:12:40 -07001552audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1553{
Eric Laurent44622db2014-08-01 19:00:33 -07001554 if (name == NULL) {
1555 return 0;
1556 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001557 if (!settingsAllowed()) {
1558 return 0;
1559 }
1560 Mutex::Autolock _l(mLock);
1561 return loadHwModule_l(name);
1562}
1563
1564// loadHwModule_l() must be called with AudioFlinger::mLock held
1565audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1566{
1567 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1568 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1569 ALOGW("loadHwModule() module %s already loaded", name);
1570 return mAudioHwDevs.keyAt(i);
1571 }
1572 }
1573
Eric Laurenta4c5a552012-03-29 10:12:40 -07001574 audio_hw_device_t *dev;
1575
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001576 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001577 if (rc) {
1578 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1579 return 0;
1580 }
1581
1582 mHardwareStatus = AUDIO_HW_INIT;
1583 rc = dev->init_check(dev);
1584 mHardwareStatus = AUDIO_HW_IDLE;
1585 if (rc) {
1586 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1587 return 0;
1588 }
1589
John Grossmanee578c02012-07-23 17:05:46 -07001590 // Check and cache this HAL's level of support for master mute and master
1591 // volume. If this is the first HAL opened, and it supports the get
1592 // methods, use the initial values provided by the HAL as the current
1593 // master mute and volume settings.
1594
1595 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1596 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001597 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001598
1599 if (0 == mAudioHwDevs.size()) {
1600 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1601 if (NULL != dev->get_master_volume) {
1602 float mv;
1603 if (OK == dev->get_master_volume(dev, &mv)) {
1604 mMasterVolume = mv;
1605 }
1606 }
1607
1608 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1609 if (NULL != dev->get_master_mute) {
1610 bool mm;
1611 if (OK == dev->get_master_mute(dev, &mm)) {
1612 mMasterMute = mm;
1613 }
1614 }
1615 }
1616
Eric Laurenta4c5a552012-03-29 10:12:40 -07001617 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001618 if ((NULL != dev->set_master_volume) &&
1619 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1620 flags = static_cast<AudioHwDevice::Flags>(flags |
1621 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1622 }
1623
1624 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1625 if ((NULL != dev->set_master_mute) &&
1626 (OK == dev->set_master_mute(dev, mMasterMute))) {
1627 flags = static_cast<AudioHwDevice::Flags>(flags |
1628 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1629 }
1630
Eric Laurenta4c5a552012-03-29 10:12:40 -07001631 mHardwareStatus = AUDIO_HW_IDLE;
1632 }
1633
Glenn Kasteneeecb982016-02-26 10:44:04 -08001634 audio_module_handle_t handle = nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001635 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001636
1637 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001638 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001639
1640 return handle;
1641
1642}
1643
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001644// ----------------------------------------------------------------------------
1645
Glenn Kasten3b16c762012-11-14 08:44:39 -08001646uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001647{
1648 Mutex::Autolock _l(mLock);
1649 PlaybackThread *thread = primaryPlaybackThread_l();
1650 return thread != NULL ? thread->sampleRate() : 0;
1651}
1652
Glenn Kastene33054e2012-11-14 12:54:39 -08001653size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001654{
1655 Mutex::Autolock _l(mLock);
1656 PlaybackThread *thread = primaryPlaybackThread_l();
1657 return thread != NULL ? thread->frameCountHAL() : 0;
1658}
1659
1660// ----------------------------------------------------------------------------
1661
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001662status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1663{
1664 uid_t uid = IPCThreadState::self()->getCallingUid();
1665 if (uid != AID_SYSTEM) {
1666 return PERMISSION_DENIED;
1667 }
1668 Mutex::Autolock _l(mLock);
1669 if (mIsDeviceTypeKnown) {
1670 return INVALID_OPERATION;
1671 }
1672 mIsLowRamDevice = isLowRamDevice;
1673 mIsDeviceTypeKnown = true;
1674 return NO_ERROR;
1675}
1676
Eric Laurent93c3d412014-08-01 14:48:35 -07001677audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1678{
1679 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001680
1681 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1682 if (index >= 0) {
1683 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1684 mHwAvSyncIds.valueAt(index), sessionId);
1685 return mHwAvSyncIds.valueAt(index);
1686 }
1687
1688 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1689 if (dev == NULL) {
1690 return AUDIO_HW_SYNC_INVALID;
1691 }
1692 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1693 AudioParameter param = AudioParameter(String8(reply));
1694 free(reply);
1695
1696 int value;
1697 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1698 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1699 return AUDIO_HW_SYNC_INVALID;
1700 }
1701
1702 // allow only one session for a given HW A/V sync ID.
1703 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1704 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1705 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1706 value, mHwAvSyncIds.keyAt(i));
1707 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001708 break;
1709 }
1710 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001711
1712 mHwAvSyncIds.add(sessionId, value);
1713
1714 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1715 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1716 uint32_t sessions = thread->hasAudioSession(sessionId);
1717 if (sessions & PlaybackThread::TRACK_SESSION) {
1718 AudioParameter param = AudioParameter();
1719 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1720 thread->setParameters(param.toString());
1721 break;
1722 }
1723 }
1724
1725 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1726 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001727}
1728
Eric Laurent72e3f392015-05-20 14:43:50 -07001729status_t AudioFlinger::systemReady()
1730{
1731 Mutex::Autolock _l(mLock);
1732 ALOGI("%s", __FUNCTION__);
1733 if (mSystemReady) {
1734 ALOGW("%s called twice", __FUNCTION__);
1735 return NO_ERROR;
1736 }
1737 mSystemReady = true;
1738 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1739 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1740 thread->systemReady();
1741 }
1742 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1743 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1744 thread->systemReady();
1745 }
1746 return NO_ERROR;
1747}
1748
Eric Laurentfa90e842014-10-17 18:12:31 -07001749// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1750void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1751{
1752 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1753 if (index >= 0) {
1754 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1755 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1756 AudioParameter param = AudioParameter();
1757 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1758 thread->setParameters(param.toString());
1759 }
1760}
1761
1762
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001763// ----------------------------------------------------------------------------
1764
Eric Laurent83b88082014-06-20 18:31:16 -07001765
1766sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001767 audio_io_handle_t *output,
1768 audio_config_t *config,
1769 audio_devices_t devices,
1770 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001771 audio_output_flags_t flags)
1772{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001773 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001774 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001775 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001776 }
1777
1778 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Glenn Kasteneeecb982016-02-26 10:44:04 -08001779
Eric Laurentcf2c0212014-07-25 16:20:43 -07001780 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001781 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1782 } else {
1783 // Audio Policy does not currently request a specific output handle.
1784 // If this is ever needed, see openInput_l() for example code.
1785 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1786 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001787 }
Eric Laurent83b88082014-06-20 18:31:16 -07001788
1789 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1790
Eric Laurent83b88082014-06-20 18:31:16 -07001791 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001792 // This if statement allows overriding the audio policy settings
1793 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1794 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1795 // Check only for Normal Mixing mode
1796 if (kEnableExtendedPrecision) {
1797 // Specify format (uncomment one below to choose)
1798 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1799 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1800 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1801 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001802 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001803 }
1804 if (kEnableExtendedChannels) {
1805 // Specify channel mask (uncomment one below to choose)
1806 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1807 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1808 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1809 }
Eric Laurent83b88082014-06-20 18:31:16 -07001810 }
1811
Phil Burk062e67a2015-02-11 13:40:50 -08001812 AudioStreamOut *outputStream = NULL;
1813 status_t status = outHwDev->openOutputStream(
1814 &outputStream,
1815 *output,
1816 devices,
1817 flags,
1818 config,
1819 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001820
1821 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001822
Phil Burk062e67a2015-02-11 13:40:50 -08001823 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001824
1825 PlaybackThread *thread;
1826 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurent51716182016-02-29 18:00:56 -08001827 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady,
1828 config->offload_info.bit_rate);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001829 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001830 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1831 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001832 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001833 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001834 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001835 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001836 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001837 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001838 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001839 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001840 return thread;
1841 }
1842
1843 return 0;
1844}
1845
Eric Laurentcf2c0212014-07-25 16:20:43 -07001846status_t AudioFlinger::openOutput(audio_module_handle_t module,
1847 audio_io_handle_t *output,
1848 audio_config_t *config,
1849 audio_devices_t *devices,
1850 const String8& address,
1851 uint32_t *latencyMs,
1852 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001853{
Phil Burk062e67a2015-02-11 13:40:50 -08001854 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001855 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001856 (devices != NULL) ? *devices : 0,
1857 config->sample_rate,
1858 config->format,
1859 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001860 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001861
Eric Laurentcf2c0212014-07-25 16:20:43 -07001862 if (*devices == AUDIO_DEVICE_NONE) {
1863 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001864 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001865
Mathias Agopian65ab4712010-07-14 17:59:35 -07001866 Mutex::Autolock _l(mLock);
1867
Eric Laurentcf2c0212014-07-25 16:20:43 -07001868 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001869 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001870 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001871
1872 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001873 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001874
1875 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001876 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001877 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001878 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001879
1880 AutoMutex lock(mHardwareLock);
1881 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001882 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001883 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001884 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001885 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001886 }
1887
Eric Laurentcf2c0212014-07-25 16:20:43 -07001888 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001889}
1890
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001891audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1892 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001893{
1894 Mutex::Autolock _l(mLock);
1895 MixerThread *thread1 = checkMixerThread_l(output1);
1896 MixerThread *thread2 = checkMixerThread_l(output2);
1897
1898 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001899 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1900 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001901 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001902 }
1903
Glenn Kasteneeecb982016-02-26 10:44:04 -08001904 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07001905 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001906 thread->addOutputTrack(thread2);
1907 mPlaybackThreads.add(id, thread);
1908 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001909 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001910 return id;
1911}
1912
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001913status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001914{
Glenn Kastend96c5722012-04-25 13:44:49 -07001915 return closeOutput_nonvirtual(output);
1916}
1917
1918status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1919{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001920 // keep strong reference on the playback thread so that
1921 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001922 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923 {
1924 Mutex::Autolock _l(mLock);
1925 thread = checkPlaybackThread_l(output);
1926 if (thread == NULL) {
1927 return BAD_VALUE;
1928 }
1929
Steve Block3856b092011-10-20 11:56:00 +01001930 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001931
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001932 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001934 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001935 DuplicatingThread *dupThread =
1936 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001938 }
1939 }
1940 }
1941
1942
1943 mPlaybackThreads.removeItem(output);
1944 // save all effects to the default thread
1945 if (mPlaybackThreads.size()) {
1946 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1947 if (dstThread != NULL) {
1948 // audioflinger lock is held here so the acquisition order of thread locks does not
1949 // matter
1950 Mutex::Autolock _dl(dstThread->mLock);
1951 Mutex::Autolock _sl(thread->mLock);
1952 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1953 for (size_t i = 0; i < effectChains.size(); i ++) {
1954 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955 }
1956 }
1957 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001958 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1959 ioDesc->mIoHandle = output;
1960 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001961 }
1962 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001963 // The thread entity (active unit of execution) is no longer running here,
1964 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001965
Eric Laurentf6870ae2015-05-08 10:50:03 -07001966 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001967 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968 }
Eric Laurent83b88082014-06-20 18:31:16 -07001969
Mathias Agopian65ab4712010-07-14 17:59:35 -07001970 return NO_ERROR;
1971}
1972
Eric Laurent83b88082014-06-20 18:31:16 -07001973void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1974{
1975 AudioStreamOut *out = thread->clearOutput();
1976 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1977 // from now on thread->mOutput is NULL
1978 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1979 delete out;
1980}
1981
1982void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1983{
1984 mPlaybackThreads.removeItem(thread->mId);
1985 thread->exit();
1986 closeOutputFinish(thread);
1987}
1988
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001989status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001990{
1991 Mutex::Autolock _l(mLock);
1992 PlaybackThread *thread = checkPlaybackThread_l(output);
1993
1994 if (thread == NULL) {
1995 return BAD_VALUE;
1996 }
1997
Steve Block3856b092011-10-20 11:56:00 +01001998 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001999 thread->suspend();
2000
2001 return NO_ERROR;
2002}
2003
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002004status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002005{
2006 Mutex::Autolock _l(mLock);
2007 PlaybackThread *thread = checkPlaybackThread_l(output);
2008
2009 if (thread == NULL) {
2010 return BAD_VALUE;
2011 }
2012
Steve Block3856b092011-10-20 11:56:00 +01002013 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002014
2015 thread->restore();
2016
2017 return NO_ERROR;
2018}
2019
Eric Laurentcf2c0212014-07-25 16:20:43 -07002020status_t AudioFlinger::openInput(audio_module_handle_t module,
2021 audio_io_handle_t *input,
2022 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002023 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002024 const String8& address,
2025 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002026 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002027{
Eric Laurent83b88082014-06-20 18:31:16 -07002028 Mutex::Autolock _l(mLock);
2029
Glenn Kastene7d66712015-03-05 13:46:52 -08002030 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002031 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002032 }
2033
Glenn Kastene7d66712015-03-05 13:46:52 -08002034 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002035
2036 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002037 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002038 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002039 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002040 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002041 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002042}
Dima Zavin799a70e2011-04-18 16:57:27 -07002043
Eric Laurent83b88082014-06-20 18:31:16 -07002044sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002045 audio_io_handle_t *input,
2046 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002047 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002048 const String8& address,
2049 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002050 audio_input_flags_t flags)
2051{
Glenn Kastene7d66712015-03-05 13:46:52 -08002052 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002053 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002054 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002055 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002056 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002057
Glenn Kasteneeecb982016-02-26 10:44:04 -08002058 // Audio Policy can request a specific handle for hardware hotword.
2059 // The goal here is not to re-open an already opened input.
2060 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002061 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002062 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2063 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2064 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2065 return 0;
2066 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2067 // This should not happen in a transient state with current design.
2068 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2069 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002070 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002071
Eric Laurentcf2c0212014-07-25 16:20:43 -07002072 audio_config_t halconfig = *config;
2073 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002074 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002075 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002076 &inStream, flags, address.string(), source);
2077 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002078 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002079 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002080 halconfig.sample_rate,
2081 halconfig.format,
2082 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002083 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002084 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002085
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002086 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002087 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002088 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002089 audio_is_linear_pcm(config->format) &&
2090 audio_is_linear_pcm(halconfig.format) &&
2091 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002092 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2093 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002094 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002095 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002096 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002097 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002098 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002099 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002100 }
2101
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002102 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002103
Glenn Kasten46909e72013-02-26 09:20:22 -08002104#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002105 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2106 // or (re-)create if current Pipe is idle and does not match the new format
2107 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002108 enum {
2109 TEE_SINK_NO, // don't copy input
2110 TEE_SINK_NEW, // copy input using a new pipe
2111 TEE_SINK_OLD, // copy input using an existing pipe
2112 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002113 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2114 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002115 if (!mTeeSinkInputEnabled) {
2116 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002117 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002118 kind = TEE_SINK_NO;
2119 } else if (mRecordTeeSink == 0) {
2120 kind = TEE_SINK_NEW;
2121 } else if (mRecordTeeSink->getStrongCount() != 1) {
2122 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002123 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002124 kind = TEE_SINK_OLD;
2125 } else {
2126 kind = TEE_SINK_NEW;
2127 }
2128 switch (kind) {
2129 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002130 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002131 size_t numCounterOffers = 0;
2132 const NBAIO_Format offers[1] = {format};
2133 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2134 ALOG_ASSERT(index == 0);
2135 PipeReader *pipeReader = new PipeReader(*pipe);
2136 numCounterOffers = 0;
2137 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2138 ALOG_ASSERT(index == 0);
2139 mRecordTeeSink = pipe;
2140 mRecordTeeSource = pipeReader;
2141 teeSink = pipe;
2142 }
2143 break;
2144 case TEE_SINK_OLD:
2145 teeSink = mRecordTeeSink;
2146 break;
2147 case TEE_SINK_NO:
2148 default:
2149 break;
2150 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002151#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002152
Eric Laurentcf2c0212014-07-25 16:20:43 -07002153 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002154
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002155 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002156 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002157 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002158 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002159 inputStream,
2160 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002161 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002162 devices,
2163 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002164#ifdef TEE_SINK
2165 , teeSink
2166#endif
2167 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002168 mRecordThreads.add(*input, thread);
2169 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002170 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002171 }
2172
Eric Laurentcf2c0212014-07-25 16:20:43 -07002173 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002174 return 0;
2175}
2176
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002177status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002178{
Glenn Kastend96c5722012-04-25 13:44:49 -07002179 return closeInput_nonvirtual(input);
2180}
2181
2182status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2183{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184 // keep strong reference on the record thread so that
2185 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002186 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002187 {
2188 Mutex::Autolock _l(mLock);
2189 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002190 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002191 return BAD_VALUE;
2192 }
2193
Steve Block3856b092011-10-20 11:56:00 +01002194 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002195
2196 // If we still have effect chains, it means that a client still holds a handle
2197 // on at least one effect. We must either move the chain to an existing thread with the
2198 // same session ID or put it aside in case a new record thread is opened for a
2199 // new capture on the same session
2200 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002201 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002202 Mutex::Autolock _sl(thread->mLock);
2203 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002204 // Note: maximum one chain per record thread
2205 if (effectChains.size() != 0) {
2206 chain = effectChains[0];
2207 }
2208 }
2209 if (chain != 0) {
2210 // first check if a record thread is already opened with a client on the same session.
2211 // This should only happen in case of overlap between one thread tear down and the
2212 // creation of its replacement
2213 size_t i;
2214 for (i = 0; i < mRecordThreads.size(); i++) {
2215 sp<RecordThread> t = mRecordThreads.valueAt(i);
2216 if (t == thread) {
2217 continue;
2218 }
2219 if (t->hasAudioSession(chain->sessionId()) != 0) {
2220 Mutex::Autolock _l(t->mLock);
2221 ALOGV("closeInput() found thread %d for effect session %d",
2222 t->id(), chain->sessionId());
2223 t->addEffectChain_l(chain);
2224 break;
2225 }
2226 }
2227 // put the chain aside if we could not find a record thread with the same session id.
2228 if (i == mRecordThreads.size()) {
2229 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002230 }
2231 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002232 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2233 ioDesc->mIoHandle = input;
2234 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002235 mRecordThreads.removeItem(input);
2236 }
Eric Laurent83b88082014-06-20 18:31:16 -07002237 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2238 // we have a different lock for notification client
2239 closeInputFinish(thread);
2240 return NO_ERROR;
2241}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002242
Eric Laurent83b88082014-06-20 18:31:16 -07002243void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2244{
2245 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002246 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002247 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002248 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002249 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002250 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002251}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002252
Eric Laurent83b88082014-06-20 18:31:16 -07002253void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2254{
2255 mRecordThreads.removeItem(thread->mId);
2256 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002257}
2258
Glenn Kastend2304db2014-02-03 07:40:31 -08002259status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002260{
2261 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002262 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002263
2264 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2265 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002266 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002267 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002268
2269 return NO_ERROR;
2270}
2271
2272
Glenn Kasteneeecb982016-02-26 10:44:04 -08002273audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002274{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002275 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002276}
2277
Glenn Kastend848eb42016-03-08 13:42:11 -08002278void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002279{
2280 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002281 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002282 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2283 if (pid != -1 && (caller == getpid_cached)) {
2284 caller = pid;
2285 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002286
Eric Laurent021cf962014-05-13 10:18:14 -07002287 {
2288 Mutex::Autolock _cl(mClientLock);
2289 // Ignore requests received from processes not known as notification client. The request
2290 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2291 // called from a different pid leaving a stale session reference. Also we don't know how
2292 // to clear this reference if the client process dies.
2293 if (mNotificationClients.indexOfKey(caller) < 0) {
2294 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2295 return;
2296 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002297 }
2298
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002299 size_t num = mAudioSessionRefs.size();
2300 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002301 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002302 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2303 ref->mCnt++;
2304 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002305 return;
2306 }
2307 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002308 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2309 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002310}
2311
Glenn Kastend848eb42016-03-08 13:42:11 -08002312void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002313{
2314 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002315 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002316 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2317 if (pid != -1 && (caller == getpid_cached)) {
2318 caller = pid;
2319 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002320 size_t num = mAudioSessionRefs.size();
2321 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002322 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002323 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2324 ref->mCnt--;
2325 ALOGV(" decremented refcount to %d", ref->mCnt);
2326 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002327 mAudioSessionRefs.removeAt(i);
2328 delete ref;
2329 purgeStaleEffects_l();
2330 }
2331 return;
2332 }
2333 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002334 // If the caller is mediaserver it is likely that the session being released was acquired
2335 // on behalf of a process not in notification clients and we ignore the warning.
2336 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002337}
2338
2339void AudioFlinger::purgeStaleEffects_l() {
2340
Steve Block3856b092011-10-20 11:56:00 +01002341 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002342
2343 Vector< sp<EffectChain> > chains;
2344
2345 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2346 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2347 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2348 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002349 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2350 chains.push(ec);
2351 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002352 }
2353 }
2354 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2355 sp<RecordThread> t = mRecordThreads.valueAt(i);
2356 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2357 sp<EffectChain> ec = t->mEffectChains[j];
2358 chains.push(ec);
2359 }
2360 }
2361
2362 for (size_t i = 0; i < chains.size(); i++) {
2363 sp<EffectChain> ec = chains[i];
2364 int sessionid = ec->sessionId();
2365 sp<ThreadBase> t = ec->mThread.promote();
2366 if (t == 0) {
2367 continue;
2368 }
2369 size_t numsessionrefs = mAudioSessionRefs.size();
2370 bool found = false;
2371 for (size_t k = 0; k < numsessionrefs; k++) {
2372 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002373 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002374 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002375 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002376 found = true;
2377 break;
2378 }
2379 }
2380 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002381 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002382 // remove all effects from the chain
2383 while (ec->mEffects.size()) {
2384 sp<EffectModule> effect = ec->mEffects[0];
2385 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002386 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002387 if (effect->purgeHandles()) {
2388 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002389 }
2390 AudioSystem::unregisterEffect(effect->id());
2391 }
2392 }
2393 }
2394 return;
2395}
2396
Glenn Kasteneeecb982016-02-26 10:44:04 -08002397// checkThread_l() must be called with AudioFlinger::mLock held
2398AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2399{
2400 ThreadBase *thread = NULL;
2401 switch (audio_unique_id_get_use(ioHandle)) {
2402 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2403 thread = checkPlaybackThread_l(ioHandle);
2404 break;
2405 case AUDIO_UNIQUE_ID_USE_INPUT:
2406 thread = checkRecordThread_l(ioHandle);
2407 break;
2408 default:
2409 break;
2410 }
2411 return thread;
2412}
2413
Mathias Agopian65ab4712010-07-14 17:59:35 -07002414// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002415AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002416{
Glenn Kastena1117922012-01-26 10:53:32 -08002417 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002418}
2419
2420// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002421AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002422{
2423 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002424 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002425}
2426
2427// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002428AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002429{
Glenn Kastena1117922012-01-26 10:53:32 -08002430 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002431}
2432
Glenn Kasteneeecb982016-02-26 10:44:04 -08002433audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002434{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002435 int32_t base = android_atomic_add(AUDIO_UNIQUE_ID_USE_MAX, &mNextUniqueId);
2436 // We have no way of recovering from wraparound
2437 LOG_ALWAYS_FATAL_IF(base == 0, "unique ID overflow");
2438 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
2439 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2440 return (audio_unique_id_t) (base | use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002441}
2442
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002443AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002444{
2445 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2446 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002447 if(thread->isDuplicating()) {
2448 continue;
2449 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002450 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002451 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002452 return thread;
2453 }
2454 }
2455 return NULL;
2456}
2457
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002458audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002459{
2460 PlaybackThread *thread = primaryPlaybackThread_l();
2461
2462 if (thread == NULL) {
2463 return 0;
2464 }
2465
Eric Laurentf1c04f92012-08-28 14:26:53 -07002466 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002467}
2468
Eric Laurenta011e352012-03-29 15:51:43 -07002469sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002470 audio_session_t triggerSession,
2471 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002472 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002473 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002474{
2475 Mutex::Autolock _l(mLock);
2476
2477 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2478 status_t playStatus = NAME_NOT_FOUND;
2479 status_t recStatus = NAME_NOT_FOUND;
2480 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2481 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2482 if (playStatus == NO_ERROR) {
2483 return event;
2484 }
2485 }
2486 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2487 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2488 if (recStatus == NO_ERROR) {
2489 return event;
2490 }
2491 }
2492 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2493 mPendingSyncEvents.add(event);
2494 } else {
2495 ALOGV("createSyncEvent() invalid event %d", event->type());
2496 event.clear();
2497 }
2498 return event;
2499}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002500
Mathias Agopian65ab4712010-07-14 17:59:35 -07002501// ----------------------------------------------------------------------------
2502// Effect management
2503// ----------------------------------------------------------------------------
2504
2505
Glenn Kastenf587ba52012-01-26 16:25:10 -08002506status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002507{
2508 Mutex::Autolock _l(mLock);
2509 return EffectQueryNumberEffects(numEffects);
2510}
2511
Glenn Kastenf587ba52012-01-26 16:25:10 -08002512status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002513{
2514 Mutex::Autolock _l(mLock);
2515 return EffectQueryEffect(index, descriptor);
2516}
2517
Glenn Kasten5e92a782012-01-30 07:40:52 -08002518status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002519 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002520{
2521 Mutex::Autolock _l(mLock);
2522 return EffectGetDescriptor(pUuid, descriptor);
2523}
2524
2525
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002526sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002527 effect_descriptor_t *pDesc,
2528 const sp<IEffectClient>& effectClient,
2529 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002530 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002531 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002532 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002533 status_t *status,
2534 int *id,
2535 int *enabled)
2536{
2537 status_t lStatus = NO_ERROR;
2538 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002539 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002540
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002541 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002542 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002543 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002544
2545 if (pDesc == NULL) {
2546 lStatus = BAD_VALUE;
2547 goto Exit;
2548 }
2549
Eric Laurent84e9a102010-09-23 16:10:16 -07002550 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002551 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002552 lStatus = PERMISSION_DENIED;
2553 goto Exit;
2554 }
2555
Dima Zavinfce7a472011-04-19 22:30:36 -07002556 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002557 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002558 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002559 lStatus = PERMISSION_DENIED;
2560 goto Exit;
2561 }
2562
Mathias Agopian65ab4712010-07-14 17:59:35 -07002563 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002564 if (!EffectIsNullUuid(&pDesc->uuid)) {
2565 // if uuid is specified, request effect descriptor
2566 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2567 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002568 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002569 goto Exit;
2570 }
2571 } else {
2572 // if uuid is not specified, look for an available implementation
2573 // of the required type in effect factory
2574 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002575 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002576 lStatus = BAD_VALUE;
2577 goto Exit;
2578 }
2579 uint32_t numEffects = 0;
2580 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002581 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002582 bool found = false;
2583
2584 lStatus = EffectQueryNumberEffects(&numEffects);
2585 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002586 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002587 goto Exit;
2588 }
2589 for (uint32_t i = 0; i < numEffects; i++) {
2590 lStatus = EffectQueryEffect(i, &desc);
2591 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002592 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002593 continue;
2594 }
2595 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2596 // If matching type found save effect descriptor. If the session is
2597 // 0 and the effect is not auxiliary, continue enumeration in case
2598 // an auxiliary version of this effect type is available
2599 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002600 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002601 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002602 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2603 break;
2604 }
2605 }
2606 }
2607 if (!found) {
2608 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002609 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002610 goto Exit;
2611 }
2612 // For same effect type, chose auxiliary version over insert version if
2613 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002614 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002615 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002616 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002617 }
2618 }
2619
2620 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002621 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002622 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2623 lStatus = INVALID_OPERATION;
2624 goto Exit;
2625 }
2626
Eric Laurent59255e42011-07-27 19:49:51 -07002627 // check recording permission for visualizer
2628 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002629 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002630 lStatus = PERMISSION_DENIED;
2631 goto Exit;
2632 }
2633
Mathias Agopian65ab4712010-07-14 17:59:35 -07002634 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002635 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002636 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002637 // if the output returned by getOutputForEffect() is removed before we lock the
2638 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2639 // and we will exit safely
2640 io = AudioSystem::getOutputForEffect(&desc);
2641 ALOGV("createEffect got output %d", io);
2642 }
2643
2644 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002645
2646 // If output is not specified try to find a matching audio session ID in one of the
2647 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002648 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2649 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002650 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002651 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002652 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2653 // output must be specified by AudioPolicyManager when using session
2654 // AUDIO_SESSION_OUTPUT_STAGE
2655 lStatus = BAD_VALUE;
2656 goto Exit;
2657 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002658 // look for the thread where the specified audio session is present
2659 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2660 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2661 io = mPlaybackThreads.keyAt(i);
2662 break;
2663 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002664 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002665 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002666 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2667 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2668 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002669 break;
2670 }
2671 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002672 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002673 // If no output thread contains the requested session ID, default to
2674 // first output. The effect chain will be moved to the correct output
2675 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002676 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002677 io = mPlaybackThreads.keyAt(0);
2678 }
Steve Block3856b092011-10-20 11:56:00 +01002679 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002680 }
2681 ThreadBase *thread = checkRecordThread_l(io);
2682 if (thread == NULL) {
2683 thread = checkPlaybackThread_l(io);
2684 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002685 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002686 lStatus = BAD_VALUE;
2687 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002688 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002689 } else {
2690 // Check if one effect chain was awaiting for an effect to be created on this
2691 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08002692 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07002693 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002694 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002695 thread->addEffectChain_l(chain);
2696 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002697 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002698
Eric Laurent021cf962014-05-13 10:18:14 -07002699 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002700
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002701 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002702 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2703 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002704 if (handle != 0 && id != NULL) {
2705 *id = handle->id();
2706 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002707 if (handle == 0) {
2708 // remove local strong reference to Client with mClientLock held
2709 Mutex::Autolock _cl(mClientLock);
2710 client.clear();
2711 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002712 }
2713
2714Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002715 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002716 return handle;
2717}
2718
Glenn Kastend848eb42016-03-08 13:42:11 -08002719status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002720 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002721{
Steve Block3856b092011-10-20 11:56:00 +01002722 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002723 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002724 Mutex::Autolock _l(mLock);
2725 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002726 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002727 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002728 }
Eric Laurentde070132010-07-13 04:45:46 -07002729 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2730 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002731 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002732 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002733 }
Eric Laurentde070132010-07-13 04:45:46 -07002734 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2735 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002736 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002737 return BAD_VALUE;
2738 }
2739
2740 Mutex::Autolock _dl(dstThread->mLock);
2741 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002742 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002743}
2744
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002745// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08002746status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002747 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002748 AudioFlinger::PlaybackThread *dstThread,
2749 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002750{
Steve Block3856b092011-10-20 11:56:00 +01002751 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002752 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002753
Eric Laurent59255e42011-07-27 19:49:51 -07002754 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002755 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002756 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002757 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002758 return INVALID_OPERATION;
2759 }
2760
Andy Hung9a592762014-07-21 21:56:01 -07002761 // Check whether the destination thread has a channel count of FCC_2, which is
2762 // currently required for (most) effects. Prevent moving the effect chain here rather
2763 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentf6870ae2015-05-08 10:50:03 -07002764 if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
Eric Laurentb10352f2014-11-03 16:25:39 -08002765 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002766 ALOGW("moveEffectChain_l() effect chain failed because"
2767 " destination thread %p channel count(%u) != %u",
2768 dstThread, dstThread->mChannelCount, FCC_2);
2769 return INVALID_OPERATION;
2770 }
2771
Eric Laurent39e94f82010-07-28 01:32:47 -07002772 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002773 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002774 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002775 // removed.
2776 srcThread->removeEffectChain_l(chain);
2777
2778 // transfer all effects one by one so that new effect chain is created on new thread with
2779 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002780 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002781 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002782 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002783 Vector< sp<EffectModule> > removed;
2784 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002785 while (effect != 0) {
2786 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002787 removed.add(effect);
2788 status = dstThread->addEffect_l(effect);
2789 if (status != NO_ERROR) {
2790 break;
2791 }
Eric Laurentec35a142011-10-05 17:42:25 -07002792 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2793 if (effect->state() == EffectModule::ACTIVE ||
2794 effect->state() == EffectModule::STOPPING) {
2795 effect->start();
2796 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002797 // if the move request is not received from audio policy manager, the effect must be
2798 // re-registered with the new strategy and output
2799 if (dstChain == 0) {
2800 dstChain = effect->chain().promote();
2801 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002802 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002803 status = NO_INIT;
2804 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002805 }
2806 strategy = dstChain->strategy();
2807 }
2808 if (reRegister) {
2809 AudioSystem::unregisterEffect(effect->id());
2810 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002811 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002812 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002813 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002814 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002815 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002816 }
Eric Laurentde070132010-07-13 04:45:46 -07002817 effect = chain->getEffectFromId_l(0);
2818 }
2819
Eric Laurent5baf2af2013-09-12 17:37:00 -07002820 if (status != NO_ERROR) {
2821 for (size_t i = 0; i < removed.size(); i++) {
2822 srcThread->addEffect_l(removed[i]);
2823 if (dstChain != 0 && reRegister) {
2824 AudioSystem::unregisterEffect(removed[i]->id());
2825 AudioSystem::registerEffect(&removed[i]->desc(),
2826 srcThread->id(),
2827 strategy,
2828 sessionId,
2829 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002830 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002831 }
2832 }
2833 }
2834
2835 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002836}
2837
Eric Laurent5baf2af2013-09-12 17:37:00 -07002838bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002839{
2840 if (mGlobalEffectEnableTime != 0 &&
2841 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2842 return true;
2843 }
2844
2845 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2846 sp<EffectChain> ec =
2847 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002848 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002849 return true;
2850 }
2851 }
2852 return false;
2853}
2854
Eric Laurent5baf2af2013-09-12 17:37:00 -07002855void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002856{
2857 Mutex::Autolock _l(mLock);
2858
2859 mGlobalEffectEnableTime = systemTime();
2860
2861 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2862 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2863 if (t->mType == ThreadBase::OFFLOAD) {
2864 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2865 }
2866 }
2867
2868}
2869
Eric Laurentaaa44472014-09-12 17:41:50 -07002870status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2871{
Glenn Kastend848eb42016-03-08 13:42:11 -08002872 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002873 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2874 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2875 if (index >= 0) {
2876 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2877 return ALREADY_EXISTS;
2878 }
2879 mOrphanEffectChains.add(session, chain);
2880 return NO_ERROR;
2881}
2882
2883sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2884{
2885 sp<EffectChain> chain;
2886 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2887 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2888 if (index >= 0) {
2889 chain = mOrphanEffectChains.valueAt(index);
2890 mOrphanEffectChains.removeItemsAt(index);
2891 }
2892 return chain;
2893}
2894
2895bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2896{
2897 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08002898 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002899 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2900 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2901 if (index >= 0) {
2902 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2903 if (chain->removeEffect_l(effect) == 0) {
2904 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2905 mOrphanEffectChains.removeItemsAt(index);
2906 }
2907 return true;
2908 }
2909 return false;
2910}
2911
2912
Glenn Kastenda6ef132013-01-10 12:31:01 -08002913struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002914#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2915 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002916};
2917
2918int comparEntry(const void *p1, const void *p2)
2919{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002920 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002921}
2922
Glenn Kasten46909e72013-02-26 09:20:22 -08002923#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002924void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002925{
Eric Laurent81784c32012-11-19 14:55:58 -08002926 NBAIO_Source *teeSource = source.get();
2927 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002928 // .wav rotation
2929 // There is a benign race condition if 2 threads call this simultaneously.
2930 // They would both traverse the directory, but the result would simply be
2931 // failures at unlink() which are ignored. It's also unlikely since
2932 // normally dumpsys is only done by bugreport or from the command line.
2933 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08002934 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08002935 size_t teePathLen = strlen(teePath);
2936 DIR *dir = opendir(teePath);
2937 teePath[teePathLen++] = '/';
2938 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002939#define TEE_MAX_SORT 20 // number of entries to sort
2940#define TEE_MAX_KEEP 10 // number of entries to keep
2941 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002942 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002943 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002944 struct dirent de;
2945 struct dirent *result = NULL;
2946 int rc = readdir_r(dir, &de, &result);
2947 if (rc != 0) {
2948 ALOGW("readdir_r failed %d", rc);
2949 break;
2950 }
2951 if (result == NULL) {
2952 break;
2953 }
2954 if (result != &de) {
2955 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2956 break;
2957 }
2958 // ignore non .wav file entries
2959 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002960 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08002961 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2962 continue;
2963 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08002964 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002965 }
2966 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002967 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002968 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002969 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
2970 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002971 (void) unlink(teePath);
2972 }
2973 }
2974 } else {
2975 if (fd >= 0) {
Glenn Kasten9a003992016-02-23 15:24:34 -08002976 dprintf(fd, "unable to rotate tees in %.*s: %s\n", teePathLen, teePath,
2977 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002978 }
2979 }
Eric Laurent81784c32012-11-19 14:55:58 -08002980 char teeTime[16];
2981 struct timeval tv;
2982 gettimeofday(&tv, NULL);
2983 struct tm tm;
2984 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002985 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2986 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2987 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2988 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002989 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002990 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002991 char wavHeader[44];
2992 memcpy(wavHeader,
2993 "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",
2994 sizeof(wavHeader));
2995 NBAIO_Format format = teeSource->format();
2996 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002997 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002998 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002999 wavHeader[22] = channelCount; // number of channels
3000 wavHeader[24] = sampleRate; // sample rate
3001 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07003002 wavHeader[32] = frameSize; // block alignment
3003 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003004 write(teeFd, wavHeader, sizeof(wavHeader));
3005 size_t total = 0;
3006 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003007#define TEE_SINK_READ 1024 // frames per I/O operation
3008 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003009 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003010 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003011 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003012 bool wasFirstRead = firstRead;
3013 firstRead = false;
3014 if (actual <= 0) {
3015 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3016 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003017 }
Eric Laurent81784c32012-11-19 14:55:58 -08003018 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003019 }
Eric Laurent81784c32012-11-19 14:55:58 -08003020 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003021 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003022 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003023 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003024 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003025 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003026 uint32_t temp = 44 + total * frameSize - 8;
3027 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003028 write(teeFd, &temp, sizeof(temp));
3029 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003030 temp = total * frameSize;
3031 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003032 write(teeFd, &temp, sizeof(temp));
3033 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003034 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003035 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003036 }
Eric Laurent59255e42011-07-27 19:49:51 -07003037 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003038 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003039 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003040 }
Eric Laurent59255e42011-07-27 19:49:51 -07003041 }
3042 }
3043}
Glenn Kasten46909e72013-02-26 09:20:22 -08003044#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003045
Mathias Agopian65ab4712010-07-14 17:59:35 -07003046// ----------------------------------------------------------------------------
3047
3048status_t AudioFlinger::onTransact(
3049 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3050{
3051 return BnAudioFlinger::onTransact(code, data, reply, flags);
3052}
3053
Glenn Kasten63238ef2015-03-02 15:50:29 -08003054} // namespace android